blob: a095970a1ee9fbe7a4989cab935d9622124e88ba [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800102 Register reg = calling_convention.GetRegisterAt(gp_index);
103 if (reg == A1 || reg == A3) {
104 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200105 gp_index++;
106 }
107 Register low_even = calling_convention.GetRegisterAt(gp_index);
108 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
109 DCHECK_EQ(low_even + 1, high_odd);
110 next_location = Location::RegisterPairLocation(low_even, high_odd);
111 } else {
112 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
113 next_location = Location::DoubleStackSlot(stack_offset);
114 }
115 break;
116 }
117
118 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
119 // will take up the even/odd pair, while floats are stored in even regs only.
120 // On 64 bit FPU, both double and float are stored in even registers only.
121 case Primitive::kPrimFloat:
122 case Primitive::kPrimDouble: {
123 uint32_t float_index = float_index_++;
124 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
125 next_location = Location::FpuRegisterLocation(
126 calling_convention.GetFpuRegisterAt(float_index));
127 } else {
128 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
129 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
130 : Location::StackSlot(stack_offset);
131 }
132 break;
133 }
134
135 case Primitive::kPrimVoid:
136 LOG(FATAL) << "Unexpected parameter type " << type;
137 break;
138 }
139
140 // Space on the stack is reserved for all arguments.
141 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
142
143 return next_location;
144}
145
146Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
147 return MipsReturnLocation(type);
148}
149
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100150// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
151#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700152#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200153
154class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
155 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000156 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 LocationSummary* locations = instruction_->GetLocations();
160 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
161 __ Bind(GetEntryLabel());
162 if (instruction_->CanThrowIntoCatchBlock()) {
163 // Live registers will be restored in the catch block if caught.
164 SaveLiveRegisters(codegen, instruction_->GetLocations());
165 }
166 // We're moving two locations to locations that could overlap, so we need a parallel
167 // move resolver.
168 InvokeRuntimeCallingConvention calling_convention;
169 codegen->EmitParallelMoves(locations->InAt(0),
170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
171 Primitive::kPrimInt,
172 locations->InAt(1),
173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100175 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
176 ? kQuickThrowStringBounds
177 : kQuickThrowArrayBounds;
178 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100179 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
181 }
182
183 bool IsFatal() const OVERRIDE { return true; }
184
185 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
186
187 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
189};
190
191class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
192 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000193 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200194
195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
196 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
197 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100198 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200199 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
200 }
201
202 bool IsFatal() const OVERRIDE { return true; }
203
204 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
205
206 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
208};
209
210class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
211 public:
212 LoadClassSlowPathMIPS(HLoadClass* cls,
213 HInstruction* at,
214 uint32_t dex_pc,
215 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000216 : SlowPathCodeMIPS(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
218 }
219
220 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000221 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200222 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
223
224 __ Bind(GetEntryLabel());
225 SaveLiveRegisters(codegen, locations);
226
227 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000228 dex::TypeIndex type_index = cls_->GetTypeIndex();
229 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230
Serban Constantinescufca16662016-07-14 09:21:59 +0100231 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
232 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000233 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200234 if (do_clinit_) {
235 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
236 } else {
237 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
238 }
239
240 // Move the class to the desired location.
241 Location out = locations->Out();
242 if (out.IsValid()) {
243 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 Primitive::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200245 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
246 }
247
248 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
250 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
251 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
252 DCHECK(out.IsValid());
253 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
254 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
255 // so that we can avoid another calculation here.
256 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
257 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
258 DCHECK_NE(out.AsRegister<Register>(), AT);
259 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000260 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800261 bool reordering = __ SetReorder(false);
262 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
263 __ StoreToOffset(kStoreWord, out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
264 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000265 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200266 __ B(GetExitLabel());
267 }
268
269 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
270
271 private:
272 // The class this slow path will load.
273 HLoadClass* const cls_;
274
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200275 // The dex PC of `at_`.
276 const uint32_t dex_pc_;
277
278 // Whether to initialize the class.
279 const bool do_clinit_;
280
281 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
282};
283
284class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
285 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
289 LocationSummary* locations = instruction_->GetLocations();
290 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
291 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
292
293 __ Bind(GetEntryLabel());
294 SaveLiveRegisters(codegen, locations);
295
296 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000297 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000298 const dex::StringIndex string_index = load->GetStringIndex();
299 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100300 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
302 Primitive::Type type = instruction_->GetType();
303 mips_codegen->MoveLocation(locations->Out(),
304 calling_convention.GetReturnLocation(type),
305 type);
306
307 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000308
309 // Store the resolved String to the BSS entry.
310 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
311 // .bss entry address in the fast path, so that we can avoid another calculation here.
312 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
313 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
314 Register out = locations->Out().AsRegister<Register>();
315 DCHECK_NE(out, AT);
316 CodeGeneratorMIPS::PcRelativePatchInfo* info =
317 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800318 bool reordering = __ SetReorder(false);
319 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
320 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
321 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000322
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200323 __ B(GetExitLabel());
324 }
325
326 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
327
328 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
330};
331
332class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
333 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000334 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
338 __ Bind(GetEntryLabel());
339 if (instruction_->CanThrowIntoCatchBlock()) {
340 // Live registers will be restored in the catch block if caught.
341 SaveLiveRegisters(codegen, instruction_->GetLocations());
342 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100343 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200344 instruction_,
345 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100346 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
348 }
349
350 bool IsFatal() const OVERRIDE { return true; }
351
352 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
353
354 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
356};
357
358class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
359 public:
360 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000361 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200362
363 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
364 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
365 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100366 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200367 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200368 if (successor_ == nullptr) {
369 __ B(GetReturnLabel());
370 } else {
371 __ B(mips_codegen->GetLabelOf(successor_));
372 }
373 }
374
375 MipsLabel* GetReturnLabel() {
376 DCHECK(successor_ == nullptr);
377 return &return_label_;
378 }
379
380 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
381
382 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200383 // If not null, the block to branch to after the suspend check.
384 HBasicBlock* const successor_;
385
386 // If `successor_` is null, the label to branch to after the suspend check.
387 MipsLabel return_label_;
388
389 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
390};
391
392class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
393 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200395
396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
397 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200398 uint32_t dex_pc = instruction_->GetDexPc();
399 DCHECK(instruction_->IsCheckCast()
400 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
401 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
402
403 __ Bind(GetEntryLabel());
404 SaveLiveRegisters(codegen, locations);
405
406 // We're moving two locations to locations that could overlap, so we need a parallel
407 // move resolver.
408 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800409 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
411 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800412 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
414 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100416 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800417 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 Primitive::Type ret_type = instruction_->GetType();
419 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
420 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 } else {
422 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800423 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
424 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425 }
426
427 RestoreLiveRegisters(codegen, locations);
428 __ B(GetExitLabel());
429 }
430
431 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
432
433 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
435};
436
437class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
438 public:
Aart Bik42249c32016-01-07 15:33:50 -0800439 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000440 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441
442 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800443 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100445 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000446 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 }
448
449 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
450
451 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200452 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
453};
454
455CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
456 const MipsInstructionSetFeatures& isa_features,
457 const CompilerOptions& compiler_options,
458 OptimizingCompilerStats* stats)
459 : CodeGenerator(graph,
460 kNumberOfCoreRegisters,
461 kNumberOfFRegisters,
462 kNumberOfRegisterPairs,
463 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
464 arraysize(kCoreCalleeSaves)),
465 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
466 arraysize(kFpuCalleeSaves)),
467 compiler_options,
468 stats),
469 block_labels_(nullptr),
470 location_builder_(graph, this),
471 instruction_visitor_(graph, this),
472 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100473 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700474 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700475 uint32_literals_(std::less<uint32_t>(),
476 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700477 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
478 boot_image_string_patches_(StringReferenceValueComparator(),
479 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
480 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
481 boot_image_type_patches_(TypeReferenceValueComparator(),
482 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
483 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000484 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700485 boot_image_address_patches_(std::less<uint32_t>(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
487 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200488 // Save RA (containing the return address) to mimic Quick.
489 AddAllocatedRegister(Location::RegisterLocation(RA));
490}
491
492#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100493// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
494#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700495#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200496
497void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
498 // Ensure that we fix up branches.
499 __ FinalizeCode();
500
501 // Adjust native pc offsets in stack maps.
502 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800503 uint32_t old_position =
504 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200505 uint32_t new_position = __ GetAdjustedPosition(old_position);
506 DCHECK_GE(new_position, old_position);
507 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
508 }
509
510 // Adjust pc offsets for the disassembly information.
511 if (disasm_info_ != nullptr) {
512 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
513 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
514 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
515 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
516 it.second.start = __ GetAdjustedPosition(it.second.start);
517 it.second.end = __ GetAdjustedPosition(it.second.end);
518 }
519 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
520 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
521 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
522 }
523 }
524
525 CodeGenerator::Finalize(allocator);
526}
527
528MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
529 return codegen_->GetAssembler();
530}
531
532void ParallelMoveResolverMIPS::EmitMove(size_t index) {
533 DCHECK_LT(index, moves_.size());
534 MoveOperands* move = moves_[index];
535 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
536}
537
538void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
539 DCHECK_LT(index, moves_.size());
540 MoveOperands* move = moves_[index];
541 Primitive::Type type = move->GetType();
542 Location loc1 = move->GetDestination();
543 Location loc2 = move->GetSource();
544
545 DCHECK(!loc1.IsConstant());
546 DCHECK(!loc2.IsConstant());
547
548 if (loc1.Equals(loc2)) {
549 return;
550 }
551
552 if (loc1.IsRegister() && loc2.IsRegister()) {
553 // Swap 2 GPRs.
554 Register r1 = loc1.AsRegister<Register>();
555 Register r2 = loc2.AsRegister<Register>();
556 __ Move(TMP, r2);
557 __ Move(r2, r1);
558 __ Move(r1, TMP);
559 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
560 FRegister f1 = loc1.AsFpuRegister<FRegister>();
561 FRegister f2 = loc2.AsFpuRegister<FRegister>();
562 if (type == Primitive::kPrimFloat) {
563 __ MovS(FTMP, f2);
564 __ MovS(f2, f1);
565 __ MovS(f1, FTMP);
566 } else {
567 DCHECK_EQ(type, Primitive::kPrimDouble);
568 __ MovD(FTMP, f2);
569 __ MovD(f2, f1);
570 __ MovD(f1, FTMP);
571 }
572 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
573 (loc1.IsFpuRegister() && loc2.IsRegister())) {
574 // Swap FPR and GPR.
575 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
576 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
577 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200578 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200579 __ Move(TMP, r2);
580 __ Mfc1(r2, f1);
581 __ Mtc1(TMP, f1);
582 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
583 // Swap 2 GPR register pairs.
584 Register r1 = loc1.AsRegisterPairLow<Register>();
585 Register r2 = loc2.AsRegisterPairLow<Register>();
586 __ Move(TMP, r2);
587 __ Move(r2, r1);
588 __ Move(r1, TMP);
589 r1 = loc1.AsRegisterPairHigh<Register>();
590 r2 = loc2.AsRegisterPairHigh<Register>();
591 __ Move(TMP, r2);
592 __ Move(r2, r1);
593 __ Move(r1, TMP);
594 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
595 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
596 // Swap FPR and GPR register pair.
597 DCHECK_EQ(type, Primitive::kPrimDouble);
598 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
599 : loc2.AsFpuRegister<FRegister>();
600 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
601 : loc2.AsRegisterPairLow<Register>();
602 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
603 : loc2.AsRegisterPairHigh<Register>();
604 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
605 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
606 // unpredictable and the following mfch1 will fail.
607 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800608 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200609 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800610 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200611 __ Move(r2_l, TMP);
612 __ Move(r2_h, AT);
613 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
614 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
615 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
618 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200619 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
620 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000621 __ Move(TMP, reg);
622 __ LoadFromOffset(kLoadWord, reg, SP, offset);
623 __ StoreToOffset(kStoreWord, TMP, SP, offset);
624 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
625 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
626 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
627 : loc2.AsRegisterPairLow<Register>();
628 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
629 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200630 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000631 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
632 : loc2.GetHighStackIndex(kMipsWordSize);
633 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000634 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000635 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000636 __ Move(TMP, reg_h);
637 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
638 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200639 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
640 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
641 : loc2.AsFpuRegister<FRegister>();
642 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
643 if (type == Primitive::kPrimFloat) {
644 __ MovS(FTMP, reg);
645 __ LoadSFromOffset(reg, SP, offset);
646 __ StoreSToOffset(FTMP, SP, offset);
647 } else {
648 DCHECK_EQ(type, Primitive::kPrimDouble);
649 __ MovD(FTMP, reg);
650 __ LoadDFromOffset(reg, SP, offset);
651 __ StoreDToOffset(FTMP, SP, offset);
652 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200653 } else {
654 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
655 }
656}
657
658void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
659 __ Pop(static_cast<Register>(reg));
660}
661
662void ParallelMoveResolverMIPS::SpillScratch(int reg) {
663 __ Push(static_cast<Register>(reg));
664}
665
666void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
667 // Allocate a scratch register other than TMP, if available.
668 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
669 // automatically unspilled when the scratch scope object is destroyed).
670 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
671 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
672 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
673 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
674 __ LoadFromOffset(kLoadWord,
675 Register(ensure_scratch.GetRegister()),
676 SP,
677 index1 + stack_offset);
678 __ LoadFromOffset(kLoadWord,
679 TMP,
680 SP,
681 index2 + stack_offset);
682 __ StoreToOffset(kStoreWord,
683 Register(ensure_scratch.GetRegister()),
684 SP,
685 index2 + stack_offset);
686 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
687 }
688}
689
Alexey Frunze73296a72016-06-03 22:51:46 -0700690void CodeGeneratorMIPS::ComputeSpillMask() {
691 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
692 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
693 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
694 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
695 // registers, include the ZERO register to force alignment of FPU callee-saved registers
696 // within the stack frame.
697 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
698 core_spill_mask_ |= (1 << ZERO);
699 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700700}
701
702bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700703 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700704 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
705 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
706 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700707 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
708 // saved in an unused temporary register) and saving of RA and the current method pointer
709 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700710 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700711}
712
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200713static dwarf::Reg DWARFReg(Register reg) {
714 return dwarf::Reg::MipsCore(static_cast<int>(reg));
715}
716
717// TODO: mapping of floating-point registers to DWARF.
718
719void CodeGeneratorMIPS::GenerateFrameEntry() {
720 __ Bind(&frame_entry_label_);
721
722 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
723
724 if (do_overflow_check) {
725 __ LoadFromOffset(kLoadWord,
726 ZERO,
727 SP,
728 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
729 RecordPcInfo(nullptr, 0);
730 }
731
732 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700733 CHECK_EQ(fpu_spill_mask_, 0u);
734 CHECK_EQ(core_spill_mask_, 1u << RA);
735 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200736 return;
737 }
738
739 // Make sure the frame size isn't unreasonably large.
740 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
741 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
742 }
743
744 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200747 __ IncreaseFrameSize(ofs);
748
Alexey Frunze73296a72016-06-03 22:51:46 -0700749 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
750 Register reg = static_cast<Register>(MostSignificantBit(mask));
751 mask ^= 1u << reg;
752 ofs -= kMipsWordSize;
753 // The ZERO register is only included for alignment.
754 if (reg != ZERO) {
755 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756 __ cfi().RelOffset(DWARFReg(reg), ofs);
757 }
758 }
759
Alexey Frunze73296a72016-06-03 22:51:46 -0700760 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
761 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
762 mask ^= 1u << reg;
763 ofs -= kMipsDoublewordSize;
764 __ StoreDToOffset(reg, SP, ofs);
765 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766 }
767
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100768 // Save the current method if we need it. Note that we do not
769 // do this in HCurrentMethod, as the instruction might have been removed
770 // in the SSA graph.
771 if (RequiresCurrentMethod()) {
772 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
773 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100774
775 if (GetGraph()->HasShouldDeoptimizeFlag()) {
776 // Initialize should deoptimize flag to 0.
777 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
778 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200779}
780
781void CodeGeneratorMIPS::GenerateFrameExit() {
782 __ cfi().RememberState();
783
784 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200785 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200786
Alexey Frunze73296a72016-06-03 22:51:46 -0700787 // For better instruction scheduling restore RA before other registers.
788 uint32_t ofs = GetFrameSize();
789 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
790 Register reg = static_cast<Register>(MostSignificantBit(mask));
791 mask ^= 1u << reg;
792 ofs -= kMipsWordSize;
793 // The ZERO register is only included for alignment.
794 if (reg != ZERO) {
795 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200796 __ cfi().Restore(DWARFReg(reg));
797 }
798 }
799
Alexey Frunze73296a72016-06-03 22:51:46 -0700800 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
801 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
802 mask ^= 1u << reg;
803 ofs -= kMipsDoublewordSize;
804 __ LoadDFromOffset(reg, SP, ofs);
805 // TODO: __ cfi().Restore(DWARFReg(reg));
806 }
807
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700808 size_t frame_size = GetFrameSize();
809 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
810 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
811 bool reordering = __ SetReorder(false);
812 if (exchange) {
813 __ Jr(RA);
814 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
815 } else {
816 __ DecreaseFrameSize(frame_size);
817 __ Jr(RA);
818 __ Nop(); // In delay slot.
819 }
820 __ SetReorder(reordering);
821 } else {
822 __ Jr(RA);
823 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200824 }
825
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200826 __ cfi().RestoreState();
827 __ cfi().DefCFAOffset(GetFrameSize());
828}
829
830void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
831 __ Bind(GetLabelOf(block));
832}
833
834void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
835 if (src.Equals(dst)) {
836 return;
837 }
838
839 if (src.IsConstant()) {
840 MoveConstant(dst, src.GetConstant());
841 } else {
842 if (Primitive::Is64BitType(dst_type)) {
843 Move64(dst, src);
844 } else {
845 Move32(dst, src);
846 }
847 }
848}
849
850void CodeGeneratorMIPS::Move32(Location destination, Location source) {
851 if (source.Equals(destination)) {
852 return;
853 }
854
855 if (destination.IsRegister()) {
856 if (source.IsRegister()) {
857 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
858 } else if (source.IsFpuRegister()) {
859 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
860 } else {
861 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
862 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
863 }
864 } else if (destination.IsFpuRegister()) {
865 if (source.IsRegister()) {
866 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
867 } else if (source.IsFpuRegister()) {
868 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
869 } else {
870 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
871 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
872 }
873 } else {
874 DCHECK(destination.IsStackSlot()) << destination;
875 if (source.IsRegister()) {
876 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
877 } else if (source.IsFpuRegister()) {
878 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
879 } else {
880 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
881 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
882 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
883 }
884 }
885}
886
887void CodeGeneratorMIPS::Move64(Location destination, Location source) {
888 if (source.Equals(destination)) {
889 return;
890 }
891
892 if (destination.IsRegisterPair()) {
893 if (source.IsRegisterPair()) {
894 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
895 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
896 } else if (source.IsFpuRegister()) {
897 Register dst_high = destination.AsRegisterPairHigh<Register>();
898 Register dst_low = destination.AsRegisterPairLow<Register>();
899 FRegister src = source.AsFpuRegister<FRegister>();
900 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800901 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200902 } else {
903 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
904 int32_t off = source.GetStackIndex();
905 Register r = destination.AsRegisterPairLow<Register>();
906 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
907 }
908 } else if (destination.IsFpuRegister()) {
909 if (source.IsRegisterPair()) {
910 FRegister dst = destination.AsFpuRegister<FRegister>();
911 Register src_high = source.AsRegisterPairHigh<Register>();
912 Register src_low = source.AsRegisterPairLow<Register>();
913 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800914 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200915 } else if (source.IsFpuRegister()) {
916 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
917 } else {
918 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
919 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
920 }
921 } else {
922 DCHECK(destination.IsDoubleStackSlot()) << destination;
923 int32_t off = destination.GetStackIndex();
924 if (source.IsRegisterPair()) {
925 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
926 } else if (source.IsFpuRegister()) {
927 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
928 } else {
929 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
930 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
931 __ StoreToOffset(kStoreWord, TMP, SP, off);
932 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
933 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
934 }
935 }
936}
937
938void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
939 if (c->IsIntConstant() || c->IsNullConstant()) {
940 // Move 32 bit constant.
941 int32_t value = GetInt32ValueOf(c);
942 if (destination.IsRegister()) {
943 Register dst = destination.AsRegister<Register>();
944 __ LoadConst32(dst, value);
945 } else {
946 DCHECK(destination.IsStackSlot())
947 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700948 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200949 }
950 } else if (c->IsLongConstant()) {
951 // Move 64 bit constant.
952 int64_t value = GetInt64ValueOf(c);
953 if (destination.IsRegisterPair()) {
954 Register r_h = destination.AsRegisterPairHigh<Register>();
955 Register r_l = destination.AsRegisterPairLow<Register>();
956 __ LoadConst64(r_h, r_l, value);
957 } else {
958 DCHECK(destination.IsDoubleStackSlot())
959 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700960 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200961 }
962 } else if (c->IsFloatConstant()) {
963 // Move 32 bit float constant.
964 int32_t value = GetInt32ValueOf(c);
965 if (destination.IsFpuRegister()) {
966 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
967 } else {
968 DCHECK(destination.IsStackSlot())
969 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700970 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200971 }
972 } else {
973 // Move 64 bit double constant.
974 DCHECK(c->IsDoubleConstant()) << c->DebugName();
975 int64_t value = GetInt64ValueOf(c);
976 if (destination.IsFpuRegister()) {
977 FRegister fd = destination.AsFpuRegister<FRegister>();
978 __ LoadDConst64(fd, value, TMP);
979 } else {
980 DCHECK(destination.IsDoubleStackSlot())
981 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700982 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200983 }
984 }
985}
986
987void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
988 DCHECK(destination.IsRegister());
989 Register dst = destination.AsRegister<Register>();
990 __ LoadConst32(dst, value);
991}
992
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200993void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
994 if (location.IsRegister()) {
995 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700996 } else if (location.IsRegisterPair()) {
997 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
998 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200999 } else {
1000 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1001 }
1002}
1003
Vladimir Markoaad75c62016-10-03 08:46:48 +00001004template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1005inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1006 const ArenaDeque<PcRelativePatchInfo>& infos,
1007 ArenaVector<LinkerPatch>* linker_patches) {
1008 for (const PcRelativePatchInfo& info : infos) {
1009 const DexFile& dex_file = info.target_dex_file;
1010 size_t offset_or_index = info.offset_or_index;
1011 DCHECK(info.high_label.IsBound());
1012 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1013 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1014 // the assembler's base label used for PC-relative addressing.
1015 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1016 ? __ GetLabelLocation(&info.pc_rel_label)
1017 : __ GetPcRelBaseLabelLocation();
1018 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1019 }
1020}
1021
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001022void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1023 DCHECK(linker_patches->empty());
1024 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001025 pc_relative_dex_cache_patches_.size() +
1026 pc_relative_string_patches_.size() +
1027 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001028 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001029 boot_image_string_patches_.size() +
1030 boot_image_type_patches_.size() +
1031 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001032 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001033 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1034 linker_patches);
1035 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001036 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001037 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1038 linker_patches);
1039 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001040 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1041 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001042 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1043 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001044 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001045 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1046 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001047 for (const auto& entry : boot_image_string_patches_) {
1048 const StringReference& target_string = entry.first;
1049 Literal* literal = entry.second;
1050 DCHECK(literal->GetLabel()->IsBound());
1051 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1052 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1053 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001054 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001055 }
1056 for (const auto& entry : boot_image_type_patches_) {
1057 const TypeReference& target_type = entry.first;
1058 Literal* literal = entry.second;
1059 DCHECK(literal->GetLabel()->IsBound());
1060 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1061 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1062 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001063 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001064 }
1065 for (const auto& entry : boot_image_address_patches_) {
1066 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1067 Literal* literal = entry.second;
1068 DCHECK(literal->GetLabel()->IsBound());
1069 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1070 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1071 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001072 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001073}
1074
1075CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001076 const DexFile& dex_file, dex::StringIndex string_index) {
1077 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001078}
1079
1080CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001081 const DexFile& dex_file, dex::TypeIndex type_index) {
1082 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001083}
1084
Vladimir Marko1998cd02017-01-13 13:02:58 +00001085CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1086 const DexFile& dex_file, dex::TypeIndex type_index) {
1087 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1088}
1089
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001090CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1091 const DexFile& dex_file, uint32_t element_offset) {
1092 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1093}
1094
1095CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1096 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1097 patches->emplace_back(dex_file, offset_or_index);
1098 return &patches->back();
1099}
1100
Alexey Frunze06a46c42016-07-19 15:00:40 -07001101Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1102 return map->GetOrCreate(
1103 value,
1104 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1105}
1106
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001107Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1108 MethodToLiteralMap* map) {
1109 return map->GetOrCreate(
1110 target_method,
1111 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1112}
1113
Alexey Frunze06a46c42016-07-19 15:00:40 -07001114Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001115 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001116 return boot_image_string_patches_.GetOrCreate(
1117 StringReference(&dex_file, string_index),
1118 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1119}
1120
1121Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001122 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001123 return boot_image_type_patches_.GetOrCreate(
1124 TypeReference(&dex_file, type_index),
1125 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1126}
1127
1128Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1129 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1130 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1131 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1132}
1133
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001134void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1135 Register out,
1136 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001137 if (GetInstructionSetFeatures().IsR6()) {
1138 DCHECK_EQ(base, ZERO);
1139 __ Bind(&info->high_label);
1140 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001141 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001142 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001143 } else {
1144 // If base is ZERO, emit NAL to obtain the actual base.
1145 if (base == ZERO) {
1146 // Generate a dummy PC-relative call to obtain PC.
1147 __ Nal();
1148 }
1149 __ Bind(&info->high_label);
1150 __ Lui(out, /* placeholder */ 0x1234);
1151 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1152 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1153 if (base == ZERO) {
1154 __ Bind(&info->pc_rel_label);
1155 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001156 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001157 __ Addu(out, out, (base == ZERO) ? RA : base);
1158 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001159 // The immediately following instruction will add the sign-extended low half of the 32-bit
1160 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001161}
1162
Goran Jakovljevice114da22016-12-26 14:21:43 +01001163void CodeGeneratorMIPS::MarkGCCard(Register object,
1164 Register value,
1165 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001166 MipsLabel done;
1167 Register card = AT;
1168 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001169 if (value_can_be_null) {
1170 __ Beqz(value, &done);
1171 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001172 __ LoadFromOffset(kLoadWord,
1173 card,
1174 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001175 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001176 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1177 __ Addu(temp, card, temp);
1178 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001179 if (value_can_be_null) {
1180 __ Bind(&done);
1181 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001182}
1183
David Brazdil58282f42016-01-14 12:45:10 +00001184void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001185 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1186 blocked_core_registers_[ZERO] = true;
1187 blocked_core_registers_[K0] = true;
1188 blocked_core_registers_[K1] = true;
1189 blocked_core_registers_[GP] = true;
1190 blocked_core_registers_[SP] = true;
1191 blocked_core_registers_[RA] = true;
1192
1193 // AT and TMP(T8) are used as temporary/scratch registers
1194 // (similar to how AT is used by MIPS assemblers).
1195 blocked_core_registers_[AT] = true;
1196 blocked_core_registers_[TMP] = true;
1197 blocked_fpu_registers_[FTMP] = true;
1198
1199 // Reserve suspend and thread registers.
1200 blocked_core_registers_[S0] = true;
1201 blocked_core_registers_[TR] = true;
1202
1203 // Reserve T9 for function calls
1204 blocked_core_registers_[T9] = true;
1205
1206 // Reserve odd-numbered FPU registers.
1207 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1208 blocked_fpu_registers_[i] = true;
1209 }
1210
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001211 if (GetGraph()->IsDebuggable()) {
1212 // Stubs do not save callee-save floating point registers. If the graph
1213 // is debuggable, we need to deal with these registers differently. For
1214 // now, just block them.
1215 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1216 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1217 }
1218 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001219}
1220
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001221size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1222 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1223 return kMipsWordSize;
1224}
1225
1226size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1227 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1228 return kMipsWordSize;
1229}
1230
1231size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1232 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1233 return kMipsDoublewordSize;
1234}
1235
1236size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1237 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1238 return kMipsDoublewordSize;
1239}
1240
1241void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001242 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001243}
1244
1245void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001246 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001247}
1248
Serban Constantinescufca16662016-07-14 09:21:59 +01001249constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1250
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001251void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1252 HInstruction* instruction,
1253 uint32_t dex_pc,
1254 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001255 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001256 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001257 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001258 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001259 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001260 // Reserve argument space on stack (for $a0-$a3) for
1261 // entrypoints that directly reference native implementations.
1262 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001263 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001264 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001265 } else {
1266 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001267 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001268 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001269 if (EntrypointRequiresStackMap(entrypoint)) {
1270 RecordPcInfo(instruction, dex_pc, slow_path);
1271 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001272}
1273
1274void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1275 Register class_reg) {
1276 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1277 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1278 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1279 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1280 __ Sync(0);
1281 __ Bind(slow_path->GetExitLabel());
1282}
1283
1284void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1285 __ Sync(0); // Only stype 0 is supported.
1286}
1287
1288void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1289 HBasicBlock* successor) {
1290 SuspendCheckSlowPathMIPS* slow_path =
1291 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1292 codegen_->AddSlowPath(slow_path);
1293
1294 __ LoadFromOffset(kLoadUnsignedHalfword,
1295 TMP,
1296 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001297 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001298 if (successor == nullptr) {
1299 __ Bnez(TMP, slow_path->GetEntryLabel());
1300 __ Bind(slow_path->GetReturnLabel());
1301 } else {
1302 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1303 __ B(slow_path->GetEntryLabel());
1304 // slow_path will return to GetLabelOf(successor).
1305 }
1306}
1307
1308InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1309 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001310 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 assembler_(codegen->GetAssembler()),
1312 codegen_(codegen) {}
1313
1314void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1315 DCHECK_EQ(instruction->InputCount(), 2U);
1316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1317 Primitive::Type type = instruction->GetResultType();
1318 switch (type) {
1319 case Primitive::kPrimInt: {
1320 locations->SetInAt(0, Location::RequiresRegister());
1321 HInstruction* right = instruction->InputAt(1);
1322 bool can_use_imm = false;
1323 if (right->IsConstant()) {
1324 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1325 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1326 can_use_imm = IsUint<16>(imm);
1327 } else if (instruction->IsAdd()) {
1328 can_use_imm = IsInt<16>(imm);
1329 } else {
1330 DCHECK(instruction->IsSub());
1331 can_use_imm = IsInt<16>(-imm);
1332 }
1333 }
1334 if (can_use_imm)
1335 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1336 else
1337 locations->SetInAt(1, Location::RequiresRegister());
1338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1339 break;
1340 }
1341
1342 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001343 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001344 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001346 break;
1347 }
1348
1349 case Primitive::kPrimFloat:
1350 case Primitive::kPrimDouble:
1351 DCHECK(instruction->IsAdd() || instruction->IsSub());
1352 locations->SetInAt(0, Location::RequiresFpuRegister());
1353 locations->SetInAt(1, Location::RequiresFpuRegister());
1354 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1355 break;
1356
1357 default:
1358 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1359 }
1360}
1361
1362void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1363 Primitive::Type type = instruction->GetType();
1364 LocationSummary* locations = instruction->GetLocations();
1365
1366 switch (type) {
1367 case Primitive::kPrimInt: {
1368 Register dst = locations->Out().AsRegister<Register>();
1369 Register lhs = locations->InAt(0).AsRegister<Register>();
1370 Location rhs_location = locations->InAt(1);
1371
1372 Register rhs_reg = ZERO;
1373 int32_t rhs_imm = 0;
1374 bool use_imm = rhs_location.IsConstant();
1375 if (use_imm) {
1376 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1377 } else {
1378 rhs_reg = rhs_location.AsRegister<Register>();
1379 }
1380
1381 if (instruction->IsAnd()) {
1382 if (use_imm)
1383 __ Andi(dst, lhs, rhs_imm);
1384 else
1385 __ And(dst, lhs, rhs_reg);
1386 } else if (instruction->IsOr()) {
1387 if (use_imm)
1388 __ Ori(dst, lhs, rhs_imm);
1389 else
1390 __ Or(dst, lhs, rhs_reg);
1391 } else if (instruction->IsXor()) {
1392 if (use_imm)
1393 __ Xori(dst, lhs, rhs_imm);
1394 else
1395 __ Xor(dst, lhs, rhs_reg);
1396 } else if (instruction->IsAdd()) {
1397 if (use_imm)
1398 __ Addiu(dst, lhs, rhs_imm);
1399 else
1400 __ Addu(dst, lhs, rhs_reg);
1401 } else {
1402 DCHECK(instruction->IsSub());
1403 if (use_imm)
1404 __ Addiu(dst, lhs, -rhs_imm);
1405 else
1406 __ Subu(dst, lhs, rhs_reg);
1407 }
1408 break;
1409 }
1410
1411 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001412 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1413 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1414 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1415 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001416 Location rhs_location = locations->InAt(1);
1417 bool use_imm = rhs_location.IsConstant();
1418 if (!use_imm) {
1419 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1420 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1421 if (instruction->IsAnd()) {
1422 __ And(dst_low, lhs_low, rhs_low);
1423 __ And(dst_high, lhs_high, rhs_high);
1424 } else if (instruction->IsOr()) {
1425 __ Or(dst_low, lhs_low, rhs_low);
1426 __ Or(dst_high, lhs_high, rhs_high);
1427 } else if (instruction->IsXor()) {
1428 __ Xor(dst_low, lhs_low, rhs_low);
1429 __ Xor(dst_high, lhs_high, rhs_high);
1430 } else if (instruction->IsAdd()) {
1431 if (lhs_low == rhs_low) {
1432 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1433 __ Slt(TMP, lhs_low, ZERO);
1434 __ Addu(dst_low, lhs_low, rhs_low);
1435 } else {
1436 __ Addu(dst_low, lhs_low, rhs_low);
1437 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1438 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1439 }
1440 __ Addu(dst_high, lhs_high, rhs_high);
1441 __ Addu(dst_high, dst_high, TMP);
1442 } else {
1443 DCHECK(instruction->IsSub());
1444 __ Sltu(TMP, lhs_low, rhs_low);
1445 __ Subu(dst_low, lhs_low, rhs_low);
1446 __ Subu(dst_high, lhs_high, rhs_high);
1447 __ Subu(dst_high, dst_high, TMP);
1448 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001449 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001450 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1451 if (instruction->IsOr()) {
1452 uint32_t low = Low32Bits(value);
1453 uint32_t high = High32Bits(value);
1454 if (IsUint<16>(low)) {
1455 if (dst_low != lhs_low || low != 0) {
1456 __ Ori(dst_low, lhs_low, low);
1457 }
1458 } else {
1459 __ LoadConst32(TMP, low);
1460 __ Or(dst_low, lhs_low, TMP);
1461 }
1462 if (IsUint<16>(high)) {
1463 if (dst_high != lhs_high || high != 0) {
1464 __ Ori(dst_high, lhs_high, high);
1465 }
1466 } else {
1467 if (high != low) {
1468 __ LoadConst32(TMP, high);
1469 }
1470 __ Or(dst_high, lhs_high, TMP);
1471 }
1472 } else if (instruction->IsXor()) {
1473 uint32_t low = Low32Bits(value);
1474 uint32_t high = High32Bits(value);
1475 if (IsUint<16>(low)) {
1476 if (dst_low != lhs_low || low != 0) {
1477 __ Xori(dst_low, lhs_low, low);
1478 }
1479 } else {
1480 __ LoadConst32(TMP, low);
1481 __ Xor(dst_low, lhs_low, TMP);
1482 }
1483 if (IsUint<16>(high)) {
1484 if (dst_high != lhs_high || high != 0) {
1485 __ Xori(dst_high, lhs_high, high);
1486 }
1487 } else {
1488 if (high != low) {
1489 __ LoadConst32(TMP, high);
1490 }
1491 __ Xor(dst_high, lhs_high, TMP);
1492 }
1493 } else if (instruction->IsAnd()) {
1494 uint32_t low = Low32Bits(value);
1495 uint32_t high = High32Bits(value);
1496 if (IsUint<16>(low)) {
1497 __ Andi(dst_low, lhs_low, low);
1498 } else if (low != 0xFFFFFFFF) {
1499 __ LoadConst32(TMP, low);
1500 __ And(dst_low, lhs_low, TMP);
1501 } else if (dst_low != lhs_low) {
1502 __ Move(dst_low, lhs_low);
1503 }
1504 if (IsUint<16>(high)) {
1505 __ Andi(dst_high, lhs_high, high);
1506 } else if (high != 0xFFFFFFFF) {
1507 if (high != low) {
1508 __ LoadConst32(TMP, high);
1509 }
1510 __ And(dst_high, lhs_high, TMP);
1511 } else if (dst_high != lhs_high) {
1512 __ Move(dst_high, lhs_high);
1513 }
1514 } else {
1515 if (instruction->IsSub()) {
1516 value = -value;
1517 } else {
1518 DCHECK(instruction->IsAdd());
1519 }
1520 int32_t low = Low32Bits(value);
1521 int32_t high = High32Bits(value);
1522 if (IsInt<16>(low)) {
1523 if (dst_low != lhs_low || low != 0) {
1524 __ Addiu(dst_low, lhs_low, low);
1525 }
1526 if (low != 0) {
1527 __ Sltiu(AT, dst_low, low);
1528 }
1529 } else {
1530 __ LoadConst32(TMP, low);
1531 __ Addu(dst_low, lhs_low, TMP);
1532 __ Sltu(AT, dst_low, TMP);
1533 }
1534 if (IsInt<16>(high)) {
1535 if (dst_high != lhs_high || high != 0) {
1536 __ Addiu(dst_high, lhs_high, high);
1537 }
1538 } else {
1539 if (high != low) {
1540 __ LoadConst32(TMP, high);
1541 }
1542 __ Addu(dst_high, lhs_high, TMP);
1543 }
1544 if (low != 0) {
1545 __ Addu(dst_high, dst_high, AT);
1546 }
1547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 }
1549 break;
1550 }
1551
1552 case Primitive::kPrimFloat:
1553 case Primitive::kPrimDouble: {
1554 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1555 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1556 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1557 if (instruction->IsAdd()) {
1558 if (type == Primitive::kPrimFloat) {
1559 __ AddS(dst, lhs, rhs);
1560 } else {
1561 __ AddD(dst, lhs, rhs);
1562 }
1563 } else {
1564 DCHECK(instruction->IsSub());
1565 if (type == Primitive::kPrimFloat) {
1566 __ SubS(dst, lhs, rhs);
1567 } else {
1568 __ SubD(dst, lhs, rhs);
1569 }
1570 }
1571 break;
1572 }
1573
1574 default:
1575 LOG(FATAL) << "Unexpected binary operation type " << type;
1576 }
1577}
1578
1579void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001580 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001581
1582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1583 Primitive::Type type = instr->GetResultType();
1584 switch (type) {
1585 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001586 locations->SetInAt(0, Location::RequiresRegister());
1587 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1589 break;
1590 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001591 locations->SetInAt(0, Location::RequiresRegister());
1592 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1593 locations->SetOut(Location::RequiresRegister());
1594 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001595 default:
1596 LOG(FATAL) << "Unexpected shift type " << type;
1597 }
1598}
1599
1600static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1601
1602void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001603 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001604 LocationSummary* locations = instr->GetLocations();
1605 Primitive::Type type = instr->GetType();
1606
1607 Location rhs_location = locations->InAt(1);
1608 bool use_imm = rhs_location.IsConstant();
1609 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1610 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001611 const uint32_t shift_mask =
1612 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001613 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001614 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1615 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001616
1617 switch (type) {
1618 case Primitive::kPrimInt: {
1619 Register dst = locations->Out().AsRegister<Register>();
1620 Register lhs = locations->InAt(0).AsRegister<Register>();
1621 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001622 if (shift_value == 0) {
1623 if (dst != lhs) {
1624 __ Move(dst, lhs);
1625 }
1626 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627 __ Sll(dst, lhs, shift_value);
1628 } else if (instr->IsShr()) {
1629 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001630 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001631 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001632 } else {
1633 if (has_ins_rotr) {
1634 __ Rotr(dst, lhs, shift_value);
1635 } else {
1636 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1637 __ Srl(dst, lhs, shift_value);
1638 __ Or(dst, dst, TMP);
1639 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001640 }
1641 } else {
1642 if (instr->IsShl()) {
1643 __ Sllv(dst, lhs, rhs_reg);
1644 } else if (instr->IsShr()) {
1645 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001646 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001647 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001648 } else {
1649 if (has_ins_rotr) {
1650 __ Rotrv(dst, lhs, rhs_reg);
1651 } else {
1652 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001653 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1654 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1655 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1656 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1657 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001658 __ Sllv(TMP, lhs, TMP);
1659 __ Srlv(dst, lhs, rhs_reg);
1660 __ Or(dst, dst, TMP);
1661 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001662 }
1663 }
1664 break;
1665 }
1666
1667 case Primitive::kPrimLong: {
1668 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1669 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1670 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1671 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1672 if (use_imm) {
1673 if (shift_value == 0) {
1674 codegen_->Move64(locations->Out(), locations->InAt(0));
1675 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001676 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001677 if (instr->IsShl()) {
1678 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1679 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1680 __ Sll(dst_low, lhs_low, shift_value);
1681 } else if (instr->IsShr()) {
1682 __ Srl(dst_low, lhs_low, shift_value);
1683 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1684 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001685 } else if (instr->IsUShr()) {
1686 __ Srl(dst_low, lhs_low, shift_value);
1687 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1688 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001689 } else {
1690 __ Srl(dst_low, lhs_low, shift_value);
1691 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1692 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001693 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001694 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001695 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001696 if (instr->IsShl()) {
1697 __ Sll(dst_low, lhs_low, shift_value);
1698 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1699 __ Sll(dst_high, lhs_high, shift_value);
1700 __ Or(dst_high, dst_high, TMP);
1701 } else if (instr->IsShr()) {
1702 __ Sra(dst_high, lhs_high, shift_value);
1703 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1704 __ Srl(dst_low, lhs_low, shift_value);
1705 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001706 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001707 __ Srl(dst_high, lhs_high, shift_value);
1708 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1709 __ Srl(dst_low, lhs_low, shift_value);
1710 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001711 } else {
1712 __ Srl(TMP, lhs_low, shift_value);
1713 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1714 __ Or(dst_low, dst_low, TMP);
1715 __ Srl(TMP, lhs_high, shift_value);
1716 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1717 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001718 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001719 }
1720 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001721 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001722 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001723 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001724 __ Move(dst_low, ZERO);
1725 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001726 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001727 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001728 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001729 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001730 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001731 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001732 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001733 // 64-bit rotation by 32 is just a swap.
1734 __ Move(dst_low, lhs_high);
1735 __ Move(dst_high, lhs_low);
1736 } else {
1737 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001738 __ Srl(dst_low, lhs_high, shift_value_high);
1739 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1740 __ Srl(dst_high, lhs_low, shift_value_high);
1741 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001742 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001743 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1744 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001745 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001746 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1747 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001748 __ Or(dst_high, dst_high, TMP);
1749 }
1750 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001751 }
1752 }
1753 } else {
1754 MipsLabel done;
1755 if (instr->IsShl()) {
1756 __ Sllv(dst_low, lhs_low, rhs_reg);
1757 __ Nor(AT, ZERO, rhs_reg);
1758 __ Srl(TMP, lhs_low, 1);
1759 __ Srlv(TMP, TMP, AT);
1760 __ Sllv(dst_high, lhs_high, rhs_reg);
1761 __ Or(dst_high, dst_high, TMP);
1762 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1763 __ Beqz(TMP, &done);
1764 __ Move(dst_high, dst_low);
1765 __ Move(dst_low, ZERO);
1766 } else if (instr->IsShr()) {
1767 __ Srav(dst_high, lhs_high, rhs_reg);
1768 __ Nor(AT, ZERO, rhs_reg);
1769 __ Sll(TMP, lhs_high, 1);
1770 __ Sllv(TMP, TMP, AT);
1771 __ Srlv(dst_low, lhs_low, rhs_reg);
1772 __ Or(dst_low, dst_low, TMP);
1773 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1774 __ Beqz(TMP, &done);
1775 __ Move(dst_low, dst_high);
1776 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001777 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001778 __ Srlv(dst_high, lhs_high, rhs_reg);
1779 __ Nor(AT, ZERO, rhs_reg);
1780 __ Sll(TMP, lhs_high, 1);
1781 __ Sllv(TMP, TMP, AT);
1782 __ Srlv(dst_low, lhs_low, rhs_reg);
1783 __ Or(dst_low, dst_low, TMP);
1784 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1785 __ Beqz(TMP, &done);
1786 __ Move(dst_low, dst_high);
1787 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001788 } else {
1789 __ Nor(AT, ZERO, rhs_reg);
1790 __ Srlv(TMP, lhs_low, rhs_reg);
1791 __ Sll(dst_low, lhs_high, 1);
1792 __ Sllv(dst_low, dst_low, AT);
1793 __ Or(dst_low, dst_low, TMP);
1794 __ Srlv(TMP, lhs_high, rhs_reg);
1795 __ Sll(dst_high, lhs_low, 1);
1796 __ Sllv(dst_high, dst_high, AT);
1797 __ Or(dst_high, dst_high, TMP);
1798 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1799 __ Beqz(TMP, &done);
1800 __ Move(TMP, dst_high);
1801 __ Move(dst_high, dst_low);
1802 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001803 }
1804 __ Bind(&done);
1805 }
1806 break;
1807 }
1808
1809 default:
1810 LOG(FATAL) << "Unexpected shift operation type " << type;
1811 }
1812}
1813
1814void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1815 HandleBinaryOp(instruction);
1816}
1817
1818void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1819 HandleBinaryOp(instruction);
1820}
1821
1822void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1823 HandleBinaryOp(instruction);
1824}
1825
1826void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1827 HandleBinaryOp(instruction);
1828}
1829
1830void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1833 locations->SetInAt(0, Location::RequiresRegister());
1834 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1835 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1836 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1837 } else {
1838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1839 }
1840}
1841
Alexey Frunze2923db72016-08-20 01:55:47 -07001842auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1843 auto null_checker = [this, instruction]() {
1844 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1845 };
1846 return null_checker;
1847}
1848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1850 LocationSummary* locations = instruction->GetLocations();
1851 Register obj = locations->InAt(0).AsRegister<Register>();
1852 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001853 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001854 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001855
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001856 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 switch (type) {
1858 case Primitive::kPrimBoolean: {
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(kLoadUnsignedByte, 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(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 }
1868 break;
1869 }
1870
1871 case Primitive::kPrimByte: {
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_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001876 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001877 } else {
1878 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001879 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880 }
1881 break;
1882 }
1883
1884 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001885 Register out = locations->Out().AsRegister<Register>();
1886 if (index.IsConstant()) {
1887 size_t offset =
1888 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001889 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001890 } else {
1891 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1892 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001893 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001894 }
1895 break;
1896 }
1897
1898 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001899 Register out = locations->Out().AsRegister<Register>();
1900 if (index.IsConstant()) {
1901 size_t offset =
1902 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001903 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001904 } else {
1905 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1906 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001907 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908 }
1909 break;
1910 }
1911
1912 case Primitive::kPrimInt:
1913 case Primitive::kPrimNot: {
1914 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 Register out = locations->Out().AsRegister<Register>();
1916 if (index.IsConstant()) {
1917 size_t offset =
1918 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001919 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920 } else {
1921 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1922 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001923 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924 }
1925 break;
1926 }
1927
1928 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 Register out = locations->Out().AsRegisterPairLow<Register>();
1930 if (index.IsConstant()) {
1931 size_t offset =
1932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001933 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 } else {
1935 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1936 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001937 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001938 }
1939 break;
1940 }
1941
1942 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1944 if (index.IsConstant()) {
1945 size_t offset =
1946 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001947 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948 } else {
1949 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1950 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001951 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 }
1953 break;
1954 }
1955
1956 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001957 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1958 if (index.IsConstant()) {
1959 size_t offset =
1960 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001961 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001962 } else {
1963 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1964 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001965 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001966 }
1967 break;
1968 }
1969
1970 case Primitive::kPrimVoid:
1971 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1972 UNREACHABLE();
1973 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001974}
1975
1976void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1978 locations->SetInAt(0, Location::RequiresRegister());
1979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1980}
1981
1982void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1983 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001984 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001985 Register obj = locations->InAt(0).AsRegister<Register>();
1986 Register out = locations->Out().AsRegister<Register>();
1987 __ LoadFromOffset(kLoadWord, out, obj, offset);
1988 codegen_->MaybeRecordImplicitNullCheck(instruction);
1989}
1990
Alexey Frunzef58b2482016-09-02 22:14:06 -07001991Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1992 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1993 ? Location::ConstantLocation(instruction->AsConstant())
1994 : Location::RequiresRegister();
1995}
1996
1997Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1998 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1999 // We can store a non-zero float or double constant without first loading it into the FPU,
2000 // but we should only prefer this if the constant has a single use.
2001 if (instruction->IsConstant() &&
2002 (instruction->AsConstant()->IsZeroBitPattern() ||
2003 instruction->GetUses().HasExactlyOneElement())) {
2004 return Location::ConstantLocation(instruction->AsConstant());
2005 // Otherwise fall through and require an FPU register for the constant.
2006 }
2007 return Location::RequiresFpuRegister();
2008}
2009
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002011 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002012 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2013 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002014 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002015 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002016 InvokeRuntimeCallingConvention calling_convention;
2017 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2018 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2019 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2020 } else {
2021 locations->SetInAt(0, Location::RequiresRegister());
2022 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2023 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002024 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002025 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002026 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002027 }
2028 }
2029}
2030
2031void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2032 LocationSummary* locations = instruction->GetLocations();
2033 Register obj = locations->InAt(0).AsRegister<Register>();
2034 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002035 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036 Primitive::Type value_type = instruction->GetComponentType();
2037 bool needs_runtime_call = locations->WillCall();
2038 bool needs_write_barrier =
2039 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002040 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002041 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002042
2043 switch (value_type) {
2044 case Primitive::kPrimBoolean:
2045 case Primitive::kPrimByte: {
2046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002048 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002050 __ Addu(base_reg, obj, index.AsRegister<Register>());
2051 }
2052 if (value_location.IsConstant()) {
2053 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2054 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2055 } else {
2056 Register value = value_location.AsRegister<Register>();
2057 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002058 }
2059 break;
2060 }
2061
2062 case Primitive::kPrimShort:
2063 case Primitive::kPrimChar: {
2064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002065 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002066 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002067 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002068 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2069 __ Addu(base_reg, obj, base_reg);
2070 }
2071 if (value_location.IsConstant()) {
2072 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2073 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2074 } else {
2075 Register value = value_location.AsRegister<Register>();
2076 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002077 }
2078 break;
2079 }
2080
2081 case Primitive::kPrimInt:
2082 case Primitive::kPrimNot: {
2083 if (!needs_runtime_call) {
2084 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002086 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002088 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2089 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002090 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002091 if (value_location.IsConstant()) {
2092 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2093 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2094 DCHECK(!needs_write_barrier);
2095 } else {
2096 Register value = value_location.AsRegister<Register>();
2097 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2098 if (needs_write_barrier) {
2099 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevice114da22016-12-26 14:21:43 +01002100 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunzef58b2482016-09-02 22:14:06 -07002101 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002102 }
2103 } else {
2104 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002105 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2107 }
2108 break;
2109 }
2110
2111 case Primitive::kPrimLong: {
2112 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002113 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002114 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002116 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2117 __ Addu(base_reg, obj, base_reg);
2118 }
2119 if (value_location.IsConstant()) {
2120 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2121 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2122 } else {
2123 Register value = value_location.AsRegisterPairLow<Register>();
2124 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002125 }
2126 break;
2127 }
2128
2129 case Primitive::kPrimFloat: {
2130 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002131 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002132 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002134 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2135 __ Addu(base_reg, obj, base_reg);
2136 }
2137 if (value_location.IsConstant()) {
2138 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2139 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2140 } else {
2141 FRegister value = value_location.AsFpuRegister<FRegister>();
2142 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002143 }
2144 break;
2145 }
2146
2147 case Primitive::kPrimDouble: {
2148 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002149 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002150 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002151 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002152 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2153 __ Addu(base_reg, obj, base_reg);
2154 }
2155 if (value_location.IsConstant()) {
2156 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2157 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2158 } else {
2159 FRegister value = value_location.AsFpuRegister<FRegister>();
2160 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002161 }
2162 break;
2163 }
2164
2165 case Primitive::kPrimVoid:
2166 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2167 UNREACHABLE();
2168 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002169}
2170
2171void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002172 RegisterSet caller_saves = RegisterSet::Empty();
2173 InvokeRuntimeCallingConvention calling_convention;
2174 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2175 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2176 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002177 locations->SetInAt(0, Location::RequiresRegister());
2178 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002179}
2180
2181void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2182 LocationSummary* locations = instruction->GetLocations();
2183 BoundsCheckSlowPathMIPS* slow_path =
2184 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2185 codegen_->AddSlowPath(slow_path);
2186
2187 Register index = locations->InAt(0).AsRegister<Register>();
2188 Register length = locations->InAt(1).AsRegister<Register>();
2189
2190 // length is limited by the maximum positive signed 32-bit integer.
2191 // Unsigned comparison of length and index checks for index < 0
2192 // and for length <= index simultaneously.
2193 __ Bgeu(index, length, slow_path->GetEntryLabel());
2194}
2195
2196void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2198 instruction,
2199 LocationSummary::kCallOnSlowPath);
2200 locations->SetInAt(0, Location::RequiresRegister());
2201 locations->SetInAt(1, Location::RequiresRegister());
2202 // Note that TypeCheckSlowPathMIPS uses this register too.
2203 locations->AddTemp(Location::RequiresRegister());
2204}
2205
2206void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2207 LocationSummary* locations = instruction->GetLocations();
2208 Register obj = locations->InAt(0).AsRegister<Register>();
2209 Register cls = locations->InAt(1).AsRegister<Register>();
2210 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2211
2212 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2213 codegen_->AddSlowPath(slow_path);
2214
2215 // TODO: avoid this check if we know obj is not null.
2216 __ Beqz(obj, slow_path->GetExitLabel());
2217 // Compare the class of `obj` with `cls`.
2218 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2219 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2220 __ Bind(slow_path->GetExitLabel());
2221}
2222
2223void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2224 LocationSummary* locations =
2225 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2226 locations->SetInAt(0, Location::RequiresRegister());
2227 if (check->HasUses()) {
2228 locations->SetOut(Location::SameAsFirstInput());
2229 }
2230}
2231
2232void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2233 // We assume the class is not null.
2234 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2235 check->GetLoadClass(),
2236 check,
2237 check->GetDexPc(),
2238 true);
2239 codegen_->AddSlowPath(slow_path);
2240 GenerateClassInitializationCheck(slow_path,
2241 check->GetLocations()->InAt(0).AsRegister<Register>());
2242}
2243
2244void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2245 Primitive::Type in_type = compare->InputAt(0)->GetType();
2246
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002247 LocationSummary* locations =
2248 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002249
2250 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002251 case Primitive::kPrimBoolean:
2252 case Primitive::kPrimByte:
2253 case Primitive::kPrimShort:
2254 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002255 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002256 locations->SetInAt(0, Location::RequiresRegister());
2257 locations->SetInAt(1, Location::RequiresRegister());
2258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2259 break;
2260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002261 case Primitive::kPrimLong:
2262 locations->SetInAt(0, Location::RequiresRegister());
2263 locations->SetInAt(1, Location::RequiresRegister());
2264 // Output overlaps because it is written before doing the low comparison.
2265 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2266 break;
2267
2268 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002269 case Primitive::kPrimDouble:
2270 locations->SetInAt(0, Location::RequiresFpuRegister());
2271 locations->SetInAt(1, Location::RequiresFpuRegister());
2272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274
2275 default:
2276 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2277 }
2278}
2279
2280void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2281 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002282 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002283 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002284 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285
2286 // 0 if: left == right
2287 // 1 if: left > right
2288 // -1 if: left < right
2289 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002290 case Primitive::kPrimBoolean:
2291 case Primitive::kPrimByte:
2292 case Primitive::kPrimShort:
2293 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002294 case Primitive::kPrimInt: {
2295 Register lhs = locations->InAt(0).AsRegister<Register>();
2296 Register rhs = locations->InAt(1).AsRegister<Register>();
2297 __ Slt(TMP, lhs, rhs);
2298 __ Slt(res, rhs, lhs);
2299 __ Subu(res, res, TMP);
2300 break;
2301 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002302 case Primitive::kPrimLong: {
2303 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002304 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2305 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2306 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2307 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2308 // TODO: more efficient (direct) comparison with a constant.
2309 __ Slt(TMP, lhs_high, rhs_high);
2310 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2311 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2312 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2313 __ Sltu(TMP, lhs_low, rhs_low);
2314 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2315 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2316 __ Bind(&done);
2317 break;
2318 }
2319
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002320 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002321 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002322 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2323 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2324 MipsLabel done;
2325 if (isR6) {
2326 __ CmpEqS(FTMP, lhs, rhs);
2327 __ LoadConst32(res, 0);
2328 __ Bc1nez(FTMP, &done);
2329 if (gt_bias) {
2330 __ CmpLtS(FTMP, lhs, rhs);
2331 __ LoadConst32(res, -1);
2332 __ Bc1nez(FTMP, &done);
2333 __ LoadConst32(res, 1);
2334 } else {
2335 __ CmpLtS(FTMP, rhs, lhs);
2336 __ LoadConst32(res, 1);
2337 __ Bc1nez(FTMP, &done);
2338 __ LoadConst32(res, -1);
2339 }
2340 } else {
2341 if (gt_bias) {
2342 __ ColtS(0, lhs, rhs);
2343 __ LoadConst32(res, -1);
2344 __ Bc1t(0, &done);
2345 __ CeqS(0, lhs, rhs);
2346 __ LoadConst32(res, 1);
2347 __ Movt(res, ZERO, 0);
2348 } else {
2349 __ ColtS(0, rhs, lhs);
2350 __ LoadConst32(res, 1);
2351 __ Bc1t(0, &done);
2352 __ CeqS(0, lhs, rhs);
2353 __ LoadConst32(res, -1);
2354 __ Movt(res, ZERO, 0);
2355 }
2356 }
2357 __ Bind(&done);
2358 break;
2359 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002360 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002361 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002362 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2363 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2364 MipsLabel done;
2365 if (isR6) {
2366 __ CmpEqD(FTMP, lhs, rhs);
2367 __ LoadConst32(res, 0);
2368 __ Bc1nez(FTMP, &done);
2369 if (gt_bias) {
2370 __ CmpLtD(FTMP, lhs, rhs);
2371 __ LoadConst32(res, -1);
2372 __ Bc1nez(FTMP, &done);
2373 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002374 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 __ CmpLtD(FTMP, rhs, lhs);
2376 __ LoadConst32(res, 1);
2377 __ Bc1nez(FTMP, &done);
2378 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002379 }
2380 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002381 if (gt_bias) {
2382 __ ColtD(0, lhs, rhs);
2383 __ LoadConst32(res, -1);
2384 __ Bc1t(0, &done);
2385 __ CeqD(0, lhs, rhs);
2386 __ LoadConst32(res, 1);
2387 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002388 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002389 __ ColtD(0, rhs, lhs);
2390 __ LoadConst32(res, 1);
2391 __ Bc1t(0, &done);
2392 __ CeqD(0, lhs, rhs);
2393 __ LoadConst32(res, -1);
2394 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002395 }
2396 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002397 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 break;
2399 }
2400
2401 default:
2402 LOG(FATAL) << "Unimplemented compare type " << in_type;
2403 }
2404}
2405
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002406void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002408 switch (instruction->InputAt(0)->GetType()) {
2409 default:
2410 case Primitive::kPrimLong:
2411 locations->SetInAt(0, Location::RequiresRegister());
2412 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2413 break;
2414
2415 case Primitive::kPrimFloat:
2416 case Primitive::kPrimDouble:
2417 locations->SetInAt(0, Location::RequiresFpuRegister());
2418 locations->SetInAt(1, Location::RequiresFpuRegister());
2419 break;
2420 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002421 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2423 }
2424}
2425
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002426void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002427 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002428 return;
2429 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002431 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 LocationSummary* locations = instruction->GetLocations();
2433 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002434 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002435
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002436 switch (type) {
2437 default:
2438 // Integer case.
2439 GenerateIntCompare(instruction->GetCondition(), locations);
2440 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002441
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002442 case Primitive::kPrimLong:
2443 // TODO: don't use branches.
2444 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 break;
2446
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002447 case Primitive::kPrimFloat:
2448 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002449 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2450 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002451 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002452
2453 // Convert the branches into the result.
2454 MipsLabel done;
2455
2456 // False case: result = 0.
2457 __ LoadConst32(dst, 0);
2458 __ B(&done);
2459
2460 // True case: result = 1.
2461 __ Bind(&true_label);
2462 __ LoadConst32(dst, 1);
2463 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002464}
2465
Alexey Frunze7e99e052015-11-24 19:28:01 -08002466void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2467 DCHECK(instruction->IsDiv() || instruction->IsRem());
2468 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2469
2470 LocationSummary* locations = instruction->GetLocations();
2471 Location second = locations->InAt(1);
2472 DCHECK(second.IsConstant());
2473
2474 Register out = locations->Out().AsRegister<Register>();
2475 Register dividend = locations->InAt(0).AsRegister<Register>();
2476 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2477 DCHECK(imm == 1 || imm == -1);
2478
2479 if (instruction->IsRem()) {
2480 __ Move(out, ZERO);
2481 } else {
2482 if (imm == -1) {
2483 __ Subu(out, ZERO, dividend);
2484 } else if (out != dividend) {
2485 __ Move(out, dividend);
2486 }
2487 }
2488}
2489
2490void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2491 DCHECK(instruction->IsDiv() || instruction->IsRem());
2492 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2493
2494 LocationSummary* locations = instruction->GetLocations();
2495 Location second = locations->InAt(1);
2496 DCHECK(second.IsConstant());
2497
2498 Register out = locations->Out().AsRegister<Register>();
2499 Register dividend = locations->InAt(0).AsRegister<Register>();
2500 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002501 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002502 int ctz_imm = CTZ(abs_imm);
2503
2504 if (instruction->IsDiv()) {
2505 if (ctz_imm == 1) {
2506 // Fast path for division by +/-2, which is very common.
2507 __ Srl(TMP, dividend, 31);
2508 } else {
2509 __ Sra(TMP, dividend, 31);
2510 __ Srl(TMP, TMP, 32 - ctz_imm);
2511 }
2512 __ Addu(out, dividend, TMP);
2513 __ Sra(out, out, ctz_imm);
2514 if (imm < 0) {
2515 __ Subu(out, ZERO, out);
2516 }
2517 } else {
2518 if (ctz_imm == 1) {
2519 // Fast path for modulo +/-2, which is very common.
2520 __ Sra(TMP, dividend, 31);
2521 __ Subu(out, dividend, TMP);
2522 __ Andi(out, out, 1);
2523 __ Addu(out, out, TMP);
2524 } else {
2525 __ Sra(TMP, dividend, 31);
2526 __ Srl(TMP, TMP, 32 - ctz_imm);
2527 __ Addu(out, dividend, TMP);
2528 if (IsUint<16>(abs_imm - 1)) {
2529 __ Andi(out, out, abs_imm - 1);
2530 } else {
2531 __ Sll(out, out, 32 - ctz_imm);
2532 __ Srl(out, out, 32 - ctz_imm);
2533 }
2534 __ Subu(out, out, TMP);
2535 }
2536 }
2537}
2538
2539void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2540 DCHECK(instruction->IsDiv() || instruction->IsRem());
2541 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2542
2543 LocationSummary* locations = instruction->GetLocations();
2544 Location second = locations->InAt(1);
2545 DCHECK(second.IsConstant());
2546
2547 Register out = locations->Out().AsRegister<Register>();
2548 Register dividend = locations->InAt(0).AsRegister<Register>();
2549 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2550
2551 int64_t magic;
2552 int shift;
2553 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2554
2555 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2556
2557 __ LoadConst32(TMP, magic);
2558 if (isR6) {
2559 __ MuhR6(TMP, dividend, TMP);
2560 } else {
2561 __ MultR2(dividend, TMP);
2562 __ Mfhi(TMP);
2563 }
2564 if (imm > 0 && magic < 0) {
2565 __ Addu(TMP, TMP, dividend);
2566 } else if (imm < 0 && magic > 0) {
2567 __ Subu(TMP, TMP, dividend);
2568 }
2569
2570 if (shift != 0) {
2571 __ Sra(TMP, TMP, shift);
2572 }
2573
2574 if (instruction->IsDiv()) {
2575 __ Sra(out, TMP, 31);
2576 __ Subu(out, TMP, out);
2577 } else {
2578 __ Sra(AT, TMP, 31);
2579 __ Subu(AT, TMP, AT);
2580 __ LoadConst32(TMP, imm);
2581 if (isR6) {
2582 __ MulR6(TMP, AT, TMP);
2583 } else {
2584 __ MulR2(TMP, AT, TMP);
2585 }
2586 __ Subu(out, dividend, TMP);
2587 }
2588}
2589
2590void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2591 DCHECK(instruction->IsDiv() || instruction->IsRem());
2592 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2593
2594 LocationSummary* locations = instruction->GetLocations();
2595 Register out = locations->Out().AsRegister<Register>();
2596 Location second = locations->InAt(1);
2597
2598 if (second.IsConstant()) {
2599 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2600 if (imm == 0) {
2601 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2602 } else if (imm == 1 || imm == -1) {
2603 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002604 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002605 DivRemByPowerOfTwo(instruction);
2606 } else {
2607 DCHECK(imm <= -2 || imm >= 2);
2608 GenerateDivRemWithAnyConstant(instruction);
2609 }
2610 } else {
2611 Register dividend = locations->InAt(0).AsRegister<Register>();
2612 Register divisor = second.AsRegister<Register>();
2613 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2614 if (instruction->IsDiv()) {
2615 if (isR6) {
2616 __ DivR6(out, dividend, divisor);
2617 } else {
2618 __ DivR2(out, dividend, divisor);
2619 }
2620 } else {
2621 if (isR6) {
2622 __ ModR6(out, dividend, divisor);
2623 } else {
2624 __ ModR2(out, dividend, divisor);
2625 }
2626 }
2627 }
2628}
2629
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002630void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2631 Primitive::Type type = div->GetResultType();
2632 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002633 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002634 : LocationSummary::kNoCall;
2635
2636 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2637
2638 switch (type) {
2639 case Primitive::kPrimInt:
2640 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002641 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002642 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2643 break;
2644
2645 case Primitive::kPrimLong: {
2646 InvokeRuntimeCallingConvention calling_convention;
2647 locations->SetInAt(0, Location::RegisterPairLocation(
2648 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2649 locations->SetInAt(1, Location::RegisterPairLocation(
2650 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2651 locations->SetOut(calling_convention.GetReturnLocation(type));
2652 break;
2653 }
2654
2655 case Primitive::kPrimFloat:
2656 case Primitive::kPrimDouble:
2657 locations->SetInAt(0, Location::RequiresFpuRegister());
2658 locations->SetInAt(1, Location::RequiresFpuRegister());
2659 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2660 break;
2661
2662 default:
2663 LOG(FATAL) << "Unexpected div type " << type;
2664 }
2665}
2666
2667void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2668 Primitive::Type type = instruction->GetType();
2669 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002670
2671 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002672 case Primitive::kPrimInt:
2673 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002675 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002676 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2678 break;
2679 }
2680 case Primitive::kPrimFloat:
2681 case Primitive::kPrimDouble: {
2682 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2683 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2684 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2685 if (type == Primitive::kPrimFloat) {
2686 __ DivS(dst, lhs, rhs);
2687 } else {
2688 __ DivD(dst, lhs, rhs);
2689 }
2690 break;
2691 }
2692 default:
2693 LOG(FATAL) << "Unexpected div type " << type;
2694 }
2695}
2696
2697void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002698 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002700}
2701
2702void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2703 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2704 codegen_->AddSlowPath(slow_path);
2705 Location value = instruction->GetLocations()->InAt(0);
2706 Primitive::Type type = instruction->GetType();
2707
2708 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002709 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002710 case Primitive::kPrimByte:
2711 case Primitive::kPrimChar:
2712 case Primitive::kPrimShort:
2713 case Primitive::kPrimInt: {
2714 if (value.IsConstant()) {
2715 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2716 __ B(slow_path->GetEntryLabel());
2717 } else {
2718 // A division by a non-null constant is valid. We don't need to perform
2719 // any check, so simply fall through.
2720 }
2721 } else {
2722 DCHECK(value.IsRegister()) << value;
2723 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2724 }
2725 break;
2726 }
2727 case Primitive::kPrimLong: {
2728 if (value.IsConstant()) {
2729 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2730 __ B(slow_path->GetEntryLabel());
2731 } else {
2732 // A division by a non-null constant is valid. We don't need to perform
2733 // any check, so simply fall through.
2734 }
2735 } else {
2736 DCHECK(value.IsRegisterPair()) << value;
2737 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2738 __ Beqz(TMP, slow_path->GetEntryLabel());
2739 }
2740 break;
2741 }
2742 default:
2743 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2744 }
2745}
2746
2747void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2748 LocationSummary* locations =
2749 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2750 locations->SetOut(Location::ConstantLocation(constant));
2751}
2752
2753void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2754 // Will be generated at use site.
2755}
2756
2757void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2758 exit->SetLocations(nullptr);
2759}
2760
2761void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2762}
2763
2764void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2765 LocationSummary* locations =
2766 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2767 locations->SetOut(Location::ConstantLocation(constant));
2768}
2769
2770void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2771 // Will be generated at use site.
2772}
2773
2774void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2775 got->SetLocations(nullptr);
2776}
2777
2778void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2779 DCHECK(!successor->IsExitBlock());
2780 HBasicBlock* block = got->GetBlock();
2781 HInstruction* previous = got->GetPrevious();
2782 HLoopInformation* info = block->GetLoopInformation();
2783
2784 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2785 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2786 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2787 return;
2788 }
2789 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2790 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2791 }
2792 if (!codegen_->GoesToNextBlock(block, successor)) {
2793 __ B(codegen_->GetLabelOf(successor));
2794 }
2795}
2796
2797void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2798 HandleGoto(got, got->GetSuccessor());
2799}
2800
2801void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2802 try_boundary->SetLocations(nullptr);
2803}
2804
2805void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2806 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2807 if (!successor->IsExitBlock()) {
2808 HandleGoto(try_boundary, successor);
2809 }
2810}
2811
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002812void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2813 LocationSummary* locations) {
2814 Register dst = locations->Out().AsRegister<Register>();
2815 Register lhs = locations->InAt(0).AsRegister<Register>();
2816 Location rhs_location = locations->InAt(1);
2817 Register rhs_reg = ZERO;
2818 int64_t rhs_imm = 0;
2819 bool use_imm = rhs_location.IsConstant();
2820 if (use_imm) {
2821 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2822 } else {
2823 rhs_reg = rhs_location.AsRegister<Register>();
2824 }
2825
2826 switch (cond) {
2827 case kCondEQ:
2828 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002829 if (use_imm && IsInt<16>(-rhs_imm)) {
2830 if (rhs_imm == 0) {
2831 if (cond == kCondEQ) {
2832 __ Sltiu(dst, lhs, 1);
2833 } else {
2834 __ Sltu(dst, ZERO, lhs);
2835 }
2836 } else {
2837 __ Addiu(dst, lhs, -rhs_imm);
2838 if (cond == kCondEQ) {
2839 __ Sltiu(dst, dst, 1);
2840 } else {
2841 __ Sltu(dst, ZERO, dst);
2842 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002843 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002844 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002845 if (use_imm && IsUint<16>(rhs_imm)) {
2846 __ Xori(dst, lhs, rhs_imm);
2847 } else {
2848 if (use_imm) {
2849 rhs_reg = TMP;
2850 __ LoadConst32(rhs_reg, rhs_imm);
2851 }
2852 __ Xor(dst, lhs, rhs_reg);
2853 }
2854 if (cond == kCondEQ) {
2855 __ Sltiu(dst, dst, 1);
2856 } else {
2857 __ Sltu(dst, ZERO, dst);
2858 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002859 }
2860 break;
2861
2862 case kCondLT:
2863 case kCondGE:
2864 if (use_imm && IsInt<16>(rhs_imm)) {
2865 __ Slti(dst, lhs, rhs_imm);
2866 } else {
2867 if (use_imm) {
2868 rhs_reg = TMP;
2869 __ LoadConst32(rhs_reg, rhs_imm);
2870 }
2871 __ Slt(dst, lhs, rhs_reg);
2872 }
2873 if (cond == kCondGE) {
2874 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2875 // only the slt instruction but no sge.
2876 __ Xori(dst, dst, 1);
2877 }
2878 break;
2879
2880 case kCondLE:
2881 case kCondGT:
2882 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2883 // Simulate lhs <= rhs via lhs < rhs + 1.
2884 __ Slti(dst, lhs, rhs_imm + 1);
2885 if (cond == kCondGT) {
2886 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2887 // only the slti instruction but no sgti.
2888 __ Xori(dst, dst, 1);
2889 }
2890 } else {
2891 if (use_imm) {
2892 rhs_reg = TMP;
2893 __ LoadConst32(rhs_reg, rhs_imm);
2894 }
2895 __ Slt(dst, rhs_reg, lhs);
2896 if (cond == kCondLE) {
2897 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2898 // only the slt instruction but no sle.
2899 __ Xori(dst, dst, 1);
2900 }
2901 }
2902 break;
2903
2904 case kCondB:
2905 case kCondAE:
2906 if (use_imm && IsInt<16>(rhs_imm)) {
2907 // Sltiu sign-extends its 16-bit immediate operand before
2908 // the comparison and thus lets us compare directly with
2909 // unsigned values in the ranges [0, 0x7fff] and
2910 // [0xffff8000, 0xffffffff].
2911 __ Sltiu(dst, lhs, rhs_imm);
2912 } else {
2913 if (use_imm) {
2914 rhs_reg = TMP;
2915 __ LoadConst32(rhs_reg, rhs_imm);
2916 }
2917 __ Sltu(dst, lhs, rhs_reg);
2918 }
2919 if (cond == kCondAE) {
2920 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2921 // only the sltu instruction but no sgeu.
2922 __ Xori(dst, dst, 1);
2923 }
2924 break;
2925
2926 case kCondBE:
2927 case kCondA:
2928 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2929 // Simulate lhs <= rhs via lhs < rhs + 1.
2930 // Note that this only works if rhs + 1 does not overflow
2931 // to 0, hence the check above.
2932 // Sltiu sign-extends its 16-bit immediate operand before
2933 // the comparison and thus lets us compare directly with
2934 // unsigned values in the ranges [0, 0x7fff] and
2935 // [0xffff8000, 0xffffffff].
2936 __ Sltiu(dst, lhs, rhs_imm + 1);
2937 if (cond == kCondA) {
2938 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2939 // only the sltiu instruction but no sgtiu.
2940 __ Xori(dst, dst, 1);
2941 }
2942 } else {
2943 if (use_imm) {
2944 rhs_reg = TMP;
2945 __ LoadConst32(rhs_reg, rhs_imm);
2946 }
2947 __ Sltu(dst, rhs_reg, lhs);
2948 if (cond == kCondBE) {
2949 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2950 // only the sltu instruction but no sleu.
2951 __ Xori(dst, dst, 1);
2952 }
2953 }
2954 break;
2955 }
2956}
2957
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002958bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2959 LocationSummary* input_locations,
2960 Register dst) {
2961 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2962 Location rhs_location = input_locations->InAt(1);
2963 Register rhs_reg = ZERO;
2964 int64_t rhs_imm = 0;
2965 bool use_imm = rhs_location.IsConstant();
2966 if (use_imm) {
2967 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2968 } else {
2969 rhs_reg = rhs_location.AsRegister<Register>();
2970 }
2971
2972 switch (cond) {
2973 case kCondEQ:
2974 case kCondNE:
2975 if (use_imm && IsInt<16>(-rhs_imm)) {
2976 __ Addiu(dst, lhs, -rhs_imm);
2977 } else if (use_imm && IsUint<16>(rhs_imm)) {
2978 __ Xori(dst, lhs, rhs_imm);
2979 } else {
2980 if (use_imm) {
2981 rhs_reg = TMP;
2982 __ LoadConst32(rhs_reg, rhs_imm);
2983 }
2984 __ Xor(dst, lhs, rhs_reg);
2985 }
2986 return (cond == kCondEQ);
2987
2988 case kCondLT:
2989 case kCondGE:
2990 if (use_imm && IsInt<16>(rhs_imm)) {
2991 __ Slti(dst, lhs, rhs_imm);
2992 } else {
2993 if (use_imm) {
2994 rhs_reg = TMP;
2995 __ LoadConst32(rhs_reg, rhs_imm);
2996 }
2997 __ Slt(dst, lhs, rhs_reg);
2998 }
2999 return (cond == kCondGE);
3000
3001 case kCondLE:
3002 case kCondGT:
3003 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3004 // Simulate lhs <= rhs via lhs < rhs + 1.
3005 __ Slti(dst, lhs, rhs_imm + 1);
3006 return (cond == kCondGT);
3007 } else {
3008 if (use_imm) {
3009 rhs_reg = TMP;
3010 __ LoadConst32(rhs_reg, rhs_imm);
3011 }
3012 __ Slt(dst, rhs_reg, lhs);
3013 return (cond == kCondLE);
3014 }
3015
3016 case kCondB:
3017 case kCondAE:
3018 if (use_imm && IsInt<16>(rhs_imm)) {
3019 // Sltiu sign-extends its 16-bit immediate operand before
3020 // the comparison and thus lets us compare directly with
3021 // unsigned values in the ranges [0, 0x7fff] and
3022 // [0xffff8000, 0xffffffff].
3023 __ Sltiu(dst, lhs, rhs_imm);
3024 } else {
3025 if (use_imm) {
3026 rhs_reg = TMP;
3027 __ LoadConst32(rhs_reg, rhs_imm);
3028 }
3029 __ Sltu(dst, lhs, rhs_reg);
3030 }
3031 return (cond == kCondAE);
3032
3033 case kCondBE:
3034 case kCondA:
3035 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3036 // Simulate lhs <= rhs via lhs < rhs + 1.
3037 // Note that this only works if rhs + 1 does not overflow
3038 // to 0, hence the check above.
3039 // Sltiu sign-extends its 16-bit immediate operand before
3040 // the comparison and thus lets us compare directly with
3041 // unsigned values in the ranges [0, 0x7fff] and
3042 // [0xffff8000, 0xffffffff].
3043 __ Sltiu(dst, lhs, rhs_imm + 1);
3044 return (cond == kCondA);
3045 } else {
3046 if (use_imm) {
3047 rhs_reg = TMP;
3048 __ LoadConst32(rhs_reg, rhs_imm);
3049 }
3050 __ Sltu(dst, rhs_reg, lhs);
3051 return (cond == kCondBE);
3052 }
3053 }
3054}
3055
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003056void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3057 LocationSummary* locations,
3058 MipsLabel* label) {
3059 Register lhs = locations->InAt(0).AsRegister<Register>();
3060 Location rhs_location = locations->InAt(1);
3061 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003062 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003063 bool use_imm = rhs_location.IsConstant();
3064 if (use_imm) {
3065 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3066 } else {
3067 rhs_reg = rhs_location.AsRegister<Register>();
3068 }
3069
3070 if (use_imm && rhs_imm == 0) {
3071 switch (cond) {
3072 case kCondEQ:
3073 case kCondBE: // <= 0 if zero
3074 __ Beqz(lhs, label);
3075 break;
3076 case kCondNE:
3077 case kCondA: // > 0 if non-zero
3078 __ Bnez(lhs, label);
3079 break;
3080 case kCondLT:
3081 __ Bltz(lhs, label);
3082 break;
3083 case kCondGE:
3084 __ Bgez(lhs, label);
3085 break;
3086 case kCondLE:
3087 __ Blez(lhs, label);
3088 break;
3089 case kCondGT:
3090 __ Bgtz(lhs, label);
3091 break;
3092 case kCondB: // always false
3093 break;
3094 case kCondAE: // always true
3095 __ B(label);
3096 break;
3097 }
3098 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003099 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3100 if (isR6 || !use_imm) {
3101 if (use_imm) {
3102 rhs_reg = TMP;
3103 __ LoadConst32(rhs_reg, rhs_imm);
3104 }
3105 switch (cond) {
3106 case kCondEQ:
3107 __ Beq(lhs, rhs_reg, label);
3108 break;
3109 case kCondNE:
3110 __ Bne(lhs, rhs_reg, label);
3111 break;
3112 case kCondLT:
3113 __ Blt(lhs, rhs_reg, label);
3114 break;
3115 case kCondGE:
3116 __ Bge(lhs, rhs_reg, label);
3117 break;
3118 case kCondLE:
3119 __ Bge(rhs_reg, lhs, label);
3120 break;
3121 case kCondGT:
3122 __ Blt(rhs_reg, lhs, label);
3123 break;
3124 case kCondB:
3125 __ Bltu(lhs, rhs_reg, label);
3126 break;
3127 case kCondAE:
3128 __ Bgeu(lhs, rhs_reg, label);
3129 break;
3130 case kCondBE:
3131 __ Bgeu(rhs_reg, lhs, label);
3132 break;
3133 case kCondA:
3134 __ Bltu(rhs_reg, lhs, label);
3135 break;
3136 }
3137 } else {
3138 // Special cases for more efficient comparison with constants on R2.
3139 switch (cond) {
3140 case kCondEQ:
3141 __ LoadConst32(TMP, rhs_imm);
3142 __ Beq(lhs, TMP, label);
3143 break;
3144 case kCondNE:
3145 __ LoadConst32(TMP, rhs_imm);
3146 __ Bne(lhs, TMP, label);
3147 break;
3148 case kCondLT:
3149 if (IsInt<16>(rhs_imm)) {
3150 __ Slti(TMP, lhs, rhs_imm);
3151 __ Bnez(TMP, label);
3152 } else {
3153 __ LoadConst32(TMP, rhs_imm);
3154 __ Blt(lhs, TMP, label);
3155 }
3156 break;
3157 case kCondGE:
3158 if (IsInt<16>(rhs_imm)) {
3159 __ Slti(TMP, lhs, rhs_imm);
3160 __ Beqz(TMP, label);
3161 } else {
3162 __ LoadConst32(TMP, rhs_imm);
3163 __ Bge(lhs, TMP, label);
3164 }
3165 break;
3166 case kCondLE:
3167 if (IsInt<16>(rhs_imm + 1)) {
3168 // Simulate lhs <= rhs via lhs < rhs + 1.
3169 __ Slti(TMP, lhs, rhs_imm + 1);
3170 __ Bnez(TMP, label);
3171 } else {
3172 __ LoadConst32(TMP, rhs_imm);
3173 __ Bge(TMP, lhs, label);
3174 }
3175 break;
3176 case kCondGT:
3177 if (IsInt<16>(rhs_imm + 1)) {
3178 // Simulate lhs > rhs via !(lhs < rhs + 1).
3179 __ Slti(TMP, lhs, rhs_imm + 1);
3180 __ Beqz(TMP, label);
3181 } else {
3182 __ LoadConst32(TMP, rhs_imm);
3183 __ Blt(TMP, lhs, label);
3184 }
3185 break;
3186 case kCondB:
3187 if (IsInt<16>(rhs_imm)) {
3188 __ Sltiu(TMP, lhs, rhs_imm);
3189 __ Bnez(TMP, label);
3190 } else {
3191 __ LoadConst32(TMP, rhs_imm);
3192 __ Bltu(lhs, TMP, label);
3193 }
3194 break;
3195 case kCondAE:
3196 if (IsInt<16>(rhs_imm)) {
3197 __ Sltiu(TMP, lhs, rhs_imm);
3198 __ Beqz(TMP, label);
3199 } else {
3200 __ LoadConst32(TMP, rhs_imm);
3201 __ Bgeu(lhs, TMP, label);
3202 }
3203 break;
3204 case kCondBE:
3205 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3206 // Simulate lhs <= rhs via lhs < rhs + 1.
3207 // Note that this only works if rhs + 1 does not overflow
3208 // to 0, hence the check above.
3209 __ Sltiu(TMP, lhs, rhs_imm + 1);
3210 __ Bnez(TMP, label);
3211 } else {
3212 __ LoadConst32(TMP, rhs_imm);
3213 __ Bgeu(TMP, lhs, label);
3214 }
3215 break;
3216 case kCondA:
3217 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3218 // Simulate lhs > rhs via !(lhs < rhs + 1).
3219 // Note that this only works if rhs + 1 does not overflow
3220 // to 0, hence the check above.
3221 __ Sltiu(TMP, lhs, rhs_imm + 1);
3222 __ Beqz(TMP, label);
3223 } else {
3224 __ LoadConst32(TMP, rhs_imm);
3225 __ Bltu(TMP, lhs, label);
3226 }
3227 break;
3228 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003229 }
3230 }
3231}
3232
3233void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3234 LocationSummary* locations,
3235 MipsLabel* label) {
3236 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3237 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3238 Location rhs_location = locations->InAt(1);
3239 Register rhs_high = ZERO;
3240 Register rhs_low = ZERO;
3241 int64_t imm = 0;
3242 uint32_t imm_high = 0;
3243 uint32_t imm_low = 0;
3244 bool use_imm = rhs_location.IsConstant();
3245 if (use_imm) {
3246 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3247 imm_high = High32Bits(imm);
3248 imm_low = Low32Bits(imm);
3249 } else {
3250 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3251 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3252 }
3253
3254 if (use_imm && imm == 0) {
3255 switch (cond) {
3256 case kCondEQ:
3257 case kCondBE: // <= 0 if zero
3258 __ Or(TMP, lhs_high, lhs_low);
3259 __ Beqz(TMP, label);
3260 break;
3261 case kCondNE:
3262 case kCondA: // > 0 if non-zero
3263 __ Or(TMP, lhs_high, lhs_low);
3264 __ Bnez(TMP, label);
3265 break;
3266 case kCondLT:
3267 __ Bltz(lhs_high, label);
3268 break;
3269 case kCondGE:
3270 __ Bgez(lhs_high, label);
3271 break;
3272 case kCondLE:
3273 __ Or(TMP, lhs_high, lhs_low);
3274 __ Sra(AT, lhs_high, 31);
3275 __ Bgeu(AT, TMP, label);
3276 break;
3277 case kCondGT:
3278 __ Or(TMP, lhs_high, lhs_low);
3279 __ Sra(AT, lhs_high, 31);
3280 __ Bltu(AT, TMP, label);
3281 break;
3282 case kCondB: // always false
3283 break;
3284 case kCondAE: // always true
3285 __ B(label);
3286 break;
3287 }
3288 } else if (use_imm) {
3289 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3290 switch (cond) {
3291 case kCondEQ:
3292 __ LoadConst32(TMP, imm_high);
3293 __ Xor(TMP, TMP, lhs_high);
3294 __ LoadConst32(AT, imm_low);
3295 __ Xor(AT, AT, lhs_low);
3296 __ Or(TMP, TMP, AT);
3297 __ Beqz(TMP, label);
3298 break;
3299 case kCondNE:
3300 __ LoadConst32(TMP, imm_high);
3301 __ Xor(TMP, TMP, lhs_high);
3302 __ LoadConst32(AT, imm_low);
3303 __ Xor(AT, AT, lhs_low);
3304 __ Or(TMP, TMP, AT);
3305 __ Bnez(TMP, label);
3306 break;
3307 case kCondLT:
3308 __ LoadConst32(TMP, imm_high);
3309 __ Blt(lhs_high, TMP, label);
3310 __ Slt(TMP, TMP, lhs_high);
3311 __ LoadConst32(AT, imm_low);
3312 __ Sltu(AT, lhs_low, AT);
3313 __ Blt(TMP, AT, label);
3314 break;
3315 case kCondGE:
3316 __ LoadConst32(TMP, imm_high);
3317 __ Blt(TMP, lhs_high, label);
3318 __ Slt(TMP, lhs_high, TMP);
3319 __ LoadConst32(AT, imm_low);
3320 __ Sltu(AT, lhs_low, AT);
3321 __ Or(TMP, TMP, AT);
3322 __ Beqz(TMP, label);
3323 break;
3324 case kCondLE:
3325 __ LoadConst32(TMP, imm_high);
3326 __ Blt(lhs_high, TMP, label);
3327 __ Slt(TMP, TMP, lhs_high);
3328 __ LoadConst32(AT, imm_low);
3329 __ Sltu(AT, AT, lhs_low);
3330 __ Or(TMP, TMP, AT);
3331 __ Beqz(TMP, label);
3332 break;
3333 case kCondGT:
3334 __ LoadConst32(TMP, imm_high);
3335 __ Blt(TMP, lhs_high, label);
3336 __ Slt(TMP, lhs_high, TMP);
3337 __ LoadConst32(AT, imm_low);
3338 __ Sltu(AT, AT, lhs_low);
3339 __ Blt(TMP, AT, label);
3340 break;
3341 case kCondB:
3342 __ LoadConst32(TMP, imm_high);
3343 __ Bltu(lhs_high, TMP, label);
3344 __ Sltu(TMP, TMP, lhs_high);
3345 __ LoadConst32(AT, imm_low);
3346 __ Sltu(AT, lhs_low, AT);
3347 __ Blt(TMP, AT, label);
3348 break;
3349 case kCondAE:
3350 __ LoadConst32(TMP, imm_high);
3351 __ Bltu(TMP, lhs_high, label);
3352 __ Sltu(TMP, lhs_high, TMP);
3353 __ LoadConst32(AT, imm_low);
3354 __ Sltu(AT, lhs_low, AT);
3355 __ Or(TMP, TMP, AT);
3356 __ Beqz(TMP, label);
3357 break;
3358 case kCondBE:
3359 __ LoadConst32(TMP, imm_high);
3360 __ Bltu(lhs_high, TMP, label);
3361 __ Sltu(TMP, TMP, lhs_high);
3362 __ LoadConst32(AT, imm_low);
3363 __ Sltu(AT, AT, lhs_low);
3364 __ Or(TMP, TMP, AT);
3365 __ Beqz(TMP, label);
3366 break;
3367 case kCondA:
3368 __ LoadConst32(TMP, imm_high);
3369 __ Bltu(TMP, lhs_high, label);
3370 __ Sltu(TMP, lhs_high, TMP);
3371 __ LoadConst32(AT, imm_low);
3372 __ Sltu(AT, AT, lhs_low);
3373 __ Blt(TMP, AT, label);
3374 break;
3375 }
3376 } else {
3377 switch (cond) {
3378 case kCondEQ:
3379 __ Xor(TMP, lhs_high, rhs_high);
3380 __ Xor(AT, lhs_low, rhs_low);
3381 __ Or(TMP, TMP, AT);
3382 __ Beqz(TMP, label);
3383 break;
3384 case kCondNE:
3385 __ Xor(TMP, lhs_high, rhs_high);
3386 __ Xor(AT, lhs_low, rhs_low);
3387 __ Or(TMP, TMP, AT);
3388 __ Bnez(TMP, label);
3389 break;
3390 case kCondLT:
3391 __ Blt(lhs_high, rhs_high, label);
3392 __ Slt(TMP, rhs_high, lhs_high);
3393 __ Sltu(AT, lhs_low, rhs_low);
3394 __ Blt(TMP, AT, label);
3395 break;
3396 case kCondGE:
3397 __ Blt(rhs_high, lhs_high, label);
3398 __ Slt(TMP, lhs_high, rhs_high);
3399 __ Sltu(AT, lhs_low, rhs_low);
3400 __ Or(TMP, TMP, AT);
3401 __ Beqz(TMP, label);
3402 break;
3403 case kCondLE:
3404 __ Blt(lhs_high, rhs_high, label);
3405 __ Slt(TMP, rhs_high, lhs_high);
3406 __ Sltu(AT, rhs_low, lhs_low);
3407 __ Or(TMP, TMP, AT);
3408 __ Beqz(TMP, label);
3409 break;
3410 case kCondGT:
3411 __ Blt(rhs_high, lhs_high, label);
3412 __ Slt(TMP, lhs_high, rhs_high);
3413 __ Sltu(AT, rhs_low, lhs_low);
3414 __ Blt(TMP, AT, label);
3415 break;
3416 case kCondB:
3417 __ Bltu(lhs_high, rhs_high, label);
3418 __ Sltu(TMP, rhs_high, lhs_high);
3419 __ Sltu(AT, lhs_low, rhs_low);
3420 __ Blt(TMP, AT, label);
3421 break;
3422 case kCondAE:
3423 __ Bltu(rhs_high, lhs_high, label);
3424 __ Sltu(TMP, lhs_high, rhs_high);
3425 __ Sltu(AT, lhs_low, rhs_low);
3426 __ Or(TMP, TMP, AT);
3427 __ Beqz(TMP, label);
3428 break;
3429 case kCondBE:
3430 __ Bltu(lhs_high, rhs_high, label);
3431 __ Sltu(TMP, rhs_high, lhs_high);
3432 __ Sltu(AT, rhs_low, lhs_low);
3433 __ Or(TMP, TMP, AT);
3434 __ Beqz(TMP, label);
3435 break;
3436 case kCondA:
3437 __ Bltu(rhs_high, lhs_high, label);
3438 __ Sltu(TMP, lhs_high, rhs_high);
3439 __ Sltu(AT, rhs_low, lhs_low);
3440 __ Blt(TMP, AT, label);
3441 break;
3442 }
3443 }
3444}
3445
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003446void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3447 bool gt_bias,
3448 Primitive::Type type,
3449 LocationSummary* locations) {
3450 Register dst = locations->Out().AsRegister<Register>();
3451 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3452 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3453 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3454 if (type == Primitive::kPrimFloat) {
3455 if (isR6) {
3456 switch (cond) {
3457 case kCondEQ:
3458 __ CmpEqS(FTMP, lhs, rhs);
3459 __ Mfc1(dst, FTMP);
3460 __ Andi(dst, dst, 1);
3461 break;
3462 case kCondNE:
3463 __ CmpEqS(FTMP, lhs, rhs);
3464 __ Mfc1(dst, FTMP);
3465 __ Addiu(dst, dst, 1);
3466 break;
3467 case kCondLT:
3468 if (gt_bias) {
3469 __ CmpLtS(FTMP, lhs, rhs);
3470 } else {
3471 __ CmpUltS(FTMP, lhs, rhs);
3472 }
3473 __ Mfc1(dst, FTMP);
3474 __ Andi(dst, dst, 1);
3475 break;
3476 case kCondLE:
3477 if (gt_bias) {
3478 __ CmpLeS(FTMP, lhs, rhs);
3479 } else {
3480 __ CmpUleS(FTMP, lhs, rhs);
3481 }
3482 __ Mfc1(dst, FTMP);
3483 __ Andi(dst, dst, 1);
3484 break;
3485 case kCondGT:
3486 if (gt_bias) {
3487 __ CmpUltS(FTMP, rhs, lhs);
3488 } else {
3489 __ CmpLtS(FTMP, rhs, lhs);
3490 }
3491 __ Mfc1(dst, FTMP);
3492 __ Andi(dst, dst, 1);
3493 break;
3494 case kCondGE:
3495 if (gt_bias) {
3496 __ CmpUleS(FTMP, rhs, lhs);
3497 } else {
3498 __ CmpLeS(FTMP, rhs, lhs);
3499 }
3500 __ Mfc1(dst, FTMP);
3501 __ Andi(dst, dst, 1);
3502 break;
3503 default:
3504 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3505 UNREACHABLE();
3506 }
3507 } else {
3508 switch (cond) {
3509 case kCondEQ:
3510 __ CeqS(0, lhs, rhs);
3511 __ LoadConst32(dst, 1);
3512 __ Movf(dst, ZERO, 0);
3513 break;
3514 case kCondNE:
3515 __ CeqS(0, lhs, rhs);
3516 __ LoadConst32(dst, 1);
3517 __ Movt(dst, ZERO, 0);
3518 break;
3519 case kCondLT:
3520 if (gt_bias) {
3521 __ ColtS(0, lhs, rhs);
3522 } else {
3523 __ CultS(0, lhs, rhs);
3524 }
3525 __ LoadConst32(dst, 1);
3526 __ Movf(dst, ZERO, 0);
3527 break;
3528 case kCondLE:
3529 if (gt_bias) {
3530 __ ColeS(0, lhs, rhs);
3531 } else {
3532 __ CuleS(0, lhs, rhs);
3533 }
3534 __ LoadConst32(dst, 1);
3535 __ Movf(dst, ZERO, 0);
3536 break;
3537 case kCondGT:
3538 if (gt_bias) {
3539 __ CultS(0, rhs, lhs);
3540 } else {
3541 __ ColtS(0, rhs, lhs);
3542 }
3543 __ LoadConst32(dst, 1);
3544 __ Movf(dst, ZERO, 0);
3545 break;
3546 case kCondGE:
3547 if (gt_bias) {
3548 __ CuleS(0, rhs, lhs);
3549 } else {
3550 __ ColeS(0, rhs, lhs);
3551 }
3552 __ LoadConst32(dst, 1);
3553 __ Movf(dst, ZERO, 0);
3554 break;
3555 default:
3556 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3557 UNREACHABLE();
3558 }
3559 }
3560 } else {
3561 DCHECK_EQ(type, Primitive::kPrimDouble);
3562 if (isR6) {
3563 switch (cond) {
3564 case kCondEQ:
3565 __ CmpEqD(FTMP, lhs, rhs);
3566 __ Mfc1(dst, FTMP);
3567 __ Andi(dst, dst, 1);
3568 break;
3569 case kCondNE:
3570 __ CmpEqD(FTMP, lhs, rhs);
3571 __ Mfc1(dst, FTMP);
3572 __ Addiu(dst, dst, 1);
3573 break;
3574 case kCondLT:
3575 if (gt_bias) {
3576 __ CmpLtD(FTMP, lhs, rhs);
3577 } else {
3578 __ CmpUltD(FTMP, lhs, rhs);
3579 }
3580 __ Mfc1(dst, FTMP);
3581 __ Andi(dst, dst, 1);
3582 break;
3583 case kCondLE:
3584 if (gt_bias) {
3585 __ CmpLeD(FTMP, lhs, rhs);
3586 } else {
3587 __ CmpUleD(FTMP, lhs, rhs);
3588 }
3589 __ Mfc1(dst, FTMP);
3590 __ Andi(dst, dst, 1);
3591 break;
3592 case kCondGT:
3593 if (gt_bias) {
3594 __ CmpUltD(FTMP, rhs, lhs);
3595 } else {
3596 __ CmpLtD(FTMP, rhs, lhs);
3597 }
3598 __ Mfc1(dst, FTMP);
3599 __ Andi(dst, dst, 1);
3600 break;
3601 case kCondGE:
3602 if (gt_bias) {
3603 __ CmpUleD(FTMP, rhs, lhs);
3604 } else {
3605 __ CmpLeD(FTMP, rhs, lhs);
3606 }
3607 __ Mfc1(dst, FTMP);
3608 __ Andi(dst, dst, 1);
3609 break;
3610 default:
3611 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3612 UNREACHABLE();
3613 }
3614 } else {
3615 switch (cond) {
3616 case kCondEQ:
3617 __ CeqD(0, lhs, rhs);
3618 __ LoadConst32(dst, 1);
3619 __ Movf(dst, ZERO, 0);
3620 break;
3621 case kCondNE:
3622 __ CeqD(0, lhs, rhs);
3623 __ LoadConst32(dst, 1);
3624 __ Movt(dst, ZERO, 0);
3625 break;
3626 case kCondLT:
3627 if (gt_bias) {
3628 __ ColtD(0, lhs, rhs);
3629 } else {
3630 __ CultD(0, lhs, rhs);
3631 }
3632 __ LoadConst32(dst, 1);
3633 __ Movf(dst, ZERO, 0);
3634 break;
3635 case kCondLE:
3636 if (gt_bias) {
3637 __ ColeD(0, lhs, rhs);
3638 } else {
3639 __ CuleD(0, lhs, rhs);
3640 }
3641 __ LoadConst32(dst, 1);
3642 __ Movf(dst, ZERO, 0);
3643 break;
3644 case kCondGT:
3645 if (gt_bias) {
3646 __ CultD(0, rhs, lhs);
3647 } else {
3648 __ ColtD(0, rhs, lhs);
3649 }
3650 __ LoadConst32(dst, 1);
3651 __ Movf(dst, ZERO, 0);
3652 break;
3653 case kCondGE:
3654 if (gt_bias) {
3655 __ CuleD(0, rhs, lhs);
3656 } else {
3657 __ ColeD(0, rhs, lhs);
3658 }
3659 __ LoadConst32(dst, 1);
3660 __ Movf(dst, ZERO, 0);
3661 break;
3662 default:
3663 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3664 UNREACHABLE();
3665 }
3666 }
3667 }
3668}
3669
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003670bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3671 bool gt_bias,
3672 Primitive::Type type,
3673 LocationSummary* input_locations,
3674 int cc) {
3675 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3676 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3677 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3678 if (type == Primitive::kPrimFloat) {
3679 switch (cond) {
3680 case kCondEQ:
3681 __ CeqS(cc, lhs, rhs);
3682 return false;
3683 case kCondNE:
3684 __ CeqS(cc, lhs, rhs);
3685 return true;
3686 case kCondLT:
3687 if (gt_bias) {
3688 __ ColtS(cc, lhs, rhs);
3689 } else {
3690 __ CultS(cc, lhs, rhs);
3691 }
3692 return false;
3693 case kCondLE:
3694 if (gt_bias) {
3695 __ ColeS(cc, lhs, rhs);
3696 } else {
3697 __ CuleS(cc, lhs, rhs);
3698 }
3699 return false;
3700 case kCondGT:
3701 if (gt_bias) {
3702 __ CultS(cc, rhs, lhs);
3703 } else {
3704 __ ColtS(cc, rhs, lhs);
3705 }
3706 return false;
3707 case kCondGE:
3708 if (gt_bias) {
3709 __ CuleS(cc, rhs, lhs);
3710 } else {
3711 __ ColeS(cc, rhs, lhs);
3712 }
3713 return false;
3714 default:
3715 LOG(FATAL) << "Unexpected non-floating-point condition";
3716 UNREACHABLE();
3717 }
3718 } else {
3719 DCHECK_EQ(type, Primitive::kPrimDouble);
3720 switch (cond) {
3721 case kCondEQ:
3722 __ CeqD(cc, lhs, rhs);
3723 return false;
3724 case kCondNE:
3725 __ CeqD(cc, lhs, rhs);
3726 return true;
3727 case kCondLT:
3728 if (gt_bias) {
3729 __ ColtD(cc, lhs, rhs);
3730 } else {
3731 __ CultD(cc, lhs, rhs);
3732 }
3733 return false;
3734 case kCondLE:
3735 if (gt_bias) {
3736 __ ColeD(cc, lhs, rhs);
3737 } else {
3738 __ CuleD(cc, lhs, rhs);
3739 }
3740 return false;
3741 case kCondGT:
3742 if (gt_bias) {
3743 __ CultD(cc, rhs, lhs);
3744 } else {
3745 __ ColtD(cc, rhs, lhs);
3746 }
3747 return false;
3748 case kCondGE:
3749 if (gt_bias) {
3750 __ CuleD(cc, rhs, lhs);
3751 } else {
3752 __ ColeD(cc, rhs, lhs);
3753 }
3754 return false;
3755 default:
3756 LOG(FATAL) << "Unexpected non-floating-point condition";
3757 UNREACHABLE();
3758 }
3759 }
3760}
3761
3762bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3763 bool gt_bias,
3764 Primitive::Type type,
3765 LocationSummary* input_locations,
3766 FRegister dst) {
3767 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3768 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3769 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3770 if (type == Primitive::kPrimFloat) {
3771 switch (cond) {
3772 case kCondEQ:
3773 __ CmpEqS(dst, lhs, rhs);
3774 return false;
3775 case kCondNE:
3776 __ CmpEqS(dst, lhs, rhs);
3777 return true;
3778 case kCondLT:
3779 if (gt_bias) {
3780 __ CmpLtS(dst, lhs, rhs);
3781 } else {
3782 __ CmpUltS(dst, lhs, rhs);
3783 }
3784 return false;
3785 case kCondLE:
3786 if (gt_bias) {
3787 __ CmpLeS(dst, lhs, rhs);
3788 } else {
3789 __ CmpUleS(dst, lhs, rhs);
3790 }
3791 return false;
3792 case kCondGT:
3793 if (gt_bias) {
3794 __ CmpUltS(dst, rhs, lhs);
3795 } else {
3796 __ CmpLtS(dst, rhs, lhs);
3797 }
3798 return false;
3799 case kCondGE:
3800 if (gt_bias) {
3801 __ CmpUleS(dst, rhs, lhs);
3802 } else {
3803 __ CmpLeS(dst, rhs, lhs);
3804 }
3805 return false;
3806 default:
3807 LOG(FATAL) << "Unexpected non-floating-point condition";
3808 UNREACHABLE();
3809 }
3810 } else {
3811 DCHECK_EQ(type, Primitive::kPrimDouble);
3812 switch (cond) {
3813 case kCondEQ:
3814 __ CmpEqD(dst, lhs, rhs);
3815 return false;
3816 case kCondNE:
3817 __ CmpEqD(dst, lhs, rhs);
3818 return true;
3819 case kCondLT:
3820 if (gt_bias) {
3821 __ CmpLtD(dst, lhs, rhs);
3822 } else {
3823 __ CmpUltD(dst, lhs, rhs);
3824 }
3825 return false;
3826 case kCondLE:
3827 if (gt_bias) {
3828 __ CmpLeD(dst, lhs, rhs);
3829 } else {
3830 __ CmpUleD(dst, lhs, rhs);
3831 }
3832 return false;
3833 case kCondGT:
3834 if (gt_bias) {
3835 __ CmpUltD(dst, rhs, lhs);
3836 } else {
3837 __ CmpLtD(dst, rhs, lhs);
3838 }
3839 return false;
3840 case kCondGE:
3841 if (gt_bias) {
3842 __ CmpUleD(dst, rhs, lhs);
3843 } else {
3844 __ CmpLeD(dst, rhs, lhs);
3845 }
3846 return false;
3847 default:
3848 LOG(FATAL) << "Unexpected non-floating-point condition";
3849 UNREACHABLE();
3850 }
3851 }
3852}
3853
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003854void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3855 bool gt_bias,
3856 Primitive::Type type,
3857 LocationSummary* locations,
3858 MipsLabel* label) {
3859 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3860 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3861 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3862 if (type == Primitive::kPrimFloat) {
3863 if (isR6) {
3864 switch (cond) {
3865 case kCondEQ:
3866 __ CmpEqS(FTMP, lhs, rhs);
3867 __ Bc1nez(FTMP, label);
3868 break;
3869 case kCondNE:
3870 __ CmpEqS(FTMP, lhs, rhs);
3871 __ Bc1eqz(FTMP, label);
3872 break;
3873 case kCondLT:
3874 if (gt_bias) {
3875 __ CmpLtS(FTMP, lhs, rhs);
3876 } else {
3877 __ CmpUltS(FTMP, lhs, rhs);
3878 }
3879 __ Bc1nez(FTMP, label);
3880 break;
3881 case kCondLE:
3882 if (gt_bias) {
3883 __ CmpLeS(FTMP, lhs, rhs);
3884 } else {
3885 __ CmpUleS(FTMP, lhs, rhs);
3886 }
3887 __ Bc1nez(FTMP, label);
3888 break;
3889 case kCondGT:
3890 if (gt_bias) {
3891 __ CmpUltS(FTMP, rhs, lhs);
3892 } else {
3893 __ CmpLtS(FTMP, rhs, lhs);
3894 }
3895 __ Bc1nez(FTMP, label);
3896 break;
3897 case kCondGE:
3898 if (gt_bias) {
3899 __ CmpUleS(FTMP, rhs, lhs);
3900 } else {
3901 __ CmpLeS(FTMP, rhs, lhs);
3902 }
3903 __ Bc1nez(FTMP, label);
3904 break;
3905 default:
3906 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003907 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003908 }
3909 } else {
3910 switch (cond) {
3911 case kCondEQ:
3912 __ CeqS(0, lhs, rhs);
3913 __ Bc1t(0, label);
3914 break;
3915 case kCondNE:
3916 __ CeqS(0, lhs, rhs);
3917 __ Bc1f(0, label);
3918 break;
3919 case kCondLT:
3920 if (gt_bias) {
3921 __ ColtS(0, lhs, rhs);
3922 } else {
3923 __ CultS(0, lhs, rhs);
3924 }
3925 __ Bc1t(0, label);
3926 break;
3927 case kCondLE:
3928 if (gt_bias) {
3929 __ ColeS(0, lhs, rhs);
3930 } else {
3931 __ CuleS(0, lhs, rhs);
3932 }
3933 __ Bc1t(0, label);
3934 break;
3935 case kCondGT:
3936 if (gt_bias) {
3937 __ CultS(0, rhs, lhs);
3938 } else {
3939 __ ColtS(0, rhs, lhs);
3940 }
3941 __ Bc1t(0, label);
3942 break;
3943 case kCondGE:
3944 if (gt_bias) {
3945 __ CuleS(0, rhs, lhs);
3946 } else {
3947 __ ColeS(0, rhs, lhs);
3948 }
3949 __ Bc1t(0, label);
3950 break;
3951 default:
3952 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003953 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003954 }
3955 }
3956 } else {
3957 DCHECK_EQ(type, Primitive::kPrimDouble);
3958 if (isR6) {
3959 switch (cond) {
3960 case kCondEQ:
3961 __ CmpEqD(FTMP, lhs, rhs);
3962 __ Bc1nez(FTMP, label);
3963 break;
3964 case kCondNE:
3965 __ CmpEqD(FTMP, lhs, rhs);
3966 __ Bc1eqz(FTMP, label);
3967 break;
3968 case kCondLT:
3969 if (gt_bias) {
3970 __ CmpLtD(FTMP, lhs, rhs);
3971 } else {
3972 __ CmpUltD(FTMP, lhs, rhs);
3973 }
3974 __ Bc1nez(FTMP, label);
3975 break;
3976 case kCondLE:
3977 if (gt_bias) {
3978 __ CmpLeD(FTMP, lhs, rhs);
3979 } else {
3980 __ CmpUleD(FTMP, lhs, rhs);
3981 }
3982 __ Bc1nez(FTMP, label);
3983 break;
3984 case kCondGT:
3985 if (gt_bias) {
3986 __ CmpUltD(FTMP, rhs, lhs);
3987 } else {
3988 __ CmpLtD(FTMP, rhs, lhs);
3989 }
3990 __ Bc1nez(FTMP, label);
3991 break;
3992 case kCondGE:
3993 if (gt_bias) {
3994 __ CmpUleD(FTMP, rhs, lhs);
3995 } else {
3996 __ CmpLeD(FTMP, rhs, lhs);
3997 }
3998 __ Bc1nez(FTMP, label);
3999 break;
4000 default:
4001 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004002 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004003 }
4004 } else {
4005 switch (cond) {
4006 case kCondEQ:
4007 __ CeqD(0, lhs, rhs);
4008 __ Bc1t(0, label);
4009 break;
4010 case kCondNE:
4011 __ CeqD(0, lhs, rhs);
4012 __ Bc1f(0, label);
4013 break;
4014 case kCondLT:
4015 if (gt_bias) {
4016 __ ColtD(0, lhs, rhs);
4017 } else {
4018 __ CultD(0, lhs, rhs);
4019 }
4020 __ Bc1t(0, label);
4021 break;
4022 case kCondLE:
4023 if (gt_bias) {
4024 __ ColeD(0, lhs, rhs);
4025 } else {
4026 __ CuleD(0, lhs, rhs);
4027 }
4028 __ Bc1t(0, label);
4029 break;
4030 case kCondGT:
4031 if (gt_bias) {
4032 __ CultD(0, rhs, lhs);
4033 } else {
4034 __ ColtD(0, rhs, lhs);
4035 }
4036 __ Bc1t(0, label);
4037 break;
4038 case kCondGE:
4039 if (gt_bias) {
4040 __ CuleD(0, rhs, lhs);
4041 } else {
4042 __ ColeD(0, rhs, lhs);
4043 }
4044 __ Bc1t(0, label);
4045 break;
4046 default:
4047 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004048 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004049 }
4050 }
4051 }
4052}
4053
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004054void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004055 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004056 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004057 MipsLabel* false_target) {
4058 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004059
David Brazdil0debae72015-11-12 18:37:00 +00004060 if (true_target == nullptr && false_target == nullptr) {
4061 // Nothing to do. The code always falls through.
4062 return;
4063 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004064 // Constant condition, statically compared against "true" (integer value 1).
4065 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004066 if (true_target != nullptr) {
4067 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004068 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004069 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004070 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004071 if (false_target != nullptr) {
4072 __ B(false_target);
4073 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004074 }
David Brazdil0debae72015-11-12 18:37:00 +00004075 return;
4076 }
4077
4078 // The following code generates these patterns:
4079 // (1) true_target == nullptr && false_target != nullptr
4080 // - opposite condition true => branch to false_target
4081 // (2) true_target != nullptr && false_target == nullptr
4082 // - condition true => branch to true_target
4083 // (3) true_target != nullptr && false_target != nullptr
4084 // - condition true => branch to true_target
4085 // - branch to false_target
4086 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004087 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004088 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004089 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004090 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004091 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4092 } else {
4093 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4094 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004095 } else {
4096 // The condition instruction has not been materialized, use its inputs as
4097 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004098 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004099 Primitive::Type type = condition->InputAt(0)->GetType();
4100 LocationSummary* locations = cond->GetLocations();
4101 IfCondition if_cond = condition->GetCondition();
4102 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004103
David Brazdil0debae72015-11-12 18:37:00 +00004104 if (true_target == nullptr) {
4105 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004106 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004107 }
4108
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004109 switch (type) {
4110 default:
4111 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4112 break;
4113 case Primitive::kPrimLong:
4114 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4115 break;
4116 case Primitive::kPrimFloat:
4117 case Primitive::kPrimDouble:
4118 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4119 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004120 }
4121 }
David Brazdil0debae72015-11-12 18:37:00 +00004122
4123 // If neither branch falls through (case 3), the conditional branch to `true_target`
4124 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4125 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004126 __ B(false_target);
4127 }
4128}
4129
4130void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4131 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004132 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004133 locations->SetInAt(0, Location::RequiresRegister());
4134 }
4135}
4136
4137void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004138 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4139 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4140 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4141 nullptr : codegen_->GetLabelOf(true_successor);
4142 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4143 nullptr : codegen_->GetLabelOf(false_successor);
4144 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004145}
4146
4147void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4148 LocationSummary* locations = new (GetGraph()->GetArena())
4149 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004150 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004151 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004152 locations->SetInAt(0, Location::RequiresRegister());
4153 }
4154}
4155
4156void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004157 SlowPathCodeMIPS* slow_path =
4158 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004159 GenerateTestAndBranch(deoptimize,
4160 /* condition_input_index */ 0,
4161 slow_path->GetEntryLabel(),
4162 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004163}
4164
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004165// This function returns true if a conditional move can be generated for HSelect.
4166// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4167// branches and regular moves.
4168//
4169// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4170//
4171// While determining feasibility of a conditional move and setting inputs/outputs
4172// are two distinct tasks, this function does both because they share quite a bit
4173// of common logic.
4174static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4175 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4176 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4177 HCondition* condition = cond->AsCondition();
4178
4179 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4180 Primitive::Type dst_type = select->GetType();
4181
4182 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4183 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4184 bool is_true_value_zero_constant =
4185 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4186 bool is_false_value_zero_constant =
4187 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4188
4189 bool can_move_conditionally = false;
4190 bool use_const_for_false_in = false;
4191 bool use_const_for_true_in = false;
4192
4193 if (!cond->IsConstant()) {
4194 switch (cond_type) {
4195 default:
4196 switch (dst_type) {
4197 default:
4198 // Moving int on int condition.
4199 if (is_r6) {
4200 if (is_true_value_zero_constant) {
4201 // seleqz out_reg, false_reg, cond_reg
4202 can_move_conditionally = true;
4203 use_const_for_true_in = true;
4204 } else if (is_false_value_zero_constant) {
4205 // selnez out_reg, true_reg, cond_reg
4206 can_move_conditionally = true;
4207 use_const_for_false_in = true;
4208 } else if (materialized) {
4209 // Not materializing unmaterialized int conditions
4210 // to keep the instruction count low.
4211 // selnez AT, true_reg, cond_reg
4212 // seleqz TMP, false_reg, cond_reg
4213 // or out_reg, AT, TMP
4214 can_move_conditionally = true;
4215 }
4216 } else {
4217 // movn out_reg, true_reg/ZERO, cond_reg
4218 can_move_conditionally = true;
4219 use_const_for_true_in = is_true_value_zero_constant;
4220 }
4221 break;
4222 case Primitive::kPrimLong:
4223 // Moving long on int condition.
4224 if (is_r6) {
4225 if (is_true_value_zero_constant) {
4226 // seleqz out_reg_lo, false_reg_lo, cond_reg
4227 // seleqz out_reg_hi, false_reg_hi, cond_reg
4228 can_move_conditionally = true;
4229 use_const_for_true_in = true;
4230 } else if (is_false_value_zero_constant) {
4231 // selnez out_reg_lo, true_reg_lo, cond_reg
4232 // selnez out_reg_hi, true_reg_hi, cond_reg
4233 can_move_conditionally = true;
4234 use_const_for_false_in = true;
4235 }
4236 // Other long conditional moves would generate 6+ instructions,
4237 // which is too many.
4238 } else {
4239 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4240 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4241 can_move_conditionally = true;
4242 use_const_for_true_in = is_true_value_zero_constant;
4243 }
4244 break;
4245 case Primitive::kPrimFloat:
4246 case Primitive::kPrimDouble:
4247 // Moving float/double on int condition.
4248 if (is_r6) {
4249 if (materialized) {
4250 // Not materializing unmaterialized int conditions
4251 // to keep the instruction count low.
4252 can_move_conditionally = true;
4253 if (is_true_value_zero_constant) {
4254 // sltu TMP, ZERO, cond_reg
4255 // mtc1 TMP, temp_cond_reg
4256 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4257 use_const_for_true_in = true;
4258 } else if (is_false_value_zero_constant) {
4259 // sltu TMP, ZERO, cond_reg
4260 // mtc1 TMP, temp_cond_reg
4261 // selnez.fmt out_reg, true_reg, temp_cond_reg
4262 use_const_for_false_in = true;
4263 } else {
4264 // sltu TMP, ZERO, cond_reg
4265 // mtc1 TMP, temp_cond_reg
4266 // sel.fmt temp_cond_reg, false_reg, true_reg
4267 // mov.fmt out_reg, temp_cond_reg
4268 }
4269 }
4270 } else {
4271 // movn.fmt out_reg, true_reg, cond_reg
4272 can_move_conditionally = true;
4273 }
4274 break;
4275 }
4276 break;
4277 case Primitive::kPrimLong:
4278 // We don't materialize long comparison now
4279 // and use conditional branches instead.
4280 break;
4281 case Primitive::kPrimFloat:
4282 case Primitive::kPrimDouble:
4283 switch (dst_type) {
4284 default:
4285 // Moving int on float/double condition.
4286 if (is_r6) {
4287 if (is_true_value_zero_constant) {
4288 // mfc1 TMP, temp_cond_reg
4289 // seleqz out_reg, false_reg, TMP
4290 can_move_conditionally = true;
4291 use_const_for_true_in = true;
4292 } else if (is_false_value_zero_constant) {
4293 // mfc1 TMP, temp_cond_reg
4294 // selnez out_reg, true_reg, TMP
4295 can_move_conditionally = true;
4296 use_const_for_false_in = true;
4297 } else {
4298 // mfc1 TMP, temp_cond_reg
4299 // selnez AT, true_reg, TMP
4300 // seleqz TMP, false_reg, TMP
4301 // or out_reg, AT, TMP
4302 can_move_conditionally = true;
4303 }
4304 } else {
4305 // movt out_reg, true_reg/ZERO, cc
4306 can_move_conditionally = true;
4307 use_const_for_true_in = is_true_value_zero_constant;
4308 }
4309 break;
4310 case Primitive::kPrimLong:
4311 // Moving long on float/double condition.
4312 if (is_r6) {
4313 if (is_true_value_zero_constant) {
4314 // mfc1 TMP, temp_cond_reg
4315 // seleqz out_reg_lo, false_reg_lo, TMP
4316 // seleqz out_reg_hi, false_reg_hi, TMP
4317 can_move_conditionally = true;
4318 use_const_for_true_in = true;
4319 } else if (is_false_value_zero_constant) {
4320 // mfc1 TMP, temp_cond_reg
4321 // selnez out_reg_lo, true_reg_lo, TMP
4322 // selnez out_reg_hi, true_reg_hi, TMP
4323 can_move_conditionally = true;
4324 use_const_for_false_in = true;
4325 }
4326 // Other long conditional moves would generate 6+ instructions,
4327 // which is too many.
4328 } else {
4329 // movt out_reg_lo, true_reg_lo/ZERO, cc
4330 // movt out_reg_hi, true_reg_hi/ZERO, cc
4331 can_move_conditionally = true;
4332 use_const_for_true_in = is_true_value_zero_constant;
4333 }
4334 break;
4335 case Primitive::kPrimFloat:
4336 case Primitive::kPrimDouble:
4337 // Moving float/double on float/double condition.
4338 if (is_r6) {
4339 can_move_conditionally = true;
4340 if (is_true_value_zero_constant) {
4341 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4342 use_const_for_true_in = true;
4343 } else if (is_false_value_zero_constant) {
4344 // selnez.fmt out_reg, true_reg, temp_cond_reg
4345 use_const_for_false_in = true;
4346 } else {
4347 // sel.fmt temp_cond_reg, false_reg, true_reg
4348 // mov.fmt out_reg, temp_cond_reg
4349 }
4350 } else {
4351 // movt.fmt out_reg, true_reg, cc
4352 can_move_conditionally = true;
4353 }
4354 break;
4355 }
4356 break;
4357 }
4358 }
4359
4360 if (can_move_conditionally) {
4361 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4362 } else {
4363 DCHECK(!use_const_for_false_in);
4364 DCHECK(!use_const_for_true_in);
4365 }
4366
4367 if (locations_to_set != nullptr) {
4368 if (use_const_for_false_in) {
4369 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4370 } else {
4371 locations_to_set->SetInAt(0,
4372 Primitive::IsFloatingPointType(dst_type)
4373 ? Location::RequiresFpuRegister()
4374 : Location::RequiresRegister());
4375 }
4376 if (use_const_for_true_in) {
4377 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4378 } else {
4379 locations_to_set->SetInAt(1,
4380 Primitive::IsFloatingPointType(dst_type)
4381 ? Location::RequiresFpuRegister()
4382 : Location::RequiresRegister());
4383 }
4384 if (materialized) {
4385 locations_to_set->SetInAt(2, Location::RequiresRegister());
4386 }
4387 // On R6 we don't require the output to be the same as the
4388 // first input for conditional moves unlike on R2.
4389 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4390 if (is_out_same_as_first_in) {
4391 locations_to_set->SetOut(Location::SameAsFirstInput());
4392 } else {
4393 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4394 ? Location::RequiresFpuRegister()
4395 : Location::RequiresRegister());
4396 }
4397 }
4398
4399 return can_move_conditionally;
4400}
4401
4402void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4403 LocationSummary* locations = select->GetLocations();
4404 Location dst = locations->Out();
4405 Location src = locations->InAt(1);
4406 Register src_reg = ZERO;
4407 Register src_reg_high = ZERO;
4408 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4409 Register cond_reg = TMP;
4410 int cond_cc = 0;
4411 Primitive::Type cond_type = Primitive::kPrimInt;
4412 bool cond_inverted = false;
4413 Primitive::Type dst_type = select->GetType();
4414
4415 if (IsBooleanValueOrMaterializedCondition(cond)) {
4416 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4417 } else {
4418 HCondition* condition = cond->AsCondition();
4419 LocationSummary* cond_locations = cond->GetLocations();
4420 IfCondition if_cond = condition->GetCondition();
4421 cond_type = condition->InputAt(0)->GetType();
4422 switch (cond_type) {
4423 default:
4424 DCHECK_NE(cond_type, Primitive::kPrimLong);
4425 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4426 break;
4427 case Primitive::kPrimFloat:
4428 case Primitive::kPrimDouble:
4429 cond_inverted = MaterializeFpCompareR2(if_cond,
4430 condition->IsGtBias(),
4431 cond_type,
4432 cond_locations,
4433 cond_cc);
4434 break;
4435 }
4436 }
4437
4438 DCHECK(dst.Equals(locations->InAt(0)));
4439 if (src.IsRegister()) {
4440 src_reg = src.AsRegister<Register>();
4441 } else if (src.IsRegisterPair()) {
4442 src_reg = src.AsRegisterPairLow<Register>();
4443 src_reg_high = src.AsRegisterPairHigh<Register>();
4444 } else if (src.IsConstant()) {
4445 DCHECK(src.GetConstant()->IsZeroBitPattern());
4446 }
4447
4448 switch (cond_type) {
4449 default:
4450 switch (dst_type) {
4451 default:
4452 if (cond_inverted) {
4453 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4454 } else {
4455 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4456 }
4457 break;
4458 case Primitive::kPrimLong:
4459 if (cond_inverted) {
4460 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4461 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4462 } else {
4463 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4464 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4465 }
4466 break;
4467 case Primitive::kPrimFloat:
4468 if (cond_inverted) {
4469 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4470 } else {
4471 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4472 }
4473 break;
4474 case Primitive::kPrimDouble:
4475 if (cond_inverted) {
4476 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4477 } else {
4478 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4479 }
4480 break;
4481 }
4482 break;
4483 case Primitive::kPrimLong:
4484 LOG(FATAL) << "Unreachable";
4485 UNREACHABLE();
4486 case Primitive::kPrimFloat:
4487 case Primitive::kPrimDouble:
4488 switch (dst_type) {
4489 default:
4490 if (cond_inverted) {
4491 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4492 } else {
4493 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4494 }
4495 break;
4496 case Primitive::kPrimLong:
4497 if (cond_inverted) {
4498 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4499 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4500 } else {
4501 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4502 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4503 }
4504 break;
4505 case Primitive::kPrimFloat:
4506 if (cond_inverted) {
4507 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4508 } else {
4509 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4510 }
4511 break;
4512 case Primitive::kPrimDouble:
4513 if (cond_inverted) {
4514 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4515 } else {
4516 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4517 }
4518 break;
4519 }
4520 break;
4521 }
4522}
4523
4524void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4525 LocationSummary* locations = select->GetLocations();
4526 Location dst = locations->Out();
4527 Location false_src = locations->InAt(0);
4528 Location true_src = locations->InAt(1);
4529 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4530 Register cond_reg = TMP;
4531 FRegister fcond_reg = FTMP;
4532 Primitive::Type cond_type = Primitive::kPrimInt;
4533 bool cond_inverted = false;
4534 Primitive::Type dst_type = select->GetType();
4535
4536 if (IsBooleanValueOrMaterializedCondition(cond)) {
4537 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4538 } else {
4539 HCondition* condition = cond->AsCondition();
4540 LocationSummary* cond_locations = cond->GetLocations();
4541 IfCondition if_cond = condition->GetCondition();
4542 cond_type = condition->InputAt(0)->GetType();
4543 switch (cond_type) {
4544 default:
4545 DCHECK_NE(cond_type, Primitive::kPrimLong);
4546 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4547 break;
4548 case Primitive::kPrimFloat:
4549 case Primitive::kPrimDouble:
4550 cond_inverted = MaterializeFpCompareR6(if_cond,
4551 condition->IsGtBias(),
4552 cond_type,
4553 cond_locations,
4554 fcond_reg);
4555 break;
4556 }
4557 }
4558
4559 if (true_src.IsConstant()) {
4560 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4561 }
4562 if (false_src.IsConstant()) {
4563 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4564 }
4565
4566 switch (dst_type) {
4567 default:
4568 if (Primitive::IsFloatingPointType(cond_type)) {
4569 __ Mfc1(cond_reg, fcond_reg);
4570 }
4571 if (true_src.IsConstant()) {
4572 if (cond_inverted) {
4573 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4574 } else {
4575 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4576 }
4577 } else if (false_src.IsConstant()) {
4578 if (cond_inverted) {
4579 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4580 } else {
4581 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4582 }
4583 } else {
4584 DCHECK_NE(cond_reg, AT);
4585 if (cond_inverted) {
4586 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4587 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4588 } else {
4589 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4590 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4591 }
4592 __ Or(dst.AsRegister<Register>(), AT, TMP);
4593 }
4594 break;
4595 case Primitive::kPrimLong: {
4596 if (Primitive::IsFloatingPointType(cond_type)) {
4597 __ Mfc1(cond_reg, fcond_reg);
4598 }
4599 Register dst_lo = dst.AsRegisterPairLow<Register>();
4600 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4601 if (true_src.IsConstant()) {
4602 Register src_lo = false_src.AsRegisterPairLow<Register>();
4603 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4604 if (cond_inverted) {
4605 __ Selnez(dst_lo, src_lo, cond_reg);
4606 __ Selnez(dst_hi, src_hi, cond_reg);
4607 } else {
4608 __ Seleqz(dst_lo, src_lo, cond_reg);
4609 __ Seleqz(dst_hi, src_hi, cond_reg);
4610 }
4611 } else {
4612 DCHECK(false_src.IsConstant());
4613 Register src_lo = true_src.AsRegisterPairLow<Register>();
4614 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4615 if (cond_inverted) {
4616 __ Seleqz(dst_lo, src_lo, cond_reg);
4617 __ Seleqz(dst_hi, src_hi, cond_reg);
4618 } else {
4619 __ Selnez(dst_lo, src_lo, cond_reg);
4620 __ Selnez(dst_hi, src_hi, cond_reg);
4621 }
4622 }
4623 break;
4624 }
4625 case Primitive::kPrimFloat: {
4626 if (!Primitive::IsFloatingPointType(cond_type)) {
4627 // sel*.fmt tests bit 0 of the condition register, account for that.
4628 __ Sltu(TMP, ZERO, cond_reg);
4629 __ Mtc1(TMP, fcond_reg);
4630 }
4631 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4632 if (true_src.IsConstant()) {
4633 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4634 if (cond_inverted) {
4635 __ SelnezS(dst_reg, src_reg, fcond_reg);
4636 } else {
4637 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4638 }
4639 } else if (false_src.IsConstant()) {
4640 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4641 if (cond_inverted) {
4642 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4643 } else {
4644 __ SelnezS(dst_reg, src_reg, fcond_reg);
4645 }
4646 } else {
4647 if (cond_inverted) {
4648 __ SelS(fcond_reg,
4649 true_src.AsFpuRegister<FRegister>(),
4650 false_src.AsFpuRegister<FRegister>());
4651 } else {
4652 __ SelS(fcond_reg,
4653 false_src.AsFpuRegister<FRegister>(),
4654 true_src.AsFpuRegister<FRegister>());
4655 }
4656 __ MovS(dst_reg, fcond_reg);
4657 }
4658 break;
4659 }
4660 case Primitive::kPrimDouble: {
4661 if (!Primitive::IsFloatingPointType(cond_type)) {
4662 // sel*.fmt tests bit 0 of the condition register, account for that.
4663 __ Sltu(TMP, ZERO, cond_reg);
4664 __ Mtc1(TMP, fcond_reg);
4665 }
4666 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4667 if (true_src.IsConstant()) {
4668 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4669 if (cond_inverted) {
4670 __ SelnezD(dst_reg, src_reg, fcond_reg);
4671 } else {
4672 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4673 }
4674 } else if (false_src.IsConstant()) {
4675 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4676 if (cond_inverted) {
4677 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4678 } else {
4679 __ SelnezD(dst_reg, src_reg, fcond_reg);
4680 }
4681 } else {
4682 if (cond_inverted) {
4683 __ SelD(fcond_reg,
4684 true_src.AsFpuRegister<FRegister>(),
4685 false_src.AsFpuRegister<FRegister>());
4686 } else {
4687 __ SelD(fcond_reg,
4688 false_src.AsFpuRegister<FRegister>(),
4689 true_src.AsFpuRegister<FRegister>());
4690 }
4691 __ MovD(dst_reg, fcond_reg);
4692 }
4693 break;
4694 }
4695 }
4696}
4697
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004698void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4699 LocationSummary* locations = new (GetGraph()->GetArena())
4700 LocationSummary(flag, LocationSummary::kNoCall);
4701 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004702}
4703
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004704void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4705 __ LoadFromOffset(kLoadWord,
4706 flag->GetLocations()->Out().AsRegister<Register>(),
4707 SP,
4708 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004709}
4710
David Brazdil74eb1b22015-12-14 11:44:01 +00004711void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4712 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004713 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004714}
4715
4716void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004717 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4718 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4719 if (is_r6) {
4720 GenConditionalMoveR6(select);
4721 } else {
4722 GenConditionalMoveR2(select);
4723 }
4724 } else {
4725 LocationSummary* locations = select->GetLocations();
4726 MipsLabel false_target;
4727 GenerateTestAndBranch(select,
4728 /* condition_input_index */ 2,
4729 /* true_target */ nullptr,
4730 &false_target);
4731 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4732 __ Bind(&false_target);
4733 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004734}
4735
David Srbecky0cf44932015-12-09 14:09:59 +00004736void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4737 new (GetGraph()->GetArena()) LocationSummary(info);
4738}
4739
David Srbeckyd28f4a02016-03-14 17:14:24 +00004740void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4741 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004742}
4743
4744void CodeGeneratorMIPS::GenerateNop() {
4745 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004746}
4747
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004748void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4749 Primitive::Type field_type = field_info.GetFieldType();
4750 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4751 bool generate_volatile = field_info.IsVolatile() && is_wide;
4752 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004753 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004754
4755 locations->SetInAt(0, Location::RequiresRegister());
4756 if (generate_volatile) {
4757 InvokeRuntimeCallingConvention calling_convention;
4758 // need A0 to hold base + offset
4759 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4760 if (field_type == Primitive::kPrimLong) {
4761 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4762 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004763 // Use Location::Any() to prevent situations when running out of available fp registers.
4764 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004765 // Need some temp core regs since FP results are returned in core registers
4766 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4767 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4768 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4769 }
4770 } else {
4771 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4772 locations->SetOut(Location::RequiresFpuRegister());
4773 } else {
4774 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4775 }
4776 }
4777}
4778
4779void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4780 const FieldInfo& field_info,
4781 uint32_t dex_pc) {
4782 Primitive::Type type = field_info.GetFieldType();
4783 LocationSummary* locations = instruction->GetLocations();
4784 Register obj = locations->InAt(0).AsRegister<Register>();
4785 LoadOperandType load_type = kLoadUnsignedByte;
4786 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004787 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004788 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004789
4790 switch (type) {
4791 case Primitive::kPrimBoolean:
4792 load_type = kLoadUnsignedByte;
4793 break;
4794 case Primitive::kPrimByte:
4795 load_type = kLoadSignedByte;
4796 break;
4797 case Primitive::kPrimShort:
4798 load_type = kLoadSignedHalfword;
4799 break;
4800 case Primitive::kPrimChar:
4801 load_type = kLoadUnsignedHalfword;
4802 break;
4803 case Primitive::kPrimInt:
4804 case Primitive::kPrimFloat:
4805 case Primitive::kPrimNot:
4806 load_type = kLoadWord;
4807 break;
4808 case Primitive::kPrimLong:
4809 case Primitive::kPrimDouble:
4810 load_type = kLoadDoubleword;
4811 break;
4812 case Primitive::kPrimVoid:
4813 LOG(FATAL) << "Unreachable type " << type;
4814 UNREACHABLE();
4815 }
4816
4817 if (is_volatile && load_type == kLoadDoubleword) {
4818 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004819 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004820 // Do implicit Null check
4821 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4822 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004823 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004824 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4825 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004826 // FP results are returned in core registers. Need to move them.
4827 Location out = locations->Out();
4828 if (out.IsFpuRegister()) {
4829 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4830 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4831 out.AsFpuRegister<FRegister>());
4832 } else {
4833 DCHECK(out.IsDoubleStackSlot());
4834 __ StoreToOffset(kStoreWord,
4835 locations->GetTemp(1).AsRegister<Register>(),
4836 SP,
4837 out.GetStackIndex());
4838 __ StoreToOffset(kStoreWord,
4839 locations->GetTemp(2).AsRegister<Register>(),
4840 SP,
4841 out.GetStackIndex() + 4);
4842 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004843 }
4844 } else {
4845 if (!Primitive::IsFloatingPointType(type)) {
4846 Register dst;
4847 if (type == Primitive::kPrimLong) {
4848 DCHECK(locations->Out().IsRegisterPair());
4849 dst = locations->Out().AsRegisterPairLow<Register>();
4850 } else {
4851 DCHECK(locations->Out().IsRegister());
4852 dst = locations->Out().AsRegister<Register>();
4853 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004854 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004855 } else {
4856 DCHECK(locations->Out().IsFpuRegister());
4857 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4858 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004859 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004860 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004861 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004862 }
4863 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004864 }
4865
4866 if (is_volatile) {
4867 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4868 }
4869}
4870
4871void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4872 Primitive::Type field_type = field_info.GetFieldType();
4873 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4874 bool generate_volatile = field_info.IsVolatile() && is_wide;
4875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004876 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004877
4878 locations->SetInAt(0, Location::RequiresRegister());
4879 if (generate_volatile) {
4880 InvokeRuntimeCallingConvention calling_convention;
4881 // need A0 to hold base + offset
4882 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4883 if (field_type == Primitive::kPrimLong) {
4884 locations->SetInAt(1, Location::RegisterPairLocation(
4885 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4886 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004887 // Use Location::Any() to prevent situations when running out of available fp registers.
4888 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004889 // Pass FP parameters in core registers.
4890 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4891 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4892 }
4893 } else {
4894 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004895 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004896 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004897 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004898 }
4899 }
4900}
4901
4902void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4903 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01004904 uint32_t dex_pc,
4905 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004906 Primitive::Type type = field_info.GetFieldType();
4907 LocationSummary* locations = instruction->GetLocations();
4908 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004909 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004910 StoreOperandType store_type = kStoreByte;
4911 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004912 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004913 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004914
4915 switch (type) {
4916 case Primitive::kPrimBoolean:
4917 case Primitive::kPrimByte:
4918 store_type = kStoreByte;
4919 break;
4920 case Primitive::kPrimShort:
4921 case Primitive::kPrimChar:
4922 store_type = kStoreHalfword;
4923 break;
4924 case Primitive::kPrimInt:
4925 case Primitive::kPrimFloat:
4926 case Primitive::kPrimNot:
4927 store_type = kStoreWord;
4928 break;
4929 case Primitive::kPrimLong:
4930 case Primitive::kPrimDouble:
4931 store_type = kStoreDoubleword;
4932 break;
4933 case Primitive::kPrimVoid:
4934 LOG(FATAL) << "Unreachable type " << type;
4935 UNREACHABLE();
4936 }
4937
4938 if (is_volatile) {
4939 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4940 }
4941
4942 if (is_volatile && store_type == kStoreDoubleword) {
4943 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004944 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004945 // Do implicit Null check.
4946 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4947 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4948 if (type == Primitive::kPrimDouble) {
4949 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004950 if (value_location.IsFpuRegister()) {
4951 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4952 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004953 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004954 value_location.AsFpuRegister<FRegister>());
4955 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004956 __ LoadFromOffset(kLoadWord,
4957 locations->GetTemp(1).AsRegister<Register>(),
4958 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004959 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004960 __ LoadFromOffset(kLoadWord,
4961 locations->GetTemp(2).AsRegister<Register>(),
4962 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004963 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004964 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004965 DCHECK(value_location.IsConstant());
4966 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4967 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004968 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4969 locations->GetTemp(1).AsRegister<Register>(),
4970 value);
4971 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004973 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004974 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4975 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004976 if (value_location.IsConstant()) {
4977 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4978 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4979 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980 Register src;
4981 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004982 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004983 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004984 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004985 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004986 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004987 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004988 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004989 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004990 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004991 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004992 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004993 }
4994 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004995 }
4996
4997 // TODO: memory barriers?
4998 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004999 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01005000 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005001 }
5002
5003 if (is_volatile) {
5004 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5005 }
5006}
5007
5008void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5009 HandleFieldGet(instruction, instruction->GetFieldInfo());
5010}
5011
5012void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5013 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5014}
5015
5016void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5017 HandleFieldSet(instruction, instruction->GetFieldInfo());
5018}
5019
5020void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01005021 HandleFieldSet(instruction,
5022 instruction->GetFieldInfo(),
5023 instruction->GetDexPc(),
5024 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005025}
5026
Alexey Frunze06a46c42016-07-19 15:00:40 -07005027void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5028 HInstruction* instruction ATTRIBUTE_UNUSED,
5029 Location root,
5030 Register obj,
5031 uint32_t offset) {
5032 Register root_reg = root.AsRegister<Register>();
5033 if (kEmitCompilerReadBarrier) {
5034 UNIMPLEMENTED(FATAL) << "for read barrier";
5035 } else {
5036 // Plain GC root load with no read barrier.
5037 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5038 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5039 // Note that GC roots are not affected by heap poisoning, thus we
5040 // do not have to unpoison `root_reg` here.
5041 }
5042}
5043
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005044void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5045 LocationSummary::CallKind call_kind =
5046 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5048 locations->SetInAt(0, Location::RequiresRegister());
5049 locations->SetInAt(1, Location::RequiresRegister());
5050 // The output does overlap inputs.
5051 // Note that TypeCheckSlowPathMIPS uses this register too.
5052 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5053}
5054
5055void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5056 LocationSummary* locations = instruction->GetLocations();
5057 Register obj = locations->InAt(0).AsRegister<Register>();
5058 Register cls = locations->InAt(1).AsRegister<Register>();
5059 Register out = locations->Out().AsRegister<Register>();
5060
5061 MipsLabel done;
5062
5063 // Return 0 if `obj` is null.
5064 // TODO: Avoid this check if we know `obj` is not null.
5065 __ Move(out, ZERO);
5066 __ Beqz(obj, &done);
5067
5068 // Compare the class of `obj` with `cls`.
5069 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5070 if (instruction->IsExactCheck()) {
5071 // Classes must be equal for the instanceof to succeed.
5072 __ Xor(out, out, cls);
5073 __ Sltiu(out, out, 1);
5074 } else {
5075 // If the classes are not equal, we go into a slow path.
5076 DCHECK(locations->OnlyCallsOnSlowPath());
5077 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5078 codegen_->AddSlowPath(slow_path);
5079 __ Bne(out, cls, slow_path->GetEntryLabel());
5080 __ LoadConst32(out, 1);
5081 __ Bind(slow_path->GetExitLabel());
5082 }
5083
5084 __ Bind(&done);
5085}
5086
5087void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5089 locations->SetOut(Location::ConstantLocation(constant));
5090}
5091
5092void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5093 // Will be generated at use site.
5094}
5095
5096void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5097 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5098 locations->SetOut(Location::ConstantLocation(constant));
5099}
5100
5101void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5102 // Will be generated at use site.
5103}
5104
5105void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5106 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5107 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5108}
5109
5110void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5111 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005112 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005113 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005114 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005115}
5116
5117void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5118 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5119 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005120 Location receiver = invoke->GetLocations()->InAt(0);
5121 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005122 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005123
5124 // Set the hidden argument.
5125 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5126 invoke->GetDexMethodIndex());
5127
5128 // temp = object->GetClass();
5129 if (receiver.IsStackSlot()) {
5130 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5131 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5132 } else {
5133 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5134 }
5135 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005136 __ LoadFromOffset(kLoadWord, temp, temp,
5137 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5138 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005139 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005140 // temp = temp->GetImtEntryAt(method_offset);
5141 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5142 // T9 = temp->GetEntryPoint();
5143 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5144 // T9();
5145 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005146 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005147 DCHECK(!codegen_->IsLeafMethod());
5148 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5149}
5150
5151void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005152 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5153 if (intrinsic.TryDispatch(invoke)) {
5154 return;
5155 }
5156
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005157 HandleInvoke(invoke);
5158}
5159
5160void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005161 // Explicit clinit checks triggered by static invokes must have been pruned by
5162 // art::PrepareForRegisterAllocation.
5163 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005164
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005165 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5166 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005167
Chris Larsen701566a2015-10-27 15:29:13 -07005168 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5169 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005170 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5171 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5172 }
Chris Larsen701566a2015-10-27 15:29:13 -07005173 return;
5174 }
5175
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005176 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005177
5178 // Add the extra input register if either the dex cache array base register
5179 // or the PC-relative base register for accessing literals is needed.
5180 if (has_extra_input) {
5181 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5182 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005183}
5184
Orion Hodsonac141392017-01-13 11:53:47 +00005185void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5186 HandleInvoke(invoke);
5187}
5188
5189void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5190 codegen_->GenerateInvokePolymorphicCall(invoke);
5191}
5192
Chris Larsen701566a2015-10-27 15:29:13 -07005193static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005194 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005195 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5196 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005197 return true;
5198 }
5199 return false;
5200}
5201
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005202HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005203 HLoadString::LoadKind desired_string_load_kind) {
5204 if (kEmitCompilerReadBarrier) {
5205 UNIMPLEMENTED(FATAL) << "for read barrier";
5206 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005207 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005208 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005209 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5210 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005211 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005212 bool is_r6 = GetInstructionSetFeatures().IsR6();
5213 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005214 switch (desired_string_load_kind) {
5215 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5216 DCHECK(!GetCompilerOptions().GetCompilePic());
5217 break;
5218 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5219 DCHECK(GetCompilerOptions().GetCompilePic());
5220 break;
5221 case HLoadString::LoadKind::kBootImageAddress:
5222 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005223 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005224 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005225 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005226 case HLoadString::LoadKind::kJitTableAddress:
5227 DCHECK(Runtime::Current()->UseJitCompilation());
5228 // TODO: implement.
5229 fallback_load = true;
5230 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005231 case HLoadString::LoadKind::kDexCacheViaMethod:
5232 fallback_load = false;
5233 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005234 }
5235 if (fallback_load) {
5236 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5237 }
5238 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005239}
5240
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005241HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5242 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005243 if (kEmitCompilerReadBarrier) {
5244 UNIMPLEMENTED(FATAL) << "for read barrier";
5245 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005246 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005247 // is incompatible with it.
5248 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005249 bool is_r6 = GetInstructionSetFeatures().IsR6();
5250 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005251 switch (desired_class_load_kind) {
5252 case HLoadClass::LoadKind::kReferrersClass:
5253 fallback_load = false;
5254 break;
5255 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5256 DCHECK(!GetCompilerOptions().GetCompilePic());
5257 break;
5258 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5259 DCHECK(GetCompilerOptions().GetCompilePic());
5260 break;
5261 case HLoadClass::LoadKind::kBootImageAddress:
5262 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005263 case HLoadClass::LoadKind::kBssEntry:
5264 DCHECK(!Runtime::Current()->UseJitCompilation());
5265 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005266 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005267 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005268 // TODO: implement.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005269 fallback_load = true;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005270 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005271 case HLoadClass::LoadKind::kDexCacheViaMethod:
5272 fallback_load = false;
5273 break;
5274 }
5275 if (fallback_load) {
5276 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5277 }
5278 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005279}
5280
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005281Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5282 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005283 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005284 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5285 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5286 if (!invoke->GetLocations()->Intrinsified()) {
5287 return location.AsRegister<Register>();
5288 }
5289 // For intrinsics we allow any location, so it may be on the stack.
5290 if (!location.IsRegister()) {
5291 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5292 return temp;
5293 }
5294 // For register locations, check if the register was saved. If so, get it from the stack.
5295 // Note: There is a chance that the register was saved but not overwritten, so we could
5296 // save one load. However, since this is just an intrinsic slow path we prefer this
5297 // simple and more robust approach rather that trying to determine if that's the case.
5298 SlowPathCode* slow_path = GetCurrentSlowPath();
5299 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5300 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5301 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5302 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5303 return temp;
5304 }
5305 return location.AsRegister<Register>();
5306}
5307
Vladimir Markodc151b22015-10-15 18:02:30 +01005308HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5309 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005310 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005311 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005312 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005313 // is incompatible with it.
5314 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005315 bool is_r6 = GetInstructionSetFeatures().IsR6();
5316 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005317 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005318 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005319 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005320 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005321 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005322 break;
5323 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005324 if (fallback_load) {
5325 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5326 dispatch_info.method_load_data = 0;
5327 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005328 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005329}
5330
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005331void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5332 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005333 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005334 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5335 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005336 bool is_r6 = GetInstructionSetFeatures().IsR6();
5337 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005338 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5339 : ZERO;
5340
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005341 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005342 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005343 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005344 uint32_t offset =
5345 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005346 __ LoadFromOffset(kLoadWord,
5347 temp.AsRegister<Register>(),
5348 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005349 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005350 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005351 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005352 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005353 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005354 break;
5355 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5356 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5357 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005358 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5359 if (is_r6) {
5360 uint32_t offset = invoke->GetDexCacheArrayOffset();
5361 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5362 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
5363 bool reordering = __ SetReorder(false);
5364 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
5365 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
5366 __ SetReorder(reordering);
5367 } else {
5368 HMipsDexCacheArraysBase* base =
5369 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5370 int32_t offset =
5371 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5372 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5373 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005374 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005375 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005376 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005377 Register reg = temp.AsRegister<Register>();
5378 Register method_reg;
5379 if (current_method.IsRegister()) {
5380 method_reg = current_method.AsRegister<Register>();
5381 } else {
5382 // TODO: use the appropriate DCHECK() here if possible.
5383 // DCHECK(invoke->GetLocations()->Intrinsified());
5384 DCHECK(!current_method.IsValid());
5385 method_reg = reg;
5386 __ Lw(reg, SP, kCurrentMethodStackOffset);
5387 }
5388
5389 // temp = temp->dex_cache_resolved_methods_;
5390 __ LoadFromOffset(kLoadWord,
5391 reg,
5392 method_reg,
5393 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005394 // temp = temp[index_in_cache];
5395 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5396 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005397 __ LoadFromOffset(kLoadWord,
5398 reg,
5399 reg,
5400 CodeGenerator::GetCachePointerOffset(index_in_cache));
5401 break;
5402 }
5403 }
5404
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005405 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005406 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005407 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005408 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005409 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5410 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005411 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005412 T9,
5413 callee_method.AsRegister<Register>(),
5414 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005415 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005416 // T9()
5417 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005418 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005419 break;
5420 }
5421 DCHECK(!IsLeafMethod());
5422}
5423
5424void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005425 // Explicit clinit checks triggered by static invokes must have been pruned by
5426 // art::PrepareForRegisterAllocation.
5427 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005428
5429 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5430 return;
5431 }
5432
5433 LocationSummary* locations = invoke->GetLocations();
5434 codegen_->GenerateStaticOrDirectCall(invoke,
5435 locations->HasTemps()
5436 ? locations->GetTemp(0)
5437 : Location::NoLocation());
5438 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5439}
5440
Chris Larsen3acee732015-11-18 13:31:08 -08005441void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005442 // Use the calling convention instead of the location of the receiver, as
5443 // intrinsics may have put the receiver in a different register. In the intrinsics
5444 // slow path, the arguments have been moved to the right place, so here we are
5445 // guaranteed that the receiver is the first register of the calling convention.
5446 InvokeDexCallingConvention calling_convention;
5447 Register receiver = calling_convention.GetRegisterAt(0);
5448
Chris Larsen3acee732015-11-18 13:31:08 -08005449 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005450 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5451 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5452 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005453 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454
5455 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005456 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005457 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005458 // temp = temp->GetMethodAt(method_offset);
5459 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5460 // T9 = temp->GetEntryPoint();
5461 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5462 // T9();
5463 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005464 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005465}
5466
5467void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5468 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5469 return;
5470 }
5471
5472 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005473 DCHECK(!codegen_->IsLeafMethod());
5474 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5475}
5476
5477void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005478 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5479 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005480 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005481 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005482 cls,
5483 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005484 Location::RegisterLocation(V0));
Alexey Frunze06a46c42016-07-19 15:00:40 -07005485 return;
5486 }
Vladimir Marko41559982017-01-06 14:04:23 +00005487 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005488
5489 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5490 ? LocationSummary::kCallOnSlowPath
5491 : LocationSummary::kNoCall;
5492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005493 switch (load_kind) {
5494 // We need an extra register for PC-relative literals on R2.
5495 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005496 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005497 case HLoadClass::LoadKind::kBootImageAddress:
5498 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005499 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5500 break;
5501 }
5502 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005503 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005504 locations->SetInAt(0, Location::RequiresRegister());
5505 break;
5506 default:
5507 break;
5508 }
5509 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005510}
5511
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005512// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5513// move.
5514void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005515 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5516 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5517 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01005518 return;
5519 }
Vladimir Marko41559982017-01-06 14:04:23 +00005520 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01005521
Vladimir Marko41559982017-01-06 14:04:23 +00005522 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005523 Location out_loc = locations->Out();
5524 Register out = out_loc.AsRegister<Register>();
5525 Register base_or_current_method_reg;
5526 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5527 switch (load_kind) {
5528 // We need an extra register for PC-relative literals on R2.
5529 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005530 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005531 case HLoadClass::LoadKind::kBootImageAddress:
5532 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005533 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5534 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005535 case HLoadClass::LoadKind::kReferrersClass:
5536 case HLoadClass::LoadKind::kDexCacheViaMethod:
5537 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5538 break;
5539 default:
5540 base_or_current_method_reg = ZERO;
5541 break;
5542 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005543
Alexey Frunze06a46c42016-07-19 15:00:40 -07005544 bool generate_null_check = false;
5545 switch (load_kind) {
5546 case HLoadClass::LoadKind::kReferrersClass: {
5547 DCHECK(!cls->CanCallRuntime());
5548 DCHECK(!cls->MustGenerateClinitCheck());
5549 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5550 GenerateGcRootFieldLoad(cls,
5551 out_loc,
5552 base_or_current_method_reg,
5553 ArtMethod::DeclaringClassOffset().Int32Value());
5554 break;
5555 }
5556 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005557 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005558 __ LoadLiteral(out,
5559 base_or_current_method_reg,
5560 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5561 cls->GetTypeIndex()));
5562 break;
5563 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005564 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005565 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5566 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005567 bool reordering = __ SetReorder(false);
5568 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5569 __ Addiu(out, out, /* placeholder */ 0x5678);
5570 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005571 break;
5572 }
5573 case HLoadClass::LoadKind::kBootImageAddress: {
5574 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005575 uint32_t address = dchecked_integral_cast<uint32_t>(
5576 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5577 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005578 __ LoadLiteral(out,
5579 base_or_current_method_reg,
5580 codegen_->DeduplicateBootImageAddressLiteral(address));
5581 break;
5582 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005583 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005584 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005585 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005586 bool reordering = __ SetReorder(false);
5587 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5588 __ LoadFromOffset(kLoadWord, out, out, /* placeholder */ 0x5678);
5589 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005590 generate_null_check = true;
5591 break;
5592 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005593 case HLoadClass::LoadKind::kJitTableAddress: {
5594 LOG(FATAL) << "Unimplemented";
Alexey Frunze06a46c42016-07-19 15:00:40 -07005595 break;
5596 }
Vladimir Marko41559982017-01-06 14:04:23 +00005597 case HLoadClass::LoadKind::kDexCacheViaMethod:
5598 LOG(FATAL) << "UNREACHABLE";
5599 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005600 }
5601
5602 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5603 DCHECK(cls->CanCallRuntime());
5604 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5605 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5606 codegen_->AddSlowPath(slow_path);
5607 if (generate_null_check) {
5608 __ Beqz(out, slow_path->GetEntryLabel());
5609 }
5610 if (cls->MustGenerateClinitCheck()) {
5611 GenerateClassInitializationCheck(slow_path, out);
5612 } else {
5613 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005614 }
5615 }
5616}
5617
5618static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005619 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005620}
5621
5622void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5623 LocationSummary* locations =
5624 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5625 locations->SetOut(Location::RequiresRegister());
5626}
5627
5628void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5629 Register out = load->GetLocations()->Out().AsRegister<Register>();
5630 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5631}
5632
5633void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5634 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5635}
5636
5637void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5638 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5639}
5640
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005641void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005642 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005643 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005644 HLoadString::LoadKind load_kind = load->GetLoadKind();
5645 switch (load_kind) {
5646 // We need an extra register for PC-relative literals on R2.
5647 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5648 case HLoadString::LoadKind::kBootImageAddress:
5649 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005650 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005651 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5652 break;
5653 }
5654 FALLTHROUGH_INTENDED;
5655 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005656 case HLoadString::LoadKind::kDexCacheViaMethod:
5657 locations->SetInAt(0, Location::RequiresRegister());
5658 break;
5659 default:
5660 break;
5661 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005662 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5663 InvokeRuntimeCallingConvention calling_convention;
5664 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5665 } else {
5666 locations->SetOut(Location::RequiresRegister());
5667 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005668}
5669
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005670// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5671// move.
5672void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005673 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005674 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005675 Location out_loc = locations->Out();
5676 Register out = out_loc.AsRegister<Register>();
5677 Register base_or_current_method_reg;
5678 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5679 switch (load_kind) {
5680 // We need an extra register for PC-relative literals on R2.
5681 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5682 case HLoadString::LoadKind::kBootImageAddress:
5683 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005684 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005685 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5686 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005687 default:
5688 base_or_current_method_reg = ZERO;
5689 break;
5690 }
5691
5692 switch (load_kind) {
5693 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005694 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005695 __ LoadLiteral(out,
5696 base_or_current_method_reg,
5697 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5698 load->GetStringIndex()));
5699 return; // No dex cache slow path.
5700 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005701 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005702 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005703 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005704 bool reordering = __ SetReorder(false);
5705 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5706 __ Addiu(out, out, /* placeholder */ 0x5678);
5707 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005708 return; // No dex cache slow path.
5709 }
5710 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005711 uint32_t address = dchecked_integral_cast<uint32_t>(
5712 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5713 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005714 __ LoadLiteral(out,
5715 base_or_current_method_reg,
5716 codegen_->DeduplicateBootImageAddressLiteral(address));
5717 return; // No dex cache slow path.
5718 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005719 case HLoadString::LoadKind::kBssEntry: {
5720 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5721 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005722 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005723 bool reordering = __ SetReorder(false);
5724 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5725 __ LoadFromOffset(kLoadWord, out, out, /* placeholder */ 0x5678);
5726 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005727 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5728 codegen_->AddSlowPath(slow_path);
5729 __ Beqz(out, slow_path->GetEntryLabel());
5730 __ Bind(slow_path->GetExitLabel());
5731 return;
5732 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005733 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005734 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005735 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005736
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005737 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005738 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5739 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005740 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005741 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5742 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005743}
5744
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005745void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5747 locations->SetOut(Location::ConstantLocation(constant));
5748}
5749
5750void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5751 // Will be generated at use site.
5752}
5753
5754void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5755 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005757 InvokeRuntimeCallingConvention calling_convention;
5758 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5759}
5760
5761void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5762 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005763 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005764 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5765 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005766 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005767 }
5768 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5769}
5770
5771void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5772 LocationSummary* locations =
5773 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5774 switch (mul->GetResultType()) {
5775 case Primitive::kPrimInt:
5776 case Primitive::kPrimLong:
5777 locations->SetInAt(0, Location::RequiresRegister());
5778 locations->SetInAt(1, Location::RequiresRegister());
5779 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5780 break;
5781
5782 case Primitive::kPrimFloat:
5783 case Primitive::kPrimDouble:
5784 locations->SetInAt(0, Location::RequiresFpuRegister());
5785 locations->SetInAt(1, Location::RequiresFpuRegister());
5786 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5787 break;
5788
5789 default:
5790 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5791 }
5792}
5793
5794void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5795 Primitive::Type type = instruction->GetType();
5796 LocationSummary* locations = instruction->GetLocations();
5797 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5798
5799 switch (type) {
5800 case Primitive::kPrimInt: {
5801 Register dst = locations->Out().AsRegister<Register>();
5802 Register lhs = locations->InAt(0).AsRegister<Register>();
5803 Register rhs = locations->InAt(1).AsRegister<Register>();
5804
5805 if (isR6) {
5806 __ MulR6(dst, lhs, rhs);
5807 } else {
5808 __ MulR2(dst, lhs, rhs);
5809 }
5810 break;
5811 }
5812 case Primitive::kPrimLong: {
5813 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5814 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5815 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5816 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5817 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5818 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5819
5820 // Extra checks to protect caused by the existance of A1_A2.
5821 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5822 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5823 DCHECK_NE(dst_high, lhs_low);
5824 DCHECK_NE(dst_high, rhs_low);
5825
5826 // A_B * C_D
5827 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5828 // dst_lo: [ low(B*D) ]
5829 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5830
5831 if (isR6) {
5832 __ MulR6(TMP, lhs_high, rhs_low);
5833 __ MulR6(dst_high, lhs_low, rhs_high);
5834 __ Addu(dst_high, dst_high, TMP);
5835 __ MuhuR6(TMP, lhs_low, rhs_low);
5836 __ Addu(dst_high, dst_high, TMP);
5837 __ MulR6(dst_low, lhs_low, rhs_low);
5838 } else {
5839 __ MulR2(TMP, lhs_high, rhs_low);
5840 __ MulR2(dst_high, lhs_low, rhs_high);
5841 __ Addu(dst_high, dst_high, TMP);
5842 __ MultuR2(lhs_low, rhs_low);
5843 __ Mfhi(TMP);
5844 __ Addu(dst_high, dst_high, TMP);
5845 __ Mflo(dst_low);
5846 }
5847 break;
5848 }
5849 case Primitive::kPrimFloat:
5850 case Primitive::kPrimDouble: {
5851 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5852 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5853 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5854 if (type == Primitive::kPrimFloat) {
5855 __ MulS(dst, lhs, rhs);
5856 } else {
5857 __ MulD(dst, lhs, rhs);
5858 }
5859 break;
5860 }
5861 default:
5862 LOG(FATAL) << "Unexpected mul type " << type;
5863 }
5864}
5865
5866void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5867 LocationSummary* locations =
5868 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5869 switch (neg->GetResultType()) {
5870 case Primitive::kPrimInt:
5871 case Primitive::kPrimLong:
5872 locations->SetInAt(0, Location::RequiresRegister());
5873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5874 break;
5875
5876 case Primitive::kPrimFloat:
5877 case Primitive::kPrimDouble:
5878 locations->SetInAt(0, Location::RequiresFpuRegister());
5879 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5880 break;
5881
5882 default:
5883 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5884 }
5885}
5886
5887void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5888 Primitive::Type type = instruction->GetType();
5889 LocationSummary* locations = instruction->GetLocations();
5890
5891 switch (type) {
5892 case Primitive::kPrimInt: {
5893 Register dst = locations->Out().AsRegister<Register>();
5894 Register src = locations->InAt(0).AsRegister<Register>();
5895 __ Subu(dst, ZERO, src);
5896 break;
5897 }
5898 case Primitive::kPrimLong: {
5899 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5900 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5901 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5902 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5903 __ Subu(dst_low, ZERO, src_low);
5904 __ Sltu(TMP, ZERO, dst_low);
5905 __ Subu(dst_high, ZERO, src_high);
5906 __ Subu(dst_high, dst_high, TMP);
5907 break;
5908 }
5909 case Primitive::kPrimFloat:
5910 case Primitive::kPrimDouble: {
5911 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5912 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5913 if (type == Primitive::kPrimFloat) {
5914 __ NegS(dst, src);
5915 } else {
5916 __ NegD(dst, src);
5917 }
5918 break;
5919 }
5920 default:
5921 LOG(FATAL) << "Unexpected neg type " << type;
5922 }
5923}
5924
5925void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5926 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005928 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005929 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005930 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5931 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005932}
5933
5934void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005935 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5936 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005937}
5938
5939void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5940 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005941 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005942 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005943 if (instruction->IsStringAlloc()) {
5944 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5945 } else {
5946 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005947 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005948 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5949}
5950
5951void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005952 if (instruction->IsStringAlloc()) {
5953 // String is allocated through StringFactory. Call NewEmptyString entry point.
5954 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005955 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005956 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5957 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5958 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005959 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005960 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5961 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005962 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005963 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005964 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005965}
5966
5967void LocationsBuilderMIPS::VisitNot(HNot* 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::VisitNot(HNot* instruction) {
5974 Primitive::Type type = instruction->GetType();
5975 LocationSummary* locations = instruction->GetLocations();
5976
5977 switch (type) {
5978 case Primitive::kPrimInt: {
5979 Register dst = locations->Out().AsRegister<Register>();
5980 Register src = locations->InAt(0).AsRegister<Register>();
5981 __ Nor(dst, src, ZERO);
5982 break;
5983 }
5984
5985 case Primitive::kPrimLong: {
5986 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5987 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5988 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5989 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5990 __ Nor(dst_high, src_high, ZERO);
5991 __ Nor(dst_low, src_low, ZERO);
5992 break;
5993 }
5994
5995 default:
5996 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5997 }
5998}
5999
6000void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6002 locations->SetInAt(0, Location::RequiresRegister());
6003 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6004}
6005
6006void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6007 LocationSummary* locations = instruction->GetLocations();
6008 __ Xori(locations->Out().AsRegister<Register>(),
6009 locations->InAt(0).AsRegister<Register>(),
6010 1);
6011}
6012
6013void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006014 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6015 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006016}
6017
Calin Juravle2ae48182016-03-16 14:05:09 +00006018void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6019 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006020 return;
6021 }
6022 Location obj = instruction->GetLocations()->InAt(0);
6023
6024 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006025 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006026}
6027
Calin Juravle2ae48182016-03-16 14:05:09 +00006028void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006029 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006030 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006031
6032 Location obj = instruction->GetLocations()->InAt(0);
6033
6034 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6035}
6036
6037void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006038 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006039}
6040
6041void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6042 HandleBinaryOp(instruction);
6043}
6044
6045void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6046 HandleBinaryOp(instruction);
6047}
6048
6049void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6050 LOG(FATAL) << "Unreachable";
6051}
6052
6053void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6054 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6055}
6056
6057void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6059 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6060 if (location.IsStackSlot()) {
6061 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6062 } else if (location.IsDoubleStackSlot()) {
6063 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6064 }
6065 locations->SetOut(location);
6066}
6067
6068void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6069 ATTRIBUTE_UNUSED) {
6070 // Nothing to do, the parameter is already at its location.
6071}
6072
6073void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6074 LocationSummary* locations =
6075 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6076 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6077}
6078
6079void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6080 ATTRIBUTE_UNUSED) {
6081 // Nothing to do, the method is already at its location.
6082}
6083
6084void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006086 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006087 locations->SetInAt(i, Location::Any());
6088 }
6089 locations->SetOut(Location::Any());
6090}
6091
6092void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6093 LOG(FATAL) << "Unreachable";
6094}
6095
6096void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6097 Primitive::Type type = rem->GetResultType();
6098 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006099 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006100 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6101
6102 switch (type) {
6103 case Primitive::kPrimInt:
6104 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006105 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006106 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6107 break;
6108
6109 case Primitive::kPrimLong: {
6110 InvokeRuntimeCallingConvention calling_convention;
6111 locations->SetInAt(0, Location::RegisterPairLocation(
6112 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6113 locations->SetInAt(1, Location::RegisterPairLocation(
6114 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6115 locations->SetOut(calling_convention.GetReturnLocation(type));
6116 break;
6117 }
6118
6119 case Primitive::kPrimFloat:
6120 case Primitive::kPrimDouble: {
6121 InvokeRuntimeCallingConvention calling_convention;
6122 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6123 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6124 locations->SetOut(calling_convention.GetReturnLocation(type));
6125 break;
6126 }
6127
6128 default:
6129 LOG(FATAL) << "Unexpected rem type " << type;
6130 }
6131}
6132
6133void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6134 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006135
6136 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006137 case Primitive::kPrimInt:
6138 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006139 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006140 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006141 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006142 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6143 break;
6144 }
6145 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006146 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006147 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006148 break;
6149 }
6150 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006151 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006152 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153 break;
6154 }
6155 default:
6156 LOG(FATAL) << "Unexpected rem type " << type;
6157 }
6158}
6159
6160void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6161 memory_barrier->SetLocations(nullptr);
6162}
6163
6164void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6165 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6166}
6167
6168void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6169 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6170 Primitive::Type return_type = ret->InputAt(0)->GetType();
6171 locations->SetInAt(0, MipsReturnLocation(return_type));
6172}
6173
6174void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6175 codegen_->GenerateFrameExit();
6176}
6177
6178void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6179 ret->SetLocations(nullptr);
6180}
6181
6182void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6183 codegen_->GenerateFrameExit();
6184}
6185
Alexey Frunze92d90602015-12-18 18:16:36 -08006186void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6187 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006188}
6189
Alexey Frunze92d90602015-12-18 18:16:36 -08006190void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6191 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006192}
6193
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006194void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6195 HandleShift(shl);
6196}
6197
6198void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6199 HandleShift(shl);
6200}
6201
6202void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6203 HandleShift(shr);
6204}
6205
6206void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6207 HandleShift(shr);
6208}
6209
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006210void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6211 HandleBinaryOp(instruction);
6212}
6213
6214void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6215 HandleBinaryOp(instruction);
6216}
6217
6218void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6219 HandleFieldGet(instruction, instruction->GetFieldInfo());
6220}
6221
6222void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6223 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6224}
6225
6226void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6227 HandleFieldSet(instruction, instruction->GetFieldInfo());
6228}
6229
6230void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006231 HandleFieldSet(instruction,
6232 instruction->GetFieldInfo(),
6233 instruction->GetDexPc(),
6234 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235}
6236
6237void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6238 HUnresolvedInstanceFieldGet* instruction) {
6239 FieldAccessCallingConventionMIPS calling_convention;
6240 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6241 instruction->GetFieldType(),
6242 calling_convention);
6243}
6244
6245void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6246 HUnresolvedInstanceFieldGet* instruction) {
6247 FieldAccessCallingConventionMIPS calling_convention;
6248 codegen_->GenerateUnresolvedFieldAccess(instruction,
6249 instruction->GetFieldType(),
6250 instruction->GetFieldIndex(),
6251 instruction->GetDexPc(),
6252 calling_convention);
6253}
6254
6255void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6256 HUnresolvedInstanceFieldSet* instruction) {
6257 FieldAccessCallingConventionMIPS calling_convention;
6258 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6259 instruction->GetFieldType(),
6260 calling_convention);
6261}
6262
6263void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6264 HUnresolvedInstanceFieldSet* instruction) {
6265 FieldAccessCallingConventionMIPS calling_convention;
6266 codegen_->GenerateUnresolvedFieldAccess(instruction,
6267 instruction->GetFieldType(),
6268 instruction->GetFieldIndex(),
6269 instruction->GetDexPc(),
6270 calling_convention);
6271}
6272
6273void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6274 HUnresolvedStaticFieldGet* instruction) {
6275 FieldAccessCallingConventionMIPS calling_convention;
6276 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6277 instruction->GetFieldType(),
6278 calling_convention);
6279}
6280
6281void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6282 HUnresolvedStaticFieldGet* instruction) {
6283 FieldAccessCallingConventionMIPS calling_convention;
6284 codegen_->GenerateUnresolvedFieldAccess(instruction,
6285 instruction->GetFieldType(),
6286 instruction->GetFieldIndex(),
6287 instruction->GetDexPc(),
6288 calling_convention);
6289}
6290
6291void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6292 HUnresolvedStaticFieldSet* instruction) {
6293 FieldAccessCallingConventionMIPS calling_convention;
6294 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6295 instruction->GetFieldType(),
6296 calling_convention);
6297}
6298
6299void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6300 HUnresolvedStaticFieldSet* instruction) {
6301 FieldAccessCallingConventionMIPS calling_convention;
6302 codegen_->GenerateUnresolvedFieldAccess(instruction,
6303 instruction->GetFieldType(),
6304 instruction->GetFieldIndex(),
6305 instruction->GetDexPc(),
6306 calling_convention);
6307}
6308
6309void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006310 LocationSummary* locations =
6311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006312 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006313}
6314
6315void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6316 HBasicBlock* block = instruction->GetBlock();
6317 if (block->GetLoopInformation() != nullptr) {
6318 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6319 // The back edge will generate the suspend check.
6320 return;
6321 }
6322 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6323 // The goto will generate the suspend check.
6324 return;
6325 }
6326 GenerateSuspendCheck(instruction, nullptr);
6327}
6328
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006329void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6330 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006331 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006332 InvokeRuntimeCallingConvention calling_convention;
6333 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6334}
6335
6336void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006337 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006338 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6339}
6340
6341void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6342 Primitive::Type input_type = conversion->GetInputType();
6343 Primitive::Type result_type = conversion->GetResultType();
6344 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006345 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006346
6347 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6348 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6349 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6350 }
6351
6352 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006353 if (!isR6 &&
6354 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6355 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006356 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006357 }
6358
6359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6360
6361 if (call_kind == LocationSummary::kNoCall) {
6362 if (Primitive::IsFloatingPointType(input_type)) {
6363 locations->SetInAt(0, Location::RequiresFpuRegister());
6364 } else {
6365 locations->SetInAt(0, Location::RequiresRegister());
6366 }
6367
6368 if (Primitive::IsFloatingPointType(result_type)) {
6369 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6370 } else {
6371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6372 }
6373 } else {
6374 InvokeRuntimeCallingConvention calling_convention;
6375
6376 if (Primitive::IsFloatingPointType(input_type)) {
6377 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6378 } else {
6379 DCHECK_EQ(input_type, Primitive::kPrimLong);
6380 locations->SetInAt(0, Location::RegisterPairLocation(
6381 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6382 }
6383
6384 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6385 }
6386}
6387
6388void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6389 LocationSummary* locations = conversion->GetLocations();
6390 Primitive::Type result_type = conversion->GetResultType();
6391 Primitive::Type input_type = conversion->GetInputType();
6392 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006393 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394
6395 DCHECK_NE(input_type, result_type);
6396
6397 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6398 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6399 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6400 Register src = locations->InAt(0).AsRegister<Register>();
6401
Alexey Frunzea871ef12016-06-27 15:20:11 -07006402 if (dst_low != src) {
6403 __ Move(dst_low, src);
6404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006405 __ Sra(dst_high, src, 31);
6406 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6407 Register dst = locations->Out().AsRegister<Register>();
6408 Register src = (input_type == Primitive::kPrimLong)
6409 ? locations->InAt(0).AsRegisterPairLow<Register>()
6410 : locations->InAt(0).AsRegister<Register>();
6411
6412 switch (result_type) {
6413 case Primitive::kPrimChar:
6414 __ Andi(dst, src, 0xFFFF);
6415 break;
6416 case Primitive::kPrimByte:
6417 if (has_sign_extension) {
6418 __ Seb(dst, src);
6419 } else {
6420 __ Sll(dst, src, 24);
6421 __ Sra(dst, dst, 24);
6422 }
6423 break;
6424 case Primitive::kPrimShort:
6425 if (has_sign_extension) {
6426 __ Seh(dst, src);
6427 } else {
6428 __ Sll(dst, src, 16);
6429 __ Sra(dst, dst, 16);
6430 }
6431 break;
6432 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006433 if (dst != src) {
6434 __ Move(dst, src);
6435 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006436 break;
6437
6438 default:
6439 LOG(FATAL) << "Unexpected type conversion from " << input_type
6440 << " to " << result_type;
6441 }
6442 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006443 if (input_type == Primitive::kPrimLong) {
6444 if (isR6) {
6445 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6446 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6447 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6448 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6449 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6450 __ Mtc1(src_low, FTMP);
6451 __ Mthc1(src_high, FTMP);
6452 if (result_type == Primitive::kPrimFloat) {
6453 __ Cvtsl(dst, FTMP);
6454 } else {
6455 __ Cvtdl(dst, FTMP);
6456 }
6457 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006458 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6459 : kQuickL2d;
6460 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006461 if (result_type == Primitive::kPrimFloat) {
6462 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6463 } else {
6464 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6465 }
6466 }
6467 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006468 Register src = locations->InAt(0).AsRegister<Register>();
6469 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6470 __ Mtc1(src, FTMP);
6471 if (result_type == Primitive::kPrimFloat) {
6472 __ Cvtsw(dst, FTMP);
6473 } else {
6474 __ Cvtdw(dst, FTMP);
6475 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006476 }
6477 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6478 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006479 if (result_type == Primitive::kPrimLong) {
6480 if (isR6) {
6481 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6482 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6483 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6484 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6485 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6486 MipsLabel truncate;
6487 MipsLabel done;
6488
6489 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6490 // value when the input is either a NaN or is outside of the range of the output type
6491 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6492 // the same result.
6493 //
6494 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6495 // value of the output type if the input is outside of the range after the truncation or
6496 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6497 // results. This matches the desired float/double-to-int/long conversion exactly.
6498 //
6499 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6500 //
6501 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6502 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6503 // even though it must be NAN2008=1 on R6.
6504 //
6505 // The code takes care of the different behaviors by first comparing the input to the
6506 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6507 // If the input is greater than or equal to the minimum, it procedes to the truncate
6508 // instruction, which will handle such an input the same way irrespective of NAN2008.
6509 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6510 // in order to return either zero or the minimum value.
6511 //
6512 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6513 // truncate instruction for MIPS64R6.
6514 if (input_type == Primitive::kPrimFloat) {
6515 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6516 __ LoadConst32(TMP, min_val);
6517 __ Mtc1(TMP, FTMP);
6518 __ CmpLeS(FTMP, FTMP, src);
6519 } else {
6520 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6521 __ LoadConst32(TMP, High32Bits(min_val));
6522 __ Mtc1(ZERO, FTMP);
6523 __ Mthc1(TMP, FTMP);
6524 __ CmpLeD(FTMP, FTMP, src);
6525 }
6526
6527 __ Bc1nez(FTMP, &truncate);
6528
6529 if (input_type == Primitive::kPrimFloat) {
6530 __ CmpEqS(FTMP, src, src);
6531 } else {
6532 __ CmpEqD(FTMP, src, src);
6533 }
6534 __ Move(dst_low, ZERO);
6535 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6536 __ Mfc1(TMP, FTMP);
6537 __ And(dst_high, dst_high, TMP);
6538
6539 __ B(&done);
6540
6541 __ Bind(&truncate);
6542
6543 if (input_type == Primitive::kPrimFloat) {
6544 __ TruncLS(FTMP, src);
6545 } else {
6546 __ TruncLD(FTMP, src);
6547 }
6548 __ Mfc1(dst_low, FTMP);
6549 __ Mfhc1(dst_high, FTMP);
6550
6551 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006552 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006553 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6554 : kQuickD2l;
6555 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006556 if (input_type == Primitive::kPrimFloat) {
6557 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6558 } else {
6559 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6560 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006561 }
6562 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006563 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6564 Register dst = locations->Out().AsRegister<Register>();
6565 MipsLabel truncate;
6566 MipsLabel done;
6567
6568 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6569 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6570 // even though it must be NAN2008=1 on R6.
6571 //
6572 // For details see the large comment above for the truncation of float/double to long on R6.
6573 //
6574 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6575 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006576 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006577 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6578 __ LoadConst32(TMP, min_val);
6579 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006580 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006581 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6582 __ LoadConst32(TMP, High32Bits(min_val));
6583 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006584 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006585 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006586
6587 if (isR6) {
6588 if (input_type == Primitive::kPrimFloat) {
6589 __ CmpLeS(FTMP, FTMP, src);
6590 } else {
6591 __ CmpLeD(FTMP, FTMP, src);
6592 }
6593 __ Bc1nez(FTMP, &truncate);
6594
6595 if (input_type == Primitive::kPrimFloat) {
6596 __ CmpEqS(FTMP, src, src);
6597 } else {
6598 __ CmpEqD(FTMP, src, src);
6599 }
6600 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6601 __ Mfc1(TMP, FTMP);
6602 __ And(dst, dst, TMP);
6603 } else {
6604 if (input_type == Primitive::kPrimFloat) {
6605 __ ColeS(0, FTMP, src);
6606 } else {
6607 __ ColeD(0, FTMP, src);
6608 }
6609 __ Bc1t(0, &truncate);
6610
6611 if (input_type == Primitive::kPrimFloat) {
6612 __ CeqS(0, src, src);
6613 } else {
6614 __ CeqD(0, src, src);
6615 }
6616 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6617 __ Movf(dst, ZERO, 0);
6618 }
6619
6620 __ B(&done);
6621
6622 __ Bind(&truncate);
6623
6624 if (input_type == Primitive::kPrimFloat) {
6625 __ TruncWS(FTMP, src);
6626 } else {
6627 __ TruncWD(FTMP, src);
6628 }
6629 __ Mfc1(dst, FTMP);
6630
6631 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006632 }
6633 } else if (Primitive::IsFloatingPointType(result_type) &&
6634 Primitive::IsFloatingPointType(input_type)) {
6635 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6636 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6637 if (result_type == Primitive::kPrimFloat) {
6638 __ Cvtsd(dst, src);
6639 } else {
6640 __ Cvtds(dst, src);
6641 }
6642 } else {
6643 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6644 << " to " << result_type;
6645 }
6646}
6647
6648void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6649 HandleShift(ushr);
6650}
6651
6652void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6653 HandleShift(ushr);
6654}
6655
6656void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6657 HandleBinaryOp(instruction);
6658}
6659
6660void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6661 HandleBinaryOp(instruction);
6662}
6663
6664void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6665 // Nothing to do, this should be removed during prepare for register allocator.
6666 LOG(FATAL) << "Unreachable";
6667}
6668
6669void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6670 // Nothing to do, this should be removed during prepare for register allocator.
6671 LOG(FATAL) << "Unreachable";
6672}
6673
6674void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006675 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006676}
6677
6678void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006679 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006680}
6681
6682void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006683 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006684}
6685
6686void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006687 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006688}
6689
6690void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006691 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006692}
6693
6694void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006695 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006696}
6697
6698void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006699 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006700}
6701
6702void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006703 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006704}
6705
6706void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006707 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006708}
6709
6710void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006711 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006712}
6713
6714void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006715 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006716}
6717
6718void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006719 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006720}
6721
6722void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006723 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724}
6725
6726void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006727 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006728}
6729
6730void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006731 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732}
6733
6734void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006735 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006736}
6737
6738void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006739 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006740}
6741
6742void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006743 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006744}
6745
6746void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006747 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006748}
6749
6750void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006751 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006752}
6753
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006754void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6755 LocationSummary* locations =
6756 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6757 locations->SetInAt(0, Location::RequiresRegister());
6758}
6759
Alexey Frunze96b66822016-09-10 02:32:44 -07006760void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6761 int32_t lower_bound,
6762 uint32_t num_entries,
6763 HBasicBlock* switch_block,
6764 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006765 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006766 Register temp_reg = TMP;
6767 __ Addiu32(temp_reg, value_reg, -lower_bound);
6768 // Jump to default if index is negative
6769 // Note: We don't check the case that index is positive while value < lower_bound, because in
6770 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6771 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6772
Alexey Frunze96b66822016-09-10 02:32:44 -07006773 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006774 // Jump to successors[0] if value == lower_bound.
6775 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6776 int32_t last_index = 0;
6777 for (; num_entries - last_index > 2; last_index += 2) {
6778 __ Addiu(temp_reg, temp_reg, -2);
6779 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6780 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6781 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6782 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6783 }
6784 if (num_entries - last_index == 2) {
6785 // The last missing case_value.
6786 __ Addiu(temp_reg, temp_reg, -1);
6787 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006788 }
6789
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006790 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006791 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006792 __ B(codegen_->GetLabelOf(default_block));
6793 }
6794}
6795
Alexey Frunze96b66822016-09-10 02:32:44 -07006796void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6797 Register constant_area,
6798 int32_t lower_bound,
6799 uint32_t num_entries,
6800 HBasicBlock* switch_block,
6801 HBasicBlock* default_block) {
6802 // Create a jump table.
6803 std::vector<MipsLabel*> labels(num_entries);
6804 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6805 for (uint32_t i = 0; i < num_entries; i++) {
6806 labels[i] = codegen_->GetLabelOf(successors[i]);
6807 }
6808 JumpTable* table = __ CreateJumpTable(std::move(labels));
6809
6810 // Is the value in range?
6811 __ Addiu32(TMP, value_reg, -lower_bound);
6812 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6813 __ Sltiu(AT, TMP, num_entries);
6814 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6815 } else {
6816 __ LoadConst32(AT, num_entries);
6817 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6818 }
6819
6820 // We are in the range of the table.
6821 // Load the target address from the jump table, indexing by the value.
6822 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6823 __ Sll(TMP, TMP, 2);
6824 __ Addu(TMP, TMP, AT);
6825 __ Lw(TMP, TMP, 0);
6826 // Compute the absolute target address by adding the table start address
6827 // (the table contains offsets to targets relative to its start).
6828 __ Addu(TMP, TMP, AT);
6829 // And jump.
6830 __ Jr(TMP);
6831 __ NopIfNoReordering();
6832}
6833
6834void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6835 int32_t lower_bound = switch_instr->GetStartValue();
6836 uint32_t num_entries = switch_instr->GetNumEntries();
6837 LocationSummary* locations = switch_instr->GetLocations();
6838 Register value_reg = locations->InAt(0).AsRegister<Register>();
6839 HBasicBlock* switch_block = switch_instr->GetBlock();
6840 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6841
6842 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6843 num_entries > kPackedSwitchJumpTableThreshold) {
6844 // R6 uses PC-relative addressing to access the jump table.
6845 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6846 // the jump table and it is implemented by changing HPackedSwitch to
6847 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6848 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6849 GenTableBasedPackedSwitch(value_reg,
6850 ZERO,
6851 lower_bound,
6852 num_entries,
6853 switch_block,
6854 default_block);
6855 } else {
6856 GenPackedSwitchWithCompares(value_reg,
6857 lower_bound,
6858 num_entries,
6859 switch_block,
6860 default_block);
6861 }
6862}
6863
6864void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6865 LocationSummary* locations =
6866 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6867 locations->SetInAt(0, Location::RequiresRegister());
6868 // Constant area pointer (HMipsComputeBaseMethodAddress).
6869 locations->SetInAt(1, Location::RequiresRegister());
6870}
6871
6872void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6873 int32_t lower_bound = switch_instr->GetStartValue();
6874 uint32_t num_entries = switch_instr->GetNumEntries();
6875 LocationSummary* locations = switch_instr->GetLocations();
6876 Register value_reg = locations->InAt(0).AsRegister<Register>();
6877 Register constant_area = locations->InAt(1).AsRegister<Register>();
6878 HBasicBlock* switch_block = switch_instr->GetBlock();
6879 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6880
6881 // This is an R2-only path. HPackedSwitch has been changed to
6882 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6883 // required to address the jump table relative to PC.
6884 GenTableBasedPackedSwitch(value_reg,
6885 constant_area,
6886 lower_bound,
6887 num_entries,
6888 switch_block,
6889 default_block);
6890}
6891
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006892void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6893 HMipsComputeBaseMethodAddress* insn) {
6894 LocationSummary* locations =
6895 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6896 locations->SetOut(Location::RequiresRegister());
6897}
6898
6899void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6900 HMipsComputeBaseMethodAddress* insn) {
6901 LocationSummary* locations = insn->GetLocations();
6902 Register reg = locations->Out().AsRegister<Register>();
6903
6904 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6905
6906 // Generate a dummy PC-relative call to obtain PC.
6907 __ Nal();
6908 // Grab the return address off RA.
6909 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006910 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006911
6912 // Remember this offset (the obtained PC value) for later use with constant area.
6913 __ BindPcRelBaseLabel();
6914}
6915
6916void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6918 locations->SetOut(Location::RequiresRegister());
6919}
6920
6921void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6922 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6923 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6924 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006925 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6926 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006927 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006928 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
6929 __ Addiu(reg, reg, /* placeholder */ 0x5678);
6930 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006931}
6932
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006933void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6934 // The trampoline uses the same calling convention as dex calling conventions,
6935 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6936 // the method_idx.
6937 HandleInvoke(invoke);
6938}
6939
6940void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6941 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6942}
6943
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006944void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6945 LocationSummary* locations =
6946 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6947 locations->SetInAt(0, Location::RequiresRegister());
6948 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006949}
6950
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006951void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6952 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006953 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006954 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006955 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006956 __ LoadFromOffset(kLoadWord,
6957 locations->Out().AsRegister<Register>(),
6958 locations->InAt(0).AsRegister<Register>(),
6959 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006960 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006961 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006962 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006963 __ LoadFromOffset(kLoadWord,
6964 locations->Out().AsRegister<Register>(),
6965 locations->InAt(0).AsRegister<Register>(),
6966 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006967 __ LoadFromOffset(kLoadWord,
6968 locations->Out().AsRegister<Register>(),
6969 locations->Out().AsRegister<Register>(),
6970 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006971 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006972}
6973
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006974#undef __
6975#undef QUICK_ENTRY_POINT
6976
6977} // namespace mips
6978} // namespace art