blob: 287891feaea64bf2bbf68bad5645c1c5db8f0408 [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 {
2512 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
2513 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002514 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002515 }
2516 break;
2517 }
2518
2519 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002520 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002521 if (maybe_compressed_char_at) {
2522 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2523 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2524 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2525 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2526 "Expecting 0=compressed, 1=uncompressed");
2527 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002528 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002529 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2530 if (maybe_compressed_char_at) {
2531 MipsLabel uncompressed_load, done;
2532 __ Bnez(TMP, &uncompressed_load);
2533 __ LoadFromOffset(kLoadUnsignedByte,
2534 out,
2535 obj,
2536 data_offset + (const_index << TIMES_1));
2537 __ B(&done);
2538 __ Bind(&uncompressed_load);
2539 __ LoadFromOffset(kLoadUnsignedHalfword,
2540 out,
2541 obj,
2542 data_offset + (const_index << TIMES_2));
2543 __ Bind(&done);
2544 } else {
2545 __ LoadFromOffset(kLoadUnsignedHalfword,
2546 out,
2547 obj,
2548 data_offset + (const_index << TIMES_2),
2549 null_checker);
2550 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002552 Register index_reg = index.AsRegister<Register>();
2553 if (maybe_compressed_char_at) {
2554 MipsLabel uncompressed_load, done;
2555 __ Bnez(TMP, &uncompressed_load);
2556 __ Addu(TMP, obj, index_reg);
2557 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2558 __ B(&done);
2559 __ Bind(&uncompressed_load);
2560 __ Sll(TMP, index_reg, TIMES_2);
2561 __ Addu(TMP, obj, TMP);
2562 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2563 __ Bind(&done);
2564 } else {
2565 __ Sll(TMP, index_reg, TIMES_2);
2566 __ Addu(TMP, obj, TMP);
2567 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2568 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002569 }
2570 break;
2571 }
2572
Alexey Frunze15958152017-02-09 19:08:30 -08002573 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002575 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002576 if (index.IsConstant()) {
2577 size_t offset =
2578 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002579 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002580 } else {
2581 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2582 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002583 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584 }
2585 break;
2586 }
2587
Alexey Frunze15958152017-02-09 19:08:30 -08002588 case Primitive::kPrimNot: {
2589 static_assert(
2590 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2591 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2592 // /* HeapReference<Object> */ out =
2593 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2594 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2595 Location temp = locations->GetTemp(0);
2596 // Note that a potential implicit null check is handled in this
2597 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2598 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2599 out_loc,
2600 obj,
2601 data_offset,
2602 index,
2603 temp,
2604 /* needs_null_check */ true);
2605 } else {
2606 Register out = out_loc.AsRegister<Register>();
2607 if (index.IsConstant()) {
2608 size_t offset =
2609 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2610 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2611 // If read barriers are enabled, emit read barriers other than
2612 // Baker's using a slow path (and also unpoison the loaded
2613 // reference, if heap poisoning is enabled).
2614 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2615 } else {
2616 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2617 __ Addu(TMP, obj, TMP);
2618 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2619 // If read barriers are enabled, emit read barriers other than
2620 // Baker's using a slow path (and also unpoison the loaded
2621 // reference, if heap poisoning is enabled).
2622 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2623 out_loc,
2624 out_loc,
2625 obj_loc,
2626 data_offset,
2627 index);
2628 }
2629 }
2630 break;
2631 }
2632
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002634 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002635 if (index.IsConstant()) {
2636 size_t offset =
2637 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002638 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002639 } else {
2640 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2641 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002642 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 }
2644 break;
2645 }
2646
2647 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002648 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002649 if (index.IsConstant()) {
2650 size_t offset =
2651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002652 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002653 } else {
2654 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2655 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002656 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002657 }
2658 break;
2659 }
2660
2661 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002662 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 if (index.IsConstant()) {
2664 size_t offset =
2665 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002666 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667 } else {
2668 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2669 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002670 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 }
2672 break;
2673 }
2674
2675 case Primitive::kPrimVoid:
2676 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2677 UNREACHABLE();
2678 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002679}
2680
2681void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2683 locations->SetInAt(0, Location::RequiresRegister());
2684 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2685}
2686
2687void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2688 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002689 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690 Register obj = locations->InAt(0).AsRegister<Register>();
2691 Register out = locations->Out().AsRegister<Register>();
2692 __ LoadFromOffset(kLoadWord, out, obj, offset);
2693 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002694 // Mask out compression flag from String's array length.
2695 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2696 __ Srl(out, out, 1u);
2697 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002698}
2699
Alexey Frunzef58b2482016-09-02 22:14:06 -07002700Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2701 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2702 ? Location::ConstantLocation(instruction->AsConstant())
2703 : Location::RequiresRegister();
2704}
2705
2706Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2707 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2708 // We can store a non-zero float or double constant without first loading it into the FPU,
2709 // but we should only prefer this if the constant has a single use.
2710 if (instruction->IsConstant() &&
2711 (instruction->AsConstant()->IsZeroBitPattern() ||
2712 instruction->GetUses().HasExactlyOneElement())) {
2713 return Location::ConstantLocation(instruction->AsConstant());
2714 // Otherwise fall through and require an FPU register for the constant.
2715 }
2716 return Location::RequiresFpuRegister();
2717}
2718
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002719void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002720 Primitive::Type value_type = instruction->GetComponentType();
2721
2722 bool needs_write_barrier =
2723 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2724 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2725
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2727 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002728 may_need_runtime_call_for_type_check ?
2729 LocationSummary::kCallOnSlowPath :
2730 LocationSummary::kNoCall);
2731
2732 locations->SetInAt(0, Location::RequiresRegister());
2733 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2734 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2735 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002736 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002737 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2738 }
2739 if (needs_write_barrier) {
2740 // Temporary register for the write barrier.
2741 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002742 }
2743}
2744
2745void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2746 LocationSummary* locations = instruction->GetLocations();
2747 Register obj = locations->InAt(0).AsRegister<Register>();
2748 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002749 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002750 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002751 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002752 bool needs_write_barrier =
2753 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002754 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002755 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002756
2757 switch (value_type) {
2758 case Primitive::kPrimBoolean:
2759 case Primitive::kPrimByte: {
2760 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002761 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002762 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002763 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002764 __ Addu(base_reg, obj, index.AsRegister<Register>());
2765 }
2766 if (value_location.IsConstant()) {
2767 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2768 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2769 } else {
2770 Register value = value_location.AsRegister<Register>();
2771 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002772 }
2773 break;
2774 }
2775
2776 case Primitive::kPrimShort:
2777 case Primitive::kPrimChar: {
2778 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002779 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002780 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002782 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2783 __ Addu(base_reg, obj, base_reg);
2784 }
2785 if (value_location.IsConstant()) {
2786 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2787 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2788 } else {
2789 Register value = value_location.AsRegister<Register>();
2790 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 }
2792 break;
2793 }
2794
Alexey Frunze15958152017-02-09 19:08:30 -08002795 case Primitive::kPrimInt: {
2796 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2797 if (index.IsConstant()) {
2798 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2799 } else {
2800 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2801 __ Addu(base_reg, obj, base_reg);
2802 }
2803 if (value_location.IsConstant()) {
2804 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2805 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2806 } else {
2807 Register value = value_location.AsRegister<Register>();
2808 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2809 }
2810 break;
2811 }
2812
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002814 if (value_location.IsConstant()) {
2815 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002816 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002818 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002819 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002820 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2821 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822 }
Alexey Frunze15958152017-02-09 19:08:30 -08002823 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2824 DCHECK_EQ(value, 0);
2825 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2826 DCHECK(!needs_write_barrier);
2827 DCHECK(!may_need_runtime_call_for_type_check);
2828 break;
2829 }
2830
2831 DCHECK(needs_write_barrier);
2832 Register value = value_location.AsRegister<Register>();
2833 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2834 Register temp2 = TMP; // Doesn't need to survive slow path.
2835 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2836 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2837 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2838 MipsLabel done;
2839 SlowPathCodeMIPS* slow_path = nullptr;
2840
2841 if (may_need_runtime_call_for_type_check) {
2842 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2843 codegen_->AddSlowPath(slow_path);
2844 if (instruction->GetValueCanBeNull()) {
2845 MipsLabel non_zero;
2846 __ Bnez(value, &non_zero);
2847 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2848 if (index.IsConstant()) {
2849 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002850 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002851 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2852 __ Addu(base_reg, obj, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002853 }
Alexey Frunze15958152017-02-09 19:08:30 -08002854 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2855 __ B(&done);
2856 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002857 }
Alexey Frunze15958152017-02-09 19:08:30 -08002858
2859 // Note that when read barriers are enabled, the type checks
2860 // are performed without read barriers. This is fine, even in
2861 // the case where a class object is in the from-space after
2862 // the flip, as a comparison involving such a type would not
2863 // produce a false positive; it may of course produce a false
2864 // negative, in which case we would take the ArraySet slow
2865 // path.
2866
2867 // /* HeapReference<Class> */ temp1 = obj->klass_
2868 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2869 __ MaybeUnpoisonHeapReference(temp1);
2870
2871 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2872 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2873 // /* HeapReference<Class> */ temp2 = value->klass_
2874 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2875 // If heap poisoning is enabled, no need to unpoison `temp1`
2876 // nor `temp2`, as we are comparing two poisoned references.
2877
2878 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2879 MipsLabel do_put;
2880 __ Beq(temp1, temp2, &do_put);
2881 // If heap poisoning is enabled, the `temp1` reference has
2882 // not been unpoisoned yet; unpoison it now.
2883 __ MaybeUnpoisonHeapReference(temp1);
2884
2885 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2886 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2887 // If heap poisoning is enabled, no need to unpoison
2888 // `temp1`, as we are comparing against null below.
2889 __ Bnez(temp1, slow_path->GetEntryLabel());
2890 __ Bind(&do_put);
2891 } else {
2892 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2893 }
2894 }
2895
2896 Register source = value;
2897 if (kPoisonHeapReferences) {
2898 // Note that in the case where `value` is a null reference,
2899 // we do not enter this block, as a null reference does not
2900 // need poisoning.
2901 __ Move(temp1, value);
2902 __ PoisonHeapReference(temp1);
2903 source = temp1;
2904 }
2905
2906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2907 if (index.IsConstant()) {
2908 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002909 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002910 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2911 __ Addu(base_reg, obj, base_reg);
2912 }
2913 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2914
2915 if (!may_need_runtime_call_for_type_check) {
2916 codegen_->MaybeRecordImplicitNullCheck(instruction);
2917 }
2918
2919 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2920
2921 if (done.IsLinked()) {
2922 __ Bind(&done);
2923 }
2924
2925 if (slow_path != nullptr) {
2926 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002927 }
2928 break;
2929 }
2930
2931 case Primitive::kPrimLong: {
2932 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002934 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002935 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002936 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2937 __ Addu(base_reg, obj, base_reg);
2938 }
2939 if (value_location.IsConstant()) {
2940 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2941 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2942 } else {
2943 Register value = value_location.AsRegisterPairLow<Register>();
2944 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002945 }
2946 break;
2947 }
2948
2949 case Primitive::kPrimFloat: {
2950 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002951 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002952 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002953 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002954 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2955 __ Addu(base_reg, obj, base_reg);
2956 }
2957 if (value_location.IsConstant()) {
2958 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2959 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2960 } else {
2961 FRegister value = value_location.AsFpuRegister<FRegister>();
2962 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 }
2964 break;
2965 }
2966
2967 case Primitive::kPrimDouble: {
2968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002969 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002970 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002971 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002972 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2973 __ Addu(base_reg, obj, base_reg);
2974 }
2975 if (value_location.IsConstant()) {
2976 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2977 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2978 } else {
2979 FRegister value = value_location.AsFpuRegister<FRegister>();
2980 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 }
2982 break;
2983 }
2984
2985 case Primitive::kPrimVoid:
2986 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2987 UNREACHABLE();
2988 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002989}
2990
2991void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002992 RegisterSet caller_saves = RegisterSet::Empty();
2993 InvokeRuntimeCallingConvention calling_convention;
2994 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2995 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2996 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002997 locations->SetInAt(0, Location::RequiresRegister());
2998 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002999}
3000
3001void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3002 LocationSummary* locations = instruction->GetLocations();
3003 BoundsCheckSlowPathMIPS* slow_path =
3004 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3005 codegen_->AddSlowPath(slow_path);
3006
3007 Register index = locations->InAt(0).AsRegister<Register>();
3008 Register length = locations->InAt(1).AsRegister<Register>();
3009
3010 // length is limited by the maximum positive signed 32-bit integer.
3011 // Unsigned comparison of length and index checks for index < 0
3012 // and for length <= index simultaneously.
3013 __ Bgeu(index, length, slow_path->GetEntryLabel());
3014}
3015
Alexey Frunze15958152017-02-09 19:08:30 -08003016// Temp is used for read barrier.
3017static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3018 if (kEmitCompilerReadBarrier &&
3019 (kUseBakerReadBarrier ||
3020 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3021 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3022 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3023 return 1;
3024 }
3025 return 0;
3026}
3027
3028// Extra temp is used for read barrier.
3029static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3030 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3031}
3032
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003033void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003034 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3035 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3036
3037 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3038 switch (type_check_kind) {
3039 case TypeCheckKind::kExactCheck:
3040 case TypeCheckKind::kAbstractClassCheck:
3041 case TypeCheckKind::kClassHierarchyCheck:
3042 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003043 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003044 ? LocationSummary::kCallOnSlowPath
3045 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3046 break;
3047 case TypeCheckKind::kArrayCheck:
3048 case TypeCheckKind::kUnresolvedCheck:
3049 case TypeCheckKind::kInterfaceCheck:
3050 call_kind = LocationSummary::kCallOnSlowPath;
3051 break;
3052 }
3053
3054 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003055 locations->SetInAt(0, Location::RequiresRegister());
3056 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003057 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003058}
3059
3060void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003061 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003062 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003063 Location obj_loc = locations->InAt(0);
3064 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003065 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003066 Location temp_loc = locations->GetTemp(0);
3067 Register temp = temp_loc.AsRegister<Register>();
3068 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3069 DCHECK_LE(num_temps, 2u);
3070 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003071 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3072 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3073 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3074 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3075 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3076 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3077 const uint32_t object_array_data_offset =
3078 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3079 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003081 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3082 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3083 // read barriers is done for performance and code size reasons.
3084 bool is_type_check_slow_path_fatal = false;
3085 if (!kEmitCompilerReadBarrier) {
3086 is_type_check_slow_path_fatal =
3087 (type_check_kind == TypeCheckKind::kExactCheck ||
3088 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3089 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3090 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3091 !instruction->CanThrowIntoCatchBlock();
3092 }
3093 SlowPathCodeMIPS* slow_path =
3094 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3095 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003096 codegen_->AddSlowPath(slow_path);
3097
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003098 // Avoid this check if we know `obj` is not null.
3099 if (instruction->MustDoNullCheck()) {
3100 __ Beqz(obj, &done);
3101 }
3102
3103 switch (type_check_kind) {
3104 case TypeCheckKind::kExactCheck:
3105 case TypeCheckKind::kArrayCheck: {
3106 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003107 GenerateReferenceLoadTwoRegisters(instruction,
3108 temp_loc,
3109 obj_loc,
3110 class_offset,
3111 maybe_temp2_loc,
3112 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003113 // Jump to slow path for throwing the exception or doing a
3114 // more involved array check.
3115 __ Bne(temp, cls, slow_path->GetEntryLabel());
3116 break;
3117 }
3118
3119 case TypeCheckKind::kAbstractClassCheck: {
3120 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003121 GenerateReferenceLoadTwoRegisters(instruction,
3122 temp_loc,
3123 obj_loc,
3124 class_offset,
3125 maybe_temp2_loc,
3126 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003127 // If the class is abstract, we eagerly fetch the super class of the
3128 // object to avoid doing a comparison we know will fail.
3129 MipsLabel loop;
3130 __ Bind(&loop);
3131 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003132 GenerateReferenceLoadOneRegister(instruction,
3133 temp_loc,
3134 super_offset,
3135 maybe_temp2_loc,
3136 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003137 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3138 // exception.
3139 __ Beqz(temp, slow_path->GetEntryLabel());
3140 // Otherwise, compare the classes.
3141 __ Bne(temp, cls, &loop);
3142 break;
3143 }
3144
3145 case TypeCheckKind::kClassHierarchyCheck: {
3146 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003147 GenerateReferenceLoadTwoRegisters(instruction,
3148 temp_loc,
3149 obj_loc,
3150 class_offset,
3151 maybe_temp2_loc,
3152 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003153 // Walk over the class hierarchy to find a match.
3154 MipsLabel loop;
3155 __ Bind(&loop);
3156 __ Beq(temp, cls, &done);
3157 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003158 GenerateReferenceLoadOneRegister(instruction,
3159 temp_loc,
3160 super_offset,
3161 maybe_temp2_loc,
3162 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003163 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3164 // exception. Otherwise, jump to the beginning of the loop.
3165 __ Bnez(temp, &loop);
3166 __ B(slow_path->GetEntryLabel());
3167 break;
3168 }
3169
3170 case TypeCheckKind::kArrayObjectCheck: {
3171 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003172 GenerateReferenceLoadTwoRegisters(instruction,
3173 temp_loc,
3174 obj_loc,
3175 class_offset,
3176 maybe_temp2_loc,
3177 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003178 // Do an exact check.
3179 __ Beq(temp, cls, &done);
3180 // Otherwise, we need to check that the object's class is a non-primitive array.
3181 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003182 GenerateReferenceLoadOneRegister(instruction,
3183 temp_loc,
3184 component_offset,
3185 maybe_temp2_loc,
3186 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003187 // If the component type is null, jump to the slow path to throw the exception.
3188 __ Beqz(temp, slow_path->GetEntryLabel());
3189 // Otherwise, the object is indeed an array, further check that this component
3190 // type is not a primitive type.
3191 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3192 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3193 __ Bnez(temp, slow_path->GetEntryLabel());
3194 break;
3195 }
3196
3197 case TypeCheckKind::kUnresolvedCheck:
3198 // We always go into the type check slow path for the unresolved check case.
3199 // We cannot directly call the CheckCast runtime entry point
3200 // without resorting to a type checking slow path here (i.e. by
3201 // calling InvokeRuntime directly), as it would require to
3202 // assign fixed registers for the inputs of this HInstanceOf
3203 // instruction (following the runtime calling convention), which
3204 // might be cluttered by the potential first read barrier
3205 // emission at the beginning of this method.
3206 __ B(slow_path->GetEntryLabel());
3207 break;
3208
3209 case TypeCheckKind::kInterfaceCheck: {
3210 // Avoid read barriers to improve performance of the fast path. We can not get false
3211 // positives by doing this.
3212 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003213 GenerateReferenceLoadTwoRegisters(instruction,
3214 temp_loc,
3215 obj_loc,
3216 class_offset,
3217 maybe_temp2_loc,
3218 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003219 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003220 GenerateReferenceLoadTwoRegisters(instruction,
3221 temp_loc,
3222 temp_loc,
3223 iftable_offset,
3224 maybe_temp2_loc,
3225 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003226 // Iftable is never null.
3227 __ Lw(TMP, temp, array_length_offset);
3228 // Loop through the iftable and check if any class matches.
3229 MipsLabel loop;
3230 __ Bind(&loop);
3231 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3232 __ Beqz(TMP, slow_path->GetEntryLabel());
3233 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3234 __ MaybeUnpoisonHeapReference(AT);
3235 // Go to next interface.
3236 __ Addiu(TMP, TMP, -2);
3237 // Compare the classes and continue the loop if they do not match.
3238 __ Bne(AT, cls, &loop);
3239 break;
3240 }
3241 }
3242
3243 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003244 __ Bind(slow_path->GetExitLabel());
3245}
3246
3247void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3248 LocationSummary* locations =
3249 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3250 locations->SetInAt(0, Location::RequiresRegister());
3251 if (check->HasUses()) {
3252 locations->SetOut(Location::SameAsFirstInput());
3253 }
3254}
3255
3256void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3257 // We assume the class is not null.
3258 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3259 check->GetLoadClass(),
3260 check,
3261 check->GetDexPc(),
3262 true);
3263 codegen_->AddSlowPath(slow_path);
3264 GenerateClassInitializationCheck(slow_path,
3265 check->GetLocations()->InAt(0).AsRegister<Register>());
3266}
3267
3268void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3269 Primitive::Type in_type = compare->InputAt(0)->GetType();
3270
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003271 LocationSummary* locations =
3272 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003273
3274 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003275 case Primitive::kPrimBoolean:
3276 case Primitive::kPrimByte:
3277 case Primitive::kPrimShort:
3278 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003279 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003280 locations->SetInAt(0, Location::RequiresRegister());
3281 locations->SetInAt(1, Location::RequiresRegister());
3282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3283 break;
3284
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003285 case Primitive::kPrimLong:
3286 locations->SetInAt(0, Location::RequiresRegister());
3287 locations->SetInAt(1, Location::RequiresRegister());
3288 // Output overlaps because it is written before doing the low comparison.
3289 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3290 break;
3291
3292 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003293 case Primitive::kPrimDouble:
3294 locations->SetInAt(0, Location::RequiresFpuRegister());
3295 locations->SetInAt(1, Location::RequiresFpuRegister());
3296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003297 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003298
3299 default:
3300 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3301 }
3302}
3303
3304void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3305 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003306 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003307 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003308 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003309
3310 // 0 if: left == right
3311 // 1 if: left > right
3312 // -1 if: left < right
3313 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003314 case Primitive::kPrimBoolean:
3315 case Primitive::kPrimByte:
3316 case Primitive::kPrimShort:
3317 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003318 case Primitive::kPrimInt: {
3319 Register lhs = locations->InAt(0).AsRegister<Register>();
3320 Register rhs = locations->InAt(1).AsRegister<Register>();
3321 __ Slt(TMP, lhs, rhs);
3322 __ Slt(res, rhs, lhs);
3323 __ Subu(res, res, TMP);
3324 break;
3325 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003326 case Primitive::kPrimLong: {
3327 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003328 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3329 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3330 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3331 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3332 // TODO: more efficient (direct) comparison with a constant.
3333 __ Slt(TMP, lhs_high, rhs_high);
3334 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3335 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3336 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3337 __ Sltu(TMP, lhs_low, rhs_low);
3338 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3339 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3340 __ Bind(&done);
3341 break;
3342 }
3343
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003344 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003345 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003346 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3347 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3348 MipsLabel done;
3349 if (isR6) {
3350 __ CmpEqS(FTMP, lhs, rhs);
3351 __ LoadConst32(res, 0);
3352 __ Bc1nez(FTMP, &done);
3353 if (gt_bias) {
3354 __ CmpLtS(FTMP, lhs, rhs);
3355 __ LoadConst32(res, -1);
3356 __ Bc1nez(FTMP, &done);
3357 __ LoadConst32(res, 1);
3358 } else {
3359 __ CmpLtS(FTMP, rhs, lhs);
3360 __ LoadConst32(res, 1);
3361 __ Bc1nez(FTMP, &done);
3362 __ LoadConst32(res, -1);
3363 }
3364 } else {
3365 if (gt_bias) {
3366 __ ColtS(0, lhs, rhs);
3367 __ LoadConst32(res, -1);
3368 __ Bc1t(0, &done);
3369 __ CeqS(0, lhs, rhs);
3370 __ LoadConst32(res, 1);
3371 __ Movt(res, ZERO, 0);
3372 } else {
3373 __ ColtS(0, rhs, lhs);
3374 __ LoadConst32(res, 1);
3375 __ Bc1t(0, &done);
3376 __ CeqS(0, lhs, rhs);
3377 __ LoadConst32(res, -1);
3378 __ Movt(res, ZERO, 0);
3379 }
3380 }
3381 __ Bind(&done);
3382 break;
3383 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003384 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003385 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003386 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3387 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3388 MipsLabel done;
3389 if (isR6) {
3390 __ CmpEqD(FTMP, lhs, rhs);
3391 __ LoadConst32(res, 0);
3392 __ Bc1nez(FTMP, &done);
3393 if (gt_bias) {
3394 __ CmpLtD(FTMP, lhs, rhs);
3395 __ LoadConst32(res, -1);
3396 __ Bc1nez(FTMP, &done);
3397 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003398 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003399 __ CmpLtD(FTMP, rhs, lhs);
3400 __ LoadConst32(res, 1);
3401 __ Bc1nez(FTMP, &done);
3402 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003403 }
3404 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003405 if (gt_bias) {
3406 __ ColtD(0, lhs, rhs);
3407 __ LoadConst32(res, -1);
3408 __ Bc1t(0, &done);
3409 __ CeqD(0, lhs, rhs);
3410 __ LoadConst32(res, 1);
3411 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003412 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003413 __ ColtD(0, rhs, lhs);
3414 __ LoadConst32(res, 1);
3415 __ Bc1t(0, &done);
3416 __ CeqD(0, lhs, rhs);
3417 __ LoadConst32(res, -1);
3418 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003419 }
3420 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003421 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003422 break;
3423 }
3424
3425 default:
3426 LOG(FATAL) << "Unimplemented compare type " << in_type;
3427 }
3428}
3429
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003430void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003431 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003432 switch (instruction->InputAt(0)->GetType()) {
3433 default:
3434 case Primitive::kPrimLong:
3435 locations->SetInAt(0, Location::RequiresRegister());
3436 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3437 break;
3438
3439 case Primitive::kPrimFloat:
3440 case Primitive::kPrimDouble:
3441 locations->SetInAt(0, Location::RequiresFpuRegister());
3442 locations->SetInAt(1, Location::RequiresFpuRegister());
3443 break;
3444 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003445 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3447 }
3448}
3449
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003450void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003451 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003452 return;
3453 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003454
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003455 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003456 LocationSummary* locations = instruction->GetLocations();
3457 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003458 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003459
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003460 switch (type) {
3461 default:
3462 // Integer case.
3463 GenerateIntCompare(instruction->GetCondition(), locations);
3464 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003465
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003466 case Primitive::kPrimLong:
3467 // TODO: don't use branches.
3468 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003469 break;
3470
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003471 case Primitive::kPrimFloat:
3472 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003473 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3474 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003475 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003476
3477 // Convert the branches into the result.
3478 MipsLabel done;
3479
3480 // False case: result = 0.
3481 __ LoadConst32(dst, 0);
3482 __ B(&done);
3483
3484 // True case: result = 1.
3485 __ Bind(&true_label);
3486 __ LoadConst32(dst, 1);
3487 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003488}
3489
Alexey Frunze7e99e052015-11-24 19:28:01 -08003490void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3491 DCHECK(instruction->IsDiv() || instruction->IsRem());
3492 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3493
3494 LocationSummary* locations = instruction->GetLocations();
3495 Location second = locations->InAt(1);
3496 DCHECK(second.IsConstant());
3497
3498 Register out = locations->Out().AsRegister<Register>();
3499 Register dividend = locations->InAt(0).AsRegister<Register>();
3500 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3501 DCHECK(imm == 1 || imm == -1);
3502
3503 if (instruction->IsRem()) {
3504 __ Move(out, ZERO);
3505 } else {
3506 if (imm == -1) {
3507 __ Subu(out, ZERO, dividend);
3508 } else if (out != dividend) {
3509 __ Move(out, dividend);
3510 }
3511 }
3512}
3513
3514void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3515 DCHECK(instruction->IsDiv() || instruction->IsRem());
3516 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3517
3518 LocationSummary* locations = instruction->GetLocations();
3519 Location second = locations->InAt(1);
3520 DCHECK(second.IsConstant());
3521
3522 Register out = locations->Out().AsRegister<Register>();
3523 Register dividend = locations->InAt(0).AsRegister<Register>();
3524 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003525 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003526 int ctz_imm = CTZ(abs_imm);
3527
3528 if (instruction->IsDiv()) {
3529 if (ctz_imm == 1) {
3530 // Fast path for division by +/-2, which is very common.
3531 __ Srl(TMP, dividend, 31);
3532 } else {
3533 __ Sra(TMP, dividend, 31);
3534 __ Srl(TMP, TMP, 32 - ctz_imm);
3535 }
3536 __ Addu(out, dividend, TMP);
3537 __ Sra(out, out, ctz_imm);
3538 if (imm < 0) {
3539 __ Subu(out, ZERO, out);
3540 }
3541 } else {
3542 if (ctz_imm == 1) {
3543 // Fast path for modulo +/-2, which is very common.
3544 __ Sra(TMP, dividend, 31);
3545 __ Subu(out, dividend, TMP);
3546 __ Andi(out, out, 1);
3547 __ Addu(out, out, TMP);
3548 } else {
3549 __ Sra(TMP, dividend, 31);
3550 __ Srl(TMP, TMP, 32 - ctz_imm);
3551 __ Addu(out, dividend, TMP);
3552 if (IsUint<16>(abs_imm - 1)) {
3553 __ Andi(out, out, abs_imm - 1);
3554 } else {
3555 __ Sll(out, out, 32 - ctz_imm);
3556 __ Srl(out, out, 32 - ctz_imm);
3557 }
3558 __ Subu(out, out, TMP);
3559 }
3560 }
3561}
3562
3563void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3564 DCHECK(instruction->IsDiv() || instruction->IsRem());
3565 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3566
3567 LocationSummary* locations = instruction->GetLocations();
3568 Location second = locations->InAt(1);
3569 DCHECK(second.IsConstant());
3570
3571 Register out = locations->Out().AsRegister<Register>();
3572 Register dividend = locations->InAt(0).AsRegister<Register>();
3573 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3574
3575 int64_t magic;
3576 int shift;
3577 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3578
3579 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3580
3581 __ LoadConst32(TMP, magic);
3582 if (isR6) {
3583 __ MuhR6(TMP, dividend, TMP);
3584 } else {
3585 __ MultR2(dividend, TMP);
3586 __ Mfhi(TMP);
3587 }
3588 if (imm > 0 && magic < 0) {
3589 __ Addu(TMP, TMP, dividend);
3590 } else if (imm < 0 && magic > 0) {
3591 __ Subu(TMP, TMP, dividend);
3592 }
3593
3594 if (shift != 0) {
3595 __ Sra(TMP, TMP, shift);
3596 }
3597
3598 if (instruction->IsDiv()) {
3599 __ Sra(out, TMP, 31);
3600 __ Subu(out, TMP, out);
3601 } else {
3602 __ Sra(AT, TMP, 31);
3603 __ Subu(AT, TMP, AT);
3604 __ LoadConst32(TMP, imm);
3605 if (isR6) {
3606 __ MulR6(TMP, AT, TMP);
3607 } else {
3608 __ MulR2(TMP, AT, TMP);
3609 }
3610 __ Subu(out, dividend, TMP);
3611 }
3612}
3613
3614void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3615 DCHECK(instruction->IsDiv() || instruction->IsRem());
3616 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3617
3618 LocationSummary* locations = instruction->GetLocations();
3619 Register out = locations->Out().AsRegister<Register>();
3620 Location second = locations->InAt(1);
3621
3622 if (second.IsConstant()) {
3623 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3624 if (imm == 0) {
3625 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3626 } else if (imm == 1 || imm == -1) {
3627 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003628 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003629 DivRemByPowerOfTwo(instruction);
3630 } else {
3631 DCHECK(imm <= -2 || imm >= 2);
3632 GenerateDivRemWithAnyConstant(instruction);
3633 }
3634 } else {
3635 Register dividend = locations->InAt(0).AsRegister<Register>();
3636 Register divisor = second.AsRegister<Register>();
3637 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3638 if (instruction->IsDiv()) {
3639 if (isR6) {
3640 __ DivR6(out, dividend, divisor);
3641 } else {
3642 __ DivR2(out, dividend, divisor);
3643 }
3644 } else {
3645 if (isR6) {
3646 __ ModR6(out, dividend, divisor);
3647 } else {
3648 __ ModR2(out, dividend, divisor);
3649 }
3650 }
3651 }
3652}
3653
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003654void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3655 Primitive::Type type = div->GetResultType();
3656 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003657 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003658 : LocationSummary::kNoCall;
3659
3660 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3661
3662 switch (type) {
3663 case Primitive::kPrimInt:
3664 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003665 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3667 break;
3668
3669 case Primitive::kPrimLong: {
3670 InvokeRuntimeCallingConvention calling_convention;
3671 locations->SetInAt(0, Location::RegisterPairLocation(
3672 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3673 locations->SetInAt(1, Location::RegisterPairLocation(
3674 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3675 locations->SetOut(calling_convention.GetReturnLocation(type));
3676 break;
3677 }
3678
3679 case Primitive::kPrimFloat:
3680 case Primitive::kPrimDouble:
3681 locations->SetInAt(0, Location::RequiresFpuRegister());
3682 locations->SetInAt(1, Location::RequiresFpuRegister());
3683 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3684 break;
3685
3686 default:
3687 LOG(FATAL) << "Unexpected div type " << type;
3688 }
3689}
3690
3691void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3692 Primitive::Type type = instruction->GetType();
3693 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003694
3695 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003696 case Primitive::kPrimInt:
3697 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003698 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003699 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003700 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003701 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3702 break;
3703 }
3704 case Primitive::kPrimFloat:
3705 case Primitive::kPrimDouble: {
3706 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3707 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3708 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3709 if (type == Primitive::kPrimFloat) {
3710 __ DivS(dst, lhs, rhs);
3711 } else {
3712 __ DivD(dst, lhs, rhs);
3713 }
3714 break;
3715 }
3716 default:
3717 LOG(FATAL) << "Unexpected div type " << type;
3718 }
3719}
3720
3721void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003722 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003723 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003724}
3725
3726void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3727 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3728 codegen_->AddSlowPath(slow_path);
3729 Location value = instruction->GetLocations()->InAt(0);
3730 Primitive::Type type = instruction->GetType();
3731
3732 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003733 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003734 case Primitive::kPrimByte:
3735 case Primitive::kPrimChar:
3736 case Primitive::kPrimShort:
3737 case Primitive::kPrimInt: {
3738 if (value.IsConstant()) {
3739 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3740 __ B(slow_path->GetEntryLabel());
3741 } else {
3742 // A division by a non-null constant is valid. We don't need to perform
3743 // any check, so simply fall through.
3744 }
3745 } else {
3746 DCHECK(value.IsRegister()) << value;
3747 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3748 }
3749 break;
3750 }
3751 case Primitive::kPrimLong: {
3752 if (value.IsConstant()) {
3753 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3754 __ B(slow_path->GetEntryLabel());
3755 } else {
3756 // A division by a non-null constant is valid. We don't need to perform
3757 // any check, so simply fall through.
3758 }
3759 } else {
3760 DCHECK(value.IsRegisterPair()) << value;
3761 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3762 __ Beqz(TMP, slow_path->GetEntryLabel());
3763 }
3764 break;
3765 }
3766 default:
3767 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3768 }
3769}
3770
3771void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3772 LocationSummary* locations =
3773 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3774 locations->SetOut(Location::ConstantLocation(constant));
3775}
3776
3777void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3778 // Will be generated at use site.
3779}
3780
3781void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3782 exit->SetLocations(nullptr);
3783}
3784
3785void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3786}
3787
3788void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3789 LocationSummary* locations =
3790 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3791 locations->SetOut(Location::ConstantLocation(constant));
3792}
3793
3794void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3795 // Will be generated at use site.
3796}
3797
3798void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3799 got->SetLocations(nullptr);
3800}
3801
3802void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3803 DCHECK(!successor->IsExitBlock());
3804 HBasicBlock* block = got->GetBlock();
3805 HInstruction* previous = got->GetPrevious();
3806 HLoopInformation* info = block->GetLoopInformation();
3807
3808 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3809 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3810 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3811 return;
3812 }
3813 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3814 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3815 }
3816 if (!codegen_->GoesToNextBlock(block, successor)) {
3817 __ B(codegen_->GetLabelOf(successor));
3818 }
3819}
3820
3821void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3822 HandleGoto(got, got->GetSuccessor());
3823}
3824
3825void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3826 try_boundary->SetLocations(nullptr);
3827}
3828
3829void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3830 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3831 if (!successor->IsExitBlock()) {
3832 HandleGoto(try_boundary, successor);
3833 }
3834}
3835
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003836void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3837 LocationSummary* locations) {
3838 Register dst = locations->Out().AsRegister<Register>();
3839 Register lhs = locations->InAt(0).AsRegister<Register>();
3840 Location rhs_location = locations->InAt(1);
3841 Register rhs_reg = ZERO;
3842 int64_t rhs_imm = 0;
3843 bool use_imm = rhs_location.IsConstant();
3844 if (use_imm) {
3845 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3846 } else {
3847 rhs_reg = rhs_location.AsRegister<Register>();
3848 }
3849
3850 switch (cond) {
3851 case kCondEQ:
3852 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003853 if (use_imm && IsInt<16>(-rhs_imm)) {
3854 if (rhs_imm == 0) {
3855 if (cond == kCondEQ) {
3856 __ Sltiu(dst, lhs, 1);
3857 } else {
3858 __ Sltu(dst, ZERO, lhs);
3859 }
3860 } else {
3861 __ Addiu(dst, lhs, -rhs_imm);
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 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003868 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003869 if (use_imm && IsUint<16>(rhs_imm)) {
3870 __ Xori(dst, lhs, rhs_imm);
3871 } else {
3872 if (use_imm) {
3873 rhs_reg = TMP;
3874 __ LoadConst32(rhs_reg, rhs_imm);
3875 }
3876 __ Xor(dst, lhs, rhs_reg);
3877 }
3878 if (cond == kCondEQ) {
3879 __ Sltiu(dst, dst, 1);
3880 } else {
3881 __ Sltu(dst, ZERO, dst);
3882 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003883 }
3884 break;
3885
3886 case kCondLT:
3887 case kCondGE:
3888 if (use_imm && IsInt<16>(rhs_imm)) {
3889 __ Slti(dst, lhs, rhs_imm);
3890 } else {
3891 if (use_imm) {
3892 rhs_reg = TMP;
3893 __ LoadConst32(rhs_reg, rhs_imm);
3894 }
3895 __ Slt(dst, lhs, rhs_reg);
3896 }
3897 if (cond == kCondGE) {
3898 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3899 // only the slt instruction but no sge.
3900 __ Xori(dst, dst, 1);
3901 }
3902 break;
3903
3904 case kCondLE:
3905 case kCondGT:
3906 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3907 // Simulate lhs <= rhs via lhs < rhs + 1.
3908 __ Slti(dst, lhs, rhs_imm + 1);
3909 if (cond == kCondGT) {
3910 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3911 // only the slti instruction but no sgti.
3912 __ Xori(dst, dst, 1);
3913 }
3914 } else {
3915 if (use_imm) {
3916 rhs_reg = TMP;
3917 __ LoadConst32(rhs_reg, rhs_imm);
3918 }
3919 __ Slt(dst, rhs_reg, lhs);
3920 if (cond == kCondLE) {
3921 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3922 // only the slt instruction but no sle.
3923 __ Xori(dst, dst, 1);
3924 }
3925 }
3926 break;
3927
3928 case kCondB:
3929 case kCondAE:
3930 if (use_imm && IsInt<16>(rhs_imm)) {
3931 // Sltiu sign-extends its 16-bit immediate operand before
3932 // the comparison and thus lets us compare directly with
3933 // unsigned values in the ranges [0, 0x7fff] and
3934 // [0xffff8000, 0xffffffff].
3935 __ Sltiu(dst, lhs, rhs_imm);
3936 } else {
3937 if (use_imm) {
3938 rhs_reg = TMP;
3939 __ LoadConst32(rhs_reg, rhs_imm);
3940 }
3941 __ Sltu(dst, lhs, rhs_reg);
3942 }
3943 if (cond == kCondAE) {
3944 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3945 // only the sltu instruction but no sgeu.
3946 __ Xori(dst, dst, 1);
3947 }
3948 break;
3949
3950 case kCondBE:
3951 case kCondA:
3952 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3953 // Simulate lhs <= rhs via lhs < rhs + 1.
3954 // Note that this only works if rhs + 1 does not overflow
3955 // to 0, hence the check above.
3956 // Sltiu sign-extends its 16-bit immediate operand before
3957 // the comparison and thus lets us compare directly with
3958 // unsigned values in the ranges [0, 0x7fff] and
3959 // [0xffff8000, 0xffffffff].
3960 __ Sltiu(dst, lhs, rhs_imm + 1);
3961 if (cond == kCondA) {
3962 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3963 // only the sltiu instruction but no sgtiu.
3964 __ Xori(dst, dst, 1);
3965 }
3966 } else {
3967 if (use_imm) {
3968 rhs_reg = TMP;
3969 __ LoadConst32(rhs_reg, rhs_imm);
3970 }
3971 __ Sltu(dst, rhs_reg, lhs);
3972 if (cond == kCondBE) {
3973 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3974 // only the sltu instruction but no sleu.
3975 __ Xori(dst, dst, 1);
3976 }
3977 }
3978 break;
3979 }
3980}
3981
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003982bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3983 LocationSummary* input_locations,
3984 Register dst) {
3985 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3986 Location rhs_location = input_locations->InAt(1);
3987 Register rhs_reg = ZERO;
3988 int64_t rhs_imm = 0;
3989 bool use_imm = rhs_location.IsConstant();
3990 if (use_imm) {
3991 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3992 } else {
3993 rhs_reg = rhs_location.AsRegister<Register>();
3994 }
3995
3996 switch (cond) {
3997 case kCondEQ:
3998 case kCondNE:
3999 if (use_imm && IsInt<16>(-rhs_imm)) {
4000 __ Addiu(dst, lhs, -rhs_imm);
4001 } else if (use_imm && IsUint<16>(rhs_imm)) {
4002 __ Xori(dst, lhs, rhs_imm);
4003 } else {
4004 if (use_imm) {
4005 rhs_reg = TMP;
4006 __ LoadConst32(rhs_reg, rhs_imm);
4007 }
4008 __ Xor(dst, lhs, rhs_reg);
4009 }
4010 return (cond == kCondEQ);
4011
4012 case kCondLT:
4013 case kCondGE:
4014 if (use_imm && IsInt<16>(rhs_imm)) {
4015 __ Slti(dst, lhs, rhs_imm);
4016 } else {
4017 if (use_imm) {
4018 rhs_reg = TMP;
4019 __ LoadConst32(rhs_reg, rhs_imm);
4020 }
4021 __ Slt(dst, lhs, rhs_reg);
4022 }
4023 return (cond == kCondGE);
4024
4025 case kCondLE:
4026 case kCondGT:
4027 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4028 // Simulate lhs <= rhs via lhs < rhs + 1.
4029 __ Slti(dst, lhs, rhs_imm + 1);
4030 return (cond == kCondGT);
4031 } else {
4032 if (use_imm) {
4033 rhs_reg = TMP;
4034 __ LoadConst32(rhs_reg, rhs_imm);
4035 }
4036 __ Slt(dst, rhs_reg, lhs);
4037 return (cond == kCondLE);
4038 }
4039
4040 case kCondB:
4041 case kCondAE:
4042 if (use_imm && IsInt<16>(rhs_imm)) {
4043 // Sltiu sign-extends its 16-bit immediate operand before
4044 // the comparison and thus lets us compare directly with
4045 // unsigned values in the ranges [0, 0x7fff] and
4046 // [0xffff8000, 0xffffffff].
4047 __ Sltiu(dst, lhs, rhs_imm);
4048 } else {
4049 if (use_imm) {
4050 rhs_reg = TMP;
4051 __ LoadConst32(rhs_reg, rhs_imm);
4052 }
4053 __ Sltu(dst, lhs, rhs_reg);
4054 }
4055 return (cond == kCondAE);
4056
4057 case kCondBE:
4058 case kCondA:
4059 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4060 // Simulate lhs <= rhs via lhs < rhs + 1.
4061 // Note that this only works if rhs + 1 does not overflow
4062 // to 0, hence the check above.
4063 // Sltiu sign-extends its 16-bit immediate operand before
4064 // the comparison and thus lets us compare directly with
4065 // unsigned values in the ranges [0, 0x7fff] and
4066 // [0xffff8000, 0xffffffff].
4067 __ Sltiu(dst, lhs, rhs_imm + 1);
4068 return (cond == kCondA);
4069 } else {
4070 if (use_imm) {
4071 rhs_reg = TMP;
4072 __ LoadConst32(rhs_reg, rhs_imm);
4073 }
4074 __ Sltu(dst, rhs_reg, lhs);
4075 return (cond == kCondBE);
4076 }
4077 }
4078}
4079
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004080void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4081 LocationSummary* locations,
4082 MipsLabel* label) {
4083 Register lhs = locations->InAt(0).AsRegister<Register>();
4084 Location rhs_location = locations->InAt(1);
4085 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004086 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004087 bool use_imm = rhs_location.IsConstant();
4088 if (use_imm) {
4089 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4090 } else {
4091 rhs_reg = rhs_location.AsRegister<Register>();
4092 }
4093
4094 if (use_imm && rhs_imm == 0) {
4095 switch (cond) {
4096 case kCondEQ:
4097 case kCondBE: // <= 0 if zero
4098 __ Beqz(lhs, label);
4099 break;
4100 case kCondNE:
4101 case kCondA: // > 0 if non-zero
4102 __ Bnez(lhs, label);
4103 break;
4104 case kCondLT:
4105 __ Bltz(lhs, label);
4106 break;
4107 case kCondGE:
4108 __ Bgez(lhs, label);
4109 break;
4110 case kCondLE:
4111 __ Blez(lhs, label);
4112 break;
4113 case kCondGT:
4114 __ Bgtz(lhs, label);
4115 break;
4116 case kCondB: // always false
4117 break;
4118 case kCondAE: // always true
4119 __ B(label);
4120 break;
4121 }
4122 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004123 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4124 if (isR6 || !use_imm) {
4125 if (use_imm) {
4126 rhs_reg = TMP;
4127 __ LoadConst32(rhs_reg, rhs_imm);
4128 }
4129 switch (cond) {
4130 case kCondEQ:
4131 __ Beq(lhs, rhs_reg, label);
4132 break;
4133 case kCondNE:
4134 __ Bne(lhs, rhs_reg, label);
4135 break;
4136 case kCondLT:
4137 __ Blt(lhs, rhs_reg, label);
4138 break;
4139 case kCondGE:
4140 __ Bge(lhs, rhs_reg, label);
4141 break;
4142 case kCondLE:
4143 __ Bge(rhs_reg, lhs, label);
4144 break;
4145 case kCondGT:
4146 __ Blt(rhs_reg, lhs, label);
4147 break;
4148 case kCondB:
4149 __ Bltu(lhs, rhs_reg, label);
4150 break;
4151 case kCondAE:
4152 __ Bgeu(lhs, rhs_reg, label);
4153 break;
4154 case kCondBE:
4155 __ Bgeu(rhs_reg, lhs, label);
4156 break;
4157 case kCondA:
4158 __ Bltu(rhs_reg, lhs, label);
4159 break;
4160 }
4161 } else {
4162 // Special cases for more efficient comparison with constants on R2.
4163 switch (cond) {
4164 case kCondEQ:
4165 __ LoadConst32(TMP, rhs_imm);
4166 __ Beq(lhs, TMP, label);
4167 break;
4168 case kCondNE:
4169 __ LoadConst32(TMP, rhs_imm);
4170 __ Bne(lhs, TMP, label);
4171 break;
4172 case kCondLT:
4173 if (IsInt<16>(rhs_imm)) {
4174 __ Slti(TMP, lhs, rhs_imm);
4175 __ Bnez(TMP, label);
4176 } else {
4177 __ LoadConst32(TMP, rhs_imm);
4178 __ Blt(lhs, TMP, label);
4179 }
4180 break;
4181 case kCondGE:
4182 if (IsInt<16>(rhs_imm)) {
4183 __ Slti(TMP, lhs, rhs_imm);
4184 __ Beqz(TMP, label);
4185 } else {
4186 __ LoadConst32(TMP, rhs_imm);
4187 __ Bge(lhs, TMP, label);
4188 }
4189 break;
4190 case kCondLE:
4191 if (IsInt<16>(rhs_imm + 1)) {
4192 // Simulate lhs <= rhs via lhs < rhs + 1.
4193 __ Slti(TMP, lhs, rhs_imm + 1);
4194 __ Bnez(TMP, label);
4195 } else {
4196 __ LoadConst32(TMP, rhs_imm);
4197 __ Bge(TMP, lhs, label);
4198 }
4199 break;
4200 case kCondGT:
4201 if (IsInt<16>(rhs_imm + 1)) {
4202 // Simulate lhs > rhs via !(lhs < rhs + 1).
4203 __ Slti(TMP, lhs, rhs_imm + 1);
4204 __ Beqz(TMP, label);
4205 } else {
4206 __ LoadConst32(TMP, rhs_imm);
4207 __ Blt(TMP, lhs, label);
4208 }
4209 break;
4210 case kCondB:
4211 if (IsInt<16>(rhs_imm)) {
4212 __ Sltiu(TMP, lhs, rhs_imm);
4213 __ Bnez(TMP, label);
4214 } else {
4215 __ LoadConst32(TMP, rhs_imm);
4216 __ Bltu(lhs, TMP, label);
4217 }
4218 break;
4219 case kCondAE:
4220 if (IsInt<16>(rhs_imm)) {
4221 __ Sltiu(TMP, lhs, rhs_imm);
4222 __ Beqz(TMP, label);
4223 } else {
4224 __ LoadConst32(TMP, rhs_imm);
4225 __ Bgeu(lhs, TMP, label);
4226 }
4227 break;
4228 case kCondBE:
4229 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4230 // Simulate lhs <= rhs via lhs < rhs + 1.
4231 // Note that this only works if rhs + 1 does not overflow
4232 // to 0, hence the check above.
4233 __ Sltiu(TMP, lhs, rhs_imm + 1);
4234 __ Bnez(TMP, label);
4235 } else {
4236 __ LoadConst32(TMP, rhs_imm);
4237 __ Bgeu(TMP, lhs, label);
4238 }
4239 break;
4240 case kCondA:
4241 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4242 // Simulate lhs > rhs via !(lhs < rhs + 1).
4243 // Note that this only works if rhs + 1 does not overflow
4244 // to 0, hence the check above.
4245 __ Sltiu(TMP, lhs, rhs_imm + 1);
4246 __ Beqz(TMP, label);
4247 } else {
4248 __ LoadConst32(TMP, rhs_imm);
4249 __ Bltu(TMP, lhs, label);
4250 }
4251 break;
4252 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004253 }
4254 }
4255}
4256
4257void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4258 LocationSummary* locations,
4259 MipsLabel* label) {
4260 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4261 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4262 Location rhs_location = locations->InAt(1);
4263 Register rhs_high = ZERO;
4264 Register rhs_low = ZERO;
4265 int64_t imm = 0;
4266 uint32_t imm_high = 0;
4267 uint32_t imm_low = 0;
4268 bool use_imm = rhs_location.IsConstant();
4269 if (use_imm) {
4270 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4271 imm_high = High32Bits(imm);
4272 imm_low = Low32Bits(imm);
4273 } else {
4274 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4275 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4276 }
4277
4278 if (use_imm && imm == 0) {
4279 switch (cond) {
4280 case kCondEQ:
4281 case kCondBE: // <= 0 if zero
4282 __ Or(TMP, lhs_high, lhs_low);
4283 __ Beqz(TMP, label);
4284 break;
4285 case kCondNE:
4286 case kCondA: // > 0 if non-zero
4287 __ Or(TMP, lhs_high, lhs_low);
4288 __ Bnez(TMP, label);
4289 break;
4290 case kCondLT:
4291 __ Bltz(lhs_high, label);
4292 break;
4293 case kCondGE:
4294 __ Bgez(lhs_high, label);
4295 break;
4296 case kCondLE:
4297 __ Or(TMP, lhs_high, lhs_low);
4298 __ Sra(AT, lhs_high, 31);
4299 __ Bgeu(AT, TMP, label);
4300 break;
4301 case kCondGT:
4302 __ Or(TMP, lhs_high, lhs_low);
4303 __ Sra(AT, lhs_high, 31);
4304 __ Bltu(AT, TMP, label);
4305 break;
4306 case kCondB: // always false
4307 break;
4308 case kCondAE: // always true
4309 __ B(label);
4310 break;
4311 }
4312 } else if (use_imm) {
4313 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4314 switch (cond) {
4315 case kCondEQ:
4316 __ LoadConst32(TMP, imm_high);
4317 __ Xor(TMP, TMP, lhs_high);
4318 __ LoadConst32(AT, imm_low);
4319 __ Xor(AT, AT, lhs_low);
4320 __ Or(TMP, TMP, AT);
4321 __ Beqz(TMP, label);
4322 break;
4323 case kCondNE:
4324 __ LoadConst32(TMP, imm_high);
4325 __ Xor(TMP, TMP, lhs_high);
4326 __ LoadConst32(AT, imm_low);
4327 __ Xor(AT, AT, lhs_low);
4328 __ Or(TMP, TMP, AT);
4329 __ Bnez(TMP, label);
4330 break;
4331 case kCondLT:
4332 __ LoadConst32(TMP, imm_high);
4333 __ Blt(lhs_high, TMP, label);
4334 __ Slt(TMP, TMP, lhs_high);
4335 __ LoadConst32(AT, imm_low);
4336 __ Sltu(AT, lhs_low, AT);
4337 __ Blt(TMP, AT, label);
4338 break;
4339 case kCondGE:
4340 __ LoadConst32(TMP, imm_high);
4341 __ Blt(TMP, lhs_high, label);
4342 __ Slt(TMP, lhs_high, TMP);
4343 __ LoadConst32(AT, imm_low);
4344 __ Sltu(AT, lhs_low, AT);
4345 __ Or(TMP, TMP, AT);
4346 __ Beqz(TMP, label);
4347 break;
4348 case kCondLE:
4349 __ LoadConst32(TMP, imm_high);
4350 __ Blt(lhs_high, TMP, label);
4351 __ Slt(TMP, TMP, lhs_high);
4352 __ LoadConst32(AT, imm_low);
4353 __ Sltu(AT, AT, lhs_low);
4354 __ Or(TMP, TMP, AT);
4355 __ Beqz(TMP, label);
4356 break;
4357 case kCondGT:
4358 __ LoadConst32(TMP, imm_high);
4359 __ Blt(TMP, lhs_high, label);
4360 __ Slt(TMP, lhs_high, TMP);
4361 __ LoadConst32(AT, imm_low);
4362 __ Sltu(AT, AT, lhs_low);
4363 __ Blt(TMP, AT, label);
4364 break;
4365 case kCondB:
4366 __ LoadConst32(TMP, imm_high);
4367 __ Bltu(lhs_high, TMP, label);
4368 __ Sltu(TMP, TMP, lhs_high);
4369 __ LoadConst32(AT, imm_low);
4370 __ Sltu(AT, lhs_low, AT);
4371 __ Blt(TMP, AT, label);
4372 break;
4373 case kCondAE:
4374 __ LoadConst32(TMP, imm_high);
4375 __ Bltu(TMP, lhs_high, label);
4376 __ Sltu(TMP, lhs_high, TMP);
4377 __ LoadConst32(AT, imm_low);
4378 __ Sltu(AT, lhs_low, AT);
4379 __ Or(TMP, TMP, AT);
4380 __ Beqz(TMP, label);
4381 break;
4382 case kCondBE:
4383 __ LoadConst32(TMP, imm_high);
4384 __ Bltu(lhs_high, TMP, label);
4385 __ Sltu(TMP, TMP, lhs_high);
4386 __ LoadConst32(AT, imm_low);
4387 __ Sltu(AT, AT, lhs_low);
4388 __ Or(TMP, TMP, AT);
4389 __ Beqz(TMP, label);
4390 break;
4391 case kCondA:
4392 __ LoadConst32(TMP, imm_high);
4393 __ Bltu(TMP, lhs_high, label);
4394 __ Sltu(TMP, lhs_high, TMP);
4395 __ LoadConst32(AT, imm_low);
4396 __ Sltu(AT, AT, lhs_low);
4397 __ Blt(TMP, AT, label);
4398 break;
4399 }
4400 } else {
4401 switch (cond) {
4402 case kCondEQ:
4403 __ Xor(TMP, lhs_high, rhs_high);
4404 __ Xor(AT, lhs_low, rhs_low);
4405 __ Or(TMP, TMP, AT);
4406 __ Beqz(TMP, label);
4407 break;
4408 case kCondNE:
4409 __ Xor(TMP, lhs_high, rhs_high);
4410 __ Xor(AT, lhs_low, rhs_low);
4411 __ Or(TMP, TMP, AT);
4412 __ Bnez(TMP, label);
4413 break;
4414 case kCondLT:
4415 __ Blt(lhs_high, rhs_high, label);
4416 __ Slt(TMP, rhs_high, lhs_high);
4417 __ Sltu(AT, lhs_low, rhs_low);
4418 __ Blt(TMP, AT, label);
4419 break;
4420 case kCondGE:
4421 __ Blt(rhs_high, lhs_high, label);
4422 __ Slt(TMP, lhs_high, rhs_high);
4423 __ Sltu(AT, lhs_low, rhs_low);
4424 __ Or(TMP, TMP, AT);
4425 __ Beqz(TMP, label);
4426 break;
4427 case kCondLE:
4428 __ Blt(lhs_high, rhs_high, label);
4429 __ Slt(TMP, rhs_high, lhs_high);
4430 __ Sltu(AT, rhs_low, lhs_low);
4431 __ Or(TMP, TMP, AT);
4432 __ Beqz(TMP, label);
4433 break;
4434 case kCondGT:
4435 __ Blt(rhs_high, lhs_high, label);
4436 __ Slt(TMP, lhs_high, rhs_high);
4437 __ Sltu(AT, rhs_low, lhs_low);
4438 __ Blt(TMP, AT, label);
4439 break;
4440 case kCondB:
4441 __ Bltu(lhs_high, rhs_high, label);
4442 __ Sltu(TMP, rhs_high, lhs_high);
4443 __ Sltu(AT, lhs_low, rhs_low);
4444 __ Blt(TMP, AT, label);
4445 break;
4446 case kCondAE:
4447 __ Bltu(rhs_high, lhs_high, label);
4448 __ Sltu(TMP, lhs_high, rhs_high);
4449 __ Sltu(AT, lhs_low, rhs_low);
4450 __ Or(TMP, TMP, AT);
4451 __ Beqz(TMP, label);
4452 break;
4453 case kCondBE:
4454 __ Bltu(lhs_high, rhs_high, label);
4455 __ Sltu(TMP, rhs_high, lhs_high);
4456 __ Sltu(AT, rhs_low, lhs_low);
4457 __ Or(TMP, TMP, AT);
4458 __ Beqz(TMP, label);
4459 break;
4460 case kCondA:
4461 __ Bltu(rhs_high, lhs_high, label);
4462 __ Sltu(TMP, lhs_high, rhs_high);
4463 __ Sltu(AT, rhs_low, lhs_low);
4464 __ Blt(TMP, AT, label);
4465 break;
4466 }
4467 }
4468}
4469
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004470void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4471 bool gt_bias,
4472 Primitive::Type type,
4473 LocationSummary* locations) {
4474 Register dst = locations->Out().AsRegister<Register>();
4475 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4476 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4477 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4478 if (type == Primitive::kPrimFloat) {
4479 if (isR6) {
4480 switch (cond) {
4481 case kCondEQ:
4482 __ CmpEqS(FTMP, lhs, rhs);
4483 __ Mfc1(dst, FTMP);
4484 __ Andi(dst, dst, 1);
4485 break;
4486 case kCondNE:
4487 __ CmpEqS(FTMP, lhs, rhs);
4488 __ Mfc1(dst, FTMP);
4489 __ Addiu(dst, dst, 1);
4490 break;
4491 case kCondLT:
4492 if (gt_bias) {
4493 __ CmpLtS(FTMP, lhs, rhs);
4494 } else {
4495 __ CmpUltS(FTMP, lhs, rhs);
4496 }
4497 __ Mfc1(dst, FTMP);
4498 __ Andi(dst, dst, 1);
4499 break;
4500 case kCondLE:
4501 if (gt_bias) {
4502 __ CmpLeS(FTMP, lhs, rhs);
4503 } else {
4504 __ CmpUleS(FTMP, lhs, rhs);
4505 }
4506 __ Mfc1(dst, FTMP);
4507 __ Andi(dst, dst, 1);
4508 break;
4509 case kCondGT:
4510 if (gt_bias) {
4511 __ CmpUltS(FTMP, rhs, lhs);
4512 } else {
4513 __ CmpLtS(FTMP, rhs, lhs);
4514 }
4515 __ Mfc1(dst, FTMP);
4516 __ Andi(dst, dst, 1);
4517 break;
4518 case kCondGE:
4519 if (gt_bias) {
4520 __ CmpUleS(FTMP, rhs, lhs);
4521 } else {
4522 __ CmpLeS(FTMP, rhs, lhs);
4523 }
4524 __ Mfc1(dst, FTMP);
4525 __ Andi(dst, dst, 1);
4526 break;
4527 default:
4528 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4529 UNREACHABLE();
4530 }
4531 } else {
4532 switch (cond) {
4533 case kCondEQ:
4534 __ CeqS(0, lhs, rhs);
4535 __ LoadConst32(dst, 1);
4536 __ Movf(dst, ZERO, 0);
4537 break;
4538 case kCondNE:
4539 __ CeqS(0, lhs, rhs);
4540 __ LoadConst32(dst, 1);
4541 __ Movt(dst, ZERO, 0);
4542 break;
4543 case kCondLT:
4544 if (gt_bias) {
4545 __ ColtS(0, lhs, rhs);
4546 } else {
4547 __ CultS(0, lhs, rhs);
4548 }
4549 __ LoadConst32(dst, 1);
4550 __ Movf(dst, ZERO, 0);
4551 break;
4552 case kCondLE:
4553 if (gt_bias) {
4554 __ ColeS(0, lhs, rhs);
4555 } else {
4556 __ CuleS(0, lhs, rhs);
4557 }
4558 __ LoadConst32(dst, 1);
4559 __ Movf(dst, ZERO, 0);
4560 break;
4561 case kCondGT:
4562 if (gt_bias) {
4563 __ CultS(0, rhs, lhs);
4564 } else {
4565 __ ColtS(0, rhs, lhs);
4566 }
4567 __ LoadConst32(dst, 1);
4568 __ Movf(dst, ZERO, 0);
4569 break;
4570 case kCondGE:
4571 if (gt_bias) {
4572 __ CuleS(0, rhs, lhs);
4573 } else {
4574 __ ColeS(0, rhs, lhs);
4575 }
4576 __ LoadConst32(dst, 1);
4577 __ Movf(dst, ZERO, 0);
4578 break;
4579 default:
4580 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4581 UNREACHABLE();
4582 }
4583 }
4584 } else {
4585 DCHECK_EQ(type, Primitive::kPrimDouble);
4586 if (isR6) {
4587 switch (cond) {
4588 case kCondEQ:
4589 __ CmpEqD(FTMP, lhs, rhs);
4590 __ Mfc1(dst, FTMP);
4591 __ Andi(dst, dst, 1);
4592 break;
4593 case kCondNE:
4594 __ CmpEqD(FTMP, lhs, rhs);
4595 __ Mfc1(dst, FTMP);
4596 __ Addiu(dst, dst, 1);
4597 break;
4598 case kCondLT:
4599 if (gt_bias) {
4600 __ CmpLtD(FTMP, lhs, rhs);
4601 } else {
4602 __ CmpUltD(FTMP, lhs, rhs);
4603 }
4604 __ Mfc1(dst, FTMP);
4605 __ Andi(dst, dst, 1);
4606 break;
4607 case kCondLE:
4608 if (gt_bias) {
4609 __ CmpLeD(FTMP, lhs, rhs);
4610 } else {
4611 __ CmpUleD(FTMP, lhs, rhs);
4612 }
4613 __ Mfc1(dst, FTMP);
4614 __ Andi(dst, dst, 1);
4615 break;
4616 case kCondGT:
4617 if (gt_bias) {
4618 __ CmpUltD(FTMP, rhs, lhs);
4619 } else {
4620 __ CmpLtD(FTMP, rhs, lhs);
4621 }
4622 __ Mfc1(dst, FTMP);
4623 __ Andi(dst, dst, 1);
4624 break;
4625 case kCondGE:
4626 if (gt_bias) {
4627 __ CmpUleD(FTMP, rhs, lhs);
4628 } else {
4629 __ CmpLeD(FTMP, rhs, lhs);
4630 }
4631 __ Mfc1(dst, FTMP);
4632 __ Andi(dst, dst, 1);
4633 break;
4634 default:
4635 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4636 UNREACHABLE();
4637 }
4638 } else {
4639 switch (cond) {
4640 case kCondEQ:
4641 __ CeqD(0, lhs, rhs);
4642 __ LoadConst32(dst, 1);
4643 __ Movf(dst, ZERO, 0);
4644 break;
4645 case kCondNE:
4646 __ CeqD(0, lhs, rhs);
4647 __ LoadConst32(dst, 1);
4648 __ Movt(dst, ZERO, 0);
4649 break;
4650 case kCondLT:
4651 if (gt_bias) {
4652 __ ColtD(0, lhs, rhs);
4653 } else {
4654 __ CultD(0, lhs, rhs);
4655 }
4656 __ LoadConst32(dst, 1);
4657 __ Movf(dst, ZERO, 0);
4658 break;
4659 case kCondLE:
4660 if (gt_bias) {
4661 __ ColeD(0, lhs, rhs);
4662 } else {
4663 __ CuleD(0, lhs, rhs);
4664 }
4665 __ LoadConst32(dst, 1);
4666 __ Movf(dst, ZERO, 0);
4667 break;
4668 case kCondGT:
4669 if (gt_bias) {
4670 __ CultD(0, rhs, lhs);
4671 } else {
4672 __ ColtD(0, rhs, lhs);
4673 }
4674 __ LoadConst32(dst, 1);
4675 __ Movf(dst, ZERO, 0);
4676 break;
4677 case kCondGE:
4678 if (gt_bias) {
4679 __ CuleD(0, rhs, lhs);
4680 } else {
4681 __ ColeD(0, rhs, lhs);
4682 }
4683 __ LoadConst32(dst, 1);
4684 __ Movf(dst, ZERO, 0);
4685 break;
4686 default:
4687 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4688 UNREACHABLE();
4689 }
4690 }
4691 }
4692}
4693
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004694bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4695 bool gt_bias,
4696 Primitive::Type type,
4697 LocationSummary* input_locations,
4698 int cc) {
4699 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4700 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4701 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4702 if (type == Primitive::kPrimFloat) {
4703 switch (cond) {
4704 case kCondEQ:
4705 __ CeqS(cc, lhs, rhs);
4706 return false;
4707 case kCondNE:
4708 __ CeqS(cc, lhs, rhs);
4709 return true;
4710 case kCondLT:
4711 if (gt_bias) {
4712 __ ColtS(cc, lhs, rhs);
4713 } else {
4714 __ CultS(cc, lhs, rhs);
4715 }
4716 return false;
4717 case kCondLE:
4718 if (gt_bias) {
4719 __ ColeS(cc, lhs, rhs);
4720 } else {
4721 __ CuleS(cc, lhs, rhs);
4722 }
4723 return false;
4724 case kCondGT:
4725 if (gt_bias) {
4726 __ CultS(cc, rhs, lhs);
4727 } else {
4728 __ ColtS(cc, rhs, lhs);
4729 }
4730 return false;
4731 case kCondGE:
4732 if (gt_bias) {
4733 __ CuleS(cc, rhs, lhs);
4734 } else {
4735 __ ColeS(cc, rhs, lhs);
4736 }
4737 return false;
4738 default:
4739 LOG(FATAL) << "Unexpected non-floating-point condition";
4740 UNREACHABLE();
4741 }
4742 } else {
4743 DCHECK_EQ(type, Primitive::kPrimDouble);
4744 switch (cond) {
4745 case kCondEQ:
4746 __ CeqD(cc, lhs, rhs);
4747 return false;
4748 case kCondNE:
4749 __ CeqD(cc, lhs, rhs);
4750 return true;
4751 case kCondLT:
4752 if (gt_bias) {
4753 __ ColtD(cc, lhs, rhs);
4754 } else {
4755 __ CultD(cc, lhs, rhs);
4756 }
4757 return false;
4758 case kCondLE:
4759 if (gt_bias) {
4760 __ ColeD(cc, lhs, rhs);
4761 } else {
4762 __ CuleD(cc, lhs, rhs);
4763 }
4764 return false;
4765 case kCondGT:
4766 if (gt_bias) {
4767 __ CultD(cc, rhs, lhs);
4768 } else {
4769 __ ColtD(cc, rhs, lhs);
4770 }
4771 return false;
4772 case kCondGE:
4773 if (gt_bias) {
4774 __ CuleD(cc, rhs, lhs);
4775 } else {
4776 __ ColeD(cc, rhs, lhs);
4777 }
4778 return false;
4779 default:
4780 LOG(FATAL) << "Unexpected non-floating-point condition";
4781 UNREACHABLE();
4782 }
4783 }
4784}
4785
4786bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4787 bool gt_bias,
4788 Primitive::Type type,
4789 LocationSummary* input_locations,
4790 FRegister dst) {
4791 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4792 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4793 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4794 if (type == Primitive::kPrimFloat) {
4795 switch (cond) {
4796 case kCondEQ:
4797 __ CmpEqS(dst, lhs, rhs);
4798 return false;
4799 case kCondNE:
4800 __ CmpEqS(dst, lhs, rhs);
4801 return true;
4802 case kCondLT:
4803 if (gt_bias) {
4804 __ CmpLtS(dst, lhs, rhs);
4805 } else {
4806 __ CmpUltS(dst, lhs, rhs);
4807 }
4808 return false;
4809 case kCondLE:
4810 if (gt_bias) {
4811 __ CmpLeS(dst, lhs, rhs);
4812 } else {
4813 __ CmpUleS(dst, lhs, rhs);
4814 }
4815 return false;
4816 case kCondGT:
4817 if (gt_bias) {
4818 __ CmpUltS(dst, rhs, lhs);
4819 } else {
4820 __ CmpLtS(dst, rhs, lhs);
4821 }
4822 return false;
4823 case kCondGE:
4824 if (gt_bias) {
4825 __ CmpUleS(dst, rhs, lhs);
4826 } else {
4827 __ CmpLeS(dst, rhs, lhs);
4828 }
4829 return false;
4830 default:
4831 LOG(FATAL) << "Unexpected non-floating-point condition";
4832 UNREACHABLE();
4833 }
4834 } else {
4835 DCHECK_EQ(type, Primitive::kPrimDouble);
4836 switch (cond) {
4837 case kCondEQ:
4838 __ CmpEqD(dst, lhs, rhs);
4839 return false;
4840 case kCondNE:
4841 __ CmpEqD(dst, lhs, rhs);
4842 return true;
4843 case kCondLT:
4844 if (gt_bias) {
4845 __ CmpLtD(dst, lhs, rhs);
4846 } else {
4847 __ CmpUltD(dst, lhs, rhs);
4848 }
4849 return false;
4850 case kCondLE:
4851 if (gt_bias) {
4852 __ CmpLeD(dst, lhs, rhs);
4853 } else {
4854 __ CmpUleD(dst, lhs, rhs);
4855 }
4856 return false;
4857 case kCondGT:
4858 if (gt_bias) {
4859 __ CmpUltD(dst, rhs, lhs);
4860 } else {
4861 __ CmpLtD(dst, rhs, lhs);
4862 }
4863 return false;
4864 case kCondGE:
4865 if (gt_bias) {
4866 __ CmpUleD(dst, rhs, lhs);
4867 } else {
4868 __ CmpLeD(dst, rhs, lhs);
4869 }
4870 return false;
4871 default:
4872 LOG(FATAL) << "Unexpected non-floating-point condition";
4873 UNREACHABLE();
4874 }
4875 }
4876}
4877
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004878void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
4879 bool gt_bias,
4880 Primitive::Type type,
4881 LocationSummary* locations,
4882 MipsLabel* label) {
4883 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4884 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4885 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4886 if (type == Primitive::kPrimFloat) {
4887 if (isR6) {
4888 switch (cond) {
4889 case kCondEQ:
4890 __ CmpEqS(FTMP, lhs, rhs);
4891 __ Bc1nez(FTMP, label);
4892 break;
4893 case kCondNE:
4894 __ CmpEqS(FTMP, lhs, rhs);
4895 __ Bc1eqz(FTMP, label);
4896 break;
4897 case kCondLT:
4898 if (gt_bias) {
4899 __ CmpLtS(FTMP, lhs, rhs);
4900 } else {
4901 __ CmpUltS(FTMP, lhs, rhs);
4902 }
4903 __ Bc1nez(FTMP, label);
4904 break;
4905 case kCondLE:
4906 if (gt_bias) {
4907 __ CmpLeS(FTMP, lhs, rhs);
4908 } else {
4909 __ CmpUleS(FTMP, lhs, rhs);
4910 }
4911 __ Bc1nez(FTMP, label);
4912 break;
4913 case kCondGT:
4914 if (gt_bias) {
4915 __ CmpUltS(FTMP, rhs, lhs);
4916 } else {
4917 __ CmpLtS(FTMP, rhs, lhs);
4918 }
4919 __ Bc1nez(FTMP, label);
4920 break;
4921 case kCondGE:
4922 if (gt_bias) {
4923 __ CmpUleS(FTMP, rhs, lhs);
4924 } else {
4925 __ CmpLeS(FTMP, rhs, lhs);
4926 }
4927 __ Bc1nez(FTMP, label);
4928 break;
4929 default:
4930 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004931 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004932 }
4933 } else {
4934 switch (cond) {
4935 case kCondEQ:
4936 __ CeqS(0, lhs, rhs);
4937 __ Bc1t(0, label);
4938 break;
4939 case kCondNE:
4940 __ CeqS(0, lhs, rhs);
4941 __ Bc1f(0, label);
4942 break;
4943 case kCondLT:
4944 if (gt_bias) {
4945 __ ColtS(0, lhs, rhs);
4946 } else {
4947 __ CultS(0, lhs, rhs);
4948 }
4949 __ Bc1t(0, label);
4950 break;
4951 case kCondLE:
4952 if (gt_bias) {
4953 __ ColeS(0, lhs, rhs);
4954 } else {
4955 __ CuleS(0, lhs, rhs);
4956 }
4957 __ Bc1t(0, label);
4958 break;
4959 case kCondGT:
4960 if (gt_bias) {
4961 __ CultS(0, rhs, lhs);
4962 } else {
4963 __ ColtS(0, rhs, lhs);
4964 }
4965 __ Bc1t(0, label);
4966 break;
4967 case kCondGE:
4968 if (gt_bias) {
4969 __ CuleS(0, rhs, lhs);
4970 } else {
4971 __ ColeS(0, rhs, lhs);
4972 }
4973 __ Bc1t(0, label);
4974 break;
4975 default:
4976 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004977 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004978 }
4979 }
4980 } else {
4981 DCHECK_EQ(type, Primitive::kPrimDouble);
4982 if (isR6) {
4983 switch (cond) {
4984 case kCondEQ:
4985 __ CmpEqD(FTMP, lhs, rhs);
4986 __ Bc1nez(FTMP, label);
4987 break;
4988 case kCondNE:
4989 __ CmpEqD(FTMP, lhs, rhs);
4990 __ Bc1eqz(FTMP, label);
4991 break;
4992 case kCondLT:
4993 if (gt_bias) {
4994 __ CmpLtD(FTMP, lhs, rhs);
4995 } else {
4996 __ CmpUltD(FTMP, lhs, rhs);
4997 }
4998 __ Bc1nez(FTMP, label);
4999 break;
5000 case kCondLE:
5001 if (gt_bias) {
5002 __ CmpLeD(FTMP, lhs, rhs);
5003 } else {
5004 __ CmpUleD(FTMP, lhs, rhs);
5005 }
5006 __ Bc1nez(FTMP, label);
5007 break;
5008 case kCondGT:
5009 if (gt_bias) {
5010 __ CmpUltD(FTMP, rhs, lhs);
5011 } else {
5012 __ CmpLtD(FTMP, rhs, lhs);
5013 }
5014 __ Bc1nez(FTMP, label);
5015 break;
5016 case kCondGE:
5017 if (gt_bias) {
5018 __ CmpUleD(FTMP, rhs, lhs);
5019 } else {
5020 __ CmpLeD(FTMP, rhs, lhs);
5021 }
5022 __ Bc1nez(FTMP, label);
5023 break;
5024 default:
5025 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005026 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005027 }
5028 } else {
5029 switch (cond) {
5030 case kCondEQ:
5031 __ CeqD(0, lhs, rhs);
5032 __ Bc1t(0, label);
5033 break;
5034 case kCondNE:
5035 __ CeqD(0, lhs, rhs);
5036 __ Bc1f(0, label);
5037 break;
5038 case kCondLT:
5039 if (gt_bias) {
5040 __ ColtD(0, lhs, rhs);
5041 } else {
5042 __ CultD(0, lhs, rhs);
5043 }
5044 __ Bc1t(0, label);
5045 break;
5046 case kCondLE:
5047 if (gt_bias) {
5048 __ ColeD(0, lhs, rhs);
5049 } else {
5050 __ CuleD(0, lhs, rhs);
5051 }
5052 __ Bc1t(0, label);
5053 break;
5054 case kCondGT:
5055 if (gt_bias) {
5056 __ CultD(0, rhs, lhs);
5057 } else {
5058 __ ColtD(0, rhs, lhs);
5059 }
5060 __ Bc1t(0, label);
5061 break;
5062 case kCondGE:
5063 if (gt_bias) {
5064 __ CuleD(0, rhs, lhs);
5065 } else {
5066 __ ColeD(0, rhs, lhs);
5067 }
5068 __ Bc1t(0, label);
5069 break;
5070 default:
5071 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005072 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005073 }
5074 }
5075 }
5076}
5077
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005078void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005079 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005080 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005081 MipsLabel* false_target) {
5082 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005083
David Brazdil0debae72015-11-12 18:37:00 +00005084 if (true_target == nullptr && false_target == nullptr) {
5085 // Nothing to do. The code always falls through.
5086 return;
5087 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005088 // Constant condition, statically compared against "true" (integer value 1).
5089 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005090 if (true_target != nullptr) {
5091 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005093 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005094 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005095 if (false_target != nullptr) {
5096 __ B(false_target);
5097 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005098 }
David Brazdil0debae72015-11-12 18:37:00 +00005099 return;
5100 }
5101
5102 // The following code generates these patterns:
5103 // (1) true_target == nullptr && false_target != nullptr
5104 // - opposite condition true => branch to false_target
5105 // (2) true_target != nullptr && false_target == nullptr
5106 // - condition true => branch to true_target
5107 // (3) true_target != nullptr && false_target != nullptr
5108 // - condition true => branch to true_target
5109 // - branch to false_target
5110 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005111 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005112 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005113 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005114 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005115 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5116 } else {
5117 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005119 } else {
5120 // The condition instruction has not been materialized, use its inputs as
5121 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005122 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005123 Primitive::Type type = condition->InputAt(0)->GetType();
5124 LocationSummary* locations = cond->GetLocations();
5125 IfCondition if_cond = condition->GetCondition();
5126 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005127
David Brazdil0debae72015-11-12 18:37:00 +00005128 if (true_target == nullptr) {
5129 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005130 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005131 }
5132
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005133 switch (type) {
5134 default:
5135 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5136 break;
5137 case Primitive::kPrimLong:
5138 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5139 break;
5140 case Primitive::kPrimFloat:
5141 case Primitive::kPrimDouble:
5142 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5143 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144 }
5145 }
David Brazdil0debae72015-11-12 18:37:00 +00005146
5147 // If neither branch falls through (case 3), the conditional branch to `true_target`
5148 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5149 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005150 __ B(false_target);
5151 }
5152}
5153
5154void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5155 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005156 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005157 locations->SetInAt(0, Location::RequiresRegister());
5158 }
5159}
5160
5161void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005162 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5163 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5164 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5165 nullptr : codegen_->GetLabelOf(true_successor);
5166 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5167 nullptr : codegen_->GetLabelOf(false_successor);
5168 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005169}
5170
5171void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5172 LocationSummary* locations = new (GetGraph()->GetArena())
5173 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005174 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00005175 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005176 locations->SetInAt(0, Location::RequiresRegister());
5177 }
5178}
5179
5180void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005181 SlowPathCodeMIPS* slow_path =
5182 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005183 GenerateTestAndBranch(deoptimize,
5184 /* condition_input_index */ 0,
5185 slow_path->GetEntryLabel(),
5186 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005187}
5188
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005189// This function returns true if a conditional move can be generated for HSelect.
5190// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5191// branches and regular moves.
5192//
5193// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5194//
5195// While determining feasibility of a conditional move and setting inputs/outputs
5196// are two distinct tasks, this function does both because they share quite a bit
5197// of common logic.
5198static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5199 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5200 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5201 HCondition* condition = cond->AsCondition();
5202
5203 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5204 Primitive::Type dst_type = select->GetType();
5205
5206 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5207 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5208 bool is_true_value_zero_constant =
5209 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5210 bool is_false_value_zero_constant =
5211 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5212
5213 bool can_move_conditionally = false;
5214 bool use_const_for_false_in = false;
5215 bool use_const_for_true_in = false;
5216
5217 if (!cond->IsConstant()) {
5218 switch (cond_type) {
5219 default:
5220 switch (dst_type) {
5221 default:
5222 // Moving int on int condition.
5223 if (is_r6) {
5224 if (is_true_value_zero_constant) {
5225 // seleqz out_reg, false_reg, cond_reg
5226 can_move_conditionally = true;
5227 use_const_for_true_in = true;
5228 } else if (is_false_value_zero_constant) {
5229 // selnez out_reg, true_reg, cond_reg
5230 can_move_conditionally = true;
5231 use_const_for_false_in = true;
5232 } else if (materialized) {
5233 // Not materializing unmaterialized int conditions
5234 // to keep the instruction count low.
5235 // selnez AT, true_reg, cond_reg
5236 // seleqz TMP, false_reg, cond_reg
5237 // or out_reg, AT, TMP
5238 can_move_conditionally = true;
5239 }
5240 } else {
5241 // movn out_reg, true_reg/ZERO, cond_reg
5242 can_move_conditionally = true;
5243 use_const_for_true_in = is_true_value_zero_constant;
5244 }
5245 break;
5246 case Primitive::kPrimLong:
5247 // Moving long on int condition.
5248 if (is_r6) {
5249 if (is_true_value_zero_constant) {
5250 // seleqz out_reg_lo, false_reg_lo, cond_reg
5251 // seleqz out_reg_hi, false_reg_hi, cond_reg
5252 can_move_conditionally = true;
5253 use_const_for_true_in = true;
5254 } else if (is_false_value_zero_constant) {
5255 // selnez out_reg_lo, true_reg_lo, cond_reg
5256 // selnez out_reg_hi, true_reg_hi, cond_reg
5257 can_move_conditionally = true;
5258 use_const_for_false_in = true;
5259 }
5260 // Other long conditional moves would generate 6+ instructions,
5261 // which is too many.
5262 } else {
5263 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5264 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5265 can_move_conditionally = true;
5266 use_const_for_true_in = is_true_value_zero_constant;
5267 }
5268 break;
5269 case Primitive::kPrimFloat:
5270 case Primitive::kPrimDouble:
5271 // Moving float/double on int condition.
5272 if (is_r6) {
5273 if (materialized) {
5274 // Not materializing unmaterialized int conditions
5275 // to keep the instruction count low.
5276 can_move_conditionally = true;
5277 if (is_true_value_zero_constant) {
5278 // sltu TMP, ZERO, cond_reg
5279 // mtc1 TMP, temp_cond_reg
5280 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5281 use_const_for_true_in = true;
5282 } else if (is_false_value_zero_constant) {
5283 // sltu TMP, ZERO, cond_reg
5284 // mtc1 TMP, temp_cond_reg
5285 // selnez.fmt out_reg, true_reg, temp_cond_reg
5286 use_const_for_false_in = true;
5287 } else {
5288 // sltu TMP, ZERO, cond_reg
5289 // mtc1 TMP, temp_cond_reg
5290 // sel.fmt temp_cond_reg, false_reg, true_reg
5291 // mov.fmt out_reg, temp_cond_reg
5292 }
5293 }
5294 } else {
5295 // movn.fmt out_reg, true_reg, cond_reg
5296 can_move_conditionally = true;
5297 }
5298 break;
5299 }
5300 break;
5301 case Primitive::kPrimLong:
5302 // We don't materialize long comparison now
5303 // and use conditional branches instead.
5304 break;
5305 case Primitive::kPrimFloat:
5306 case Primitive::kPrimDouble:
5307 switch (dst_type) {
5308 default:
5309 // Moving int on float/double condition.
5310 if (is_r6) {
5311 if (is_true_value_zero_constant) {
5312 // mfc1 TMP, temp_cond_reg
5313 // seleqz out_reg, false_reg, TMP
5314 can_move_conditionally = true;
5315 use_const_for_true_in = true;
5316 } else if (is_false_value_zero_constant) {
5317 // mfc1 TMP, temp_cond_reg
5318 // selnez out_reg, true_reg, TMP
5319 can_move_conditionally = true;
5320 use_const_for_false_in = true;
5321 } else {
5322 // mfc1 TMP, temp_cond_reg
5323 // selnez AT, true_reg, TMP
5324 // seleqz TMP, false_reg, TMP
5325 // or out_reg, AT, TMP
5326 can_move_conditionally = true;
5327 }
5328 } else {
5329 // movt out_reg, true_reg/ZERO, cc
5330 can_move_conditionally = true;
5331 use_const_for_true_in = is_true_value_zero_constant;
5332 }
5333 break;
5334 case Primitive::kPrimLong:
5335 // Moving long on float/double condition.
5336 if (is_r6) {
5337 if (is_true_value_zero_constant) {
5338 // mfc1 TMP, temp_cond_reg
5339 // seleqz out_reg_lo, false_reg_lo, TMP
5340 // seleqz out_reg_hi, false_reg_hi, TMP
5341 can_move_conditionally = true;
5342 use_const_for_true_in = true;
5343 } else if (is_false_value_zero_constant) {
5344 // mfc1 TMP, temp_cond_reg
5345 // selnez out_reg_lo, true_reg_lo, TMP
5346 // selnez out_reg_hi, true_reg_hi, TMP
5347 can_move_conditionally = true;
5348 use_const_for_false_in = true;
5349 }
5350 // Other long conditional moves would generate 6+ instructions,
5351 // which is too many.
5352 } else {
5353 // movt out_reg_lo, true_reg_lo/ZERO, cc
5354 // movt out_reg_hi, true_reg_hi/ZERO, cc
5355 can_move_conditionally = true;
5356 use_const_for_true_in = is_true_value_zero_constant;
5357 }
5358 break;
5359 case Primitive::kPrimFloat:
5360 case Primitive::kPrimDouble:
5361 // Moving float/double on float/double condition.
5362 if (is_r6) {
5363 can_move_conditionally = true;
5364 if (is_true_value_zero_constant) {
5365 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5366 use_const_for_true_in = true;
5367 } else if (is_false_value_zero_constant) {
5368 // selnez.fmt out_reg, true_reg, temp_cond_reg
5369 use_const_for_false_in = true;
5370 } else {
5371 // sel.fmt temp_cond_reg, false_reg, true_reg
5372 // mov.fmt out_reg, temp_cond_reg
5373 }
5374 } else {
5375 // movt.fmt out_reg, true_reg, cc
5376 can_move_conditionally = true;
5377 }
5378 break;
5379 }
5380 break;
5381 }
5382 }
5383
5384 if (can_move_conditionally) {
5385 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5386 } else {
5387 DCHECK(!use_const_for_false_in);
5388 DCHECK(!use_const_for_true_in);
5389 }
5390
5391 if (locations_to_set != nullptr) {
5392 if (use_const_for_false_in) {
5393 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5394 } else {
5395 locations_to_set->SetInAt(0,
5396 Primitive::IsFloatingPointType(dst_type)
5397 ? Location::RequiresFpuRegister()
5398 : Location::RequiresRegister());
5399 }
5400 if (use_const_for_true_in) {
5401 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5402 } else {
5403 locations_to_set->SetInAt(1,
5404 Primitive::IsFloatingPointType(dst_type)
5405 ? Location::RequiresFpuRegister()
5406 : Location::RequiresRegister());
5407 }
5408 if (materialized) {
5409 locations_to_set->SetInAt(2, Location::RequiresRegister());
5410 }
5411 // On R6 we don't require the output to be the same as the
5412 // first input for conditional moves unlike on R2.
5413 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5414 if (is_out_same_as_first_in) {
5415 locations_to_set->SetOut(Location::SameAsFirstInput());
5416 } else {
5417 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5418 ? Location::RequiresFpuRegister()
5419 : Location::RequiresRegister());
5420 }
5421 }
5422
5423 return can_move_conditionally;
5424}
5425
5426void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5427 LocationSummary* locations = select->GetLocations();
5428 Location dst = locations->Out();
5429 Location src = locations->InAt(1);
5430 Register src_reg = ZERO;
5431 Register src_reg_high = ZERO;
5432 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5433 Register cond_reg = TMP;
5434 int cond_cc = 0;
5435 Primitive::Type cond_type = Primitive::kPrimInt;
5436 bool cond_inverted = false;
5437 Primitive::Type dst_type = select->GetType();
5438
5439 if (IsBooleanValueOrMaterializedCondition(cond)) {
5440 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5441 } else {
5442 HCondition* condition = cond->AsCondition();
5443 LocationSummary* cond_locations = cond->GetLocations();
5444 IfCondition if_cond = condition->GetCondition();
5445 cond_type = condition->InputAt(0)->GetType();
5446 switch (cond_type) {
5447 default:
5448 DCHECK_NE(cond_type, Primitive::kPrimLong);
5449 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5450 break;
5451 case Primitive::kPrimFloat:
5452 case Primitive::kPrimDouble:
5453 cond_inverted = MaterializeFpCompareR2(if_cond,
5454 condition->IsGtBias(),
5455 cond_type,
5456 cond_locations,
5457 cond_cc);
5458 break;
5459 }
5460 }
5461
5462 DCHECK(dst.Equals(locations->InAt(0)));
5463 if (src.IsRegister()) {
5464 src_reg = src.AsRegister<Register>();
5465 } else if (src.IsRegisterPair()) {
5466 src_reg = src.AsRegisterPairLow<Register>();
5467 src_reg_high = src.AsRegisterPairHigh<Register>();
5468 } else if (src.IsConstant()) {
5469 DCHECK(src.GetConstant()->IsZeroBitPattern());
5470 }
5471
5472 switch (cond_type) {
5473 default:
5474 switch (dst_type) {
5475 default:
5476 if (cond_inverted) {
5477 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5478 } else {
5479 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5480 }
5481 break;
5482 case Primitive::kPrimLong:
5483 if (cond_inverted) {
5484 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5485 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5486 } else {
5487 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5488 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5489 }
5490 break;
5491 case Primitive::kPrimFloat:
5492 if (cond_inverted) {
5493 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5494 } else {
5495 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5496 }
5497 break;
5498 case Primitive::kPrimDouble:
5499 if (cond_inverted) {
5500 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5501 } else {
5502 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5503 }
5504 break;
5505 }
5506 break;
5507 case Primitive::kPrimLong:
5508 LOG(FATAL) << "Unreachable";
5509 UNREACHABLE();
5510 case Primitive::kPrimFloat:
5511 case Primitive::kPrimDouble:
5512 switch (dst_type) {
5513 default:
5514 if (cond_inverted) {
5515 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5516 } else {
5517 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5518 }
5519 break;
5520 case Primitive::kPrimLong:
5521 if (cond_inverted) {
5522 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5523 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5524 } else {
5525 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5526 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5527 }
5528 break;
5529 case Primitive::kPrimFloat:
5530 if (cond_inverted) {
5531 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5532 } else {
5533 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5534 }
5535 break;
5536 case Primitive::kPrimDouble:
5537 if (cond_inverted) {
5538 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5539 } else {
5540 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5541 }
5542 break;
5543 }
5544 break;
5545 }
5546}
5547
5548void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5549 LocationSummary* locations = select->GetLocations();
5550 Location dst = locations->Out();
5551 Location false_src = locations->InAt(0);
5552 Location true_src = locations->InAt(1);
5553 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5554 Register cond_reg = TMP;
5555 FRegister fcond_reg = FTMP;
5556 Primitive::Type cond_type = Primitive::kPrimInt;
5557 bool cond_inverted = false;
5558 Primitive::Type dst_type = select->GetType();
5559
5560 if (IsBooleanValueOrMaterializedCondition(cond)) {
5561 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5562 } else {
5563 HCondition* condition = cond->AsCondition();
5564 LocationSummary* cond_locations = cond->GetLocations();
5565 IfCondition if_cond = condition->GetCondition();
5566 cond_type = condition->InputAt(0)->GetType();
5567 switch (cond_type) {
5568 default:
5569 DCHECK_NE(cond_type, Primitive::kPrimLong);
5570 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5571 break;
5572 case Primitive::kPrimFloat:
5573 case Primitive::kPrimDouble:
5574 cond_inverted = MaterializeFpCompareR6(if_cond,
5575 condition->IsGtBias(),
5576 cond_type,
5577 cond_locations,
5578 fcond_reg);
5579 break;
5580 }
5581 }
5582
5583 if (true_src.IsConstant()) {
5584 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5585 }
5586 if (false_src.IsConstant()) {
5587 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5588 }
5589
5590 switch (dst_type) {
5591 default:
5592 if (Primitive::IsFloatingPointType(cond_type)) {
5593 __ Mfc1(cond_reg, fcond_reg);
5594 }
5595 if (true_src.IsConstant()) {
5596 if (cond_inverted) {
5597 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5598 } else {
5599 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5600 }
5601 } else if (false_src.IsConstant()) {
5602 if (cond_inverted) {
5603 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5604 } else {
5605 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5606 }
5607 } else {
5608 DCHECK_NE(cond_reg, AT);
5609 if (cond_inverted) {
5610 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5611 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5612 } else {
5613 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5614 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5615 }
5616 __ Or(dst.AsRegister<Register>(), AT, TMP);
5617 }
5618 break;
5619 case Primitive::kPrimLong: {
5620 if (Primitive::IsFloatingPointType(cond_type)) {
5621 __ Mfc1(cond_reg, fcond_reg);
5622 }
5623 Register dst_lo = dst.AsRegisterPairLow<Register>();
5624 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5625 if (true_src.IsConstant()) {
5626 Register src_lo = false_src.AsRegisterPairLow<Register>();
5627 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5628 if (cond_inverted) {
5629 __ Selnez(dst_lo, src_lo, cond_reg);
5630 __ Selnez(dst_hi, src_hi, cond_reg);
5631 } else {
5632 __ Seleqz(dst_lo, src_lo, cond_reg);
5633 __ Seleqz(dst_hi, src_hi, cond_reg);
5634 }
5635 } else {
5636 DCHECK(false_src.IsConstant());
5637 Register src_lo = true_src.AsRegisterPairLow<Register>();
5638 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5639 if (cond_inverted) {
5640 __ Seleqz(dst_lo, src_lo, cond_reg);
5641 __ Seleqz(dst_hi, src_hi, cond_reg);
5642 } else {
5643 __ Selnez(dst_lo, src_lo, cond_reg);
5644 __ Selnez(dst_hi, src_hi, cond_reg);
5645 }
5646 }
5647 break;
5648 }
5649 case Primitive::kPrimFloat: {
5650 if (!Primitive::IsFloatingPointType(cond_type)) {
5651 // sel*.fmt tests bit 0 of the condition register, account for that.
5652 __ Sltu(TMP, ZERO, cond_reg);
5653 __ Mtc1(TMP, fcond_reg);
5654 }
5655 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5656 if (true_src.IsConstant()) {
5657 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5658 if (cond_inverted) {
5659 __ SelnezS(dst_reg, src_reg, fcond_reg);
5660 } else {
5661 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5662 }
5663 } else if (false_src.IsConstant()) {
5664 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5665 if (cond_inverted) {
5666 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5667 } else {
5668 __ SelnezS(dst_reg, src_reg, fcond_reg);
5669 }
5670 } else {
5671 if (cond_inverted) {
5672 __ SelS(fcond_reg,
5673 true_src.AsFpuRegister<FRegister>(),
5674 false_src.AsFpuRegister<FRegister>());
5675 } else {
5676 __ SelS(fcond_reg,
5677 false_src.AsFpuRegister<FRegister>(),
5678 true_src.AsFpuRegister<FRegister>());
5679 }
5680 __ MovS(dst_reg, fcond_reg);
5681 }
5682 break;
5683 }
5684 case Primitive::kPrimDouble: {
5685 if (!Primitive::IsFloatingPointType(cond_type)) {
5686 // sel*.fmt tests bit 0 of the condition register, account for that.
5687 __ Sltu(TMP, ZERO, cond_reg);
5688 __ Mtc1(TMP, fcond_reg);
5689 }
5690 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5691 if (true_src.IsConstant()) {
5692 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5693 if (cond_inverted) {
5694 __ SelnezD(dst_reg, src_reg, fcond_reg);
5695 } else {
5696 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5697 }
5698 } else if (false_src.IsConstant()) {
5699 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5700 if (cond_inverted) {
5701 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5702 } else {
5703 __ SelnezD(dst_reg, src_reg, fcond_reg);
5704 }
5705 } else {
5706 if (cond_inverted) {
5707 __ SelD(fcond_reg,
5708 true_src.AsFpuRegister<FRegister>(),
5709 false_src.AsFpuRegister<FRegister>());
5710 } else {
5711 __ SelD(fcond_reg,
5712 false_src.AsFpuRegister<FRegister>(),
5713 true_src.AsFpuRegister<FRegister>());
5714 }
5715 __ MovD(dst_reg, fcond_reg);
5716 }
5717 break;
5718 }
5719 }
5720}
5721
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005722void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5723 LocationSummary* locations = new (GetGraph()->GetArena())
5724 LocationSummary(flag, LocationSummary::kNoCall);
5725 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005726}
5727
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005728void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5729 __ LoadFromOffset(kLoadWord,
5730 flag->GetLocations()->Out().AsRegister<Register>(),
5731 SP,
5732 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005733}
5734
David Brazdil74eb1b22015-12-14 11:44:01 +00005735void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5736 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005737 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005738}
5739
5740void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005741 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5742 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5743 if (is_r6) {
5744 GenConditionalMoveR6(select);
5745 } else {
5746 GenConditionalMoveR2(select);
5747 }
5748 } else {
5749 LocationSummary* locations = select->GetLocations();
5750 MipsLabel false_target;
5751 GenerateTestAndBranch(select,
5752 /* condition_input_index */ 2,
5753 /* true_target */ nullptr,
5754 &false_target);
5755 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5756 __ Bind(&false_target);
5757 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005758}
5759
David Srbecky0cf44932015-12-09 14:09:59 +00005760void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5761 new (GetGraph()->GetArena()) LocationSummary(info);
5762}
5763
David Srbeckyd28f4a02016-03-14 17:14:24 +00005764void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5765 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005766}
5767
5768void CodeGeneratorMIPS::GenerateNop() {
5769 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005770}
5771
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005772void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5773 Primitive::Type field_type = field_info.GetFieldType();
5774 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5775 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08005776 bool object_field_get_with_read_barrier =
5777 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08005779 instruction,
5780 generate_volatile
5781 ? LocationSummary::kCallOnMainOnly
5782 : (object_field_get_with_read_barrier
5783 ? LocationSummary::kCallOnSlowPath
5784 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005785
5786 locations->SetInAt(0, Location::RequiresRegister());
5787 if (generate_volatile) {
5788 InvokeRuntimeCallingConvention calling_convention;
5789 // need A0 to hold base + offset
5790 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5791 if (field_type == Primitive::kPrimLong) {
5792 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
5793 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005794 // Use Location::Any() to prevent situations when running out of available fp registers.
5795 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005796 // Need some temp core regs since FP results are returned in core registers
5797 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
5798 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
5799 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
5800 }
5801 } else {
5802 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5803 locations->SetOut(Location::RequiresFpuRegister());
5804 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005805 // The output overlaps in the case of an object field get with
5806 // read barriers enabled: we do not want the move to overwrite the
5807 // object's location, as we need it to emit the read barrier.
5808 locations->SetOut(Location::RequiresRegister(),
5809 object_field_get_with_read_barrier
5810 ? Location::kOutputOverlap
5811 : Location::kNoOutputOverlap);
5812 }
5813 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5814 // We need a temporary register for the read barrier marking slow
5815 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
5816 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005817 }
5818 }
5819}
5820
5821void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
5822 const FieldInfo& field_info,
5823 uint32_t dex_pc) {
5824 Primitive::Type type = field_info.GetFieldType();
5825 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005826 Location obj_loc = locations->InAt(0);
5827 Register obj = obj_loc.AsRegister<Register>();
5828 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005829 LoadOperandType load_type = kLoadUnsignedByte;
5830 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005831 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005832 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005833
5834 switch (type) {
5835 case Primitive::kPrimBoolean:
5836 load_type = kLoadUnsignedByte;
5837 break;
5838 case Primitive::kPrimByte:
5839 load_type = kLoadSignedByte;
5840 break;
5841 case Primitive::kPrimShort:
5842 load_type = kLoadSignedHalfword;
5843 break;
5844 case Primitive::kPrimChar:
5845 load_type = kLoadUnsignedHalfword;
5846 break;
5847 case Primitive::kPrimInt:
5848 case Primitive::kPrimFloat:
5849 case Primitive::kPrimNot:
5850 load_type = kLoadWord;
5851 break;
5852 case Primitive::kPrimLong:
5853 case Primitive::kPrimDouble:
5854 load_type = kLoadDoubleword;
5855 break;
5856 case Primitive::kPrimVoid:
5857 LOG(FATAL) << "Unreachable type " << type;
5858 UNREACHABLE();
5859 }
5860
5861 if (is_volatile && load_type == kLoadDoubleword) {
5862 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005863 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005864 // Do implicit Null check
5865 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5866 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01005867 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005868 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
5869 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005870 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08005871 if (dst_loc.IsFpuRegister()) {
5872 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005873 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08005874 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005875 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005876 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005877 __ StoreToOffset(kStoreWord,
5878 locations->GetTemp(1).AsRegister<Register>(),
5879 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005880 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005881 __ StoreToOffset(kStoreWord,
5882 locations->GetTemp(2).AsRegister<Register>(),
5883 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005884 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005885 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005886 }
5887 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005888 if (type == Primitive::kPrimNot) {
5889 // /* HeapReference<Object> */ dst = *(obj + offset)
5890 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5891 Location temp_loc = locations->GetTemp(0);
5892 // Note that a potential implicit null check is handled in this
5893 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
5894 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5895 dst_loc,
5896 obj,
5897 offset,
5898 temp_loc,
5899 /* needs_null_check */ true);
5900 if (is_volatile) {
5901 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5902 }
5903 } else {
5904 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
5905 if (is_volatile) {
5906 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5907 }
5908 // If read barriers are enabled, emit read barriers other than
5909 // Baker's using a slow path (and also unpoison the loaded
5910 // reference, if heap poisoning is enabled).
5911 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
5912 }
5913 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005914 Register dst;
5915 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08005916 DCHECK(dst_loc.IsRegisterPair());
5917 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005918 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005919 DCHECK(dst_loc.IsRegister());
5920 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005921 }
Alexey Frunze2923db72016-08-20 01:55:47 -07005922 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005923 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005924 DCHECK(dst_loc.IsFpuRegister());
5925 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005926 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005927 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005928 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005929 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005930 }
5931 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005932 }
5933
Alexey Frunze15958152017-02-09 19:08:30 -08005934 // Memory barriers, in the case of references, are handled in the
5935 // previous switch statement.
5936 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005937 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5938 }
5939}
5940
5941void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5942 Primitive::Type field_type = field_info.GetFieldType();
5943 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5944 bool generate_volatile = field_info.IsVolatile() && is_wide;
5945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005946 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005947
5948 locations->SetInAt(0, Location::RequiresRegister());
5949 if (generate_volatile) {
5950 InvokeRuntimeCallingConvention calling_convention;
5951 // need A0 to hold base + offset
5952 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5953 if (field_type == Primitive::kPrimLong) {
5954 locations->SetInAt(1, Location::RegisterPairLocation(
5955 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5956 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005957 // Use Location::Any() to prevent situations when running out of available fp registers.
5958 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005959 // Pass FP parameters in core registers.
5960 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5961 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
5962 }
5963 } else {
5964 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005965 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005966 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005967 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005968 }
5969 }
5970}
5971
5972void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
5973 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01005974 uint32_t dex_pc,
5975 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005976 Primitive::Type type = field_info.GetFieldType();
5977 LocationSummary* locations = instruction->GetLocations();
5978 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07005979 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005980 StoreOperandType store_type = kStoreByte;
5981 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005982 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08005983 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005984 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005985
5986 switch (type) {
5987 case Primitive::kPrimBoolean:
5988 case Primitive::kPrimByte:
5989 store_type = kStoreByte;
5990 break;
5991 case Primitive::kPrimShort:
5992 case Primitive::kPrimChar:
5993 store_type = kStoreHalfword;
5994 break;
5995 case Primitive::kPrimInt:
5996 case Primitive::kPrimFloat:
5997 case Primitive::kPrimNot:
5998 store_type = kStoreWord;
5999 break;
6000 case Primitive::kPrimLong:
6001 case Primitive::kPrimDouble:
6002 store_type = kStoreDoubleword;
6003 break;
6004 case Primitive::kPrimVoid:
6005 LOG(FATAL) << "Unreachable type " << type;
6006 UNREACHABLE();
6007 }
6008
6009 if (is_volatile) {
6010 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6011 }
6012
6013 if (is_volatile && store_type == kStoreDoubleword) {
6014 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006015 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006016 // Do implicit Null check.
6017 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6018 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6019 if (type == Primitive::kPrimDouble) {
6020 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006021 if (value_location.IsFpuRegister()) {
6022 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6023 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006024 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006025 value_location.AsFpuRegister<FRegister>());
6026 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006027 __ LoadFromOffset(kLoadWord,
6028 locations->GetTemp(1).AsRegister<Register>(),
6029 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006030 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006031 __ LoadFromOffset(kLoadWord,
6032 locations->GetTemp(2).AsRegister<Register>(),
6033 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006034 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006035 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006036 DCHECK(value_location.IsConstant());
6037 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6038 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006039 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6040 locations->GetTemp(1).AsRegister<Register>(),
6041 value);
6042 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006043 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006044 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006045 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6046 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006047 if (value_location.IsConstant()) {
6048 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6049 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6050 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006051 Register src;
6052 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006053 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006054 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006055 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006056 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006057 if (kPoisonHeapReferences && needs_write_barrier) {
6058 // Note that in the case where `value` is a null reference,
6059 // we do not enter this block, as a null reference does not
6060 // need poisoning.
6061 DCHECK_EQ(type, Primitive::kPrimNot);
6062 __ PoisonHeapReference(TMP, src);
6063 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6064 } else {
6065 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6066 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006067 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006068 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006069 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006070 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006071 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006072 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006073 }
6074 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006075 }
6076
Alexey Frunzec061de12017-02-14 13:27:23 -08006077 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006078 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006079 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006080 }
6081
6082 if (is_volatile) {
6083 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6084 }
6085}
6086
6087void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6088 HandleFieldGet(instruction, instruction->GetFieldInfo());
6089}
6090
6091void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6092 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6093}
6094
6095void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6096 HandleFieldSet(instruction, instruction->GetFieldInfo());
6097}
6098
6099void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006100 HandleFieldSet(instruction,
6101 instruction->GetFieldInfo(),
6102 instruction->GetDexPc(),
6103 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006104}
6105
Alexey Frunze15958152017-02-09 19:08:30 -08006106void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6107 HInstruction* instruction,
6108 Location out,
6109 uint32_t offset,
6110 Location maybe_temp,
6111 ReadBarrierOption read_barrier_option) {
6112 Register out_reg = out.AsRegister<Register>();
6113 if (read_barrier_option == kWithReadBarrier) {
6114 CHECK(kEmitCompilerReadBarrier);
6115 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6116 if (kUseBakerReadBarrier) {
6117 // Load with fast path based Baker's read barrier.
6118 // /* HeapReference<Object> */ out = *(out + offset)
6119 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6120 out,
6121 out_reg,
6122 offset,
6123 maybe_temp,
6124 /* needs_null_check */ false);
6125 } else {
6126 // Load with slow path based read barrier.
6127 // Save the value of `out` into `maybe_temp` before overwriting it
6128 // in the following move operation, as we will need it for the
6129 // read barrier below.
6130 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6131 // /* HeapReference<Object> */ out = *(out + offset)
6132 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6133 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6134 }
6135 } else {
6136 // Plain load with no read barrier.
6137 // /* HeapReference<Object> */ out = *(out + offset)
6138 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6139 __ MaybeUnpoisonHeapReference(out_reg);
6140 }
6141}
6142
6143void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6144 HInstruction* instruction,
6145 Location out,
6146 Location obj,
6147 uint32_t offset,
6148 Location maybe_temp,
6149 ReadBarrierOption read_barrier_option) {
6150 Register out_reg = out.AsRegister<Register>();
6151 Register obj_reg = obj.AsRegister<Register>();
6152 if (read_barrier_option == kWithReadBarrier) {
6153 CHECK(kEmitCompilerReadBarrier);
6154 if (kUseBakerReadBarrier) {
6155 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6156 // Load with fast path based Baker's read barrier.
6157 // /* HeapReference<Object> */ out = *(obj + offset)
6158 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6159 out,
6160 obj_reg,
6161 offset,
6162 maybe_temp,
6163 /* needs_null_check */ false);
6164 } else {
6165 // Load with slow path based read barrier.
6166 // /* HeapReference<Object> */ out = *(obj + offset)
6167 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6168 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6169 }
6170 } else {
6171 // Plain load with no read barrier.
6172 // /* HeapReference<Object> */ out = *(obj + offset)
6173 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6174 __ MaybeUnpoisonHeapReference(out_reg);
6175 }
6176}
6177
6178void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6179 Location root,
6180 Register obj,
6181 uint32_t offset,
6182 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006183 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006184 if (read_barrier_option == kWithReadBarrier) {
6185 DCHECK(kEmitCompilerReadBarrier);
6186 if (kUseBakerReadBarrier) {
6187 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6188 // Baker's read barrier are used:
6189 //
6190 // root = obj.field;
6191 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6192 // if (temp != null) {
6193 // root = temp(root)
6194 // }
6195
6196 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6197 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6198 static_assert(
6199 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6200 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6201 "have different sizes.");
6202 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6203 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6204 "have different sizes.");
6205
6206 // Slow path marking the GC root `root`.
6207 Location temp = Location::RegisterLocation(T9);
6208 SlowPathCodeMIPS* slow_path =
6209 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6210 instruction,
6211 root,
6212 /*entrypoint*/ temp);
6213 codegen_->AddSlowPath(slow_path);
6214
6215 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6216 const int32_t entry_point_offset =
6217 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6218 // Loading the entrypoint does not require a load acquire since it is only changed when
6219 // threads are suspended or running a checkpoint.
6220 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6221 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6222 // checking GetIsGcMarking.
6223 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6224 __ Bind(slow_path->GetExitLabel());
6225 } else {
6226 // GC root loaded through a slow path for read barriers other
6227 // than Baker's.
6228 // /* GcRoot<mirror::Object>* */ root = obj + offset
6229 __ Addiu32(root_reg, obj, offset);
6230 // /* mirror::Object* */ root = root->Read()
6231 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6232 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006233 } else {
6234 // Plain GC root load with no read barrier.
6235 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6236 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6237 // Note that GC roots are not affected by heap poisoning, thus we
6238 // do not have to unpoison `root_reg` here.
6239 }
6240}
6241
Alexey Frunze15958152017-02-09 19:08:30 -08006242void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6243 Location ref,
6244 Register obj,
6245 uint32_t offset,
6246 Location temp,
6247 bool needs_null_check) {
6248 DCHECK(kEmitCompilerReadBarrier);
6249 DCHECK(kUseBakerReadBarrier);
6250
6251 // /* HeapReference<Object> */ ref = *(obj + offset)
6252 Location no_index = Location::NoLocation();
6253 ScaleFactor no_scale_factor = TIMES_1;
6254 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6255 ref,
6256 obj,
6257 offset,
6258 no_index,
6259 no_scale_factor,
6260 temp,
6261 needs_null_check);
6262}
6263
6264void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6265 Location ref,
6266 Register obj,
6267 uint32_t data_offset,
6268 Location index,
6269 Location temp,
6270 bool needs_null_check) {
6271 DCHECK(kEmitCompilerReadBarrier);
6272 DCHECK(kUseBakerReadBarrier);
6273
6274 static_assert(
6275 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6276 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6277 // /* HeapReference<Object> */ ref =
6278 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6279 ScaleFactor scale_factor = TIMES_4;
6280 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6281 ref,
6282 obj,
6283 data_offset,
6284 index,
6285 scale_factor,
6286 temp,
6287 needs_null_check);
6288}
6289
6290void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6291 Location ref,
6292 Register obj,
6293 uint32_t offset,
6294 Location index,
6295 ScaleFactor scale_factor,
6296 Location temp,
6297 bool needs_null_check,
6298 bool always_update_field) {
6299 DCHECK(kEmitCompilerReadBarrier);
6300 DCHECK(kUseBakerReadBarrier);
6301
6302 // In slow path based read barriers, the read barrier call is
6303 // inserted after the original load. However, in fast path based
6304 // Baker's read barriers, we need to perform the load of
6305 // mirror::Object::monitor_ *before* the original reference load.
6306 // This load-load ordering is required by the read barrier.
6307 // The fast path/slow path (for Baker's algorithm) should look like:
6308 //
6309 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6310 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6311 // HeapReference<Object> ref = *src; // Original reference load.
6312 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6313 // if (is_gray) {
6314 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6315 // }
6316 //
6317 // Note: the original implementation in ReadBarrier::Barrier is
6318 // slightly more complex as it performs additional checks that we do
6319 // not do here for performance reasons.
6320
6321 Register ref_reg = ref.AsRegister<Register>();
6322 Register temp_reg = temp.AsRegister<Register>();
6323 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6324
6325 // /* int32_t */ monitor = obj->monitor_
6326 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6327 if (needs_null_check) {
6328 MaybeRecordImplicitNullCheck(instruction);
6329 }
6330 // /* LockWord */ lock_word = LockWord(monitor)
6331 static_assert(sizeof(LockWord) == sizeof(int32_t),
6332 "art::LockWord and int32_t have different sizes.");
6333
6334 __ Sync(0); // Barrier to prevent load-load reordering.
6335
6336 // The actual reference load.
6337 if (index.IsValid()) {
6338 // Load types involving an "index": ArrayGet,
6339 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6340 // intrinsics.
6341 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6342 if (index.IsConstant()) {
6343 size_t computed_offset =
6344 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6345 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6346 } else {
6347 // Handle the special case of the
6348 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6349 // intrinsics, which use a register pair as index ("long
6350 // offset"), of which only the low part contains data.
6351 Register index_reg = index.IsRegisterPair()
6352 ? index.AsRegisterPairLow<Register>()
6353 : index.AsRegister<Register>();
6354 __ Sll(TMP, index_reg, scale_factor);
6355 __ Addu(TMP, obj, TMP);
6356 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6357 }
6358 } else {
6359 // /* HeapReference<Object> */ ref = *(obj + offset)
6360 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6361 }
6362
6363 // Object* ref = ref_addr->AsMirrorPtr()
6364 __ MaybeUnpoisonHeapReference(ref_reg);
6365
6366 // Slow path marking the object `ref` when it is gray.
6367 SlowPathCodeMIPS* slow_path;
6368 if (always_update_field) {
6369 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6370 // of the form `obj + field_offset`, where `obj` is a register and
6371 // `field_offset` is a register pair (of which only the lower half
6372 // is used). Thus `offset` and `scale_factor` above are expected
6373 // to be null in this code path.
6374 DCHECK_EQ(offset, 0u);
6375 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6376 slow_path = new (GetGraph()->GetArena())
6377 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6378 ref,
6379 obj,
6380 /* field_offset */ index,
6381 temp_reg);
6382 } else {
6383 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6384 }
6385 AddSlowPath(slow_path);
6386
6387 // if (rb_state == ReadBarrier::GrayState())
6388 // ref = ReadBarrier::Mark(ref);
6389 // Given the numeric representation, it's enough to check the low bit of the
6390 // rb_state. We do that by shifting the bit into the sign bit (31) and
6391 // performing a branch on less than zero.
6392 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6393 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6394 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6395 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6396 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6397 __ Bind(slow_path->GetExitLabel());
6398}
6399
6400void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6401 Location out,
6402 Location ref,
6403 Location obj,
6404 uint32_t offset,
6405 Location index) {
6406 DCHECK(kEmitCompilerReadBarrier);
6407
6408 // Insert a slow path based read barrier *after* the reference load.
6409 //
6410 // If heap poisoning is enabled, the unpoisoning of the loaded
6411 // reference will be carried out by the runtime within the slow
6412 // path.
6413 //
6414 // Note that `ref` currently does not get unpoisoned (when heap
6415 // poisoning is enabled), which is alright as the `ref` argument is
6416 // not used by the artReadBarrierSlow entry point.
6417 //
6418 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6419 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6420 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6421 AddSlowPath(slow_path);
6422
6423 __ B(slow_path->GetEntryLabel());
6424 __ Bind(slow_path->GetExitLabel());
6425}
6426
6427void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6428 Location out,
6429 Location ref,
6430 Location obj,
6431 uint32_t offset,
6432 Location index) {
6433 if (kEmitCompilerReadBarrier) {
6434 // Baker's read barriers shall be handled by the fast path
6435 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6436 DCHECK(!kUseBakerReadBarrier);
6437 // If heap poisoning is enabled, unpoisoning will be taken care of
6438 // by the runtime within the slow path.
6439 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6440 } else if (kPoisonHeapReferences) {
6441 __ UnpoisonHeapReference(out.AsRegister<Register>());
6442 }
6443}
6444
6445void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6446 Location out,
6447 Location root) {
6448 DCHECK(kEmitCompilerReadBarrier);
6449
6450 // Insert a slow path based read barrier *after* the GC root load.
6451 //
6452 // Note that GC roots are not affected by heap poisoning, so we do
6453 // not need to do anything special for this here.
6454 SlowPathCodeMIPS* slow_path =
6455 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6456 AddSlowPath(slow_path);
6457
6458 __ B(slow_path->GetEntryLabel());
6459 __ Bind(slow_path->GetExitLabel());
6460}
6461
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006462void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006463 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6464 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6465 switch (type_check_kind) {
6466 case TypeCheckKind::kExactCheck:
6467 case TypeCheckKind::kAbstractClassCheck:
6468 case TypeCheckKind::kClassHierarchyCheck:
6469 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006470 call_kind =
6471 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006472 break;
6473 case TypeCheckKind::kArrayCheck:
6474 case TypeCheckKind::kUnresolvedCheck:
6475 case TypeCheckKind::kInterfaceCheck:
6476 call_kind = LocationSummary::kCallOnSlowPath;
6477 break;
6478 }
6479
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006480 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6481 locations->SetInAt(0, Location::RequiresRegister());
6482 locations->SetInAt(1, Location::RequiresRegister());
6483 // The output does overlap inputs.
6484 // Note that TypeCheckSlowPathMIPS uses this register too.
6485 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006486 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006487}
6488
6489void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006490 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006491 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006492 Location obj_loc = locations->InAt(0);
6493 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006494 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006495 Location out_loc = locations->Out();
6496 Register out = out_loc.AsRegister<Register>();
6497 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6498 DCHECK_LE(num_temps, 1u);
6499 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006500 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6501 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6502 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6503 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006504 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006505 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506
6507 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006508 // Avoid this check if we know `obj` is not null.
6509 if (instruction->MustDoNullCheck()) {
6510 __ Move(out, ZERO);
6511 __ Beqz(obj, &done);
6512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006513
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006514 switch (type_check_kind) {
6515 case TypeCheckKind::kExactCheck: {
6516 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006517 GenerateReferenceLoadTwoRegisters(instruction,
6518 out_loc,
6519 obj_loc,
6520 class_offset,
6521 maybe_temp_loc,
6522 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006523 // Classes must be equal for the instanceof to succeed.
6524 __ Xor(out, out, cls);
6525 __ Sltiu(out, out, 1);
6526 break;
6527 }
6528
6529 case TypeCheckKind::kAbstractClassCheck: {
6530 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006531 GenerateReferenceLoadTwoRegisters(instruction,
6532 out_loc,
6533 obj_loc,
6534 class_offset,
6535 maybe_temp_loc,
6536 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006537 // If the class is abstract, we eagerly fetch the super class of the
6538 // object to avoid doing a comparison we know will fail.
6539 MipsLabel loop;
6540 __ Bind(&loop);
6541 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006542 GenerateReferenceLoadOneRegister(instruction,
6543 out_loc,
6544 super_offset,
6545 maybe_temp_loc,
6546 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006547 // If `out` is null, we use it for the result, and jump to `done`.
6548 __ Beqz(out, &done);
6549 __ Bne(out, cls, &loop);
6550 __ LoadConst32(out, 1);
6551 break;
6552 }
6553
6554 case TypeCheckKind::kClassHierarchyCheck: {
6555 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006556 GenerateReferenceLoadTwoRegisters(instruction,
6557 out_loc,
6558 obj_loc,
6559 class_offset,
6560 maybe_temp_loc,
6561 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006562 // Walk over the class hierarchy to find a match.
6563 MipsLabel loop, success;
6564 __ Bind(&loop);
6565 __ Beq(out, cls, &success);
6566 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006567 GenerateReferenceLoadOneRegister(instruction,
6568 out_loc,
6569 super_offset,
6570 maybe_temp_loc,
6571 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006572 __ Bnez(out, &loop);
6573 // If `out` is null, we use it for the result, and jump to `done`.
6574 __ B(&done);
6575 __ Bind(&success);
6576 __ LoadConst32(out, 1);
6577 break;
6578 }
6579
6580 case TypeCheckKind::kArrayObjectCheck: {
6581 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006582 GenerateReferenceLoadTwoRegisters(instruction,
6583 out_loc,
6584 obj_loc,
6585 class_offset,
6586 maybe_temp_loc,
6587 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006588 // Do an exact check.
6589 MipsLabel success;
6590 __ Beq(out, cls, &success);
6591 // Otherwise, we need to check that the object's class is a non-primitive array.
6592 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006593 GenerateReferenceLoadOneRegister(instruction,
6594 out_loc,
6595 component_offset,
6596 maybe_temp_loc,
6597 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006598 // If `out` is null, we use it for the result, and jump to `done`.
6599 __ Beqz(out, &done);
6600 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6601 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6602 __ Sltiu(out, out, 1);
6603 __ B(&done);
6604 __ Bind(&success);
6605 __ LoadConst32(out, 1);
6606 break;
6607 }
6608
6609 case TypeCheckKind::kArrayCheck: {
6610 // No read barrier since the slow path will retry upon failure.
6611 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006612 GenerateReferenceLoadTwoRegisters(instruction,
6613 out_loc,
6614 obj_loc,
6615 class_offset,
6616 maybe_temp_loc,
6617 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006618 DCHECK(locations->OnlyCallsOnSlowPath());
6619 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6620 /* is_fatal */ false);
6621 codegen_->AddSlowPath(slow_path);
6622 __ Bne(out, cls, slow_path->GetEntryLabel());
6623 __ LoadConst32(out, 1);
6624 break;
6625 }
6626
6627 case TypeCheckKind::kUnresolvedCheck:
6628 case TypeCheckKind::kInterfaceCheck: {
6629 // Note that we indeed only call on slow path, but we always go
6630 // into the slow path for the unresolved and interface check
6631 // cases.
6632 //
6633 // We cannot directly call the InstanceofNonTrivial runtime
6634 // entry point without resorting to a type checking slow path
6635 // here (i.e. by calling InvokeRuntime directly), as it would
6636 // require to assign fixed registers for the inputs of this
6637 // HInstanceOf instruction (following the runtime calling
6638 // convention), which might be cluttered by the potential first
6639 // read barrier emission at the beginning of this method.
6640 //
6641 // TODO: Introduce a new runtime entry point taking the object
6642 // to test (instead of its class) as argument, and let it deal
6643 // with the read barrier issues. This will let us refactor this
6644 // case of the `switch` code as it was previously (with a direct
6645 // call to the runtime not using a type checking slow path).
6646 // This should also be beneficial for the other cases above.
6647 DCHECK(locations->OnlyCallsOnSlowPath());
6648 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6649 /* is_fatal */ false);
6650 codegen_->AddSlowPath(slow_path);
6651 __ B(slow_path->GetEntryLabel());
6652 break;
6653 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006654 }
6655
6656 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006657
6658 if (slow_path != nullptr) {
6659 __ Bind(slow_path->GetExitLabel());
6660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006661}
6662
6663void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6665 locations->SetOut(Location::ConstantLocation(constant));
6666}
6667
6668void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6669 // Will be generated at use site.
6670}
6671
6672void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6673 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6674 locations->SetOut(Location::ConstantLocation(constant));
6675}
6676
6677void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6678 // Will be generated at use site.
6679}
6680
6681void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6682 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6683 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6684}
6685
6686void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6687 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006688 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006689 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006690 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006691}
6692
6693void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6694 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6695 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006696 Location receiver = invoke->GetLocations()->InAt(0);
6697 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006698 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006699
6700 // Set the hidden argument.
6701 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6702 invoke->GetDexMethodIndex());
6703
6704 // temp = object->GetClass();
6705 if (receiver.IsStackSlot()) {
6706 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6707 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6708 } else {
6709 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6710 }
6711 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006712 // Instead of simply (possibly) unpoisoning `temp` here, we should
6713 // emit a read barrier for the previous class reference load.
6714 // However this is not required in practice, as this is an
6715 // intermediate/temporary reference and because the current
6716 // concurrent copying collector keeps the from-space memory
6717 // intact/accessible until the end of the marking phase (the
6718 // concurrent copying collector may not in the future).
6719 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006720 __ LoadFromOffset(kLoadWord, temp, temp,
6721 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6722 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006723 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724 // temp = temp->GetImtEntryAt(method_offset);
6725 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6726 // T9 = temp->GetEntryPoint();
6727 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6728 // T9();
6729 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006730 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731 DCHECK(!codegen_->IsLeafMethod());
6732 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6733}
6734
6735void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006736 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6737 if (intrinsic.TryDispatch(invoke)) {
6738 return;
6739 }
6740
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006741 HandleInvoke(invoke);
6742}
6743
6744void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006745 // Explicit clinit checks triggered by static invokes must have been pruned by
6746 // art::PrepareForRegisterAllocation.
6747 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006748
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006749 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6750 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006751
Chris Larsen701566a2015-10-27 15:29:13 -07006752 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6753 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006754 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6755 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6756 }
Chris Larsen701566a2015-10-27 15:29:13 -07006757 return;
6758 }
6759
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006760 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006761
6762 // Add the extra input register if either the dex cache array base register
6763 // or the PC-relative base register for accessing literals is needed.
6764 if (has_extra_input) {
6765 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
6766 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006767}
6768
Orion Hodsonac141392017-01-13 11:53:47 +00006769void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6770 HandleInvoke(invoke);
6771}
6772
6773void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6774 codegen_->GenerateInvokePolymorphicCall(invoke);
6775}
6776
Chris Larsen701566a2015-10-27 15:29:13 -07006777static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006778 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07006779 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
6780 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006781 return true;
6782 }
6783 return false;
6784}
6785
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006786HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07006787 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006788 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006789 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006790 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
6791 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07006792 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006793 bool is_r6 = GetInstructionSetFeatures().IsR6();
6794 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006795 switch (desired_string_load_kind) {
6796 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6797 DCHECK(!GetCompilerOptions().GetCompilePic());
6798 break;
6799 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6800 DCHECK(GetCompilerOptions().GetCompilePic());
6801 break;
6802 case HLoadString::LoadKind::kBootImageAddress:
6803 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006804 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006805 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006806 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006807 case HLoadString::LoadKind::kJitTableAddress:
6808 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006809 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006810 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006811 case HLoadString::LoadKind::kDexCacheViaMethod:
6812 fallback_load = false;
6813 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006814 }
6815 if (fallback_load) {
6816 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
6817 }
6818 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006819}
6820
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006821HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
6822 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006823 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006824 // is incompatible with it.
6825 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006826 bool is_r6 = GetInstructionSetFeatures().IsR6();
6827 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006828 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006829 case HLoadClass::LoadKind::kInvalid:
6830 LOG(FATAL) << "UNREACHABLE";
6831 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07006832 case HLoadClass::LoadKind::kReferrersClass:
6833 fallback_load = false;
6834 break;
6835 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6836 DCHECK(!GetCompilerOptions().GetCompilePic());
6837 break;
6838 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6839 DCHECK(GetCompilerOptions().GetCompilePic());
6840 break;
6841 case HLoadClass::LoadKind::kBootImageAddress:
6842 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006843 case HLoadClass::LoadKind::kBssEntry:
6844 DCHECK(!Runtime::Current()->UseJitCompilation());
6845 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006846 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006847 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006848 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006849 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006850 case HLoadClass::LoadKind::kDexCacheViaMethod:
6851 fallback_load = false;
6852 break;
6853 }
6854 if (fallback_load) {
6855 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
6856 }
6857 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006858}
6859
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006860Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6861 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006862 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006863 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6864 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6865 if (!invoke->GetLocations()->Intrinsified()) {
6866 return location.AsRegister<Register>();
6867 }
6868 // For intrinsics we allow any location, so it may be on the stack.
6869 if (!location.IsRegister()) {
6870 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6871 return temp;
6872 }
6873 // For register locations, check if the register was saved. If so, get it from the stack.
6874 // Note: There is a chance that the register was saved but not overwritten, so we could
6875 // save one load. However, since this is just an intrinsic slow path we prefer this
6876 // simple and more robust approach rather that trying to determine if that's the case.
6877 SlowPathCode* slow_path = GetCurrentSlowPath();
6878 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6879 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6880 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6881 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6882 return temp;
6883 }
6884 return location.AsRegister<Register>();
6885}
6886
Vladimir Markodc151b22015-10-15 18:02:30 +01006887HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
6888 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006889 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006890 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006891 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006892 // is incompatible with it.
6893 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006894 bool is_r6 = GetInstructionSetFeatures().IsR6();
6895 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006896 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006897 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006898 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01006899 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006900 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01006901 break;
6902 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006903 if (fallback_load) {
6904 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6905 dispatch_info.method_load_data = 0;
6906 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006907 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006908}
6909
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006910void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
6911 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006912 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006913 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
6914 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006915 bool is_r6 = GetInstructionSetFeatures().IsR6();
6916 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006917 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
6918 : ZERO;
6919
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006920 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006921 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006922 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006923 uint32_t offset =
6924 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006925 __ LoadFromOffset(kLoadWord,
6926 temp.AsRegister<Register>(),
6927 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006928 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006929 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006930 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006931 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006932 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006933 break;
6934 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6935 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6936 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006937 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
6938 if (is_r6) {
6939 uint32_t offset = invoke->GetDexCacheArrayOffset();
6940 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6941 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
6942 bool reordering = __ SetReorder(false);
6943 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
6944 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
6945 __ SetReorder(reordering);
6946 } else {
6947 HMipsDexCacheArraysBase* base =
6948 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
6949 int32_t offset =
6950 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
6951 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6952 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006953 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006954 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006955 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006956 Register reg = temp.AsRegister<Register>();
6957 Register method_reg;
6958 if (current_method.IsRegister()) {
6959 method_reg = current_method.AsRegister<Register>();
6960 } else {
6961 // TODO: use the appropriate DCHECK() here if possible.
6962 // DCHECK(invoke->GetLocations()->Intrinsified());
6963 DCHECK(!current_method.IsValid());
6964 method_reg = reg;
6965 __ Lw(reg, SP, kCurrentMethodStackOffset);
6966 }
6967
6968 // temp = temp->dex_cache_resolved_methods_;
6969 __ LoadFromOffset(kLoadWord,
6970 reg,
6971 method_reg,
6972 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006973 // temp = temp[index_in_cache];
6974 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6975 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006976 __ LoadFromOffset(kLoadWord,
6977 reg,
6978 reg,
6979 CodeGenerator::GetCachePointerOffset(index_in_cache));
6980 break;
6981 }
6982 }
6983
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006984 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006985 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006986 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006987 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006988 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6989 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01006990 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006991 T9,
6992 callee_method.AsRegister<Register>(),
6993 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006994 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006995 // T9()
6996 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006997 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006998 break;
6999 }
7000 DCHECK(!IsLeafMethod());
7001}
7002
7003void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007004 // Explicit clinit checks triggered by static invokes must have been pruned by
7005 // art::PrepareForRegisterAllocation.
7006 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007007
7008 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7009 return;
7010 }
7011
7012 LocationSummary* locations = invoke->GetLocations();
7013 codegen_->GenerateStaticOrDirectCall(invoke,
7014 locations->HasTemps()
7015 ? locations->GetTemp(0)
7016 : Location::NoLocation());
7017 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7018}
7019
Chris Larsen3acee732015-11-18 13:31:08 -08007020void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007021 // Use the calling convention instead of the location of the receiver, as
7022 // intrinsics may have put the receiver in a different register. In the intrinsics
7023 // slow path, the arguments have been moved to the right place, so here we are
7024 // guaranteed that the receiver is the first register of the calling convention.
7025 InvokeDexCallingConvention calling_convention;
7026 Register receiver = calling_convention.GetRegisterAt(0);
7027
Chris Larsen3acee732015-11-18 13:31:08 -08007028 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007029 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7030 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7031 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007032 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007033
7034 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007035 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007036 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007037 // Instead of simply (possibly) unpoisoning `temp` here, we should
7038 // emit a read barrier for the previous class reference load.
7039 // However this is not required in practice, as this is an
7040 // intermediate/temporary reference and because the current
7041 // concurrent copying collector keeps the from-space memory
7042 // intact/accessible until the end of the marking phase (the
7043 // concurrent copying collector may not in the future).
7044 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007045 // temp = temp->GetMethodAt(method_offset);
7046 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7047 // T9 = temp->GetEntryPoint();
7048 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7049 // T9();
7050 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007051 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08007052}
7053
7054void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7055 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7056 return;
7057 }
7058
7059 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007060 DCHECK(!codegen_->IsLeafMethod());
7061 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7062}
7063
7064void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007065 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7066 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007067 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007068 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007069 cls,
7070 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze15958152017-02-09 19:08:30 -08007071 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunze06a46c42016-07-19 15:00:40 -07007072 return;
7073 }
Vladimir Marko41559982017-01-06 14:04:23 +00007074 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007075
Alexey Frunze15958152017-02-09 19:08:30 -08007076 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7077 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007078 ? LocationSummary::kCallOnSlowPath
7079 : LocationSummary::kNoCall;
7080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007081 switch (load_kind) {
7082 // We need an extra register for PC-relative literals on R2.
7083 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007084 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007085 case HLoadClass::LoadKind::kBootImageAddress:
7086 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007087 if (codegen_->GetInstructionSetFeatures().IsR6()) {
7088 break;
7089 }
7090 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007091 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007092 locations->SetInAt(0, Location::RequiresRegister());
7093 break;
7094 default:
7095 break;
7096 }
7097 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007098}
7099
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007100// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7101// move.
7102void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007103 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7104 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7105 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007106 return;
7107 }
Vladimir Marko41559982017-01-06 14:04:23 +00007108 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007109
Vladimir Marko41559982017-01-06 14:04:23 +00007110 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007111 Location out_loc = locations->Out();
7112 Register out = out_loc.AsRegister<Register>();
7113 Register base_or_current_method_reg;
7114 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7115 switch (load_kind) {
7116 // We need an extra register for PC-relative literals on R2.
7117 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007118 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007119 case HLoadClass::LoadKind::kBootImageAddress:
7120 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007121 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7122 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007123 case HLoadClass::LoadKind::kReferrersClass:
7124 case HLoadClass::LoadKind::kDexCacheViaMethod:
7125 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7126 break;
7127 default:
7128 base_or_current_method_reg = ZERO;
7129 break;
7130 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007131
Alexey Frunze15958152017-02-09 19:08:30 -08007132 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7133 ? kWithoutReadBarrier
7134 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007135 bool generate_null_check = false;
7136 switch (load_kind) {
7137 case HLoadClass::LoadKind::kReferrersClass: {
7138 DCHECK(!cls->CanCallRuntime());
7139 DCHECK(!cls->MustGenerateClinitCheck());
7140 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7141 GenerateGcRootFieldLoad(cls,
7142 out_loc,
7143 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007144 ArtMethod::DeclaringClassOffset().Int32Value(),
7145 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007146 break;
7147 }
7148 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007149 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007150 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007151 __ LoadLiteral(out,
7152 base_or_current_method_reg,
7153 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
7154 cls->GetTypeIndex()));
7155 break;
7156 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007157 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007158 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007159 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7160 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007161 bool reordering = __ SetReorder(false);
7162 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7163 __ Addiu(out, out, /* placeholder */ 0x5678);
7164 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007165 break;
7166 }
7167 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007168 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007169 uint32_t address = dchecked_integral_cast<uint32_t>(
7170 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7171 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007172 __ LoadLiteral(out,
7173 base_or_current_method_reg,
7174 codegen_->DeduplicateBootImageAddressLiteral(address));
7175 break;
7176 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007177 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007178 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007179 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007180 bool reordering = __ SetReorder(false);
7181 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08007182 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007183 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007184 generate_null_check = true;
7185 break;
7186 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007187 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007188 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7189 cls->GetTypeIndex(),
7190 cls->GetClass());
7191 bool reordering = __ SetReorder(false);
7192 __ Bind(&info->high_label);
7193 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007194 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007195 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007196 break;
7197 }
Vladimir Marko41559982017-01-06 14:04:23 +00007198 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007199 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007200 LOG(FATAL) << "UNREACHABLE";
7201 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007202 }
7203
7204 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7205 DCHECK(cls->CanCallRuntime());
7206 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7207 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7208 codegen_->AddSlowPath(slow_path);
7209 if (generate_null_check) {
7210 __ Beqz(out, slow_path->GetEntryLabel());
7211 }
7212 if (cls->MustGenerateClinitCheck()) {
7213 GenerateClassInitializationCheck(slow_path, out);
7214 } else {
7215 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007216 }
7217 }
7218}
7219
7220static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007221 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007222}
7223
7224void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7225 LocationSummary* locations =
7226 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7227 locations->SetOut(Location::RequiresRegister());
7228}
7229
7230void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7231 Register out = load->GetLocations()->Out().AsRegister<Register>();
7232 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7233}
7234
7235void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7236 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7237}
7238
7239void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7240 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7241}
7242
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007243void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007244 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007245 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007246 HLoadString::LoadKind load_kind = load->GetLoadKind();
7247 switch (load_kind) {
7248 // We need an extra register for PC-relative literals on R2.
7249 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7250 case HLoadString::LoadKind::kBootImageAddress:
7251 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007252 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007253 if (codegen_->GetInstructionSetFeatures().IsR6()) {
7254 break;
7255 }
7256 FALLTHROUGH_INTENDED;
7257 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007258 case HLoadString::LoadKind::kDexCacheViaMethod:
7259 locations->SetInAt(0, Location::RequiresRegister());
7260 break;
7261 default:
7262 break;
7263 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007264 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
7265 InvokeRuntimeCallingConvention calling_convention;
7266 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
7267 } else {
7268 locations->SetOut(Location::RequiresRegister());
7269 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007270}
7271
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007272// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7273// move.
7274void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007275 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007276 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007277 Location out_loc = locations->Out();
7278 Register out = out_loc.AsRegister<Register>();
7279 Register base_or_current_method_reg;
7280 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7281 switch (load_kind) {
7282 // We need an extra register for PC-relative literals on R2.
7283 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7284 case HLoadString::LoadKind::kBootImageAddress:
7285 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007286 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007287 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7288 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007289 default:
7290 base_or_current_method_reg = ZERO;
7291 break;
7292 }
7293
7294 switch (load_kind) {
7295 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007296 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007297 __ LoadLiteral(out,
7298 base_or_current_method_reg,
7299 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7300 load->GetStringIndex()));
7301 return; // No dex cache slow path.
7302 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007303 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007304 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007305 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007306 bool reordering = __ SetReorder(false);
7307 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7308 __ Addiu(out, out, /* placeholder */ 0x5678);
7309 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007310 return; // No dex cache slow path.
7311 }
7312 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007313 uint32_t address = dchecked_integral_cast<uint32_t>(
7314 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7315 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007316 __ LoadLiteral(out,
7317 base_or_current_method_reg,
7318 codegen_->DeduplicateBootImageAddressLiteral(address));
7319 return; // No dex cache slow path.
7320 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007321 case HLoadString::LoadKind::kBssEntry: {
7322 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7323 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007324 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007325 bool reordering = __ SetReorder(false);
7326 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08007327 GenerateGcRootFieldLoad(load,
7328 out_loc,
7329 out,
7330 /* placeholder */ 0x5678,
7331 kCompilerReadBarrierOption);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007332 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007333 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7334 codegen_->AddSlowPath(slow_path);
7335 __ Beqz(out, slow_path->GetEntryLabel());
7336 __ Bind(slow_path->GetExitLabel());
7337 return;
7338 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007339 case HLoadString::LoadKind::kJitTableAddress: {
7340 CodeGeneratorMIPS::JitPatchInfo* info =
7341 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7342 load->GetStringIndex(),
7343 load->GetString());
7344 bool reordering = __ SetReorder(false);
7345 __ Bind(&info->high_label);
7346 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007347 GenerateGcRootFieldLoad(load,
7348 out_loc,
7349 out,
7350 /* placeholder */ 0x5678,
7351 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007352 __ SetReorder(reordering);
7353 return;
7354 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007355 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007356 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007357 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007358
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007359 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007360 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7361 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007362 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007363 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7364 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007365}
7366
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007367void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7368 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7369 locations->SetOut(Location::ConstantLocation(constant));
7370}
7371
7372void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7373 // Will be generated at use site.
7374}
7375
7376void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7377 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007378 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007379 InvokeRuntimeCallingConvention calling_convention;
7380 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7381}
7382
7383void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7384 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007385 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007386 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7387 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007388 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007389 }
7390 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7391}
7392
7393void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7394 LocationSummary* locations =
7395 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7396 switch (mul->GetResultType()) {
7397 case Primitive::kPrimInt:
7398 case Primitive::kPrimLong:
7399 locations->SetInAt(0, Location::RequiresRegister());
7400 locations->SetInAt(1, Location::RequiresRegister());
7401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7402 break;
7403
7404 case Primitive::kPrimFloat:
7405 case Primitive::kPrimDouble:
7406 locations->SetInAt(0, Location::RequiresFpuRegister());
7407 locations->SetInAt(1, Location::RequiresFpuRegister());
7408 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7409 break;
7410
7411 default:
7412 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7413 }
7414}
7415
7416void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7417 Primitive::Type type = instruction->GetType();
7418 LocationSummary* locations = instruction->GetLocations();
7419 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7420
7421 switch (type) {
7422 case Primitive::kPrimInt: {
7423 Register dst = locations->Out().AsRegister<Register>();
7424 Register lhs = locations->InAt(0).AsRegister<Register>();
7425 Register rhs = locations->InAt(1).AsRegister<Register>();
7426
7427 if (isR6) {
7428 __ MulR6(dst, lhs, rhs);
7429 } else {
7430 __ MulR2(dst, lhs, rhs);
7431 }
7432 break;
7433 }
7434 case Primitive::kPrimLong: {
7435 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7436 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7437 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7438 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7439 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7440 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7441
7442 // Extra checks to protect caused by the existance of A1_A2.
7443 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7444 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7445 DCHECK_NE(dst_high, lhs_low);
7446 DCHECK_NE(dst_high, rhs_low);
7447
7448 // A_B * C_D
7449 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7450 // dst_lo: [ low(B*D) ]
7451 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7452
7453 if (isR6) {
7454 __ MulR6(TMP, lhs_high, rhs_low);
7455 __ MulR6(dst_high, lhs_low, rhs_high);
7456 __ Addu(dst_high, dst_high, TMP);
7457 __ MuhuR6(TMP, lhs_low, rhs_low);
7458 __ Addu(dst_high, dst_high, TMP);
7459 __ MulR6(dst_low, lhs_low, rhs_low);
7460 } else {
7461 __ MulR2(TMP, lhs_high, rhs_low);
7462 __ MulR2(dst_high, lhs_low, rhs_high);
7463 __ Addu(dst_high, dst_high, TMP);
7464 __ MultuR2(lhs_low, rhs_low);
7465 __ Mfhi(TMP);
7466 __ Addu(dst_high, dst_high, TMP);
7467 __ Mflo(dst_low);
7468 }
7469 break;
7470 }
7471 case Primitive::kPrimFloat:
7472 case Primitive::kPrimDouble: {
7473 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7474 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7475 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7476 if (type == Primitive::kPrimFloat) {
7477 __ MulS(dst, lhs, rhs);
7478 } else {
7479 __ MulD(dst, lhs, rhs);
7480 }
7481 break;
7482 }
7483 default:
7484 LOG(FATAL) << "Unexpected mul type " << type;
7485 }
7486}
7487
7488void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7489 LocationSummary* locations =
7490 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7491 switch (neg->GetResultType()) {
7492 case Primitive::kPrimInt:
7493 case Primitive::kPrimLong:
7494 locations->SetInAt(0, Location::RequiresRegister());
7495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7496 break;
7497
7498 case Primitive::kPrimFloat:
7499 case Primitive::kPrimDouble:
7500 locations->SetInAt(0, Location::RequiresFpuRegister());
7501 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7502 break;
7503
7504 default:
7505 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7506 }
7507}
7508
7509void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7510 Primitive::Type type = instruction->GetType();
7511 LocationSummary* locations = instruction->GetLocations();
7512
7513 switch (type) {
7514 case Primitive::kPrimInt: {
7515 Register dst = locations->Out().AsRegister<Register>();
7516 Register src = locations->InAt(0).AsRegister<Register>();
7517 __ Subu(dst, ZERO, src);
7518 break;
7519 }
7520 case Primitive::kPrimLong: {
7521 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7522 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7523 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7524 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7525 __ Subu(dst_low, ZERO, src_low);
7526 __ Sltu(TMP, ZERO, dst_low);
7527 __ Subu(dst_high, ZERO, src_high);
7528 __ Subu(dst_high, dst_high, TMP);
7529 break;
7530 }
7531 case Primitive::kPrimFloat:
7532 case Primitive::kPrimDouble: {
7533 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7534 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7535 if (type == Primitive::kPrimFloat) {
7536 __ NegS(dst, src);
7537 } else {
7538 __ NegD(dst, src);
7539 }
7540 break;
7541 }
7542 default:
7543 LOG(FATAL) << "Unexpected neg type " << type;
7544 }
7545}
7546
7547void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7548 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007549 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007550 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007551 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007552 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7553 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007554}
7555
7556void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007557 // Note: if heap poisoning is enabled, the entry point takes care
7558 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007559 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7560 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007561}
7562
7563void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7564 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007565 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007566 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007567 if (instruction->IsStringAlloc()) {
7568 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7569 } else {
7570 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007571 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007572 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7573}
7574
7575void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007576 // Note: if heap poisoning is enabled, the entry point takes care
7577 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007578 if (instruction->IsStringAlloc()) {
7579 // String is allocated through StringFactory. Call NewEmptyString entry point.
7580 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007581 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007582 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7583 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7584 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007585 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007586 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7587 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007588 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007589 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007590 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007591}
7592
7593void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7595 locations->SetInAt(0, Location::RequiresRegister());
7596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7597}
7598
7599void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7600 Primitive::Type type = instruction->GetType();
7601 LocationSummary* locations = instruction->GetLocations();
7602
7603 switch (type) {
7604 case Primitive::kPrimInt: {
7605 Register dst = locations->Out().AsRegister<Register>();
7606 Register src = locations->InAt(0).AsRegister<Register>();
7607 __ Nor(dst, src, ZERO);
7608 break;
7609 }
7610
7611 case Primitive::kPrimLong: {
7612 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7613 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7614 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7615 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7616 __ Nor(dst_high, src_high, ZERO);
7617 __ Nor(dst_low, src_low, ZERO);
7618 break;
7619 }
7620
7621 default:
7622 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7623 }
7624}
7625
7626void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7627 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7628 locations->SetInAt(0, Location::RequiresRegister());
7629 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7630}
7631
7632void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7633 LocationSummary* locations = instruction->GetLocations();
7634 __ Xori(locations->Out().AsRegister<Register>(),
7635 locations->InAt(0).AsRegister<Register>(),
7636 1);
7637}
7638
7639void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007640 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7641 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007642}
7643
Calin Juravle2ae48182016-03-16 14:05:09 +00007644void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7645 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007646 return;
7647 }
7648 Location obj = instruction->GetLocations()->InAt(0);
7649
7650 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007651 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007652}
7653
Calin Juravle2ae48182016-03-16 14:05:09 +00007654void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007655 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007656 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007657
7658 Location obj = instruction->GetLocations()->InAt(0);
7659
7660 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7661}
7662
7663void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007664 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007665}
7666
7667void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7668 HandleBinaryOp(instruction);
7669}
7670
7671void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7672 HandleBinaryOp(instruction);
7673}
7674
7675void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7676 LOG(FATAL) << "Unreachable";
7677}
7678
7679void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7680 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7681}
7682
7683void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7684 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7685 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7686 if (location.IsStackSlot()) {
7687 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7688 } else if (location.IsDoubleStackSlot()) {
7689 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7690 }
7691 locations->SetOut(location);
7692}
7693
7694void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7695 ATTRIBUTE_UNUSED) {
7696 // Nothing to do, the parameter is already at its location.
7697}
7698
7699void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
7700 LocationSummary* locations =
7701 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7702 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
7703}
7704
7705void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
7706 ATTRIBUTE_UNUSED) {
7707 // Nothing to do, the method is already at its location.
7708}
7709
7710void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
7711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01007712 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007713 locations->SetInAt(i, Location::Any());
7714 }
7715 locations->SetOut(Location::Any());
7716}
7717
7718void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
7719 LOG(FATAL) << "Unreachable";
7720}
7721
7722void LocationsBuilderMIPS::VisitRem(HRem* rem) {
7723 Primitive::Type type = rem->GetResultType();
7724 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007725 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
7727
7728 switch (type) {
7729 case Primitive::kPrimInt:
7730 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08007731 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7733 break;
7734
7735 case Primitive::kPrimLong: {
7736 InvokeRuntimeCallingConvention calling_convention;
7737 locations->SetInAt(0, Location::RegisterPairLocation(
7738 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7739 locations->SetInAt(1, Location::RegisterPairLocation(
7740 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
7741 locations->SetOut(calling_convention.GetReturnLocation(type));
7742 break;
7743 }
7744
7745 case Primitive::kPrimFloat:
7746 case Primitive::kPrimDouble: {
7747 InvokeRuntimeCallingConvention calling_convention;
7748 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
7749 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
7750 locations->SetOut(calling_convention.GetReturnLocation(type));
7751 break;
7752 }
7753
7754 default:
7755 LOG(FATAL) << "Unexpected rem type " << type;
7756 }
7757}
7758
7759void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
7760 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007761
7762 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08007763 case Primitive::kPrimInt:
7764 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007765 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007766 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007767 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007768 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
7769 break;
7770 }
7771 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007772 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007773 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007774 break;
7775 }
7776 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007777 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007778 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007779 break;
7780 }
7781 default:
7782 LOG(FATAL) << "Unexpected rem type " << type;
7783 }
7784}
7785
7786void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7787 memory_barrier->SetLocations(nullptr);
7788}
7789
7790void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7791 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7792}
7793
7794void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
7795 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
7796 Primitive::Type return_type = ret->InputAt(0)->GetType();
7797 locations->SetInAt(0, MipsReturnLocation(return_type));
7798}
7799
7800void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7801 codegen_->GenerateFrameExit();
7802}
7803
7804void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
7805 ret->SetLocations(nullptr);
7806}
7807
7808void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7809 codegen_->GenerateFrameExit();
7810}
7811
Alexey Frunze92d90602015-12-18 18:16:36 -08007812void LocationsBuilderMIPS::VisitRor(HRor* ror) {
7813 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007814}
7815
Alexey Frunze92d90602015-12-18 18:16:36 -08007816void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
7817 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007818}
7819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007820void LocationsBuilderMIPS::VisitShl(HShl* shl) {
7821 HandleShift(shl);
7822}
7823
7824void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
7825 HandleShift(shl);
7826}
7827
7828void LocationsBuilderMIPS::VisitShr(HShr* shr) {
7829 HandleShift(shr);
7830}
7831
7832void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
7833 HandleShift(shr);
7834}
7835
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007836void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
7837 HandleBinaryOp(instruction);
7838}
7839
7840void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
7841 HandleBinaryOp(instruction);
7842}
7843
7844void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7845 HandleFieldGet(instruction, instruction->GetFieldInfo());
7846}
7847
7848void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7849 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
7850}
7851
7852void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7853 HandleFieldSet(instruction, instruction->GetFieldInfo());
7854}
7855
7856void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01007857 HandleFieldSet(instruction,
7858 instruction->GetFieldInfo(),
7859 instruction->GetDexPc(),
7860 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007861}
7862
7863void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
7864 HUnresolvedInstanceFieldGet* instruction) {
7865 FieldAccessCallingConventionMIPS calling_convention;
7866 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7867 instruction->GetFieldType(),
7868 calling_convention);
7869}
7870
7871void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
7872 HUnresolvedInstanceFieldGet* instruction) {
7873 FieldAccessCallingConventionMIPS calling_convention;
7874 codegen_->GenerateUnresolvedFieldAccess(instruction,
7875 instruction->GetFieldType(),
7876 instruction->GetFieldIndex(),
7877 instruction->GetDexPc(),
7878 calling_convention);
7879}
7880
7881void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
7882 HUnresolvedInstanceFieldSet* instruction) {
7883 FieldAccessCallingConventionMIPS calling_convention;
7884 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7885 instruction->GetFieldType(),
7886 calling_convention);
7887}
7888
7889void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
7890 HUnresolvedInstanceFieldSet* instruction) {
7891 FieldAccessCallingConventionMIPS calling_convention;
7892 codegen_->GenerateUnresolvedFieldAccess(instruction,
7893 instruction->GetFieldType(),
7894 instruction->GetFieldIndex(),
7895 instruction->GetDexPc(),
7896 calling_convention);
7897}
7898
7899void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
7900 HUnresolvedStaticFieldGet* instruction) {
7901 FieldAccessCallingConventionMIPS calling_convention;
7902 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7903 instruction->GetFieldType(),
7904 calling_convention);
7905}
7906
7907void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
7908 HUnresolvedStaticFieldGet* instruction) {
7909 FieldAccessCallingConventionMIPS calling_convention;
7910 codegen_->GenerateUnresolvedFieldAccess(instruction,
7911 instruction->GetFieldType(),
7912 instruction->GetFieldIndex(),
7913 instruction->GetDexPc(),
7914 calling_convention);
7915}
7916
7917void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
7918 HUnresolvedStaticFieldSet* instruction) {
7919 FieldAccessCallingConventionMIPS calling_convention;
7920 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7921 instruction->GetFieldType(),
7922 calling_convention);
7923}
7924
7925void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
7926 HUnresolvedStaticFieldSet* instruction) {
7927 FieldAccessCallingConventionMIPS calling_convention;
7928 codegen_->GenerateUnresolvedFieldAccess(instruction,
7929 instruction->GetFieldType(),
7930 instruction->GetFieldIndex(),
7931 instruction->GetDexPc(),
7932 calling_convention);
7933}
7934
7935void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01007936 LocationSummary* locations =
7937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01007938 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007939}
7940
7941void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
7942 HBasicBlock* block = instruction->GetBlock();
7943 if (block->GetLoopInformation() != nullptr) {
7944 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7945 // The back edge will generate the suspend check.
7946 return;
7947 }
7948 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7949 // The goto will generate the suspend check.
7950 return;
7951 }
7952 GenerateSuspendCheck(instruction, nullptr);
7953}
7954
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007955void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
7956 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007958 InvokeRuntimeCallingConvention calling_convention;
7959 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7960}
7961
7962void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007963 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007964 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7965}
7966
7967void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
7968 Primitive::Type input_type = conversion->GetInputType();
7969 Primitive::Type result_type = conversion->GetResultType();
7970 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007971 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007972
7973 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
7974 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
7975 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
7976 }
7977
7978 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007979 if (!isR6 &&
7980 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
7981 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007982 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007983 }
7984
7985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
7986
7987 if (call_kind == LocationSummary::kNoCall) {
7988 if (Primitive::IsFloatingPointType(input_type)) {
7989 locations->SetInAt(0, Location::RequiresFpuRegister());
7990 } else {
7991 locations->SetInAt(0, Location::RequiresRegister());
7992 }
7993
7994 if (Primitive::IsFloatingPointType(result_type)) {
7995 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7996 } else {
7997 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7998 }
7999 } else {
8000 InvokeRuntimeCallingConvention calling_convention;
8001
8002 if (Primitive::IsFloatingPointType(input_type)) {
8003 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8004 } else {
8005 DCHECK_EQ(input_type, Primitive::kPrimLong);
8006 locations->SetInAt(0, Location::RegisterPairLocation(
8007 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8008 }
8009
8010 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8011 }
8012}
8013
8014void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8015 LocationSummary* locations = conversion->GetLocations();
8016 Primitive::Type result_type = conversion->GetResultType();
8017 Primitive::Type input_type = conversion->GetInputType();
8018 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008019 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008020
8021 DCHECK_NE(input_type, result_type);
8022
8023 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8024 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8025 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8026 Register src = locations->InAt(0).AsRegister<Register>();
8027
Alexey Frunzea871ef12016-06-27 15:20:11 -07008028 if (dst_low != src) {
8029 __ Move(dst_low, src);
8030 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008031 __ Sra(dst_high, src, 31);
8032 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8033 Register dst = locations->Out().AsRegister<Register>();
8034 Register src = (input_type == Primitive::kPrimLong)
8035 ? locations->InAt(0).AsRegisterPairLow<Register>()
8036 : locations->InAt(0).AsRegister<Register>();
8037
8038 switch (result_type) {
8039 case Primitive::kPrimChar:
8040 __ Andi(dst, src, 0xFFFF);
8041 break;
8042 case Primitive::kPrimByte:
8043 if (has_sign_extension) {
8044 __ Seb(dst, src);
8045 } else {
8046 __ Sll(dst, src, 24);
8047 __ Sra(dst, dst, 24);
8048 }
8049 break;
8050 case Primitive::kPrimShort:
8051 if (has_sign_extension) {
8052 __ Seh(dst, src);
8053 } else {
8054 __ Sll(dst, src, 16);
8055 __ Sra(dst, dst, 16);
8056 }
8057 break;
8058 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008059 if (dst != src) {
8060 __ Move(dst, src);
8061 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008062 break;
8063
8064 default:
8065 LOG(FATAL) << "Unexpected type conversion from " << input_type
8066 << " to " << result_type;
8067 }
8068 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008069 if (input_type == Primitive::kPrimLong) {
8070 if (isR6) {
8071 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8072 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8073 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8074 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8075 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8076 __ Mtc1(src_low, FTMP);
8077 __ Mthc1(src_high, FTMP);
8078 if (result_type == Primitive::kPrimFloat) {
8079 __ Cvtsl(dst, FTMP);
8080 } else {
8081 __ Cvtdl(dst, FTMP);
8082 }
8083 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008084 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8085 : kQuickL2d;
8086 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008087 if (result_type == Primitive::kPrimFloat) {
8088 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8089 } else {
8090 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8091 }
8092 }
8093 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008094 Register src = locations->InAt(0).AsRegister<Register>();
8095 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8096 __ Mtc1(src, FTMP);
8097 if (result_type == Primitive::kPrimFloat) {
8098 __ Cvtsw(dst, FTMP);
8099 } else {
8100 __ Cvtdw(dst, FTMP);
8101 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008102 }
8103 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8104 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008105 if (result_type == Primitive::kPrimLong) {
8106 if (isR6) {
8107 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8108 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8109 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8110 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8111 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8112 MipsLabel truncate;
8113 MipsLabel done;
8114
8115 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8116 // value when the input is either a NaN or is outside of the range of the output type
8117 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8118 // the same result.
8119 //
8120 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8121 // value of the output type if the input is outside of the range after the truncation or
8122 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8123 // results. This matches the desired float/double-to-int/long conversion exactly.
8124 //
8125 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
8126 //
8127 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8128 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8129 // even though it must be NAN2008=1 on R6.
8130 //
8131 // The code takes care of the different behaviors by first comparing the input to the
8132 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8133 // If the input is greater than or equal to the minimum, it procedes to the truncate
8134 // instruction, which will handle such an input the same way irrespective of NAN2008.
8135 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8136 // in order to return either zero or the minimum value.
8137 //
8138 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8139 // truncate instruction for MIPS64R6.
8140 if (input_type == Primitive::kPrimFloat) {
8141 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
8142 __ LoadConst32(TMP, min_val);
8143 __ Mtc1(TMP, FTMP);
8144 __ CmpLeS(FTMP, FTMP, src);
8145 } else {
8146 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
8147 __ LoadConst32(TMP, High32Bits(min_val));
8148 __ Mtc1(ZERO, FTMP);
8149 __ Mthc1(TMP, FTMP);
8150 __ CmpLeD(FTMP, FTMP, src);
8151 }
8152
8153 __ Bc1nez(FTMP, &truncate);
8154
8155 if (input_type == Primitive::kPrimFloat) {
8156 __ CmpEqS(FTMP, src, src);
8157 } else {
8158 __ CmpEqD(FTMP, src, src);
8159 }
8160 __ Move(dst_low, ZERO);
8161 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
8162 __ Mfc1(TMP, FTMP);
8163 __ And(dst_high, dst_high, TMP);
8164
8165 __ B(&done);
8166
8167 __ Bind(&truncate);
8168
8169 if (input_type == Primitive::kPrimFloat) {
8170 __ TruncLS(FTMP, src);
8171 } else {
8172 __ TruncLD(FTMP, src);
8173 }
8174 __ Mfc1(dst_low, FTMP);
8175 __ Mfhc1(dst_high, FTMP);
8176
8177 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008178 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008179 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8180 : kQuickD2l;
8181 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008182 if (input_type == Primitive::kPrimFloat) {
8183 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8184 } else {
8185 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8186 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008187 }
8188 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008189 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8190 Register dst = locations->Out().AsRegister<Register>();
8191 MipsLabel truncate;
8192 MipsLabel done;
8193
8194 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8195 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8196 // even though it must be NAN2008=1 on R6.
8197 //
8198 // For details see the large comment above for the truncation of float/double to long on R6.
8199 //
8200 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8201 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008202 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008203 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8204 __ LoadConst32(TMP, min_val);
8205 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008206 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008207 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8208 __ LoadConst32(TMP, High32Bits(min_val));
8209 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07008210 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008211 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008212
8213 if (isR6) {
8214 if (input_type == Primitive::kPrimFloat) {
8215 __ CmpLeS(FTMP, FTMP, src);
8216 } else {
8217 __ CmpLeD(FTMP, FTMP, src);
8218 }
8219 __ Bc1nez(FTMP, &truncate);
8220
8221 if (input_type == Primitive::kPrimFloat) {
8222 __ CmpEqS(FTMP, src, src);
8223 } else {
8224 __ CmpEqD(FTMP, src, src);
8225 }
8226 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8227 __ Mfc1(TMP, FTMP);
8228 __ And(dst, dst, TMP);
8229 } else {
8230 if (input_type == Primitive::kPrimFloat) {
8231 __ ColeS(0, FTMP, src);
8232 } else {
8233 __ ColeD(0, FTMP, src);
8234 }
8235 __ Bc1t(0, &truncate);
8236
8237 if (input_type == Primitive::kPrimFloat) {
8238 __ CeqS(0, src, src);
8239 } else {
8240 __ CeqD(0, src, src);
8241 }
8242 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8243 __ Movf(dst, ZERO, 0);
8244 }
8245
8246 __ B(&done);
8247
8248 __ Bind(&truncate);
8249
8250 if (input_type == Primitive::kPrimFloat) {
8251 __ TruncWS(FTMP, src);
8252 } else {
8253 __ TruncWD(FTMP, src);
8254 }
8255 __ Mfc1(dst, FTMP);
8256
8257 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008258 }
8259 } else if (Primitive::IsFloatingPointType(result_type) &&
8260 Primitive::IsFloatingPointType(input_type)) {
8261 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8262 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8263 if (result_type == Primitive::kPrimFloat) {
8264 __ Cvtsd(dst, src);
8265 } else {
8266 __ Cvtds(dst, src);
8267 }
8268 } else {
8269 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8270 << " to " << result_type;
8271 }
8272}
8273
8274void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8275 HandleShift(ushr);
8276}
8277
8278void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8279 HandleShift(ushr);
8280}
8281
8282void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8283 HandleBinaryOp(instruction);
8284}
8285
8286void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8287 HandleBinaryOp(instruction);
8288}
8289
8290void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8291 // Nothing to do, this should be removed during prepare for register allocator.
8292 LOG(FATAL) << "Unreachable";
8293}
8294
8295void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8296 // Nothing to do, this should be removed during prepare for register allocator.
8297 LOG(FATAL) << "Unreachable";
8298}
8299
8300void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008301 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008302}
8303
8304void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008305 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008306}
8307
8308void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008309 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008310}
8311
8312void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008313 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008314}
8315
8316void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008317 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008318}
8319
8320void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008321 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008322}
8323
8324void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008325 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008326}
8327
8328void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008329 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008330}
8331
8332void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008333 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008334}
8335
8336void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008337 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008338}
8339
8340void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008341 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342}
8343
8344void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008345 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008346}
8347
8348void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008349 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008350}
8351
8352void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008353 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008354}
8355
8356void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008357 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008358}
8359
8360void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008361 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008362}
8363
8364void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008365 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366}
8367
8368void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008369 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008370}
8371
8372void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008373 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008374}
8375
8376void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008377 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008378}
8379
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008380void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8381 LocationSummary* locations =
8382 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8383 locations->SetInAt(0, Location::RequiresRegister());
8384}
8385
Alexey Frunze96b66822016-09-10 02:32:44 -07008386void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8387 int32_t lower_bound,
8388 uint32_t num_entries,
8389 HBasicBlock* switch_block,
8390 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008391 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008392 Register temp_reg = TMP;
8393 __ Addiu32(temp_reg, value_reg, -lower_bound);
8394 // Jump to default if index is negative
8395 // Note: We don't check the case that index is positive while value < lower_bound, because in
8396 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8397 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8398
Alexey Frunze96b66822016-09-10 02:32:44 -07008399 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008400 // Jump to successors[0] if value == lower_bound.
8401 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8402 int32_t last_index = 0;
8403 for (; num_entries - last_index > 2; last_index += 2) {
8404 __ Addiu(temp_reg, temp_reg, -2);
8405 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8406 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8407 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8408 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8409 }
8410 if (num_entries - last_index == 2) {
8411 // The last missing case_value.
8412 __ Addiu(temp_reg, temp_reg, -1);
8413 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008414 }
8415
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008416 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008417 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008418 __ B(codegen_->GetLabelOf(default_block));
8419 }
8420}
8421
Alexey Frunze96b66822016-09-10 02:32:44 -07008422void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8423 Register constant_area,
8424 int32_t lower_bound,
8425 uint32_t num_entries,
8426 HBasicBlock* switch_block,
8427 HBasicBlock* default_block) {
8428 // Create a jump table.
8429 std::vector<MipsLabel*> labels(num_entries);
8430 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8431 for (uint32_t i = 0; i < num_entries; i++) {
8432 labels[i] = codegen_->GetLabelOf(successors[i]);
8433 }
8434 JumpTable* table = __ CreateJumpTable(std::move(labels));
8435
8436 // Is the value in range?
8437 __ Addiu32(TMP, value_reg, -lower_bound);
8438 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8439 __ Sltiu(AT, TMP, num_entries);
8440 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8441 } else {
8442 __ LoadConst32(AT, num_entries);
8443 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8444 }
8445
8446 // We are in the range of the table.
8447 // Load the target address from the jump table, indexing by the value.
8448 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
8449 __ Sll(TMP, TMP, 2);
8450 __ Addu(TMP, TMP, AT);
8451 __ Lw(TMP, TMP, 0);
8452 // Compute the absolute target address by adding the table start address
8453 // (the table contains offsets to targets relative to its start).
8454 __ Addu(TMP, TMP, AT);
8455 // And jump.
8456 __ Jr(TMP);
8457 __ NopIfNoReordering();
8458}
8459
8460void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8461 int32_t lower_bound = switch_instr->GetStartValue();
8462 uint32_t num_entries = switch_instr->GetNumEntries();
8463 LocationSummary* locations = switch_instr->GetLocations();
8464 Register value_reg = locations->InAt(0).AsRegister<Register>();
8465 HBasicBlock* switch_block = switch_instr->GetBlock();
8466 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8467
8468 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8469 num_entries > kPackedSwitchJumpTableThreshold) {
8470 // R6 uses PC-relative addressing to access the jump table.
8471 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8472 // the jump table and it is implemented by changing HPackedSwitch to
8473 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8474 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8475 GenTableBasedPackedSwitch(value_reg,
8476 ZERO,
8477 lower_bound,
8478 num_entries,
8479 switch_block,
8480 default_block);
8481 } else {
8482 GenPackedSwitchWithCompares(value_reg,
8483 lower_bound,
8484 num_entries,
8485 switch_block,
8486 default_block);
8487 }
8488}
8489
8490void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8491 LocationSummary* locations =
8492 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8493 locations->SetInAt(0, Location::RequiresRegister());
8494 // Constant area pointer (HMipsComputeBaseMethodAddress).
8495 locations->SetInAt(1, Location::RequiresRegister());
8496}
8497
8498void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8499 int32_t lower_bound = switch_instr->GetStartValue();
8500 uint32_t num_entries = switch_instr->GetNumEntries();
8501 LocationSummary* locations = switch_instr->GetLocations();
8502 Register value_reg = locations->InAt(0).AsRegister<Register>();
8503 Register constant_area = locations->InAt(1).AsRegister<Register>();
8504 HBasicBlock* switch_block = switch_instr->GetBlock();
8505 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8506
8507 // This is an R2-only path. HPackedSwitch has been changed to
8508 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8509 // required to address the jump table relative to PC.
8510 GenTableBasedPackedSwitch(value_reg,
8511 constant_area,
8512 lower_bound,
8513 num_entries,
8514 switch_block,
8515 default_block);
8516}
8517
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008518void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8519 HMipsComputeBaseMethodAddress* insn) {
8520 LocationSummary* locations =
8521 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8522 locations->SetOut(Location::RequiresRegister());
8523}
8524
8525void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8526 HMipsComputeBaseMethodAddress* insn) {
8527 LocationSummary* locations = insn->GetLocations();
8528 Register reg = locations->Out().AsRegister<Register>();
8529
8530 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8531
8532 // Generate a dummy PC-relative call to obtain PC.
8533 __ Nal();
8534 // Grab the return address off RA.
8535 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008536 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008537
8538 // Remember this offset (the obtained PC value) for later use with constant area.
8539 __ BindPcRelBaseLabel();
8540}
8541
8542void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8543 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8544 locations->SetOut(Location::RequiresRegister());
8545}
8546
8547void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8548 Register reg = base->GetLocations()->Out().AsRegister<Register>();
8549 CodeGeneratorMIPS::PcRelativePatchInfo* info =
8550 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008551 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8552 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008553 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008554 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
8555 __ Addiu(reg, reg, /* placeholder */ 0x5678);
8556 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008557}
8558
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008559void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8560 // The trampoline uses the same calling convention as dex calling conventions,
8561 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8562 // the method_idx.
8563 HandleInvoke(invoke);
8564}
8565
8566void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8567 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8568}
8569
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008570void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8571 LocationSummary* locations =
8572 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8573 locations->SetInAt(0, Location::RequiresRegister());
8574 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008575}
8576
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008577void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8578 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008579 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008580 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008581 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008582 __ LoadFromOffset(kLoadWord,
8583 locations->Out().AsRegister<Register>(),
8584 locations->InAt(0).AsRegister<Register>(),
8585 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008586 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008587 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008588 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008589 __ LoadFromOffset(kLoadWord,
8590 locations->Out().AsRegister<Register>(),
8591 locations->InAt(0).AsRegister<Register>(),
8592 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008593 __ LoadFromOffset(kLoadWord,
8594 locations->Out().AsRegister<Register>(),
8595 locations->Out().AsRegister<Register>(),
8596 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008597 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008598}
8599
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008600#undef __
8601#undef QUICK_ENTRY_POINT
8602
8603} // namespace mips
8604} // namespace art