blob: c9dde7cc55228b206bc79f44e5b644052c410fde [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)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800487 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
488 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700489 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490 // Save RA (containing the return address) to mimic Quick.
491 AddAllocatedRegister(Location::RegisterLocation(RA));
492}
493
494#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100495// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
496#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700497#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498
499void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
500 // Ensure that we fix up branches.
501 __ FinalizeCode();
502
503 // Adjust native pc offsets in stack maps.
504 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800505 uint32_t old_position =
506 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200507 uint32_t new_position = __ GetAdjustedPosition(old_position);
508 DCHECK_GE(new_position, old_position);
509 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
510 }
511
512 // Adjust pc offsets for the disassembly information.
513 if (disasm_info_ != nullptr) {
514 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
515 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
516 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
517 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
518 it.second.start = __ GetAdjustedPosition(it.second.start);
519 it.second.end = __ GetAdjustedPosition(it.second.end);
520 }
521 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
522 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
523 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
524 }
525 }
526
527 CodeGenerator::Finalize(allocator);
528}
529
530MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
531 return codegen_->GetAssembler();
532}
533
534void ParallelMoveResolverMIPS::EmitMove(size_t index) {
535 DCHECK_LT(index, moves_.size());
536 MoveOperands* move = moves_[index];
537 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
538}
539
540void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
541 DCHECK_LT(index, moves_.size());
542 MoveOperands* move = moves_[index];
543 Primitive::Type type = move->GetType();
544 Location loc1 = move->GetDestination();
545 Location loc2 = move->GetSource();
546
547 DCHECK(!loc1.IsConstant());
548 DCHECK(!loc2.IsConstant());
549
550 if (loc1.Equals(loc2)) {
551 return;
552 }
553
554 if (loc1.IsRegister() && loc2.IsRegister()) {
555 // Swap 2 GPRs.
556 Register r1 = loc1.AsRegister<Register>();
557 Register r2 = loc2.AsRegister<Register>();
558 __ Move(TMP, r2);
559 __ Move(r2, r1);
560 __ Move(r1, TMP);
561 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
562 FRegister f1 = loc1.AsFpuRegister<FRegister>();
563 FRegister f2 = loc2.AsFpuRegister<FRegister>();
564 if (type == Primitive::kPrimFloat) {
565 __ MovS(FTMP, f2);
566 __ MovS(f2, f1);
567 __ MovS(f1, FTMP);
568 } else {
569 DCHECK_EQ(type, Primitive::kPrimDouble);
570 __ MovD(FTMP, f2);
571 __ MovD(f2, f1);
572 __ MovD(f1, FTMP);
573 }
574 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
575 (loc1.IsFpuRegister() && loc2.IsRegister())) {
576 // Swap FPR and GPR.
577 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
578 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
579 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200580 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200581 __ Move(TMP, r2);
582 __ Mfc1(r2, f1);
583 __ Mtc1(TMP, f1);
584 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
585 // Swap 2 GPR register pairs.
586 Register r1 = loc1.AsRegisterPairLow<Register>();
587 Register r2 = loc2.AsRegisterPairLow<Register>();
588 __ Move(TMP, r2);
589 __ Move(r2, r1);
590 __ Move(r1, TMP);
591 r1 = loc1.AsRegisterPairHigh<Register>();
592 r2 = loc2.AsRegisterPairHigh<Register>();
593 __ Move(TMP, r2);
594 __ Move(r2, r1);
595 __ Move(r1, TMP);
596 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
597 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
598 // Swap FPR and GPR register pair.
599 DCHECK_EQ(type, Primitive::kPrimDouble);
600 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
601 : loc2.AsFpuRegister<FRegister>();
602 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
603 : loc2.AsRegisterPairLow<Register>();
604 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
605 : loc2.AsRegisterPairHigh<Register>();
606 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
607 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
608 // unpredictable and the following mfch1 will fail.
609 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800610 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200611 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800612 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200613 __ Move(r2_l, TMP);
614 __ Move(r2_h, AT);
615 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
617 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
618 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000619 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
620 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200621 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
622 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000623 __ Move(TMP, reg);
624 __ LoadFromOffset(kLoadWord, reg, SP, offset);
625 __ StoreToOffset(kStoreWord, TMP, SP, offset);
626 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
627 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
628 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
629 : loc2.AsRegisterPairLow<Register>();
630 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
631 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200632 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000633 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
634 : loc2.GetHighStackIndex(kMipsWordSize);
635 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000636 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000637 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000638 __ Move(TMP, reg_h);
639 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
640 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200641 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
642 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
643 : loc2.AsFpuRegister<FRegister>();
644 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
645 if (type == Primitive::kPrimFloat) {
646 __ MovS(FTMP, reg);
647 __ LoadSFromOffset(reg, SP, offset);
648 __ StoreSToOffset(FTMP, SP, offset);
649 } else {
650 DCHECK_EQ(type, Primitive::kPrimDouble);
651 __ MovD(FTMP, reg);
652 __ LoadDFromOffset(reg, SP, offset);
653 __ StoreDToOffset(FTMP, SP, offset);
654 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200655 } else {
656 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
657 }
658}
659
660void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
661 __ Pop(static_cast<Register>(reg));
662}
663
664void ParallelMoveResolverMIPS::SpillScratch(int reg) {
665 __ Push(static_cast<Register>(reg));
666}
667
668void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
669 // Allocate a scratch register other than TMP, if available.
670 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
671 // automatically unspilled when the scratch scope object is destroyed).
672 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
673 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
674 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
675 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
676 __ LoadFromOffset(kLoadWord,
677 Register(ensure_scratch.GetRegister()),
678 SP,
679 index1 + stack_offset);
680 __ LoadFromOffset(kLoadWord,
681 TMP,
682 SP,
683 index2 + stack_offset);
684 __ StoreToOffset(kStoreWord,
685 Register(ensure_scratch.GetRegister()),
686 SP,
687 index2 + stack_offset);
688 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
689 }
690}
691
Alexey Frunze73296a72016-06-03 22:51:46 -0700692void CodeGeneratorMIPS::ComputeSpillMask() {
693 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
694 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
695 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
696 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
697 // registers, include the ZERO register to force alignment of FPU callee-saved registers
698 // within the stack frame.
699 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
700 core_spill_mask_ |= (1 << ZERO);
701 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700702}
703
704bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700705 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700706 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
707 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
708 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700709 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700710}
711
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200712static dwarf::Reg DWARFReg(Register reg) {
713 return dwarf::Reg::MipsCore(static_cast<int>(reg));
714}
715
716// TODO: mapping of floating-point registers to DWARF.
717
718void CodeGeneratorMIPS::GenerateFrameEntry() {
719 __ Bind(&frame_entry_label_);
720
721 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
722
723 if (do_overflow_check) {
724 __ LoadFromOffset(kLoadWord,
725 ZERO,
726 SP,
727 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
728 RecordPcInfo(nullptr, 0);
729 }
730
731 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700732 CHECK_EQ(fpu_spill_mask_, 0u);
733 CHECK_EQ(core_spill_mask_, 1u << RA);
734 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200735 return;
736 }
737
738 // Make sure the frame size isn't unreasonably large.
739 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
740 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
741 }
742
743 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200746 __ IncreaseFrameSize(ofs);
747
Alexey Frunze73296a72016-06-03 22:51:46 -0700748 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
749 Register reg = static_cast<Register>(MostSignificantBit(mask));
750 mask ^= 1u << reg;
751 ofs -= kMipsWordSize;
752 // The ZERO register is only included for alignment.
753 if (reg != ZERO) {
754 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200755 __ cfi().RelOffset(DWARFReg(reg), ofs);
756 }
757 }
758
Alexey Frunze73296a72016-06-03 22:51:46 -0700759 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
760 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
761 mask ^= 1u << reg;
762 ofs -= kMipsDoublewordSize;
763 __ StoreDToOffset(reg, SP, ofs);
764 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200765 }
766
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100767 // Save the current method if we need it. Note that we do not
768 // do this in HCurrentMethod, as the instruction might have been removed
769 // in the SSA graph.
770 if (RequiresCurrentMethod()) {
771 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
772 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100773
774 if (GetGraph()->HasShouldDeoptimizeFlag()) {
775 // Initialize should deoptimize flag to 0.
776 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
777 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200778}
779
780void CodeGeneratorMIPS::GenerateFrameExit() {
781 __ cfi().RememberState();
782
783 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200784 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200785
Alexey Frunze73296a72016-06-03 22:51:46 -0700786 // For better instruction scheduling restore RA before other registers.
787 uint32_t ofs = GetFrameSize();
788 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
789 Register reg = static_cast<Register>(MostSignificantBit(mask));
790 mask ^= 1u << reg;
791 ofs -= kMipsWordSize;
792 // The ZERO register is only included for alignment.
793 if (reg != ZERO) {
794 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200795 __ cfi().Restore(DWARFReg(reg));
796 }
797 }
798
Alexey Frunze73296a72016-06-03 22:51:46 -0700799 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
800 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
801 mask ^= 1u << reg;
802 ofs -= kMipsDoublewordSize;
803 __ LoadDFromOffset(reg, SP, ofs);
804 // TODO: __ cfi().Restore(DWARFReg(reg));
805 }
806
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700807 size_t frame_size = GetFrameSize();
808 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
809 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
810 bool reordering = __ SetReorder(false);
811 if (exchange) {
812 __ Jr(RA);
813 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
814 } else {
815 __ DecreaseFrameSize(frame_size);
816 __ Jr(RA);
817 __ Nop(); // In delay slot.
818 }
819 __ SetReorder(reordering);
820 } else {
821 __ Jr(RA);
822 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200823 }
824
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200825 __ cfi().RestoreState();
826 __ cfi().DefCFAOffset(GetFrameSize());
827}
828
829void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
830 __ Bind(GetLabelOf(block));
831}
832
833void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
834 if (src.Equals(dst)) {
835 return;
836 }
837
838 if (src.IsConstant()) {
839 MoveConstant(dst, src.GetConstant());
840 } else {
841 if (Primitive::Is64BitType(dst_type)) {
842 Move64(dst, src);
843 } else {
844 Move32(dst, src);
845 }
846 }
847}
848
849void CodeGeneratorMIPS::Move32(Location destination, Location source) {
850 if (source.Equals(destination)) {
851 return;
852 }
853
854 if (destination.IsRegister()) {
855 if (source.IsRegister()) {
856 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
857 } else if (source.IsFpuRegister()) {
858 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
859 } else {
860 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
861 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
862 }
863 } else if (destination.IsFpuRegister()) {
864 if (source.IsRegister()) {
865 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
866 } else if (source.IsFpuRegister()) {
867 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
868 } else {
869 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
870 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
871 }
872 } else {
873 DCHECK(destination.IsStackSlot()) << destination;
874 if (source.IsRegister()) {
875 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
876 } else if (source.IsFpuRegister()) {
877 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
878 } else {
879 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
880 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
881 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
882 }
883 }
884}
885
886void CodeGeneratorMIPS::Move64(Location destination, Location source) {
887 if (source.Equals(destination)) {
888 return;
889 }
890
891 if (destination.IsRegisterPair()) {
892 if (source.IsRegisterPair()) {
893 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
894 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
895 } else if (source.IsFpuRegister()) {
896 Register dst_high = destination.AsRegisterPairHigh<Register>();
897 Register dst_low = destination.AsRegisterPairLow<Register>();
898 FRegister src = source.AsFpuRegister<FRegister>();
899 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800900 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200901 } else {
902 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
903 int32_t off = source.GetStackIndex();
904 Register r = destination.AsRegisterPairLow<Register>();
905 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
906 }
907 } else if (destination.IsFpuRegister()) {
908 if (source.IsRegisterPair()) {
909 FRegister dst = destination.AsFpuRegister<FRegister>();
910 Register src_high = source.AsRegisterPairHigh<Register>();
911 Register src_low = source.AsRegisterPairLow<Register>();
912 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800913 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200914 } else if (source.IsFpuRegister()) {
915 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
916 } else {
917 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
918 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
919 }
920 } else {
921 DCHECK(destination.IsDoubleStackSlot()) << destination;
922 int32_t off = destination.GetStackIndex();
923 if (source.IsRegisterPair()) {
924 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
925 } else if (source.IsFpuRegister()) {
926 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
927 } else {
928 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
929 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
930 __ StoreToOffset(kStoreWord, TMP, SP, off);
931 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
932 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
933 }
934 }
935}
936
937void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
938 if (c->IsIntConstant() || c->IsNullConstant()) {
939 // Move 32 bit constant.
940 int32_t value = GetInt32ValueOf(c);
941 if (destination.IsRegister()) {
942 Register dst = destination.AsRegister<Register>();
943 __ LoadConst32(dst, value);
944 } else {
945 DCHECK(destination.IsStackSlot())
946 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700947 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200948 }
949 } else if (c->IsLongConstant()) {
950 // Move 64 bit constant.
951 int64_t value = GetInt64ValueOf(c);
952 if (destination.IsRegisterPair()) {
953 Register r_h = destination.AsRegisterPairHigh<Register>();
954 Register r_l = destination.AsRegisterPairLow<Register>();
955 __ LoadConst64(r_h, r_l, value);
956 } else {
957 DCHECK(destination.IsDoubleStackSlot())
958 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700959 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200960 }
961 } else if (c->IsFloatConstant()) {
962 // Move 32 bit float constant.
963 int32_t value = GetInt32ValueOf(c);
964 if (destination.IsFpuRegister()) {
965 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
966 } else {
967 DCHECK(destination.IsStackSlot())
968 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700969 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200970 }
971 } else {
972 // Move 64 bit double constant.
973 DCHECK(c->IsDoubleConstant()) << c->DebugName();
974 int64_t value = GetInt64ValueOf(c);
975 if (destination.IsFpuRegister()) {
976 FRegister fd = destination.AsFpuRegister<FRegister>();
977 __ LoadDConst64(fd, value, TMP);
978 } else {
979 DCHECK(destination.IsDoubleStackSlot())
980 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700981 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200982 }
983 }
984}
985
986void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
987 DCHECK(destination.IsRegister());
988 Register dst = destination.AsRegister<Register>();
989 __ LoadConst32(dst, value);
990}
991
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200992void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
993 if (location.IsRegister()) {
994 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700995 } else if (location.IsRegisterPair()) {
996 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
997 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200998 } else {
999 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1000 }
1001}
1002
Vladimir Markoaad75c62016-10-03 08:46:48 +00001003template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1004inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1005 const ArenaDeque<PcRelativePatchInfo>& infos,
1006 ArenaVector<LinkerPatch>* linker_patches) {
1007 for (const PcRelativePatchInfo& info : infos) {
1008 const DexFile& dex_file = info.target_dex_file;
1009 size_t offset_or_index = info.offset_or_index;
1010 DCHECK(info.high_label.IsBound());
1011 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1012 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1013 // the assembler's base label used for PC-relative addressing.
1014 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1015 ? __ GetLabelLocation(&info.pc_rel_label)
1016 : __ GetPcRelBaseLabelLocation();
1017 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1018 }
1019}
1020
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001021void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1022 DCHECK(linker_patches->empty());
1023 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001024 pc_relative_dex_cache_patches_.size() +
1025 pc_relative_string_patches_.size() +
1026 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001027 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001028 boot_image_string_patches_.size() +
1029 boot_image_type_patches_.size() +
1030 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001031 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001032 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1033 linker_patches);
1034 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001035 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001036 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1037 linker_patches);
1038 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001039 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1040 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001041 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1042 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001043 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001044 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1045 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001046 for (const auto& entry : boot_image_string_patches_) {
1047 const StringReference& target_string = entry.first;
1048 Literal* literal = entry.second;
1049 DCHECK(literal->GetLabel()->IsBound());
1050 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1051 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1052 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001053 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001054 }
1055 for (const auto& entry : boot_image_type_patches_) {
1056 const TypeReference& target_type = entry.first;
1057 Literal* literal = entry.second;
1058 DCHECK(literal->GetLabel()->IsBound());
1059 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1060 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1061 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001062 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001063 }
1064 for (const auto& entry : boot_image_address_patches_) {
1065 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1066 Literal* literal = entry.second;
1067 DCHECK(literal->GetLabel()->IsBound());
1068 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1069 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1070 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001071 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001072}
1073
1074CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001075 const DexFile& dex_file, dex::StringIndex string_index) {
1076 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001077}
1078
1079CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001080 const DexFile& dex_file, dex::TypeIndex type_index) {
1081 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001082}
1083
Vladimir Marko1998cd02017-01-13 13:02:58 +00001084CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1085 const DexFile& dex_file, dex::TypeIndex type_index) {
1086 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1087}
1088
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001089CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1090 const DexFile& dex_file, uint32_t element_offset) {
1091 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1092}
1093
1094CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1095 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1096 patches->emplace_back(dex_file, offset_or_index);
1097 return &patches->back();
1098}
1099
Alexey Frunze06a46c42016-07-19 15:00:40 -07001100Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1101 return map->GetOrCreate(
1102 value,
1103 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1104}
1105
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001106Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1107 MethodToLiteralMap* map) {
1108 return map->GetOrCreate(
1109 target_method,
1110 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1111}
1112
Alexey Frunze06a46c42016-07-19 15:00:40 -07001113Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001114 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001115 return boot_image_string_patches_.GetOrCreate(
1116 StringReference(&dex_file, string_index),
1117 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1118}
1119
1120Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001121 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001122 return boot_image_type_patches_.GetOrCreate(
1123 TypeReference(&dex_file, type_index),
1124 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1125}
1126
1127Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1128 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1129 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1130 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1131}
1132
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001133void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1134 Register out,
1135 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001136 if (GetInstructionSetFeatures().IsR6()) {
1137 DCHECK_EQ(base, ZERO);
1138 __ Bind(&info->high_label);
1139 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001140 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001141 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001142 } else {
1143 // If base is ZERO, emit NAL to obtain the actual base.
1144 if (base == ZERO) {
1145 // Generate a dummy PC-relative call to obtain PC.
1146 __ Nal();
1147 }
1148 __ Bind(&info->high_label);
1149 __ Lui(out, /* placeholder */ 0x1234);
1150 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1151 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1152 if (base == ZERO) {
1153 __ Bind(&info->pc_rel_label);
1154 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001155 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001156 __ Addu(out, out, (base == ZERO) ? RA : base);
1157 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001158 // The immediately following instruction will add the sign-extended low half of the 32-bit
1159 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001160}
1161
Alexey Frunze627c1a02017-01-30 19:28:14 -08001162CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1163 const DexFile& dex_file,
1164 dex::StringIndex dex_index,
1165 Handle<mirror::String> handle) {
1166 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1167 reinterpret_cast64<uint64_t>(handle.GetReference()));
1168 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1169 return &jit_string_patches_.back();
1170}
1171
1172CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1173 const DexFile& dex_file,
1174 dex::TypeIndex dex_index,
1175 Handle<mirror::Class> handle) {
1176 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1177 reinterpret_cast64<uint64_t>(handle.GetReference()));
1178 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1179 return &jit_class_patches_.back();
1180}
1181
1182void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1183 const uint8_t* roots_data,
1184 const CodeGeneratorMIPS::JitPatchInfo& info,
1185 uint64_t index_in_table) const {
1186 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1187 uintptr_t address =
1188 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1189 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1190 // lui reg, addr32_high
1191 DCHECK_EQ(code[literal_offset + 0], 0x34);
1192 DCHECK_EQ(code[literal_offset + 1], 0x12);
1193 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1194 DCHECK_EQ(code[literal_offset + 3], 0x3C);
1195 // lw reg, reg, addr32_low
1196 DCHECK_EQ(code[literal_offset + 4], 0x78);
1197 DCHECK_EQ(code[literal_offset + 5], 0x56);
1198 DCHECK_EQ((code[literal_offset + 7] & 0xFC), 0x8C);
1199 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "lw reg, reg, addr32_low".
1200 // lui reg, addr32_high
1201 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1202 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
1203 // lw reg, reg, addr32_low
1204 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1205 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1206}
1207
1208void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1209 for (const JitPatchInfo& info : jit_string_patches_) {
1210 const auto& it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1211 dex::StringIndex(info.index)));
1212 DCHECK(it != jit_string_roots_.end());
1213 PatchJitRootUse(code, roots_data, info, it->second);
1214 }
1215 for (const JitPatchInfo& info : jit_class_patches_) {
1216 const auto& it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1217 dex::TypeIndex(info.index)));
1218 DCHECK(it != jit_class_roots_.end());
1219 PatchJitRootUse(code, roots_data, info, it->second);
1220 }
1221}
1222
Goran Jakovljevice114da22016-12-26 14:21:43 +01001223void CodeGeneratorMIPS::MarkGCCard(Register object,
1224 Register value,
1225 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001226 MipsLabel done;
1227 Register card = AT;
1228 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001229 if (value_can_be_null) {
1230 __ Beqz(value, &done);
1231 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001232 __ LoadFromOffset(kLoadWord,
1233 card,
1234 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001235 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1237 __ Addu(temp, card, temp);
1238 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001239 if (value_can_be_null) {
1240 __ Bind(&done);
1241 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242}
1243
David Brazdil58282f42016-01-14 12:45:10 +00001244void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1246 blocked_core_registers_[ZERO] = true;
1247 blocked_core_registers_[K0] = true;
1248 blocked_core_registers_[K1] = true;
1249 blocked_core_registers_[GP] = true;
1250 blocked_core_registers_[SP] = true;
1251 blocked_core_registers_[RA] = true;
1252
1253 // AT and TMP(T8) are used as temporary/scratch registers
1254 // (similar to how AT is used by MIPS assemblers).
1255 blocked_core_registers_[AT] = true;
1256 blocked_core_registers_[TMP] = true;
1257 blocked_fpu_registers_[FTMP] = true;
1258
1259 // Reserve suspend and thread registers.
1260 blocked_core_registers_[S0] = true;
1261 blocked_core_registers_[TR] = true;
1262
1263 // Reserve T9 for function calls
1264 blocked_core_registers_[T9] = true;
1265
1266 // Reserve odd-numbered FPU registers.
1267 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1268 blocked_fpu_registers_[i] = true;
1269 }
1270
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001271 if (GetGraph()->IsDebuggable()) {
1272 // Stubs do not save callee-save floating point registers. If the graph
1273 // is debuggable, we need to deal with these registers differently. For
1274 // now, just block them.
1275 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1276 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1277 }
1278 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001279}
1280
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1282 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1283 return kMipsWordSize;
1284}
1285
1286size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1287 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1288 return kMipsWordSize;
1289}
1290
1291size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1292 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1293 return kMipsDoublewordSize;
1294}
1295
1296size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1297 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1298 return kMipsDoublewordSize;
1299}
1300
1301void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001302 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001303}
1304
1305void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001306 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001307}
1308
Serban Constantinescufca16662016-07-14 09:21:59 +01001309constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1310
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1312 HInstruction* instruction,
1313 uint32_t dex_pc,
1314 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001315 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001316 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001317 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001318 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001319 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001320 // Reserve argument space on stack (for $a0-$a3) for
1321 // entrypoints that directly reference native implementations.
1322 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001323 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001324 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001325 } else {
1326 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001327 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001328 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001329 if (EntrypointRequiresStackMap(entrypoint)) {
1330 RecordPcInfo(instruction, dex_pc, slow_path);
1331 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001332}
1333
1334void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1335 Register class_reg) {
1336 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1337 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1338 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1339 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1340 __ Sync(0);
1341 __ Bind(slow_path->GetExitLabel());
1342}
1343
1344void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1345 __ Sync(0); // Only stype 0 is supported.
1346}
1347
1348void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1349 HBasicBlock* successor) {
1350 SuspendCheckSlowPathMIPS* slow_path =
1351 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1352 codegen_->AddSlowPath(slow_path);
1353
1354 __ LoadFromOffset(kLoadUnsignedHalfword,
1355 TMP,
1356 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001357 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001358 if (successor == nullptr) {
1359 __ Bnez(TMP, slow_path->GetEntryLabel());
1360 __ Bind(slow_path->GetReturnLabel());
1361 } else {
1362 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1363 __ B(slow_path->GetEntryLabel());
1364 // slow_path will return to GetLabelOf(successor).
1365 }
1366}
1367
1368InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1369 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001370 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001371 assembler_(codegen->GetAssembler()),
1372 codegen_(codegen) {}
1373
1374void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1375 DCHECK_EQ(instruction->InputCount(), 2U);
1376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1377 Primitive::Type type = instruction->GetResultType();
1378 switch (type) {
1379 case Primitive::kPrimInt: {
1380 locations->SetInAt(0, Location::RequiresRegister());
1381 HInstruction* right = instruction->InputAt(1);
1382 bool can_use_imm = false;
1383 if (right->IsConstant()) {
1384 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1385 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1386 can_use_imm = IsUint<16>(imm);
1387 } else if (instruction->IsAdd()) {
1388 can_use_imm = IsInt<16>(imm);
1389 } else {
1390 DCHECK(instruction->IsSub());
1391 can_use_imm = IsInt<16>(-imm);
1392 }
1393 }
1394 if (can_use_imm)
1395 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1396 else
1397 locations->SetInAt(1, Location::RequiresRegister());
1398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1399 break;
1400 }
1401
1402 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001403 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001404 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406 break;
1407 }
1408
1409 case Primitive::kPrimFloat:
1410 case Primitive::kPrimDouble:
1411 DCHECK(instruction->IsAdd() || instruction->IsSub());
1412 locations->SetInAt(0, Location::RequiresFpuRegister());
1413 locations->SetInAt(1, Location::RequiresFpuRegister());
1414 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1415 break;
1416
1417 default:
1418 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1419 }
1420}
1421
1422void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1423 Primitive::Type type = instruction->GetType();
1424 LocationSummary* locations = instruction->GetLocations();
1425
1426 switch (type) {
1427 case Primitive::kPrimInt: {
1428 Register dst = locations->Out().AsRegister<Register>();
1429 Register lhs = locations->InAt(0).AsRegister<Register>();
1430 Location rhs_location = locations->InAt(1);
1431
1432 Register rhs_reg = ZERO;
1433 int32_t rhs_imm = 0;
1434 bool use_imm = rhs_location.IsConstant();
1435 if (use_imm) {
1436 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1437 } else {
1438 rhs_reg = rhs_location.AsRegister<Register>();
1439 }
1440
1441 if (instruction->IsAnd()) {
1442 if (use_imm)
1443 __ Andi(dst, lhs, rhs_imm);
1444 else
1445 __ And(dst, lhs, rhs_reg);
1446 } else if (instruction->IsOr()) {
1447 if (use_imm)
1448 __ Ori(dst, lhs, rhs_imm);
1449 else
1450 __ Or(dst, lhs, rhs_reg);
1451 } else if (instruction->IsXor()) {
1452 if (use_imm)
1453 __ Xori(dst, lhs, rhs_imm);
1454 else
1455 __ Xor(dst, lhs, rhs_reg);
1456 } else if (instruction->IsAdd()) {
1457 if (use_imm)
1458 __ Addiu(dst, lhs, rhs_imm);
1459 else
1460 __ Addu(dst, lhs, rhs_reg);
1461 } else {
1462 DCHECK(instruction->IsSub());
1463 if (use_imm)
1464 __ Addiu(dst, lhs, -rhs_imm);
1465 else
1466 __ Subu(dst, lhs, rhs_reg);
1467 }
1468 break;
1469 }
1470
1471 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001472 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1473 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1474 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1475 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001476 Location rhs_location = locations->InAt(1);
1477 bool use_imm = rhs_location.IsConstant();
1478 if (!use_imm) {
1479 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1480 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1481 if (instruction->IsAnd()) {
1482 __ And(dst_low, lhs_low, rhs_low);
1483 __ And(dst_high, lhs_high, rhs_high);
1484 } else if (instruction->IsOr()) {
1485 __ Or(dst_low, lhs_low, rhs_low);
1486 __ Or(dst_high, lhs_high, rhs_high);
1487 } else if (instruction->IsXor()) {
1488 __ Xor(dst_low, lhs_low, rhs_low);
1489 __ Xor(dst_high, lhs_high, rhs_high);
1490 } else if (instruction->IsAdd()) {
1491 if (lhs_low == rhs_low) {
1492 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1493 __ Slt(TMP, lhs_low, ZERO);
1494 __ Addu(dst_low, lhs_low, rhs_low);
1495 } else {
1496 __ Addu(dst_low, lhs_low, rhs_low);
1497 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1498 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1499 }
1500 __ Addu(dst_high, lhs_high, rhs_high);
1501 __ Addu(dst_high, dst_high, TMP);
1502 } else {
1503 DCHECK(instruction->IsSub());
1504 __ Sltu(TMP, lhs_low, rhs_low);
1505 __ Subu(dst_low, lhs_low, rhs_low);
1506 __ Subu(dst_high, lhs_high, rhs_high);
1507 __ Subu(dst_high, dst_high, TMP);
1508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001509 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001510 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1511 if (instruction->IsOr()) {
1512 uint32_t low = Low32Bits(value);
1513 uint32_t high = High32Bits(value);
1514 if (IsUint<16>(low)) {
1515 if (dst_low != lhs_low || low != 0) {
1516 __ Ori(dst_low, lhs_low, low);
1517 }
1518 } else {
1519 __ LoadConst32(TMP, low);
1520 __ Or(dst_low, lhs_low, TMP);
1521 }
1522 if (IsUint<16>(high)) {
1523 if (dst_high != lhs_high || high != 0) {
1524 __ Ori(dst_high, lhs_high, high);
1525 }
1526 } else {
1527 if (high != low) {
1528 __ LoadConst32(TMP, high);
1529 }
1530 __ Or(dst_high, lhs_high, TMP);
1531 }
1532 } else if (instruction->IsXor()) {
1533 uint32_t low = Low32Bits(value);
1534 uint32_t high = High32Bits(value);
1535 if (IsUint<16>(low)) {
1536 if (dst_low != lhs_low || low != 0) {
1537 __ Xori(dst_low, lhs_low, low);
1538 }
1539 } else {
1540 __ LoadConst32(TMP, low);
1541 __ Xor(dst_low, lhs_low, TMP);
1542 }
1543 if (IsUint<16>(high)) {
1544 if (dst_high != lhs_high || high != 0) {
1545 __ Xori(dst_high, lhs_high, high);
1546 }
1547 } else {
1548 if (high != low) {
1549 __ LoadConst32(TMP, high);
1550 }
1551 __ Xor(dst_high, lhs_high, TMP);
1552 }
1553 } else if (instruction->IsAnd()) {
1554 uint32_t low = Low32Bits(value);
1555 uint32_t high = High32Bits(value);
1556 if (IsUint<16>(low)) {
1557 __ Andi(dst_low, lhs_low, low);
1558 } else if (low != 0xFFFFFFFF) {
1559 __ LoadConst32(TMP, low);
1560 __ And(dst_low, lhs_low, TMP);
1561 } else if (dst_low != lhs_low) {
1562 __ Move(dst_low, lhs_low);
1563 }
1564 if (IsUint<16>(high)) {
1565 __ Andi(dst_high, lhs_high, high);
1566 } else if (high != 0xFFFFFFFF) {
1567 if (high != low) {
1568 __ LoadConst32(TMP, high);
1569 }
1570 __ And(dst_high, lhs_high, TMP);
1571 } else if (dst_high != lhs_high) {
1572 __ Move(dst_high, lhs_high);
1573 }
1574 } else {
1575 if (instruction->IsSub()) {
1576 value = -value;
1577 } else {
1578 DCHECK(instruction->IsAdd());
1579 }
1580 int32_t low = Low32Bits(value);
1581 int32_t high = High32Bits(value);
1582 if (IsInt<16>(low)) {
1583 if (dst_low != lhs_low || low != 0) {
1584 __ Addiu(dst_low, lhs_low, low);
1585 }
1586 if (low != 0) {
1587 __ Sltiu(AT, dst_low, low);
1588 }
1589 } else {
1590 __ LoadConst32(TMP, low);
1591 __ Addu(dst_low, lhs_low, TMP);
1592 __ Sltu(AT, dst_low, TMP);
1593 }
1594 if (IsInt<16>(high)) {
1595 if (dst_high != lhs_high || high != 0) {
1596 __ Addiu(dst_high, lhs_high, high);
1597 }
1598 } else {
1599 if (high != low) {
1600 __ LoadConst32(TMP, high);
1601 }
1602 __ Addu(dst_high, lhs_high, TMP);
1603 }
1604 if (low != 0) {
1605 __ Addu(dst_high, dst_high, AT);
1606 }
1607 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001608 }
1609 break;
1610 }
1611
1612 case Primitive::kPrimFloat:
1613 case Primitive::kPrimDouble: {
1614 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1615 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1616 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1617 if (instruction->IsAdd()) {
1618 if (type == Primitive::kPrimFloat) {
1619 __ AddS(dst, lhs, rhs);
1620 } else {
1621 __ AddD(dst, lhs, rhs);
1622 }
1623 } else {
1624 DCHECK(instruction->IsSub());
1625 if (type == Primitive::kPrimFloat) {
1626 __ SubS(dst, lhs, rhs);
1627 } else {
1628 __ SubD(dst, lhs, rhs);
1629 }
1630 }
1631 break;
1632 }
1633
1634 default:
1635 LOG(FATAL) << "Unexpected binary operation type " << type;
1636 }
1637}
1638
1639void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001640 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641
1642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1643 Primitive::Type type = instr->GetResultType();
1644 switch (type) {
1645 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1649 break;
1650 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001651 locations->SetInAt(0, Location::RequiresRegister());
1652 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1653 locations->SetOut(Location::RequiresRegister());
1654 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001655 default:
1656 LOG(FATAL) << "Unexpected shift type " << type;
1657 }
1658}
1659
1660static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1661
1662void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001663 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001664 LocationSummary* locations = instr->GetLocations();
1665 Primitive::Type type = instr->GetType();
1666
1667 Location rhs_location = locations->InAt(1);
1668 bool use_imm = rhs_location.IsConstant();
1669 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1670 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001671 const uint32_t shift_mask =
1672 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001673 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001674 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1675 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001676
1677 switch (type) {
1678 case Primitive::kPrimInt: {
1679 Register dst = locations->Out().AsRegister<Register>();
1680 Register lhs = locations->InAt(0).AsRegister<Register>();
1681 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001682 if (shift_value == 0) {
1683 if (dst != lhs) {
1684 __ Move(dst, lhs);
1685 }
1686 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001687 __ Sll(dst, lhs, shift_value);
1688 } else if (instr->IsShr()) {
1689 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001690 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001691 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001692 } else {
1693 if (has_ins_rotr) {
1694 __ Rotr(dst, lhs, shift_value);
1695 } else {
1696 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1697 __ Srl(dst, lhs, shift_value);
1698 __ Or(dst, dst, TMP);
1699 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001700 }
1701 } else {
1702 if (instr->IsShl()) {
1703 __ Sllv(dst, lhs, rhs_reg);
1704 } else if (instr->IsShr()) {
1705 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001706 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001707 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001708 } else {
1709 if (has_ins_rotr) {
1710 __ Rotrv(dst, lhs, rhs_reg);
1711 } else {
1712 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001713 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1714 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1715 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1716 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1717 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001718 __ Sllv(TMP, lhs, TMP);
1719 __ Srlv(dst, lhs, rhs_reg);
1720 __ Or(dst, dst, TMP);
1721 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001722 }
1723 }
1724 break;
1725 }
1726
1727 case Primitive::kPrimLong: {
1728 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1729 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1730 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1731 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1732 if (use_imm) {
1733 if (shift_value == 0) {
1734 codegen_->Move64(locations->Out(), locations->InAt(0));
1735 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001736 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001737 if (instr->IsShl()) {
1738 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1739 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1740 __ Sll(dst_low, lhs_low, shift_value);
1741 } else if (instr->IsShr()) {
1742 __ Srl(dst_low, lhs_low, shift_value);
1743 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1744 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001745 } else if (instr->IsUShr()) {
1746 __ Srl(dst_low, lhs_low, shift_value);
1747 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1748 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001749 } else {
1750 __ Srl(dst_low, lhs_low, shift_value);
1751 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1752 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001753 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001754 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001755 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001756 if (instr->IsShl()) {
1757 __ Sll(dst_low, lhs_low, shift_value);
1758 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1759 __ Sll(dst_high, lhs_high, shift_value);
1760 __ Or(dst_high, dst_high, TMP);
1761 } else if (instr->IsShr()) {
1762 __ Sra(dst_high, lhs_high, shift_value);
1763 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1764 __ Srl(dst_low, lhs_low, shift_value);
1765 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001766 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001767 __ Srl(dst_high, lhs_high, shift_value);
1768 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1769 __ Srl(dst_low, lhs_low, shift_value);
1770 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001771 } else {
1772 __ Srl(TMP, lhs_low, shift_value);
1773 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1774 __ Or(dst_low, dst_low, TMP);
1775 __ Srl(TMP, lhs_high, shift_value);
1776 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1777 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001778 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001779 }
1780 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001781 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001782 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001783 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001784 __ Move(dst_low, ZERO);
1785 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001786 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001787 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001788 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001789 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001790 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001791 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001792 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001793 // 64-bit rotation by 32 is just a swap.
1794 __ Move(dst_low, lhs_high);
1795 __ Move(dst_high, lhs_low);
1796 } else {
1797 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001798 __ Srl(dst_low, lhs_high, shift_value_high);
1799 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1800 __ Srl(dst_high, lhs_low, shift_value_high);
1801 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001802 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001803 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1804 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001805 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001806 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1807 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001808 __ Or(dst_high, dst_high, TMP);
1809 }
1810 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001811 }
1812 }
1813 } else {
1814 MipsLabel done;
1815 if (instr->IsShl()) {
1816 __ Sllv(dst_low, lhs_low, rhs_reg);
1817 __ Nor(AT, ZERO, rhs_reg);
1818 __ Srl(TMP, lhs_low, 1);
1819 __ Srlv(TMP, TMP, AT);
1820 __ Sllv(dst_high, lhs_high, rhs_reg);
1821 __ Or(dst_high, dst_high, TMP);
1822 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1823 __ Beqz(TMP, &done);
1824 __ Move(dst_high, dst_low);
1825 __ Move(dst_low, ZERO);
1826 } else if (instr->IsShr()) {
1827 __ Srav(dst_high, lhs_high, rhs_reg);
1828 __ Nor(AT, ZERO, rhs_reg);
1829 __ Sll(TMP, lhs_high, 1);
1830 __ Sllv(TMP, TMP, AT);
1831 __ Srlv(dst_low, lhs_low, rhs_reg);
1832 __ Or(dst_low, dst_low, TMP);
1833 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1834 __ Beqz(TMP, &done);
1835 __ Move(dst_low, dst_high);
1836 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001837 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001838 __ Srlv(dst_high, lhs_high, rhs_reg);
1839 __ Nor(AT, ZERO, rhs_reg);
1840 __ Sll(TMP, lhs_high, 1);
1841 __ Sllv(TMP, TMP, AT);
1842 __ Srlv(dst_low, lhs_low, rhs_reg);
1843 __ Or(dst_low, dst_low, TMP);
1844 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1845 __ Beqz(TMP, &done);
1846 __ Move(dst_low, dst_high);
1847 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001848 } else {
1849 __ Nor(AT, ZERO, rhs_reg);
1850 __ Srlv(TMP, lhs_low, rhs_reg);
1851 __ Sll(dst_low, lhs_high, 1);
1852 __ Sllv(dst_low, dst_low, AT);
1853 __ Or(dst_low, dst_low, TMP);
1854 __ Srlv(TMP, lhs_high, rhs_reg);
1855 __ Sll(dst_high, lhs_low, 1);
1856 __ Sllv(dst_high, dst_high, AT);
1857 __ Or(dst_high, dst_high, TMP);
1858 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1859 __ Beqz(TMP, &done);
1860 __ Move(TMP, dst_high);
1861 __ Move(dst_high, dst_low);
1862 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 }
1864 __ Bind(&done);
1865 }
1866 break;
1867 }
1868
1869 default:
1870 LOG(FATAL) << "Unexpected shift operation type " << type;
1871 }
1872}
1873
1874void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1875 HandleBinaryOp(instruction);
1876}
1877
1878void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1879 HandleBinaryOp(instruction);
1880}
1881
1882void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1883 HandleBinaryOp(instruction);
1884}
1885
1886void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1887 HandleBinaryOp(instruction);
1888}
1889
1890void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1891 LocationSummary* locations =
1892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1893 locations->SetInAt(0, Location::RequiresRegister());
1894 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1895 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1896 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1897 } else {
1898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1899 }
1900}
1901
Alexey Frunze2923db72016-08-20 01:55:47 -07001902auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1903 auto null_checker = [this, instruction]() {
1904 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1905 };
1906 return null_checker;
1907}
1908
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1910 LocationSummary* locations = instruction->GetLocations();
1911 Register obj = locations->InAt(0).AsRegister<Register>();
1912 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001913 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001914 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001916 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001917 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
1918 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 switch (type) {
1920 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 Register out = locations->Out().AsRegister<Register>();
1922 if (index.IsConstant()) {
1923 size_t offset =
1924 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001925 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001926 } else {
1927 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001928 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 }
1930 break;
1931 }
1932
1933 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 Register out = locations->Out().AsRegister<Register>();
1935 if (index.IsConstant()) {
1936 size_t offset =
1937 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001938 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 } else {
1940 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001941 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 }
1943 break;
1944 }
1945
1946 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 Register out = locations->Out().AsRegister<Register>();
1948 if (index.IsConstant()) {
1949 size_t offset =
1950 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001951 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 } else {
1953 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1954 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001955 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 }
1957 break;
1958 }
1959
1960 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001962 if (maybe_compressed_char_at) {
1963 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1964 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
1965 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
1966 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1967 "Expecting 0=compressed, 1=uncompressed");
1968 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001969 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001970 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
1971 if (maybe_compressed_char_at) {
1972 MipsLabel uncompressed_load, done;
1973 __ Bnez(TMP, &uncompressed_load);
1974 __ LoadFromOffset(kLoadUnsignedByte,
1975 out,
1976 obj,
1977 data_offset + (const_index << TIMES_1));
1978 __ B(&done);
1979 __ Bind(&uncompressed_load);
1980 __ LoadFromOffset(kLoadUnsignedHalfword,
1981 out,
1982 obj,
1983 data_offset + (const_index << TIMES_2));
1984 __ Bind(&done);
1985 } else {
1986 __ LoadFromOffset(kLoadUnsignedHalfword,
1987 out,
1988 obj,
1989 data_offset + (const_index << TIMES_2),
1990 null_checker);
1991 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001992 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001993 Register index_reg = index.AsRegister<Register>();
1994 if (maybe_compressed_char_at) {
1995 MipsLabel uncompressed_load, done;
1996 __ Bnez(TMP, &uncompressed_load);
1997 __ Addu(TMP, obj, index_reg);
1998 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1999 __ B(&done);
2000 __ Bind(&uncompressed_load);
2001 __ Sll(TMP, index_reg, TIMES_2);
2002 __ Addu(TMP, obj, TMP);
2003 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2004 __ Bind(&done);
2005 } else {
2006 __ Sll(TMP, index_reg, TIMES_2);
2007 __ Addu(TMP, obj, TMP);
2008 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2009 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 }
2011 break;
2012 }
2013
2014 case Primitive::kPrimInt:
2015 case Primitive::kPrimNot: {
2016 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 Register out = locations->Out().AsRegister<Register>();
2018 if (index.IsConstant()) {
2019 size_t offset =
2020 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002021 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002022 } else {
2023 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2024 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002025 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 }
2027 break;
2028 }
2029
2030 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031 Register out = locations->Out().AsRegisterPairLow<Register>();
2032 if (index.IsConstant()) {
2033 size_t offset =
2034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002035 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036 } else {
2037 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2038 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 }
2041 break;
2042 }
2043
2044 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002045 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2046 if (index.IsConstant()) {
2047 size_t offset =
2048 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002049 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 } else {
2051 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2052 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002053 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002054 }
2055 break;
2056 }
2057
2058 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 FRegister out = locations->Out().AsFpuRegister<FRegister>();
2060 if (index.IsConstant()) {
2061 size_t offset =
2062 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002063 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 } else {
2065 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2066 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002067 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002068 }
2069 break;
2070 }
2071
2072 case Primitive::kPrimVoid:
2073 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2074 UNREACHABLE();
2075 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076}
2077
2078void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2080 locations->SetInAt(0, Location::RequiresRegister());
2081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2082}
2083
2084void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2085 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002086 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 Register obj = locations->InAt(0).AsRegister<Register>();
2088 Register out = locations->Out().AsRegister<Register>();
2089 __ LoadFromOffset(kLoadWord, out, obj, offset);
2090 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002091 // Mask out compression flag from String's array length.
2092 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2093 __ Srl(out, out, 1u);
2094 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002095}
2096
Alexey Frunzef58b2482016-09-02 22:14:06 -07002097Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2098 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2099 ? Location::ConstantLocation(instruction->AsConstant())
2100 : Location::RequiresRegister();
2101}
2102
2103Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2104 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2105 // We can store a non-zero float or double constant without first loading it into the FPU,
2106 // but we should only prefer this if the constant has a single use.
2107 if (instruction->IsConstant() &&
2108 (instruction->AsConstant()->IsZeroBitPattern() ||
2109 instruction->GetUses().HasExactlyOneElement())) {
2110 return Location::ConstantLocation(instruction->AsConstant());
2111 // Otherwise fall through and require an FPU register for the constant.
2112 }
2113 return Location::RequiresFpuRegister();
2114}
2115
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002116void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002117 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2119 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002120 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002121 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 InvokeRuntimeCallingConvention calling_convention;
2123 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2124 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2125 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2126 } else {
2127 locations->SetInAt(0, Location::RequiresRegister());
2128 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2129 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002130 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002131 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002132 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002133 }
2134 }
2135}
2136
2137void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2138 LocationSummary* locations = instruction->GetLocations();
2139 Register obj = locations->InAt(0).AsRegister<Register>();
2140 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002141 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 Primitive::Type value_type = instruction->GetComponentType();
2143 bool needs_runtime_call = locations->WillCall();
2144 bool needs_write_barrier =
2145 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002146 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002147 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148
2149 switch (value_type) {
2150 case Primitive::kPrimBoolean:
2151 case Primitive::kPrimByte: {
2152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002153 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002154 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002156 __ Addu(base_reg, obj, index.AsRegister<Register>());
2157 }
2158 if (value_location.IsConstant()) {
2159 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2160 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2161 } else {
2162 Register value = value_location.AsRegister<Register>();
2163 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002164 }
2165 break;
2166 }
2167
2168 case Primitive::kPrimShort:
2169 case Primitive::kPrimChar: {
2170 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002171 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002172 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002173 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002174 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2175 __ Addu(base_reg, obj, base_reg);
2176 }
2177 if (value_location.IsConstant()) {
2178 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2179 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2180 } else {
2181 Register value = value_location.AsRegister<Register>();
2182 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002183 }
2184 break;
2185 }
2186
2187 case Primitive::kPrimInt:
2188 case Primitive::kPrimNot: {
2189 if (!needs_runtime_call) {
2190 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002191 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002192 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002193 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002194 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2195 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002196 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002197 if (value_location.IsConstant()) {
2198 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2199 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2200 DCHECK(!needs_write_barrier);
2201 } else {
2202 Register value = value_location.AsRegister<Register>();
2203 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2204 if (needs_write_barrier) {
2205 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevice114da22016-12-26 14:21:43 +01002206 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunzef58b2482016-09-02 22:14:06 -07002207 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002208 }
2209 } else {
2210 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002211 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002212 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2213 }
2214 break;
2215 }
2216
2217 case Primitive::kPrimLong: {
2218 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002219 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002220 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002221 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002222 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2223 __ Addu(base_reg, obj, base_reg);
2224 }
2225 if (value_location.IsConstant()) {
2226 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2227 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2228 } else {
2229 Register value = value_location.AsRegisterPairLow<Register>();
2230 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002231 }
2232 break;
2233 }
2234
2235 case Primitive::kPrimFloat: {
2236 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002237 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002238 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002239 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002240 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2241 __ Addu(base_reg, obj, base_reg);
2242 }
2243 if (value_location.IsConstant()) {
2244 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2245 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2246 } else {
2247 FRegister value = value_location.AsFpuRegister<FRegister>();
2248 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002249 }
2250 break;
2251 }
2252
2253 case Primitive::kPrimDouble: {
2254 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002255 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002256 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002257 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002258 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2259 __ Addu(base_reg, obj, base_reg);
2260 }
2261 if (value_location.IsConstant()) {
2262 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2263 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2264 } else {
2265 FRegister value = value_location.AsFpuRegister<FRegister>();
2266 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267 }
2268 break;
2269 }
2270
2271 case Primitive::kPrimVoid:
2272 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2273 UNREACHABLE();
2274 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275}
2276
2277void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002278 RegisterSet caller_saves = RegisterSet::Empty();
2279 InvokeRuntimeCallingConvention calling_convention;
2280 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2281 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2282 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285}
2286
2287void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2288 LocationSummary* locations = instruction->GetLocations();
2289 BoundsCheckSlowPathMIPS* slow_path =
2290 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2291 codegen_->AddSlowPath(slow_path);
2292
2293 Register index = locations->InAt(0).AsRegister<Register>();
2294 Register length = locations->InAt(1).AsRegister<Register>();
2295
2296 // length is limited by the maximum positive signed 32-bit integer.
2297 // Unsigned comparison of length and index checks for index < 0
2298 // and for length <= index simultaneously.
2299 __ Bgeu(index, length, slow_path->GetEntryLabel());
2300}
2301
2302void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2304 instruction,
2305 LocationSummary::kCallOnSlowPath);
2306 locations->SetInAt(0, Location::RequiresRegister());
2307 locations->SetInAt(1, Location::RequiresRegister());
2308 // Note that TypeCheckSlowPathMIPS uses this register too.
2309 locations->AddTemp(Location::RequiresRegister());
2310}
2311
2312void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2313 LocationSummary* locations = instruction->GetLocations();
2314 Register obj = locations->InAt(0).AsRegister<Register>();
2315 Register cls = locations->InAt(1).AsRegister<Register>();
2316 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2317
2318 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2319 codegen_->AddSlowPath(slow_path);
2320
2321 // TODO: avoid this check if we know obj is not null.
2322 __ Beqz(obj, slow_path->GetExitLabel());
2323 // Compare the class of `obj` with `cls`.
2324 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2325 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2326 __ Bind(slow_path->GetExitLabel());
2327}
2328
2329void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2330 LocationSummary* locations =
2331 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2332 locations->SetInAt(0, Location::RequiresRegister());
2333 if (check->HasUses()) {
2334 locations->SetOut(Location::SameAsFirstInput());
2335 }
2336}
2337
2338void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2339 // We assume the class is not null.
2340 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2341 check->GetLoadClass(),
2342 check,
2343 check->GetDexPc(),
2344 true);
2345 codegen_->AddSlowPath(slow_path);
2346 GenerateClassInitializationCheck(slow_path,
2347 check->GetLocations()->InAt(0).AsRegister<Register>());
2348}
2349
2350void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2351 Primitive::Type in_type = compare->InputAt(0)->GetType();
2352
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002353 LocationSummary* locations =
2354 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355
2356 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002357 case Primitive::kPrimBoolean:
2358 case Primitive::kPrimByte:
2359 case Primitive::kPrimShort:
2360 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002361 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002362 locations->SetInAt(0, Location::RequiresRegister());
2363 locations->SetInAt(1, Location::RequiresRegister());
2364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2365 break;
2366
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002367 case Primitive::kPrimLong:
2368 locations->SetInAt(0, Location::RequiresRegister());
2369 locations->SetInAt(1, Location::RequiresRegister());
2370 // Output overlaps because it is written before doing the low comparison.
2371 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2372 break;
2373
2374 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 case Primitive::kPrimDouble:
2376 locations->SetInAt(0, Location::RequiresFpuRegister());
2377 locations->SetInAt(1, Location::RequiresFpuRegister());
2378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002379 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002380
2381 default:
2382 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2383 }
2384}
2385
2386void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2387 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002388 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002390 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002391
2392 // 0 if: left == right
2393 // 1 if: left > right
2394 // -1 if: left < right
2395 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002396 case Primitive::kPrimBoolean:
2397 case Primitive::kPrimByte:
2398 case Primitive::kPrimShort:
2399 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002400 case Primitive::kPrimInt: {
2401 Register lhs = locations->InAt(0).AsRegister<Register>();
2402 Register rhs = locations->InAt(1).AsRegister<Register>();
2403 __ Slt(TMP, lhs, rhs);
2404 __ Slt(res, rhs, lhs);
2405 __ Subu(res, res, TMP);
2406 break;
2407 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408 case Primitive::kPrimLong: {
2409 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002410 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2411 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2412 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2413 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2414 // TODO: more efficient (direct) comparison with a constant.
2415 __ Slt(TMP, lhs_high, rhs_high);
2416 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2417 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2418 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2419 __ Sltu(TMP, lhs_low, rhs_low);
2420 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2421 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2422 __ Bind(&done);
2423 break;
2424 }
2425
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002426 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002427 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002428 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2429 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2430 MipsLabel done;
2431 if (isR6) {
2432 __ CmpEqS(FTMP, lhs, rhs);
2433 __ LoadConst32(res, 0);
2434 __ Bc1nez(FTMP, &done);
2435 if (gt_bias) {
2436 __ CmpLtS(FTMP, lhs, rhs);
2437 __ LoadConst32(res, -1);
2438 __ Bc1nez(FTMP, &done);
2439 __ LoadConst32(res, 1);
2440 } else {
2441 __ CmpLtS(FTMP, rhs, lhs);
2442 __ LoadConst32(res, 1);
2443 __ Bc1nez(FTMP, &done);
2444 __ LoadConst32(res, -1);
2445 }
2446 } else {
2447 if (gt_bias) {
2448 __ ColtS(0, lhs, rhs);
2449 __ LoadConst32(res, -1);
2450 __ Bc1t(0, &done);
2451 __ CeqS(0, lhs, rhs);
2452 __ LoadConst32(res, 1);
2453 __ Movt(res, ZERO, 0);
2454 } else {
2455 __ ColtS(0, rhs, lhs);
2456 __ LoadConst32(res, 1);
2457 __ Bc1t(0, &done);
2458 __ CeqS(0, lhs, rhs);
2459 __ LoadConst32(res, -1);
2460 __ Movt(res, ZERO, 0);
2461 }
2462 }
2463 __ Bind(&done);
2464 break;
2465 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002467 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002468 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2469 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2470 MipsLabel done;
2471 if (isR6) {
2472 __ CmpEqD(FTMP, lhs, rhs);
2473 __ LoadConst32(res, 0);
2474 __ Bc1nez(FTMP, &done);
2475 if (gt_bias) {
2476 __ CmpLtD(FTMP, lhs, rhs);
2477 __ LoadConst32(res, -1);
2478 __ Bc1nez(FTMP, &done);
2479 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002480 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002481 __ CmpLtD(FTMP, rhs, lhs);
2482 __ LoadConst32(res, 1);
2483 __ Bc1nez(FTMP, &done);
2484 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002485 }
2486 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002487 if (gt_bias) {
2488 __ ColtD(0, lhs, rhs);
2489 __ LoadConst32(res, -1);
2490 __ Bc1t(0, &done);
2491 __ CeqD(0, lhs, rhs);
2492 __ LoadConst32(res, 1);
2493 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002494 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002495 __ ColtD(0, rhs, lhs);
2496 __ LoadConst32(res, 1);
2497 __ Bc1t(0, &done);
2498 __ CeqD(0, lhs, rhs);
2499 __ LoadConst32(res, -1);
2500 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002501 }
2502 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002503 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002504 break;
2505 }
2506
2507 default:
2508 LOG(FATAL) << "Unimplemented compare type " << in_type;
2509 }
2510}
2511
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002512void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002514 switch (instruction->InputAt(0)->GetType()) {
2515 default:
2516 case Primitive::kPrimLong:
2517 locations->SetInAt(0, Location::RequiresRegister());
2518 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2519 break;
2520
2521 case Primitive::kPrimFloat:
2522 case Primitive::kPrimDouble:
2523 locations->SetInAt(0, Location::RequiresFpuRegister());
2524 locations->SetInAt(1, Location::RequiresFpuRegister());
2525 break;
2526 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002527 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002528 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2529 }
2530}
2531
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002532void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002533 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002534 return;
2535 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002536
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002537 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002538 LocationSummary* locations = instruction->GetLocations();
2539 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002540 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002541
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002542 switch (type) {
2543 default:
2544 // Integer case.
2545 GenerateIntCompare(instruction->GetCondition(), locations);
2546 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002547
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002548 case Primitive::kPrimLong:
2549 // TODO: don't use branches.
2550 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551 break;
2552
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002553 case Primitive::kPrimFloat:
2554 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002555 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2556 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002557 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002558
2559 // Convert the branches into the result.
2560 MipsLabel done;
2561
2562 // False case: result = 0.
2563 __ LoadConst32(dst, 0);
2564 __ B(&done);
2565
2566 // True case: result = 1.
2567 __ Bind(&true_label);
2568 __ LoadConst32(dst, 1);
2569 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002570}
2571
Alexey Frunze7e99e052015-11-24 19:28:01 -08002572void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2573 DCHECK(instruction->IsDiv() || instruction->IsRem());
2574 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2575
2576 LocationSummary* locations = instruction->GetLocations();
2577 Location second = locations->InAt(1);
2578 DCHECK(second.IsConstant());
2579
2580 Register out = locations->Out().AsRegister<Register>();
2581 Register dividend = locations->InAt(0).AsRegister<Register>();
2582 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2583 DCHECK(imm == 1 || imm == -1);
2584
2585 if (instruction->IsRem()) {
2586 __ Move(out, ZERO);
2587 } else {
2588 if (imm == -1) {
2589 __ Subu(out, ZERO, dividend);
2590 } else if (out != dividend) {
2591 __ Move(out, dividend);
2592 }
2593 }
2594}
2595
2596void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2597 DCHECK(instruction->IsDiv() || instruction->IsRem());
2598 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2599
2600 LocationSummary* locations = instruction->GetLocations();
2601 Location second = locations->InAt(1);
2602 DCHECK(second.IsConstant());
2603
2604 Register out = locations->Out().AsRegister<Register>();
2605 Register dividend = locations->InAt(0).AsRegister<Register>();
2606 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002607 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002608 int ctz_imm = CTZ(abs_imm);
2609
2610 if (instruction->IsDiv()) {
2611 if (ctz_imm == 1) {
2612 // Fast path for division by +/-2, which is very common.
2613 __ Srl(TMP, dividend, 31);
2614 } else {
2615 __ Sra(TMP, dividend, 31);
2616 __ Srl(TMP, TMP, 32 - ctz_imm);
2617 }
2618 __ Addu(out, dividend, TMP);
2619 __ Sra(out, out, ctz_imm);
2620 if (imm < 0) {
2621 __ Subu(out, ZERO, out);
2622 }
2623 } else {
2624 if (ctz_imm == 1) {
2625 // Fast path for modulo +/-2, which is very common.
2626 __ Sra(TMP, dividend, 31);
2627 __ Subu(out, dividend, TMP);
2628 __ Andi(out, out, 1);
2629 __ Addu(out, out, TMP);
2630 } else {
2631 __ Sra(TMP, dividend, 31);
2632 __ Srl(TMP, TMP, 32 - ctz_imm);
2633 __ Addu(out, dividend, TMP);
2634 if (IsUint<16>(abs_imm - 1)) {
2635 __ Andi(out, out, abs_imm - 1);
2636 } else {
2637 __ Sll(out, out, 32 - ctz_imm);
2638 __ Srl(out, out, 32 - ctz_imm);
2639 }
2640 __ Subu(out, out, TMP);
2641 }
2642 }
2643}
2644
2645void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2646 DCHECK(instruction->IsDiv() || instruction->IsRem());
2647 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2648
2649 LocationSummary* locations = instruction->GetLocations();
2650 Location second = locations->InAt(1);
2651 DCHECK(second.IsConstant());
2652
2653 Register out = locations->Out().AsRegister<Register>();
2654 Register dividend = locations->InAt(0).AsRegister<Register>();
2655 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2656
2657 int64_t magic;
2658 int shift;
2659 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2660
2661 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2662
2663 __ LoadConst32(TMP, magic);
2664 if (isR6) {
2665 __ MuhR6(TMP, dividend, TMP);
2666 } else {
2667 __ MultR2(dividend, TMP);
2668 __ Mfhi(TMP);
2669 }
2670 if (imm > 0 && magic < 0) {
2671 __ Addu(TMP, TMP, dividend);
2672 } else if (imm < 0 && magic > 0) {
2673 __ Subu(TMP, TMP, dividend);
2674 }
2675
2676 if (shift != 0) {
2677 __ Sra(TMP, TMP, shift);
2678 }
2679
2680 if (instruction->IsDiv()) {
2681 __ Sra(out, TMP, 31);
2682 __ Subu(out, TMP, out);
2683 } else {
2684 __ Sra(AT, TMP, 31);
2685 __ Subu(AT, TMP, AT);
2686 __ LoadConst32(TMP, imm);
2687 if (isR6) {
2688 __ MulR6(TMP, AT, TMP);
2689 } else {
2690 __ MulR2(TMP, AT, TMP);
2691 }
2692 __ Subu(out, dividend, TMP);
2693 }
2694}
2695
2696void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2697 DCHECK(instruction->IsDiv() || instruction->IsRem());
2698 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2699
2700 LocationSummary* locations = instruction->GetLocations();
2701 Register out = locations->Out().AsRegister<Register>();
2702 Location second = locations->InAt(1);
2703
2704 if (second.IsConstant()) {
2705 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2706 if (imm == 0) {
2707 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2708 } else if (imm == 1 || imm == -1) {
2709 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002710 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002711 DivRemByPowerOfTwo(instruction);
2712 } else {
2713 DCHECK(imm <= -2 || imm >= 2);
2714 GenerateDivRemWithAnyConstant(instruction);
2715 }
2716 } else {
2717 Register dividend = locations->InAt(0).AsRegister<Register>();
2718 Register divisor = second.AsRegister<Register>();
2719 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2720 if (instruction->IsDiv()) {
2721 if (isR6) {
2722 __ DivR6(out, dividend, divisor);
2723 } else {
2724 __ DivR2(out, dividend, divisor);
2725 }
2726 } else {
2727 if (isR6) {
2728 __ ModR6(out, dividend, divisor);
2729 } else {
2730 __ ModR2(out, dividend, divisor);
2731 }
2732 }
2733 }
2734}
2735
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002736void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2737 Primitive::Type type = div->GetResultType();
2738 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002739 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002740 : LocationSummary::kNoCall;
2741
2742 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2743
2744 switch (type) {
2745 case Primitive::kPrimInt:
2746 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002747 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2749 break;
2750
2751 case Primitive::kPrimLong: {
2752 InvokeRuntimeCallingConvention calling_convention;
2753 locations->SetInAt(0, Location::RegisterPairLocation(
2754 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2755 locations->SetInAt(1, Location::RegisterPairLocation(
2756 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2757 locations->SetOut(calling_convention.GetReturnLocation(type));
2758 break;
2759 }
2760
2761 case Primitive::kPrimFloat:
2762 case Primitive::kPrimDouble:
2763 locations->SetInAt(0, Location::RequiresFpuRegister());
2764 locations->SetInAt(1, Location::RequiresFpuRegister());
2765 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2766 break;
2767
2768 default:
2769 LOG(FATAL) << "Unexpected div type " << type;
2770 }
2771}
2772
2773void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2774 Primitive::Type type = instruction->GetType();
2775 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002776
2777 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002778 case Primitive::kPrimInt:
2779 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002780 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002782 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002783 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2784 break;
2785 }
2786 case Primitive::kPrimFloat:
2787 case Primitive::kPrimDouble: {
2788 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2789 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2790 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2791 if (type == Primitive::kPrimFloat) {
2792 __ DivS(dst, lhs, rhs);
2793 } else {
2794 __ DivD(dst, lhs, rhs);
2795 }
2796 break;
2797 }
2798 default:
2799 LOG(FATAL) << "Unexpected div type " << type;
2800 }
2801}
2802
2803void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002804 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002805 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002806}
2807
2808void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2809 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2810 codegen_->AddSlowPath(slow_path);
2811 Location value = instruction->GetLocations()->InAt(0);
2812 Primitive::Type type = instruction->GetType();
2813
2814 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002815 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002816 case Primitive::kPrimByte:
2817 case Primitive::kPrimChar:
2818 case Primitive::kPrimShort:
2819 case Primitive::kPrimInt: {
2820 if (value.IsConstant()) {
2821 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2822 __ B(slow_path->GetEntryLabel());
2823 } else {
2824 // A division by a non-null constant is valid. We don't need to perform
2825 // any check, so simply fall through.
2826 }
2827 } else {
2828 DCHECK(value.IsRegister()) << value;
2829 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2830 }
2831 break;
2832 }
2833 case Primitive::kPrimLong: {
2834 if (value.IsConstant()) {
2835 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2836 __ B(slow_path->GetEntryLabel());
2837 } else {
2838 // A division by a non-null constant is valid. We don't need to perform
2839 // any check, so simply fall through.
2840 }
2841 } else {
2842 DCHECK(value.IsRegisterPair()) << value;
2843 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2844 __ Beqz(TMP, slow_path->GetEntryLabel());
2845 }
2846 break;
2847 }
2848 default:
2849 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2850 }
2851}
2852
2853void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2854 LocationSummary* locations =
2855 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2856 locations->SetOut(Location::ConstantLocation(constant));
2857}
2858
2859void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2860 // Will be generated at use site.
2861}
2862
2863void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2864 exit->SetLocations(nullptr);
2865}
2866
2867void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2868}
2869
2870void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2871 LocationSummary* locations =
2872 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2873 locations->SetOut(Location::ConstantLocation(constant));
2874}
2875
2876void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2877 // Will be generated at use site.
2878}
2879
2880void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2881 got->SetLocations(nullptr);
2882}
2883
2884void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2885 DCHECK(!successor->IsExitBlock());
2886 HBasicBlock* block = got->GetBlock();
2887 HInstruction* previous = got->GetPrevious();
2888 HLoopInformation* info = block->GetLoopInformation();
2889
2890 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2891 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2892 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2893 return;
2894 }
2895 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2896 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2897 }
2898 if (!codegen_->GoesToNextBlock(block, successor)) {
2899 __ B(codegen_->GetLabelOf(successor));
2900 }
2901}
2902
2903void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2904 HandleGoto(got, got->GetSuccessor());
2905}
2906
2907void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2908 try_boundary->SetLocations(nullptr);
2909}
2910
2911void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2912 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2913 if (!successor->IsExitBlock()) {
2914 HandleGoto(try_boundary, successor);
2915 }
2916}
2917
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002918void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2919 LocationSummary* locations) {
2920 Register dst = locations->Out().AsRegister<Register>();
2921 Register lhs = locations->InAt(0).AsRegister<Register>();
2922 Location rhs_location = locations->InAt(1);
2923 Register rhs_reg = ZERO;
2924 int64_t rhs_imm = 0;
2925 bool use_imm = rhs_location.IsConstant();
2926 if (use_imm) {
2927 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2928 } else {
2929 rhs_reg = rhs_location.AsRegister<Register>();
2930 }
2931
2932 switch (cond) {
2933 case kCondEQ:
2934 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002935 if (use_imm && IsInt<16>(-rhs_imm)) {
2936 if (rhs_imm == 0) {
2937 if (cond == kCondEQ) {
2938 __ Sltiu(dst, lhs, 1);
2939 } else {
2940 __ Sltu(dst, ZERO, lhs);
2941 }
2942 } else {
2943 __ Addiu(dst, lhs, -rhs_imm);
2944 if (cond == kCondEQ) {
2945 __ Sltiu(dst, dst, 1);
2946 } else {
2947 __ Sltu(dst, ZERO, dst);
2948 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002949 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002950 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002951 if (use_imm && IsUint<16>(rhs_imm)) {
2952 __ Xori(dst, lhs, rhs_imm);
2953 } else {
2954 if (use_imm) {
2955 rhs_reg = TMP;
2956 __ LoadConst32(rhs_reg, rhs_imm);
2957 }
2958 __ Xor(dst, lhs, rhs_reg);
2959 }
2960 if (cond == kCondEQ) {
2961 __ Sltiu(dst, dst, 1);
2962 } else {
2963 __ Sltu(dst, ZERO, dst);
2964 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002965 }
2966 break;
2967
2968 case kCondLT:
2969 case kCondGE:
2970 if (use_imm && IsInt<16>(rhs_imm)) {
2971 __ Slti(dst, lhs, rhs_imm);
2972 } else {
2973 if (use_imm) {
2974 rhs_reg = TMP;
2975 __ LoadConst32(rhs_reg, rhs_imm);
2976 }
2977 __ Slt(dst, lhs, rhs_reg);
2978 }
2979 if (cond == kCondGE) {
2980 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2981 // only the slt instruction but no sge.
2982 __ Xori(dst, dst, 1);
2983 }
2984 break;
2985
2986 case kCondLE:
2987 case kCondGT:
2988 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2989 // Simulate lhs <= rhs via lhs < rhs + 1.
2990 __ Slti(dst, lhs, rhs_imm + 1);
2991 if (cond == kCondGT) {
2992 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2993 // only the slti instruction but no sgti.
2994 __ Xori(dst, dst, 1);
2995 }
2996 } else {
2997 if (use_imm) {
2998 rhs_reg = TMP;
2999 __ LoadConst32(rhs_reg, rhs_imm);
3000 }
3001 __ Slt(dst, rhs_reg, lhs);
3002 if (cond == kCondLE) {
3003 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3004 // only the slt instruction but no sle.
3005 __ Xori(dst, dst, 1);
3006 }
3007 }
3008 break;
3009
3010 case kCondB:
3011 case kCondAE:
3012 if (use_imm && IsInt<16>(rhs_imm)) {
3013 // Sltiu sign-extends its 16-bit immediate operand before
3014 // the comparison and thus lets us compare directly with
3015 // unsigned values in the ranges [0, 0x7fff] and
3016 // [0xffff8000, 0xffffffff].
3017 __ Sltiu(dst, lhs, rhs_imm);
3018 } else {
3019 if (use_imm) {
3020 rhs_reg = TMP;
3021 __ LoadConst32(rhs_reg, rhs_imm);
3022 }
3023 __ Sltu(dst, lhs, rhs_reg);
3024 }
3025 if (cond == kCondAE) {
3026 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3027 // only the sltu instruction but no sgeu.
3028 __ Xori(dst, dst, 1);
3029 }
3030 break;
3031
3032 case kCondBE:
3033 case kCondA:
3034 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3035 // Simulate lhs <= rhs via lhs < rhs + 1.
3036 // Note that this only works if rhs + 1 does not overflow
3037 // to 0, hence the check above.
3038 // Sltiu sign-extends its 16-bit immediate operand before
3039 // the comparison and thus lets us compare directly with
3040 // unsigned values in the ranges [0, 0x7fff] and
3041 // [0xffff8000, 0xffffffff].
3042 __ Sltiu(dst, lhs, rhs_imm + 1);
3043 if (cond == kCondA) {
3044 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3045 // only the sltiu instruction but no sgtiu.
3046 __ Xori(dst, dst, 1);
3047 }
3048 } else {
3049 if (use_imm) {
3050 rhs_reg = TMP;
3051 __ LoadConst32(rhs_reg, rhs_imm);
3052 }
3053 __ Sltu(dst, rhs_reg, lhs);
3054 if (cond == kCondBE) {
3055 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3056 // only the sltu instruction but no sleu.
3057 __ Xori(dst, dst, 1);
3058 }
3059 }
3060 break;
3061 }
3062}
3063
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003064bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3065 LocationSummary* input_locations,
3066 Register dst) {
3067 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3068 Location rhs_location = input_locations->InAt(1);
3069 Register rhs_reg = ZERO;
3070 int64_t rhs_imm = 0;
3071 bool use_imm = rhs_location.IsConstant();
3072 if (use_imm) {
3073 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3074 } else {
3075 rhs_reg = rhs_location.AsRegister<Register>();
3076 }
3077
3078 switch (cond) {
3079 case kCondEQ:
3080 case kCondNE:
3081 if (use_imm && IsInt<16>(-rhs_imm)) {
3082 __ Addiu(dst, lhs, -rhs_imm);
3083 } else if (use_imm && IsUint<16>(rhs_imm)) {
3084 __ Xori(dst, lhs, rhs_imm);
3085 } else {
3086 if (use_imm) {
3087 rhs_reg = TMP;
3088 __ LoadConst32(rhs_reg, rhs_imm);
3089 }
3090 __ Xor(dst, lhs, rhs_reg);
3091 }
3092 return (cond == kCondEQ);
3093
3094 case kCondLT:
3095 case kCondGE:
3096 if (use_imm && IsInt<16>(rhs_imm)) {
3097 __ Slti(dst, lhs, rhs_imm);
3098 } else {
3099 if (use_imm) {
3100 rhs_reg = TMP;
3101 __ LoadConst32(rhs_reg, rhs_imm);
3102 }
3103 __ Slt(dst, lhs, rhs_reg);
3104 }
3105 return (cond == kCondGE);
3106
3107 case kCondLE:
3108 case kCondGT:
3109 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3110 // Simulate lhs <= rhs via lhs < rhs + 1.
3111 __ Slti(dst, lhs, rhs_imm + 1);
3112 return (cond == kCondGT);
3113 } else {
3114 if (use_imm) {
3115 rhs_reg = TMP;
3116 __ LoadConst32(rhs_reg, rhs_imm);
3117 }
3118 __ Slt(dst, rhs_reg, lhs);
3119 return (cond == kCondLE);
3120 }
3121
3122 case kCondB:
3123 case kCondAE:
3124 if (use_imm && IsInt<16>(rhs_imm)) {
3125 // Sltiu sign-extends its 16-bit immediate operand before
3126 // the comparison and thus lets us compare directly with
3127 // unsigned values in the ranges [0, 0x7fff] and
3128 // [0xffff8000, 0xffffffff].
3129 __ Sltiu(dst, lhs, rhs_imm);
3130 } else {
3131 if (use_imm) {
3132 rhs_reg = TMP;
3133 __ LoadConst32(rhs_reg, rhs_imm);
3134 }
3135 __ Sltu(dst, lhs, rhs_reg);
3136 }
3137 return (cond == kCondAE);
3138
3139 case kCondBE:
3140 case kCondA:
3141 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3142 // Simulate lhs <= rhs via lhs < rhs + 1.
3143 // Note that this only works if rhs + 1 does not overflow
3144 // to 0, hence the check above.
3145 // Sltiu sign-extends its 16-bit immediate operand before
3146 // the comparison and thus lets us compare directly with
3147 // unsigned values in the ranges [0, 0x7fff] and
3148 // [0xffff8000, 0xffffffff].
3149 __ Sltiu(dst, lhs, rhs_imm + 1);
3150 return (cond == kCondA);
3151 } else {
3152 if (use_imm) {
3153 rhs_reg = TMP;
3154 __ LoadConst32(rhs_reg, rhs_imm);
3155 }
3156 __ Sltu(dst, rhs_reg, lhs);
3157 return (cond == kCondBE);
3158 }
3159 }
3160}
3161
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003162void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3163 LocationSummary* locations,
3164 MipsLabel* label) {
3165 Register lhs = locations->InAt(0).AsRegister<Register>();
3166 Location rhs_location = locations->InAt(1);
3167 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003168 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003169 bool use_imm = rhs_location.IsConstant();
3170 if (use_imm) {
3171 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3172 } else {
3173 rhs_reg = rhs_location.AsRegister<Register>();
3174 }
3175
3176 if (use_imm && rhs_imm == 0) {
3177 switch (cond) {
3178 case kCondEQ:
3179 case kCondBE: // <= 0 if zero
3180 __ Beqz(lhs, label);
3181 break;
3182 case kCondNE:
3183 case kCondA: // > 0 if non-zero
3184 __ Bnez(lhs, label);
3185 break;
3186 case kCondLT:
3187 __ Bltz(lhs, label);
3188 break;
3189 case kCondGE:
3190 __ Bgez(lhs, label);
3191 break;
3192 case kCondLE:
3193 __ Blez(lhs, label);
3194 break;
3195 case kCondGT:
3196 __ Bgtz(lhs, label);
3197 break;
3198 case kCondB: // always false
3199 break;
3200 case kCondAE: // always true
3201 __ B(label);
3202 break;
3203 }
3204 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003205 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3206 if (isR6 || !use_imm) {
3207 if (use_imm) {
3208 rhs_reg = TMP;
3209 __ LoadConst32(rhs_reg, rhs_imm);
3210 }
3211 switch (cond) {
3212 case kCondEQ:
3213 __ Beq(lhs, rhs_reg, label);
3214 break;
3215 case kCondNE:
3216 __ Bne(lhs, rhs_reg, label);
3217 break;
3218 case kCondLT:
3219 __ Blt(lhs, rhs_reg, label);
3220 break;
3221 case kCondGE:
3222 __ Bge(lhs, rhs_reg, label);
3223 break;
3224 case kCondLE:
3225 __ Bge(rhs_reg, lhs, label);
3226 break;
3227 case kCondGT:
3228 __ Blt(rhs_reg, lhs, label);
3229 break;
3230 case kCondB:
3231 __ Bltu(lhs, rhs_reg, label);
3232 break;
3233 case kCondAE:
3234 __ Bgeu(lhs, rhs_reg, label);
3235 break;
3236 case kCondBE:
3237 __ Bgeu(rhs_reg, lhs, label);
3238 break;
3239 case kCondA:
3240 __ Bltu(rhs_reg, lhs, label);
3241 break;
3242 }
3243 } else {
3244 // Special cases for more efficient comparison with constants on R2.
3245 switch (cond) {
3246 case kCondEQ:
3247 __ LoadConst32(TMP, rhs_imm);
3248 __ Beq(lhs, TMP, label);
3249 break;
3250 case kCondNE:
3251 __ LoadConst32(TMP, rhs_imm);
3252 __ Bne(lhs, TMP, label);
3253 break;
3254 case kCondLT:
3255 if (IsInt<16>(rhs_imm)) {
3256 __ Slti(TMP, lhs, rhs_imm);
3257 __ Bnez(TMP, label);
3258 } else {
3259 __ LoadConst32(TMP, rhs_imm);
3260 __ Blt(lhs, TMP, label);
3261 }
3262 break;
3263 case kCondGE:
3264 if (IsInt<16>(rhs_imm)) {
3265 __ Slti(TMP, lhs, rhs_imm);
3266 __ Beqz(TMP, label);
3267 } else {
3268 __ LoadConst32(TMP, rhs_imm);
3269 __ Bge(lhs, TMP, label);
3270 }
3271 break;
3272 case kCondLE:
3273 if (IsInt<16>(rhs_imm + 1)) {
3274 // Simulate lhs <= rhs via lhs < rhs + 1.
3275 __ Slti(TMP, lhs, rhs_imm + 1);
3276 __ Bnez(TMP, label);
3277 } else {
3278 __ LoadConst32(TMP, rhs_imm);
3279 __ Bge(TMP, lhs, label);
3280 }
3281 break;
3282 case kCondGT:
3283 if (IsInt<16>(rhs_imm + 1)) {
3284 // Simulate lhs > rhs via !(lhs < rhs + 1).
3285 __ Slti(TMP, lhs, rhs_imm + 1);
3286 __ Beqz(TMP, label);
3287 } else {
3288 __ LoadConst32(TMP, rhs_imm);
3289 __ Blt(TMP, lhs, label);
3290 }
3291 break;
3292 case kCondB:
3293 if (IsInt<16>(rhs_imm)) {
3294 __ Sltiu(TMP, lhs, rhs_imm);
3295 __ Bnez(TMP, label);
3296 } else {
3297 __ LoadConst32(TMP, rhs_imm);
3298 __ Bltu(lhs, TMP, label);
3299 }
3300 break;
3301 case kCondAE:
3302 if (IsInt<16>(rhs_imm)) {
3303 __ Sltiu(TMP, lhs, rhs_imm);
3304 __ Beqz(TMP, label);
3305 } else {
3306 __ LoadConst32(TMP, rhs_imm);
3307 __ Bgeu(lhs, TMP, label);
3308 }
3309 break;
3310 case kCondBE:
3311 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3312 // Simulate lhs <= rhs via lhs < rhs + 1.
3313 // Note that this only works if rhs + 1 does not overflow
3314 // to 0, hence the check above.
3315 __ Sltiu(TMP, lhs, rhs_imm + 1);
3316 __ Bnez(TMP, label);
3317 } else {
3318 __ LoadConst32(TMP, rhs_imm);
3319 __ Bgeu(TMP, lhs, label);
3320 }
3321 break;
3322 case kCondA:
3323 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3324 // Simulate lhs > rhs via !(lhs < rhs + 1).
3325 // Note that this only works if rhs + 1 does not overflow
3326 // to 0, hence the check above.
3327 __ Sltiu(TMP, lhs, rhs_imm + 1);
3328 __ Beqz(TMP, label);
3329 } else {
3330 __ LoadConst32(TMP, rhs_imm);
3331 __ Bltu(TMP, lhs, label);
3332 }
3333 break;
3334 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003335 }
3336 }
3337}
3338
3339void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3340 LocationSummary* locations,
3341 MipsLabel* label) {
3342 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3343 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3344 Location rhs_location = locations->InAt(1);
3345 Register rhs_high = ZERO;
3346 Register rhs_low = ZERO;
3347 int64_t imm = 0;
3348 uint32_t imm_high = 0;
3349 uint32_t imm_low = 0;
3350 bool use_imm = rhs_location.IsConstant();
3351 if (use_imm) {
3352 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3353 imm_high = High32Bits(imm);
3354 imm_low = Low32Bits(imm);
3355 } else {
3356 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3357 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3358 }
3359
3360 if (use_imm && imm == 0) {
3361 switch (cond) {
3362 case kCondEQ:
3363 case kCondBE: // <= 0 if zero
3364 __ Or(TMP, lhs_high, lhs_low);
3365 __ Beqz(TMP, label);
3366 break;
3367 case kCondNE:
3368 case kCondA: // > 0 if non-zero
3369 __ Or(TMP, lhs_high, lhs_low);
3370 __ Bnez(TMP, label);
3371 break;
3372 case kCondLT:
3373 __ Bltz(lhs_high, label);
3374 break;
3375 case kCondGE:
3376 __ Bgez(lhs_high, label);
3377 break;
3378 case kCondLE:
3379 __ Or(TMP, lhs_high, lhs_low);
3380 __ Sra(AT, lhs_high, 31);
3381 __ Bgeu(AT, TMP, label);
3382 break;
3383 case kCondGT:
3384 __ Or(TMP, lhs_high, lhs_low);
3385 __ Sra(AT, lhs_high, 31);
3386 __ Bltu(AT, TMP, label);
3387 break;
3388 case kCondB: // always false
3389 break;
3390 case kCondAE: // always true
3391 __ B(label);
3392 break;
3393 }
3394 } else if (use_imm) {
3395 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3396 switch (cond) {
3397 case kCondEQ:
3398 __ LoadConst32(TMP, imm_high);
3399 __ Xor(TMP, TMP, lhs_high);
3400 __ LoadConst32(AT, imm_low);
3401 __ Xor(AT, AT, lhs_low);
3402 __ Or(TMP, TMP, AT);
3403 __ Beqz(TMP, label);
3404 break;
3405 case kCondNE:
3406 __ LoadConst32(TMP, imm_high);
3407 __ Xor(TMP, TMP, lhs_high);
3408 __ LoadConst32(AT, imm_low);
3409 __ Xor(AT, AT, lhs_low);
3410 __ Or(TMP, TMP, AT);
3411 __ Bnez(TMP, label);
3412 break;
3413 case kCondLT:
3414 __ LoadConst32(TMP, imm_high);
3415 __ Blt(lhs_high, TMP, label);
3416 __ Slt(TMP, TMP, lhs_high);
3417 __ LoadConst32(AT, imm_low);
3418 __ Sltu(AT, lhs_low, AT);
3419 __ Blt(TMP, AT, label);
3420 break;
3421 case kCondGE:
3422 __ LoadConst32(TMP, imm_high);
3423 __ Blt(TMP, lhs_high, label);
3424 __ Slt(TMP, lhs_high, TMP);
3425 __ LoadConst32(AT, imm_low);
3426 __ Sltu(AT, lhs_low, AT);
3427 __ Or(TMP, TMP, AT);
3428 __ Beqz(TMP, label);
3429 break;
3430 case kCondLE:
3431 __ LoadConst32(TMP, imm_high);
3432 __ Blt(lhs_high, TMP, label);
3433 __ Slt(TMP, TMP, lhs_high);
3434 __ LoadConst32(AT, imm_low);
3435 __ Sltu(AT, AT, lhs_low);
3436 __ Or(TMP, TMP, AT);
3437 __ Beqz(TMP, label);
3438 break;
3439 case kCondGT:
3440 __ LoadConst32(TMP, imm_high);
3441 __ Blt(TMP, lhs_high, label);
3442 __ Slt(TMP, lhs_high, TMP);
3443 __ LoadConst32(AT, imm_low);
3444 __ Sltu(AT, AT, lhs_low);
3445 __ Blt(TMP, AT, label);
3446 break;
3447 case kCondB:
3448 __ LoadConst32(TMP, imm_high);
3449 __ Bltu(lhs_high, TMP, label);
3450 __ Sltu(TMP, TMP, lhs_high);
3451 __ LoadConst32(AT, imm_low);
3452 __ Sltu(AT, lhs_low, AT);
3453 __ Blt(TMP, AT, label);
3454 break;
3455 case kCondAE:
3456 __ LoadConst32(TMP, imm_high);
3457 __ Bltu(TMP, lhs_high, label);
3458 __ Sltu(TMP, lhs_high, TMP);
3459 __ LoadConst32(AT, imm_low);
3460 __ Sltu(AT, lhs_low, AT);
3461 __ Or(TMP, TMP, AT);
3462 __ Beqz(TMP, label);
3463 break;
3464 case kCondBE:
3465 __ LoadConst32(TMP, imm_high);
3466 __ Bltu(lhs_high, TMP, label);
3467 __ Sltu(TMP, TMP, lhs_high);
3468 __ LoadConst32(AT, imm_low);
3469 __ Sltu(AT, AT, lhs_low);
3470 __ Or(TMP, TMP, AT);
3471 __ Beqz(TMP, label);
3472 break;
3473 case kCondA:
3474 __ LoadConst32(TMP, imm_high);
3475 __ Bltu(TMP, lhs_high, label);
3476 __ Sltu(TMP, lhs_high, TMP);
3477 __ LoadConst32(AT, imm_low);
3478 __ Sltu(AT, AT, lhs_low);
3479 __ Blt(TMP, AT, label);
3480 break;
3481 }
3482 } else {
3483 switch (cond) {
3484 case kCondEQ:
3485 __ Xor(TMP, lhs_high, rhs_high);
3486 __ Xor(AT, lhs_low, rhs_low);
3487 __ Or(TMP, TMP, AT);
3488 __ Beqz(TMP, label);
3489 break;
3490 case kCondNE:
3491 __ Xor(TMP, lhs_high, rhs_high);
3492 __ Xor(AT, lhs_low, rhs_low);
3493 __ Or(TMP, TMP, AT);
3494 __ Bnez(TMP, label);
3495 break;
3496 case kCondLT:
3497 __ Blt(lhs_high, rhs_high, label);
3498 __ Slt(TMP, rhs_high, lhs_high);
3499 __ Sltu(AT, lhs_low, rhs_low);
3500 __ Blt(TMP, AT, label);
3501 break;
3502 case kCondGE:
3503 __ Blt(rhs_high, lhs_high, label);
3504 __ Slt(TMP, lhs_high, rhs_high);
3505 __ Sltu(AT, lhs_low, rhs_low);
3506 __ Or(TMP, TMP, AT);
3507 __ Beqz(TMP, label);
3508 break;
3509 case kCondLE:
3510 __ Blt(lhs_high, rhs_high, label);
3511 __ Slt(TMP, rhs_high, lhs_high);
3512 __ Sltu(AT, rhs_low, lhs_low);
3513 __ Or(TMP, TMP, AT);
3514 __ Beqz(TMP, label);
3515 break;
3516 case kCondGT:
3517 __ Blt(rhs_high, lhs_high, label);
3518 __ Slt(TMP, lhs_high, rhs_high);
3519 __ Sltu(AT, rhs_low, lhs_low);
3520 __ Blt(TMP, AT, label);
3521 break;
3522 case kCondB:
3523 __ Bltu(lhs_high, rhs_high, label);
3524 __ Sltu(TMP, rhs_high, lhs_high);
3525 __ Sltu(AT, lhs_low, rhs_low);
3526 __ Blt(TMP, AT, label);
3527 break;
3528 case kCondAE:
3529 __ Bltu(rhs_high, lhs_high, label);
3530 __ Sltu(TMP, lhs_high, rhs_high);
3531 __ Sltu(AT, lhs_low, rhs_low);
3532 __ Or(TMP, TMP, AT);
3533 __ Beqz(TMP, label);
3534 break;
3535 case kCondBE:
3536 __ Bltu(lhs_high, rhs_high, label);
3537 __ Sltu(TMP, rhs_high, lhs_high);
3538 __ Sltu(AT, rhs_low, lhs_low);
3539 __ Or(TMP, TMP, AT);
3540 __ Beqz(TMP, label);
3541 break;
3542 case kCondA:
3543 __ Bltu(rhs_high, lhs_high, label);
3544 __ Sltu(TMP, lhs_high, rhs_high);
3545 __ Sltu(AT, rhs_low, lhs_low);
3546 __ Blt(TMP, AT, label);
3547 break;
3548 }
3549 }
3550}
3551
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003552void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3553 bool gt_bias,
3554 Primitive::Type type,
3555 LocationSummary* locations) {
3556 Register dst = locations->Out().AsRegister<Register>();
3557 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3558 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3559 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3560 if (type == Primitive::kPrimFloat) {
3561 if (isR6) {
3562 switch (cond) {
3563 case kCondEQ:
3564 __ CmpEqS(FTMP, lhs, rhs);
3565 __ Mfc1(dst, FTMP);
3566 __ Andi(dst, dst, 1);
3567 break;
3568 case kCondNE:
3569 __ CmpEqS(FTMP, lhs, rhs);
3570 __ Mfc1(dst, FTMP);
3571 __ Addiu(dst, dst, 1);
3572 break;
3573 case kCondLT:
3574 if (gt_bias) {
3575 __ CmpLtS(FTMP, lhs, rhs);
3576 } else {
3577 __ CmpUltS(FTMP, lhs, rhs);
3578 }
3579 __ Mfc1(dst, FTMP);
3580 __ Andi(dst, dst, 1);
3581 break;
3582 case kCondLE:
3583 if (gt_bias) {
3584 __ CmpLeS(FTMP, lhs, rhs);
3585 } else {
3586 __ CmpUleS(FTMP, lhs, rhs);
3587 }
3588 __ Mfc1(dst, FTMP);
3589 __ Andi(dst, dst, 1);
3590 break;
3591 case kCondGT:
3592 if (gt_bias) {
3593 __ CmpUltS(FTMP, rhs, lhs);
3594 } else {
3595 __ CmpLtS(FTMP, rhs, lhs);
3596 }
3597 __ Mfc1(dst, FTMP);
3598 __ Andi(dst, dst, 1);
3599 break;
3600 case kCondGE:
3601 if (gt_bias) {
3602 __ CmpUleS(FTMP, rhs, lhs);
3603 } else {
3604 __ CmpLeS(FTMP, rhs, lhs);
3605 }
3606 __ Mfc1(dst, FTMP);
3607 __ Andi(dst, dst, 1);
3608 break;
3609 default:
3610 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3611 UNREACHABLE();
3612 }
3613 } else {
3614 switch (cond) {
3615 case kCondEQ:
3616 __ CeqS(0, lhs, rhs);
3617 __ LoadConst32(dst, 1);
3618 __ Movf(dst, ZERO, 0);
3619 break;
3620 case kCondNE:
3621 __ CeqS(0, lhs, rhs);
3622 __ LoadConst32(dst, 1);
3623 __ Movt(dst, ZERO, 0);
3624 break;
3625 case kCondLT:
3626 if (gt_bias) {
3627 __ ColtS(0, lhs, rhs);
3628 } else {
3629 __ CultS(0, lhs, rhs);
3630 }
3631 __ LoadConst32(dst, 1);
3632 __ Movf(dst, ZERO, 0);
3633 break;
3634 case kCondLE:
3635 if (gt_bias) {
3636 __ ColeS(0, lhs, rhs);
3637 } else {
3638 __ CuleS(0, lhs, rhs);
3639 }
3640 __ LoadConst32(dst, 1);
3641 __ Movf(dst, ZERO, 0);
3642 break;
3643 case kCondGT:
3644 if (gt_bias) {
3645 __ CultS(0, rhs, lhs);
3646 } else {
3647 __ ColtS(0, rhs, lhs);
3648 }
3649 __ LoadConst32(dst, 1);
3650 __ Movf(dst, ZERO, 0);
3651 break;
3652 case kCondGE:
3653 if (gt_bias) {
3654 __ CuleS(0, rhs, lhs);
3655 } else {
3656 __ ColeS(0, rhs, lhs);
3657 }
3658 __ LoadConst32(dst, 1);
3659 __ Movf(dst, ZERO, 0);
3660 break;
3661 default:
3662 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3663 UNREACHABLE();
3664 }
3665 }
3666 } else {
3667 DCHECK_EQ(type, Primitive::kPrimDouble);
3668 if (isR6) {
3669 switch (cond) {
3670 case kCondEQ:
3671 __ CmpEqD(FTMP, lhs, rhs);
3672 __ Mfc1(dst, FTMP);
3673 __ Andi(dst, dst, 1);
3674 break;
3675 case kCondNE:
3676 __ CmpEqD(FTMP, lhs, rhs);
3677 __ Mfc1(dst, FTMP);
3678 __ Addiu(dst, dst, 1);
3679 break;
3680 case kCondLT:
3681 if (gt_bias) {
3682 __ CmpLtD(FTMP, lhs, rhs);
3683 } else {
3684 __ CmpUltD(FTMP, lhs, rhs);
3685 }
3686 __ Mfc1(dst, FTMP);
3687 __ Andi(dst, dst, 1);
3688 break;
3689 case kCondLE:
3690 if (gt_bias) {
3691 __ CmpLeD(FTMP, lhs, rhs);
3692 } else {
3693 __ CmpUleD(FTMP, lhs, rhs);
3694 }
3695 __ Mfc1(dst, FTMP);
3696 __ Andi(dst, dst, 1);
3697 break;
3698 case kCondGT:
3699 if (gt_bias) {
3700 __ CmpUltD(FTMP, rhs, lhs);
3701 } else {
3702 __ CmpLtD(FTMP, rhs, lhs);
3703 }
3704 __ Mfc1(dst, FTMP);
3705 __ Andi(dst, dst, 1);
3706 break;
3707 case kCondGE:
3708 if (gt_bias) {
3709 __ CmpUleD(FTMP, rhs, lhs);
3710 } else {
3711 __ CmpLeD(FTMP, rhs, lhs);
3712 }
3713 __ Mfc1(dst, FTMP);
3714 __ Andi(dst, dst, 1);
3715 break;
3716 default:
3717 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3718 UNREACHABLE();
3719 }
3720 } else {
3721 switch (cond) {
3722 case kCondEQ:
3723 __ CeqD(0, lhs, rhs);
3724 __ LoadConst32(dst, 1);
3725 __ Movf(dst, ZERO, 0);
3726 break;
3727 case kCondNE:
3728 __ CeqD(0, lhs, rhs);
3729 __ LoadConst32(dst, 1);
3730 __ Movt(dst, ZERO, 0);
3731 break;
3732 case kCondLT:
3733 if (gt_bias) {
3734 __ ColtD(0, lhs, rhs);
3735 } else {
3736 __ CultD(0, lhs, rhs);
3737 }
3738 __ LoadConst32(dst, 1);
3739 __ Movf(dst, ZERO, 0);
3740 break;
3741 case kCondLE:
3742 if (gt_bias) {
3743 __ ColeD(0, lhs, rhs);
3744 } else {
3745 __ CuleD(0, lhs, rhs);
3746 }
3747 __ LoadConst32(dst, 1);
3748 __ Movf(dst, ZERO, 0);
3749 break;
3750 case kCondGT:
3751 if (gt_bias) {
3752 __ CultD(0, rhs, lhs);
3753 } else {
3754 __ ColtD(0, rhs, lhs);
3755 }
3756 __ LoadConst32(dst, 1);
3757 __ Movf(dst, ZERO, 0);
3758 break;
3759 case kCondGE:
3760 if (gt_bias) {
3761 __ CuleD(0, rhs, lhs);
3762 } else {
3763 __ ColeD(0, rhs, lhs);
3764 }
3765 __ LoadConst32(dst, 1);
3766 __ Movf(dst, ZERO, 0);
3767 break;
3768 default:
3769 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3770 UNREACHABLE();
3771 }
3772 }
3773 }
3774}
3775
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003776bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3777 bool gt_bias,
3778 Primitive::Type type,
3779 LocationSummary* input_locations,
3780 int cc) {
3781 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3782 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3783 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3784 if (type == Primitive::kPrimFloat) {
3785 switch (cond) {
3786 case kCondEQ:
3787 __ CeqS(cc, lhs, rhs);
3788 return false;
3789 case kCondNE:
3790 __ CeqS(cc, lhs, rhs);
3791 return true;
3792 case kCondLT:
3793 if (gt_bias) {
3794 __ ColtS(cc, lhs, rhs);
3795 } else {
3796 __ CultS(cc, lhs, rhs);
3797 }
3798 return false;
3799 case kCondLE:
3800 if (gt_bias) {
3801 __ ColeS(cc, lhs, rhs);
3802 } else {
3803 __ CuleS(cc, lhs, rhs);
3804 }
3805 return false;
3806 case kCondGT:
3807 if (gt_bias) {
3808 __ CultS(cc, rhs, lhs);
3809 } else {
3810 __ ColtS(cc, rhs, lhs);
3811 }
3812 return false;
3813 case kCondGE:
3814 if (gt_bias) {
3815 __ CuleS(cc, rhs, lhs);
3816 } else {
3817 __ ColeS(cc, rhs, lhs);
3818 }
3819 return false;
3820 default:
3821 LOG(FATAL) << "Unexpected non-floating-point condition";
3822 UNREACHABLE();
3823 }
3824 } else {
3825 DCHECK_EQ(type, Primitive::kPrimDouble);
3826 switch (cond) {
3827 case kCondEQ:
3828 __ CeqD(cc, lhs, rhs);
3829 return false;
3830 case kCondNE:
3831 __ CeqD(cc, lhs, rhs);
3832 return true;
3833 case kCondLT:
3834 if (gt_bias) {
3835 __ ColtD(cc, lhs, rhs);
3836 } else {
3837 __ CultD(cc, lhs, rhs);
3838 }
3839 return false;
3840 case kCondLE:
3841 if (gt_bias) {
3842 __ ColeD(cc, lhs, rhs);
3843 } else {
3844 __ CuleD(cc, lhs, rhs);
3845 }
3846 return false;
3847 case kCondGT:
3848 if (gt_bias) {
3849 __ CultD(cc, rhs, lhs);
3850 } else {
3851 __ ColtD(cc, rhs, lhs);
3852 }
3853 return false;
3854 case kCondGE:
3855 if (gt_bias) {
3856 __ CuleD(cc, rhs, lhs);
3857 } else {
3858 __ ColeD(cc, rhs, lhs);
3859 }
3860 return false;
3861 default:
3862 LOG(FATAL) << "Unexpected non-floating-point condition";
3863 UNREACHABLE();
3864 }
3865 }
3866}
3867
3868bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3869 bool gt_bias,
3870 Primitive::Type type,
3871 LocationSummary* input_locations,
3872 FRegister dst) {
3873 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3874 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3875 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3876 if (type == Primitive::kPrimFloat) {
3877 switch (cond) {
3878 case kCondEQ:
3879 __ CmpEqS(dst, lhs, rhs);
3880 return false;
3881 case kCondNE:
3882 __ CmpEqS(dst, lhs, rhs);
3883 return true;
3884 case kCondLT:
3885 if (gt_bias) {
3886 __ CmpLtS(dst, lhs, rhs);
3887 } else {
3888 __ CmpUltS(dst, lhs, rhs);
3889 }
3890 return false;
3891 case kCondLE:
3892 if (gt_bias) {
3893 __ CmpLeS(dst, lhs, rhs);
3894 } else {
3895 __ CmpUleS(dst, lhs, rhs);
3896 }
3897 return false;
3898 case kCondGT:
3899 if (gt_bias) {
3900 __ CmpUltS(dst, rhs, lhs);
3901 } else {
3902 __ CmpLtS(dst, rhs, lhs);
3903 }
3904 return false;
3905 case kCondGE:
3906 if (gt_bias) {
3907 __ CmpUleS(dst, rhs, lhs);
3908 } else {
3909 __ CmpLeS(dst, rhs, lhs);
3910 }
3911 return false;
3912 default:
3913 LOG(FATAL) << "Unexpected non-floating-point condition";
3914 UNREACHABLE();
3915 }
3916 } else {
3917 DCHECK_EQ(type, Primitive::kPrimDouble);
3918 switch (cond) {
3919 case kCondEQ:
3920 __ CmpEqD(dst, lhs, rhs);
3921 return false;
3922 case kCondNE:
3923 __ CmpEqD(dst, lhs, rhs);
3924 return true;
3925 case kCondLT:
3926 if (gt_bias) {
3927 __ CmpLtD(dst, lhs, rhs);
3928 } else {
3929 __ CmpUltD(dst, lhs, rhs);
3930 }
3931 return false;
3932 case kCondLE:
3933 if (gt_bias) {
3934 __ CmpLeD(dst, lhs, rhs);
3935 } else {
3936 __ CmpUleD(dst, lhs, rhs);
3937 }
3938 return false;
3939 case kCondGT:
3940 if (gt_bias) {
3941 __ CmpUltD(dst, rhs, lhs);
3942 } else {
3943 __ CmpLtD(dst, rhs, lhs);
3944 }
3945 return false;
3946 case kCondGE:
3947 if (gt_bias) {
3948 __ CmpUleD(dst, rhs, lhs);
3949 } else {
3950 __ CmpLeD(dst, rhs, lhs);
3951 }
3952 return false;
3953 default:
3954 LOG(FATAL) << "Unexpected non-floating-point condition";
3955 UNREACHABLE();
3956 }
3957 }
3958}
3959
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003960void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3961 bool gt_bias,
3962 Primitive::Type type,
3963 LocationSummary* locations,
3964 MipsLabel* label) {
3965 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3966 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3967 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3968 if (type == Primitive::kPrimFloat) {
3969 if (isR6) {
3970 switch (cond) {
3971 case kCondEQ:
3972 __ CmpEqS(FTMP, lhs, rhs);
3973 __ Bc1nez(FTMP, label);
3974 break;
3975 case kCondNE:
3976 __ CmpEqS(FTMP, lhs, rhs);
3977 __ Bc1eqz(FTMP, label);
3978 break;
3979 case kCondLT:
3980 if (gt_bias) {
3981 __ CmpLtS(FTMP, lhs, rhs);
3982 } else {
3983 __ CmpUltS(FTMP, lhs, rhs);
3984 }
3985 __ Bc1nez(FTMP, label);
3986 break;
3987 case kCondLE:
3988 if (gt_bias) {
3989 __ CmpLeS(FTMP, lhs, rhs);
3990 } else {
3991 __ CmpUleS(FTMP, lhs, rhs);
3992 }
3993 __ Bc1nez(FTMP, label);
3994 break;
3995 case kCondGT:
3996 if (gt_bias) {
3997 __ CmpUltS(FTMP, rhs, lhs);
3998 } else {
3999 __ CmpLtS(FTMP, rhs, lhs);
4000 }
4001 __ Bc1nez(FTMP, label);
4002 break;
4003 case kCondGE:
4004 if (gt_bias) {
4005 __ CmpUleS(FTMP, rhs, lhs);
4006 } else {
4007 __ CmpLeS(FTMP, rhs, lhs);
4008 }
4009 __ Bc1nez(FTMP, label);
4010 break;
4011 default:
4012 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004013 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004014 }
4015 } else {
4016 switch (cond) {
4017 case kCondEQ:
4018 __ CeqS(0, lhs, rhs);
4019 __ Bc1t(0, label);
4020 break;
4021 case kCondNE:
4022 __ CeqS(0, lhs, rhs);
4023 __ Bc1f(0, label);
4024 break;
4025 case kCondLT:
4026 if (gt_bias) {
4027 __ ColtS(0, lhs, rhs);
4028 } else {
4029 __ CultS(0, lhs, rhs);
4030 }
4031 __ Bc1t(0, label);
4032 break;
4033 case kCondLE:
4034 if (gt_bias) {
4035 __ ColeS(0, lhs, rhs);
4036 } else {
4037 __ CuleS(0, lhs, rhs);
4038 }
4039 __ Bc1t(0, label);
4040 break;
4041 case kCondGT:
4042 if (gt_bias) {
4043 __ CultS(0, rhs, lhs);
4044 } else {
4045 __ ColtS(0, rhs, lhs);
4046 }
4047 __ Bc1t(0, label);
4048 break;
4049 case kCondGE:
4050 if (gt_bias) {
4051 __ CuleS(0, rhs, lhs);
4052 } else {
4053 __ ColeS(0, rhs, lhs);
4054 }
4055 __ Bc1t(0, label);
4056 break;
4057 default:
4058 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004059 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004060 }
4061 }
4062 } else {
4063 DCHECK_EQ(type, Primitive::kPrimDouble);
4064 if (isR6) {
4065 switch (cond) {
4066 case kCondEQ:
4067 __ CmpEqD(FTMP, lhs, rhs);
4068 __ Bc1nez(FTMP, label);
4069 break;
4070 case kCondNE:
4071 __ CmpEqD(FTMP, lhs, rhs);
4072 __ Bc1eqz(FTMP, label);
4073 break;
4074 case kCondLT:
4075 if (gt_bias) {
4076 __ CmpLtD(FTMP, lhs, rhs);
4077 } else {
4078 __ CmpUltD(FTMP, lhs, rhs);
4079 }
4080 __ Bc1nez(FTMP, label);
4081 break;
4082 case kCondLE:
4083 if (gt_bias) {
4084 __ CmpLeD(FTMP, lhs, rhs);
4085 } else {
4086 __ CmpUleD(FTMP, lhs, rhs);
4087 }
4088 __ Bc1nez(FTMP, label);
4089 break;
4090 case kCondGT:
4091 if (gt_bias) {
4092 __ CmpUltD(FTMP, rhs, lhs);
4093 } else {
4094 __ CmpLtD(FTMP, rhs, lhs);
4095 }
4096 __ Bc1nez(FTMP, label);
4097 break;
4098 case kCondGE:
4099 if (gt_bias) {
4100 __ CmpUleD(FTMP, rhs, lhs);
4101 } else {
4102 __ CmpLeD(FTMP, rhs, lhs);
4103 }
4104 __ Bc1nez(FTMP, label);
4105 break;
4106 default:
4107 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004108 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004109 }
4110 } else {
4111 switch (cond) {
4112 case kCondEQ:
4113 __ CeqD(0, lhs, rhs);
4114 __ Bc1t(0, label);
4115 break;
4116 case kCondNE:
4117 __ CeqD(0, lhs, rhs);
4118 __ Bc1f(0, label);
4119 break;
4120 case kCondLT:
4121 if (gt_bias) {
4122 __ ColtD(0, lhs, rhs);
4123 } else {
4124 __ CultD(0, lhs, rhs);
4125 }
4126 __ Bc1t(0, label);
4127 break;
4128 case kCondLE:
4129 if (gt_bias) {
4130 __ ColeD(0, lhs, rhs);
4131 } else {
4132 __ CuleD(0, lhs, rhs);
4133 }
4134 __ Bc1t(0, label);
4135 break;
4136 case kCondGT:
4137 if (gt_bias) {
4138 __ CultD(0, rhs, lhs);
4139 } else {
4140 __ ColtD(0, rhs, lhs);
4141 }
4142 __ Bc1t(0, label);
4143 break;
4144 case kCondGE:
4145 if (gt_bias) {
4146 __ CuleD(0, rhs, lhs);
4147 } else {
4148 __ ColeD(0, rhs, lhs);
4149 }
4150 __ Bc1t(0, label);
4151 break;
4152 default:
4153 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004154 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004155 }
4156 }
4157 }
4158}
4159
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004160void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004161 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004162 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004163 MipsLabel* false_target) {
4164 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004165
David Brazdil0debae72015-11-12 18:37:00 +00004166 if (true_target == nullptr && false_target == nullptr) {
4167 // Nothing to do. The code always falls through.
4168 return;
4169 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004170 // Constant condition, statically compared against "true" (integer value 1).
4171 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004172 if (true_target != nullptr) {
4173 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004174 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004175 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004176 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004177 if (false_target != nullptr) {
4178 __ B(false_target);
4179 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004180 }
David Brazdil0debae72015-11-12 18:37:00 +00004181 return;
4182 }
4183
4184 // The following code generates these patterns:
4185 // (1) true_target == nullptr && false_target != nullptr
4186 // - opposite condition true => branch to false_target
4187 // (2) true_target != nullptr && false_target == nullptr
4188 // - condition true => branch to true_target
4189 // (3) true_target != nullptr && false_target != nullptr
4190 // - condition true => branch to true_target
4191 // - branch to false_target
4192 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004193 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004194 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004195 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004196 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004197 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4198 } else {
4199 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4200 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004201 } else {
4202 // The condition instruction has not been materialized, use its inputs as
4203 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004204 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004205 Primitive::Type type = condition->InputAt(0)->GetType();
4206 LocationSummary* locations = cond->GetLocations();
4207 IfCondition if_cond = condition->GetCondition();
4208 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004209
David Brazdil0debae72015-11-12 18:37:00 +00004210 if (true_target == nullptr) {
4211 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004212 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004213 }
4214
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004215 switch (type) {
4216 default:
4217 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4218 break;
4219 case Primitive::kPrimLong:
4220 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4221 break;
4222 case Primitive::kPrimFloat:
4223 case Primitive::kPrimDouble:
4224 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4225 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004226 }
4227 }
David Brazdil0debae72015-11-12 18:37:00 +00004228
4229 // If neither branch falls through (case 3), the conditional branch to `true_target`
4230 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4231 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004232 __ B(false_target);
4233 }
4234}
4235
4236void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004238 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004239 locations->SetInAt(0, Location::RequiresRegister());
4240 }
4241}
4242
4243void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004244 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4245 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4246 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4247 nullptr : codegen_->GetLabelOf(true_successor);
4248 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4249 nullptr : codegen_->GetLabelOf(false_successor);
4250 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004251}
4252
4253void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4254 LocationSummary* locations = new (GetGraph()->GetArena())
4255 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004256 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004257 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004258 locations->SetInAt(0, Location::RequiresRegister());
4259 }
4260}
4261
4262void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004263 SlowPathCodeMIPS* slow_path =
4264 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004265 GenerateTestAndBranch(deoptimize,
4266 /* condition_input_index */ 0,
4267 slow_path->GetEntryLabel(),
4268 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004269}
4270
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004271// This function returns true if a conditional move can be generated for HSelect.
4272// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4273// branches and regular moves.
4274//
4275// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4276//
4277// While determining feasibility of a conditional move and setting inputs/outputs
4278// are two distinct tasks, this function does both because they share quite a bit
4279// of common logic.
4280static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4281 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4282 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4283 HCondition* condition = cond->AsCondition();
4284
4285 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4286 Primitive::Type dst_type = select->GetType();
4287
4288 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4289 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4290 bool is_true_value_zero_constant =
4291 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4292 bool is_false_value_zero_constant =
4293 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4294
4295 bool can_move_conditionally = false;
4296 bool use_const_for_false_in = false;
4297 bool use_const_for_true_in = false;
4298
4299 if (!cond->IsConstant()) {
4300 switch (cond_type) {
4301 default:
4302 switch (dst_type) {
4303 default:
4304 // Moving int on int condition.
4305 if (is_r6) {
4306 if (is_true_value_zero_constant) {
4307 // seleqz out_reg, false_reg, cond_reg
4308 can_move_conditionally = true;
4309 use_const_for_true_in = true;
4310 } else if (is_false_value_zero_constant) {
4311 // selnez out_reg, true_reg, cond_reg
4312 can_move_conditionally = true;
4313 use_const_for_false_in = true;
4314 } else if (materialized) {
4315 // Not materializing unmaterialized int conditions
4316 // to keep the instruction count low.
4317 // selnez AT, true_reg, cond_reg
4318 // seleqz TMP, false_reg, cond_reg
4319 // or out_reg, AT, TMP
4320 can_move_conditionally = true;
4321 }
4322 } else {
4323 // movn out_reg, true_reg/ZERO, cond_reg
4324 can_move_conditionally = true;
4325 use_const_for_true_in = is_true_value_zero_constant;
4326 }
4327 break;
4328 case Primitive::kPrimLong:
4329 // Moving long on int condition.
4330 if (is_r6) {
4331 if (is_true_value_zero_constant) {
4332 // seleqz out_reg_lo, false_reg_lo, cond_reg
4333 // seleqz out_reg_hi, false_reg_hi, cond_reg
4334 can_move_conditionally = true;
4335 use_const_for_true_in = true;
4336 } else if (is_false_value_zero_constant) {
4337 // selnez out_reg_lo, true_reg_lo, cond_reg
4338 // selnez out_reg_hi, true_reg_hi, cond_reg
4339 can_move_conditionally = true;
4340 use_const_for_false_in = true;
4341 }
4342 // Other long conditional moves would generate 6+ instructions,
4343 // which is too many.
4344 } else {
4345 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4346 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4347 can_move_conditionally = true;
4348 use_const_for_true_in = is_true_value_zero_constant;
4349 }
4350 break;
4351 case Primitive::kPrimFloat:
4352 case Primitive::kPrimDouble:
4353 // Moving float/double on int condition.
4354 if (is_r6) {
4355 if (materialized) {
4356 // Not materializing unmaterialized int conditions
4357 // to keep the instruction count low.
4358 can_move_conditionally = true;
4359 if (is_true_value_zero_constant) {
4360 // sltu TMP, ZERO, cond_reg
4361 // mtc1 TMP, temp_cond_reg
4362 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4363 use_const_for_true_in = true;
4364 } else if (is_false_value_zero_constant) {
4365 // sltu TMP, ZERO, cond_reg
4366 // mtc1 TMP, temp_cond_reg
4367 // selnez.fmt out_reg, true_reg, temp_cond_reg
4368 use_const_for_false_in = true;
4369 } else {
4370 // sltu TMP, ZERO, cond_reg
4371 // mtc1 TMP, temp_cond_reg
4372 // sel.fmt temp_cond_reg, false_reg, true_reg
4373 // mov.fmt out_reg, temp_cond_reg
4374 }
4375 }
4376 } else {
4377 // movn.fmt out_reg, true_reg, cond_reg
4378 can_move_conditionally = true;
4379 }
4380 break;
4381 }
4382 break;
4383 case Primitive::kPrimLong:
4384 // We don't materialize long comparison now
4385 // and use conditional branches instead.
4386 break;
4387 case Primitive::kPrimFloat:
4388 case Primitive::kPrimDouble:
4389 switch (dst_type) {
4390 default:
4391 // Moving int on float/double condition.
4392 if (is_r6) {
4393 if (is_true_value_zero_constant) {
4394 // mfc1 TMP, temp_cond_reg
4395 // seleqz out_reg, false_reg, TMP
4396 can_move_conditionally = true;
4397 use_const_for_true_in = true;
4398 } else if (is_false_value_zero_constant) {
4399 // mfc1 TMP, temp_cond_reg
4400 // selnez out_reg, true_reg, TMP
4401 can_move_conditionally = true;
4402 use_const_for_false_in = true;
4403 } else {
4404 // mfc1 TMP, temp_cond_reg
4405 // selnez AT, true_reg, TMP
4406 // seleqz TMP, false_reg, TMP
4407 // or out_reg, AT, TMP
4408 can_move_conditionally = true;
4409 }
4410 } else {
4411 // movt out_reg, true_reg/ZERO, cc
4412 can_move_conditionally = true;
4413 use_const_for_true_in = is_true_value_zero_constant;
4414 }
4415 break;
4416 case Primitive::kPrimLong:
4417 // Moving long on float/double condition.
4418 if (is_r6) {
4419 if (is_true_value_zero_constant) {
4420 // mfc1 TMP, temp_cond_reg
4421 // seleqz out_reg_lo, false_reg_lo, TMP
4422 // seleqz out_reg_hi, false_reg_hi, TMP
4423 can_move_conditionally = true;
4424 use_const_for_true_in = true;
4425 } else if (is_false_value_zero_constant) {
4426 // mfc1 TMP, temp_cond_reg
4427 // selnez out_reg_lo, true_reg_lo, TMP
4428 // selnez out_reg_hi, true_reg_hi, TMP
4429 can_move_conditionally = true;
4430 use_const_for_false_in = true;
4431 }
4432 // Other long conditional moves would generate 6+ instructions,
4433 // which is too many.
4434 } else {
4435 // movt out_reg_lo, true_reg_lo/ZERO, cc
4436 // movt out_reg_hi, true_reg_hi/ZERO, cc
4437 can_move_conditionally = true;
4438 use_const_for_true_in = is_true_value_zero_constant;
4439 }
4440 break;
4441 case Primitive::kPrimFloat:
4442 case Primitive::kPrimDouble:
4443 // Moving float/double on float/double condition.
4444 if (is_r6) {
4445 can_move_conditionally = true;
4446 if (is_true_value_zero_constant) {
4447 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4448 use_const_for_true_in = true;
4449 } else if (is_false_value_zero_constant) {
4450 // selnez.fmt out_reg, true_reg, temp_cond_reg
4451 use_const_for_false_in = true;
4452 } else {
4453 // sel.fmt temp_cond_reg, false_reg, true_reg
4454 // mov.fmt out_reg, temp_cond_reg
4455 }
4456 } else {
4457 // movt.fmt out_reg, true_reg, cc
4458 can_move_conditionally = true;
4459 }
4460 break;
4461 }
4462 break;
4463 }
4464 }
4465
4466 if (can_move_conditionally) {
4467 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4468 } else {
4469 DCHECK(!use_const_for_false_in);
4470 DCHECK(!use_const_for_true_in);
4471 }
4472
4473 if (locations_to_set != nullptr) {
4474 if (use_const_for_false_in) {
4475 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4476 } else {
4477 locations_to_set->SetInAt(0,
4478 Primitive::IsFloatingPointType(dst_type)
4479 ? Location::RequiresFpuRegister()
4480 : Location::RequiresRegister());
4481 }
4482 if (use_const_for_true_in) {
4483 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4484 } else {
4485 locations_to_set->SetInAt(1,
4486 Primitive::IsFloatingPointType(dst_type)
4487 ? Location::RequiresFpuRegister()
4488 : Location::RequiresRegister());
4489 }
4490 if (materialized) {
4491 locations_to_set->SetInAt(2, Location::RequiresRegister());
4492 }
4493 // On R6 we don't require the output to be the same as the
4494 // first input for conditional moves unlike on R2.
4495 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4496 if (is_out_same_as_first_in) {
4497 locations_to_set->SetOut(Location::SameAsFirstInput());
4498 } else {
4499 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4500 ? Location::RequiresFpuRegister()
4501 : Location::RequiresRegister());
4502 }
4503 }
4504
4505 return can_move_conditionally;
4506}
4507
4508void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4509 LocationSummary* locations = select->GetLocations();
4510 Location dst = locations->Out();
4511 Location src = locations->InAt(1);
4512 Register src_reg = ZERO;
4513 Register src_reg_high = ZERO;
4514 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4515 Register cond_reg = TMP;
4516 int cond_cc = 0;
4517 Primitive::Type cond_type = Primitive::kPrimInt;
4518 bool cond_inverted = false;
4519 Primitive::Type dst_type = select->GetType();
4520
4521 if (IsBooleanValueOrMaterializedCondition(cond)) {
4522 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4523 } else {
4524 HCondition* condition = cond->AsCondition();
4525 LocationSummary* cond_locations = cond->GetLocations();
4526 IfCondition if_cond = condition->GetCondition();
4527 cond_type = condition->InputAt(0)->GetType();
4528 switch (cond_type) {
4529 default:
4530 DCHECK_NE(cond_type, Primitive::kPrimLong);
4531 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4532 break;
4533 case Primitive::kPrimFloat:
4534 case Primitive::kPrimDouble:
4535 cond_inverted = MaterializeFpCompareR2(if_cond,
4536 condition->IsGtBias(),
4537 cond_type,
4538 cond_locations,
4539 cond_cc);
4540 break;
4541 }
4542 }
4543
4544 DCHECK(dst.Equals(locations->InAt(0)));
4545 if (src.IsRegister()) {
4546 src_reg = src.AsRegister<Register>();
4547 } else if (src.IsRegisterPair()) {
4548 src_reg = src.AsRegisterPairLow<Register>();
4549 src_reg_high = src.AsRegisterPairHigh<Register>();
4550 } else if (src.IsConstant()) {
4551 DCHECK(src.GetConstant()->IsZeroBitPattern());
4552 }
4553
4554 switch (cond_type) {
4555 default:
4556 switch (dst_type) {
4557 default:
4558 if (cond_inverted) {
4559 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4560 } else {
4561 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4562 }
4563 break;
4564 case Primitive::kPrimLong:
4565 if (cond_inverted) {
4566 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4567 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4568 } else {
4569 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4570 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4571 }
4572 break;
4573 case Primitive::kPrimFloat:
4574 if (cond_inverted) {
4575 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4576 } else {
4577 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4578 }
4579 break;
4580 case Primitive::kPrimDouble:
4581 if (cond_inverted) {
4582 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4583 } else {
4584 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4585 }
4586 break;
4587 }
4588 break;
4589 case Primitive::kPrimLong:
4590 LOG(FATAL) << "Unreachable";
4591 UNREACHABLE();
4592 case Primitive::kPrimFloat:
4593 case Primitive::kPrimDouble:
4594 switch (dst_type) {
4595 default:
4596 if (cond_inverted) {
4597 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4598 } else {
4599 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4600 }
4601 break;
4602 case Primitive::kPrimLong:
4603 if (cond_inverted) {
4604 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4605 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4606 } else {
4607 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4608 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4609 }
4610 break;
4611 case Primitive::kPrimFloat:
4612 if (cond_inverted) {
4613 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4614 } else {
4615 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4616 }
4617 break;
4618 case Primitive::kPrimDouble:
4619 if (cond_inverted) {
4620 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4621 } else {
4622 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4623 }
4624 break;
4625 }
4626 break;
4627 }
4628}
4629
4630void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4631 LocationSummary* locations = select->GetLocations();
4632 Location dst = locations->Out();
4633 Location false_src = locations->InAt(0);
4634 Location true_src = locations->InAt(1);
4635 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4636 Register cond_reg = TMP;
4637 FRegister fcond_reg = FTMP;
4638 Primitive::Type cond_type = Primitive::kPrimInt;
4639 bool cond_inverted = false;
4640 Primitive::Type dst_type = select->GetType();
4641
4642 if (IsBooleanValueOrMaterializedCondition(cond)) {
4643 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4644 } else {
4645 HCondition* condition = cond->AsCondition();
4646 LocationSummary* cond_locations = cond->GetLocations();
4647 IfCondition if_cond = condition->GetCondition();
4648 cond_type = condition->InputAt(0)->GetType();
4649 switch (cond_type) {
4650 default:
4651 DCHECK_NE(cond_type, Primitive::kPrimLong);
4652 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4653 break;
4654 case Primitive::kPrimFloat:
4655 case Primitive::kPrimDouble:
4656 cond_inverted = MaterializeFpCompareR6(if_cond,
4657 condition->IsGtBias(),
4658 cond_type,
4659 cond_locations,
4660 fcond_reg);
4661 break;
4662 }
4663 }
4664
4665 if (true_src.IsConstant()) {
4666 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4667 }
4668 if (false_src.IsConstant()) {
4669 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4670 }
4671
4672 switch (dst_type) {
4673 default:
4674 if (Primitive::IsFloatingPointType(cond_type)) {
4675 __ Mfc1(cond_reg, fcond_reg);
4676 }
4677 if (true_src.IsConstant()) {
4678 if (cond_inverted) {
4679 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4680 } else {
4681 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4682 }
4683 } else if (false_src.IsConstant()) {
4684 if (cond_inverted) {
4685 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4686 } else {
4687 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4688 }
4689 } else {
4690 DCHECK_NE(cond_reg, AT);
4691 if (cond_inverted) {
4692 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4693 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4694 } else {
4695 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4696 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4697 }
4698 __ Or(dst.AsRegister<Register>(), AT, TMP);
4699 }
4700 break;
4701 case Primitive::kPrimLong: {
4702 if (Primitive::IsFloatingPointType(cond_type)) {
4703 __ Mfc1(cond_reg, fcond_reg);
4704 }
4705 Register dst_lo = dst.AsRegisterPairLow<Register>();
4706 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4707 if (true_src.IsConstant()) {
4708 Register src_lo = false_src.AsRegisterPairLow<Register>();
4709 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4710 if (cond_inverted) {
4711 __ Selnez(dst_lo, src_lo, cond_reg);
4712 __ Selnez(dst_hi, src_hi, cond_reg);
4713 } else {
4714 __ Seleqz(dst_lo, src_lo, cond_reg);
4715 __ Seleqz(dst_hi, src_hi, cond_reg);
4716 }
4717 } else {
4718 DCHECK(false_src.IsConstant());
4719 Register src_lo = true_src.AsRegisterPairLow<Register>();
4720 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4721 if (cond_inverted) {
4722 __ Seleqz(dst_lo, src_lo, cond_reg);
4723 __ Seleqz(dst_hi, src_hi, cond_reg);
4724 } else {
4725 __ Selnez(dst_lo, src_lo, cond_reg);
4726 __ Selnez(dst_hi, src_hi, cond_reg);
4727 }
4728 }
4729 break;
4730 }
4731 case Primitive::kPrimFloat: {
4732 if (!Primitive::IsFloatingPointType(cond_type)) {
4733 // sel*.fmt tests bit 0 of the condition register, account for that.
4734 __ Sltu(TMP, ZERO, cond_reg);
4735 __ Mtc1(TMP, fcond_reg);
4736 }
4737 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4738 if (true_src.IsConstant()) {
4739 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4740 if (cond_inverted) {
4741 __ SelnezS(dst_reg, src_reg, fcond_reg);
4742 } else {
4743 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4744 }
4745 } else if (false_src.IsConstant()) {
4746 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4747 if (cond_inverted) {
4748 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4749 } else {
4750 __ SelnezS(dst_reg, src_reg, fcond_reg);
4751 }
4752 } else {
4753 if (cond_inverted) {
4754 __ SelS(fcond_reg,
4755 true_src.AsFpuRegister<FRegister>(),
4756 false_src.AsFpuRegister<FRegister>());
4757 } else {
4758 __ SelS(fcond_reg,
4759 false_src.AsFpuRegister<FRegister>(),
4760 true_src.AsFpuRegister<FRegister>());
4761 }
4762 __ MovS(dst_reg, fcond_reg);
4763 }
4764 break;
4765 }
4766 case Primitive::kPrimDouble: {
4767 if (!Primitive::IsFloatingPointType(cond_type)) {
4768 // sel*.fmt tests bit 0 of the condition register, account for that.
4769 __ Sltu(TMP, ZERO, cond_reg);
4770 __ Mtc1(TMP, fcond_reg);
4771 }
4772 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4773 if (true_src.IsConstant()) {
4774 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4775 if (cond_inverted) {
4776 __ SelnezD(dst_reg, src_reg, fcond_reg);
4777 } else {
4778 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4779 }
4780 } else if (false_src.IsConstant()) {
4781 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4782 if (cond_inverted) {
4783 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4784 } else {
4785 __ SelnezD(dst_reg, src_reg, fcond_reg);
4786 }
4787 } else {
4788 if (cond_inverted) {
4789 __ SelD(fcond_reg,
4790 true_src.AsFpuRegister<FRegister>(),
4791 false_src.AsFpuRegister<FRegister>());
4792 } else {
4793 __ SelD(fcond_reg,
4794 false_src.AsFpuRegister<FRegister>(),
4795 true_src.AsFpuRegister<FRegister>());
4796 }
4797 __ MovD(dst_reg, fcond_reg);
4798 }
4799 break;
4800 }
4801 }
4802}
4803
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004804void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4805 LocationSummary* locations = new (GetGraph()->GetArena())
4806 LocationSummary(flag, LocationSummary::kNoCall);
4807 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004808}
4809
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004810void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4811 __ LoadFromOffset(kLoadWord,
4812 flag->GetLocations()->Out().AsRegister<Register>(),
4813 SP,
4814 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004815}
4816
David Brazdil74eb1b22015-12-14 11:44:01 +00004817void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004819 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004820}
4821
4822void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004823 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4824 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4825 if (is_r6) {
4826 GenConditionalMoveR6(select);
4827 } else {
4828 GenConditionalMoveR2(select);
4829 }
4830 } else {
4831 LocationSummary* locations = select->GetLocations();
4832 MipsLabel false_target;
4833 GenerateTestAndBranch(select,
4834 /* condition_input_index */ 2,
4835 /* true_target */ nullptr,
4836 &false_target);
4837 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4838 __ Bind(&false_target);
4839 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004840}
4841
David Srbecky0cf44932015-12-09 14:09:59 +00004842void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4843 new (GetGraph()->GetArena()) LocationSummary(info);
4844}
4845
David Srbeckyd28f4a02016-03-14 17:14:24 +00004846void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4847 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004848}
4849
4850void CodeGeneratorMIPS::GenerateNop() {
4851 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004852}
4853
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004854void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4855 Primitive::Type field_type = field_info.GetFieldType();
4856 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4857 bool generate_volatile = field_info.IsVolatile() && is_wide;
4858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004859 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004860
4861 locations->SetInAt(0, Location::RequiresRegister());
4862 if (generate_volatile) {
4863 InvokeRuntimeCallingConvention calling_convention;
4864 // need A0 to hold base + offset
4865 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4866 if (field_type == Primitive::kPrimLong) {
4867 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4868 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004869 // Use Location::Any() to prevent situations when running out of available fp registers.
4870 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004871 // Need some temp core regs since FP results are returned in core registers
4872 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4873 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4874 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4875 }
4876 } else {
4877 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4878 locations->SetOut(Location::RequiresFpuRegister());
4879 } else {
4880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4881 }
4882 }
4883}
4884
4885void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4886 const FieldInfo& field_info,
4887 uint32_t dex_pc) {
4888 Primitive::Type type = field_info.GetFieldType();
4889 LocationSummary* locations = instruction->GetLocations();
4890 Register obj = locations->InAt(0).AsRegister<Register>();
4891 LoadOperandType load_type = kLoadUnsignedByte;
4892 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004893 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004894 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004895
4896 switch (type) {
4897 case Primitive::kPrimBoolean:
4898 load_type = kLoadUnsignedByte;
4899 break;
4900 case Primitive::kPrimByte:
4901 load_type = kLoadSignedByte;
4902 break;
4903 case Primitive::kPrimShort:
4904 load_type = kLoadSignedHalfword;
4905 break;
4906 case Primitive::kPrimChar:
4907 load_type = kLoadUnsignedHalfword;
4908 break;
4909 case Primitive::kPrimInt:
4910 case Primitive::kPrimFloat:
4911 case Primitive::kPrimNot:
4912 load_type = kLoadWord;
4913 break;
4914 case Primitive::kPrimLong:
4915 case Primitive::kPrimDouble:
4916 load_type = kLoadDoubleword;
4917 break;
4918 case Primitive::kPrimVoid:
4919 LOG(FATAL) << "Unreachable type " << type;
4920 UNREACHABLE();
4921 }
4922
4923 if (is_volatile && load_type == kLoadDoubleword) {
4924 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004925 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004926 // Do implicit Null check
4927 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4928 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004929 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004930 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4931 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004932 // FP results are returned in core registers. Need to move them.
4933 Location out = locations->Out();
4934 if (out.IsFpuRegister()) {
4935 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4936 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4937 out.AsFpuRegister<FRegister>());
4938 } else {
4939 DCHECK(out.IsDoubleStackSlot());
4940 __ StoreToOffset(kStoreWord,
4941 locations->GetTemp(1).AsRegister<Register>(),
4942 SP,
4943 out.GetStackIndex());
4944 __ StoreToOffset(kStoreWord,
4945 locations->GetTemp(2).AsRegister<Register>(),
4946 SP,
4947 out.GetStackIndex() + 4);
4948 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004949 }
4950 } else {
4951 if (!Primitive::IsFloatingPointType(type)) {
4952 Register dst;
4953 if (type == Primitive::kPrimLong) {
4954 DCHECK(locations->Out().IsRegisterPair());
4955 dst = locations->Out().AsRegisterPairLow<Register>();
4956 } else {
4957 DCHECK(locations->Out().IsRegister());
4958 dst = locations->Out().AsRegister<Register>();
4959 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004960 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004961 } else {
4962 DCHECK(locations->Out().IsFpuRegister());
4963 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4964 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004965 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004966 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004967 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004968 }
4969 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004970 }
4971
4972 if (is_volatile) {
4973 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4974 }
4975}
4976
4977void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4978 Primitive::Type field_type = field_info.GetFieldType();
4979 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4980 bool generate_volatile = field_info.IsVolatile() && is_wide;
4981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004982 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004983
4984 locations->SetInAt(0, Location::RequiresRegister());
4985 if (generate_volatile) {
4986 InvokeRuntimeCallingConvention calling_convention;
4987 // need A0 to hold base + offset
4988 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4989 if (field_type == Primitive::kPrimLong) {
4990 locations->SetInAt(1, Location::RegisterPairLocation(
4991 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4992 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004993 // Use Location::Any() to prevent situations when running out of available fp registers.
4994 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004995 // Pass FP parameters in core registers.
4996 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4997 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4998 }
4999 } else {
5000 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005001 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005002 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005003 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005004 }
5005 }
5006}
5007
5008void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
5009 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01005010 uint32_t dex_pc,
5011 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005012 Primitive::Type type = field_info.GetFieldType();
5013 LocationSummary* locations = instruction->GetLocations();
5014 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07005015 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005016 StoreOperandType store_type = kStoreByte;
5017 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005018 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07005019 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005020
5021 switch (type) {
5022 case Primitive::kPrimBoolean:
5023 case Primitive::kPrimByte:
5024 store_type = kStoreByte;
5025 break;
5026 case Primitive::kPrimShort:
5027 case Primitive::kPrimChar:
5028 store_type = kStoreHalfword;
5029 break;
5030 case Primitive::kPrimInt:
5031 case Primitive::kPrimFloat:
5032 case Primitive::kPrimNot:
5033 store_type = kStoreWord;
5034 break;
5035 case Primitive::kPrimLong:
5036 case Primitive::kPrimDouble:
5037 store_type = kStoreDoubleword;
5038 break;
5039 case Primitive::kPrimVoid:
5040 LOG(FATAL) << "Unreachable type " << type;
5041 UNREACHABLE();
5042 }
5043
5044 if (is_volatile) {
5045 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5046 }
5047
5048 if (is_volatile && store_type == kStoreDoubleword) {
5049 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005050 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005051 // Do implicit Null check.
5052 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5053 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5054 if (type == Primitive::kPrimDouble) {
5055 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07005056 if (value_location.IsFpuRegister()) {
5057 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
5058 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005059 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07005060 value_location.AsFpuRegister<FRegister>());
5061 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005062 __ LoadFromOffset(kLoadWord,
5063 locations->GetTemp(1).AsRegister<Register>(),
5064 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005065 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005066 __ LoadFromOffset(kLoadWord,
5067 locations->GetTemp(2).AsRegister<Register>(),
5068 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07005069 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005070 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005071 DCHECK(value_location.IsConstant());
5072 DCHECK(value_location.GetConstant()->IsDoubleConstant());
5073 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005074 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
5075 locations->GetTemp(1).AsRegister<Register>(),
5076 value);
5077 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005078 }
Serban Constantinescufca16662016-07-14 09:21:59 +01005079 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005080 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
5081 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005082 if (value_location.IsConstant()) {
5083 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
5084 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
5085 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005086 Register src;
5087 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005088 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005089 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005090 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005091 }
Alexey Frunze2923db72016-08-20 01:55:47 -07005092 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005093 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005094 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005095 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005096 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005097 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005098 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005099 }
5100 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005101 }
5102
5103 // TODO: memory barriers?
5104 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005105 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01005106 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005107 }
5108
5109 if (is_volatile) {
5110 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5111 }
5112}
5113
5114void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5115 HandleFieldGet(instruction, instruction->GetFieldInfo());
5116}
5117
5118void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5119 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5120}
5121
5122void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5123 HandleFieldSet(instruction, instruction->GetFieldInfo());
5124}
5125
5126void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01005127 HandleFieldSet(instruction,
5128 instruction->GetFieldInfo(),
5129 instruction->GetDexPc(),
5130 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131}
5132
Alexey Frunze06a46c42016-07-19 15:00:40 -07005133void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5134 HInstruction* instruction ATTRIBUTE_UNUSED,
5135 Location root,
5136 Register obj,
5137 uint32_t offset) {
5138 Register root_reg = root.AsRegister<Register>();
5139 if (kEmitCompilerReadBarrier) {
5140 UNIMPLEMENTED(FATAL) << "for read barrier";
5141 } else {
5142 // Plain GC root load with no read barrier.
5143 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5144 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5145 // Note that GC roots are not affected by heap poisoning, thus we
5146 // do not have to unpoison `root_reg` here.
5147 }
5148}
5149
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005150void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5151 LocationSummary::CallKind call_kind =
5152 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5153 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5154 locations->SetInAt(0, Location::RequiresRegister());
5155 locations->SetInAt(1, Location::RequiresRegister());
5156 // The output does overlap inputs.
5157 // Note that TypeCheckSlowPathMIPS uses this register too.
5158 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5159}
5160
5161void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5162 LocationSummary* locations = instruction->GetLocations();
5163 Register obj = locations->InAt(0).AsRegister<Register>();
5164 Register cls = locations->InAt(1).AsRegister<Register>();
5165 Register out = locations->Out().AsRegister<Register>();
5166
5167 MipsLabel done;
5168
5169 // Return 0 if `obj` is null.
5170 // TODO: Avoid this check if we know `obj` is not null.
5171 __ Move(out, ZERO);
5172 __ Beqz(obj, &done);
5173
5174 // Compare the class of `obj` with `cls`.
5175 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5176 if (instruction->IsExactCheck()) {
5177 // Classes must be equal for the instanceof to succeed.
5178 __ Xor(out, out, cls);
5179 __ Sltiu(out, out, 1);
5180 } else {
5181 // If the classes are not equal, we go into a slow path.
5182 DCHECK(locations->OnlyCallsOnSlowPath());
5183 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5184 codegen_->AddSlowPath(slow_path);
5185 __ Bne(out, cls, slow_path->GetEntryLabel());
5186 __ LoadConst32(out, 1);
5187 __ Bind(slow_path->GetExitLabel());
5188 }
5189
5190 __ Bind(&done);
5191}
5192
5193void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5194 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5195 locations->SetOut(Location::ConstantLocation(constant));
5196}
5197
5198void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5199 // Will be generated at use site.
5200}
5201
5202void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5203 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5204 locations->SetOut(Location::ConstantLocation(constant));
5205}
5206
5207void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5208 // Will be generated at use site.
5209}
5210
5211void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5212 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5213 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5214}
5215
5216void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5217 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005218 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005219 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005220 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005221}
5222
5223void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5224 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5225 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005226 Location receiver = invoke->GetLocations()->InAt(0);
5227 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005228 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005229
5230 // Set the hidden argument.
5231 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5232 invoke->GetDexMethodIndex());
5233
5234 // temp = object->GetClass();
5235 if (receiver.IsStackSlot()) {
5236 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5237 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5238 } else {
5239 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5240 }
5241 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005242 __ LoadFromOffset(kLoadWord, temp, temp,
5243 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5244 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005245 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005246 // temp = temp->GetImtEntryAt(method_offset);
5247 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5248 // T9 = temp->GetEntryPoint();
5249 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5250 // T9();
5251 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005252 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005253 DCHECK(!codegen_->IsLeafMethod());
5254 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5255}
5256
5257void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005258 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5259 if (intrinsic.TryDispatch(invoke)) {
5260 return;
5261 }
5262
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005263 HandleInvoke(invoke);
5264}
5265
5266void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005267 // Explicit clinit checks triggered by static invokes must have been pruned by
5268 // art::PrepareForRegisterAllocation.
5269 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005270
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005271 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5272 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005273
Chris Larsen701566a2015-10-27 15:29:13 -07005274 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5275 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005276 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5277 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5278 }
Chris Larsen701566a2015-10-27 15:29:13 -07005279 return;
5280 }
5281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005282 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005283
5284 // Add the extra input register if either the dex cache array base register
5285 // or the PC-relative base register for accessing literals is needed.
5286 if (has_extra_input) {
5287 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5288 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005289}
5290
Orion Hodsonac141392017-01-13 11:53:47 +00005291void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5292 HandleInvoke(invoke);
5293}
5294
5295void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5296 codegen_->GenerateInvokePolymorphicCall(invoke);
5297}
5298
Chris Larsen701566a2015-10-27 15:29:13 -07005299static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005300 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005301 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5302 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005303 return true;
5304 }
5305 return false;
5306}
5307
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005308HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005309 HLoadString::LoadKind desired_string_load_kind) {
5310 if (kEmitCompilerReadBarrier) {
5311 UNIMPLEMENTED(FATAL) << "for read barrier";
5312 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005313 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005314 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005315 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5316 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005317 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005318 bool is_r6 = GetInstructionSetFeatures().IsR6();
5319 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005320 switch (desired_string_load_kind) {
5321 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5322 DCHECK(!GetCompilerOptions().GetCompilePic());
5323 break;
5324 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5325 DCHECK(GetCompilerOptions().GetCompilePic());
5326 break;
5327 case HLoadString::LoadKind::kBootImageAddress:
5328 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005329 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005330 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005331 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005332 case HLoadString::LoadKind::kJitTableAddress:
5333 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005334 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005335 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005336 case HLoadString::LoadKind::kDexCacheViaMethod:
5337 fallback_load = false;
5338 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005339 }
5340 if (fallback_load) {
5341 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5342 }
5343 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005344}
5345
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005346HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5347 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005348 if (kEmitCompilerReadBarrier) {
5349 UNIMPLEMENTED(FATAL) << "for read barrier";
5350 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005351 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07005352 // is incompatible with it.
5353 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005354 bool is_r6 = GetInstructionSetFeatures().IsR6();
5355 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005356 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005357 case HLoadClass::LoadKind::kInvalid:
5358 LOG(FATAL) << "UNREACHABLE";
5359 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005360 case HLoadClass::LoadKind::kReferrersClass:
5361 fallback_load = false;
5362 break;
5363 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5364 DCHECK(!GetCompilerOptions().GetCompilePic());
5365 break;
5366 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5367 DCHECK(GetCompilerOptions().GetCompilePic());
5368 break;
5369 case HLoadClass::LoadKind::kBootImageAddress:
5370 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005371 case HLoadClass::LoadKind::kBssEntry:
5372 DCHECK(!Runtime::Current()->UseJitCompilation());
5373 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005374 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005375 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08005376 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005377 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005378 case HLoadClass::LoadKind::kDexCacheViaMethod:
5379 fallback_load = false;
5380 break;
5381 }
5382 if (fallback_load) {
5383 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5384 }
5385 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005386}
5387
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005388Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5389 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005390 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005391 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5392 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5393 if (!invoke->GetLocations()->Intrinsified()) {
5394 return location.AsRegister<Register>();
5395 }
5396 // For intrinsics we allow any location, so it may be on the stack.
5397 if (!location.IsRegister()) {
5398 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5399 return temp;
5400 }
5401 // For register locations, check if the register was saved. If so, get it from the stack.
5402 // Note: There is a chance that the register was saved but not overwritten, so we could
5403 // save one load. However, since this is just an intrinsic slow path we prefer this
5404 // simple and more robust approach rather that trying to determine if that's the case.
5405 SlowPathCode* slow_path = GetCurrentSlowPath();
5406 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5407 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5408 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5409 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5410 return temp;
5411 }
5412 return location.AsRegister<Register>();
5413}
5414
Vladimir Markodc151b22015-10-15 18:02:30 +01005415HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5416 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005417 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005418 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005419 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005420 // is incompatible with it.
5421 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005422 bool is_r6 = GetInstructionSetFeatures().IsR6();
5423 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005424 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005425 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005426 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005427 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005428 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005429 break;
5430 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005431 if (fallback_load) {
5432 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5433 dispatch_info.method_load_data = 0;
5434 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005435 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005436}
5437
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005438void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5439 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005440 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005441 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5442 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005443 bool is_r6 = GetInstructionSetFeatures().IsR6();
5444 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005445 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5446 : ZERO;
5447
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005448 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005449 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005450 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005451 uint32_t offset =
5452 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005453 __ LoadFromOffset(kLoadWord,
5454 temp.AsRegister<Register>(),
5455 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005456 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005457 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005459 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005460 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005461 break;
5462 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5463 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5464 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005465 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5466 if (is_r6) {
5467 uint32_t offset = invoke->GetDexCacheArrayOffset();
5468 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5469 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
5470 bool reordering = __ SetReorder(false);
5471 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
5472 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
5473 __ SetReorder(reordering);
5474 } else {
5475 HMipsDexCacheArraysBase* base =
5476 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5477 int32_t offset =
5478 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5479 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5480 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005481 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005482 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005483 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005484 Register reg = temp.AsRegister<Register>();
5485 Register method_reg;
5486 if (current_method.IsRegister()) {
5487 method_reg = current_method.AsRegister<Register>();
5488 } else {
5489 // TODO: use the appropriate DCHECK() here if possible.
5490 // DCHECK(invoke->GetLocations()->Intrinsified());
5491 DCHECK(!current_method.IsValid());
5492 method_reg = reg;
5493 __ Lw(reg, SP, kCurrentMethodStackOffset);
5494 }
5495
5496 // temp = temp->dex_cache_resolved_methods_;
5497 __ LoadFromOffset(kLoadWord,
5498 reg,
5499 method_reg,
5500 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005501 // temp = temp[index_in_cache];
5502 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5503 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005504 __ LoadFromOffset(kLoadWord,
5505 reg,
5506 reg,
5507 CodeGenerator::GetCachePointerOffset(index_in_cache));
5508 break;
5509 }
5510 }
5511
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005512 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005513 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005514 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005515 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005516 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5517 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005518 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005519 T9,
5520 callee_method.AsRegister<Register>(),
5521 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005522 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005523 // T9()
5524 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005525 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005526 break;
5527 }
5528 DCHECK(!IsLeafMethod());
5529}
5530
5531void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005532 // Explicit clinit checks triggered by static invokes must have been pruned by
5533 // art::PrepareForRegisterAllocation.
5534 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005535
5536 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5537 return;
5538 }
5539
5540 LocationSummary* locations = invoke->GetLocations();
5541 codegen_->GenerateStaticOrDirectCall(invoke,
5542 locations->HasTemps()
5543 ? locations->GetTemp(0)
5544 : Location::NoLocation());
5545 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5546}
5547
Chris Larsen3acee732015-11-18 13:31:08 -08005548void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005549 // Use the calling convention instead of the location of the receiver, as
5550 // intrinsics may have put the receiver in a different register. In the intrinsics
5551 // slow path, the arguments have been moved to the right place, so here we are
5552 // guaranteed that the receiver is the first register of the calling convention.
5553 InvokeDexCallingConvention calling_convention;
5554 Register receiver = calling_convention.GetRegisterAt(0);
5555
Chris Larsen3acee732015-11-18 13:31:08 -08005556 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005557 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5558 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5559 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005560 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005561
5562 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005563 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005564 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005565 // temp = temp->GetMethodAt(method_offset);
5566 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5567 // T9 = temp->GetEntryPoint();
5568 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5569 // T9();
5570 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005571 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005572}
5573
5574void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5575 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5576 return;
5577 }
5578
5579 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580 DCHECK(!codegen_->IsLeafMethod());
5581 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5582}
5583
5584void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005585 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5586 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005587 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005588 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005589 cls,
5590 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005591 Location::RegisterLocation(V0));
Alexey Frunze06a46c42016-07-19 15:00:40 -07005592 return;
5593 }
Vladimir Marko41559982017-01-06 14:04:23 +00005594 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005595
5596 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5597 ? LocationSummary::kCallOnSlowPath
5598 : LocationSummary::kNoCall;
5599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005600 switch (load_kind) {
5601 // We need an extra register for PC-relative literals on R2.
5602 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005603 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005604 case HLoadClass::LoadKind::kBootImageAddress:
5605 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005606 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5607 break;
5608 }
5609 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005610 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005611 locations->SetInAt(0, Location::RequiresRegister());
5612 break;
5613 default:
5614 break;
5615 }
5616 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005617}
5618
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005619// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5620// move.
5621void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005622 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5623 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5624 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01005625 return;
5626 }
Vladimir Marko41559982017-01-06 14:04:23 +00005627 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01005628
Vladimir Marko41559982017-01-06 14:04:23 +00005629 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005630 Location out_loc = locations->Out();
5631 Register out = out_loc.AsRegister<Register>();
5632 Register base_or_current_method_reg;
5633 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5634 switch (load_kind) {
5635 // We need an extra register for PC-relative literals on R2.
5636 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005637 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005638 case HLoadClass::LoadKind::kBootImageAddress:
5639 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005640 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5641 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005642 case HLoadClass::LoadKind::kReferrersClass:
5643 case HLoadClass::LoadKind::kDexCacheViaMethod:
5644 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5645 break;
5646 default:
5647 base_or_current_method_reg = ZERO;
5648 break;
5649 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005650
Alexey Frunze06a46c42016-07-19 15:00:40 -07005651 bool generate_null_check = false;
5652 switch (load_kind) {
5653 case HLoadClass::LoadKind::kReferrersClass: {
5654 DCHECK(!cls->CanCallRuntime());
5655 DCHECK(!cls->MustGenerateClinitCheck());
5656 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5657 GenerateGcRootFieldLoad(cls,
5658 out_loc,
5659 base_or_current_method_reg,
5660 ArtMethod::DeclaringClassOffset().Int32Value());
5661 break;
5662 }
5663 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005664 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005665 __ LoadLiteral(out,
5666 base_or_current_method_reg,
5667 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5668 cls->GetTypeIndex()));
5669 break;
5670 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005671 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005672 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5673 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005674 bool reordering = __ SetReorder(false);
5675 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5676 __ Addiu(out, out, /* placeholder */ 0x5678);
5677 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005678 break;
5679 }
5680 case HLoadClass::LoadKind::kBootImageAddress: {
5681 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005682 uint32_t address = dchecked_integral_cast<uint32_t>(
5683 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5684 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005685 __ LoadLiteral(out,
5686 base_or_current_method_reg,
5687 codegen_->DeduplicateBootImageAddressLiteral(address));
5688 break;
5689 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005690 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005691 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005692 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005693 bool reordering = __ SetReorder(false);
5694 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5695 __ LoadFromOffset(kLoadWord, out, out, /* placeholder */ 0x5678);
5696 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005697 generate_null_check = true;
5698 break;
5699 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005700 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08005701 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
5702 cls->GetTypeIndex(),
5703 cls->GetClass());
5704 bool reordering = __ SetReorder(false);
5705 __ Bind(&info->high_label);
5706 __ Lui(out, /* placeholder */ 0x1234);
5707 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
5708 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005709 break;
5710 }
Vladimir Marko41559982017-01-06 14:04:23 +00005711 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005712 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005713 LOG(FATAL) << "UNREACHABLE";
5714 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005715 }
5716
5717 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5718 DCHECK(cls->CanCallRuntime());
5719 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5720 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5721 codegen_->AddSlowPath(slow_path);
5722 if (generate_null_check) {
5723 __ Beqz(out, slow_path->GetEntryLabel());
5724 }
5725 if (cls->MustGenerateClinitCheck()) {
5726 GenerateClassInitializationCheck(slow_path, out);
5727 } else {
5728 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005729 }
5730 }
5731}
5732
5733static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005734 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005735}
5736
5737void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5738 LocationSummary* locations =
5739 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5740 locations->SetOut(Location::RequiresRegister());
5741}
5742
5743void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5744 Register out = load->GetLocations()->Out().AsRegister<Register>();
5745 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5746}
5747
5748void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5749 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5750}
5751
5752void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5753 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5754}
5755
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005756void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005757 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005758 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005759 HLoadString::LoadKind load_kind = load->GetLoadKind();
5760 switch (load_kind) {
5761 // We need an extra register for PC-relative literals on R2.
5762 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5763 case HLoadString::LoadKind::kBootImageAddress:
5764 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005765 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005766 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5767 break;
5768 }
5769 FALLTHROUGH_INTENDED;
5770 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005771 case HLoadString::LoadKind::kDexCacheViaMethod:
5772 locations->SetInAt(0, Location::RequiresRegister());
5773 break;
5774 default:
5775 break;
5776 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005777 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5778 InvokeRuntimeCallingConvention calling_convention;
5779 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5780 } else {
5781 locations->SetOut(Location::RequiresRegister());
5782 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005783}
5784
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005785// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5786// move.
5787void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005788 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005789 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005790 Location out_loc = locations->Out();
5791 Register out = out_loc.AsRegister<Register>();
5792 Register base_or_current_method_reg;
5793 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5794 switch (load_kind) {
5795 // We need an extra register for PC-relative literals on R2.
5796 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5797 case HLoadString::LoadKind::kBootImageAddress:
5798 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005799 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005800 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5801 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005802 default:
5803 base_or_current_method_reg = ZERO;
5804 break;
5805 }
5806
5807 switch (load_kind) {
5808 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005809 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005810 __ LoadLiteral(out,
5811 base_or_current_method_reg,
5812 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5813 load->GetStringIndex()));
5814 return; // No dex cache slow path.
5815 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005816 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005817 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005818 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005819 bool reordering = __ SetReorder(false);
5820 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5821 __ Addiu(out, out, /* placeholder */ 0x5678);
5822 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005823 return; // No dex cache slow path.
5824 }
5825 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005826 uint32_t address = dchecked_integral_cast<uint32_t>(
5827 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5828 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005829 __ LoadLiteral(out,
5830 base_or_current_method_reg,
5831 codegen_->DeduplicateBootImageAddressLiteral(address));
5832 return; // No dex cache slow path.
5833 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005834 case HLoadString::LoadKind::kBssEntry: {
5835 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5836 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005837 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08005838 bool reordering = __ SetReorder(false);
5839 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
5840 __ LoadFromOffset(kLoadWord, out, out, /* placeholder */ 0x5678);
5841 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005842 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5843 codegen_->AddSlowPath(slow_path);
5844 __ Beqz(out, slow_path->GetEntryLabel());
5845 __ Bind(slow_path->GetExitLabel());
5846 return;
5847 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005848 case HLoadString::LoadKind::kJitTableAddress: {
5849 CodeGeneratorMIPS::JitPatchInfo* info =
5850 codegen_->NewJitRootStringPatch(load->GetDexFile(),
5851 load->GetStringIndex(),
5852 load->GetString());
5853 bool reordering = __ SetReorder(false);
5854 __ Bind(&info->high_label);
5855 __ Lui(out, /* placeholder */ 0x1234);
5856 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
5857 __ SetReorder(reordering);
5858 return;
5859 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005860 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005861 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005862 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005863
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005864 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005865 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5866 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005867 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005868 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5869 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005870}
5871
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005872void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5873 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5874 locations->SetOut(Location::ConstantLocation(constant));
5875}
5876
5877void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5878 // Will be generated at use site.
5879}
5880
5881void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5882 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005884 InvokeRuntimeCallingConvention calling_convention;
5885 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5886}
5887
5888void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5889 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005890 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005891 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5892 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005893 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005894 }
5895 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5896}
5897
5898void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5899 LocationSummary* locations =
5900 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5901 switch (mul->GetResultType()) {
5902 case Primitive::kPrimInt:
5903 case Primitive::kPrimLong:
5904 locations->SetInAt(0, Location::RequiresRegister());
5905 locations->SetInAt(1, Location::RequiresRegister());
5906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5907 break;
5908
5909 case Primitive::kPrimFloat:
5910 case Primitive::kPrimDouble:
5911 locations->SetInAt(0, Location::RequiresFpuRegister());
5912 locations->SetInAt(1, Location::RequiresFpuRegister());
5913 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5914 break;
5915
5916 default:
5917 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5918 }
5919}
5920
5921void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5922 Primitive::Type type = instruction->GetType();
5923 LocationSummary* locations = instruction->GetLocations();
5924 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5925
5926 switch (type) {
5927 case Primitive::kPrimInt: {
5928 Register dst = locations->Out().AsRegister<Register>();
5929 Register lhs = locations->InAt(0).AsRegister<Register>();
5930 Register rhs = locations->InAt(1).AsRegister<Register>();
5931
5932 if (isR6) {
5933 __ MulR6(dst, lhs, rhs);
5934 } else {
5935 __ MulR2(dst, lhs, rhs);
5936 }
5937 break;
5938 }
5939 case Primitive::kPrimLong: {
5940 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5941 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5942 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5943 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5944 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5945 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5946
5947 // Extra checks to protect caused by the existance of A1_A2.
5948 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5949 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5950 DCHECK_NE(dst_high, lhs_low);
5951 DCHECK_NE(dst_high, rhs_low);
5952
5953 // A_B * C_D
5954 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5955 // dst_lo: [ low(B*D) ]
5956 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5957
5958 if (isR6) {
5959 __ MulR6(TMP, lhs_high, rhs_low);
5960 __ MulR6(dst_high, lhs_low, rhs_high);
5961 __ Addu(dst_high, dst_high, TMP);
5962 __ MuhuR6(TMP, lhs_low, rhs_low);
5963 __ Addu(dst_high, dst_high, TMP);
5964 __ MulR6(dst_low, lhs_low, rhs_low);
5965 } else {
5966 __ MulR2(TMP, lhs_high, rhs_low);
5967 __ MulR2(dst_high, lhs_low, rhs_high);
5968 __ Addu(dst_high, dst_high, TMP);
5969 __ MultuR2(lhs_low, rhs_low);
5970 __ Mfhi(TMP);
5971 __ Addu(dst_high, dst_high, TMP);
5972 __ Mflo(dst_low);
5973 }
5974 break;
5975 }
5976 case Primitive::kPrimFloat:
5977 case Primitive::kPrimDouble: {
5978 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5979 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5980 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5981 if (type == Primitive::kPrimFloat) {
5982 __ MulS(dst, lhs, rhs);
5983 } else {
5984 __ MulD(dst, lhs, rhs);
5985 }
5986 break;
5987 }
5988 default:
5989 LOG(FATAL) << "Unexpected mul type " << type;
5990 }
5991}
5992
5993void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5994 LocationSummary* locations =
5995 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5996 switch (neg->GetResultType()) {
5997 case Primitive::kPrimInt:
5998 case Primitive::kPrimLong:
5999 locations->SetInAt(0, Location::RequiresRegister());
6000 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6001 break;
6002
6003 case Primitive::kPrimFloat:
6004 case Primitive::kPrimDouble:
6005 locations->SetInAt(0, Location::RequiresFpuRegister());
6006 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6007 break;
6008
6009 default:
6010 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6011 }
6012}
6013
6014void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
6015 Primitive::Type type = instruction->GetType();
6016 LocationSummary* locations = instruction->GetLocations();
6017
6018 switch (type) {
6019 case Primitive::kPrimInt: {
6020 Register dst = locations->Out().AsRegister<Register>();
6021 Register src = locations->InAt(0).AsRegister<Register>();
6022 __ Subu(dst, ZERO, src);
6023 break;
6024 }
6025 case Primitive::kPrimLong: {
6026 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6027 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6028 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6029 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6030 __ Subu(dst_low, ZERO, src_low);
6031 __ Sltu(TMP, ZERO, dst_low);
6032 __ Subu(dst_high, ZERO, src_high);
6033 __ Subu(dst_high, dst_high, TMP);
6034 break;
6035 }
6036 case Primitive::kPrimFloat:
6037 case Primitive::kPrimDouble: {
6038 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6039 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6040 if (type == Primitive::kPrimFloat) {
6041 __ NegS(dst, src);
6042 } else {
6043 __ NegD(dst, src);
6044 }
6045 break;
6046 }
6047 default:
6048 LOG(FATAL) << "Unexpected neg type " << type;
6049 }
6050}
6051
6052void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
6053 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006055 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006056 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006057 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6058 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006059}
6060
6061void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006062 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
6063 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006064}
6065
6066void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
6067 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006069 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006070 if (instruction->IsStringAlloc()) {
6071 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6072 } else {
6073 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006074 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006075 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6076}
6077
6078void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00006079 if (instruction->IsStringAlloc()) {
6080 // String is allocated through StringFactory. Call NewEmptyString entry point.
6081 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006082 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006083 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6084 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6085 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006086 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006087 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6088 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006089 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006090 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006091 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006092}
6093
6094void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6095 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6096 locations->SetInAt(0, Location::RequiresRegister());
6097 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6098}
6099
6100void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6101 Primitive::Type type = instruction->GetType();
6102 LocationSummary* locations = instruction->GetLocations();
6103
6104 switch (type) {
6105 case Primitive::kPrimInt: {
6106 Register dst = locations->Out().AsRegister<Register>();
6107 Register src = locations->InAt(0).AsRegister<Register>();
6108 __ Nor(dst, src, ZERO);
6109 break;
6110 }
6111
6112 case Primitive::kPrimLong: {
6113 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6114 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6115 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6116 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6117 __ Nor(dst_high, src_high, ZERO);
6118 __ Nor(dst_low, src_low, ZERO);
6119 break;
6120 }
6121
6122 default:
6123 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6124 }
6125}
6126
6127void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6128 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6129 locations->SetInAt(0, Location::RequiresRegister());
6130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6131}
6132
6133void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6134 LocationSummary* locations = instruction->GetLocations();
6135 __ Xori(locations->Out().AsRegister<Register>(),
6136 locations->InAt(0).AsRegister<Register>(),
6137 1);
6138}
6139
6140void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006141 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6142 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006143}
6144
Calin Juravle2ae48182016-03-16 14:05:09 +00006145void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6146 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006147 return;
6148 }
6149 Location obj = instruction->GetLocations()->InAt(0);
6150
6151 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006152 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153}
6154
Calin Juravle2ae48182016-03-16 14:05:09 +00006155void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006156 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006157 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006158
6159 Location obj = instruction->GetLocations()->InAt(0);
6160
6161 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6162}
6163
6164void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006165 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006166}
6167
6168void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6169 HandleBinaryOp(instruction);
6170}
6171
6172void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6173 HandleBinaryOp(instruction);
6174}
6175
6176void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6177 LOG(FATAL) << "Unreachable";
6178}
6179
6180void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6181 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6182}
6183
6184void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6185 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6186 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6187 if (location.IsStackSlot()) {
6188 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6189 } else if (location.IsDoubleStackSlot()) {
6190 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6191 }
6192 locations->SetOut(location);
6193}
6194
6195void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6196 ATTRIBUTE_UNUSED) {
6197 // Nothing to do, the parameter is already at its location.
6198}
6199
6200void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6201 LocationSummary* locations =
6202 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6203 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6204}
6205
6206void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6207 ATTRIBUTE_UNUSED) {
6208 // Nothing to do, the method is already at its location.
6209}
6210
6211void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006213 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006214 locations->SetInAt(i, Location::Any());
6215 }
6216 locations->SetOut(Location::Any());
6217}
6218
6219void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6220 LOG(FATAL) << "Unreachable";
6221}
6222
6223void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6224 Primitive::Type type = rem->GetResultType();
6225 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006226 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6228
6229 switch (type) {
6230 case Primitive::kPrimInt:
6231 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006232 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6234 break;
6235
6236 case Primitive::kPrimLong: {
6237 InvokeRuntimeCallingConvention calling_convention;
6238 locations->SetInAt(0, Location::RegisterPairLocation(
6239 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6240 locations->SetInAt(1, Location::RegisterPairLocation(
6241 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6242 locations->SetOut(calling_convention.GetReturnLocation(type));
6243 break;
6244 }
6245
6246 case Primitive::kPrimFloat:
6247 case Primitive::kPrimDouble: {
6248 InvokeRuntimeCallingConvention calling_convention;
6249 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6250 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6251 locations->SetOut(calling_convention.GetReturnLocation(type));
6252 break;
6253 }
6254
6255 default:
6256 LOG(FATAL) << "Unexpected rem type " << type;
6257 }
6258}
6259
6260void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6261 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006262
6263 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006264 case Primitive::kPrimInt:
6265 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006266 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006267 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006268 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006269 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6270 break;
6271 }
6272 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006273 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006274 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006275 break;
6276 }
6277 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006278 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006279 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006280 break;
6281 }
6282 default:
6283 LOG(FATAL) << "Unexpected rem type " << type;
6284 }
6285}
6286
6287void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6288 memory_barrier->SetLocations(nullptr);
6289}
6290
6291void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6292 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6293}
6294
6295void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6296 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6297 Primitive::Type return_type = ret->InputAt(0)->GetType();
6298 locations->SetInAt(0, MipsReturnLocation(return_type));
6299}
6300
6301void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6302 codegen_->GenerateFrameExit();
6303}
6304
6305void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6306 ret->SetLocations(nullptr);
6307}
6308
6309void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6310 codegen_->GenerateFrameExit();
6311}
6312
Alexey Frunze92d90602015-12-18 18:16:36 -08006313void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6314 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006315}
6316
Alexey Frunze92d90602015-12-18 18:16:36 -08006317void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6318 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006319}
6320
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006321void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6322 HandleShift(shl);
6323}
6324
6325void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6326 HandleShift(shl);
6327}
6328
6329void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6330 HandleShift(shr);
6331}
6332
6333void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6334 HandleShift(shr);
6335}
6336
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006337void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6338 HandleBinaryOp(instruction);
6339}
6340
6341void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6342 HandleBinaryOp(instruction);
6343}
6344
6345void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6346 HandleFieldGet(instruction, instruction->GetFieldInfo());
6347}
6348
6349void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6350 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6351}
6352
6353void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6354 HandleFieldSet(instruction, instruction->GetFieldInfo());
6355}
6356
6357void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006358 HandleFieldSet(instruction,
6359 instruction->GetFieldInfo(),
6360 instruction->GetDexPc(),
6361 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006362}
6363
6364void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6365 HUnresolvedInstanceFieldGet* instruction) {
6366 FieldAccessCallingConventionMIPS calling_convention;
6367 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6368 instruction->GetFieldType(),
6369 calling_convention);
6370}
6371
6372void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6373 HUnresolvedInstanceFieldGet* instruction) {
6374 FieldAccessCallingConventionMIPS calling_convention;
6375 codegen_->GenerateUnresolvedFieldAccess(instruction,
6376 instruction->GetFieldType(),
6377 instruction->GetFieldIndex(),
6378 instruction->GetDexPc(),
6379 calling_convention);
6380}
6381
6382void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6383 HUnresolvedInstanceFieldSet* instruction) {
6384 FieldAccessCallingConventionMIPS calling_convention;
6385 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6386 instruction->GetFieldType(),
6387 calling_convention);
6388}
6389
6390void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6391 HUnresolvedInstanceFieldSet* instruction) {
6392 FieldAccessCallingConventionMIPS calling_convention;
6393 codegen_->GenerateUnresolvedFieldAccess(instruction,
6394 instruction->GetFieldType(),
6395 instruction->GetFieldIndex(),
6396 instruction->GetDexPc(),
6397 calling_convention);
6398}
6399
6400void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6401 HUnresolvedStaticFieldGet* instruction) {
6402 FieldAccessCallingConventionMIPS calling_convention;
6403 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6404 instruction->GetFieldType(),
6405 calling_convention);
6406}
6407
6408void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6409 HUnresolvedStaticFieldGet* instruction) {
6410 FieldAccessCallingConventionMIPS calling_convention;
6411 codegen_->GenerateUnresolvedFieldAccess(instruction,
6412 instruction->GetFieldType(),
6413 instruction->GetFieldIndex(),
6414 instruction->GetDexPc(),
6415 calling_convention);
6416}
6417
6418void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6419 HUnresolvedStaticFieldSet* instruction) {
6420 FieldAccessCallingConventionMIPS calling_convention;
6421 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6422 instruction->GetFieldType(),
6423 calling_convention);
6424}
6425
6426void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6427 HUnresolvedStaticFieldSet* instruction) {
6428 FieldAccessCallingConventionMIPS calling_convention;
6429 codegen_->GenerateUnresolvedFieldAccess(instruction,
6430 instruction->GetFieldType(),
6431 instruction->GetFieldIndex(),
6432 instruction->GetDexPc(),
6433 calling_convention);
6434}
6435
6436void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006437 LocationSummary* locations =
6438 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006439 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440}
6441
6442void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6443 HBasicBlock* block = instruction->GetBlock();
6444 if (block->GetLoopInformation() != nullptr) {
6445 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6446 // The back edge will generate the suspend check.
6447 return;
6448 }
6449 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6450 // The goto will generate the suspend check.
6451 return;
6452 }
6453 GenerateSuspendCheck(instruction, nullptr);
6454}
6455
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006456void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6457 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006458 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006459 InvokeRuntimeCallingConvention calling_convention;
6460 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6461}
6462
6463void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006464 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006465 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6466}
6467
6468void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6469 Primitive::Type input_type = conversion->GetInputType();
6470 Primitive::Type result_type = conversion->GetResultType();
6471 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006472 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006473
6474 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6475 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6476 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6477 }
6478
6479 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006480 if (!isR6 &&
6481 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6482 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006483 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006484 }
6485
6486 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6487
6488 if (call_kind == LocationSummary::kNoCall) {
6489 if (Primitive::IsFloatingPointType(input_type)) {
6490 locations->SetInAt(0, Location::RequiresFpuRegister());
6491 } else {
6492 locations->SetInAt(0, Location::RequiresRegister());
6493 }
6494
6495 if (Primitive::IsFloatingPointType(result_type)) {
6496 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6497 } else {
6498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6499 }
6500 } else {
6501 InvokeRuntimeCallingConvention calling_convention;
6502
6503 if (Primitive::IsFloatingPointType(input_type)) {
6504 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6505 } else {
6506 DCHECK_EQ(input_type, Primitive::kPrimLong);
6507 locations->SetInAt(0, Location::RegisterPairLocation(
6508 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6509 }
6510
6511 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6512 }
6513}
6514
6515void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6516 LocationSummary* locations = conversion->GetLocations();
6517 Primitive::Type result_type = conversion->GetResultType();
6518 Primitive::Type input_type = conversion->GetInputType();
6519 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006520 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006521
6522 DCHECK_NE(input_type, result_type);
6523
6524 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6525 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6526 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6527 Register src = locations->InAt(0).AsRegister<Register>();
6528
Alexey Frunzea871ef12016-06-27 15:20:11 -07006529 if (dst_low != src) {
6530 __ Move(dst_low, src);
6531 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006532 __ Sra(dst_high, src, 31);
6533 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6534 Register dst = locations->Out().AsRegister<Register>();
6535 Register src = (input_type == Primitive::kPrimLong)
6536 ? locations->InAt(0).AsRegisterPairLow<Register>()
6537 : locations->InAt(0).AsRegister<Register>();
6538
6539 switch (result_type) {
6540 case Primitive::kPrimChar:
6541 __ Andi(dst, src, 0xFFFF);
6542 break;
6543 case Primitive::kPrimByte:
6544 if (has_sign_extension) {
6545 __ Seb(dst, src);
6546 } else {
6547 __ Sll(dst, src, 24);
6548 __ Sra(dst, dst, 24);
6549 }
6550 break;
6551 case Primitive::kPrimShort:
6552 if (has_sign_extension) {
6553 __ Seh(dst, src);
6554 } else {
6555 __ Sll(dst, src, 16);
6556 __ Sra(dst, dst, 16);
6557 }
6558 break;
6559 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006560 if (dst != src) {
6561 __ Move(dst, src);
6562 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006563 break;
6564
6565 default:
6566 LOG(FATAL) << "Unexpected type conversion from " << input_type
6567 << " to " << result_type;
6568 }
6569 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006570 if (input_type == Primitive::kPrimLong) {
6571 if (isR6) {
6572 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6573 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6574 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6575 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6576 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6577 __ Mtc1(src_low, FTMP);
6578 __ Mthc1(src_high, FTMP);
6579 if (result_type == Primitive::kPrimFloat) {
6580 __ Cvtsl(dst, FTMP);
6581 } else {
6582 __ Cvtdl(dst, FTMP);
6583 }
6584 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006585 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6586 : kQuickL2d;
6587 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006588 if (result_type == Primitive::kPrimFloat) {
6589 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6590 } else {
6591 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6592 }
6593 }
6594 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006595 Register src = locations->InAt(0).AsRegister<Register>();
6596 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6597 __ Mtc1(src, FTMP);
6598 if (result_type == Primitive::kPrimFloat) {
6599 __ Cvtsw(dst, FTMP);
6600 } else {
6601 __ Cvtdw(dst, FTMP);
6602 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006603 }
6604 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6605 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006606 if (result_type == Primitive::kPrimLong) {
6607 if (isR6) {
6608 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6609 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6610 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6611 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6612 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6613 MipsLabel truncate;
6614 MipsLabel done;
6615
6616 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6617 // value when the input is either a NaN or is outside of the range of the output type
6618 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6619 // the same result.
6620 //
6621 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6622 // value of the output type if the input is outside of the range after the truncation or
6623 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6624 // results. This matches the desired float/double-to-int/long conversion exactly.
6625 //
6626 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6627 //
6628 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6629 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6630 // even though it must be NAN2008=1 on R6.
6631 //
6632 // The code takes care of the different behaviors by first comparing the input to the
6633 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6634 // If the input is greater than or equal to the minimum, it procedes to the truncate
6635 // instruction, which will handle such an input the same way irrespective of NAN2008.
6636 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6637 // in order to return either zero or the minimum value.
6638 //
6639 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6640 // truncate instruction for MIPS64R6.
6641 if (input_type == Primitive::kPrimFloat) {
6642 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6643 __ LoadConst32(TMP, min_val);
6644 __ Mtc1(TMP, FTMP);
6645 __ CmpLeS(FTMP, FTMP, src);
6646 } else {
6647 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6648 __ LoadConst32(TMP, High32Bits(min_val));
6649 __ Mtc1(ZERO, FTMP);
6650 __ Mthc1(TMP, FTMP);
6651 __ CmpLeD(FTMP, FTMP, src);
6652 }
6653
6654 __ Bc1nez(FTMP, &truncate);
6655
6656 if (input_type == Primitive::kPrimFloat) {
6657 __ CmpEqS(FTMP, src, src);
6658 } else {
6659 __ CmpEqD(FTMP, src, src);
6660 }
6661 __ Move(dst_low, ZERO);
6662 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6663 __ Mfc1(TMP, FTMP);
6664 __ And(dst_high, dst_high, TMP);
6665
6666 __ B(&done);
6667
6668 __ Bind(&truncate);
6669
6670 if (input_type == Primitive::kPrimFloat) {
6671 __ TruncLS(FTMP, src);
6672 } else {
6673 __ TruncLD(FTMP, src);
6674 }
6675 __ Mfc1(dst_low, FTMP);
6676 __ Mfhc1(dst_high, FTMP);
6677
6678 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006679 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006680 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6681 : kQuickD2l;
6682 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006683 if (input_type == Primitive::kPrimFloat) {
6684 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6685 } else {
6686 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6687 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006688 }
6689 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006690 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6691 Register dst = locations->Out().AsRegister<Register>();
6692 MipsLabel truncate;
6693 MipsLabel done;
6694
6695 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6696 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6697 // even though it must be NAN2008=1 on R6.
6698 //
6699 // For details see the large comment above for the truncation of float/double to long on R6.
6700 //
6701 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6702 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006703 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006704 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6705 __ LoadConst32(TMP, min_val);
6706 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006708 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6709 __ LoadConst32(TMP, High32Bits(min_val));
6710 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006711 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006712 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006713
6714 if (isR6) {
6715 if (input_type == Primitive::kPrimFloat) {
6716 __ CmpLeS(FTMP, FTMP, src);
6717 } else {
6718 __ CmpLeD(FTMP, FTMP, src);
6719 }
6720 __ Bc1nez(FTMP, &truncate);
6721
6722 if (input_type == Primitive::kPrimFloat) {
6723 __ CmpEqS(FTMP, src, src);
6724 } else {
6725 __ CmpEqD(FTMP, src, src);
6726 }
6727 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6728 __ Mfc1(TMP, FTMP);
6729 __ And(dst, dst, TMP);
6730 } else {
6731 if (input_type == Primitive::kPrimFloat) {
6732 __ ColeS(0, FTMP, src);
6733 } else {
6734 __ ColeD(0, FTMP, src);
6735 }
6736 __ Bc1t(0, &truncate);
6737
6738 if (input_type == Primitive::kPrimFloat) {
6739 __ CeqS(0, src, src);
6740 } else {
6741 __ CeqD(0, src, src);
6742 }
6743 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6744 __ Movf(dst, ZERO, 0);
6745 }
6746
6747 __ B(&done);
6748
6749 __ Bind(&truncate);
6750
6751 if (input_type == Primitive::kPrimFloat) {
6752 __ TruncWS(FTMP, src);
6753 } else {
6754 __ TruncWD(FTMP, src);
6755 }
6756 __ Mfc1(dst, FTMP);
6757
6758 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006759 }
6760 } else if (Primitive::IsFloatingPointType(result_type) &&
6761 Primitive::IsFloatingPointType(input_type)) {
6762 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6763 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6764 if (result_type == Primitive::kPrimFloat) {
6765 __ Cvtsd(dst, src);
6766 } else {
6767 __ Cvtds(dst, src);
6768 }
6769 } else {
6770 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6771 << " to " << result_type;
6772 }
6773}
6774
6775void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6776 HandleShift(ushr);
6777}
6778
6779void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6780 HandleShift(ushr);
6781}
6782
6783void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6784 HandleBinaryOp(instruction);
6785}
6786
6787void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6788 HandleBinaryOp(instruction);
6789}
6790
6791void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6792 // Nothing to do, this should be removed during prepare for register allocator.
6793 LOG(FATAL) << "Unreachable";
6794}
6795
6796void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6797 // Nothing to do, this should be removed during prepare for register allocator.
6798 LOG(FATAL) << "Unreachable";
6799}
6800
6801void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006802 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006803}
6804
6805void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006806 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006807}
6808
6809void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006810 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006811}
6812
6813void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006814 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006815}
6816
6817void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006818 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006819}
6820
6821void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006822 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006823}
6824
6825void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006826 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006827}
6828
6829void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006830 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006831}
6832
6833void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006834 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006835}
6836
6837void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006838 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006839}
6840
6841void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006842 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006843}
6844
6845void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006846 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006847}
6848
6849void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006850 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006851}
6852
6853void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006854 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006855}
6856
6857void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006858 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006859}
6860
6861void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006862 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006863}
6864
6865void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006866 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006867}
6868
6869void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006870 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006871}
6872
6873void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006874 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006875}
6876
6877void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006878 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006879}
6880
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006881void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6882 LocationSummary* locations =
6883 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6884 locations->SetInAt(0, Location::RequiresRegister());
6885}
6886
Alexey Frunze96b66822016-09-10 02:32:44 -07006887void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6888 int32_t lower_bound,
6889 uint32_t num_entries,
6890 HBasicBlock* switch_block,
6891 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006892 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006893 Register temp_reg = TMP;
6894 __ Addiu32(temp_reg, value_reg, -lower_bound);
6895 // Jump to default if index is negative
6896 // Note: We don't check the case that index is positive while value < lower_bound, because in
6897 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6898 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6899
Alexey Frunze96b66822016-09-10 02:32:44 -07006900 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006901 // Jump to successors[0] if value == lower_bound.
6902 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6903 int32_t last_index = 0;
6904 for (; num_entries - last_index > 2; last_index += 2) {
6905 __ Addiu(temp_reg, temp_reg, -2);
6906 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6907 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6908 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6909 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6910 }
6911 if (num_entries - last_index == 2) {
6912 // The last missing case_value.
6913 __ Addiu(temp_reg, temp_reg, -1);
6914 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006915 }
6916
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006917 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006918 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006919 __ B(codegen_->GetLabelOf(default_block));
6920 }
6921}
6922
Alexey Frunze96b66822016-09-10 02:32:44 -07006923void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6924 Register constant_area,
6925 int32_t lower_bound,
6926 uint32_t num_entries,
6927 HBasicBlock* switch_block,
6928 HBasicBlock* default_block) {
6929 // Create a jump table.
6930 std::vector<MipsLabel*> labels(num_entries);
6931 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6932 for (uint32_t i = 0; i < num_entries; i++) {
6933 labels[i] = codegen_->GetLabelOf(successors[i]);
6934 }
6935 JumpTable* table = __ CreateJumpTable(std::move(labels));
6936
6937 // Is the value in range?
6938 __ Addiu32(TMP, value_reg, -lower_bound);
6939 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6940 __ Sltiu(AT, TMP, num_entries);
6941 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6942 } else {
6943 __ LoadConst32(AT, num_entries);
6944 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6945 }
6946
6947 // We are in the range of the table.
6948 // Load the target address from the jump table, indexing by the value.
6949 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6950 __ Sll(TMP, TMP, 2);
6951 __ Addu(TMP, TMP, AT);
6952 __ Lw(TMP, TMP, 0);
6953 // Compute the absolute target address by adding the table start address
6954 // (the table contains offsets to targets relative to its start).
6955 __ Addu(TMP, TMP, AT);
6956 // And jump.
6957 __ Jr(TMP);
6958 __ NopIfNoReordering();
6959}
6960
6961void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6962 int32_t lower_bound = switch_instr->GetStartValue();
6963 uint32_t num_entries = switch_instr->GetNumEntries();
6964 LocationSummary* locations = switch_instr->GetLocations();
6965 Register value_reg = locations->InAt(0).AsRegister<Register>();
6966 HBasicBlock* switch_block = switch_instr->GetBlock();
6967 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6968
6969 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6970 num_entries > kPackedSwitchJumpTableThreshold) {
6971 // R6 uses PC-relative addressing to access the jump table.
6972 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6973 // the jump table and it is implemented by changing HPackedSwitch to
6974 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6975 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6976 GenTableBasedPackedSwitch(value_reg,
6977 ZERO,
6978 lower_bound,
6979 num_entries,
6980 switch_block,
6981 default_block);
6982 } else {
6983 GenPackedSwitchWithCompares(value_reg,
6984 lower_bound,
6985 num_entries,
6986 switch_block,
6987 default_block);
6988 }
6989}
6990
6991void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6992 LocationSummary* locations =
6993 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6994 locations->SetInAt(0, Location::RequiresRegister());
6995 // Constant area pointer (HMipsComputeBaseMethodAddress).
6996 locations->SetInAt(1, Location::RequiresRegister());
6997}
6998
6999void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
7000 int32_t lower_bound = switch_instr->GetStartValue();
7001 uint32_t num_entries = switch_instr->GetNumEntries();
7002 LocationSummary* locations = switch_instr->GetLocations();
7003 Register value_reg = locations->InAt(0).AsRegister<Register>();
7004 Register constant_area = locations->InAt(1).AsRegister<Register>();
7005 HBasicBlock* switch_block = switch_instr->GetBlock();
7006 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7007
7008 // This is an R2-only path. HPackedSwitch has been changed to
7009 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
7010 // required to address the jump table relative to PC.
7011 GenTableBasedPackedSwitch(value_reg,
7012 constant_area,
7013 lower_bound,
7014 num_entries,
7015 switch_block,
7016 default_block);
7017}
7018
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007019void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
7020 HMipsComputeBaseMethodAddress* insn) {
7021 LocationSummary* locations =
7022 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7023 locations->SetOut(Location::RequiresRegister());
7024}
7025
7026void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
7027 HMipsComputeBaseMethodAddress* insn) {
7028 LocationSummary* locations = insn->GetLocations();
7029 Register reg = locations->Out().AsRegister<Register>();
7030
7031 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7032
7033 // Generate a dummy PC-relative call to obtain PC.
7034 __ Nal();
7035 // Grab the return address off RA.
7036 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007037 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007038
7039 // Remember this offset (the obtained PC value) for later use with constant area.
7040 __ BindPcRelBaseLabel();
7041}
7042
7043void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7045 locations->SetOut(Location::RequiresRegister());
7046}
7047
7048void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
7049 Register reg = base->GetLocations()->Out().AsRegister<Register>();
7050 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7051 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007052 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
7053 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007054 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007055 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
7056 __ Addiu(reg, reg, /* placeholder */ 0x5678);
7057 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007058}
7059
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007060void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7061 // The trampoline uses the same calling convention as dex calling conventions,
7062 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
7063 // the method_idx.
7064 HandleInvoke(invoke);
7065}
7066
7067void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
7068 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
7069}
7070
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007071void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7072 LocationSummary* locations =
7073 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7074 locations->SetInAt(0, Location::RequiresRegister());
7075 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007076}
7077
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007078void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
7079 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007080 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007081 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007082 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007083 __ LoadFromOffset(kLoadWord,
7084 locations->Out().AsRegister<Register>(),
7085 locations->InAt(0).AsRegister<Register>(),
7086 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007087 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007088 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007089 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007090 __ LoadFromOffset(kLoadWord,
7091 locations->Out().AsRegister<Register>(),
7092 locations->InAt(0).AsRegister<Register>(),
7093 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007094 __ LoadFromOffset(kLoadWord,
7095 locations->Out().AsRegister<Register>(),
7096 locations->Out().AsRegister<Register>(),
7097 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007098 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007099}
7100
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007101#undef __
7102#undef QUICK_ENTRY_POINT
7103
7104} // namespace mips
7105} // namespace art