blob: aa030b279c2a39c0aab32a045611336d48a4e293 [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:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800394 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
395 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
398 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200399 uint32_t dex_pc = instruction_->GetDexPc();
400 DCHECK(instruction_->IsCheckCast()
401 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
402 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
403
404 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800405 if (!is_fatal_) {
406 SaveLiveRegisters(codegen, locations);
407 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200408
409 // We're moving two locations to locations that could overlap, so we need a parallel
410 // move resolver.
411 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800412 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
414 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800415 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200416 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
417 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100419 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800420 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 Primitive::Type ret_type = instruction_->GetType();
422 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
423 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200424 } else {
425 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800426 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
427 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200428 }
429
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800430 if (!is_fatal_) {
431 RestoreLiveRegisters(codegen, locations);
432 __ B(GetExitLabel());
433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 }
435
436 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
437
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800438 bool IsFatal() const OVERRIDE { return is_fatal_; }
439
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800441 const bool is_fatal_;
442
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
444};
445
446class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
447 public:
Aart Bik42249c32016-01-07 15:33:50 -0800448 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000449 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450
451 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800452 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200453 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100454 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000455 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200456 }
457
458 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
459
460 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200461 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
462};
463
Alexey Frunze15958152017-02-09 19:08:30 -0800464class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
465 public:
466 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
467
468 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
469 LocationSummary* locations = instruction_->GetLocations();
470 __ Bind(GetEntryLabel());
471 SaveLiveRegisters(codegen, locations);
472
473 InvokeRuntimeCallingConvention calling_convention;
474 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
475 parallel_move.AddMove(
476 locations->InAt(0),
477 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
478 Primitive::kPrimNot,
479 nullptr);
480 parallel_move.AddMove(
481 locations->InAt(1),
482 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
483 Primitive::kPrimInt,
484 nullptr);
485 parallel_move.AddMove(
486 locations->InAt(2),
487 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
488 Primitive::kPrimNot,
489 nullptr);
490 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
491
492 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
493 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
494 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
495 RestoreLiveRegisters(codegen, locations);
496 __ B(GetExitLabel());
497 }
498
499 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
500
501 private:
502 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
503};
504
505// Slow path marking an object reference `ref` during a read
506// barrier. The field `obj.field` in the object `obj` holding this
507// reference does not get updated by this slow path after marking (see
508// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
509//
510// This means that after the execution of this slow path, `ref` will
511// always be up-to-date, but `obj.field` may not; i.e., after the
512// flip, `ref` will be a to-space reference, but `obj.field` will
513// probably still be a from-space reference (unless it gets updated by
514// another thread, or if another thread installed another object
515// reference (different from `ref`) in `obj.field`).
516//
517// If `entrypoint` is a valid location it is assumed to already be
518// holding the entrypoint. The case where the entrypoint is passed in
519// is for the GcRoot read barrier.
520class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
521 public:
522 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
523 Location ref,
524 Location entrypoint = Location::NoLocation())
525 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
526 DCHECK(kEmitCompilerReadBarrier);
527 }
528
529 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
530
531 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
532 LocationSummary* locations = instruction_->GetLocations();
533 Register ref_reg = ref_.AsRegister<Register>();
534 DCHECK(locations->CanCall());
535 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
536 DCHECK(instruction_->IsInstanceFieldGet() ||
537 instruction_->IsStaticFieldGet() ||
538 instruction_->IsArrayGet() ||
539 instruction_->IsArraySet() ||
540 instruction_->IsLoadClass() ||
541 instruction_->IsLoadString() ||
542 instruction_->IsInstanceOf() ||
543 instruction_->IsCheckCast() ||
544 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
545 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
546 << "Unexpected instruction in read barrier marking slow path: "
547 << instruction_->DebugName();
548
549 __ Bind(GetEntryLabel());
550 // No need to save live registers; it's taken care of by the
551 // entrypoint. Also, there is no need to update the stack mask,
552 // as this runtime call will not trigger a garbage collection.
553 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
554 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
555 (S2 <= ref_reg && ref_reg <= S7) ||
556 (ref_reg == FP)) << ref_reg;
557 // "Compact" slow path, saving two moves.
558 //
559 // Instead of using the standard runtime calling convention (input
560 // and output in A0 and V0 respectively):
561 //
562 // A0 <- ref
563 // V0 <- ReadBarrierMark(A0)
564 // ref <- V0
565 //
566 // we just use rX (the register containing `ref`) as input and output
567 // of a dedicated entrypoint:
568 //
569 // rX <- ReadBarrierMarkRegX(rX)
570 //
571 if (entrypoint_.IsValid()) {
572 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
573 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
574 __ Jalr(entrypoint_.AsRegister<Register>());
575 __ NopIfNoReordering();
576 } else {
577 int32_t entry_point_offset =
578 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
579 // This runtime call does not require a stack map.
580 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
581 instruction_,
582 this,
583 /* direct */ false);
584 }
585 __ B(GetExitLabel());
586 }
587
588 private:
589 // The location (register) of the marked object reference.
590 const Location ref_;
591
592 // The location of the entrypoint if already loaded.
593 const Location entrypoint_;
594
595 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
596};
597
598// Slow path marking an object reference `ref` during a read barrier,
599// and if needed, atomically updating the field `obj.field` in the
600// object `obj` holding this reference after marking (contrary to
601// ReadBarrierMarkSlowPathMIPS above, which never tries to update
602// `obj.field`).
603//
604// This means that after the execution of this slow path, both `ref`
605// and `obj.field` will be up-to-date; i.e., after the flip, both will
606// hold the same to-space reference (unless another thread installed
607// another object reference (different from `ref`) in `obj.field`).
608class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
609 public:
610 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
611 Location ref,
612 Register obj,
613 Location field_offset,
614 Register temp1)
615 : SlowPathCodeMIPS(instruction),
616 ref_(ref),
617 obj_(obj),
618 field_offset_(field_offset),
619 temp1_(temp1) {
620 DCHECK(kEmitCompilerReadBarrier);
621 }
622
623 const char* GetDescription() const OVERRIDE {
624 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
625 }
626
627 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
628 LocationSummary* locations = instruction_->GetLocations();
629 Register ref_reg = ref_.AsRegister<Register>();
630 DCHECK(locations->CanCall());
631 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
632 // This slow path is only used by the UnsafeCASObject intrinsic.
633 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
634 << "Unexpected instruction in read barrier marking and field updating slow path: "
635 << instruction_->DebugName();
636 DCHECK(instruction_->GetLocations()->Intrinsified());
637 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
638 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
639
640 __ Bind(GetEntryLabel());
641
642 // Save the old reference.
643 // Note that we cannot use AT or TMP to save the old reference, as those
644 // are used by the code that follows, but we need the old reference after
645 // the call to the ReadBarrierMarkRegX entry point.
646 DCHECK_NE(temp1_, AT);
647 DCHECK_NE(temp1_, TMP);
648 __ Move(temp1_, ref_reg);
649
650 // No need to save live registers; it's taken care of by the
651 // entrypoint. Also, there is no need to update the stack mask,
652 // as this runtime call will not trigger a garbage collection.
653 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
654 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
655 (S2 <= ref_reg && ref_reg <= S7) ||
656 (ref_reg == FP)) << ref_reg;
657 // "Compact" slow path, saving two moves.
658 //
659 // Instead of using the standard runtime calling convention (input
660 // and output in A0 and V0 respectively):
661 //
662 // A0 <- ref
663 // V0 <- ReadBarrierMark(A0)
664 // ref <- V0
665 //
666 // we just use rX (the register containing `ref`) as input and output
667 // of a dedicated entrypoint:
668 //
669 // rX <- ReadBarrierMarkRegX(rX)
670 //
671 int32_t entry_point_offset =
672 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
673 // This runtime call does not require a stack map.
674 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
675 instruction_,
676 this,
677 /* direct */ false);
678
679 // If the new reference is different from the old reference,
680 // update the field in the holder (`*(obj_ + field_offset_)`).
681 //
682 // Note that this field could also hold a different object, if
683 // another thread had concurrently changed it. In that case, the
684 // the compare-and-set (CAS) loop below would abort, leaving the
685 // field as-is.
686 MipsLabel done;
687 __ Beq(temp1_, ref_reg, &done);
688
689 // Update the the holder's field atomically. This may fail if
690 // mutator updates before us, but it's OK. This is achieved
691 // using a strong compare-and-set (CAS) operation with relaxed
692 // memory synchronization ordering, where the expected value is
693 // the old reference and the desired value is the new reference.
694
695 // Convenience aliases.
696 Register base = obj_;
697 // The UnsafeCASObject intrinsic uses a register pair as field
698 // offset ("long offset"), of which only the low part contains
699 // data.
700 Register offset = field_offset_.AsRegisterPairLow<Register>();
701 Register expected = temp1_;
702 Register value = ref_reg;
703 Register tmp_ptr = TMP; // Pointer to actual memory.
704 Register tmp = AT; // Value in memory.
705
706 __ Addu(tmp_ptr, base, offset);
707
708 if (kPoisonHeapReferences) {
709 __ PoisonHeapReference(expected);
710 // Do not poison `value` if it is the same register as
711 // `expected`, which has just been poisoned.
712 if (value != expected) {
713 __ PoisonHeapReference(value);
714 }
715 }
716
717 // do {
718 // tmp = [r_ptr] - expected;
719 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
720
721 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
722 MipsLabel loop_head, exit_loop;
723 __ Bind(&loop_head);
724 if (is_r6) {
725 __ LlR6(tmp, tmp_ptr);
726 } else {
727 __ LlR2(tmp, tmp_ptr);
728 }
729 __ Bne(tmp, expected, &exit_loop);
730 __ Move(tmp, value);
731 if (is_r6) {
732 __ ScR6(tmp, tmp_ptr);
733 } else {
734 __ ScR2(tmp, tmp_ptr);
735 }
736 __ Beqz(tmp, &loop_head);
737 __ Bind(&exit_loop);
738
739 if (kPoisonHeapReferences) {
740 __ UnpoisonHeapReference(expected);
741 // Do not unpoison `value` if it is the same register as
742 // `expected`, which has just been unpoisoned.
743 if (value != expected) {
744 __ UnpoisonHeapReference(value);
745 }
746 }
747
748 __ Bind(&done);
749 __ B(GetExitLabel());
750 }
751
752 private:
753 // The location (register) of the marked object reference.
754 const Location ref_;
755 // The register containing the object holding the marked object reference field.
756 const Register obj_;
757 // The location of the offset of the marked reference field within `obj_`.
758 Location field_offset_;
759
760 const Register temp1_;
761
762 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
763};
764
765// Slow path generating a read barrier for a heap reference.
766class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
767 public:
768 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
769 Location out,
770 Location ref,
771 Location obj,
772 uint32_t offset,
773 Location index)
774 : SlowPathCodeMIPS(instruction),
775 out_(out),
776 ref_(ref),
777 obj_(obj),
778 offset_(offset),
779 index_(index) {
780 DCHECK(kEmitCompilerReadBarrier);
781 // If `obj` is equal to `out` or `ref`, it means the initial object
782 // has been overwritten by (or after) the heap object reference load
783 // to be instrumented, e.g.:
784 //
785 // __ LoadFromOffset(kLoadWord, out, out, offset);
786 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
787 //
788 // In that case, we have lost the information about the original
789 // object, and the emitted read barrier cannot work properly.
790 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
791 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
792 }
793
794 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
795 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
796 LocationSummary* locations = instruction_->GetLocations();
797 Register reg_out = out_.AsRegister<Register>();
798 DCHECK(locations->CanCall());
799 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
800 DCHECK(instruction_->IsInstanceFieldGet() ||
801 instruction_->IsStaticFieldGet() ||
802 instruction_->IsArrayGet() ||
803 instruction_->IsInstanceOf() ||
804 instruction_->IsCheckCast() ||
805 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
806 << "Unexpected instruction in read barrier for heap reference slow path: "
807 << instruction_->DebugName();
808
809 __ Bind(GetEntryLabel());
810 SaveLiveRegisters(codegen, locations);
811
812 // We may have to change the index's value, but as `index_` is a
813 // constant member (like other "inputs" of this slow path),
814 // introduce a copy of it, `index`.
815 Location index = index_;
816 if (index_.IsValid()) {
817 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
818 if (instruction_->IsArrayGet()) {
819 // Compute the actual memory offset and store it in `index`.
820 Register index_reg = index_.AsRegister<Register>();
821 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
822 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
823 // We are about to change the value of `index_reg` (see the
824 // calls to art::mips::MipsAssembler::Sll and
825 // art::mips::MipsAssembler::Addiu32 below), but it has
826 // not been saved by the previous call to
827 // art::SlowPathCode::SaveLiveRegisters, as it is a
828 // callee-save register --
829 // art::SlowPathCode::SaveLiveRegisters does not consider
830 // callee-save registers, as it has been designed with the
831 // assumption that callee-save registers are supposed to be
832 // handled by the called function. So, as a callee-save
833 // register, `index_reg` _would_ eventually be saved onto
834 // the stack, but it would be too late: we would have
835 // changed its value earlier. Therefore, we manually save
836 // it here into another freely available register,
837 // `free_reg`, chosen of course among the caller-save
838 // registers (as a callee-save `free_reg` register would
839 // exhibit the same problem).
840 //
841 // Note we could have requested a temporary register from
842 // the register allocator instead; but we prefer not to, as
843 // this is a slow path, and we know we can find a
844 // caller-save register that is available.
845 Register free_reg = FindAvailableCallerSaveRegister(codegen);
846 __ Move(free_reg, index_reg);
847 index_reg = free_reg;
848 index = Location::RegisterLocation(index_reg);
849 } else {
850 // The initial register stored in `index_` has already been
851 // saved in the call to art::SlowPathCode::SaveLiveRegisters
852 // (as it is not a callee-save register), so we can freely
853 // use it.
854 }
855 // Shifting the index value contained in `index_reg` by the scale
856 // factor (2) cannot overflow in practice, as the runtime is
857 // unable to allocate object arrays with a size larger than
858 // 2^26 - 1 (that is, 2^28 - 4 bytes).
859 __ Sll(index_reg, index_reg, TIMES_4);
860 static_assert(
861 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
862 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
863 __ Addiu32(index_reg, index_reg, offset_);
864 } else {
865 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
866 // intrinsics, `index_` is not shifted by a scale factor of 2
867 // (as in the case of ArrayGet), as it is actually an offset
868 // to an object field within an object.
869 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
870 DCHECK(instruction_->GetLocations()->Intrinsified());
871 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
872 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
873 << instruction_->AsInvoke()->GetIntrinsic();
874 DCHECK_EQ(offset_, 0U);
875 DCHECK(index_.IsRegisterPair());
876 // UnsafeGet's offset location is a register pair, the low
877 // part contains the correct offset.
878 index = index_.ToLow();
879 }
880 }
881
882 // We're moving two or three locations to locations that could
883 // overlap, so we need a parallel move resolver.
884 InvokeRuntimeCallingConvention calling_convention;
885 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
886 parallel_move.AddMove(ref_,
887 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
888 Primitive::kPrimNot,
889 nullptr);
890 parallel_move.AddMove(obj_,
891 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
892 Primitive::kPrimNot,
893 nullptr);
894 if (index.IsValid()) {
895 parallel_move.AddMove(index,
896 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
897 Primitive::kPrimInt,
898 nullptr);
899 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
900 } else {
901 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
902 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
903 }
904 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
905 instruction_,
906 instruction_->GetDexPc(),
907 this);
908 CheckEntrypointTypes<
909 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
910 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
911
912 RestoreLiveRegisters(codegen, locations);
913 __ B(GetExitLabel());
914 }
915
916 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
917
918 private:
919 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
920 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
921 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
922 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
923 if (i != ref &&
924 i != obj &&
925 !codegen->IsCoreCalleeSaveRegister(i) &&
926 !codegen->IsBlockedCoreRegister(i)) {
927 return static_cast<Register>(i);
928 }
929 }
930 // We shall never fail to find a free caller-save register, as
931 // there are more than two core caller-save registers on MIPS
932 // (meaning it is possible to find one which is different from
933 // `ref` and `obj`).
934 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
935 LOG(FATAL) << "Could not find a free caller-save register";
936 UNREACHABLE();
937 }
938
939 const Location out_;
940 const Location ref_;
941 const Location obj_;
942 const uint32_t offset_;
943 // An additional location containing an index to an array.
944 // Only used for HArrayGet and the UnsafeGetObject &
945 // UnsafeGetObjectVolatile intrinsics.
946 const Location index_;
947
948 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
949};
950
951// Slow path generating a read barrier for a GC root.
952class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
953 public:
954 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
955 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
956 DCHECK(kEmitCompilerReadBarrier);
957 }
958
959 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
960 LocationSummary* locations = instruction_->GetLocations();
961 Register reg_out = out_.AsRegister<Register>();
962 DCHECK(locations->CanCall());
963 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
964 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
965 << "Unexpected instruction in read barrier for GC root slow path: "
966 << instruction_->DebugName();
967
968 __ Bind(GetEntryLabel());
969 SaveLiveRegisters(codegen, locations);
970
971 InvokeRuntimeCallingConvention calling_convention;
972 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
973 mips_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
974 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
975 instruction_,
976 instruction_->GetDexPc(),
977 this);
978 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
979 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
980
981 RestoreLiveRegisters(codegen, locations);
982 __ B(GetExitLabel());
983 }
984
985 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
986
987 private:
988 const Location out_;
989 const Location root_;
990
991 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
992};
993
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200994CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
995 const MipsInstructionSetFeatures& isa_features,
996 const CompilerOptions& compiler_options,
997 OptimizingCompilerStats* stats)
998 : CodeGenerator(graph,
999 kNumberOfCoreRegisters,
1000 kNumberOfFRegisters,
1001 kNumberOfRegisterPairs,
1002 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1003 arraysize(kCoreCalleeSaves)),
1004 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1005 arraysize(kFpuCalleeSaves)),
1006 compiler_options,
1007 stats),
1008 block_labels_(nullptr),
1009 location_builder_(graph, this),
1010 instruction_visitor_(graph, this),
1011 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001012 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001013 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001014 uint32_literals_(std::less<uint32_t>(),
1015 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001016 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1017 boot_image_string_patches_(StringReferenceValueComparator(),
1018 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1019 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1020 boot_image_type_patches_(TypeReferenceValueComparator(),
1021 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1022 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001023 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001024 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1025 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001026 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001027 // Save RA (containing the return address) to mimic Quick.
1028 AddAllocatedRegister(Location::RegisterLocation(RA));
1029}
1030
1031#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001032// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1033#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001034#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001035
1036void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1037 // Ensure that we fix up branches.
1038 __ FinalizeCode();
1039
1040 // Adjust native pc offsets in stack maps.
1041 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001042 uint32_t old_position =
1043 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001044 uint32_t new_position = __ GetAdjustedPosition(old_position);
1045 DCHECK_GE(new_position, old_position);
1046 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1047 }
1048
1049 // Adjust pc offsets for the disassembly information.
1050 if (disasm_info_ != nullptr) {
1051 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1052 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1053 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1054 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1055 it.second.start = __ GetAdjustedPosition(it.second.start);
1056 it.second.end = __ GetAdjustedPosition(it.second.end);
1057 }
1058 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1059 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1060 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1061 }
1062 }
1063
1064 CodeGenerator::Finalize(allocator);
1065}
1066
1067MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1068 return codegen_->GetAssembler();
1069}
1070
1071void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1072 DCHECK_LT(index, moves_.size());
1073 MoveOperands* move = moves_[index];
1074 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1075}
1076
1077void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1078 DCHECK_LT(index, moves_.size());
1079 MoveOperands* move = moves_[index];
1080 Primitive::Type type = move->GetType();
1081 Location loc1 = move->GetDestination();
1082 Location loc2 = move->GetSource();
1083
1084 DCHECK(!loc1.IsConstant());
1085 DCHECK(!loc2.IsConstant());
1086
1087 if (loc1.Equals(loc2)) {
1088 return;
1089 }
1090
1091 if (loc1.IsRegister() && loc2.IsRegister()) {
1092 // Swap 2 GPRs.
1093 Register r1 = loc1.AsRegister<Register>();
1094 Register r2 = loc2.AsRegister<Register>();
1095 __ Move(TMP, r2);
1096 __ Move(r2, r1);
1097 __ Move(r1, TMP);
1098 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1099 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1100 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1101 if (type == Primitive::kPrimFloat) {
1102 __ MovS(FTMP, f2);
1103 __ MovS(f2, f1);
1104 __ MovS(f1, FTMP);
1105 } else {
1106 DCHECK_EQ(type, Primitive::kPrimDouble);
1107 __ MovD(FTMP, f2);
1108 __ MovD(f2, f1);
1109 __ MovD(f1, FTMP);
1110 }
1111 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1112 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1113 // Swap FPR and GPR.
1114 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1115 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1116 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001117 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001118 __ Move(TMP, r2);
1119 __ Mfc1(r2, f1);
1120 __ Mtc1(TMP, f1);
1121 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1122 // Swap 2 GPR register pairs.
1123 Register r1 = loc1.AsRegisterPairLow<Register>();
1124 Register r2 = loc2.AsRegisterPairLow<Register>();
1125 __ Move(TMP, r2);
1126 __ Move(r2, r1);
1127 __ Move(r1, TMP);
1128 r1 = loc1.AsRegisterPairHigh<Register>();
1129 r2 = loc2.AsRegisterPairHigh<Register>();
1130 __ Move(TMP, r2);
1131 __ Move(r2, r1);
1132 __ Move(r1, TMP);
1133 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1134 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1135 // Swap FPR and GPR register pair.
1136 DCHECK_EQ(type, Primitive::kPrimDouble);
1137 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1138 : loc2.AsFpuRegister<FRegister>();
1139 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1140 : loc2.AsRegisterPairLow<Register>();
1141 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1142 : loc2.AsRegisterPairHigh<Register>();
1143 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1144 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1145 // unpredictable and the following mfch1 will fail.
1146 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001147 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001148 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001149 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001150 __ Move(r2_l, TMP);
1151 __ Move(r2_h, AT);
1152 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1153 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1154 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1155 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001156 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1157 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001158 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1159 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001160 __ Move(TMP, reg);
1161 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1162 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1163 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1164 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1165 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1166 : loc2.AsRegisterPairLow<Register>();
1167 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1168 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001169 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001170 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1171 : loc2.GetHighStackIndex(kMipsWordSize);
1172 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001173 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001174 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001175 __ Move(TMP, reg_h);
1176 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1177 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001178 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1179 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1180 : loc2.AsFpuRegister<FRegister>();
1181 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1182 if (type == Primitive::kPrimFloat) {
1183 __ MovS(FTMP, reg);
1184 __ LoadSFromOffset(reg, SP, offset);
1185 __ StoreSToOffset(FTMP, SP, offset);
1186 } else {
1187 DCHECK_EQ(type, Primitive::kPrimDouble);
1188 __ MovD(FTMP, reg);
1189 __ LoadDFromOffset(reg, SP, offset);
1190 __ StoreDToOffset(FTMP, SP, offset);
1191 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001192 } else {
1193 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1194 }
1195}
1196
1197void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1198 __ Pop(static_cast<Register>(reg));
1199}
1200
1201void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1202 __ Push(static_cast<Register>(reg));
1203}
1204
1205void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1206 // Allocate a scratch register other than TMP, if available.
1207 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1208 // automatically unspilled when the scratch scope object is destroyed).
1209 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1210 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1211 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1212 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1213 __ LoadFromOffset(kLoadWord,
1214 Register(ensure_scratch.GetRegister()),
1215 SP,
1216 index1 + stack_offset);
1217 __ LoadFromOffset(kLoadWord,
1218 TMP,
1219 SP,
1220 index2 + stack_offset);
1221 __ StoreToOffset(kStoreWord,
1222 Register(ensure_scratch.GetRegister()),
1223 SP,
1224 index2 + stack_offset);
1225 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1226 }
1227}
1228
Alexey Frunze73296a72016-06-03 22:51:46 -07001229void CodeGeneratorMIPS::ComputeSpillMask() {
1230 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1231 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1232 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1233 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1234 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1235 // within the stack frame.
1236 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1237 core_spill_mask_ |= (1 << ZERO);
1238 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001239}
1240
1241bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001242 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001243 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1244 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1245 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001246 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001247}
1248
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001249static dwarf::Reg DWARFReg(Register reg) {
1250 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1251}
1252
1253// TODO: mapping of floating-point registers to DWARF.
1254
1255void CodeGeneratorMIPS::GenerateFrameEntry() {
1256 __ Bind(&frame_entry_label_);
1257
1258 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1259
1260 if (do_overflow_check) {
1261 __ LoadFromOffset(kLoadWord,
1262 ZERO,
1263 SP,
1264 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1265 RecordPcInfo(nullptr, 0);
1266 }
1267
1268 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001269 CHECK_EQ(fpu_spill_mask_, 0u);
1270 CHECK_EQ(core_spill_mask_, 1u << RA);
1271 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001272 return;
1273 }
1274
1275 // Make sure the frame size isn't unreasonably large.
1276 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1277 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1278 }
1279
1280 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281
Alexey Frunze73296a72016-06-03 22:51:46 -07001282 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001283 __ IncreaseFrameSize(ofs);
1284
Alexey Frunze73296a72016-06-03 22:51:46 -07001285 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1286 Register reg = static_cast<Register>(MostSignificantBit(mask));
1287 mask ^= 1u << reg;
1288 ofs -= kMipsWordSize;
1289 // The ZERO register is only included for alignment.
1290 if (reg != ZERO) {
1291 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001292 __ cfi().RelOffset(DWARFReg(reg), ofs);
1293 }
1294 }
1295
Alexey Frunze73296a72016-06-03 22:51:46 -07001296 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1297 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1298 mask ^= 1u << reg;
1299 ofs -= kMipsDoublewordSize;
1300 __ StoreDToOffset(reg, SP, ofs);
1301 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001302 }
1303
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001304 // Save the current method if we need it. Note that we do not
1305 // do this in HCurrentMethod, as the instruction might have been removed
1306 // in the SSA graph.
1307 if (RequiresCurrentMethod()) {
1308 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1309 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001310
1311 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1312 // Initialize should deoptimize flag to 0.
1313 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1314 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315}
1316
1317void CodeGeneratorMIPS::GenerateFrameExit() {
1318 __ cfi().RememberState();
1319
1320 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001321 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001322
Alexey Frunze73296a72016-06-03 22:51:46 -07001323 // For better instruction scheduling restore RA before other registers.
1324 uint32_t ofs = GetFrameSize();
1325 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1326 Register reg = static_cast<Register>(MostSignificantBit(mask));
1327 mask ^= 1u << reg;
1328 ofs -= kMipsWordSize;
1329 // The ZERO register is only included for alignment.
1330 if (reg != ZERO) {
1331 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001332 __ cfi().Restore(DWARFReg(reg));
1333 }
1334 }
1335
Alexey Frunze73296a72016-06-03 22:51:46 -07001336 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1337 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1338 mask ^= 1u << reg;
1339 ofs -= kMipsDoublewordSize;
1340 __ LoadDFromOffset(reg, SP, ofs);
1341 // TODO: __ cfi().Restore(DWARFReg(reg));
1342 }
1343
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001344 size_t frame_size = GetFrameSize();
1345 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1346 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1347 bool reordering = __ SetReorder(false);
1348 if (exchange) {
1349 __ Jr(RA);
1350 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1351 } else {
1352 __ DecreaseFrameSize(frame_size);
1353 __ Jr(RA);
1354 __ Nop(); // In delay slot.
1355 }
1356 __ SetReorder(reordering);
1357 } else {
1358 __ Jr(RA);
1359 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001360 }
1361
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362 __ cfi().RestoreState();
1363 __ cfi().DefCFAOffset(GetFrameSize());
1364}
1365
1366void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1367 __ Bind(GetLabelOf(block));
1368}
1369
1370void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1371 if (src.Equals(dst)) {
1372 return;
1373 }
1374
1375 if (src.IsConstant()) {
1376 MoveConstant(dst, src.GetConstant());
1377 } else {
1378 if (Primitive::Is64BitType(dst_type)) {
1379 Move64(dst, src);
1380 } else {
1381 Move32(dst, src);
1382 }
1383 }
1384}
1385
1386void CodeGeneratorMIPS::Move32(Location destination, Location source) {
1387 if (source.Equals(destination)) {
1388 return;
1389 }
1390
1391 if (destination.IsRegister()) {
1392 if (source.IsRegister()) {
1393 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1394 } else if (source.IsFpuRegister()) {
1395 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1396 } else {
1397 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1398 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
1399 }
1400 } else if (destination.IsFpuRegister()) {
1401 if (source.IsRegister()) {
1402 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1403 } else if (source.IsFpuRegister()) {
1404 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1405 } else {
1406 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1407 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1408 }
1409 } else {
1410 DCHECK(destination.IsStackSlot()) << destination;
1411 if (source.IsRegister()) {
1412 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
1413 } else if (source.IsFpuRegister()) {
1414 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1415 } else {
1416 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1417 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1418 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1419 }
1420 }
1421}
1422
1423void CodeGeneratorMIPS::Move64(Location destination, Location source) {
1424 if (source.Equals(destination)) {
1425 return;
1426 }
1427
1428 if (destination.IsRegisterPair()) {
1429 if (source.IsRegisterPair()) {
1430 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1431 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1432 } else if (source.IsFpuRegister()) {
1433 Register dst_high = destination.AsRegisterPairHigh<Register>();
1434 Register dst_low = destination.AsRegisterPairLow<Register>();
1435 FRegister src = source.AsFpuRegister<FRegister>();
1436 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001437 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001438 } else {
1439 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1440 int32_t off = source.GetStackIndex();
1441 Register r = destination.AsRegisterPairLow<Register>();
1442 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1443 }
1444 } else if (destination.IsFpuRegister()) {
1445 if (source.IsRegisterPair()) {
1446 FRegister dst = destination.AsFpuRegister<FRegister>();
1447 Register src_high = source.AsRegisterPairHigh<Register>();
1448 Register src_low = source.AsRegisterPairLow<Register>();
1449 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001450 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001451 } else if (source.IsFpuRegister()) {
1452 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1453 } else {
1454 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1455 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1456 }
1457 } else {
1458 DCHECK(destination.IsDoubleStackSlot()) << destination;
1459 int32_t off = destination.GetStackIndex();
1460 if (source.IsRegisterPair()) {
1461 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
1462 } else if (source.IsFpuRegister()) {
1463 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
1464 } else {
1465 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1466 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1467 __ StoreToOffset(kStoreWord, TMP, SP, off);
1468 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1469 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
1470 }
1471 }
1472}
1473
1474void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1475 if (c->IsIntConstant() || c->IsNullConstant()) {
1476 // Move 32 bit constant.
1477 int32_t value = GetInt32ValueOf(c);
1478 if (destination.IsRegister()) {
1479 Register dst = destination.AsRegister<Register>();
1480 __ LoadConst32(dst, value);
1481 } else {
1482 DCHECK(destination.IsStackSlot())
1483 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001484 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001485 }
1486 } else if (c->IsLongConstant()) {
1487 // Move 64 bit constant.
1488 int64_t value = GetInt64ValueOf(c);
1489 if (destination.IsRegisterPair()) {
1490 Register r_h = destination.AsRegisterPairHigh<Register>();
1491 Register r_l = destination.AsRegisterPairLow<Register>();
1492 __ LoadConst64(r_h, r_l, value);
1493 } else {
1494 DCHECK(destination.IsDoubleStackSlot())
1495 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001496 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001497 }
1498 } else if (c->IsFloatConstant()) {
1499 // Move 32 bit float constant.
1500 int32_t value = GetInt32ValueOf(c);
1501 if (destination.IsFpuRegister()) {
1502 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1503 } else {
1504 DCHECK(destination.IsStackSlot())
1505 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001506 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001507 }
1508 } else {
1509 // Move 64 bit double constant.
1510 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1511 int64_t value = GetInt64ValueOf(c);
1512 if (destination.IsFpuRegister()) {
1513 FRegister fd = destination.AsFpuRegister<FRegister>();
1514 __ LoadDConst64(fd, value, TMP);
1515 } else {
1516 DCHECK(destination.IsDoubleStackSlot())
1517 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001518 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001519 }
1520 }
1521}
1522
1523void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1524 DCHECK(destination.IsRegister());
1525 Register dst = destination.AsRegister<Register>();
1526 __ LoadConst32(dst, value);
1527}
1528
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001529void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1530 if (location.IsRegister()) {
1531 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001532 } else if (location.IsRegisterPair()) {
1533 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1534 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001535 } else {
1536 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1537 }
1538}
1539
Vladimir Markoaad75c62016-10-03 08:46:48 +00001540template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1541inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1542 const ArenaDeque<PcRelativePatchInfo>& infos,
1543 ArenaVector<LinkerPatch>* linker_patches) {
1544 for (const PcRelativePatchInfo& info : infos) {
1545 const DexFile& dex_file = info.target_dex_file;
1546 size_t offset_or_index = info.offset_or_index;
1547 DCHECK(info.high_label.IsBound());
1548 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1549 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1550 // the assembler's base label used for PC-relative addressing.
1551 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1552 ? __ GetLabelLocation(&info.pc_rel_label)
1553 : __ GetPcRelBaseLabelLocation();
1554 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1555 }
1556}
1557
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001558void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1559 DCHECK(linker_patches->empty());
1560 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001561 pc_relative_dex_cache_patches_.size() +
1562 pc_relative_string_patches_.size() +
1563 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001564 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001565 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001566 boot_image_type_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001567 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001568 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1569 linker_patches);
1570 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001571 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001572 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1573 linker_patches);
1574 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001575 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1576 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001577 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1578 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001579 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001580 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1581 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001582 for (const auto& entry : boot_image_string_patches_) {
1583 const StringReference& target_string = entry.first;
1584 Literal* literal = entry.second;
1585 DCHECK(literal->GetLabel()->IsBound());
1586 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1587 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1588 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001589 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001590 }
1591 for (const auto& entry : boot_image_type_patches_) {
1592 const TypeReference& target_type = entry.first;
1593 Literal* literal = entry.second;
1594 DCHECK(literal->GetLabel()->IsBound());
1595 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1596 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1597 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001598 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001599 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001600 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001601}
1602
1603CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001604 const DexFile& dex_file, dex::StringIndex string_index) {
1605 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001606}
1607
1608CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001609 const DexFile& dex_file, dex::TypeIndex type_index) {
1610 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001611}
1612
Vladimir Marko1998cd02017-01-13 13:02:58 +00001613CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1614 const DexFile& dex_file, dex::TypeIndex type_index) {
1615 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1616}
1617
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001618CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1619 const DexFile& dex_file, uint32_t element_offset) {
1620 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1621}
1622
1623CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1624 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1625 patches->emplace_back(dex_file, offset_or_index);
1626 return &patches->back();
1627}
1628
Alexey Frunze06a46c42016-07-19 15:00:40 -07001629Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1630 return map->GetOrCreate(
1631 value,
1632 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1633}
1634
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001635Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1636 MethodToLiteralMap* map) {
1637 return map->GetOrCreate(
1638 target_method,
1639 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1640}
1641
Alexey Frunze06a46c42016-07-19 15:00:40 -07001642Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001643 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001644 return boot_image_string_patches_.GetOrCreate(
1645 StringReference(&dex_file, string_index),
1646 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1647}
1648
1649Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001650 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001651 return boot_image_type_patches_.GetOrCreate(
1652 TypeReference(&dex_file, type_index),
1653 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1654}
1655
1656Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001657 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001658}
1659
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001660void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1661 Register out,
1662 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001663 if (GetInstructionSetFeatures().IsR6()) {
1664 DCHECK_EQ(base, ZERO);
1665 __ Bind(&info->high_label);
1666 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001667 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001668 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001669 } else {
1670 // If base is ZERO, emit NAL to obtain the actual base.
1671 if (base == ZERO) {
1672 // Generate a dummy PC-relative call to obtain PC.
1673 __ Nal();
1674 }
1675 __ Bind(&info->high_label);
1676 __ Lui(out, /* placeholder */ 0x1234);
1677 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1678 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1679 if (base == ZERO) {
1680 __ Bind(&info->pc_rel_label);
1681 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001682 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001683 __ Addu(out, out, (base == ZERO) ? RA : base);
1684 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001685 // The immediately following instruction will add the sign-extended low half of the 32-bit
1686 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001687}
1688
Alexey Frunze627c1a02017-01-30 19:28:14 -08001689CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1690 const DexFile& dex_file,
1691 dex::StringIndex dex_index,
1692 Handle<mirror::String> handle) {
1693 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1694 reinterpret_cast64<uint64_t>(handle.GetReference()));
1695 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1696 return &jit_string_patches_.back();
1697}
1698
1699CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1700 const DexFile& dex_file,
1701 dex::TypeIndex dex_index,
1702 Handle<mirror::Class> handle) {
1703 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1704 reinterpret_cast64<uint64_t>(handle.GetReference()));
1705 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1706 return &jit_class_patches_.back();
1707}
1708
1709void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1710 const uint8_t* roots_data,
1711 const CodeGeneratorMIPS::JitPatchInfo& info,
1712 uint64_t index_in_table) const {
1713 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1714 uintptr_t address =
1715 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1716 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1717 // lui reg, addr32_high
1718 DCHECK_EQ(code[literal_offset + 0], 0x34);
1719 DCHECK_EQ(code[literal_offset + 1], 0x12);
1720 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1721 DCHECK_EQ(code[literal_offset + 3], 0x3C);
1722 // lw reg, reg, addr32_low
1723 DCHECK_EQ(code[literal_offset + 4], 0x78);
1724 DCHECK_EQ(code[literal_offset + 5], 0x56);
1725 DCHECK_EQ((code[literal_offset + 7] & 0xFC), 0x8C);
1726 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "lw reg, reg, addr32_low".
1727 // lui reg, addr32_high
1728 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1729 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
1730 // lw reg, reg, addr32_low
1731 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1732 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1733}
1734
1735void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1736 for (const JitPatchInfo& info : jit_string_patches_) {
1737 const auto& it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1738 dex::StringIndex(info.index)));
1739 DCHECK(it != jit_string_roots_.end());
1740 PatchJitRootUse(code, roots_data, info, it->second);
1741 }
1742 for (const JitPatchInfo& info : jit_class_patches_) {
1743 const auto& it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1744 dex::TypeIndex(info.index)));
1745 DCHECK(it != jit_class_roots_.end());
1746 PatchJitRootUse(code, roots_data, info, it->second);
1747 }
1748}
1749
Goran Jakovljevice114da22016-12-26 14:21:43 +01001750void CodeGeneratorMIPS::MarkGCCard(Register object,
1751 Register value,
1752 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001753 MipsLabel done;
1754 Register card = AT;
1755 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001756 if (value_can_be_null) {
1757 __ Beqz(value, &done);
1758 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001759 __ LoadFromOffset(kLoadWord,
1760 card,
1761 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001762 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001763 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1764 __ Addu(temp, card, temp);
1765 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001766 if (value_can_be_null) {
1767 __ Bind(&done);
1768 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001769}
1770
David Brazdil58282f42016-01-14 12:45:10 +00001771void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001772 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1773 blocked_core_registers_[ZERO] = true;
1774 blocked_core_registers_[K0] = true;
1775 blocked_core_registers_[K1] = true;
1776 blocked_core_registers_[GP] = true;
1777 blocked_core_registers_[SP] = true;
1778 blocked_core_registers_[RA] = true;
1779
1780 // AT and TMP(T8) are used as temporary/scratch registers
1781 // (similar to how AT is used by MIPS assemblers).
1782 blocked_core_registers_[AT] = true;
1783 blocked_core_registers_[TMP] = true;
1784 blocked_fpu_registers_[FTMP] = true;
1785
1786 // Reserve suspend and thread registers.
1787 blocked_core_registers_[S0] = true;
1788 blocked_core_registers_[TR] = true;
1789
1790 // Reserve T9 for function calls
1791 blocked_core_registers_[T9] = true;
1792
1793 // Reserve odd-numbered FPU registers.
1794 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1795 blocked_fpu_registers_[i] = true;
1796 }
1797
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001798 if (GetGraph()->IsDebuggable()) {
1799 // Stubs do not save callee-save floating point registers. If the graph
1800 // is debuggable, we need to deal with these registers differently. For
1801 // now, just block them.
1802 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1803 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1804 }
1805 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001806}
1807
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001808size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1809 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1810 return kMipsWordSize;
1811}
1812
1813size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1814 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1815 return kMipsWordSize;
1816}
1817
1818size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1819 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1820 return kMipsDoublewordSize;
1821}
1822
1823size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1824 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1825 return kMipsDoublewordSize;
1826}
1827
1828void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001829 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001830}
1831
1832void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001833 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001834}
1835
Serban Constantinescufca16662016-07-14 09:21:59 +01001836constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1837
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001838void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1839 HInstruction* instruction,
1840 uint32_t dex_pc,
1841 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001842 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001843 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1844 IsDirectEntrypoint(entrypoint));
1845 if (EntrypointRequiresStackMap(entrypoint)) {
1846 RecordPcInfo(instruction, dex_pc, slow_path);
1847 }
1848}
1849
1850void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1851 HInstruction* instruction,
1852 SlowPathCode* slow_path,
1853 bool direct) {
1854 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1855 GenerateInvokeRuntime(entry_point_offset, direct);
1856}
1857
1858void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001859 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001860 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001861 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001862 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 // Reserve argument space on stack (for $a0-$a3) for
1864 // entrypoints that directly reference native implementations.
1865 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001866 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001868 } else {
1869 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001870 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001871 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872}
1873
1874void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1875 Register class_reg) {
1876 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1877 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1878 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1879 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1880 __ Sync(0);
1881 __ Bind(slow_path->GetExitLabel());
1882}
1883
1884void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1885 __ Sync(0); // Only stype 0 is supported.
1886}
1887
1888void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1889 HBasicBlock* successor) {
1890 SuspendCheckSlowPathMIPS* slow_path =
1891 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1892 codegen_->AddSlowPath(slow_path);
1893
1894 __ LoadFromOffset(kLoadUnsignedHalfword,
1895 TMP,
1896 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001897 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 if (successor == nullptr) {
1899 __ Bnez(TMP, slow_path->GetEntryLabel());
1900 __ Bind(slow_path->GetReturnLabel());
1901 } else {
1902 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1903 __ B(slow_path->GetEntryLabel());
1904 // slow_path will return to GetLabelOf(successor).
1905 }
1906}
1907
1908InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1909 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001910 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001911 assembler_(codegen->GetAssembler()),
1912 codegen_(codegen) {}
1913
1914void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1915 DCHECK_EQ(instruction->InputCount(), 2U);
1916 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1917 Primitive::Type type = instruction->GetResultType();
1918 switch (type) {
1919 case Primitive::kPrimInt: {
1920 locations->SetInAt(0, Location::RequiresRegister());
1921 HInstruction* right = instruction->InputAt(1);
1922 bool can_use_imm = false;
1923 if (right->IsConstant()) {
1924 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1925 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1926 can_use_imm = IsUint<16>(imm);
1927 } else if (instruction->IsAdd()) {
1928 can_use_imm = IsInt<16>(imm);
1929 } else {
1930 DCHECK(instruction->IsSub());
1931 can_use_imm = IsInt<16>(-imm);
1932 }
1933 }
1934 if (can_use_imm)
1935 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1936 else
1937 locations->SetInAt(1, Location::RequiresRegister());
1938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1939 break;
1940 }
1941
1942 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001944 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946 break;
1947 }
1948
1949 case Primitive::kPrimFloat:
1950 case Primitive::kPrimDouble:
1951 DCHECK(instruction->IsAdd() || instruction->IsSub());
1952 locations->SetInAt(0, Location::RequiresFpuRegister());
1953 locations->SetInAt(1, Location::RequiresFpuRegister());
1954 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1955 break;
1956
1957 default:
1958 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1959 }
1960}
1961
1962void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1963 Primitive::Type type = instruction->GetType();
1964 LocationSummary* locations = instruction->GetLocations();
1965
1966 switch (type) {
1967 case Primitive::kPrimInt: {
1968 Register dst = locations->Out().AsRegister<Register>();
1969 Register lhs = locations->InAt(0).AsRegister<Register>();
1970 Location rhs_location = locations->InAt(1);
1971
1972 Register rhs_reg = ZERO;
1973 int32_t rhs_imm = 0;
1974 bool use_imm = rhs_location.IsConstant();
1975 if (use_imm) {
1976 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1977 } else {
1978 rhs_reg = rhs_location.AsRegister<Register>();
1979 }
1980
1981 if (instruction->IsAnd()) {
1982 if (use_imm)
1983 __ Andi(dst, lhs, rhs_imm);
1984 else
1985 __ And(dst, lhs, rhs_reg);
1986 } else if (instruction->IsOr()) {
1987 if (use_imm)
1988 __ Ori(dst, lhs, rhs_imm);
1989 else
1990 __ Or(dst, lhs, rhs_reg);
1991 } else if (instruction->IsXor()) {
1992 if (use_imm)
1993 __ Xori(dst, lhs, rhs_imm);
1994 else
1995 __ Xor(dst, lhs, rhs_reg);
1996 } else if (instruction->IsAdd()) {
1997 if (use_imm)
1998 __ Addiu(dst, lhs, rhs_imm);
1999 else
2000 __ Addu(dst, lhs, rhs_reg);
2001 } else {
2002 DCHECK(instruction->IsSub());
2003 if (use_imm)
2004 __ Addiu(dst, lhs, -rhs_imm);
2005 else
2006 __ Subu(dst, lhs, rhs_reg);
2007 }
2008 break;
2009 }
2010
2011 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002012 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2013 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2014 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2015 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002016 Location rhs_location = locations->InAt(1);
2017 bool use_imm = rhs_location.IsConstant();
2018 if (!use_imm) {
2019 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2020 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2021 if (instruction->IsAnd()) {
2022 __ And(dst_low, lhs_low, rhs_low);
2023 __ And(dst_high, lhs_high, rhs_high);
2024 } else if (instruction->IsOr()) {
2025 __ Or(dst_low, lhs_low, rhs_low);
2026 __ Or(dst_high, lhs_high, rhs_high);
2027 } else if (instruction->IsXor()) {
2028 __ Xor(dst_low, lhs_low, rhs_low);
2029 __ Xor(dst_high, lhs_high, rhs_high);
2030 } else if (instruction->IsAdd()) {
2031 if (lhs_low == rhs_low) {
2032 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2033 __ Slt(TMP, lhs_low, ZERO);
2034 __ Addu(dst_low, lhs_low, rhs_low);
2035 } else {
2036 __ Addu(dst_low, lhs_low, rhs_low);
2037 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2038 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2039 }
2040 __ Addu(dst_high, lhs_high, rhs_high);
2041 __ Addu(dst_high, dst_high, TMP);
2042 } else {
2043 DCHECK(instruction->IsSub());
2044 __ Sltu(TMP, lhs_low, rhs_low);
2045 __ Subu(dst_low, lhs_low, rhs_low);
2046 __ Subu(dst_high, lhs_high, rhs_high);
2047 __ Subu(dst_high, dst_high, TMP);
2048 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002050 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2051 if (instruction->IsOr()) {
2052 uint32_t low = Low32Bits(value);
2053 uint32_t high = High32Bits(value);
2054 if (IsUint<16>(low)) {
2055 if (dst_low != lhs_low || low != 0) {
2056 __ Ori(dst_low, lhs_low, low);
2057 }
2058 } else {
2059 __ LoadConst32(TMP, low);
2060 __ Or(dst_low, lhs_low, TMP);
2061 }
2062 if (IsUint<16>(high)) {
2063 if (dst_high != lhs_high || high != 0) {
2064 __ Ori(dst_high, lhs_high, high);
2065 }
2066 } else {
2067 if (high != low) {
2068 __ LoadConst32(TMP, high);
2069 }
2070 __ Or(dst_high, lhs_high, TMP);
2071 }
2072 } else if (instruction->IsXor()) {
2073 uint32_t low = Low32Bits(value);
2074 uint32_t high = High32Bits(value);
2075 if (IsUint<16>(low)) {
2076 if (dst_low != lhs_low || low != 0) {
2077 __ Xori(dst_low, lhs_low, low);
2078 }
2079 } else {
2080 __ LoadConst32(TMP, low);
2081 __ Xor(dst_low, lhs_low, TMP);
2082 }
2083 if (IsUint<16>(high)) {
2084 if (dst_high != lhs_high || high != 0) {
2085 __ Xori(dst_high, lhs_high, high);
2086 }
2087 } else {
2088 if (high != low) {
2089 __ LoadConst32(TMP, high);
2090 }
2091 __ Xor(dst_high, lhs_high, TMP);
2092 }
2093 } else if (instruction->IsAnd()) {
2094 uint32_t low = Low32Bits(value);
2095 uint32_t high = High32Bits(value);
2096 if (IsUint<16>(low)) {
2097 __ Andi(dst_low, lhs_low, low);
2098 } else if (low != 0xFFFFFFFF) {
2099 __ LoadConst32(TMP, low);
2100 __ And(dst_low, lhs_low, TMP);
2101 } else if (dst_low != lhs_low) {
2102 __ Move(dst_low, lhs_low);
2103 }
2104 if (IsUint<16>(high)) {
2105 __ Andi(dst_high, lhs_high, high);
2106 } else if (high != 0xFFFFFFFF) {
2107 if (high != low) {
2108 __ LoadConst32(TMP, high);
2109 }
2110 __ And(dst_high, lhs_high, TMP);
2111 } else if (dst_high != lhs_high) {
2112 __ Move(dst_high, lhs_high);
2113 }
2114 } else {
2115 if (instruction->IsSub()) {
2116 value = -value;
2117 } else {
2118 DCHECK(instruction->IsAdd());
2119 }
2120 int32_t low = Low32Bits(value);
2121 int32_t high = High32Bits(value);
2122 if (IsInt<16>(low)) {
2123 if (dst_low != lhs_low || low != 0) {
2124 __ Addiu(dst_low, lhs_low, low);
2125 }
2126 if (low != 0) {
2127 __ Sltiu(AT, dst_low, low);
2128 }
2129 } else {
2130 __ LoadConst32(TMP, low);
2131 __ Addu(dst_low, lhs_low, TMP);
2132 __ Sltu(AT, dst_low, TMP);
2133 }
2134 if (IsInt<16>(high)) {
2135 if (dst_high != lhs_high || high != 0) {
2136 __ Addiu(dst_high, lhs_high, high);
2137 }
2138 } else {
2139 if (high != low) {
2140 __ LoadConst32(TMP, high);
2141 }
2142 __ Addu(dst_high, lhs_high, TMP);
2143 }
2144 if (low != 0) {
2145 __ Addu(dst_high, dst_high, AT);
2146 }
2147 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 }
2149 break;
2150 }
2151
2152 case Primitive::kPrimFloat:
2153 case Primitive::kPrimDouble: {
2154 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2155 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2156 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2157 if (instruction->IsAdd()) {
2158 if (type == Primitive::kPrimFloat) {
2159 __ AddS(dst, lhs, rhs);
2160 } else {
2161 __ AddD(dst, lhs, rhs);
2162 }
2163 } else {
2164 DCHECK(instruction->IsSub());
2165 if (type == Primitive::kPrimFloat) {
2166 __ SubS(dst, lhs, rhs);
2167 } else {
2168 __ SubD(dst, lhs, rhs);
2169 }
2170 }
2171 break;
2172 }
2173
2174 default:
2175 LOG(FATAL) << "Unexpected binary operation type " << type;
2176 }
2177}
2178
2179void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002180 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002181
2182 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2183 Primitive::Type type = instr->GetResultType();
2184 switch (type) {
2185 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002186 locations->SetInAt(0, Location::RequiresRegister());
2187 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2188 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2189 break;
2190 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002191 locations->SetInAt(0, Location::RequiresRegister());
2192 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2193 locations->SetOut(Location::RequiresRegister());
2194 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002195 default:
2196 LOG(FATAL) << "Unexpected shift type " << type;
2197 }
2198}
2199
2200static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2201
2202void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002203 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002204 LocationSummary* locations = instr->GetLocations();
2205 Primitive::Type type = instr->GetType();
2206
2207 Location rhs_location = locations->InAt(1);
2208 bool use_imm = rhs_location.IsConstant();
2209 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2210 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002211 const uint32_t shift_mask =
2212 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002213 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002214 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2215 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002216
2217 switch (type) {
2218 case Primitive::kPrimInt: {
2219 Register dst = locations->Out().AsRegister<Register>();
2220 Register lhs = locations->InAt(0).AsRegister<Register>();
2221 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002222 if (shift_value == 0) {
2223 if (dst != lhs) {
2224 __ Move(dst, lhs);
2225 }
2226 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002227 __ Sll(dst, lhs, shift_value);
2228 } else if (instr->IsShr()) {
2229 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002230 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002231 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002232 } else {
2233 if (has_ins_rotr) {
2234 __ Rotr(dst, lhs, shift_value);
2235 } else {
2236 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2237 __ Srl(dst, lhs, shift_value);
2238 __ Or(dst, dst, TMP);
2239 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002240 }
2241 } else {
2242 if (instr->IsShl()) {
2243 __ Sllv(dst, lhs, rhs_reg);
2244 } else if (instr->IsShr()) {
2245 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002246 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002248 } else {
2249 if (has_ins_rotr) {
2250 __ Rotrv(dst, lhs, rhs_reg);
2251 } else {
2252 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002253 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2254 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2255 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2256 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2257 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002258 __ Sllv(TMP, lhs, TMP);
2259 __ Srlv(dst, lhs, rhs_reg);
2260 __ Or(dst, dst, TMP);
2261 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262 }
2263 }
2264 break;
2265 }
2266
2267 case Primitive::kPrimLong: {
2268 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2269 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2270 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2271 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2272 if (use_imm) {
2273 if (shift_value == 0) {
2274 codegen_->Move64(locations->Out(), locations->InAt(0));
2275 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002276 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002277 if (instr->IsShl()) {
2278 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2279 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2280 __ Sll(dst_low, lhs_low, shift_value);
2281 } else if (instr->IsShr()) {
2282 __ Srl(dst_low, lhs_low, shift_value);
2283 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2284 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002285 } else if (instr->IsUShr()) {
2286 __ Srl(dst_low, lhs_low, shift_value);
2287 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2288 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002289 } else {
2290 __ Srl(dst_low, lhs_low, shift_value);
2291 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2292 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002293 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002294 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002295 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002296 if (instr->IsShl()) {
2297 __ Sll(dst_low, lhs_low, shift_value);
2298 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2299 __ Sll(dst_high, lhs_high, shift_value);
2300 __ Or(dst_high, dst_high, TMP);
2301 } else if (instr->IsShr()) {
2302 __ Sra(dst_high, lhs_high, shift_value);
2303 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2304 __ Srl(dst_low, lhs_low, shift_value);
2305 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002306 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002307 __ Srl(dst_high, lhs_high, shift_value);
2308 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2309 __ Srl(dst_low, lhs_low, shift_value);
2310 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 } else {
2312 __ Srl(TMP, lhs_low, shift_value);
2313 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2314 __ Or(dst_low, dst_low, TMP);
2315 __ Srl(TMP, lhs_high, shift_value);
2316 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2317 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002318 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002319 }
2320 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002321 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002322 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002323 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002324 __ Move(dst_low, ZERO);
2325 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002326 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002328 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002329 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002330 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002331 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002332 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002333 // 64-bit rotation by 32 is just a swap.
2334 __ Move(dst_low, lhs_high);
2335 __ Move(dst_high, lhs_low);
2336 } else {
2337 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002338 __ Srl(dst_low, lhs_high, shift_value_high);
2339 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2340 __ Srl(dst_high, lhs_low, shift_value_high);
2341 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002342 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002343 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2344 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002345 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002346 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2347 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002348 __ Or(dst_high, dst_high, TMP);
2349 }
2350 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002351 }
2352 }
2353 } else {
2354 MipsLabel done;
2355 if (instr->IsShl()) {
2356 __ Sllv(dst_low, lhs_low, rhs_reg);
2357 __ Nor(AT, ZERO, rhs_reg);
2358 __ Srl(TMP, lhs_low, 1);
2359 __ Srlv(TMP, TMP, AT);
2360 __ Sllv(dst_high, lhs_high, rhs_reg);
2361 __ Or(dst_high, dst_high, TMP);
2362 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2363 __ Beqz(TMP, &done);
2364 __ Move(dst_high, dst_low);
2365 __ Move(dst_low, ZERO);
2366 } else if (instr->IsShr()) {
2367 __ Srav(dst_high, lhs_high, rhs_reg);
2368 __ Nor(AT, ZERO, rhs_reg);
2369 __ Sll(TMP, lhs_high, 1);
2370 __ Sllv(TMP, TMP, AT);
2371 __ Srlv(dst_low, lhs_low, rhs_reg);
2372 __ Or(dst_low, dst_low, TMP);
2373 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2374 __ Beqz(TMP, &done);
2375 __ Move(dst_low, dst_high);
2376 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002377 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 __ Srlv(dst_high, lhs_high, rhs_reg);
2379 __ Nor(AT, ZERO, rhs_reg);
2380 __ Sll(TMP, lhs_high, 1);
2381 __ Sllv(TMP, TMP, AT);
2382 __ Srlv(dst_low, lhs_low, rhs_reg);
2383 __ Or(dst_low, dst_low, TMP);
2384 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2385 __ Beqz(TMP, &done);
2386 __ Move(dst_low, dst_high);
2387 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002388 } else {
2389 __ Nor(AT, ZERO, rhs_reg);
2390 __ Srlv(TMP, lhs_low, rhs_reg);
2391 __ Sll(dst_low, lhs_high, 1);
2392 __ Sllv(dst_low, dst_low, AT);
2393 __ Or(dst_low, dst_low, TMP);
2394 __ Srlv(TMP, lhs_high, rhs_reg);
2395 __ Sll(dst_high, lhs_low, 1);
2396 __ Sllv(dst_high, dst_high, AT);
2397 __ Or(dst_high, dst_high, TMP);
2398 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2399 __ Beqz(TMP, &done);
2400 __ Move(TMP, dst_high);
2401 __ Move(dst_high, dst_low);
2402 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002403 }
2404 __ Bind(&done);
2405 }
2406 break;
2407 }
2408
2409 default:
2410 LOG(FATAL) << "Unexpected shift operation type " << type;
2411 }
2412}
2413
2414void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2415 HandleBinaryOp(instruction);
2416}
2417
2418void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2419 HandleBinaryOp(instruction);
2420}
2421
2422void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2423 HandleBinaryOp(instruction);
2424}
2425
2426void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2427 HandleBinaryOp(instruction);
2428}
2429
2430void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002431 Primitive::Type type = instruction->GetType();
2432 bool object_array_get_with_read_barrier =
2433 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002435 new (GetGraph()->GetArena()) LocationSummary(instruction,
2436 object_array_get_with_read_barrier
2437 ? LocationSummary::kCallOnSlowPath
2438 : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002439 locations->SetInAt(0, Location::RequiresRegister());
2440 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002441 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2443 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002444 // The output overlaps in the case of an object array get with
2445 // read barriers enabled: we do not want the move to overwrite the
2446 // array's location, as we need it to emit the read barrier.
2447 locations->SetOut(Location::RequiresRegister(),
2448 object_array_get_with_read_barrier
2449 ? Location::kOutputOverlap
2450 : Location::kNoOutputOverlap);
2451 }
2452 // We need a temporary register for the read barrier marking slow
2453 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2454 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2455 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002456 }
2457}
2458
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002459static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2460 auto null_checker = [codegen, instruction]() {
2461 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002462 };
2463 return null_checker;
2464}
2465
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2467 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002468 Location obj_loc = locations->InAt(0);
2469 Register obj = obj_loc.AsRegister<Register>();
2470 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002471 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002472 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002473 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002474
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002475 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002476 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2477 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002478 switch (type) {
2479 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002480 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002481 if (index.IsConstant()) {
2482 size_t offset =
2483 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002484 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002485 } else {
2486 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002487 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 }
2489 break;
2490 }
2491
2492 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002493 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002494 if (index.IsConstant()) {
2495 size_t offset =
2496 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002497 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002498 } else {
2499 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002500 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002501 }
2502 break;
2503 }
2504
2505 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002506 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 if (index.IsConstant()) {
2508 size_t offset =
2509 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002510 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002511 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002512 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002513 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002514 }
2515 break;
2516 }
2517
2518 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002519 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002520 if (maybe_compressed_char_at) {
2521 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2522 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2523 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2524 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2525 "Expecting 0=compressed, 1=uncompressed");
2526 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002527 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002528 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2529 if (maybe_compressed_char_at) {
2530 MipsLabel uncompressed_load, done;
2531 __ Bnez(TMP, &uncompressed_load);
2532 __ LoadFromOffset(kLoadUnsignedByte,
2533 out,
2534 obj,
2535 data_offset + (const_index << TIMES_1));
2536 __ B(&done);
2537 __ Bind(&uncompressed_load);
2538 __ LoadFromOffset(kLoadUnsignedHalfword,
2539 out,
2540 obj,
2541 data_offset + (const_index << TIMES_2));
2542 __ Bind(&done);
2543 } else {
2544 __ LoadFromOffset(kLoadUnsignedHalfword,
2545 out,
2546 obj,
2547 data_offset + (const_index << TIMES_2),
2548 null_checker);
2549 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002550 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002551 Register index_reg = index.AsRegister<Register>();
2552 if (maybe_compressed_char_at) {
2553 MipsLabel uncompressed_load, done;
2554 __ Bnez(TMP, &uncompressed_load);
2555 __ Addu(TMP, obj, index_reg);
2556 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2557 __ B(&done);
2558 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002559 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002560 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2561 __ Bind(&done);
2562 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002563 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002564 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2565 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 }
2567 break;
2568 }
2569
Alexey Frunze15958152017-02-09 19:08:30 -08002570 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002572 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002573 if (index.IsConstant()) {
2574 size_t offset =
2575 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002576 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002577 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002578 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002579 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580 }
2581 break;
2582 }
2583
Alexey Frunze15958152017-02-09 19:08:30 -08002584 case Primitive::kPrimNot: {
2585 static_assert(
2586 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2587 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2588 // /* HeapReference<Object> */ out =
2589 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2590 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2591 Location temp = locations->GetTemp(0);
2592 // Note that a potential implicit null check is handled in this
2593 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2594 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2595 out_loc,
2596 obj,
2597 data_offset,
2598 index,
2599 temp,
2600 /* needs_null_check */ true);
2601 } else {
2602 Register out = out_loc.AsRegister<Register>();
2603 if (index.IsConstant()) {
2604 size_t offset =
2605 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2606 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2607 // If read barriers are enabled, emit read barriers other than
2608 // Baker's using a slow path (and also unpoison the loaded
2609 // reference, if heap poisoning is enabled).
2610 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2611 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002612 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002613 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2614 // If read barriers are enabled, emit read barriers other than
2615 // Baker's using a slow path (and also unpoison the loaded
2616 // reference, if heap poisoning is enabled).
2617 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2618 out_loc,
2619 out_loc,
2620 obj_loc,
2621 data_offset,
2622 index);
2623 }
2624 }
2625 break;
2626 }
2627
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002629 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002630 if (index.IsConstant()) {
2631 size_t offset =
2632 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002633 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002634 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002635 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002636 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002637 }
2638 break;
2639 }
2640
2641 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002642 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 if (index.IsConstant()) {
2644 size_t offset =
2645 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002646 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002647 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002648 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002649 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002650 }
2651 break;
2652 }
2653
2654 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002655 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 if (index.IsConstant()) {
2657 size_t offset =
2658 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002659 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002660 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002661 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002662 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 }
2664 break;
2665 }
2666
2667 case Primitive::kPrimVoid:
2668 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2669 UNREACHABLE();
2670 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671}
2672
2673void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2675 locations->SetInAt(0, Location::RequiresRegister());
2676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2677}
2678
2679void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2680 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002681 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002682 Register obj = locations->InAt(0).AsRegister<Register>();
2683 Register out = locations->Out().AsRegister<Register>();
2684 __ LoadFromOffset(kLoadWord, out, obj, offset);
2685 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002686 // Mask out compression flag from String's array length.
2687 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2688 __ Srl(out, out, 1u);
2689 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690}
2691
Alexey Frunzef58b2482016-09-02 22:14:06 -07002692Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2693 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2694 ? Location::ConstantLocation(instruction->AsConstant())
2695 : Location::RequiresRegister();
2696}
2697
2698Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2699 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2700 // We can store a non-zero float or double constant without first loading it into the FPU,
2701 // but we should only prefer this if the constant has a single use.
2702 if (instruction->IsConstant() &&
2703 (instruction->AsConstant()->IsZeroBitPattern() ||
2704 instruction->GetUses().HasExactlyOneElement())) {
2705 return Location::ConstantLocation(instruction->AsConstant());
2706 // Otherwise fall through and require an FPU register for the constant.
2707 }
2708 return Location::RequiresFpuRegister();
2709}
2710
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002712 Primitive::Type value_type = instruction->GetComponentType();
2713
2714 bool needs_write_barrier =
2715 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2716 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2717
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2719 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002720 may_need_runtime_call_for_type_check ?
2721 LocationSummary::kCallOnSlowPath :
2722 LocationSummary::kNoCall);
2723
2724 locations->SetInAt(0, Location::RequiresRegister());
2725 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2726 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2727 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002728 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002729 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2730 }
2731 if (needs_write_barrier) {
2732 // Temporary register for the write barrier.
2733 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002734 }
2735}
2736
2737void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2738 LocationSummary* locations = instruction->GetLocations();
2739 Register obj = locations->InAt(0).AsRegister<Register>();
2740 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002741 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002742 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002743 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002744 bool needs_write_barrier =
2745 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002746 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002747 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002748
2749 switch (value_type) {
2750 case Primitive::kPrimBoolean:
2751 case Primitive::kPrimByte: {
2752 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002753 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002754 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002755 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002756 __ Addu(base_reg, obj, index.AsRegister<Register>());
2757 }
2758 if (value_location.IsConstant()) {
2759 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2760 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2761 } else {
2762 Register value = value_location.AsRegister<Register>();
2763 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002764 }
2765 break;
2766 }
2767
2768 case Primitive::kPrimShort:
2769 case Primitive::kPrimChar: {
2770 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002771 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002772 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002773 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002774 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002775 }
2776 if (value_location.IsConstant()) {
2777 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2778 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2779 } else {
2780 Register value = value_location.AsRegister<Register>();
2781 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002782 }
2783 break;
2784 }
2785
Alexey Frunze15958152017-02-09 19:08:30 -08002786 case Primitive::kPrimInt: {
2787 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2788 if (index.IsConstant()) {
2789 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2790 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002791 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002792 }
2793 if (value_location.IsConstant()) {
2794 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2795 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2796 } else {
2797 Register value = value_location.AsRegister<Register>();
2798 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2799 }
2800 break;
2801 }
2802
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002804 if (value_location.IsConstant()) {
2805 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002806 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002807 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002808 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002809 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002810 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002811 }
Alexey Frunze15958152017-02-09 19:08:30 -08002812 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2813 DCHECK_EQ(value, 0);
2814 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2815 DCHECK(!needs_write_barrier);
2816 DCHECK(!may_need_runtime_call_for_type_check);
2817 break;
2818 }
2819
2820 DCHECK(needs_write_barrier);
2821 Register value = value_location.AsRegister<Register>();
2822 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2823 Register temp2 = TMP; // Doesn't need to survive slow path.
2824 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2825 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2826 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2827 MipsLabel done;
2828 SlowPathCodeMIPS* slow_path = nullptr;
2829
2830 if (may_need_runtime_call_for_type_check) {
2831 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2832 codegen_->AddSlowPath(slow_path);
2833 if (instruction->GetValueCanBeNull()) {
2834 MipsLabel non_zero;
2835 __ Bnez(value, &non_zero);
2836 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2837 if (index.IsConstant()) {
2838 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002839 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002840 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002841 }
Alexey Frunze15958152017-02-09 19:08:30 -08002842 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2843 __ B(&done);
2844 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002845 }
Alexey Frunze15958152017-02-09 19:08:30 -08002846
2847 // Note that when read barriers are enabled, the type checks
2848 // are performed without read barriers. This is fine, even in
2849 // the case where a class object is in the from-space after
2850 // the flip, as a comparison involving such a type would not
2851 // produce a false positive; it may of course produce a false
2852 // negative, in which case we would take the ArraySet slow
2853 // path.
2854
2855 // /* HeapReference<Class> */ temp1 = obj->klass_
2856 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2857 __ MaybeUnpoisonHeapReference(temp1);
2858
2859 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2860 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2861 // /* HeapReference<Class> */ temp2 = value->klass_
2862 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2863 // If heap poisoning is enabled, no need to unpoison `temp1`
2864 // nor `temp2`, as we are comparing two poisoned references.
2865
2866 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2867 MipsLabel do_put;
2868 __ Beq(temp1, temp2, &do_put);
2869 // If heap poisoning is enabled, the `temp1` reference has
2870 // not been unpoisoned yet; unpoison it now.
2871 __ MaybeUnpoisonHeapReference(temp1);
2872
2873 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2874 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2875 // If heap poisoning is enabled, no need to unpoison
2876 // `temp1`, as we are comparing against null below.
2877 __ Bnez(temp1, slow_path->GetEntryLabel());
2878 __ Bind(&do_put);
2879 } else {
2880 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2881 }
2882 }
2883
2884 Register source = value;
2885 if (kPoisonHeapReferences) {
2886 // Note that in the case where `value` is a null reference,
2887 // we do not enter this block, as a null reference does not
2888 // need poisoning.
2889 __ Move(temp1, value);
2890 __ PoisonHeapReference(temp1);
2891 source = temp1;
2892 }
2893
2894 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2895 if (index.IsConstant()) {
2896 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002898 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002899 }
2900 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2901
2902 if (!may_need_runtime_call_for_type_check) {
2903 codegen_->MaybeRecordImplicitNullCheck(instruction);
2904 }
2905
2906 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2907
2908 if (done.IsLinked()) {
2909 __ Bind(&done);
2910 }
2911
2912 if (slow_path != nullptr) {
2913 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002914 }
2915 break;
2916 }
2917
2918 case Primitive::kPrimLong: {
2919 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002921 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002922 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002923 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002924 }
2925 if (value_location.IsConstant()) {
2926 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2927 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2928 } else {
2929 Register value = value_location.AsRegisterPairLow<Register>();
2930 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002931 }
2932 break;
2933 }
2934
2935 case Primitive::kPrimFloat: {
2936 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002938 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002939 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002940 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002941 }
2942 if (value_location.IsConstant()) {
2943 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2944 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2945 } else {
2946 FRegister value = value_location.AsFpuRegister<FRegister>();
2947 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002948 }
2949 break;
2950 }
2951
2952 case Primitive::kPrimDouble: {
2953 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002954 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002955 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002956 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002957 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002958 }
2959 if (value_location.IsConstant()) {
2960 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2961 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2962 } else {
2963 FRegister value = value_location.AsFpuRegister<FRegister>();
2964 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 }
2966 break;
2967 }
2968
2969 case Primitive::kPrimVoid:
2970 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2971 UNREACHABLE();
2972 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973}
2974
2975void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002976 RegisterSet caller_saves = RegisterSet::Empty();
2977 InvokeRuntimeCallingConvention calling_convention;
2978 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2979 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2980 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 locations->SetInAt(0, Location::RequiresRegister());
2982 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002983}
2984
2985void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2986 LocationSummary* locations = instruction->GetLocations();
2987 BoundsCheckSlowPathMIPS* slow_path =
2988 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2989 codegen_->AddSlowPath(slow_path);
2990
2991 Register index = locations->InAt(0).AsRegister<Register>();
2992 Register length = locations->InAt(1).AsRegister<Register>();
2993
2994 // length is limited by the maximum positive signed 32-bit integer.
2995 // Unsigned comparison of length and index checks for index < 0
2996 // and for length <= index simultaneously.
2997 __ Bgeu(index, length, slow_path->GetEntryLabel());
2998}
2999
Alexey Frunze15958152017-02-09 19:08:30 -08003000// Temp is used for read barrier.
3001static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3002 if (kEmitCompilerReadBarrier &&
3003 (kUseBakerReadBarrier ||
3004 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3005 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3006 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3007 return 1;
3008 }
3009 return 0;
3010}
3011
3012// Extra temp is used for read barrier.
3013static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3014 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3015}
3016
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003017void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003018 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3019 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3020
3021 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3022 switch (type_check_kind) {
3023 case TypeCheckKind::kExactCheck:
3024 case TypeCheckKind::kAbstractClassCheck:
3025 case TypeCheckKind::kClassHierarchyCheck:
3026 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003027 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003028 ? LocationSummary::kCallOnSlowPath
3029 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3030 break;
3031 case TypeCheckKind::kArrayCheck:
3032 case TypeCheckKind::kUnresolvedCheck:
3033 case TypeCheckKind::kInterfaceCheck:
3034 call_kind = LocationSummary::kCallOnSlowPath;
3035 break;
3036 }
3037
3038 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003039 locations->SetInAt(0, Location::RequiresRegister());
3040 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003041 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003042}
3043
3044void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003045 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003046 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003047 Location obj_loc = locations->InAt(0);
3048 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003049 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003050 Location temp_loc = locations->GetTemp(0);
3051 Register temp = temp_loc.AsRegister<Register>();
3052 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3053 DCHECK_LE(num_temps, 2u);
3054 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003055 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3056 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3057 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3058 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3059 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3060 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3061 const uint32_t object_array_data_offset =
3062 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3063 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003064
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003065 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3066 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3067 // read barriers is done for performance and code size reasons.
3068 bool is_type_check_slow_path_fatal = false;
3069 if (!kEmitCompilerReadBarrier) {
3070 is_type_check_slow_path_fatal =
3071 (type_check_kind == TypeCheckKind::kExactCheck ||
3072 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3073 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3074 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3075 !instruction->CanThrowIntoCatchBlock();
3076 }
3077 SlowPathCodeMIPS* slow_path =
3078 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3079 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080 codegen_->AddSlowPath(slow_path);
3081
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003082 // Avoid this check if we know `obj` is not null.
3083 if (instruction->MustDoNullCheck()) {
3084 __ Beqz(obj, &done);
3085 }
3086
3087 switch (type_check_kind) {
3088 case TypeCheckKind::kExactCheck:
3089 case TypeCheckKind::kArrayCheck: {
3090 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003091 GenerateReferenceLoadTwoRegisters(instruction,
3092 temp_loc,
3093 obj_loc,
3094 class_offset,
3095 maybe_temp2_loc,
3096 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003097 // Jump to slow path for throwing the exception or doing a
3098 // more involved array check.
3099 __ Bne(temp, cls, slow_path->GetEntryLabel());
3100 break;
3101 }
3102
3103 case TypeCheckKind::kAbstractClassCheck: {
3104 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003105 GenerateReferenceLoadTwoRegisters(instruction,
3106 temp_loc,
3107 obj_loc,
3108 class_offset,
3109 maybe_temp2_loc,
3110 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003111 // If the class is abstract, we eagerly fetch the super class of the
3112 // object to avoid doing a comparison we know will fail.
3113 MipsLabel loop;
3114 __ Bind(&loop);
3115 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003116 GenerateReferenceLoadOneRegister(instruction,
3117 temp_loc,
3118 super_offset,
3119 maybe_temp2_loc,
3120 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003121 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3122 // exception.
3123 __ Beqz(temp, slow_path->GetEntryLabel());
3124 // Otherwise, compare the classes.
3125 __ Bne(temp, cls, &loop);
3126 break;
3127 }
3128
3129 case TypeCheckKind::kClassHierarchyCheck: {
3130 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003131 GenerateReferenceLoadTwoRegisters(instruction,
3132 temp_loc,
3133 obj_loc,
3134 class_offset,
3135 maybe_temp2_loc,
3136 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003137 // Walk over the class hierarchy to find a match.
3138 MipsLabel loop;
3139 __ Bind(&loop);
3140 __ Beq(temp, cls, &done);
3141 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003142 GenerateReferenceLoadOneRegister(instruction,
3143 temp_loc,
3144 super_offset,
3145 maybe_temp2_loc,
3146 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003147 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3148 // exception. Otherwise, jump to the beginning of the loop.
3149 __ Bnez(temp, &loop);
3150 __ B(slow_path->GetEntryLabel());
3151 break;
3152 }
3153
3154 case TypeCheckKind::kArrayObjectCheck: {
3155 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003156 GenerateReferenceLoadTwoRegisters(instruction,
3157 temp_loc,
3158 obj_loc,
3159 class_offset,
3160 maybe_temp2_loc,
3161 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003162 // Do an exact check.
3163 __ Beq(temp, cls, &done);
3164 // Otherwise, we need to check that the object's class is a non-primitive array.
3165 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003166 GenerateReferenceLoadOneRegister(instruction,
3167 temp_loc,
3168 component_offset,
3169 maybe_temp2_loc,
3170 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003171 // If the component type is null, jump to the slow path to throw the exception.
3172 __ Beqz(temp, slow_path->GetEntryLabel());
3173 // Otherwise, the object is indeed an array, further check that this component
3174 // type is not a primitive type.
3175 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3176 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3177 __ Bnez(temp, slow_path->GetEntryLabel());
3178 break;
3179 }
3180
3181 case TypeCheckKind::kUnresolvedCheck:
3182 // We always go into the type check slow path for the unresolved check case.
3183 // We cannot directly call the CheckCast runtime entry point
3184 // without resorting to a type checking slow path here (i.e. by
3185 // calling InvokeRuntime directly), as it would require to
3186 // assign fixed registers for the inputs of this HInstanceOf
3187 // instruction (following the runtime calling convention), which
3188 // might be cluttered by the potential first read barrier
3189 // emission at the beginning of this method.
3190 __ B(slow_path->GetEntryLabel());
3191 break;
3192
3193 case TypeCheckKind::kInterfaceCheck: {
3194 // Avoid read barriers to improve performance of the fast path. We can not get false
3195 // positives by doing this.
3196 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003197 GenerateReferenceLoadTwoRegisters(instruction,
3198 temp_loc,
3199 obj_loc,
3200 class_offset,
3201 maybe_temp2_loc,
3202 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003203 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003204 GenerateReferenceLoadTwoRegisters(instruction,
3205 temp_loc,
3206 temp_loc,
3207 iftable_offset,
3208 maybe_temp2_loc,
3209 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003210 // Iftable is never null.
3211 __ Lw(TMP, temp, array_length_offset);
3212 // Loop through the iftable and check if any class matches.
3213 MipsLabel loop;
3214 __ Bind(&loop);
3215 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3216 __ Beqz(TMP, slow_path->GetEntryLabel());
3217 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3218 __ MaybeUnpoisonHeapReference(AT);
3219 // Go to next interface.
3220 __ Addiu(TMP, TMP, -2);
3221 // Compare the classes and continue the loop if they do not match.
3222 __ Bne(AT, cls, &loop);
3223 break;
3224 }
3225 }
3226
3227 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003228 __ Bind(slow_path->GetExitLabel());
3229}
3230
3231void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3232 LocationSummary* locations =
3233 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3234 locations->SetInAt(0, Location::RequiresRegister());
3235 if (check->HasUses()) {
3236 locations->SetOut(Location::SameAsFirstInput());
3237 }
3238}
3239
3240void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3241 // We assume the class is not null.
3242 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3243 check->GetLoadClass(),
3244 check,
3245 check->GetDexPc(),
3246 true);
3247 codegen_->AddSlowPath(slow_path);
3248 GenerateClassInitializationCheck(slow_path,
3249 check->GetLocations()->InAt(0).AsRegister<Register>());
3250}
3251
3252void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3253 Primitive::Type in_type = compare->InputAt(0)->GetType();
3254
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003255 LocationSummary* locations =
3256 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003257
3258 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003259 case Primitive::kPrimBoolean:
3260 case Primitive::kPrimByte:
3261 case Primitive::kPrimShort:
3262 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003263 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003264 locations->SetInAt(0, Location::RequiresRegister());
3265 locations->SetInAt(1, Location::RequiresRegister());
3266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3267 break;
3268
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003269 case Primitive::kPrimLong:
3270 locations->SetInAt(0, Location::RequiresRegister());
3271 locations->SetInAt(1, Location::RequiresRegister());
3272 // Output overlaps because it is written before doing the low comparison.
3273 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3274 break;
3275
3276 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003277 case Primitive::kPrimDouble:
3278 locations->SetInAt(0, Location::RequiresFpuRegister());
3279 locations->SetInAt(1, Location::RequiresFpuRegister());
3280 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003281 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003282
3283 default:
3284 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3285 }
3286}
3287
3288void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3289 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003290 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003291 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003292 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003293
3294 // 0 if: left == right
3295 // 1 if: left > right
3296 // -1 if: left < right
3297 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003298 case Primitive::kPrimBoolean:
3299 case Primitive::kPrimByte:
3300 case Primitive::kPrimShort:
3301 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003302 case Primitive::kPrimInt: {
3303 Register lhs = locations->InAt(0).AsRegister<Register>();
3304 Register rhs = locations->InAt(1).AsRegister<Register>();
3305 __ Slt(TMP, lhs, rhs);
3306 __ Slt(res, rhs, lhs);
3307 __ Subu(res, res, TMP);
3308 break;
3309 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003310 case Primitive::kPrimLong: {
3311 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003312 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3313 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3314 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3315 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3316 // TODO: more efficient (direct) comparison with a constant.
3317 __ Slt(TMP, lhs_high, rhs_high);
3318 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3319 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3320 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3321 __ Sltu(TMP, lhs_low, rhs_low);
3322 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3323 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3324 __ Bind(&done);
3325 break;
3326 }
3327
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003328 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003329 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003330 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3331 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3332 MipsLabel done;
3333 if (isR6) {
3334 __ CmpEqS(FTMP, lhs, rhs);
3335 __ LoadConst32(res, 0);
3336 __ Bc1nez(FTMP, &done);
3337 if (gt_bias) {
3338 __ CmpLtS(FTMP, lhs, rhs);
3339 __ LoadConst32(res, -1);
3340 __ Bc1nez(FTMP, &done);
3341 __ LoadConst32(res, 1);
3342 } else {
3343 __ CmpLtS(FTMP, rhs, lhs);
3344 __ LoadConst32(res, 1);
3345 __ Bc1nez(FTMP, &done);
3346 __ LoadConst32(res, -1);
3347 }
3348 } else {
3349 if (gt_bias) {
3350 __ ColtS(0, lhs, rhs);
3351 __ LoadConst32(res, -1);
3352 __ Bc1t(0, &done);
3353 __ CeqS(0, lhs, rhs);
3354 __ LoadConst32(res, 1);
3355 __ Movt(res, ZERO, 0);
3356 } else {
3357 __ ColtS(0, rhs, lhs);
3358 __ LoadConst32(res, 1);
3359 __ Bc1t(0, &done);
3360 __ CeqS(0, lhs, rhs);
3361 __ LoadConst32(res, -1);
3362 __ Movt(res, ZERO, 0);
3363 }
3364 }
3365 __ Bind(&done);
3366 break;
3367 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003368 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003369 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003370 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3371 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3372 MipsLabel done;
3373 if (isR6) {
3374 __ CmpEqD(FTMP, lhs, rhs);
3375 __ LoadConst32(res, 0);
3376 __ Bc1nez(FTMP, &done);
3377 if (gt_bias) {
3378 __ CmpLtD(FTMP, lhs, rhs);
3379 __ LoadConst32(res, -1);
3380 __ Bc1nez(FTMP, &done);
3381 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003382 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003383 __ CmpLtD(FTMP, rhs, lhs);
3384 __ LoadConst32(res, 1);
3385 __ Bc1nez(FTMP, &done);
3386 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003387 }
3388 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003389 if (gt_bias) {
3390 __ ColtD(0, lhs, rhs);
3391 __ LoadConst32(res, -1);
3392 __ Bc1t(0, &done);
3393 __ CeqD(0, lhs, rhs);
3394 __ LoadConst32(res, 1);
3395 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003396 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003397 __ ColtD(0, rhs, lhs);
3398 __ LoadConst32(res, 1);
3399 __ Bc1t(0, &done);
3400 __ CeqD(0, lhs, rhs);
3401 __ LoadConst32(res, -1);
3402 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003403 }
3404 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003405 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003406 break;
3407 }
3408
3409 default:
3410 LOG(FATAL) << "Unimplemented compare type " << in_type;
3411 }
3412}
3413
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003414void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003416 switch (instruction->InputAt(0)->GetType()) {
3417 default:
3418 case Primitive::kPrimLong:
3419 locations->SetInAt(0, Location::RequiresRegister());
3420 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3421 break;
3422
3423 case Primitive::kPrimFloat:
3424 case Primitive::kPrimDouble:
3425 locations->SetInAt(0, Location::RequiresFpuRegister());
3426 locations->SetInAt(1, Location::RequiresFpuRegister());
3427 break;
3428 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003429 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3431 }
3432}
3433
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003434void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003435 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003436 return;
3437 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003439 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003440 LocationSummary* locations = instruction->GetLocations();
3441 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003442 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003443
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003444 switch (type) {
3445 default:
3446 // Integer case.
3447 GenerateIntCompare(instruction->GetCondition(), locations);
3448 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003449
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003450 case Primitive::kPrimLong:
3451 // TODO: don't use branches.
3452 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003453 break;
3454
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003455 case Primitive::kPrimFloat:
3456 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003457 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3458 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003459 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003460
3461 // Convert the branches into the result.
3462 MipsLabel done;
3463
3464 // False case: result = 0.
3465 __ LoadConst32(dst, 0);
3466 __ B(&done);
3467
3468 // True case: result = 1.
3469 __ Bind(&true_label);
3470 __ LoadConst32(dst, 1);
3471 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003472}
3473
Alexey Frunze7e99e052015-11-24 19:28:01 -08003474void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3475 DCHECK(instruction->IsDiv() || instruction->IsRem());
3476 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3477
3478 LocationSummary* locations = instruction->GetLocations();
3479 Location second = locations->InAt(1);
3480 DCHECK(second.IsConstant());
3481
3482 Register out = locations->Out().AsRegister<Register>();
3483 Register dividend = locations->InAt(0).AsRegister<Register>();
3484 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3485 DCHECK(imm == 1 || imm == -1);
3486
3487 if (instruction->IsRem()) {
3488 __ Move(out, ZERO);
3489 } else {
3490 if (imm == -1) {
3491 __ Subu(out, ZERO, dividend);
3492 } else if (out != dividend) {
3493 __ Move(out, dividend);
3494 }
3495 }
3496}
3497
3498void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3499 DCHECK(instruction->IsDiv() || instruction->IsRem());
3500 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3501
3502 LocationSummary* locations = instruction->GetLocations();
3503 Location second = locations->InAt(1);
3504 DCHECK(second.IsConstant());
3505
3506 Register out = locations->Out().AsRegister<Register>();
3507 Register dividend = locations->InAt(0).AsRegister<Register>();
3508 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003509 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003510 int ctz_imm = CTZ(abs_imm);
3511
3512 if (instruction->IsDiv()) {
3513 if (ctz_imm == 1) {
3514 // Fast path for division by +/-2, which is very common.
3515 __ Srl(TMP, dividend, 31);
3516 } else {
3517 __ Sra(TMP, dividend, 31);
3518 __ Srl(TMP, TMP, 32 - ctz_imm);
3519 }
3520 __ Addu(out, dividend, TMP);
3521 __ Sra(out, out, ctz_imm);
3522 if (imm < 0) {
3523 __ Subu(out, ZERO, out);
3524 }
3525 } else {
3526 if (ctz_imm == 1) {
3527 // Fast path for modulo +/-2, which is very common.
3528 __ Sra(TMP, dividend, 31);
3529 __ Subu(out, dividend, TMP);
3530 __ Andi(out, out, 1);
3531 __ Addu(out, out, TMP);
3532 } else {
3533 __ Sra(TMP, dividend, 31);
3534 __ Srl(TMP, TMP, 32 - ctz_imm);
3535 __ Addu(out, dividend, TMP);
3536 if (IsUint<16>(abs_imm - 1)) {
3537 __ Andi(out, out, abs_imm - 1);
3538 } else {
3539 __ Sll(out, out, 32 - ctz_imm);
3540 __ Srl(out, out, 32 - ctz_imm);
3541 }
3542 __ Subu(out, out, TMP);
3543 }
3544 }
3545}
3546
3547void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3548 DCHECK(instruction->IsDiv() || instruction->IsRem());
3549 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3550
3551 LocationSummary* locations = instruction->GetLocations();
3552 Location second = locations->InAt(1);
3553 DCHECK(second.IsConstant());
3554
3555 Register out = locations->Out().AsRegister<Register>();
3556 Register dividend = locations->InAt(0).AsRegister<Register>();
3557 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3558
3559 int64_t magic;
3560 int shift;
3561 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3562
3563 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3564
3565 __ LoadConst32(TMP, magic);
3566 if (isR6) {
3567 __ MuhR6(TMP, dividend, TMP);
3568 } else {
3569 __ MultR2(dividend, TMP);
3570 __ Mfhi(TMP);
3571 }
3572 if (imm > 0 && magic < 0) {
3573 __ Addu(TMP, TMP, dividend);
3574 } else if (imm < 0 && magic > 0) {
3575 __ Subu(TMP, TMP, dividend);
3576 }
3577
3578 if (shift != 0) {
3579 __ Sra(TMP, TMP, shift);
3580 }
3581
3582 if (instruction->IsDiv()) {
3583 __ Sra(out, TMP, 31);
3584 __ Subu(out, TMP, out);
3585 } else {
3586 __ Sra(AT, TMP, 31);
3587 __ Subu(AT, TMP, AT);
3588 __ LoadConst32(TMP, imm);
3589 if (isR6) {
3590 __ MulR6(TMP, AT, TMP);
3591 } else {
3592 __ MulR2(TMP, AT, TMP);
3593 }
3594 __ Subu(out, dividend, TMP);
3595 }
3596}
3597
3598void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3599 DCHECK(instruction->IsDiv() || instruction->IsRem());
3600 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3601
3602 LocationSummary* locations = instruction->GetLocations();
3603 Register out = locations->Out().AsRegister<Register>();
3604 Location second = locations->InAt(1);
3605
3606 if (second.IsConstant()) {
3607 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3608 if (imm == 0) {
3609 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3610 } else if (imm == 1 || imm == -1) {
3611 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003612 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003613 DivRemByPowerOfTwo(instruction);
3614 } else {
3615 DCHECK(imm <= -2 || imm >= 2);
3616 GenerateDivRemWithAnyConstant(instruction);
3617 }
3618 } else {
3619 Register dividend = locations->InAt(0).AsRegister<Register>();
3620 Register divisor = second.AsRegister<Register>();
3621 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3622 if (instruction->IsDiv()) {
3623 if (isR6) {
3624 __ DivR6(out, dividend, divisor);
3625 } else {
3626 __ DivR2(out, dividend, divisor);
3627 }
3628 } else {
3629 if (isR6) {
3630 __ ModR6(out, dividend, divisor);
3631 } else {
3632 __ ModR2(out, dividend, divisor);
3633 }
3634 }
3635 }
3636}
3637
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003638void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3639 Primitive::Type type = div->GetResultType();
3640 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003641 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642 : LocationSummary::kNoCall;
3643
3644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3645
3646 switch (type) {
3647 case Primitive::kPrimInt:
3648 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003649 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3651 break;
3652
3653 case Primitive::kPrimLong: {
3654 InvokeRuntimeCallingConvention calling_convention;
3655 locations->SetInAt(0, Location::RegisterPairLocation(
3656 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3657 locations->SetInAt(1, Location::RegisterPairLocation(
3658 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3659 locations->SetOut(calling_convention.GetReturnLocation(type));
3660 break;
3661 }
3662
3663 case Primitive::kPrimFloat:
3664 case Primitive::kPrimDouble:
3665 locations->SetInAt(0, Location::RequiresFpuRegister());
3666 locations->SetInAt(1, Location::RequiresFpuRegister());
3667 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3668 break;
3669
3670 default:
3671 LOG(FATAL) << "Unexpected div type " << type;
3672 }
3673}
3674
3675void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3676 Primitive::Type type = instruction->GetType();
3677 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003678
3679 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003680 case Primitive::kPrimInt:
3681 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003683 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003684 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3686 break;
3687 }
3688 case Primitive::kPrimFloat:
3689 case Primitive::kPrimDouble: {
3690 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3691 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3692 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3693 if (type == Primitive::kPrimFloat) {
3694 __ DivS(dst, lhs, rhs);
3695 } else {
3696 __ DivD(dst, lhs, rhs);
3697 }
3698 break;
3699 }
3700 default:
3701 LOG(FATAL) << "Unexpected div type " << type;
3702 }
3703}
3704
3705void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003706 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003707 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003708}
3709
3710void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3711 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3712 codegen_->AddSlowPath(slow_path);
3713 Location value = instruction->GetLocations()->InAt(0);
3714 Primitive::Type type = instruction->GetType();
3715
3716 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003717 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003718 case Primitive::kPrimByte:
3719 case Primitive::kPrimChar:
3720 case Primitive::kPrimShort:
3721 case Primitive::kPrimInt: {
3722 if (value.IsConstant()) {
3723 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3724 __ B(slow_path->GetEntryLabel());
3725 } else {
3726 // A division by a non-null constant is valid. We don't need to perform
3727 // any check, so simply fall through.
3728 }
3729 } else {
3730 DCHECK(value.IsRegister()) << value;
3731 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3732 }
3733 break;
3734 }
3735 case Primitive::kPrimLong: {
3736 if (value.IsConstant()) {
3737 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3738 __ B(slow_path->GetEntryLabel());
3739 } else {
3740 // A division by a non-null constant is valid. We don't need to perform
3741 // any check, so simply fall through.
3742 }
3743 } else {
3744 DCHECK(value.IsRegisterPair()) << value;
3745 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3746 __ Beqz(TMP, slow_path->GetEntryLabel());
3747 }
3748 break;
3749 }
3750 default:
3751 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3752 }
3753}
3754
3755void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3756 LocationSummary* locations =
3757 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3758 locations->SetOut(Location::ConstantLocation(constant));
3759}
3760
3761void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3762 // Will be generated at use site.
3763}
3764
3765void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3766 exit->SetLocations(nullptr);
3767}
3768
3769void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3770}
3771
3772void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3773 LocationSummary* locations =
3774 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3775 locations->SetOut(Location::ConstantLocation(constant));
3776}
3777
3778void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3779 // Will be generated at use site.
3780}
3781
3782void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3783 got->SetLocations(nullptr);
3784}
3785
3786void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3787 DCHECK(!successor->IsExitBlock());
3788 HBasicBlock* block = got->GetBlock();
3789 HInstruction* previous = got->GetPrevious();
3790 HLoopInformation* info = block->GetLoopInformation();
3791
3792 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3793 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3794 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3795 return;
3796 }
3797 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3798 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3799 }
3800 if (!codegen_->GoesToNextBlock(block, successor)) {
3801 __ B(codegen_->GetLabelOf(successor));
3802 }
3803}
3804
3805void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3806 HandleGoto(got, got->GetSuccessor());
3807}
3808
3809void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3810 try_boundary->SetLocations(nullptr);
3811}
3812
3813void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3814 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3815 if (!successor->IsExitBlock()) {
3816 HandleGoto(try_boundary, successor);
3817 }
3818}
3819
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003820void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3821 LocationSummary* locations) {
3822 Register dst = locations->Out().AsRegister<Register>();
3823 Register lhs = locations->InAt(0).AsRegister<Register>();
3824 Location rhs_location = locations->InAt(1);
3825 Register rhs_reg = ZERO;
3826 int64_t rhs_imm = 0;
3827 bool use_imm = rhs_location.IsConstant();
3828 if (use_imm) {
3829 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3830 } else {
3831 rhs_reg = rhs_location.AsRegister<Register>();
3832 }
3833
3834 switch (cond) {
3835 case kCondEQ:
3836 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003837 if (use_imm && IsInt<16>(-rhs_imm)) {
3838 if (rhs_imm == 0) {
3839 if (cond == kCondEQ) {
3840 __ Sltiu(dst, lhs, 1);
3841 } else {
3842 __ Sltu(dst, ZERO, lhs);
3843 }
3844 } else {
3845 __ Addiu(dst, lhs, -rhs_imm);
3846 if (cond == kCondEQ) {
3847 __ Sltiu(dst, dst, 1);
3848 } else {
3849 __ Sltu(dst, ZERO, dst);
3850 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003851 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003852 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003853 if (use_imm && IsUint<16>(rhs_imm)) {
3854 __ Xori(dst, lhs, rhs_imm);
3855 } else {
3856 if (use_imm) {
3857 rhs_reg = TMP;
3858 __ LoadConst32(rhs_reg, rhs_imm);
3859 }
3860 __ Xor(dst, lhs, rhs_reg);
3861 }
3862 if (cond == kCondEQ) {
3863 __ Sltiu(dst, dst, 1);
3864 } else {
3865 __ Sltu(dst, ZERO, dst);
3866 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003867 }
3868 break;
3869
3870 case kCondLT:
3871 case kCondGE:
3872 if (use_imm && IsInt<16>(rhs_imm)) {
3873 __ Slti(dst, lhs, rhs_imm);
3874 } else {
3875 if (use_imm) {
3876 rhs_reg = TMP;
3877 __ LoadConst32(rhs_reg, rhs_imm);
3878 }
3879 __ Slt(dst, lhs, rhs_reg);
3880 }
3881 if (cond == kCondGE) {
3882 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3883 // only the slt instruction but no sge.
3884 __ Xori(dst, dst, 1);
3885 }
3886 break;
3887
3888 case kCondLE:
3889 case kCondGT:
3890 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3891 // Simulate lhs <= rhs via lhs < rhs + 1.
3892 __ Slti(dst, lhs, rhs_imm + 1);
3893 if (cond == kCondGT) {
3894 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3895 // only the slti instruction but no sgti.
3896 __ Xori(dst, dst, 1);
3897 }
3898 } else {
3899 if (use_imm) {
3900 rhs_reg = TMP;
3901 __ LoadConst32(rhs_reg, rhs_imm);
3902 }
3903 __ Slt(dst, rhs_reg, lhs);
3904 if (cond == kCondLE) {
3905 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3906 // only the slt instruction but no sle.
3907 __ Xori(dst, dst, 1);
3908 }
3909 }
3910 break;
3911
3912 case kCondB:
3913 case kCondAE:
3914 if (use_imm && IsInt<16>(rhs_imm)) {
3915 // Sltiu sign-extends its 16-bit immediate operand before
3916 // the comparison and thus lets us compare directly with
3917 // unsigned values in the ranges [0, 0x7fff] and
3918 // [0xffff8000, 0xffffffff].
3919 __ Sltiu(dst, lhs, rhs_imm);
3920 } else {
3921 if (use_imm) {
3922 rhs_reg = TMP;
3923 __ LoadConst32(rhs_reg, rhs_imm);
3924 }
3925 __ Sltu(dst, lhs, rhs_reg);
3926 }
3927 if (cond == kCondAE) {
3928 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3929 // only the sltu instruction but no sgeu.
3930 __ Xori(dst, dst, 1);
3931 }
3932 break;
3933
3934 case kCondBE:
3935 case kCondA:
3936 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3937 // Simulate lhs <= rhs via lhs < rhs + 1.
3938 // Note that this only works if rhs + 1 does not overflow
3939 // to 0, hence the check above.
3940 // Sltiu sign-extends its 16-bit immediate operand before
3941 // the comparison and thus lets us compare directly with
3942 // unsigned values in the ranges [0, 0x7fff] and
3943 // [0xffff8000, 0xffffffff].
3944 __ Sltiu(dst, lhs, rhs_imm + 1);
3945 if (cond == kCondA) {
3946 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3947 // only the sltiu instruction but no sgtiu.
3948 __ Xori(dst, dst, 1);
3949 }
3950 } else {
3951 if (use_imm) {
3952 rhs_reg = TMP;
3953 __ LoadConst32(rhs_reg, rhs_imm);
3954 }
3955 __ Sltu(dst, rhs_reg, lhs);
3956 if (cond == kCondBE) {
3957 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3958 // only the sltu instruction but no sleu.
3959 __ Xori(dst, dst, 1);
3960 }
3961 }
3962 break;
3963 }
3964}
3965
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003966bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3967 LocationSummary* input_locations,
3968 Register dst) {
3969 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3970 Location rhs_location = input_locations->InAt(1);
3971 Register rhs_reg = ZERO;
3972 int64_t rhs_imm = 0;
3973 bool use_imm = rhs_location.IsConstant();
3974 if (use_imm) {
3975 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3976 } else {
3977 rhs_reg = rhs_location.AsRegister<Register>();
3978 }
3979
3980 switch (cond) {
3981 case kCondEQ:
3982 case kCondNE:
3983 if (use_imm && IsInt<16>(-rhs_imm)) {
3984 __ Addiu(dst, lhs, -rhs_imm);
3985 } else if (use_imm && IsUint<16>(rhs_imm)) {
3986 __ Xori(dst, lhs, rhs_imm);
3987 } else {
3988 if (use_imm) {
3989 rhs_reg = TMP;
3990 __ LoadConst32(rhs_reg, rhs_imm);
3991 }
3992 __ Xor(dst, lhs, rhs_reg);
3993 }
3994 return (cond == kCondEQ);
3995
3996 case kCondLT:
3997 case kCondGE:
3998 if (use_imm && IsInt<16>(rhs_imm)) {
3999 __ Slti(dst, lhs, rhs_imm);
4000 } else {
4001 if (use_imm) {
4002 rhs_reg = TMP;
4003 __ LoadConst32(rhs_reg, rhs_imm);
4004 }
4005 __ Slt(dst, lhs, rhs_reg);
4006 }
4007 return (cond == kCondGE);
4008
4009 case kCondLE:
4010 case kCondGT:
4011 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4012 // Simulate lhs <= rhs via lhs < rhs + 1.
4013 __ Slti(dst, lhs, rhs_imm + 1);
4014 return (cond == kCondGT);
4015 } else {
4016 if (use_imm) {
4017 rhs_reg = TMP;
4018 __ LoadConst32(rhs_reg, rhs_imm);
4019 }
4020 __ Slt(dst, rhs_reg, lhs);
4021 return (cond == kCondLE);
4022 }
4023
4024 case kCondB:
4025 case kCondAE:
4026 if (use_imm && IsInt<16>(rhs_imm)) {
4027 // Sltiu sign-extends its 16-bit immediate operand before
4028 // the comparison and thus lets us compare directly with
4029 // unsigned values in the ranges [0, 0x7fff] and
4030 // [0xffff8000, 0xffffffff].
4031 __ Sltiu(dst, lhs, rhs_imm);
4032 } else {
4033 if (use_imm) {
4034 rhs_reg = TMP;
4035 __ LoadConst32(rhs_reg, rhs_imm);
4036 }
4037 __ Sltu(dst, lhs, rhs_reg);
4038 }
4039 return (cond == kCondAE);
4040
4041 case kCondBE:
4042 case kCondA:
4043 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4044 // Simulate lhs <= rhs via lhs < rhs + 1.
4045 // Note that this only works if rhs + 1 does not overflow
4046 // to 0, hence the check above.
4047 // Sltiu sign-extends its 16-bit immediate operand before
4048 // the comparison and thus lets us compare directly with
4049 // unsigned values in the ranges [0, 0x7fff] and
4050 // [0xffff8000, 0xffffffff].
4051 __ Sltiu(dst, lhs, rhs_imm + 1);
4052 return (cond == kCondA);
4053 } else {
4054 if (use_imm) {
4055 rhs_reg = TMP;
4056 __ LoadConst32(rhs_reg, rhs_imm);
4057 }
4058 __ Sltu(dst, rhs_reg, lhs);
4059 return (cond == kCondBE);
4060 }
4061 }
4062}
4063
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004064void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4065 LocationSummary* locations,
4066 MipsLabel* label) {
4067 Register lhs = locations->InAt(0).AsRegister<Register>();
4068 Location rhs_location = locations->InAt(1);
4069 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004070 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004071 bool use_imm = rhs_location.IsConstant();
4072 if (use_imm) {
4073 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4074 } else {
4075 rhs_reg = rhs_location.AsRegister<Register>();
4076 }
4077
4078 if (use_imm && rhs_imm == 0) {
4079 switch (cond) {
4080 case kCondEQ:
4081 case kCondBE: // <= 0 if zero
4082 __ Beqz(lhs, label);
4083 break;
4084 case kCondNE:
4085 case kCondA: // > 0 if non-zero
4086 __ Bnez(lhs, label);
4087 break;
4088 case kCondLT:
4089 __ Bltz(lhs, label);
4090 break;
4091 case kCondGE:
4092 __ Bgez(lhs, label);
4093 break;
4094 case kCondLE:
4095 __ Blez(lhs, label);
4096 break;
4097 case kCondGT:
4098 __ Bgtz(lhs, label);
4099 break;
4100 case kCondB: // always false
4101 break;
4102 case kCondAE: // always true
4103 __ B(label);
4104 break;
4105 }
4106 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004107 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4108 if (isR6 || !use_imm) {
4109 if (use_imm) {
4110 rhs_reg = TMP;
4111 __ LoadConst32(rhs_reg, rhs_imm);
4112 }
4113 switch (cond) {
4114 case kCondEQ:
4115 __ Beq(lhs, rhs_reg, label);
4116 break;
4117 case kCondNE:
4118 __ Bne(lhs, rhs_reg, label);
4119 break;
4120 case kCondLT:
4121 __ Blt(lhs, rhs_reg, label);
4122 break;
4123 case kCondGE:
4124 __ Bge(lhs, rhs_reg, label);
4125 break;
4126 case kCondLE:
4127 __ Bge(rhs_reg, lhs, label);
4128 break;
4129 case kCondGT:
4130 __ Blt(rhs_reg, lhs, label);
4131 break;
4132 case kCondB:
4133 __ Bltu(lhs, rhs_reg, label);
4134 break;
4135 case kCondAE:
4136 __ Bgeu(lhs, rhs_reg, label);
4137 break;
4138 case kCondBE:
4139 __ Bgeu(rhs_reg, lhs, label);
4140 break;
4141 case kCondA:
4142 __ Bltu(rhs_reg, lhs, label);
4143 break;
4144 }
4145 } else {
4146 // Special cases for more efficient comparison with constants on R2.
4147 switch (cond) {
4148 case kCondEQ:
4149 __ LoadConst32(TMP, rhs_imm);
4150 __ Beq(lhs, TMP, label);
4151 break;
4152 case kCondNE:
4153 __ LoadConst32(TMP, rhs_imm);
4154 __ Bne(lhs, TMP, label);
4155 break;
4156 case kCondLT:
4157 if (IsInt<16>(rhs_imm)) {
4158 __ Slti(TMP, lhs, rhs_imm);
4159 __ Bnez(TMP, label);
4160 } else {
4161 __ LoadConst32(TMP, rhs_imm);
4162 __ Blt(lhs, TMP, label);
4163 }
4164 break;
4165 case kCondGE:
4166 if (IsInt<16>(rhs_imm)) {
4167 __ Slti(TMP, lhs, rhs_imm);
4168 __ Beqz(TMP, label);
4169 } else {
4170 __ LoadConst32(TMP, rhs_imm);
4171 __ Bge(lhs, TMP, label);
4172 }
4173 break;
4174 case kCondLE:
4175 if (IsInt<16>(rhs_imm + 1)) {
4176 // Simulate lhs <= rhs via lhs < rhs + 1.
4177 __ Slti(TMP, lhs, rhs_imm + 1);
4178 __ Bnez(TMP, label);
4179 } else {
4180 __ LoadConst32(TMP, rhs_imm);
4181 __ Bge(TMP, lhs, label);
4182 }
4183 break;
4184 case kCondGT:
4185 if (IsInt<16>(rhs_imm + 1)) {
4186 // Simulate lhs > rhs via !(lhs < rhs + 1).
4187 __ Slti(TMP, lhs, rhs_imm + 1);
4188 __ Beqz(TMP, label);
4189 } else {
4190 __ LoadConst32(TMP, rhs_imm);
4191 __ Blt(TMP, lhs, label);
4192 }
4193 break;
4194 case kCondB:
4195 if (IsInt<16>(rhs_imm)) {
4196 __ Sltiu(TMP, lhs, rhs_imm);
4197 __ Bnez(TMP, label);
4198 } else {
4199 __ LoadConst32(TMP, rhs_imm);
4200 __ Bltu(lhs, TMP, label);
4201 }
4202 break;
4203 case kCondAE:
4204 if (IsInt<16>(rhs_imm)) {
4205 __ Sltiu(TMP, lhs, rhs_imm);
4206 __ Beqz(TMP, label);
4207 } else {
4208 __ LoadConst32(TMP, rhs_imm);
4209 __ Bgeu(lhs, TMP, label);
4210 }
4211 break;
4212 case kCondBE:
4213 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4214 // Simulate lhs <= rhs via lhs < rhs + 1.
4215 // Note that this only works if rhs + 1 does not overflow
4216 // to 0, hence the check above.
4217 __ Sltiu(TMP, lhs, rhs_imm + 1);
4218 __ Bnez(TMP, label);
4219 } else {
4220 __ LoadConst32(TMP, rhs_imm);
4221 __ Bgeu(TMP, lhs, label);
4222 }
4223 break;
4224 case kCondA:
4225 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4226 // Simulate lhs > rhs via !(lhs < rhs + 1).
4227 // Note that this only works if rhs + 1 does not overflow
4228 // to 0, hence the check above.
4229 __ Sltiu(TMP, lhs, rhs_imm + 1);
4230 __ Beqz(TMP, label);
4231 } else {
4232 __ LoadConst32(TMP, rhs_imm);
4233 __ Bltu(TMP, lhs, label);
4234 }
4235 break;
4236 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004237 }
4238 }
4239}
4240
4241void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4242 LocationSummary* locations,
4243 MipsLabel* label) {
4244 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4245 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4246 Location rhs_location = locations->InAt(1);
4247 Register rhs_high = ZERO;
4248 Register rhs_low = ZERO;
4249 int64_t imm = 0;
4250 uint32_t imm_high = 0;
4251 uint32_t imm_low = 0;
4252 bool use_imm = rhs_location.IsConstant();
4253 if (use_imm) {
4254 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4255 imm_high = High32Bits(imm);
4256 imm_low = Low32Bits(imm);
4257 } else {
4258 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4259 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4260 }
4261
4262 if (use_imm && imm == 0) {
4263 switch (cond) {
4264 case kCondEQ:
4265 case kCondBE: // <= 0 if zero
4266 __ Or(TMP, lhs_high, lhs_low);
4267 __ Beqz(TMP, label);
4268 break;
4269 case kCondNE:
4270 case kCondA: // > 0 if non-zero
4271 __ Or(TMP, lhs_high, lhs_low);
4272 __ Bnez(TMP, label);
4273 break;
4274 case kCondLT:
4275 __ Bltz(lhs_high, label);
4276 break;
4277 case kCondGE:
4278 __ Bgez(lhs_high, label);
4279 break;
4280 case kCondLE:
4281 __ Or(TMP, lhs_high, lhs_low);
4282 __ Sra(AT, lhs_high, 31);
4283 __ Bgeu(AT, TMP, label);
4284 break;
4285 case kCondGT:
4286 __ Or(TMP, lhs_high, lhs_low);
4287 __ Sra(AT, lhs_high, 31);
4288 __ Bltu(AT, TMP, label);
4289 break;
4290 case kCondB: // always false
4291 break;
4292 case kCondAE: // always true
4293 __ B(label);
4294 break;
4295 }
4296 } else if (use_imm) {
4297 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4298 switch (cond) {
4299 case kCondEQ:
4300 __ LoadConst32(TMP, imm_high);
4301 __ Xor(TMP, TMP, lhs_high);
4302 __ LoadConst32(AT, imm_low);
4303 __ Xor(AT, AT, lhs_low);
4304 __ Or(TMP, TMP, AT);
4305 __ Beqz(TMP, label);
4306 break;
4307 case kCondNE:
4308 __ LoadConst32(TMP, imm_high);
4309 __ Xor(TMP, TMP, lhs_high);
4310 __ LoadConst32(AT, imm_low);
4311 __ Xor(AT, AT, lhs_low);
4312 __ Or(TMP, TMP, AT);
4313 __ Bnez(TMP, label);
4314 break;
4315 case kCondLT:
4316 __ LoadConst32(TMP, imm_high);
4317 __ Blt(lhs_high, TMP, label);
4318 __ Slt(TMP, TMP, lhs_high);
4319 __ LoadConst32(AT, imm_low);
4320 __ Sltu(AT, lhs_low, AT);
4321 __ Blt(TMP, AT, label);
4322 break;
4323 case kCondGE:
4324 __ LoadConst32(TMP, imm_high);
4325 __ Blt(TMP, lhs_high, label);
4326 __ Slt(TMP, lhs_high, TMP);
4327 __ LoadConst32(AT, imm_low);
4328 __ Sltu(AT, lhs_low, AT);
4329 __ Or(TMP, TMP, AT);
4330 __ Beqz(TMP, label);
4331 break;
4332 case kCondLE:
4333 __ LoadConst32(TMP, imm_high);
4334 __ Blt(lhs_high, TMP, label);
4335 __ Slt(TMP, TMP, lhs_high);
4336 __ LoadConst32(AT, imm_low);
4337 __ Sltu(AT, AT, lhs_low);
4338 __ Or(TMP, TMP, AT);
4339 __ Beqz(TMP, label);
4340 break;
4341 case kCondGT:
4342 __ LoadConst32(TMP, imm_high);
4343 __ Blt(TMP, lhs_high, label);
4344 __ Slt(TMP, lhs_high, TMP);
4345 __ LoadConst32(AT, imm_low);
4346 __ Sltu(AT, AT, lhs_low);
4347 __ Blt(TMP, AT, label);
4348 break;
4349 case kCondB:
4350 __ LoadConst32(TMP, imm_high);
4351 __ Bltu(lhs_high, TMP, label);
4352 __ Sltu(TMP, TMP, lhs_high);
4353 __ LoadConst32(AT, imm_low);
4354 __ Sltu(AT, lhs_low, AT);
4355 __ Blt(TMP, AT, label);
4356 break;
4357 case kCondAE:
4358 __ LoadConst32(TMP, imm_high);
4359 __ Bltu(TMP, lhs_high, label);
4360 __ Sltu(TMP, lhs_high, TMP);
4361 __ LoadConst32(AT, imm_low);
4362 __ Sltu(AT, lhs_low, AT);
4363 __ Or(TMP, TMP, AT);
4364 __ Beqz(TMP, label);
4365 break;
4366 case kCondBE:
4367 __ LoadConst32(TMP, imm_high);
4368 __ Bltu(lhs_high, TMP, label);
4369 __ Sltu(TMP, TMP, lhs_high);
4370 __ LoadConst32(AT, imm_low);
4371 __ Sltu(AT, AT, lhs_low);
4372 __ Or(TMP, TMP, AT);
4373 __ Beqz(TMP, label);
4374 break;
4375 case kCondA:
4376 __ LoadConst32(TMP, imm_high);
4377 __ Bltu(TMP, lhs_high, label);
4378 __ Sltu(TMP, lhs_high, TMP);
4379 __ LoadConst32(AT, imm_low);
4380 __ Sltu(AT, AT, lhs_low);
4381 __ Blt(TMP, AT, label);
4382 break;
4383 }
4384 } else {
4385 switch (cond) {
4386 case kCondEQ:
4387 __ Xor(TMP, lhs_high, rhs_high);
4388 __ Xor(AT, lhs_low, rhs_low);
4389 __ Or(TMP, TMP, AT);
4390 __ Beqz(TMP, label);
4391 break;
4392 case kCondNE:
4393 __ Xor(TMP, lhs_high, rhs_high);
4394 __ Xor(AT, lhs_low, rhs_low);
4395 __ Or(TMP, TMP, AT);
4396 __ Bnez(TMP, label);
4397 break;
4398 case kCondLT:
4399 __ Blt(lhs_high, rhs_high, label);
4400 __ Slt(TMP, rhs_high, lhs_high);
4401 __ Sltu(AT, lhs_low, rhs_low);
4402 __ Blt(TMP, AT, label);
4403 break;
4404 case kCondGE:
4405 __ Blt(rhs_high, lhs_high, label);
4406 __ Slt(TMP, lhs_high, rhs_high);
4407 __ Sltu(AT, lhs_low, rhs_low);
4408 __ Or(TMP, TMP, AT);
4409 __ Beqz(TMP, label);
4410 break;
4411 case kCondLE:
4412 __ Blt(lhs_high, rhs_high, label);
4413 __ Slt(TMP, rhs_high, lhs_high);
4414 __ Sltu(AT, rhs_low, lhs_low);
4415 __ Or(TMP, TMP, AT);
4416 __ Beqz(TMP, label);
4417 break;
4418 case kCondGT:
4419 __ Blt(rhs_high, lhs_high, label);
4420 __ Slt(TMP, lhs_high, rhs_high);
4421 __ Sltu(AT, rhs_low, lhs_low);
4422 __ Blt(TMP, AT, label);
4423 break;
4424 case kCondB:
4425 __ Bltu(lhs_high, rhs_high, label);
4426 __ Sltu(TMP, rhs_high, lhs_high);
4427 __ Sltu(AT, lhs_low, rhs_low);
4428 __ Blt(TMP, AT, label);
4429 break;
4430 case kCondAE:
4431 __ Bltu(rhs_high, lhs_high, label);
4432 __ Sltu(TMP, lhs_high, rhs_high);
4433 __ Sltu(AT, lhs_low, rhs_low);
4434 __ Or(TMP, TMP, AT);
4435 __ Beqz(TMP, label);
4436 break;
4437 case kCondBE:
4438 __ Bltu(lhs_high, rhs_high, label);
4439 __ Sltu(TMP, rhs_high, lhs_high);
4440 __ Sltu(AT, rhs_low, lhs_low);
4441 __ Or(TMP, TMP, AT);
4442 __ Beqz(TMP, label);
4443 break;
4444 case kCondA:
4445 __ Bltu(rhs_high, lhs_high, label);
4446 __ Sltu(TMP, lhs_high, rhs_high);
4447 __ Sltu(AT, rhs_low, lhs_low);
4448 __ Blt(TMP, AT, label);
4449 break;
4450 }
4451 }
4452}
4453
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004454void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4455 bool gt_bias,
4456 Primitive::Type type,
4457 LocationSummary* locations) {
4458 Register dst = locations->Out().AsRegister<Register>();
4459 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4460 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4461 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4462 if (type == Primitive::kPrimFloat) {
4463 if (isR6) {
4464 switch (cond) {
4465 case kCondEQ:
4466 __ CmpEqS(FTMP, lhs, rhs);
4467 __ Mfc1(dst, FTMP);
4468 __ Andi(dst, dst, 1);
4469 break;
4470 case kCondNE:
4471 __ CmpEqS(FTMP, lhs, rhs);
4472 __ Mfc1(dst, FTMP);
4473 __ Addiu(dst, dst, 1);
4474 break;
4475 case kCondLT:
4476 if (gt_bias) {
4477 __ CmpLtS(FTMP, lhs, rhs);
4478 } else {
4479 __ CmpUltS(FTMP, lhs, rhs);
4480 }
4481 __ Mfc1(dst, FTMP);
4482 __ Andi(dst, dst, 1);
4483 break;
4484 case kCondLE:
4485 if (gt_bias) {
4486 __ CmpLeS(FTMP, lhs, rhs);
4487 } else {
4488 __ CmpUleS(FTMP, lhs, rhs);
4489 }
4490 __ Mfc1(dst, FTMP);
4491 __ Andi(dst, dst, 1);
4492 break;
4493 case kCondGT:
4494 if (gt_bias) {
4495 __ CmpUltS(FTMP, rhs, lhs);
4496 } else {
4497 __ CmpLtS(FTMP, rhs, lhs);
4498 }
4499 __ Mfc1(dst, FTMP);
4500 __ Andi(dst, dst, 1);
4501 break;
4502 case kCondGE:
4503 if (gt_bias) {
4504 __ CmpUleS(FTMP, rhs, lhs);
4505 } else {
4506 __ CmpLeS(FTMP, rhs, lhs);
4507 }
4508 __ Mfc1(dst, FTMP);
4509 __ Andi(dst, dst, 1);
4510 break;
4511 default:
4512 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4513 UNREACHABLE();
4514 }
4515 } else {
4516 switch (cond) {
4517 case kCondEQ:
4518 __ CeqS(0, lhs, rhs);
4519 __ LoadConst32(dst, 1);
4520 __ Movf(dst, ZERO, 0);
4521 break;
4522 case kCondNE:
4523 __ CeqS(0, lhs, rhs);
4524 __ LoadConst32(dst, 1);
4525 __ Movt(dst, ZERO, 0);
4526 break;
4527 case kCondLT:
4528 if (gt_bias) {
4529 __ ColtS(0, lhs, rhs);
4530 } else {
4531 __ CultS(0, lhs, rhs);
4532 }
4533 __ LoadConst32(dst, 1);
4534 __ Movf(dst, ZERO, 0);
4535 break;
4536 case kCondLE:
4537 if (gt_bias) {
4538 __ ColeS(0, lhs, rhs);
4539 } else {
4540 __ CuleS(0, lhs, rhs);
4541 }
4542 __ LoadConst32(dst, 1);
4543 __ Movf(dst, ZERO, 0);
4544 break;
4545 case kCondGT:
4546 if (gt_bias) {
4547 __ CultS(0, rhs, lhs);
4548 } else {
4549 __ ColtS(0, rhs, lhs);
4550 }
4551 __ LoadConst32(dst, 1);
4552 __ Movf(dst, ZERO, 0);
4553 break;
4554 case kCondGE:
4555 if (gt_bias) {
4556 __ CuleS(0, rhs, lhs);
4557 } else {
4558 __ ColeS(0, rhs, lhs);
4559 }
4560 __ LoadConst32(dst, 1);
4561 __ Movf(dst, ZERO, 0);
4562 break;
4563 default:
4564 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4565 UNREACHABLE();
4566 }
4567 }
4568 } else {
4569 DCHECK_EQ(type, Primitive::kPrimDouble);
4570 if (isR6) {
4571 switch (cond) {
4572 case kCondEQ:
4573 __ CmpEqD(FTMP, lhs, rhs);
4574 __ Mfc1(dst, FTMP);
4575 __ Andi(dst, dst, 1);
4576 break;
4577 case kCondNE:
4578 __ CmpEqD(FTMP, lhs, rhs);
4579 __ Mfc1(dst, FTMP);
4580 __ Addiu(dst, dst, 1);
4581 break;
4582 case kCondLT:
4583 if (gt_bias) {
4584 __ CmpLtD(FTMP, lhs, rhs);
4585 } else {
4586 __ CmpUltD(FTMP, lhs, rhs);
4587 }
4588 __ Mfc1(dst, FTMP);
4589 __ Andi(dst, dst, 1);
4590 break;
4591 case kCondLE:
4592 if (gt_bias) {
4593 __ CmpLeD(FTMP, lhs, rhs);
4594 } else {
4595 __ CmpUleD(FTMP, lhs, rhs);
4596 }
4597 __ Mfc1(dst, FTMP);
4598 __ Andi(dst, dst, 1);
4599 break;
4600 case kCondGT:
4601 if (gt_bias) {
4602 __ CmpUltD(FTMP, rhs, lhs);
4603 } else {
4604 __ CmpLtD(FTMP, rhs, lhs);
4605 }
4606 __ Mfc1(dst, FTMP);
4607 __ Andi(dst, dst, 1);
4608 break;
4609 case kCondGE:
4610 if (gt_bias) {
4611 __ CmpUleD(FTMP, rhs, lhs);
4612 } else {
4613 __ CmpLeD(FTMP, rhs, lhs);
4614 }
4615 __ Mfc1(dst, FTMP);
4616 __ Andi(dst, dst, 1);
4617 break;
4618 default:
4619 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4620 UNREACHABLE();
4621 }
4622 } else {
4623 switch (cond) {
4624 case kCondEQ:
4625 __ CeqD(0, lhs, rhs);
4626 __ LoadConst32(dst, 1);
4627 __ Movf(dst, ZERO, 0);
4628 break;
4629 case kCondNE:
4630 __ CeqD(0, lhs, rhs);
4631 __ LoadConst32(dst, 1);
4632 __ Movt(dst, ZERO, 0);
4633 break;
4634 case kCondLT:
4635 if (gt_bias) {
4636 __ ColtD(0, lhs, rhs);
4637 } else {
4638 __ CultD(0, lhs, rhs);
4639 }
4640 __ LoadConst32(dst, 1);
4641 __ Movf(dst, ZERO, 0);
4642 break;
4643 case kCondLE:
4644 if (gt_bias) {
4645 __ ColeD(0, lhs, rhs);
4646 } else {
4647 __ CuleD(0, lhs, rhs);
4648 }
4649 __ LoadConst32(dst, 1);
4650 __ Movf(dst, ZERO, 0);
4651 break;
4652 case kCondGT:
4653 if (gt_bias) {
4654 __ CultD(0, rhs, lhs);
4655 } else {
4656 __ ColtD(0, rhs, lhs);
4657 }
4658 __ LoadConst32(dst, 1);
4659 __ Movf(dst, ZERO, 0);
4660 break;
4661 case kCondGE:
4662 if (gt_bias) {
4663 __ CuleD(0, rhs, lhs);
4664 } else {
4665 __ ColeD(0, rhs, lhs);
4666 }
4667 __ LoadConst32(dst, 1);
4668 __ Movf(dst, ZERO, 0);
4669 break;
4670 default:
4671 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4672 UNREACHABLE();
4673 }
4674 }
4675 }
4676}
4677
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004678bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4679 bool gt_bias,
4680 Primitive::Type type,
4681 LocationSummary* input_locations,
4682 int cc) {
4683 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4684 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4685 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4686 if (type == Primitive::kPrimFloat) {
4687 switch (cond) {
4688 case kCondEQ:
4689 __ CeqS(cc, lhs, rhs);
4690 return false;
4691 case kCondNE:
4692 __ CeqS(cc, lhs, rhs);
4693 return true;
4694 case kCondLT:
4695 if (gt_bias) {
4696 __ ColtS(cc, lhs, rhs);
4697 } else {
4698 __ CultS(cc, lhs, rhs);
4699 }
4700 return false;
4701 case kCondLE:
4702 if (gt_bias) {
4703 __ ColeS(cc, lhs, rhs);
4704 } else {
4705 __ CuleS(cc, lhs, rhs);
4706 }
4707 return false;
4708 case kCondGT:
4709 if (gt_bias) {
4710 __ CultS(cc, rhs, lhs);
4711 } else {
4712 __ ColtS(cc, rhs, lhs);
4713 }
4714 return false;
4715 case kCondGE:
4716 if (gt_bias) {
4717 __ CuleS(cc, rhs, lhs);
4718 } else {
4719 __ ColeS(cc, rhs, lhs);
4720 }
4721 return false;
4722 default:
4723 LOG(FATAL) << "Unexpected non-floating-point condition";
4724 UNREACHABLE();
4725 }
4726 } else {
4727 DCHECK_EQ(type, Primitive::kPrimDouble);
4728 switch (cond) {
4729 case kCondEQ:
4730 __ CeqD(cc, lhs, rhs);
4731 return false;
4732 case kCondNE:
4733 __ CeqD(cc, lhs, rhs);
4734 return true;
4735 case kCondLT:
4736 if (gt_bias) {
4737 __ ColtD(cc, lhs, rhs);
4738 } else {
4739 __ CultD(cc, lhs, rhs);
4740 }
4741 return false;
4742 case kCondLE:
4743 if (gt_bias) {
4744 __ ColeD(cc, lhs, rhs);
4745 } else {
4746 __ CuleD(cc, lhs, rhs);
4747 }
4748 return false;
4749 case kCondGT:
4750 if (gt_bias) {
4751 __ CultD(cc, rhs, lhs);
4752 } else {
4753 __ ColtD(cc, rhs, lhs);
4754 }
4755 return false;
4756 case kCondGE:
4757 if (gt_bias) {
4758 __ CuleD(cc, rhs, lhs);
4759 } else {
4760 __ ColeD(cc, rhs, lhs);
4761 }
4762 return false;
4763 default:
4764 LOG(FATAL) << "Unexpected non-floating-point condition";
4765 UNREACHABLE();
4766 }
4767 }
4768}
4769
4770bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4771 bool gt_bias,
4772 Primitive::Type type,
4773 LocationSummary* input_locations,
4774 FRegister dst) {
4775 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4776 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4777 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4778 if (type == Primitive::kPrimFloat) {
4779 switch (cond) {
4780 case kCondEQ:
4781 __ CmpEqS(dst, lhs, rhs);
4782 return false;
4783 case kCondNE:
4784 __ CmpEqS(dst, lhs, rhs);
4785 return true;
4786 case kCondLT:
4787 if (gt_bias) {
4788 __ CmpLtS(dst, lhs, rhs);
4789 } else {
4790 __ CmpUltS(dst, lhs, rhs);
4791 }
4792 return false;
4793 case kCondLE:
4794 if (gt_bias) {
4795 __ CmpLeS(dst, lhs, rhs);
4796 } else {
4797 __ CmpUleS(dst, lhs, rhs);
4798 }
4799 return false;
4800 case kCondGT:
4801 if (gt_bias) {
4802 __ CmpUltS(dst, rhs, lhs);
4803 } else {
4804 __ CmpLtS(dst, rhs, lhs);
4805 }
4806 return false;
4807 case kCondGE:
4808 if (gt_bias) {
4809 __ CmpUleS(dst, rhs, lhs);
4810 } else {
4811 __ CmpLeS(dst, rhs, lhs);
4812 }
4813 return false;
4814 default:
4815 LOG(FATAL) << "Unexpected non-floating-point condition";
4816 UNREACHABLE();
4817 }
4818 } else {
4819 DCHECK_EQ(type, Primitive::kPrimDouble);
4820 switch (cond) {
4821 case kCondEQ:
4822 __ CmpEqD(dst, lhs, rhs);
4823 return false;
4824 case kCondNE:
4825 __ CmpEqD(dst, lhs, rhs);
4826 return true;
4827 case kCondLT:
4828 if (gt_bias) {
4829 __ CmpLtD(dst, lhs, rhs);
4830 } else {
4831 __ CmpUltD(dst, lhs, rhs);
4832 }
4833 return false;
4834 case kCondLE:
4835 if (gt_bias) {
4836 __ CmpLeD(dst, lhs, rhs);
4837 } else {
4838 __ CmpUleD(dst, lhs, rhs);
4839 }
4840 return false;
4841 case kCondGT:
4842 if (gt_bias) {
4843 __ CmpUltD(dst, rhs, lhs);
4844 } else {
4845 __ CmpLtD(dst, rhs, lhs);
4846 }
4847 return false;
4848 case kCondGE:
4849 if (gt_bias) {
4850 __ CmpUleD(dst, rhs, lhs);
4851 } else {
4852 __ CmpLeD(dst, rhs, lhs);
4853 }
4854 return false;
4855 default:
4856 LOG(FATAL) << "Unexpected non-floating-point condition";
4857 UNREACHABLE();
4858 }
4859 }
4860}
4861
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004862void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
4863 bool gt_bias,
4864 Primitive::Type type,
4865 LocationSummary* locations,
4866 MipsLabel* label) {
4867 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4868 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4869 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4870 if (type == Primitive::kPrimFloat) {
4871 if (isR6) {
4872 switch (cond) {
4873 case kCondEQ:
4874 __ CmpEqS(FTMP, lhs, rhs);
4875 __ Bc1nez(FTMP, label);
4876 break;
4877 case kCondNE:
4878 __ CmpEqS(FTMP, lhs, rhs);
4879 __ Bc1eqz(FTMP, label);
4880 break;
4881 case kCondLT:
4882 if (gt_bias) {
4883 __ CmpLtS(FTMP, lhs, rhs);
4884 } else {
4885 __ CmpUltS(FTMP, lhs, rhs);
4886 }
4887 __ Bc1nez(FTMP, label);
4888 break;
4889 case kCondLE:
4890 if (gt_bias) {
4891 __ CmpLeS(FTMP, lhs, rhs);
4892 } else {
4893 __ CmpUleS(FTMP, lhs, rhs);
4894 }
4895 __ Bc1nez(FTMP, label);
4896 break;
4897 case kCondGT:
4898 if (gt_bias) {
4899 __ CmpUltS(FTMP, rhs, lhs);
4900 } else {
4901 __ CmpLtS(FTMP, rhs, lhs);
4902 }
4903 __ Bc1nez(FTMP, label);
4904 break;
4905 case kCondGE:
4906 if (gt_bias) {
4907 __ CmpUleS(FTMP, rhs, lhs);
4908 } else {
4909 __ CmpLeS(FTMP, rhs, lhs);
4910 }
4911 __ Bc1nez(FTMP, label);
4912 break;
4913 default:
4914 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004915 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004916 }
4917 } else {
4918 switch (cond) {
4919 case kCondEQ:
4920 __ CeqS(0, lhs, rhs);
4921 __ Bc1t(0, label);
4922 break;
4923 case kCondNE:
4924 __ CeqS(0, lhs, rhs);
4925 __ Bc1f(0, label);
4926 break;
4927 case kCondLT:
4928 if (gt_bias) {
4929 __ ColtS(0, lhs, rhs);
4930 } else {
4931 __ CultS(0, lhs, rhs);
4932 }
4933 __ Bc1t(0, label);
4934 break;
4935 case kCondLE:
4936 if (gt_bias) {
4937 __ ColeS(0, lhs, rhs);
4938 } else {
4939 __ CuleS(0, lhs, rhs);
4940 }
4941 __ Bc1t(0, label);
4942 break;
4943 case kCondGT:
4944 if (gt_bias) {
4945 __ CultS(0, rhs, lhs);
4946 } else {
4947 __ ColtS(0, rhs, lhs);
4948 }
4949 __ Bc1t(0, label);
4950 break;
4951 case kCondGE:
4952 if (gt_bias) {
4953 __ CuleS(0, rhs, lhs);
4954 } else {
4955 __ ColeS(0, rhs, lhs);
4956 }
4957 __ Bc1t(0, label);
4958 break;
4959 default:
4960 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004961 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004962 }
4963 }
4964 } else {
4965 DCHECK_EQ(type, Primitive::kPrimDouble);
4966 if (isR6) {
4967 switch (cond) {
4968 case kCondEQ:
4969 __ CmpEqD(FTMP, lhs, rhs);
4970 __ Bc1nez(FTMP, label);
4971 break;
4972 case kCondNE:
4973 __ CmpEqD(FTMP, lhs, rhs);
4974 __ Bc1eqz(FTMP, label);
4975 break;
4976 case kCondLT:
4977 if (gt_bias) {
4978 __ CmpLtD(FTMP, lhs, rhs);
4979 } else {
4980 __ CmpUltD(FTMP, lhs, rhs);
4981 }
4982 __ Bc1nez(FTMP, label);
4983 break;
4984 case kCondLE:
4985 if (gt_bias) {
4986 __ CmpLeD(FTMP, lhs, rhs);
4987 } else {
4988 __ CmpUleD(FTMP, lhs, rhs);
4989 }
4990 __ Bc1nez(FTMP, label);
4991 break;
4992 case kCondGT:
4993 if (gt_bias) {
4994 __ CmpUltD(FTMP, rhs, lhs);
4995 } else {
4996 __ CmpLtD(FTMP, rhs, lhs);
4997 }
4998 __ Bc1nez(FTMP, label);
4999 break;
5000 case kCondGE:
5001 if (gt_bias) {
5002 __ CmpUleD(FTMP, rhs, lhs);
5003 } else {
5004 __ CmpLeD(FTMP, rhs, lhs);
5005 }
5006 __ Bc1nez(FTMP, label);
5007 break;
5008 default:
5009 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005010 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005011 }
5012 } else {
5013 switch (cond) {
5014 case kCondEQ:
5015 __ CeqD(0, lhs, rhs);
5016 __ Bc1t(0, label);
5017 break;
5018 case kCondNE:
5019 __ CeqD(0, lhs, rhs);
5020 __ Bc1f(0, label);
5021 break;
5022 case kCondLT:
5023 if (gt_bias) {
5024 __ ColtD(0, lhs, rhs);
5025 } else {
5026 __ CultD(0, lhs, rhs);
5027 }
5028 __ Bc1t(0, label);
5029 break;
5030 case kCondLE:
5031 if (gt_bias) {
5032 __ ColeD(0, lhs, rhs);
5033 } else {
5034 __ CuleD(0, lhs, rhs);
5035 }
5036 __ Bc1t(0, label);
5037 break;
5038 case kCondGT:
5039 if (gt_bias) {
5040 __ CultD(0, rhs, lhs);
5041 } else {
5042 __ ColtD(0, rhs, lhs);
5043 }
5044 __ Bc1t(0, label);
5045 break;
5046 case kCondGE:
5047 if (gt_bias) {
5048 __ CuleD(0, rhs, lhs);
5049 } else {
5050 __ ColeD(0, rhs, lhs);
5051 }
5052 __ Bc1t(0, label);
5053 break;
5054 default:
5055 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005056 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005057 }
5058 }
5059 }
5060}
5061
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005062void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005063 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005064 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005065 MipsLabel* false_target) {
5066 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005067
David Brazdil0debae72015-11-12 18:37:00 +00005068 if (true_target == nullptr && false_target == nullptr) {
5069 // Nothing to do. The code always falls through.
5070 return;
5071 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005072 // Constant condition, statically compared against "true" (integer value 1).
5073 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005074 if (true_target != nullptr) {
5075 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005076 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005077 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005078 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005079 if (false_target != nullptr) {
5080 __ B(false_target);
5081 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005082 }
David Brazdil0debae72015-11-12 18:37:00 +00005083 return;
5084 }
5085
5086 // The following code generates these patterns:
5087 // (1) true_target == nullptr && false_target != nullptr
5088 // - opposite condition true => branch to false_target
5089 // (2) true_target != nullptr && false_target == nullptr
5090 // - condition true => branch to true_target
5091 // (3) true_target != nullptr && false_target != nullptr
5092 // - condition true => branch to true_target
5093 // - branch to false_target
5094 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005095 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005096 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005097 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005098 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005099 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5100 } else {
5101 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5102 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005103 } else {
5104 // The condition instruction has not been materialized, use its inputs as
5105 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005106 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005107 Primitive::Type type = condition->InputAt(0)->GetType();
5108 LocationSummary* locations = cond->GetLocations();
5109 IfCondition if_cond = condition->GetCondition();
5110 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005111
David Brazdil0debae72015-11-12 18:37:00 +00005112 if (true_target == nullptr) {
5113 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005114 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005115 }
5116
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005117 switch (type) {
5118 default:
5119 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5120 break;
5121 case Primitive::kPrimLong:
5122 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5123 break;
5124 case Primitive::kPrimFloat:
5125 case Primitive::kPrimDouble:
5126 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5127 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005128 }
5129 }
David Brazdil0debae72015-11-12 18:37:00 +00005130
5131 // If neither branch falls through (case 3), the conditional branch to `true_target`
5132 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5133 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005134 __ B(false_target);
5135 }
5136}
5137
5138void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5139 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005140 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005141 locations->SetInAt(0, Location::RequiresRegister());
5142 }
5143}
5144
5145void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005146 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5147 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5148 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5149 nullptr : codegen_->GetLabelOf(true_successor);
5150 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5151 nullptr : codegen_->GetLabelOf(false_successor);
5152 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005153}
5154
5155void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5156 LocationSummary* locations = new (GetGraph()->GetArena())
5157 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005158 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00005159 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005160 locations->SetInAt(0, Location::RequiresRegister());
5161 }
5162}
5163
5164void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005165 SlowPathCodeMIPS* slow_path =
5166 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005167 GenerateTestAndBranch(deoptimize,
5168 /* condition_input_index */ 0,
5169 slow_path->GetEntryLabel(),
5170 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005171}
5172
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005173// This function returns true if a conditional move can be generated for HSelect.
5174// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5175// branches and regular moves.
5176//
5177// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5178//
5179// While determining feasibility of a conditional move and setting inputs/outputs
5180// are two distinct tasks, this function does both because they share quite a bit
5181// of common logic.
5182static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5183 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5184 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5185 HCondition* condition = cond->AsCondition();
5186
5187 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5188 Primitive::Type dst_type = select->GetType();
5189
5190 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5191 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5192 bool is_true_value_zero_constant =
5193 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5194 bool is_false_value_zero_constant =
5195 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5196
5197 bool can_move_conditionally = false;
5198 bool use_const_for_false_in = false;
5199 bool use_const_for_true_in = false;
5200
5201 if (!cond->IsConstant()) {
5202 switch (cond_type) {
5203 default:
5204 switch (dst_type) {
5205 default:
5206 // Moving int on int condition.
5207 if (is_r6) {
5208 if (is_true_value_zero_constant) {
5209 // seleqz out_reg, false_reg, cond_reg
5210 can_move_conditionally = true;
5211 use_const_for_true_in = true;
5212 } else if (is_false_value_zero_constant) {
5213 // selnez out_reg, true_reg, cond_reg
5214 can_move_conditionally = true;
5215 use_const_for_false_in = true;
5216 } else if (materialized) {
5217 // Not materializing unmaterialized int conditions
5218 // to keep the instruction count low.
5219 // selnez AT, true_reg, cond_reg
5220 // seleqz TMP, false_reg, cond_reg
5221 // or out_reg, AT, TMP
5222 can_move_conditionally = true;
5223 }
5224 } else {
5225 // movn out_reg, true_reg/ZERO, cond_reg
5226 can_move_conditionally = true;
5227 use_const_for_true_in = is_true_value_zero_constant;
5228 }
5229 break;
5230 case Primitive::kPrimLong:
5231 // Moving long on int condition.
5232 if (is_r6) {
5233 if (is_true_value_zero_constant) {
5234 // seleqz out_reg_lo, false_reg_lo, cond_reg
5235 // seleqz out_reg_hi, false_reg_hi, cond_reg
5236 can_move_conditionally = true;
5237 use_const_for_true_in = true;
5238 } else if (is_false_value_zero_constant) {
5239 // selnez out_reg_lo, true_reg_lo, cond_reg
5240 // selnez out_reg_hi, true_reg_hi, cond_reg
5241 can_move_conditionally = true;
5242 use_const_for_false_in = true;
5243 }
5244 // Other long conditional moves would generate 6+ instructions,
5245 // which is too many.
5246 } else {
5247 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5248 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5249 can_move_conditionally = true;
5250 use_const_for_true_in = is_true_value_zero_constant;
5251 }
5252 break;
5253 case Primitive::kPrimFloat:
5254 case Primitive::kPrimDouble:
5255 // Moving float/double on int condition.
5256 if (is_r6) {
5257 if (materialized) {
5258 // Not materializing unmaterialized int conditions
5259 // to keep the instruction count low.
5260 can_move_conditionally = true;
5261 if (is_true_value_zero_constant) {
5262 // sltu TMP, ZERO, cond_reg
5263 // mtc1 TMP, temp_cond_reg
5264 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5265 use_const_for_true_in = true;
5266 } else if (is_false_value_zero_constant) {
5267 // sltu TMP, ZERO, cond_reg
5268 // mtc1 TMP, temp_cond_reg
5269 // selnez.fmt out_reg, true_reg, temp_cond_reg
5270 use_const_for_false_in = true;
5271 } else {
5272 // sltu TMP, ZERO, cond_reg
5273 // mtc1 TMP, temp_cond_reg
5274 // sel.fmt temp_cond_reg, false_reg, true_reg
5275 // mov.fmt out_reg, temp_cond_reg
5276 }
5277 }
5278 } else {
5279 // movn.fmt out_reg, true_reg, cond_reg
5280 can_move_conditionally = true;
5281 }
5282 break;
5283 }
5284 break;
5285 case Primitive::kPrimLong:
5286 // We don't materialize long comparison now
5287 // and use conditional branches instead.
5288 break;
5289 case Primitive::kPrimFloat:
5290 case Primitive::kPrimDouble:
5291 switch (dst_type) {
5292 default:
5293 // Moving int on float/double condition.
5294 if (is_r6) {
5295 if (is_true_value_zero_constant) {
5296 // mfc1 TMP, temp_cond_reg
5297 // seleqz out_reg, false_reg, TMP
5298 can_move_conditionally = true;
5299 use_const_for_true_in = true;
5300 } else if (is_false_value_zero_constant) {
5301 // mfc1 TMP, temp_cond_reg
5302 // selnez out_reg, true_reg, TMP
5303 can_move_conditionally = true;
5304 use_const_for_false_in = true;
5305 } else {
5306 // mfc1 TMP, temp_cond_reg
5307 // selnez AT, true_reg, TMP
5308 // seleqz TMP, false_reg, TMP
5309 // or out_reg, AT, TMP
5310 can_move_conditionally = true;
5311 }
5312 } else {
5313 // movt out_reg, true_reg/ZERO, cc
5314 can_move_conditionally = true;
5315 use_const_for_true_in = is_true_value_zero_constant;
5316 }
5317 break;
5318 case Primitive::kPrimLong:
5319 // Moving long on float/double condition.
5320 if (is_r6) {
5321 if (is_true_value_zero_constant) {
5322 // mfc1 TMP, temp_cond_reg
5323 // seleqz out_reg_lo, false_reg_lo, TMP
5324 // seleqz out_reg_hi, false_reg_hi, TMP
5325 can_move_conditionally = true;
5326 use_const_for_true_in = true;
5327 } else if (is_false_value_zero_constant) {
5328 // mfc1 TMP, temp_cond_reg
5329 // selnez out_reg_lo, true_reg_lo, TMP
5330 // selnez out_reg_hi, true_reg_hi, TMP
5331 can_move_conditionally = true;
5332 use_const_for_false_in = true;
5333 }
5334 // Other long conditional moves would generate 6+ instructions,
5335 // which is too many.
5336 } else {
5337 // movt out_reg_lo, true_reg_lo/ZERO, cc
5338 // movt out_reg_hi, true_reg_hi/ZERO, cc
5339 can_move_conditionally = true;
5340 use_const_for_true_in = is_true_value_zero_constant;
5341 }
5342 break;
5343 case Primitive::kPrimFloat:
5344 case Primitive::kPrimDouble:
5345 // Moving float/double on float/double condition.
5346 if (is_r6) {
5347 can_move_conditionally = true;
5348 if (is_true_value_zero_constant) {
5349 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5350 use_const_for_true_in = true;
5351 } else if (is_false_value_zero_constant) {
5352 // selnez.fmt out_reg, true_reg, temp_cond_reg
5353 use_const_for_false_in = true;
5354 } else {
5355 // sel.fmt temp_cond_reg, false_reg, true_reg
5356 // mov.fmt out_reg, temp_cond_reg
5357 }
5358 } else {
5359 // movt.fmt out_reg, true_reg, cc
5360 can_move_conditionally = true;
5361 }
5362 break;
5363 }
5364 break;
5365 }
5366 }
5367
5368 if (can_move_conditionally) {
5369 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5370 } else {
5371 DCHECK(!use_const_for_false_in);
5372 DCHECK(!use_const_for_true_in);
5373 }
5374
5375 if (locations_to_set != nullptr) {
5376 if (use_const_for_false_in) {
5377 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5378 } else {
5379 locations_to_set->SetInAt(0,
5380 Primitive::IsFloatingPointType(dst_type)
5381 ? Location::RequiresFpuRegister()
5382 : Location::RequiresRegister());
5383 }
5384 if (use_const_for_true_in) {
5385 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5386 } else {
5387 locations_to_set->SetInAt(1,
5388 Primitive::IsFloatingPointType(dst_type)
5389 ? Location::RequiresFpuRegister()
5390 : Location::RequiresRegister());
5391 }
5392 if (materialized) {
5393 locations_to_set->SetInAt(2, Location::RequiresRegister());
5394 }
5395 // On R6 we don't require the output to be the same as the
5396 // first input for conditional moves unlike on R2.
5397 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5398 if (is_out_same_as_first_in) {
5399 locations_to_set->SetOut(Location::SameAsFirstInput());
5400 } else {
5401 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5402 ? Location::RequiresFpuRegister()
5403 : Location::RequiresRegister());
5404 }
5405 }
5406
5407 return can_move_conditionally;
5408}
5409
5410void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5411 LocationSummary* locations = select->GetLocations();
5412 Location dst = locations->Out();
5413 Location src = locations->InAt(1);
5414 Register src_reg = ZERO;
5415 Register src_reg_high = ZERO;
5416 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5417 Register cond_reg = TMP;
5418 int cond_cc = 0;
5419 Primitive::Type cond_type = Primitive::kPrimInt;
5420 bool cond_inverted = false;
5421 Primitive::Type dst_type = select->GetType();
5422
5423 if (IsBooleanValueOrMaterializedCondition(cond)) {
5424 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5425 } else {
5426 HCondition* condition = cond->AsCondition();
5427 LocationSummary* cond_locations = cond->GetLocations();
5428 IfCondition if_cond = condition->GetCondition();
5429 cond_type = condition->InputAt(0)->GetType();
5430 switch (cond_type) {
5431 default:
5432 DCHECK_NE(cond_type, Primitive::kPrimLong);
5433 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5434 break;
5435 case Primitive::kPrimFloat:
5436 case Primitive::kPrimDouble:
5437 cond_inverted = MaterializeFpCompareR2(if_cond,
5438 condition->IsGtBias(),
5439 cond_type,
5440 cond_locations,
5441 cond_cc);
5442 break;
5443 }
5444 }
5445
5446 DCHECK(dst.Equals(locations->InAt(0)));
5447 if (src.IsRegister()) {
5448 src_reg = src.AsRegister<Register>();
5449 } else if (src.IsRegisterPair()) {
5450 src_reg = src.AsRegisterPairLow<Register>();
5451 src_reg_high = src.AsRegisterPairHigh<Register>();
5452 } else if (src.IsConstant()) {
5453 DCHECK(src.GetConstant()->IsZeroBitPattern());
5454 }
5455
5456 switch (cond_type) {
5457 default:
5458 switch (dst_type) {
5459 default:
5460 if (cond_inverted) {
5461 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5462 } else {
5463 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5464 }
5465 break;
5466 case Primitive::kPrimLong:
5467 if (cond_inverted) {
5468 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5469 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5470 } else {
5471 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5472 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5473 }
5474 break;
5475 case Primitive::kPrimFloat:
5476 if (cond_inverted) {
5477 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5478 } else {
5479 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5480 }
5481 break;
5482 case Primitive::kPrimDouble:
5483 if (cond_inverted) {
5484 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5485 } else {
5486 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5487 }
5488 break;
5489 }
5490 break;
5491 case Primitive::kPrimLong:
5492 LOG(FATAL) << "Unreachable";
5493 UNREACHABLE();
5494 case Primitive::kPrimFloat:
5495 case Primitive::kPrimDouble:
5496 switch (dst_type) {
5497 default:
5498 if (cond_inverted) {
5499 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5500 } else {
5501 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5502 }
5503 break;
5504 case Primitive::kPrimLong:
5505 if (cond_inverted) {
5506 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5507 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5508 } else {
5509 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5510 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5511 }
5512 break;
5513 case Primitive::kPrimFloat:
5514 if (cond_inverted) {
5515 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5516 } else {
5517 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5518 }
5519 break;
5520 case Primitive::kPrimDouble:
5521 if (cond_inverted) {
5522 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5523 } else {
5524 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5525 }
5526 break;
5527 }
5528 break;
5529 }
5530}
5531
5532void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5533 LocationSummary* locations = select->GetLocations();
5534 Location dst = locations->Out();
5535 Location false_src = locations->InAt(0);
5536 Location true_src = locations->InAt(1);
5537 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5538 Register cond_reg = TMP;
5539 FRegister fcond_reg = FTMP;
5540 Primitive::Type cond_type = Primitive::kPrimInt;
5541 bool cond_inverted = false;
5542 Primitive::Type dst_type = select->GetType();
5543
5544 if (IsBooleanValueOrMaterializedCondition(cond)) {
5545 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5546 } else {
5547 HCondition* condition = cond->AsCondition();
5548 LocationSummary* cond_locations = cond->GetLocations();
5549 IfCondition if_cond = condition->GetCondition();
5550 cond_type = condition->InputAt(0)->GetType();
5551 switch (cond_type) {
5552 default:
5553 DCHECK_NE(cond_type, Primitive::kPrimLong);
5554 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5555 break;
5556 case Primitive::kPrimFloat:
5557 case Primitive::kPrimDouble:
5558 cond_inverted = MaterializeFpCompareR6(if_cond,
5559 condition->IsGtBias(),
5560 cond_type,
5561 cond_locations,
5562 fcond_reg);
5563 break;
5564 }
5565 }
5566
5567 if (true_src.IsConstant()) {
5568 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5569 }
5570 if (false_src.IsConstant()) {
5571 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5572 }
5573
5574 switch (dst_type) {
5575 default:
5576 if (Primitive::IsFloatingPointType(cond_type)) {
5577 __ Mfc1(cond_reg, fcond_reg);
5578 }
5579 if (true_src.IsConstant()) {
5580 if (cond_inverted) {
5581 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5582 } else {
5583 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5584 }
5585 } else if (false_src.IsConstant()) {
5586 if (cond_inverted) {
5587 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5588 } else {
5589 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5590 }
5591 } else {
5592 DCHECK_NE(cond_reg, AT);
5593 if (cond_inverted) {
5594 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5595 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5596 } else {
5597 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5598 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5599 }
5600 __ Or(dst.AsRegister<Register>(), AT, TMP);
5601 }
5602 break;
5603 case Primitive::kPrimLong: {
5604 if (Primitive::IsFloatingPointType(cond_type)) {
5605 __ Mfc1(cond_reg, fcond_reg);
5606 }
5607 Register dst_lo = dst.AsRegisterPairLow<Register>();
5608 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5609 if (true_src.IsConstant()) {
5610 Register src_lo = false_src.AsRegisterPairLow<Register>();
5611 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5612 if (cond_inverted) {
5613 __ Selnez(dst_lo, src_lo, cond_reg);
5614 __ Selnez(dst_hi, src_hi, cond_reg);
5615 } else {
5616 __ Seleqz(dst_lo, src_lo, cond_reg);
5617 __ Seleqz(dst_hi, src_hi, cond_reg);
5618 }
5619 } else {
5620 DCHECK(false_src.IsConstant());
5621 Register src_lo = true_src.AsRegisterPairLow<Register>();
5622 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5623 if (cond_inverted) {
5624 __ Seleqz(dst_lo, src_lo, cond_reg);
5625 __ Seleqz(dst_hi, src_hi, cond_reg);
5626 } else {
5627 __ Selnez(dst_lo, src_lo, cond_reg);
5628 __ Selnez(dst_hi, src_hi, cond_reg);
5629 }
5630 }
5631 break;
5632 }
5633 case Primitive::kPrimFloat: {
5634 if (!Primitive::IsFloatingPointType(cond_type)) {
5635 // sel*.fmt tests bit 0 of the condition register, account for that.
5636 __ Sltu(TMP, ZERO, cond_reg);
5637 __ Mtc1(TMP, fcond_reg);
5638 }
5639 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5640 if (true_src.IsConstant()) {
5641 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5642 if (cond_inverted) {
5643 __ SelnezS(dst_reg, src_reg, fcond_reg);
5644 } else {
5645 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5646 }
5647 } else if (false_src.IsConstant()) {
5648 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5649 if (cond_inverted) {
5650 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5651 } else {
5652 __ SelnezS(dst_reg, src_reg, fcond_reg);
5653 }
5654 } else {
5655 if (cond_inverted) {
5656 __ SelS(fcond_reg,
5657 true_src.AsFpuRegister<FRegister>(),
5658 false_src.AsFpuRegister<FRegister>());
5659 } else {
5660 __ SelS(fcond_reg,
5661 false_src.AsFpuRegister<FRegister>(),
5662 true_src.AsFpuRegister<FRegister>());
5663 }
5664 __ MovS(dst_reg, fcond_reg);
5665 }
5666 break;
5667 }
5668 case Primitive::kPrimDouble: {
5669 if (!Primitive::IsFloatingPointType(cond_type)) {
5670 // sel*.fmt tests bit 0 of the condition register, account for that.
5671 __ Sltu(TMP, ZERO, cond_reg);
5672 __ Mtc1(TMP, fcond_reg);
5673 }
5674 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5675 if (true_src.IsConstant()) {
5676 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5677 if (cond_inverted) {
5678 __ SelnezD(dst_reg, src_reg, fcond_reg);
5679 } else {
5680 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5681 }
5682 } else if (false_src.IsConstant()) {
5683 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5684 if (cond_inverted) {
5685 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5686 } else {
5687 __ SelnezD(dst_reg, src_reg, fcond_reg);
5688 }
5689 } else {
5690 if (cond_inverted) {
5691 __ SelD(fcond_reg,
5692 true_src.AsFpuRegister<FRegister>(),
5693 false_src.AsFpuRegister<FRegister>());
5694 } else {
5695 __ SelD(fcond_reg,
5696 false_src.AsFpuRegister<FRegister>(),
5697 true_src.AsFpuRegister<FRegister>());
5698 }
5699 __ MovD(dst_reg, fcond_reg);
5700 }
5701 break;
5702 }
5703 }
5704}
5705
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005706void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5707 LocationSummary* locations = new (GetGraph()->GetArena())
5708 LocationSummary(flag, LocationSummary::kNoCall);
5709 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005710}
5711
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005712void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5713 __ LoadFromOffset(kLoadWord,
5714 flag->GetLocations()->Out().AsRegister<Register>(),
5715 SP,
5716 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005717}
5718
David Brazdil74eb1b22015-12-14 11:44:01 +00005719void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005721 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005722}
5723
5724void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005725 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5726 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5727 if (is_r6) {
5728 GenConditionalMoveR6(select);
5729 } else {
5730 GenConditionalMoveR2(select);
5731 }
5732 } else {
5733 LocationSummary* locations = select->GetLocations();
5734 MipsLabel false_target;
5735 GenerateTestAndBranch(select,
5736 /* condition_input_index */ 2,
5737 /* true_target */ nullptr,
5738 &false_target);
5739 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5740 __ Bind(&false_target);
5741 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005742}
5743
David Srbecky0cf44932015-12-09 14:09:59 +00005744void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5745 new (GetGraph()->GetArena()) LocationSummary(info);
5746}
5747
David Srbeckyd28f4a02016-03-14 17:14:24 +00005748void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5749 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005750}
5751
5752void CodeGeneratorMIPS::GenerateNop() {
5753 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005754}
5755
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005756void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5757 Primitive::Type field_type = field_info.GetFieldType();
5758 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5759 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08005760 bool object_field_get_with_read_barrier =
5761 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005762 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08005763 instruction,
5764 generate_volatile
5765 ? LocationSummary::kCallOnMainOnly
5766 : (object_field_get_with_read_barrier
5767 ? LocationSummary::kCallOnSlowPath
5768 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005769
5770 locations->SetInAt(0, Location::RequiresRegister());
5771 if (generate_volatile) {
5772 InvokeRuntimeCallingConvention calling_convention;
5773 // need A0 to hold base + offset
5774 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5775 if (field_type == Primitive::kPrimLong) {
5776 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
5777 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005778 // Use Location::Any() to prevent situations when running out of available fp registers.
5779 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005780 // Need some temp core regs since FP results are returned in core registers
5781 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
5782 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
5783 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
5784 }
5785 } else {
5786 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5787 locations->SetOut(Location::RequiresFpuRegister());
5788 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005789 // The output overlaps in the case of an object field get with
5790 // read barriers enabled: we do not want the move to overwrite the
5791 // object's location, as we need it to emit the read barrier.
5792 locations->SetOut(Location::RequiresRegister(),
5793 object_field_get_with_read_barrier
5794 ? Location::kOutputOverlap
5795 : Location::kNoOutputOverlap);
5796 }
5797 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5798 // We need a temporary register for the read barrier marking slow
5799 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
5800 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801 }
5802 }
5803}
5804
5805void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
5806 const FieldInfo& field_info,
5807 uint32_t dex_pc) {
5808 Primitive::Type type = field_info.GetFieldType();
5809 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005810 Location obj_loc = locations->InAt(0);
5811 Register obj = obj_loc.AsRegister<Register>();
5812 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005813 LoadOperandType load_type = kLoadUnsignedByte;
5814 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005815 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005816 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005817
5818 switch (type) {
5819 case Primitive::kPrimBoolean:
5820 load_type = kLoadUnsignedByte;
5821 break;
5822 case Primitive::kPrimByte:
5823 load_type = kLoadSignedByte;
5824 break;
5825 case Primitive::kPrimShort:
5826 load_type = kLoadSignedHalfword;
5827 break;
5828 case Primitive::kPrimChar:
5829 load_type = kLoadUnsignedHalfword;
5830 break;
5831 case Primitive::kPrimInt:
5832 case Primitive::kPrimFloat:
5833 case Primitive::kPrimNot:
5834 load_type = kLoadWord;
5835 break;
5836 case Primitive::kPrimLong:
5837 case Primitive::kPrimDouble:
5838 load_type = kLoadDoubleword;
5839 break;
5840 case Primitive::kPrimVoid:
5841 LOG(FATAL) << "Unreachable type " << type;
5842 UNREACHABLE();
5843 }
5844
5845 if (is_volatile && load_type == kLoadDoubleword) {
5846 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005847 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005848 // Do implicit Null check
5849 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5850 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01005851 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005852 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
5853 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005854 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08005855 if (dst_loc.IsFpuRegister()) {
5856 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005857 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08005858 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005859 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005860 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005861 __ StoreToOffset(kStoreWord,
5862 locations->GetTemp(1).AsRegister<Register>(),
5863 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005864 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005865 __ StoreToOffset(kStoreWord,
5866 locations->GetTemp(2).AsRegister<Register>(),
5867 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005868 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005869 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005870 }
5871 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005872 if (type == Primitive::kPrimNot) {
5873 // /* HeapReference<Object> */ dst = *(obj + offset)
5874 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5875 Location temp_loc = locations->GetTemp(0);
5876 // Note that a potential implicit null check is handled in this
5877 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
5878 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5879 dst_loc,
5880 obj,
5881 offset,
5882 temp_loc,
5883 /* needs_null_check */ true);
5884 if (is_volatile) {
5885 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5886 }
5887 } else {
5888 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
5889 if (is_volatile) {
5890 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5891 }
5892 // If read barriers are enabled, emit read barriers other than
5893 // Baker's using a slow path (and also unpoison the loaded
5894 // reference, if heap poisoning is enabled).
5895 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
5896 }
5897 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005898 Register dst;
5899 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08005900 DCHECK(dst_loc.IsRegisterPair());
5901 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005902 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005903 DCHECK(dst_loc.IsRegister());
5904 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005905 }
Alexey Frunze2923db72016-08-20 01:55:47 -07005906 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005907 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005908 DCHECK(dst_loc.IsFpuRegister());
5909 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005910 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005911 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005912 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005913 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005914 }
5915 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005916 }
5917
Alexey Frunze15958152017-02-09 19:08:30 -08005918 // Memory barriers, in the case of references, are handled in the
5919 // previous switch statement.
5920 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005921 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5922 }
5923}
5924
5925void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5926 Primitive::Type field_type = field_info.GetFieldType();
5927 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5928 bool generate_volatile = field_info.IsVolatile() && is_wide;
5929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005930 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005931
5932 locations->SetInAt(0, Location::RequiresRegister());
5933 if (generate_volatile) {
5934 InvokeRuntimeCallingConvention calling_convention;
5935 // need A0 to hold base + offset
5936 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5937 if (field_type == Primitive::kPrimLong) {
5938 locations->SetInAt(1, Location::RegisterPairLocation(
5939 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5940 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005941 // Use Location::Any() to prevent situations when running out of available fp registers.
5942 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005943 // Pass FP parameters in core registers.
5944 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5945 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
5946 }
5947 } else {
5948 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005949 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005950 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005951 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005952 }
5953 }
5954}
5955
5956void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
5957 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01005958 uint32_t dex_pc,
5959 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005960 Primitive::Type type = field_info.GetFieldType();
5961 LocationSummary* locations = instruction->GetLocations();
5962 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07005963 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005964 StoreOperandType store_type = kStoreByte;
5965 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005966 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08005967 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005968 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005969
5970 switch (type) {
5971 case Primitive::kPrimBoolean:
5972 case Primitive::kPrimByte:
5973 store_type = kStoreByte;
5974 break;
5975 case Primitive::kPrimShort:
5976 case Primitive::kPrimChar:
5977 store_type = kStoreHalfword;
5978 break;
5979 case Primitive::kPrimInt:
5980 case Primitive::kPrimFloat:
5981 case Primitive::kPrimNot:
5982 store_type = kStoreWord;
5983 break;
5984 case Primitive::kPrimLong:
5985 case Primitive::kPrimDouble:
5986 store_type = kStoreDoubleword;
5987 break;
5988 case Primitive::kPrimVoid:
5989 LOG(FATAL) << "Unreachable type " << type;
5990 UNREACHABLE();
5991 }
5992
5993 if (is_volatile) {
5994 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5995 }
5996
5997 if (is_volatile && store_type == kStoreDoubleword) {
5998 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005999 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006000 // Do implicit Null check.
6001 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6002 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6003 if (type == Primitive::kPrimDouble) {
6004 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006005 if (value_location.IsFpuRegister()) {
6006 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6007 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006008 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006009 value_location.AsFpuRegister<FRegister>());
6010 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006011 __ LoadFromOffset(kLoadWord,
6012 locations->GetTemp(1).AsRegister<Register>(),
6013 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006014 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006015 __ LoadFromOffset(kLoadWord,
6016 locations->GetTemp(2).AsRegister<Register>(),
6017 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006018 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006019 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006020 DCHECK(value_location.IsConstant());
6021 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6022 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006023 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6024 locations->GetTemp(1).AsRegister<Register>(),
6025 value);
6026 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006027 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006028 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006029 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6030 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006031 if (value_location.IsConstant()) {
6032 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6033 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6034 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006035 Register src;
6036 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006037 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006038 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006039 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006040 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006041 if (kPoisonHeapReferences && needs_write_barrier) {
6042 // Note that in the case where `value` is a null reference,
6043 // we do not enter this block, as a null reference does not
6044 // need poisoning.
6045 DCHECK_EQ(type, Primitive::kPrimNot);
6046 __ PoisonHeapReference(TMP, src);
6047 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6048 } else {
6049 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6050 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006051 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006052 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006053 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006054 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006055 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006056 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006057 }
6058 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006059 }
6060
Alexey Frunzec061de12017-02-14 13:27:23 -08006061 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006062 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006063 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006064 }
6065
6066 if (is_volatile) {
6067 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6068 }
6069}
6070
6071void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6072 HandleFieldGet(instruction, instruction->GetFieldInfo());
6073}
6074
6075void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6076 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6077}
6078
6079void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6080 HandleFieldSet(instruction, instruction->GetFieldInfo());
6081}
6082
6083void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006084 HandleFieldSet(instruction,
6085 instruction->GetFieldInfo(),
6086 instruction->GetDexPc(),
6087 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006088}
6089
Alexey Frunze15958152017-02-09 19:08:30 -08006090void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6091 HInstruction* instruction,
6092 Location out,
6093 uint32_t offset,
6094 Location maybe_temp,
6095 ReadBarrierOption read_barrier_option) {
6096 Register out_reg = out.AsRegister<Register>();
6097 if (read_barrier_option == kWithReadBarrier) {
6098 CHECK(kEmitCompilerReadBarrier);
6099 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6100 if (kUseBakerReadBarrier) {
6101 // Load with fast path based Baker's read barrier.
6102 // /* HeapReference<Object> */ out = *(out + offset)
6103 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6104 out,
6105 out_reg,
6106 offset,
6107 maybe_temp,
6108 /* needs_null_check */ false);
6109 } else {
6110 // Load with slow path based read barrier.
6111 // Save the value of `out` into `maybe_temp` before overwriting it
6112 // in the following move operation, as we will need it for the
6113 // read barrier below.
6114 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6115 // /* HeapReference<Object> */ out = *(out + offset)
6116 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6117 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6118 }
6119 } else {
6120 // Plain load with no read barrier.
6121 // /* HeapReference<Object> */ out = *(out + offset)
6122 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6123 __ MaybeUnpoisonHeapReference(out_reg);
6124 }
6125}
6126
6127void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6128 HInstruction* instruction,
6129 Location out,
6130 Location obj,
6131 uint32_t offset,
6132 Location maybe_temp,
6133 ReadBarrierOption read_barrier_option) {
6134 Register out_reg = out.AsRegister<Register>();
6135 Register obj_reg = obj.AsRegister<Register>();
6136 if (read_barrier_option == kWithReadBarrier) {
6137 CHECK(kEmitCompilerReadBarrier);
6138 if (kUseBakerReadBarrier) {
6139 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6140 // Load with fast path based Baker's read barrier.
6141 // /* HeapReference<Object> */ out = *(obj + offset)
6142 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6143 out,
6144 obj_reg,
6145 offset,
6146 maybe_temp,
6147 /* needs_null_check */ false);
6148 } else {
6149 // Load with slow path based read barrier.
6150 // /* HeapReference<Object> */ out = *(obj + offset)
6151 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6152 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6153 }
6154 } else {
6155 // Plain load with no read barrier.
6156 // /* HeapReference<Object> */ out = *(obj + offset)
6157 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6158 __ MaybeUnpoisonHeapReference(out_reg);
6159 }
6160}
6161
6162void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6163 Location root,
6164 Register obj,
6165 uint32_t offset,
6166 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006167 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006168 if (read_barrier_option == kWithReadBarrier) {
6169 DCHECK(kEmitCompilerReadBarrier);
6170 if (kUseBakerReadBarrier) {
6171 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6172 // Baker's read barrier are used:
6173 //
6174 // root = obj.field;
6175 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6176 // if (temp != null) {
6177 // root = temp(root)
6178 // }
6179
6180 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6181 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6182 static_assert(
6183 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6184 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6185 "have different sizes.");
6186 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6187 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6188 "have different sizes.");
6189
6190 // Slow path marking the GC root `root`.
6191 Location temp = Location::RegisterLocation(T9);
6192 SlowPathCodeMIPS* slow_path =
6193 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6194 instruction,
6195 root,
6196 /*entrypoint*/ temp);
6197 codegen_->AddSlowPath(slow_path);
6198
6199 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6200 const int32_t entry_point_offset =
6201 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6202 // Loading the entrypoint does not require a load acquire since it is only changed when
6203 // threads are suspended or running a checkpoint.
6204 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6205 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6206 // checking GetIsGcMarking.
6207 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6208 __ Bind(slow_path->GetExitLabel());
6209 } else {
6210 // GC root loaded through a slow path for read barriers other
6211 // than Baker's.
6212 // /* GcRoot<mirror::Object>* */ root = obj + offset
6213 __ Addiu32(root_reg, obj, offset);
6214 // /* mirror::Object* */ root = root->Read()
6215 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6216 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006217 } else {
6218 // Plain GC root load with no read barrier.
6219 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6220 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6221 // Note that GC roots are not affected by heap poisoning, thus we
6222 // do not have to unpoison `root_reg` here.
6223 }
6224}
6225
Alexey Frunze15958152017-02-09 19:08:30 -08006226void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6227 Location ref,
6228 Register obj,
6229 uint32_t offset,
6230 Location temp,
6231 bool needs_null_check) {
6232 DCHECK(kEmitCompilerReadBarrier);
6233 DCHECK(kUseBakerReadBarrier);
6234
6235 // /* HeapReference<Object> */ ref = *(obj + offset)
6236 Location no_index = Location::NoLocation();
6237 ScaleFactor no_scale_factor = TIMES_1;
6238 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6239 ref,
6240 obj,
6241 offset,
6242 no_index,
6243 no_scale_factor,
6244 temp,
6245 needs_null_check);
6246}
6247
6248void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6249 Location ref,
6250 Register obj,
6251 uint32_t data_offset,
6252 Location index,
6253 Location temp,
6254 bool needs_null_check) {
6255 DCHECK(kEmitCompilerReadBarrier);
6256 DCHECK(kUseBakerReadBarrier);
6257
6258 static_assert(
6259 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6260 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6261 // /* HeapReference<Object> */ ref =
6262 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6263 ScaleFactor scale_factor = TIMES_4;
6264 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6265 ref,
6266 obj,
6267 data_offset,
6268 index,
6269 scale_factor,
6270 temp,
6271 needs_null_check);
6272}
6273
6274void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6275 Location ref,
6276 Register obj,
6277 uint32_t offset,
6278 Location index,
6279 ScaleFactor scale_factor,
6280 Location temp,
6281 bool needs_null_check,
6282 bool always_update_field) {
6283 DCHECK(kEmitCompilerReadBarrier);
6284 DCHECK(kUseBakerReadBarrier);
6285
6286 // In slow path based read barriers, the read barrier call is
6287 // inserted after the original load. However, in fast path based
6288 // Baker's read barriers, we need to perform the load of
6289 // mirror::Object::monitor_ *before* the original reference load.
6290 // This load-load ordering is required by the read barrier.
6291 // The fast path/slow path (for Baker's algorithm) should look like:
6292 //
6293 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6294 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6295 // HeapReference<Object> ref = *src; // Original reference load.
6296 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6297 // if (is_gray) {
6298 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6299 // }
6300 //
6301 // Note: the original implementation in ReadBarrier::Barrier is
6302 // slightly more complex as it performs additional checks that we do
6303 // not do here for performance reasons.
6304
6305 Register ref_reg = ref.AsRegister<Register>();
6306 Register temp_reg = temp.AsRegister<Register>();
6307 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6308
6309 // /* int32_t */ monitor = obj->monitor_
6310 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6311 if (needs_null_check) {
6312 MaybeRecordImplicitNullCheck(instruction);
6313 }
6314 // /* LockWord */ lock_word = LockWord(monitor)
6315 static_assert(sizeof(LockWord) == sizeof(int32_t),
6316 "art::LockWord and int32_t have different sizes.");
6317
6318 __ Sync(0); // Barrier to prevent load-load reordering.
6319
6320 // The actual reference load.
6321 if (index.IsValid()) {
6322 // Load types involving an "index": ArrayGet,
6323 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6324 // intrinsics.
6325 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6326 if (index.IsConstant()) {
6327 size_t computed_offset =
6328 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6329 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6330 } else {
6331 // Handle the special case of the
6332 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6333 // intrinsics, which use a register pair as index ("long
6334 // offset"), of which only the low part contains data.
6335 Register index_reg = index.IsRegisterPair()
6336 ? index.AsRegisterPairLow<Register>()
6337 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006338 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006339 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6340 }
6341 } else {
6342 // /* HeapReference<Object> */ ref = *(obj + offset)
6343 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6344 }
6345
6346 // Object* ref = ref_addr->AsMirrorPtr()
6347 __ MaybeUnpoisonHeapReference(ref_reg);
6348
6349 // Slow path marking the object `ref` when it is gray.
6350 SlowPathCodeMIPS* slow_path;
6351 if (always_update_field) {
6352 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6353 // of the form `obj + field_offset`, where `obj` is a register and
6354 // `field_offset` is a register pair (of which only the lower half
6355 // is used). Thus `offset` and `scale_factor` above are expected
6356 // to be null in this code path.
6357 DCHECK_EQ(offset, 0u);
6358 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6359 slow_path = new (GetGraph()->GetArena())
6360 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6361 ref,
6362 obj,
6363 /* field_offset */ index,
6364 temp_reg);
6365 } else {
6366 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6367 }
6368 AddSlowPath(slow_path);
6369
6370 // if (rb_state == ReadBarrier::GrayState())
6371 // ref = ReadBarrier::Mark(ref);
6372 // Given the numeric representation, it's enough to check the low bit of the
6373 // rb_state. We do that by shifting the bit into the sign bit (31) and
6374 // performing a branch on less than zero.
6375 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6376 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6377 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6378 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6379 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6380 __ Bind(slow_path->GetExitLabel());
6381}
6382
6383void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6384 Location out,
6385 Location ref,
6386 Location obj,
6387 uint32_t offset,
6388 Location index) {
6389 DCHECK(kEmitCompilerReadBarrier);
6390
6391 // Insert a slow path based read barrier *after* the reference load.
6392 //
6393 // If heap poisoning is enabled, the unpoisoning of the loaded
6394 // reference will be carried out by the runtime within the slow
6395 // path.
6396 //
6397 // Note that `ref` currently does not get unpoisoned (when heap
6398 // poisoning is enabled), which is alright as the `ref` argument is
6399 // not used by the artReadBarrierSlow entry point.
6400 //
6401 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6402 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6403 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6404 AddSlowPath(slow_path);
6405
6406 __ B(slow_path->GetEntryLabel());
6407 __ Bind(slow_path->GetExitLabel());
6408}
6409
6410void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6411 Location out,
6412 Location ref,
6413 Location obj,
6414 uint32_t offset,
6415 Location index) {
6416 if (kEmitCompilerReadBarrier) {
6417 // Baker's read barriers shall be handled by the fast path
6418 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6419 DCHECK(!kUseBakerReadBarrier);
6420 // If heap poisoning is enabled, unpoisoning will be taken care of
6421 // by the runtime within the slow path.
6422 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6423 } else if (kPoisonHeapReferences) {
6424 __ UnpoisonHeapReference(out.AsRegister<Register>());
6425 }
6426}
6427
6428void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6429 Location out,
6430 Location root) {
6431 DCHECK(kEmitCompilerReadBarrier);
6432
6433 // Insert a slow path based read barrier *after* the GC root load.
6434 //
6435 // Note that GC roots are not affected by heap poisoning, so we do
6436 // not need to do anything special for this here.
6437 SlowPathCodeMIPS* slow_path =
6438 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6439 AddSlowPath(slow_path);
6440
6441 __ B(slow_path->GetEntryLabel());
6442 __ Bind(slow_path->GetExitLabel());
6443}
6444
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006445void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006446 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6447 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6448 switch (type_check_kind) {
6449 case TypeCheckKind::kExactCheck:
6450 case TypeCheckKind::kAbstractClassCheck:
6451 case TypeCheckKind::kClassHierarchyCheck:
6452 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006453 call_kind =
6454 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006455 break;
6456 case TypeCheckKind::kArrayCheck:
6457 case TypeCheckKind::kUnresolvedCheck:
6458 case TypeCheckKind::kInterfaceCheck:
6459 call_kind = LocationSummary::kCallOnSlowPath;
6460 break;
6461 }
6462
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6464 locations->SetInAt(0, Location::RequiresRegister());
6465 locations->SetInAt(1, Location::RequiresRegister());
6466 // The output does overlap inputs.
6467 // Note that TypeCheckSlowPathMIPS uses this register too.
6468 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006469 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006470}
6471
6472void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006473 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006474 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006475 Location obj_loc = locations->InAt(0);
6476 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006477 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006478 Location out_loc = locations->Out();
6479 Register out = out_loc.AsRegister<Register>();
6480 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6481 DCHECK_LE(num_temps, 1u);
6482 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006483 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6484 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6485 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6486 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006487 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006488 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006489
6490 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006491 // Avoid this check if we know `obj` is not null.
6492 if (instruction->MustDoNullCheck()) {
6493 __ Move(out, ZERO);
6494 __ Beqz(obj, &done);
6495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006496
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006497 switch (type_check_kind) {
6498 case TypeCheckKind::kExactCheck: {
6499 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006500 GenerateReferenceLoadTwoRegisters(instruction,
6501 out_loc,
6502 obj_loc,
6503 class_offset,
6504 maybe_temp_loc,
6505 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006506 // Classes must be equal for the instanceof to succeed.
6507 __ Xor(out, out, cls);
6508 __ Sltiu(out, out, 1);
6509 break;
6510 }
6511
6512 case TypeCheckKind::kAbstractClassCheck: {
6513 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006514 GenerateReferenceLoadTwoRegisters(instruction,
6515 out_loc,
6516 obj_loc,
6517 class_offset,
6518 maybe_temp_loc,
6519 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006520 // If the class is abstract, we eagerly fetch the super class of the
6521 // object to avoid doing a comparison we know will fail.
6522 MipsLabel loop;
6523 __ Bind(&loop);
6524 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006525 GenerateReferenceLoadOneRegister(instruction,
6526 out_loc,
6527 super_offset,
6528 maybe_temp_loc,
6529 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006530 // If `out` is null, we use it for the result, and jump to `done`.
6531 __ Beqz(out, &done);
6532 __ Bne(out, cls, &loop);
6533 __ LoadConst32(out, 1);
6534 break;
6535 }
6536
6537 case TypeCheckKind::kClassHierarchyCheck: {
6538 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006539 GenerateReferenceLoadTwoRegisters(instruction,
6540 out_loc,
6541 obj_loc,
6542 class_offset,
6543 maybe_temp_loc,
6544 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006545 // Walk over the class hierarchy to find a match.
6546 MipsLabel loop, success;
6547 __ Bind(&loop);
6548 __ Beq(out, cls, &success);
6549 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006550 GenerateReferenceLoadOneRegister(instruction,
6551 out_loc,
6552 super_offset,
6553 maybe_temp_loc,
6554 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006555 __ Bnez(out, &loop);
6556 // If `out` is null, we use it for the result, and jump to `done`.
6557 __ B(&done);
6558 __ Bind(&success);
6559 __ LoadConst32(out, 1);
6560 break;
6561 }
6562
6563 case TypeCheckKind::kArrayObjectCheck: {
6564 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006565 GenerateReferenceLoadTwoRegisters(instruction,
6566 out_loc,
6567 obj_loc,
6568 class_offset,
6569 maybe_temp_loc,
6570 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006571 // Do an exact check.
6572 MipsLabel success;
6573 __ Beq(out, cls, &success);
6574 // Otherwise, we need to check that the object's class is a non-primitive array.
6575 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006576 GenerateReferenceLoadOneRegister(instruction,
6577 out_loc,
6578 component_offset,
6579 maybe_temp_loc,
6580 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006581 // If `out` is null, we use it for the result, and jump to `done`.
6582 __ Beqz(out, &done);
6583 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6584 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6585 __ Sltiu(out, out, 1);
6586 __ B(&done);
6587 __ Bind(&success);
6588 __ LoadConst32(out, 1);
6589 break;
6590 }
6591
6592 case TypeCheckKind::kArrayCheck: {
6593 // No read barrier since the slow path will retry upon failure.
6594 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006595 GenerateReferenceLoadTwoRegisters(instruction,
6596 out_loc,
6597 obj_loc,
6598 class_offset,
6599 maybe_temp_loc,
6600 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006601 DCHECK(locations->OnlyCallsOnSlowPath());
6602 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6603 /* is_fatal */ false);
6604 codegen_->AddSlowPath(slow_path);
6605 __ Bne(out, cls, slow_path->GetEntryLabel());
6606 __ LoadConst32(out, 1);
6607 break;
6608 }
6609
6610 case TypeCheckKind::kUnresolvedCheck:
6611 case TypeCheckKind::kInterfaceCheck: {
6612 // Note that we indeed only call on slow path, but we always go
6613 // into the slow path for the unresolved and interface check
6614 // cases.
6615 //
6616 // We cannot directly call the InstanceofNonTrivial runtime
6617 // entry point without resorting to a type checking slow path
6618 // here (i.e. by calling InvokeRuntime directly), as it would
6619 // require to assign fixed registers for the inputs of this
6620 // HInstanceOf instruction (following the runtime calling
6621 // convention), which might be cluttered by the potential first
6622 // read barrier emission at the beginning of this method.
6623 //
6624 // TODO: Introduce a new runtime entry point taking the object
6625 // to test (instead of its class) as argument, and let it deal
6626 // with the read barrier issues. This will let us refactor this
6627 // case of the `switch` code as it was previously (with a direct
6628 // call to the runtime not using a type checking slow path).
6629 // This should also be beneficial for the other cases above.
6630 DCHECK(locations->OnlyCallsOnSlowPath());
6631 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6632 /* is_fatal */ false);
6633 codegen_->AddSlowPath(slow_path);
6634 __ B(slow_path->GetEntryLabel());
6635 break;
6636 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006637 }
6638
6639 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006640
6641 if (slow_path != nullptr) {
6642 __ Bind(slow_path->GetExitLabel());
6643 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006644}
6645
6646void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6647 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6648 locations->SetOut(Location::ConstantLocation(constant));
6649}
6650
6651void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6652 // Will be generated at use site.
6653}
6654
6655void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6656 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6657 locations->SetOut(Location::ConstantLocation(constant));
6658}
6659
6660void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6661 // Will be generated at use site.
6662}
6663
6664void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6665 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6666 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6667}
6668
6669void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6670 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006671 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006672 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006673 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006674}
6675
6676void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6677 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6678 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006679 Location receiver = invoke->GetLocations()->InAt(0);
6680 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006681 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006682
6683 // Set the hidden argument.
6684 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6685 invoke->GetDexMethodIndex());
6686
6687 // temp = object->GetClass();
6688 if (receiver.IsStackSlot()) {
6689 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6690 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6691 } else {
6692 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6693 }
6694 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006695 // Instead of simply (possibly) unpoisoning `temp` here, we should
6696 // emit a read barrier for the previous class reference load.
6697 // However this is not required in practice, as this is an
6698 // intermediate/temporary reference and because the current
6699 // concurrent copying collector keeps the from-space memory
6700 // intact/accessible until the end of the marking phase (the
6701 // concurrent copying collector may not in the future).
6702 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006703 __ LoadFromOffset(kLoadWord, temp, temp,
6704 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6705 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006706 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 // temp = temp->GetImtEntryAt(method_offset);
6708 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6709 // T9 = temp->GetEntryPoint();
6710 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6711 // T9();
6712 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006713 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006714 DCHECK(!codegen_->IsLeafMethod());
6715 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6716}
6717
6718void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006719 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6720 if (intrinsic.TryDispatch(invoke)) {
6721 return;
6722 }
6723
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724 HandleInvoke(invoke);
6725}
6726
6727void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006728 // Explicit clinit checks triggered by static invokes must have been pruned by
6729 // art::PrepareForRegisterAllocation.
6730 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006732 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6733 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006734
Chris Larsen701566a2015-10-27 15:29:13 -07006735 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6736 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006737 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6738 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6739 }
Chris Larsen701566a2015-10-27 15:29:13 -07006740 return;
6741 }
6742
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006743 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006744
6745 // Add the extra input register if either the dex cache array base register
6746 // or the PC-relative base register for accessing literals is needed.
6747 if (has_extra_input) {
6748 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
6749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006750}
6751
Orion Hodsonac141392017-01-13 11:53:47 +00006752void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6753 HandleInvoke(invoke);
6754}
6755
6756void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6757 codegen_->GenerateInvokePolymorphicCall(invoke);
6758}
6759
Chris Larsen701566a2015-10-27 15:29:13 -07006760static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006761 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07006762 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
6763 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006764 return true;
6765 }
6766 return false;
6767}
6768
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006769HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07006770 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006771 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006772 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006773 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
6774 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07006775 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006776 bool is_r6 = GetInstructionSetFeatures().IsR6();
6777 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006778 switch (desired_string_load_kind) {
6779 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6780 DCHECK(!GetCompilerOptions().GetCompilePic());
6781 break;
6782 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6783 DCHECK(GetCompilerOptions().GetCompilePic());
6784 break;
6785 case HLoadString::LoadKind::kBootImageAddress:
6786 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006787 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006788 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006789 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006790 case HLoadString::LoadKind::kJitTableAddress:
6791 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006792 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006793 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006794 case HLoadString::LoadKind::kDexCacheViaMethod:
6795 fallback_load = false;
6796 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006797 }
6798 if (fallback_load) {
6799 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
6800 }
6801 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006802}
6803
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006804HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
6805 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006806 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006807 // is incompatible with it.
6808 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006809 bool is_r6 = GetInstructionSetFeatures().IsR6();
6810 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006811 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006812 case HLoadClass::LoadKind::kInvalid:
6813 LOG(FATAL) << "UNREACHABLE";
6814 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07006815 case HLoadClass::LoadKind::kReferrersClass:
6816 fallback_load = false;
6817 break;
6818 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6819 DCHECK(!GetCompilerOptions().GetCompilePic());
6820 break;
6821 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6822 DCHECK(GetCompilerOptions().GetCompilePic());
6823 break;
6824 case HLoadClass::LoadKind::kBootImageAddress:
6825 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006826 case HLoadClass::LoadKind::kBssEntry:
6827 DCHECK(!Runtime::Current()->UseJitCompilation());
6828 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006829 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006830 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006831 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006832 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006833 case HLoadClass::LoadKind::kDexCacheViaMethod:
6834 fallback_load = false;
6835 break;
6836 }
6837 if (fallback_load) {
6838 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
6839 }
6840 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006841}
6842
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006843Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6844 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006845 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006846 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6847 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6848 if (!invoke->GetLocations()->Intrinsified()) {
6849 return location.AsRegister<Register>();
6850 }
6851 // For intrinsics we allow any location, so it may be on the stack.
6852 if (!location.IsRegister()) {
6853 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6854 return temp;
6855 }
6856 // For register locations, check if the register was saved. If so, get it from the stack.
6857 // Note: There is a chance that the register was saved but not overwritten, so we could
6858 // save one load. However, since this is just an intrinsic slow path we prefer this
6859 // simple and more robust approach rather that trying to determine if that's the case.
6860 SlowPathCode* slow_path = GetCurrentSlowPath();
6861 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6862 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6863 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6864 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6865 return temp;
6866 }
6867 return location.AsRegister<Register>();
6868}
6869
Vladimir Markodc151b22015-10-15 18:02:30 +01006870HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
6871 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006872 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006873 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006874 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006875 // is incompatible with it.
6876 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006877 bool is_r6 = GetInstructionSetFeatures().IsR6();
6878 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006879 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006880 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006881 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01006882 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006883 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01006884 break;
6885 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006886 if (fallback_load) {
6887 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6888 dispatch_info.method_load_data = 0;
6889 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006890 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006891}
6892
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006893void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
6894 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006895 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006896 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
6897 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006898 bool is_r6 = GetInstructionSetFeatures().IsR6();
6899 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006900 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
6901 : ZERO;
6902
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006903 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006904 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006905 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006906 uint32_t offset =
6907 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006908 __ LoadFromOffset(kLoadWord,
6909 temp.AsRegister<Register>(),
6910 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006911 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006912 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006913 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006914 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006915 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006916 break;
6917 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6918 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6919 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006920 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
6921 if (is_r6) {
6922 uint32_t offset = invoke->GetDexCacheArrayOffset();
6923 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6924 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
6925 bool reordering = __ SetReorder(false);
6926 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
6927 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
6928 __ SetReorder(reordering);
6929 } else {
6930 HMipsDexCacheArraysBase* base =
6931 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
6932 int32_t offset =
6933 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
6934 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6935 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006936 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006937 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006938 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006939 Register reg = temp.AsRegister<Register>();
6940 Register method_reg;
6941 if (current_method.IsRegister()) {
6942 method_reg = current_method.AsRegister<Register>();
6943 } else {
6944 // TODO: use the appropriate DCHECK() here if possible.
6945 // DCHECK(invoke->GetLocations()->Intrinsified());
6946 DCHECK(!current_method.IsValid());
6947 method_reg = reg;
6948 __ Lw(reg, SP, kCurrentMethodStackOffset);
6949 }
6950
6951 // temp = temp->dex_cache_resolved_methods_;
6952 __ LoadFromOffset(kLoadWord,
6953 reg,
6954 method_reg,
6955 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006956 // temp = temp[index_in_cache];
6957 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6958 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006959 __ LoadFromOffset(kLoadWord,
6960 reg,
6961 reg,
6962 CodeGenerator::GetCachePointerOffset(index_in_cache));
6963 break;
6964 }
6965 }
6966
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006967 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006968 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006969 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006970 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006971 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6972 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01006973 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006974 T9,
6975 callee_method.AsRegister<Register>(),
6976 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006977 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006978 // T9()
6979 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006980 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006981 break;
6982 }
6983 DCHECK(!IsLeafMethod());
6984}
6985
6986void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006987 // Explicit clinit checks triggered by static invokes must have been pruned by
6988 // art::PrepareForRegisterAllocation.
6989 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006990
6991 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6992 return;
6993 }
6994
6995 LocationSummary* locations = invoke->GetLocations();
6996 codegen_->GenerateStaticOrDirectCall(invoke,
6997 locations->HasTemps()
6998 ? locations->GetTemp(0)
6999 : Location::NoLocation());
7000 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7001}
7002
Chris Larsen3acee732015-11-18 13:31:08 -08007003void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007004 // Use the calling convention instead of the location of the receiver, as
7005 // intrinsics may have put the receiver in a different register. In the intrinsics
7006 // slow path, the arguments have been moved to the right place, so here we are
7007 // guaranteed that the receiver is the first register of the calling convention.
7008 InvokeDexCallingConvention calling_convention;
7009 Register receiver = calling_convention.GetRegisterAt(0);
7010
Chris Larsen3acee732015-11-18 13:31:08 -08007011 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007012 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7013 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7014 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007015 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007016
7017 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007018 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007019 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007020 // Instead of simply (possibly) unpoisoning `temp` here, we should
7021 // emit a read barrier for the previous class reference load.
7022 // However this is not required in practice, as this is an
7023 // intermediate/temporary reference and because the current
7024 // concurrent copying collector keeps the from-space memory
7025 // intact/accessible until the end of the marking phase (the
7026 // concurrent copying collector may not in the future).
7027 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007028 // temp = temp->GetMethodAt(method_offset);
7029 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7030 // T9 = temp->GetEntryPoint();
7031 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7032 // T9();
7033 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007034 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08007035}
7036
7037void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7038 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7039 return;
7040 }
7041
7042 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007043 DCHECK(!codegen_->IsLeafMethod());
7044 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7045}
7046
7047void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007048 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7049 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007050 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007051 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007052 cls,
7053 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze15958152017-02-09 19:08:30 -08007054 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunze06a46c42016-07-19 15:00:40 -07007055 return;
7056 }
Vladimir Marko41559982017-01-06 14:04:23 +00007057 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007058
Alexey Frunze15958152017-02-09 19:08:30 -08007059 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7060 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007061 ? LocationSummary::kCallOnSlowPath
7062 : LocationSummary::kNoCall;
7063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007064 switch (load_kind) {
7065 // We need an extra register for PC-relative literals on R2.
7066 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007067 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007068 case HLoadClass::LoadKind::kBootImageAddress:
7069 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007070 if (codegen_->GetInstructionSetFeatures().IsR6()) {
7071 break;
7072 }
7073 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007074 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007075 locations->SetInAt(0, Location::RequiresRegister());
7076 break;
7077 default:
7078 break;
7079 }
7080 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007081}
7082
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007083// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7084// move.
7085void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007086 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7087 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7088 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007089 return;
7090 }
Vladimir Marko41559982017-01-06 14:04:23 +00007091 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007092
Vladimir Marko41559982017-01-06 14:04:23 +00007093 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007094 Location out_loc = locations->Out();
7095 Register out = out_loc.AsRegister<Register>();
7096 Register base_or_current_method_reg;
7097 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7098 switch (load_kind) {
7099 // We need an extra register for PC-relative literals on R2.
7100 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007101 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007102 case HLoadClass::LoadKind::kBootImageAddress:
7103 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007104 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7105 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007106 case HLoadClass::LoadKind::kReferrersClass:
7107 case HLoadClass::LoadKind::kDexCacheViaMethod:
7108 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7109 break;
7110 default:
7111 base_or_current_method_reg = ZERO;
7112 break;
7113 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007114
Alexey Frunze15958152017-02-09 19:08:30 -08007115 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7116 ? kWithoutReadBarrier
7117 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007118 bool generate_null_check = false;
7119 switch (load_kind) {
7120 case HLoadClass::LoadKind::kReferrersClass: {
7121 DCHECK(!cls->CanCallRuntime());
7122 DCHECK(!cls->MustGenerateClinitCheck());
7123 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7124 GenerateGcRootFieldLoad(cls,
7125 out_loc,
7126 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007127 ArtMethod::DeclaringClassOffset().Int32Value(),
7128 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007129 break;
7130 }
7131 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007132 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007133 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007134 __ LoadLiteral(out,
7135 base_or_current_method_reg,
7136 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
7137 cls->GetTypeIndex()));
7138 break;
7139 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007140 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007141 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007142 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7143 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007144 bool reordering = __ SetReorder(false);
7145 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7146 __ Addiu(out, out, /* placeholder */ 0x5678);
7147 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007148 break;
7149 }
7150 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007151 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007152 uint32_t address = dchecked_integral_cast<uint32_t>(
7153 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7154 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007155 __ LoadLiteral(out,
7156 base_or_current_method_reg,
7157 codegen_->DeduplicateBootImageAddressLiteral(address));
7158 break;
7159 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007160 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007161 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007162 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007163 bool reordering = __ SetReorder(false);
7164 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08007165 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007166 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007167 generate_null_check = true;
7168 break;
7169 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007170 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007171 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7172 cls->GetTypeIndex(),
7173 cls->GetClass());
7174 bool reordering = __ SetReorder(false);
7175 __ Bind(&info->high_label);
7176 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007177 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007178 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007179 break;
7180 }
Vladimir Marko41559982017-01-06 14:04:23 +00007181 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007182 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007183 LOG(FATAL) << "UNREACHABLE";
7184 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007185 }
7186
7187 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7188 DCHECK(cls->CanCallRuntime());
7189 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7190 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7191 codegen_->AddSlowPath(slow_path);
7192 if (generate_null_check) {
7193 __ Beqz(out, slow_path->GetEntryLabel());
7194 }
7195 if (cls->MustGenerateClinitCheck()) {
7196 GenerateClassInitializationCheck(slow_path, out);
7197 } else {
7198 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199 }
7200 }
7201}
7202
7203static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007204 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007205}
7206
7207void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7208 LocationSummary* locations =
7209 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7210 locations->SetOut(Location::RequiresRegister());
7211}
7212
7213void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7214 Register out = load->GetLocations()->Out().AsRegister<Register>();
7215 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7216}
7217
7218void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7219 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7220}
7221
7222void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7223 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7224}
7225
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007226void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007227 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007229 HLoadString::LoadKind load_kind = load->GetLoadKind();
7230 switch (load_kind) {
7231 // We need an extra register for PC-relative literals on R2.
7232 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7233 case HLoadString::LoadKind::kBootImageAddress:
7234 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007235 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007236 if (codegen_->GetInstructionSetFeatures().IsR6()) {
7237 break;
7238 }
7239 FALLTHROUGH_INTENDED;
7240 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007241 case HLoadString::LoadKind::kDexCacheViaMethod:
7242 locations->SetInAt(0, Location::RequiresRegister());
7243 break;
7244 default:
7245 break;
7246 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007247 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
7248 InvokeRuntimeCallingConvention calling_convention;
7249 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
7250 } else {
7251 locations->SetOut(Location::RequiresRegister());
7252 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007253}
7254
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007255// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7256// move.
7257void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007258 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007259 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007260 Location out_loc = locations->Out();
7261 Register out = out_loc.AsRegister<Register>();
7262 Register base_or_current_method_reg;
7263 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7264 switch (load_kind) {
7265 // We need an extra register for PC-relative literals on R2.
7266 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7267 case HLoadString::LoadKind::kBootImageAddress:
7268 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007269 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007270 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7271 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007272 default:
7273 base_or_current_method_reg = ZERO;
7274 break;
7275 }
7276
7277 switch (load_kind) {
7278 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007279 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007280 __ LoadLiteral(out,
7281 base_or_current_method_reg,
7282 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7283 load->GetStringIndex()));
7284 return; // No dex cache slow path.
7285 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007286 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007287 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007288 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007289 bool reordering = __ SetReorder(false);
7290 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7291 __ Addiu(out, out, /* placeholder */ 0x5678);
7292 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007293 return; // No dex cache slow path.
7294 }
7295 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007296 uint32_t address = dchecked_integral_cast<uint32_t>(
7297 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7298 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007299 __ LoadLiteral(out,
7300 base_or_current_method_reg,
7301 codegen_->DeduplicateBootImageAddressLiteral(address));
7302 return; // No dex cache slow path.
7303 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007304 case HLoadString::LoadKind::kBssEntry: {
7305 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7306 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007307 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007308 bool reordering = __ SetReorder(false);
7309 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08007310 GenerateGcRootFieldLoad(load,
7311 out_loc,
7312 out,
7313 /* placeholder */ 0x5678,
7314 kCompilerReadBarrierOption);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007315 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007316 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7317 codegen_->AddSlowPath(slow_path);
7318 __ Beqz(out, slow_path->GetEntryLabel());
7319 __ Bind(slow_path->GetExitLabel());
7320 return;
7321 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007322 case HLoadString::LoadKind::kJitTableAddress: {
7323 CodeGeneratorMIPS::JitPatchInfo* info =
7324 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7325 load->GetStringIndex(),
7326 load->GetString());
7327 bool reordering = __ SetReorder(false);
7328 __ Bind(&info->high_label);
7329 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007330 GenerateGcRootFieldLoad(load,
7331 out_loc,
7332 out,
7333 /* placeholder */ 0x5678,
7334 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007335 __ SetReorder(reordering);
7336 return;
7337 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007338 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007339 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007340 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007341
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007342 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007343 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7344 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007345 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007346 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7347 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007348}
7349
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007350void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7352 locations->SetOut(Location::ConstantLocation(constant));
7353}
7354
7355void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7356 // Will be generated at use site.
7357}
7358
7359void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7360 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007362 InvokeRuntimeCallingConvention calling_convention;
7363 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7364}
7365
7366void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7367 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007368 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007369 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7370 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007371 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007372 }
7373 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7374}
7375
7376void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7377 LocationSummary* locations =
7378 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7379 switch (mul->GetResultType()) {
7380 case Primitive::kPrimInt:
7381 case Primitive::kPrimLong:
7382 locations->SetInAt(0, Location::RequiresRegister());
7383 locations->SetInAt(1, Location::RequiresRegister());
7384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7385 break;
7386
7387 case Primitive::kPrimFloat:
7388 case Primitive::kPrimDouble:
7389 locations->SetInAt(0, Location::RequiresFpuRegister());
7390 locations->SetInAt(1, Location::RequiresFpuRegister());
7391 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7392 break;
7393
7394 default:
7395 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7396 }
7397}
7398
7399void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7400 Primitive::Type type = instruction->GetType();
7401 LocationSummary* locations = instruction->GetLocations();
7402 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7403
7404 switch (type) {
7405 case Primitive::kPrimInt: {
7406 Register dst = locations->Out().AsRegister<Register>();
7407 Register lhs = locations->InAt(0).AsRegister<Register>();
7408 Register rhs = locations->InAt(1).AsRegister<Register>();
7409
7410 if (isR6) {
7411 __ MulR6(dst, lhs, rhs);
7412 } else {
7413 __ MulR2(dst, lhs, rhs);
7414 }
7415 break;
7416 }
7417 case Primitive::kPrimLong: {
7418 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7419 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7420 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7421 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7422 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7423 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7424
7425 // Extra checks to protect caused by the existance of A1_A2.
7426 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7427 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7428 DCHECK_NE(dst_high, lhs_low);
7429 DCHECK_NE(dst_high, rhs_low);
7430
7431 // A_B * C_D
7432 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7433 // dst_lo: [ low(B*D) ]
7434 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7435
7436 if (isR6) {
7437 __ MulR6(TMP, lhs_high, rhs_low);
7438 __ MulR6(dst_high, lhs_low, rhs_high);
7439 __ Addu(dst_high, dst_high, TMP);
7440 __ MuhuR6(TMP, lhs_low, rhs_low);
7441 __ Addu(dst_high, dst_high, TMP);
7442 __ MulR6(dst_low, lhs_low, rhs_low);
7443 } else {
7444 __ MulR2(TMP, lhs_high, rhs_low);
7445 __ MulR2(dst_high, lhs_low, rhs_high);
7446 __ Addu(dst_high, dst_high, TMP);
7447 __ MultuR2(lhs_low, rhs_low);
7448 __ Mfhi(TMP);
7449 __ Addu(dst_high, dst_high, TMP);
7450 __ Mflo(dst_low);
7451 }
7452 break;
7453 }
7454 case Primitive::kPrimFloat:
7455 case Primitive::kPrimDouble: {
7456 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7457 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7458 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7459 if (type == Primitive::kPrimFloat) {
7460 __ MulS(dst, lhs, rhs);
7461 } else {
7462 __ MulD(dst, lhs, rhs);
7463 }
7464 break;
7465 }
7466 default:
7467 LOG(FATAL) << "Unexpected mul type " << type;
7468 }
7469}
7470
7471void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7472 LocationSummary* locations =
7473 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7474 switch (neg->GetResultType()) {
7475 case Primitive::kPrimInt:
7476 case Primitive::kPrimLong:
7477 locations->SetInAt(0, Location::RequiresRegister());
7478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7479 break;
7480
7481 case Primitive::kPrimFloat:
7482 case Primitive::kPrimDouble:
7483 locations->SetInAt(0, Location::RequiresFpuRegister());
7484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7485 break;
7486
7487 default:
7488 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7489 }
7490}
7491
7492void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7493 Primitive::Type type = instruction->GetType();
7494 LocationSummary* locations = instruction->GetLocations();
7495
7496 switch (type) {
7497 case Primitive::kPrimInt: {
7498 Register dst = locations->Out().AsRegister<Register>();
7499 Register src = locations->InAt(0).AsRegister<Register>();
7500 __ Subu(dst, ZERO, src);
7501 break;
7502 }
7503 case Primitive::kPrimLong: {
7504 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7505 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7506 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7507 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7508 __ Subu(dst_low, ZERO, src_low);
7509 __ Sltu(TMP, ZERO, dst_low);
7510 __ Subu(dst_high, ZERO, src_high);
7511 __ Subu(dst_high, dst_high, TMP);
7512 break;
7513 }
7514 case Primitive::kPrimFloat:
7515 case Primitive::kPrimDouble: {
7516 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7517 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7518 if (type == Primitive::kPrimFloat) {
7519 __ NegS(dst, src);
7520 } else {
7521 __ NegD(dst, src);
7522 }
7523 break;
7524 }
7525 default:
7526 LOG(FATAL) << "Unexpected neg type " << type;
7527 }
7528}
7529
7530void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7531 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007533 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007534 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007535 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7536 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007537}
7538
7539void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007540 // Note: if heap poisoning is enabled, the entry point takes care
7541 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007542 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7543 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007544}
7545
7546void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7547 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007549 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007550 if (instruction->IsStringAlloc()) {
7551 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7552 } else {
7553 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007554 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007555 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7556}
7557
7558void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007559 // Note: if heap poisoning is enabled, the entry point takes care
7560 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007561 if (instruction->IsStringAlloc()) {
7562 // String is allocated through StringFactory. Call NewEmptyString entry point.
7563 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007564 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007565 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7566 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7567 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007568 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007569 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7570 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007571 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007572 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007573 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007574}
7575
7576void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7578 locations->SetInAt(0, Location::RequiresRegister());
7579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7580}
7581
7582void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7583 Primitive::Type type = instruction->GetType();
7584 LocationSummary* locations = instruction->GetLocations();
7585
7586 switch (type) {
7587 case Primitive::kPrimInt: {
7588 Register dst = locations->Out().AsRegister<Register>();
7589 Register src = locations->InAt(0).AsRegister<Register>();
7590 __ Nor(dst, src, ZERO);
7591 break;
7592 }
7593
7594 case Primitive::kPrimLong: {
7595 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7596 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7597 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7598 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7599 __ Nor(dst_high, src_high, ZERO);
7600 __ Nor(dst_low, src_low, ZERO);
7601 break;
7602 }
7603
7604 default:
7605 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7606 }
7607}
7608
7609void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7610 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7611 locations->SetInAt(0, Location::RequiresRegister());
7612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7613}
7614
7615void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7616 LocationSummary* locations = instruction->GetLocations();
7617 __ Xori(locations->Out().AsRegister<Register>(),
7618 locations->InAt(0).AsRegister<Register>(),
7619 1);
7620}
7621
7622void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007623 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7624 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007625}
7626
Calin Juravle2ae48182016-03-16 14:05:09 +00007627void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7628 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007629 return;
7630 }
7631 Location obj = instruction->GetLocations()->InAt(0);
7632
7633 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007634 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007635}
7636
Calin Juravle2ae48182016-03-16 14:05:09 +00007637void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007638 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007639 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007640
7641 Location obj = instruction->GetLocations()->InAt(0);
7642
7643 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7644}
7645
7646void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007647 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007648}
7649
7650void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7651 HandleBinaryOp(instruction);
7652}
7653
7654void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7655 HandleBinaryOp(instruction);
7656}
7657
7658void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7659 LOG(FATAL) << "Unreachable";
7660}
7661
7662void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7663 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7664}
7665
7666void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7668 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7669 if (location.IsStackSlot()) {
7670 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7671 } else if (location.IsDoubleStackSlot()) {
7672 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7673 }
7674 locations->SetOut(location);
7675}
7676
7677void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7678 ATTRIBUTE_UNUSED) {
7679 // Nothing to do, the parameter is already at its location.
7680}
7681
7682void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
7683 LocationSummary* locations =
7684 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7685 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
7686}
7687
7688void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
7689 ATTRIBUTE_UNUSED) {
7690 // Nothing to do, the method is already at its location.
7691}
7692
7693void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
7694 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01007695 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007696 locations->SetInAt(i, Location::Any());
7697 }
7698 locations->SetOut(Location::Any());
7699}
7700
7701void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
7702 LOG(FATAL) << "Unreachable";
7703}
7704
7705void LocationsBuilderMIPS::VisitRem(HRem* rem) {
7706 Primitive::Type type = rem->GetResultType();
7707 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007708 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
7710
7711 switch (type) {
7712 case Primitive::kPrimInt:
7713 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08007714 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7716 break;
7717
7718 case Primitive::kPrimLong: {
7719 InvokeRuntimeCallingConvention calling_convention;
7720 locations->SetInAt(0, Location::RegisterPairLocation(
7721 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7722 locations->SetInAt(1, Location::RegisterPairLocation(
7723 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
7724 locations->SetOut(calling_convention.GetReturnLocation(type));
7725 break;
7726 }
7727
7728 case Primitive::kPrimFloat:
7729 case Primitive::kPrimDouble: {
7730 InvokeRuntimeCallingConvention calling_convention;
7731 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
7732 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
7733 locations->SetOut(calling_convention.GetReturnLocation(type));
7734 break;
7735 }
7736
7737 default:
7738 LOG(FATAL) << "Unexpected rem type " << type;
7739 }
7740}
7741
7742void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
7743 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007744
7745 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08007746 case Primitive::kPrimInt:
7747 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007748 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007749 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007750 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007751 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
7752 break;
7753 }
7754 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007755 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007756 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007757 break;
7758 }
7759 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007760 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007761 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007762 break;
7763 }
7764 default:
7765 LOG(FATAL) << "Unexpected rem type " << type;
7766 }
7767}
7768
7769void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7770 memory_barrier->SetLocations(nullptr);
7771}
7772
7773void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7774 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7775}
7776
7777void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
7778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
7779 Primitive::Type return_type = ret->InputAt(0)->GetType();
7780 locations->SetInAt(0, MipsReturnLocation(return_type));
7781}
7782
7783void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7784 codegen_->GenerateFrameExit();
7785}
7786
7787void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
7788 ret->SetLocations(nullptr);
7789}
7790
7791void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7792 codegen_->GenerateFrameExit();
7793}
7794
Alexey Frunze92d90602015-12-18 18:16:36 -08007795void LocationsBuilderMIPS::VisitRor(HRor* ror) {
7796 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007797}
7798
Alexey Frunze92d90602015-12-18 18:16:36 -08007799void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
7800 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007801}
7802
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007803void LocationsBuilderMIPS::VisitShl(HShl* shl) {
7804 HandleShift(shl);
7805}
7806
7807void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
7808 HandleShift(shl);
7809}
7810
7811void LocationsBuilderMIPS::VisitShr(HShr* shr) {
7812 HandleShift(shr);
7813}
7814
7815void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
7816 HandleShift(shr);
7817}
7818
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007819void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
7820 HandleBinaryOp(instruction);
7821}
7822
7823void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
7824 HandleBinaryOp(instruction);
7825}
7826
7827void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7828 HandleFieldGet(instruction, instruction->GetFieldInfo());
7829}
7830
7831void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7832 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
7833}
7834
7835void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7836 HandleFieldSet(instruction, instruction->GetFieldInfo());
7837}
7838
7839void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01007840 HandleFieldSet(instruction,
7841 instruction->GetFieldInfo(),
7842 instruction->GetDexPc(),
7843 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007844}
7845
7846void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
7847 HUnresolvedInstanceFieldGet* instruction) {
7848 FieldAccessCallingConventionMIPS calling_convention;
7849 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7850 instruction->GetFieldType(),
7851 calling_convention);
7852}
7853
7854void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
7855 HUnresolvedInstanceFieldGet* instruction) {
7856 FieldAccessCallingConventionMIPS calling_convention;
7857 codegen_->GenerateUnresolvedFieldAccess(instruction,
7858 instruction->GetFieldType(),
7859 instruction->GetFieldIndex(),
7860 instruction->GetDexPc(),
7861 calling_convention);
7862}
7863
7864void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
7865 HUnresolvedInstanceFieldSet* instruction) {
7866 FieldAccessCallingConventionMIPS calling_convention;
7867 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7868 instruction->GetFieldType(),
7869 calling_convention);
7870}
7871
7872void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
7873 HUnresolvedInstanceFieldSet* instruction) {
7874 FieldAccessCallingConventionMIPS calling_convention;
7875 codegen_->GenerateUnresolvedFieldAccess(instruction,
7876 instruction->GetFieldType(),
7877 instruction->GetFieldIndex(),
7878 instruction->GetDexPc(),
7879 calling_convention);
7880}
7881
7882void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
7883 HUnresolvedStaticFieldGet* instruction) {
7884 FieldAccessCallingConventionMIPS calling_convention;
7885 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7886 instruction->GetFieldType(),
7887 calling_convention);
7888}
7889
7890void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
7891 HUnresolvedStaticFieldGet* instruction) {
7892 FieldAccessCallingConventionMIPS calling_convention;
7893 codegen_->GenerateUnresolvedFieldAccess(instruction,
7894 instruction->GetFieldType(),
7895 instruction->GetFieldIndex(),
7896 instruction->GetDexPc(),
7897 calling_convention);
7898}
7899
7900void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
7901 HUnresolvedStaticFieldSet* instruction) {
7902 FieldAccessCallingConventionMIPS calling_convention;
7903 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7904 instruction->GetFieldType(),
7905 calling_convention);
7906}
7907
7908void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
7909 HUnresolvedStaticFieldSet* instruction) {
7910 FieldAccessCallingConventionMIPS calling_convention;
7911 codegen_->GenerateUnresolvedFieldAccess(instruction,
7912 instruction->GetFieldType(),
7913 instruction->GetFieldIndex(),
7914 instruction->GetDexPc(),
7915 calling_convention);
7916}
7917
7918void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01007919 LocationSummary* locations =
7920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01007921 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007922}
7923
7924void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
7925 HBasicBlock* block = instruction->GetBlock();
7926 if (block->GetLoopInformation() != nullptr) {
7927 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7928 // The back edge will generate the suspend check.
7929 return;
7930 }
7931 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7932 // The goto will generate the suspend check.
7933 return;
7934 }
7935 GenerateSuspendCheck(instruction, nullptr);
7936}
7937
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007938void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
7939 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007940 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007941 InvokeRuntimeCallingConvention calling_convention;
7942 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7943}
7944
7945void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007946 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007947 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7948}
7949
7950void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
7951 Primitive::Type input_type = conversion->GetInputType();
7952 Primitive::Type result_type = conversion->GetResultType();
7953 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007954 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007955
7956 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
7957 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
7958 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
7959 }
7960
7961 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007962 if (!isR6 &&
7963 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
7964 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007965 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007966 }
7967
7968 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
7969
7970 if (call_kind == LocationSummary::kNoCall) {
7971 if (Primitive::IsFloatingPointType(input_type)) {
7972 locations->SetInAt(0, Location::RequiresFpuRegister());
7973 } else {
7974 locations->SetInAt(0, Location::RequiresRegister());
7975 }
7976
7977 if (Primitive::IsFloatingPointType(result_type)) {
7978 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7979 } else {
7980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7981 }
7982 } else {
7983 InvokeRuntimeCallingConvention calling_convention;
7984
7985 if (Primitive::IsFloatingPointType(input_type)) {
7986 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
7987 } else {
7988 DCHECK_EQ(input_type, Primitive::kPrimLong);
7989 locations->SetInAt(0, Location::RegisterPairLocation(
7990 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7991 }
7992
7993 locations->SetOut(calling_convention.GetReturnLocation(result_type));
7994 }
7995}
7996
7997void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
7998 LocationSummary* locations = conversion->GetLocations();
7999 Primitive::Type result_type = conversion->GetResultType();
8000 Primitive::Type input_type = conversion->GetInputType();
8001 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008002 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008003
8004 DCHECK_NE(input_type, result_type);
8005
8006 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8007 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8008 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8009 Register src = locations->InAt(0).AsRegister<Register>();
8010
Alexey Frunzea871ef12016-06-27 15:20:11 -07008011 if (dst_low != src) {
8012 __ Move(dst_low, src);
8013 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008014 __ Sra(dst_high, src, 31);
8015 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8016 Register dst = locations->Out().AsRegister<Register>();
8017 Register src = (input_type == Primitive::kPrimLong)
8018 ? locations->InAt(0).AsRegisterPairLow<Register>()
8019 : locations->InAt(0).AsRegister<Register>();
8020
8021 switch (result_type) {
8022 case Primitive::kPrimChar:
8023 __ Andi(dst, src, 0xFFFF);
8024 break;
8025 case Primitive::kPrimByte:
8026 if (has_sign_extension) {
8027 __ Seb(dst, src);
8028 } else {
8029 __ Sll(dst, src, 24);
8030 __ Sra(dst, dst, 24);
8031 }
8032 break;
8033 case Primitive::kPrimShort:
8034 if (has_sign_extension) {
8035 __ Seh(dst, src);
8036 } else {
8037 __ Sll(dst, src, 16);
8038 __ Sra(dst, dst, 16);
8039 }
8040 break;
8041 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008042 if (dst != src) {
8043 __ Move(dst, src);
8044 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008045 break;
8046
8047 default:
8048 LOG(FATAL) << "Unexpected type conversion from " << input_type
8049 << " to " << result_type;
8050 }
8051 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008052 if (input_type == Primitive::kPrimLong) {
8053 if (isR6) {
8054 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8055 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8056 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8057 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8058 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8059 __ Mtc1(src_low, FTMP);
8060 __ Mthc1(src_high, FTMP);
8061 if (result_type == Primitive::kPrimFloat) {
8062 __ Cvtsl(dst, FTMP);
8063 } else {
8064 __ Cvtdl(dst, FTMP);
8065 }
8066 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008067 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8068 : kQuickL2d;
8069 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008070 if (result_type == Primitive::kPrimFloat) {
8071 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8072 } else {
8073 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8074 }
8075 }
8076 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008077 Register src = locations->InAt(0).AsRegister<Register>();
8078 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8079 __ Mtc1(src, FTMP);
8080 if (result_type == Primitive::kPrimFloat) {
8081 __ Cvtsw(dst, FTMP);
8082 } else {
8083 __ Cvtdw(dst, FTMP);
8084 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008085 }
8086 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8087 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008088 if (result_type == Primitive::kPrimLong) {
8089 if (isR6) {
8090 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8091 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8092 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8093 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8094 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8095 MipsLabel truncate;
8096 MipsLabel done;
8097
8098 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8099 // value when the input is either a NaN or is outside of the range of the output type
8100 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8101 // the same result.
8102 //
8103 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8104 // value of the output type if the input is outside of the range after the truncation or
8105 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8106 // results. This matches the desired float/double-to-int/long conversion exactly.
8107 //
8108 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
8109 //
8110 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8111 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8112 // even though it must be NAN2008=1 on R6.
8113 //
8114 // The code takes care of the different behaviors by first comparing the input to the
8115 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8116 // If the input is greater than or equal to the minimum, it procedes to the truncate
8117 // instruction, which will handle such an input the same way irrespective of NAN2008.
8118 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8119 // in order to return either zero or the minimum value.
8120 //
8121 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8122 // truncate instruction for MIPS64R6.
8123 if (input_type == Primitive::kPrimFloat) {
8124 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
8125 __ LoadConst32(TMP, min_val);
8126 __ Mtc1(TMP, FTMP);
8127 __ CmpLeS(FTMP, FTMP, src);
8128 } else {
8129 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
8130 __ LoadConst32(TMP, High32Bits(min_val));
8131 __ Mtc1(ZERO, FTMP);
8132 __ Mthc1(TMP, FTMP);
8133 __ CmpLeD(FTMP, FTMP, src);
8134 }
8135
8136 __ Bc1nez(FTMP, &truncate);
8137
8138 if (input_type == Primitive::kPrimFloat) {
8139 __ CmpEqS(FTMP, src, src);
8140 } else {
8141 __ CmpEqD(FTMP, src, src);
8142 }
8143 __ Move(dst_low, ZERO);
8144 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
8145 __ Mfc1(TMP, FTMP);
8146 __ And(dst_high, dst_high, TMP);
8147
8148 __ B(&done);
8149
8150 __ Bind(&truncate);
8151
8152 if (input_type == Primitive::kPrimFloat) {
8153 __ TruncLS(FTMP, src);
8154 } else {
8155 __ TruncLD(FTMP, src);
8156 }
8157 __ Mfc1(dst_low, FTMP);
8158 __ Mfhc1(dst_high, FTMP);
8159
8160 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008161 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008162 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8163 : kQuickD2l;
8164 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008165 if (input_type == Primitive::kPrimFloat) {
8166 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8167 } else {
8168 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8169 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008170 }
8171 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008172 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8173 Register dst = locations->Out().AsRegister<Register>();
8174 MipsLabel truncate;
8175 MipsLabel done;
8176
8177 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8178 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8179 // even though it must be NAN2008=1 on R6.
8180 //
8181 // For details see the large comment above for the truncation of float/double to long on R6.
8182 //
8183 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8184 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008185 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008186 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8187 __ LoadConst32(TMP, min_val);
8188 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008189 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008190 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8191 __ LoadConst32(TMP, High32Bits(min_val));
8192 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07008193 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008194 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008195
8196 if (isR6) {
8197 if (input_type == Primitive::kPrimFloat) {
8198 __ CmpLeS(FTMP, FTMP, src);
8199 } else {
8200 __ CmpLeD(FTMP, FTMP, src);
8201 }
8202 __ Bc1nez(FTMP, &truncate);
8203
8204 if (input_type == Primitive::kPrimFloat) {
8205 __ CmpEqS(FTMP, src, src);
8206 } else {
8207 __ CmpEqD(FTMP, src, src);
8208 }
8209 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8210 __ Mfc1(TMP, FTMP);
8211 __ And(dst, dst, TMP);
8212 } else {
8213 if (input_type == Primitive::kPrimFloat) {
8214 __ ColeS(0, FTMP, src);
8215 } else {
8216 __ ColeD(0, FTMP, src);
8217 }
8218 __ Bc1t(0, &truncate);
8219
8220 if (input_type == Primitive::kPrimFloat) {
8221 __ CeqS(0, src, src);
8222 } else {
8223 __ CeqD(0, src, src);
8224 }
8225 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8226 __ Movf(dst, ZERO, 0);
8227 }
8228
8229 __ B(&done);
8230
8231 __ Bind(&truncate);
8232
8233 if (input_type == Primitive::kPrimFloat) {
8234 __ TruncWS(FTMP, src);
8235 } else {
8236 __ TruncWD(FTMP, src);
8237 }
8238 __ Mfc1(dst, FTMP);
8239
8240 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008241 }
8242 } else if (Primitive::IsFloatingPointType(result_type) &&
8243 Primitive::IsFloatingPointType(input_type)) {
8244 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8245 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8246 if (result_type == Primitive::kPrimFloat) {
8247 __ Cvtsd(dst, src);
8248 } else {
8249 __ Cvtds(dst, src);
8250 }
8251 } else {
8252 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8253 << " to " << result_type;
8254 }
8255}
8256
8257void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8258 HandleShift(ushr);
8259}
8260
8261void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8262 HandleShift(ushr);
8263}
8264
8265void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8266 HandleBinaryOp(instruction);
8267}
8268
8269void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8270 HandleBinaryOp(instruction);
8271}
8272
8273void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8274 // Nothing to do, this should be removed during prepare for register allocator.
8275 LOG(FATAL) << "Unreachable";
8276}
8277
8278void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8279 // Nothing to do, this should be removed during prepare for register allocator.
8280 LOG(FATAL) << "Unreachable";
8281}
8282
8283void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008284 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008285}
8286
8287void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008288 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008289}
8290
8291void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008292 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008293}
8294
8295void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008296 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008297}
8298
8299void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008300 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008301}
8302
8303void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008304 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008305}
8306
8307void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008308 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008309}
8310
8311void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008312 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008313}
8314
8315void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008316 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008317}
8318
8319void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008320 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008321}
8322
8323void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008324 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008325}
8326
8327void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008328 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008329}
8330
8331void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008332 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008333}
8334
8335void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008336 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008337}
8338
8339void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008340 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008341}
8342
8343void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008344 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008345}
8346
8347void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008348 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008349}
8350
8351void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008352 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008353}
8354
8355void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008356 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008357}
8358
8359void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008360 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008361}
8362
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008363void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8364 LocationSummary* locations =
8365 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8366 locations->SetInAt(0, Location::RequiresRegister());
8367}
8368
Alexey Frunze96b66822016-09-10 02:32:44 -07008369void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8370 int32_t lower_bound,
8371 uint32_t num_entries,
8372 HBasicBlock* switch_block,
8373 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008374 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008375 Register temp_reg = TMP;
8376 __ Addiu32(temp_reg, value_reg, -lower_bound);
8377 // Jump to default if index is negative
8378 // Note: We don't check the case that index is positive while value < lower_bound, because in
8379 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8380 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8381
Alexey Frunze96b66822016-09-10 02:32:44 -07008382 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008383 // Jump to successors[0] if value == lower_bound.
8384 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8385 int32_t last_index = 0;
8386 for (; num_entries - last_index > 2; last_index += 2) {
8387 __ Addiu(temp_reg, temp_reg, -2);
8388 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8389 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8390 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8391 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8392 }
8393 if (num_entries - last_index == 2) {
8394 // The last missing case_value.
8395 __ Addiu(temp_reg, temp_reg, -1);
8396 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008397 }
8398
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008399 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008400 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008401 __ B(codegen_->GetLabelOf(default_block));
8402 }
8403}
8404
Alexey Frunze96b66822016-09-10 02:32:44 -07008405void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8406 Register constant_area,
8407 int32_t lower_bound,
8408 uint32_t num_entries,
8409 HBasicBlock* switch_block,
8410 HBasicBlock* default_block) {
8411 // Create a jump table.
8412 std::vector<MipsLabel*> labels(num_entries);
8413 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8414 for (uint32_t i = 0; i < num_entries; i++) {
8415 labels[i] = codegen_->GetLabelOf(successors[i]);
8416 }
8417 JumpTable* table = __ CreateJumpTable(std::move(labels));
8418
8419 // Is the value in range?
8420 __ Addiu32(TMP, value_reg, -lower_bound);
8421 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8422 __ Sltiu(AT, TMP, num_entries);
8423 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8424 } else {
8425 __ LoadConst32(AT, num_entries);
8426 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8427 }
8428
8429 // We are in the range of the table.
8430 // Load the target address from the jump table, indexing by the value.
8431 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008432 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008433 __ Lw(TMP, TMP, 0);
8434 // Compute the absolute target address by adding the table start address
8435 // (the table contains offsets to targets relative to its start).
8436 __ Addu(TMP, TMP, AT);
8437 // And jump.
8438 __ Jr(TMP);
8439 __ NopIfNoReordering();
8440}
8441
8442void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8443 int32_t lower_bound = switch_instr->GetStartValue();
8444 uint32_t num_entries = switch_instr->GetNumEntries();
8445 LocationSummary* locations = switch_instr->GetLocations();
8446 Register value_reg = locations->InAt(0).AsRegister<Register>();
8447 HBasicBlock* switch_block = switch_instr->GetBlock();
8448 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8449
8450 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8451 num_entries > kPackedSwitchJumpTableThreshold) {
8452 // R6 uses PC-relative addressing to access the jump table.
8453 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8454 // the jump table and it is implemented by changing HPackedSwitch to
8455 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8456 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8457 GenTableBasedPackedSwitch(value_reg,
8458 ZERO,
8459 lower_bound,
8460 num_entries,
8461 switch_block,
8462 default_block);
8463 } else {
8464 GenPackedSwitchWithCompares(value_reg,
8465 lower_bound,
8466 num_entries,
8467 switch_block,
8468 default_block);
8469 }
8470}
8471
8472void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8473 LocationSummary* locations =
8474 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8475 locations->SetInAt(0, Location::RequiresRegister());
8476 // Constant area pointer (HMipsComputeBaseMethodAddress).
8477 locations->SetInAt(1, Location::RequiresRegister());
8478}
8479
8480void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8481 int32_t lower_bound = switch_instr->GetStartValue();
8482 uint32_t num_entries = switch_instr->GetNumEntries();
8483 LocationSummary* locations = switch_instr->GetLocations();
8484 Register value_reg = locations->InAt(0).AsRegister<Register>();
8485 Register constant_area = locations->InAt(1).AsRegister<Register>();
8486 HBasicBlock* switch_block = switch_instr->GetBlock();
8487 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8488
8489 // This is an R2-only path. HPackedSwitch has been changed to
8490 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8491 // required to address the jump table relative to PC.
8492 GenTableBasedPackedSwitch(value_reg,
8493 constant_area,
8494 lower_bound,
8495 num_entries,
8496 switch_block,
8497 default_block);
8498}
8499
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008500void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8501 HMipsComputeBaseMethodAddress* insn) {
8502 LocationSummary* locations =
8503 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8504 locations->SetOut(Location::RequiresRegister());
8505}
8506
8507void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8508 HMipsComputeBaseMethodAddress* insn) {
8509 LocationSummary* locations = insn->GetLocations();
8510 Register reg = locations->Out().AsRegister<Register>();
8511
8512 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8513
8514 // Generate a dummy PC-relative call to obtain PC.
8515 __ Nal();
8516 // Grab the return address off RA.
8517 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008518 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008519
8520 // Remember this offset (the obtained PC value) for later use with constant area.
8521 __ BindPcRelBaseLabel();
8522}
8523
8524void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8525 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8526 locations->SetOut(Location::RequiresRegister());
8527}
8528
8529void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8530 Register reg = base->GetLocations()->Out().AsRegister<Register>();
8531 CodeGeneratorMIPS::PcRelativePatchInfo* info =
8532 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008533 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8534 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008535 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008536 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
8537 __ Addiu(reg, reg, /* placeholder */ 0x5678);
8538 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008539}
8540
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008541void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8542 // The trampoline uses the same calling convention as dex calling conventions,
8543 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8544 // the method_idx.
8545 HandleInvoke(invoke);
8546}
8547
8548void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8549 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8550}
8551
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008552void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8553 LocationSummary* locations =
8554 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8555 locations->SetInAt(0, Location::RequiresRegister());
8556 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008557}
8558
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008559void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8560 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008561 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008562 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008563 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008564 __ LoadFromOffset(kLoadWord,
8565 locations->Out().AsRegister<Register>(),
8566 locations->InAt(0).AsRegister<Register>(),
8567 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008568 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008569 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008570 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008571 __ LoadFromOffset(kLoadWord,
8572 locations->Out().AsRegister<Register>(),
8573 locations->InAt(0).AsRegister<Register>(),
8574 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008575 __ LoadFromOffset(kLoadWord,
8576 locations->Out().AsRegister<Register>(),
8577 locations->Out().AsRegister<Register>(),
8578 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008579 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008580}
8581
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008582#undef __
8583#undef QUICK_ENTRY_POINT
8584
8585} // namespace mips
8586} // namespace art