blob: 197853411226db740b7900300a33e1de104a3689 [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();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700222 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200223 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700224 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
225 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
226 InvokeRuntimeCallingConvention calling_convention;
227 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
228 const bool is_load_class_bss_entry =
229 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230 __ Bind(GetEntryLabel());
231 SaveLiveRegisters(codegen, locations);
232
Alexey Frunzec61c0762017-04-10 13:54:23 -0700233 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
234 Register entry_address = kNoRegister;
235 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
236 Register temp = locations->GetTemp(0).AsRegister<Register>();
237 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
238 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
239 // kSaveEverything call.
240 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
241 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
242 if (temp_is_a0) {
243 __ Move(entry_address, temp);
244 }
245 }
246
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000247 dex::TypeIndex type_index = cls_->GetTypeIndex();
248 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100249 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
250 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000251 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200252 if (do_clinit_) {
253 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
254 } else {
255 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
256 }
257
Alexey Frunzec61c0762017-04-10 13:54:23 -0700258 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
259 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
260 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
261 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
262 }
263
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200264 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200265 if (out.IsValid()) {
266 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000267 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700268 mips_codegen->MoveLocation(out,
269 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
270 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200271 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700273
274 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
275 if (is_load_class_bss_entry && !r2_baker_or_no_read_barriers) {
276 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
277 // the class entry.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000278 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000280 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800281 bool reordering = __ SetReorder(false);
282 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
283 __ StoreToOffset(kStoreWord, out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
284 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000285 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 __ B(GetExitLabel());
287 }
288
289 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
290
291 private:
292 // The class this slow path will load.
293 HLoadClass* const cls_;
294
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200295 // The dex PC of `at_`.
296 const uint32_t dex_pc_;
297
298 // Whether to initialize the class.
299 const bool do_clinit_;
300
301 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
302};
303
304class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
305 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000306 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307
308 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700309 DCHECK(instruction_->IsLoadString());
310 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200311 LocationSummary* locations = instruction_->GetLocations();
312 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700313 HLoadString* load = instruction_->AsLoadString();
314 const dex::StringIndex string_index = load->GetStringIndex();
315 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700317 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
318 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
319 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200320 __ Bind(GetEntryLabel());
321 SaveLiveRegisters(codegen, locations);
322
Alexey Frunzec61c0762017-04-10 13:54:23 -0700323 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
324 Register entry_address = kNoRegister;
325 if (r2_baker_or_no_read_barriers) {
326 Register temp = locations->GetTemp(0).AsRegister<Register>();
327 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
328 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
329 // kSaveEverything call.
330 entry_address = temp_is_a0 ? out : temp;
331 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
332 if (temp_is_a0) {
333 __ Move(entry_address, temp);
334 }
335 }
336
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000337 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100338 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700340
341 // Store the resolved string to the BSS entry.
342 if (r2_baker_or_no_read_barriers) {
343 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
344 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
345 }
346
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 Primitive::Type type = instruction_->GetType();
348 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700349 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000352
Alexey Frunzec61c0762017-04-10 13:54:23 -0700353 // Store the resolved string to the BSS entry.
354 if (!r2_baker_or_no_read_barriers) {
355 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
356 // the string entry.
357 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
358 CodeGeneratorMIPS::PcRelativePatchInfo* info =
359 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
360 bool reordering = __ SetReorder(false);
361 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
362 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
363 __ SetReorder(reordering);
364 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200365 __ B(GetExitLabel());
366 }
367
368 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
369
370 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200371 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
372};
373
374class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
375 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000376 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377
378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
379 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
380 __ Bind(GetEntryLabel());
381 if (instruction_->CanThrowIntoCatchBlock()) {
382 // Live registers will be restored in the catch block if caught.
383 SaveLiveRegisters(codegen, instruction_->GetLocations());
384 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100385 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200386 instruction_,
387 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100388 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
390 }
391
392 bool IsFatal() const OVERRIDE { return true; }
393
394 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
395
396 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
398};
399
400class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
401 public:
402 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
407 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100408 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 if (successor_ == nullptr) {
411 __ B(GetReturnLabel());
412 } else {
413 __ B(mips_codegen->GetLabelOf(successor_));
414 }
415 }
416
417 MipsLabel* GetReturnLabel() {
418 DCHECK(successor_ == nullptr);
419 return &return_label_;
420 }
421
422 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
423
424 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425 // If not null, the block to branch to after the suspend check.
426 HBasicBlock* const successor_;
427
428 // If `successor_` is null, the label to branch to after the suspend check.
429 MipsLabel return_label_;
430
431 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
432};
433
434class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
435 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800436 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
437 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438
439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
440 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441 uint32_t dex_pc = instruction_->GetDexPc();
442 DCHECK(instruction_->IsCheckCast()
443 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
444 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
445
446 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800447 if (!is_fatal_) {
448 SaveLiveRegisters(codegen, locations);
449 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450
451 // We're moving two locations to locations that could overlap, so we need a parallel
452 // move resolver.
453 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800454 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
456 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800457 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200458 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
459 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100461 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800462 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200463 Primitive::Type ret_type = instruction_->GetType();
464 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
465 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200466 } else {
467 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800468 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
469 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200470 }
471
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800472 if (!is_fatal_) {
473 RestoreLiveRegisters(codegen, locations);
474 __ B(GetExitLabel());
475 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200476 }
477
478 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
479
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800480 bool IsFatal() const OVERRIDE { return is_fatal_; }
481
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800483 const bool is_fatal_;
484
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
486};
487
488class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
489 public:
Aart Bik42249c32016-01-07 15:33:50 -0800490 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000491 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200492
493 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800494 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200495 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100496 LocationSummary* locations = instruction_->GetLocations();
497 SaveLiveRegisters(codegen, locations);
498 InvokeRuntimeCallingConvention calling_convention;
499 __ LoadConst32(calling_convention.GetRegisterAt(0),
500 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100501 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100502 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200503 }
504
505 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
506
507 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200508 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
509};
510
Alexey Frunze15958152017-02-09 19:08:30 -0800511class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
512 public:
513 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
516 LocationSummary* locations = instruction_->GetLocations();
517 __ Bind(GetEntryLabel());
518 SaveLiveRegisters(codegen, locations);
519
520 InvokeRuntimeCallingConvention calling_convention;
521 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
522 parallel_move.AddMove(
523 locations->InAt(0),
524 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
525 Primitive::kPrimNot,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(1),
529 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
530 Primitive::kPrimInt,
531 nullptr);
532 parallel_move.AddMove(
533 locations->InAt(2),
534 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
535 Primitive::kPrimNot,
536 nullptr);
537 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
538
539 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
540 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
541 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
542 RestoreLiveRegisters(codegen, locations);
543 __ B(GetExitLabel());
544 }
545
546 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
547
548 private:
549 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
550};
551
552// Slow path marking an object reference `ref` during a read
553// barrier. The field `obj.field` in the object `obj` holding this
554// reference does not get updated by this slow path after marking (see
555// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
556//
557// This means that after the execution of this slow path, `ref` will
558// always be up-to-date, but `obj.field` may not; i.e., after the
559// flip, `ref` will be a to-space reference, but `obj.field` will
560// probably still be a from-space reference (unless it gets updated by
561// another thread, or if another thread installed another object
562// reference (different from `ref`) in `obj.field`).
563//
564// If `entrypoint` is a valid location it is assumed to already be
565// holding the entrypoint. The case where the entrypoint is passed in
566// is for the GcRoot read barrier.
567class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
568 public:
569 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
570 Location ref,
571 Location entrypoint = Location::NoLocation())
572 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
573 DCHECK(kEmitCompilerReadBarrier);
574 }
575
576 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
577
578 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
579 LocationSummary* locations = instruction_->GetLocations();
580 Register ref_reg = ref_.AsRegister<Register>();
581 DCHECK(locations->CanCall());
582 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
583 DCHECK(instruction_->IsInstanceFieldGet() ||
584 instruction_->IsStaticFieldGet() ||
585 instruction_->IsArrayGet() ||
586 instruction_->IsArraySet() ||
587 instruction_->IsLoadClass() ||
588 instruction_->IsLoadString() ||
589 instruction_->IsInstanceOf() ||
590 instruction_->IsCheckCast() ||
591 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
592 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
593 << "Unexpected instruction in read barrier marking slow path: "
594 << instruction_->DebugName();
595
596 __ Bind(GetEntryLabel());
597 // No need to save live registers; it's taken care of by the
598 // entrypoint. Also, there is no need to update the stack mask,
599 // as this runtime call will not trigger a garbage collection.
600 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
601 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
602 (S2 <= ref_reg && ref_reg <= S7) ||
603 (ref_reg == FP)) << ref_reg;
604 // "Compact" slow path, saving two moves.
605 //
606 // Instead of using the standard runtime calling convention (input
607 // and output in A0 and V0 respectively):
608 //
609 // A0 <- ref
610 // V0 <- ReadBarrierMark(A0)
611 // ref <- V0
612 //
613 // we just use rX (the register containing `ref`) as input and output
614 // of a dedicated entrypoint:
615 //
616 // rX <- ReadBarrierMarkRegX(rX)
617 //
618 if (entrypoint_.IsValid()) {
619 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
620 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
621 __ Jalr(entrypoint_.AsRegister<Register>());
622 __ NopIfNoReordering();
623 } else {
624 int32_t entry_point_offset =
625 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
626 // This runtime call does not require a stack map.
627 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
628 instruction_,
629 this,
630 /* direct */ false);
631 }
632 __ B(GetExitLabel());
633 }
634
635 private:
636 // The location (register) of the marked object reference.
637 const Location ref_;
638
639 // The location of the entrypoint if already loaded.
640 const Location entrypoint_;
641
642 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
643};
644
645// Slow path marking an object reference `ref` during a read barrier,
646// and if needed, atomically updating the field `obj.field` in the
647// object `obj` holding this reference after marking (contrary to
648// ReadBarrierMarkSlowPathMIPS above, which never tries to update
649// `obj.field`).
650//
651// This means that after the execution of this slow path, both `ref`
652// and `obj.field` will be up-to-date; i.e., after the flip, both will
653// hold the same to-space reference (unless another thread installed
654// another object reference (different from `ref`) in `obj.field`).
655class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
656 public:
657 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
658 Location ref,
659 Register obj,
660 Location field_offset,
661 Register temp1)
662 : SlowPathCodeMIPS(instruction),
663 ref_(ref),
664 obj_(obj),
665 field_offset_(field_offset),
666 temp1_(temp1) {
667 DCHECK(kEmitCompilerReadBarrier);
668 }
669
670 const char* GetDescription() const OVERRIDE {
671 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
672 }
673
674 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
675 LocationSummary* locations = instruction_->GetLocations();
676 Register ref_reg = ref_.AsRegister<Register>();
677 DCHECK(locations->CanCall());
678 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
679 // This slow path is only used by the UnsafeCASObject intrinsic.
680 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
681 << "Unexpected instruction in read barrier marking and field updating slow path: "
682 << instruction_->DebugName();
683 DCHECK(instruction_->GetLocations()->Intrinsified());
684 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
685 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
686
687 __ Bind(GetEntryLabel());
688
689 // Save the old reference.
690 // Note that we cannot use AT or TMP to save the old reference, as those
691 // are used by the code that follows, but we need the old reference after
692 // the call to the ReadBarrierMarkRegX entry point.
693 DCHECK_NE(temp1_, AT);
694 DCHECK_NE(temp1_, TMP);
695 __ Move(temp1_, ref_reg);
696
697 // No need to save live registers; it's taken care of by the
698 // entrypoint. Also, there is no need to update the stack mask,
699 // as this runtime call will not trigger a garbage collection.
700 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
701 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
702 (S2 <= ref_reg && ref_reg <= S7) ||
703 (ref_reg == FP)) << ref_reg;
704 // "Compact" slow path, saving two moves.
705 //
706 // Instead of using the standard runtime calling convention (input
707 // and output in A0 and V0 respectively):
708 //
709 // A0 <- ref
710 // V0 <- ReadBarrierMark(A0)
711 // ref <- V0
712 //
713 // we just use rX (the register containing `ref`) as input and output
714 // of a dedicated entrypoint:
715 //
716 // rX <- ReadBarrierMarkRegX(rX)
717 //
718 int32_t entry_point_offset =
719 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
720 // This runtime call does not require a stack map.
721 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
722 instruction_,
723 this,
724 /* direct */ false);
725
726 // If the new reference is different from the old reference,
727 // update the field in the holder (`*(obj_ + field_offset_)`).
728 //
729 // Note that this field could also hold a different object, if
730 // another thread had concurrently changed it. In that case, the
731 // the compare-and-set (CAS) loop below would abort, leaving the
732 // field as-is.
733 MipsLabel done;
734 __ Beq(temp1_, ref_reg, &done);
735
736 // Update the the holder's field atomically. This may fail if
737 // mutator updates before us, but it's OK. This is achieved
738 // using a strong compare-and-set (CAS) operation with relaxed
739 // memory synchronization ordering, where the expected value is
740 // the old reference and the desired value is the new reference.
741
742 // Convenience aliases.
743 Register base = obj_;
744 // The UnsafeCASObject intrinsic uses a register pair as field
745 // offset ("long offset"), of which only the low part contains
746 // data.
747 Register offset = field_offset_.AsRegisterPairLow<Register>();
748 Register expected = temp1_;
749 Register value = ref_reg;
750 Register tmp_ptr = TMP; // Pointer to actual memory.
751 Register tmp = AT; // Value in memory.
752
753 __ Addu(tmp_ptr, base, offset);
754
755 if (kPoisonHeapReferences) {
756 __ PoisonHeapReference(expected);
757 // Do not poison `value` if it is the same register as
758 // `expected`, which has just been poisoned.
759 if (value != expected) {
760 __ PoisonHeapReference(value);
761 }
762 }
763
764 // do {
765 // tmp = [r_ptr] - expected;
766 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
767
768 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
769 MipsLabel loop_head, exit_loop;
770 __ Bind(&loop_head);
771 if (is_r6) {
772 __ LlR6(tmp, tmp_ptr);
773 } else {
774 __ LlR2(tmp, tmp_ptr);
775 }
776 __ Bne(tmp, expected, &exit_loop);
777 __ Move(tmp, value);
778 if (is_r6) {
779 __ ScR6(tmp, tmp_ptr);
780 } else {
781 __ ScR2(tmp, tmp_ptr);
782 }
783 __ Beqz(tmp, &loop_head);
784 __ Bind(&exit_loop);
785
786 if (kPoisonHeapReferences) {
787 __ UnpoisonHeapReference(expected);
788 // Do not unpoison `value` if it is the same register as
789 // `expected`, which has just been unpoisoned.
790 if (value != expected) {
791 __ UnpoisonHeapReference(value);
792 }
793 }
794
795 __ Bind(&done);
796 __ B(GetExitLabel());
797 }
798
799 private:
800 // The location (register) of the marked object reference.
801 const Location ref_;
802 // The register containing the object holding the marked object reference field.
803 const Register obj_;
804 // The location of the offset of the marked reference field within `obj_`.
805 Location field_offset_;
806
807 const Register temp1_;
808
809 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
810};
811
812// Slow path generating a read barrier for a heap reference.
813class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
814 public:
815 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
816 Location out,
817 Location ref,
818 Location obj,
819 uint32_t offset,
820 Location index)
821 : SlowPathCodeMIPS(instruction),
822 out_(out),
823 ref_(ref),
824 obj_(obj),
825 offset_(offset),
826 index_(index) {
827 DCHECK(kEmitCompilerReadBarrier);
828 // If `obj` is equal to `out` or `ref`, it means the initial object
829 // has been overwritten by (or after) the heap object reference load
830 // to be instrumented, e.g.:
831 //
832 // __ LoadFromOffset(kLoadWord, out, out, offset);
833 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
834 //
835 // In that case, we have lost the information about the original
836 // object, and the emitted read barrier cannot work properly.
837 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
838 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
839 }
840
841 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
842 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
843 LocationSummary* locations = instruction_->GetLocations();
844 Register reg_out = out_.AsRegister<Register>();
845 DCHECK(locations->CanCall());
846 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
847 DCHECK(instruction_->IsInstanceFieldGet() ||
848 instruction_->IsStaticFieldGet() ||
849 instruction_->IsArrayGet() ||
850 instruction_->IsInstanceOf() ||
851 instruction_->IsCheckCast() ||
852 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
853 << "Unexpected instruction in read barrier for heap reference slow path: "
854 << instruction_->DebugName();
855
856 __ Bind(GetEntryLabel());
857 SaveLiveRegisters(codegen, locations);
858
859 // We may have to change the index's value, but as `index_` is a
860 // constant member (like other "inputs" of this slow path),
861 // introduce a copy of it, `index`.
862 Location index = index_;
863 if (index_.IsValid()) {
864 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
865 if (instruction_->IsArrayGet()) {
866 // Compute the actual memory offset and store it in `index`.
867 Register index_reg = index_.AsRegister<Register>();
868 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
869 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
870 // We are about to change the value of `index_reg` (see the
871 // calls to art::mips::MipsAssembler::Sll and
872 // art::mips::MipsAssembler::Addiu32 below), but it has
873 // not been saved by the previous call to
874 // art::SlowPathCode::SaveLiveRegisters, as it is a
875 // callee-save register --
876 // art::SlowPathCode::SaveLiveRegisters does not consider
877 // callee-save registers, as it has been designed with the
878 // assumption that callee-save registers are supposed to be
879 // handled by the called function. So, as a callee-save
880 // register, `index_reg` _would_ eventually be saved onto
881 // the stack, but it would be too late: we would have
882 // changed its value earlier. Therefore, we manually save
883 // it here into another freely available register,
884 // `free_reg`, chosen of course among the caller-save
885 // registers (as a callee-save `free_reg` register would
886 // exhibit the same problem).
887 //
888 // Note we could have requested a temporary register from
889 // the register allocator instead; but we prefer not to, as
890 // this is a slow path, and we know we can find a
891 // caller-save register that is available.
892 Register free_reg = FindAvailableCallerSaveRegister(codegen);
893 __ Move(free_reg, index_reg);
894 index_reg = free_reg;
895 index = Location::RegisterLocation(index_reg);
896 } else {
897 // The initial register stored in `index_` has already been
898 // saved in the call to art::SlowPathCode::SaveLiveRegisters
899 // (as it is not a callee-save register), so we can freely
900 // use it.
901 }
902 // Shifting the index value contained in `index_reg` by the scale
903 // factor (2) cannot overflow in practice, as the runtime is
904 // unable to allocate object arrays with a size larger than
905 // 2^26 - 1 (that is, 2^28 - 4 bytes).
906 __ Sll(index_reg, index_reg, TIMES_4);
907 static_assert(
908 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
909 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
910 __ Addiu32(index_reg, index_reg, offset_);
911 } else {
912 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
913 // intrinsics, `index_` is not shifted by a scale factor of 2
914 // (as in the case of ArrayGet), as it is actually an offset
915 // to an object field within an object.
916 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
917 DCHECK(instruction_->GetLocations()->Intrinsified());
918 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
919 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
920 << instruction_->AsInvoke()->GetIntrinsic();
921 DCHECK_EQ(offset_, 0U);
922 DCHECK(index_.IsRegisterPair());
923 // UnsafeGet's offset location is a register pair, the low
924 // part contains the correct offset.
925 index = index_.ToLow();
926 }
927 }
928
929 // We're moving two or three locations to locations that could
930 // overlap, so we need a parallel move resolver.
931 InvokeRuntimeCallingConvention calling_convention;
932 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
933 parallel_move.AddMove(ref_,
934 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
935 Primitive::kPrimNot,
936 nullptr);
937 parallel_move.AddMove(obj_,
938 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
939 Primitive::kPrimNot,
940 nullptr);
941 if (index.IsValid()) {
942 parallel_move.AddMove(index,
943 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
944 Primitive::kPrimInt,
945 nullptr);
946 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
947 } else {
948 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
949 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
950 }
951 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
952 instruction_,
953 instruction_->GetDexPc(),
954 this);
955 CheckEntrypointTypes<
956 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
957 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
958
959 RestoreLiveRegisters(codegen, locations);
960 __ B(GetExitLabel());
961 }
962
963 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
964
965 private:
966 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
967 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
968 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
969 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
970 if (i != ref &&
971 i != obj &&
972 !codegen->IsCoreCalleeSaveRegister(i) &&
973 !codegen->IsBlockedCoreRegister(i)) {
974 return static_cast<Register>(i);
975 }
976 }
977 // We shall never fail to find a free caller-save register, as
978 // there are more than two core caller-save registers on MIPS
979 // (meaning it is possible to find one which is different from
980 // `ref` and `obj`).
981 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
982 LOG(FATAL) << "Could not find a free caller-save register";
983 UNREACHABLE();
984 }
985
986 const Location out_;
987 const Location ref_;
988 const Location obj_;
989 const uint32_t offset_;
990 // An additional location containing an index to an array.
991 // Only used for HArrayGet and the UnsafeGetObject &
992 // UnsafeGetObjectVolatile intrinsics.
993 const Location index_;
994
995 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
996};
997
998// Slow path generating a read barrier for a GC root.
999class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1000 public:
1001 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1002 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1003 DCHECK(kEmitCompilerReadBarrier);
1004 }
1005
1006 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1007 LocationSummary* locations = instruction_->GetLocations();
1008 Register reg_out = out_.AsRegister<Register>();
1009 DCHECK(locations->CanCall());
1010 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1011 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1012 << "Unexpected instruction in read barrier for GC root slow path: "
1013 << instruction_->DebugName();
1014
1015 __ Bind(GetEntryLabel());
1016 SaveLiveRegisters(codegen, locations);
1017
1018 InvokeRuntimeCallingConvention calling_convention;
1019 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
1020 mips_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
1021 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1022 instruction_,
1023 instruction_->GetDexPc(),
1024 this);
1025 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1026 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
1027
1028 RestoreLiveRegisters(codegen, locations);
1029 __ B(GetExitLabel());
1030 }
1031
1032 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1033
1034 private:
1035 const Location out_;
1036 const Location root_;
1037
1038 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1039};
1040
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001041CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1042 const MipsInstructionSetFeatures& isa_features,
1043 const CompilerOptions& compiler_options,
1044 OptimizingCompilerStats* stats)
1045 : CodeGenerator(graph,
1046 kNumberOfCoreRegisters,
1047 kNumberOfFRegisters,
1048 kNumberOfRegisterPairs,
1049 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1050 arraysize(kCoreCalleeSaves)),
1051 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1052 arraysize(kFpuCalleeSaves)),
1053 compiler_options,
1054 stats),
1055 block_labels_(nullptr),
1056 location_builder_(graph, this),
1057 instruction_visitor_(graph, this),
1058 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001059 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001060 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001061 uint32_literals_(std::less<uint32_t>(),
1062 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001063 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001064 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001065 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001066 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001067 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001068 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1069 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001070 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001071 // Save RA (containing the return address) to mimic Quick.
1072 AddAllocatedRegister(Location::RegisterLocation(RA));
1073}
1074
1075#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001076// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1077#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001078#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001079
1080void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1081 // Ensure that we fix up branches.
1082 __ FinalizeCode();
1083
1084 // Adjust native pc offsets in stack maps.
1085 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001086 uint32_t old_position =
1087 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001088 uint32_t new_position = __ GetAdjustedPosition(old_position);
1089 DCHECK_GE(new_position, old_position);
1090 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1091 }
1092
1093 // Adjust pc offsets for the disassembly information.
1094 if (disasm_info_ != nullptr) {
1095 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1096 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1097 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1098 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1099 it.second.start = __ GetAdjustedPosition(it.second.start);
1100 it.second.end = __ GetAdjustedPosition(it.second.end);
1101 }
1102 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1103 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1104 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1105 }
1106 }
1107
1108 CodeGenerator::Finalize(allocator);
1109}
1110
1111MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1112 return codegen_->GetAssembler();
1113}
1114
1115void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1116 DCHECK_LT(index, moves_.size());
1117 MoveOperands* move = moves_[index];
1118 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1119}
1120
1121void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1122 DCHECK_LT(index, moves_.size());
1123 MoveOperands* move = moves_[index];
1124 Primitive::Type type = move->GetType();
1125 Location loc1 = move->GetDestination();
1126 Location loc2 = move->GetSource();
1127
1128 DCHECK(!loc1.IsConstant());
1129 DCHECK(!loc2.IsConstant());
1130
1131 if (loc1.Equals(loc2)) {
1132 return;
1133 }
1134
1135 if (loc1.IsRegister() && loc2.IsRegister()) {
1136 // Swap 2 GPRs.
1137 Register r1 = loc1.AsRegister<Register>();
1138 Register r2 = loc2.AsRegister<Register>();
1139 __ Move(TMP, r2);
1140 __ Move(r2, r1);
1141 __ Move(r1, TMP);
1142 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1143 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1144 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1145 if (type == Primitive::kPrimFloat) {
1146 __ MovS(FTMP, f2);
1147 __ MovS(f2, f1);
1148 __ MovS(f1, FTMP);
1149 } else {
1150 DCHECK_EQ(type, Primitive::kPrimDouble);
1151 __ MovD(FTMP, f2);
1152 __ MovD(f2, f1);
1153 __ MovD(f1, FTMP);
1154 }
1155 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1156 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1157 // Swap FPR and GPR.
1158 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1159 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1160 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001161 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001162 __ Move(TMP, r2);
1163 __ Mfc1(r2, f1);
1164 __ Mtc1(TMP, f1);
1165 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1166 // Swap 2 GPR register pairs.
1167 Register r1 = loc1.AsRegisterPairLow<Register>();
1168 Register r2 = loc2.AsRegisterPairLow<Register>();
1169 __ Move(TMP, r2);
1170 __ Move(r2, r1);
1171 __ Move(r1, TMP);
1172 r1 = loc1.AsRegisterPairHigh<Register>();
1173 r2 = loc2.AsRegisterPairHigh<Register>();
1174 __ Move(TMP, r2);
1175 __ Move(r2, r1);
1176 __ Move(r1, TMP);
1177 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1178 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1179 // Swap FPR and GPR register pair.
1180 DCHECK_EQ(type, Primitive::kPrimDouble);
1181 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1182 : loc2.AsFpuRegister<FRegister>();
1183 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1184 : loc2.AsRegisterPairLow<Register>();
1185 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1186 : loc2.AsRegisterPairHigh<Register>();
1187 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1188 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1189 // unpredictable and the following mfch1 will fail.
1190 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001191 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001192 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001193 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001194 __ Move(r2_l, TMP);
1195 __ Move(r2_h, AT);
1196 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1197 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1198 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1199 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001200 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1201 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001202 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1203 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001204 __ Move(TMP, reg);
1205 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1206 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1207 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1208 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1209 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1210 : loc2.AsRegisterPairLow<Register>();
1211 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1212 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001213 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001214 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1215 : loc2.GetHighStackIndex(kMipsWordSize);
1216 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001217 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001218 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001219 __ Move(TMP, reg_h);
1220 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1221 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001222 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1223 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1224 : loc2.AsFpuRegister<FRegister>();
1225 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1226 if (type == Primitive::kPrimFloat) {
1227 __ MovS(FTMP, reg);
1228 __ LoadSFromOffset(reg, SP, offset);
1229 __ StoreSToOffset(FTMP, SP, offset);
1230 } else {
1231 DCHECK_EQ(type, Primitive::kPrimDouble);
1232 __ MovD(FTMP, reg);
1233 __ LoadDFromOffset(reg, SP, offset);
1234 __ StoreDToOffset(FTMP, SP, offset);
1235 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236 } else {
1237 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1238 }
1239}
1240
1241void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1242 __ Pop(static_cast<Register>(reg));
1243}
1244
1245void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1246 __ Push(static_cast<Register>(reg));
1247}
1248
1249void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1250 // Allocate a scratch register other than TMP, if available.
1251 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1252 // automatically unspilled when the scratch scope object is destroyed).
1253 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1254 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1255 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1256 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1257 __ LoadFromOffset(kLoadWord,
1258 Register(ensure_scratch.GetRegister()),
1259 SP,
1260 index1 + stack_offset);
1261 __ LoadFromOffset(kLoadWord,
1262 TMP,
1263 SP,
1264 index2 + stack_offset);
1265 __ StoreToOffset(kStoreWord,
1266 Register(ensure_scratch.GetRegister()),
1267 SP,
1268 index2 + stack_offset);
1269 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1270 }
1271}
1272
Alexey Frunze73296a72016-06-03 22:51:46 -07001273void CodeGeneratorMIPS::ComputeSpillMask() {
1274 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1275 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1276 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1277 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1278 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1279 // within the stack frame.
1280 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1281 core_spill_mask_ |= (1 << ZERO);
1282 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001283}
1284
1285bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001286 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001287 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1288 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1289 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001290 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001291}
1292
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001293static dwarf::Reg DWARFReg(Register reg) {
1294 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1295}
1296
1297// TODO: mapping of floating-point registers to DWARF.
1298
1299void CodeGeneratorMIPS::GenerateFrameEntry() {
1300 __ Bind(&frame_entry_label_);
1301
1302 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1303
1304 if (do_overflow_check) {
1305 __ LoadFromOffset(kLoadWord,
1306 ZERO,
1307 SP,
1308 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1309 RecordPcInfo(nullptr, 0);
1310 }
1311
1312 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001313 CHECK_EQ(fpu_spill_mask_, 0u);
1314 CHECK_EQ(core_spill_mask_, 1u << RA);
1315 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 return;
1317 }
1318
1319 // Make sure the frame size isn't unreasonably large.
1320 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1321 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1322 }
1323
1324 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325
Alexey Frunze73296a72016-06-03 22:51:46 -07001326 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001327 __ IncreaseFrameSize(ofs);
1328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1330 Register reg = static_cast<Register>(MostSignificantBit(mask));
1331 mask ^= 1u << reg;
1332 ofs -= kMipsWordSize;
1333 // The ZERO register is only included for alignment.
1334 if (reg != ZERO) {
1335 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001336 __ cfi().RelOffset(DWARFReg(reg), ofs);
1337 }
1338 }
1339
Alexey Frunze73296a72016-06-03 22:51:46 -07001340 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1341 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1342 mask ^= 1u << reg;
1343 ofs -= kMipsDoublewordSize;
1344 __ StoreDToOffset(reg, SP, ofs);
1345 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001346 }
1347
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001348 // Save the current method if we need it. Note that we do not
1349 // do this in HCurrentMethod, as the instruction might have been removed
1350 // in the SSA graph.
1351 if (RequiresCurrentMethod()) {
1352 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1353 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001354
1355 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1356 // Initialize should deoptimize flag to 0.
1357 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1358 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001359}
1360
1361void CodeGeneratorMIPS::GenerateFrameExit() {
1362 __ cfi().RememberState();
1363
1364 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001365 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001366
Alexey Frunze73296a72016-06-03 22:51:46 -07001367 // For better instruction scheduling restore RA before other registers.
1368 uint32_t ofs = GetFrameSize();
1369 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1370 Register reg = static_cast<Register>(MostSignificantBit(mask));
1371 mask ^= 1u << reg;
1372 ofs -= kMipsWordSize;
1373 // The ZERO register is only included for alignment.
1374 if (reg != ZERO) {
1375 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001376 __ cfi().Restore(DWARFReg(reg));
1377 }
1378 }
1379
Alexey Frunze73296a72016-06-03 22:51:46 -07001380 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1381 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1382 mask ^= 1u << reg;
1383 ofs -= kMipsDoublewordSize;
1384 __ LoadDFromOffset(reg, SP, ofs);
1385 // TODO: __ cfi().Restore(DWARFReg(reg));
1386 }
1387
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001388 size_t frame_size = GetFrameSize();
1389 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1390 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1391 bool reordering = __ SetReorder(false);
1392 if (exchange) {
1393 __ Jr(RA);
1394 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1395 } else {
1396 __ DecreaseFrameSize(frame_size);
1397 __ Jr(RA);
1398 __ Nop(); // In delay slot.
1399 }
1400 __ SetReorder(reordering);
1401 } else {
1402 __ Jr(RA);
1403 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001404 }
1405
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406 __ cfi().RestoreState();
1407 __ cfi().DefCFAOffset(GetFrameSize());
1408}
1409
1410void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1411 __ Bind(GetLabelOf(block));
1412}
1413
1414void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1415 if (src.Equals(dst)) {
1416 return;
1417 }
1418
1419 if (src.IsConstant()) {
1420 MoveConstant(dst, src.GetConstant());
1421 } else {
1422 if (Primitive::Is64BitType(dst_type)) {
1423 Move64(dst, src);
1424 } else {
1425 Move32(dst, src);
1426 }
1427 }
1428}
1429
1430void CodeGeneratorMIPS::Move32(Location destination, Location source) {
1431 if (source.Equals(destination)) {
1432 return;
1433 }
1434
1435 if (destination.IsRegister()) {
1436 if (source.IsRegister()) {
1437 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1438 } else if (source.IsFpuRegister()) {
1439 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1440 } else {
1441 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1442 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
1443 }
1444 } else if (destination.IsFpuRegister()) {
1445 if (source.IsRegister()) {
1446 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1447 } else if (source.IsFpuRegister()) {
1448 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1449 } else {
1450 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1451 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1452 }
1453 } else {
1454 DCHECK(destination.IsStackSlot()) << destination;
1455 if (source.IsRegister()) {
1456 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
1457 } else if (source.IsFpuRegister()) {
1458 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1459 } else {
1460 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1461 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1462 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1463 }
1464 }
1465}
1466
1467void CodeGeneratorMIPS::Move64(Location destination, Location source) {
1468 if (source.Equals(destination)) {
1469 return;
1470 }
1471
1472 if (destination.IsRegisterPair()) {
1473 if (source.IsRegisterPair()) {
1474 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1475 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1476 } else if (source.IsFpuRegister()) {
1477 Register dst_high = destination.AsRegisterPairHigh<Register>();
1478 Register dst_low = destination.AsRegisterPairLow<Register>();
1479 FRegister src = source.AsFpuRegister<FRegister>();
1480 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001481 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001482 } else {
1483 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1484 int32_t off = source.GetStackIndex();
1485 Register r = destination.AsRegisterPairLow<Register>();
1486 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1487 }
1488 } else if (destination.IsFpuRegister()) {
1489 if (source.IsRegisterPair()) {
1490 FRegister dst = destination.AsFpuRegister<FRegister>();
1491 Register src_high = source.AsRegisterPairHigh<Register>();
1492 Register src_low = source.AsRegisterPairLow<Register>();
1493 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001494 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001495 } else if (source.IsFpuRegister()) {
1496 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1497 } else {
1498 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1499 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1500 }
1501 } else {
1502 DCHECK(destination.IsDoubleStackSlot()) << destination;
1503 int32_t off = destination.GetStackIndex();
1504 if (source.IsRegisterPair()) {
1505 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
1506 } else if (source.IsFpuRegister()) {
1507 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
1508 } else {
1509 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1510 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1511 __ StoreToOffset(kStoreWord, TMP, SP, off);
1512 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1513 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
1514 }
1515 }
1516}
1517
1518void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1519 if (c->IsIntConstant() || c->IsNullConstant()) {
1520 // Move 32 bit constant.
1521 int32_t value = GetInt32ValueOf(c);
1522 if (destination.IsRegister()) {
1523 Register dst = destination.AsRegister<Register>();
1524 __ LoadConst32(dst, value);
1525 } else {
1526 DCHECK(destination.IsStackSlot())
1527 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001528 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001529 }
1530 } else if (c->IsLongConstant()) {
1531 // Move 64 bit constant.
1532 int64_t value = GetInt64ValueOf(c);
1533 if (destination.IsRegisterPair()) {
1534 Register r_h = destination.AsRegisterPairHigh<Register>();
1535 Register r_l = destination.AsRegisterPairLow<Register>();
1536 __ LoadConst64(r_h, r_l, value);
1537 } else {
1538 DCHECK(destination.IsDoubleStackSlot())
1539 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001540 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001541 }
1542 } else if (c->IsFloatConstant()) {
1543 // Move 32 bit float constant.
1544 int32_t value = GetInt32ValueOf(c);
1545 if (destination.IsFpuRegister()) {
1546 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1547 } else {
1548 DCHECK(destination.IsStackSlot())
1549 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001550 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001551 }
1552 } else {
1553 // Move 64 bit double constant.
1554 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1555 int64_t value = GetInt64ValueOf(c);
1556 if (destination.IsFpuRegister()) {
1557 FRegister fd = destination.AsFpuRegister<FRegister>();
1558 __ LoadDConst64(fd, value, TMP);
1559 } else {
1560 DCHECK(destination.IsDoubleStackSlot())
1561 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001562 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001563 }
1564 }
1565}
1566
1567void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1568 DCHECK(destination.IsRegister());
1569 Register dst = destination.AsRegister<Register>();
1570 __ LoadConst32(dst, value);
1571}
1572
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001573void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1574 if (location.IsRegister()) {
1575 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001576 } else if (location.IsRegisterPair()) {
1577 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1578 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001579 } else {
1580 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1581 }
1582}
1583
Vladimir Markoaad75c62016-10-03 08:46:48 +00001584template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1585inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1586 const ArenaDeque<PcRelativePatchInfo>& infos,
1587 ArenaVector<LinkerPatch>* linker_patches) {
1588 for (const PcRelativePatchInfo& info : infos) {
1589 const DexFile& dex_file = info.target_dex_file;
1590 size_t offset_or_index = info.offset_or_index;
1591 DCHECK(info.high_label.IsBound());
1592 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1593 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1594 // the assembler's base label used for PC-relative addressing.
1595 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1596 ? __ GetLabelLocation(&info.pc_rel_label)
1597 : __ GetPcRelBaseLabelLocation();
1598 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1599 }
1600}
1601
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001602void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1603 DCHECK(linker_patches->empty());
1604 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001605 pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001606 pc_relative_method_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001607 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001608 type_bss_entry_patches_.size() +
1609 pc_relative_string_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001610 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001611 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1612 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001613 if (GetCompilerOptions().IsBootImage()) {
1614 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00001615 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001616 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1617 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001618 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1619 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001620 } else {
1621 DCHECK(pc_relative_method_patches_.empty());
1622 DCHECK(pc_relative_type_patches_.empty());
1623 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1624 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001625 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001626 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1627 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001628 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001629}
1630
Vladimir Marko65979462017-05-19 17:25:12 +01001631CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
1632 MethodReference target_method) {
1633 return NewPcRelativePatch(*target_method.dex_file,
1634 target_method.dex_method_index,
1635 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001636}
1637
1638CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001639 const DexFile& dex_file, dex::TypeIndex type_index) {
1640 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001641}
1642
Vladimir Marko1998cd02017-01-13 13:02:58 +00001643CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1644 const DexFile& dex_file, dex::TypeIndex type_index) {
1645 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1646}
1647
Vladimir Marko65979462017-05-19 17:25:12 +01001648CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1649 const DexFile& dex_file, dex::StringIndex string_index) {
1650 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
1651}
1652
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001653CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1654 const DexFile& dex_file, uint32_t element_offset) {
1655 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1656}
1657
1658CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1659 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1660 patches->emplace_back(dex_file, offset_or_index);
1661 return &patches->back();
1662}
1663
Alexey Frunze06a46c42016-07-19 15:00:40 -07001664Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1665 return map->GetOrCreate(
1666 value,
1667 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1668}
1669
Alexey Frunze06a46c42016-07-19 15:00:40 -07001670Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001671 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001672}
1673
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001674void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1675 Register out,
1676 Register base) {
Alexey Frunze6079dca2017-05-28 19:10:28 -07001677 DCHECK_NE(out, base);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001678 if (GetInstructionSetFeatures().IsR6()) {
1679 DCHECK_EQ(base, ZERO);
1680 __ Bind(&info->high_label);
1681 __ Bind(&info->pc_rel_label);
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 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001684 } else {
1685 // If base is ZERO, emit NAL to obtain the actual base.
1686 if (base == ZERO) {
1687 // Generate a dummy PC-relative call to obtain PC.
1688 __ Nal();
1689 }
1690 __ Bind(&info->high_label);
1691 __ Lui(out, /* placeholder */ 0x1234);
1692 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1693 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1694 if (base == ZERO) {
1695 __ Bind(&info->pc_rel_label);
1696 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001697 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001698 __ Addu(out, out, (base == ZERO) ? RA : base);
1699 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001700 // The immediately following instruction will add the sign-extended low half of the 32-bit
1701 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001702}
1703
Alexey Frunze627c1a02017-01-30 19:28:14 -08001704CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1705 const DexFile& dex_file,
1706 dex::StringIndex dex_index,
1707 Handle<mirror::String> handle) {
1708 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1709 reinterpret_cast64<uint64_t>(handle.GetReference()));
1710 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1711 return &jit_string_patches_.back();
1712}
1713
1714CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1715 const DexFile& dex_file,
1716 dex::TypeIndex dex_index,
1717 Handle<mirror::Class> handle) {
1718 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1719 reinterpret_cast64<uint64_t>(handle.GetReference()));
1720 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1721 return &jit_class_patches_.back();
1722}
1723
1724void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1725 const uint8_t* roots_data,
1726 const CodeGeneratorMIPS::JitPatchInfo& info,
1727 uint64_t index_in_table) const {
1728 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1729 uintptr_t address =
1730 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1731 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1732 // lui reg, addr32_high
1733 DCHECK_EQ(code[literal_offset + 0], 0x34);
1734 DCHECK_EQ(code[literal_offset + 1], 0x12);
1735 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1736 DCHECK_EQ(code[literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001737 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001738 DCHECK_EQ(code[literal_offset + 4], 0x78);
1739 DCHECK_EQ(code[literal_offset + 5], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001740 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001741 // lui reg, addr32_high
1742 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1743 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001744 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001745 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1746 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1747}
1748
1749void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1750 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001751 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1752 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001753 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001754 uint64_t index_in_table = it->second;
1755 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001756 }
1757 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001758 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1759 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001760 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001761 uint64_t index_in_table = it->second;
1762 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001763 }
1764}
1765
Goran Jakovljevice114da22016-12-26 14:21:43 +01001766void CodeGeneratorMIPS::MarkGCCard(Register object,
1767 Register value,
1768 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001769 MipsLabel done;
1770 Register card = AT;
1771 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001772 if (value_can_be_null) {
1773 __ Beqz(value, &done);
1774 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001775 __ LoadFromOffset(kLoadWord,
1776 card,
1777 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001778 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001779 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1780 __ Addu(temp, card, temp);
1781 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001782 if (value_can_be_null) {
1783 __ Bind(&done);
1784 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001785}
1786
David Brazdil58282f42016-01-14 12:45:10 +00001787void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001788 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1789 blocked_core_registers_[ZERO] = true;
1790 blocked_core_registers_[K0] = true;
1791 blocked_core_registers_[K1] = true;
1792 blocked_core_registers_[GP] = true;
1793 blocked_core_registers_[SP] = true;
1794 blocked_core_registers_[RA] = true;
1795
1796 // AT and TMP(T8) are used as temporary/scratch registers
1797 // (similar to how AT is used by MIPS assemblers).
1798 blocked_core_registers_[AT] = true;
1799 blocked_core_registers_[TMP] = true;
1800 blocked_fpu_registers_[FTMP] = true;
1801
1802 // Reserve suspend and thread registers.
1803 blocked_core_registers_[S0] = true;
1804 blocked_core_registers_[TR] = true;
1805
1806 // Reserve T9 for function calls
1807 blocked_core_registers_[T9] = true;
1808
1809 // Reserve odd-numbered FPU registers.
1810 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1811 blocked_fpu_registers_[i] = true;
1812 }
1813
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001814 if (GetGraph()->IsDebuggable()) {
1815 // Stubs do not save callee-save floating point registers. If the graph
1816 // is debuggable, we need to deal with these registers differently. For
1817 // now, just block them.
1818 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1819 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1820 }
1821 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001822}
1823
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001824size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1825 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1826 return kMipsWordSize;
1827}
1828
1829size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1830 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1831 return kMipsWordSize;
1832}
1833
1834size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1835 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1836 return kMipsDoublewordSize;
1837}
1838
1839size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1840 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1841 return kMipsDoublewordSize;
1842}
1843
1844void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001845 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001846}
1847
1848void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001849 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850}
1851
Serban Constantinescufca16662016-07-14 09:21:59 +01001852constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1853
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1855 HInstruction* instruction,
1856 uint32_t dex_pc,
1857 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001858 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001859 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1860 IsDirectEntrypoint(entrypoint));
1861 if (EntrypointRequiresStackMap(entrypoint)) {
1862 RecordPcInfo(instruction, dex_pc, slow_path);
1863 }
1864}
1865
1866void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1867 HInstruction* instruction,
1868 SlowPathCode* slow_path,
1869 bool direct) {
1870 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1871 GenerateInvokeRuntime(entry_point_offset, direct);
1872}
1873
1874void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001875 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001876 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001877 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001878 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 // Reserve argument space on stack (for $a0-$a3) for
1880 // entrypoints that directly reference native implementations.
1881 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001882 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001883 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001884 } else {
1885 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001886 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001887 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001888}
1889
1890void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1891 Register class_reg) {
1892 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1893 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1894 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1895 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1896 __ Sync(0);
1897 __ Bind(slow_path->GetExitLabel());
1898}
1899
1900void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1901 __ Sync(0); // Only stype 0 is supported.
1902}
1903
1904void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1905 HBasicBlock* successor) {
1906 SuspendCheckSlowPathMIPS* slow_path =
1907 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1908 codegen_->AddSlowPath(slow_path);
1909
1910 __ LoadFromOffset(kLoadUnsignedHalfword,
1911 TMP,
1912 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001913 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914 if (successor == nullptr) {
1915 __ Bnez(TMP, slow_path->GetEntryLabel());
1916 __ Bind(slow_path->GetReturnLabel());
1917 } else {
1918 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1919 __ B(slow_path->GetEntryLabel());
1920 // slow_path will return to GetLabelOf(successor).
1921 }
1922}
1923
1924InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1925 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001926 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001927 assembler_(codegen->GetAssembler()),
1928 codegen_(codegen) {}
1929
1930void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1931 DCHECK_EQ(instruction->InputCount(), 2U);
1932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1933 Primitive::Type type = instruction->GetResultType();
1934 switch (type) {
1935 case Primitive::kPrimInt: {
1936 locations->SetInAt(0, Location::RequiresRegister());
1937 HInstruction* right = instruction->InputAt(1);
1938 bool can_use_imm = false;
1939 if (right->IsConstant()) {
1940 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1941 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1942 can_use_imm = IsUint<16>(imm);
1943 } else if (instruction->IsAdd()) {
1944 can_use_imm = IsInt<16>(imm);
1945 } else {
1946 DCHECK(instruction->IsSub());
1947 can_use_imm = IsInt<16>(-imm);
1948 }
1949 }
1950 if (can_use_imm)
1951 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1952 else
1953 locations->SetInAt(1, Location::RequiresRegister());
1954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1955 break;
1956 }
1957
1958 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001960 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001962 break;
1963 }
1964
1965 case Primitive::kPrimFloat:
1966 case Primitive::kPrimDouble:
1967 DCHECK(instruction->IsAdd() || instruction->IsSub());
1968 locations->SetInAt(0, Location::RequiresFpuRegister());
1969 locations->SetInAt(1, Location::RequiresFpuRegister());
1970 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1971 break;
1972
1973 default:
1974 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1975 }
1976}
1977
1978void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1979 Primitive::Type type = instruction->GetType();
1980 LocationSummary* locations = instruction->GetLocations();
1981
1982 switch (type) {
1983 case Primitive::kPrimInt: {
1984 Register dst = locations->Out().AsRegister<Register>();
1985 Register lhs = locations->InAt(0).AsRegister<Register>();
1986 Location rhs_location = locations->InAt(1);
1987
1988 Register rhs_reg = ZERO;
1989 int32_t rhs_imm = 0;
1990 bool use_imm = rhs_location.IsConstant();
1991 if (use_imm) {
1992 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1993 } else {
1994 rhs_reg = rhs_location.AsRegister<Register>();
1995 }
1996
1997 if (instruction->IsAnd()) {
1998 if (use_imm)
1999 __ Andi(dst, lhs, rhs_imm);
2000 else
2001 __ And(dst, lhs, rhs_reg);
2002 } else if (instruction->IsOr()) {
2003 if (use_imm)
2004 __ Ori(dst, lhs, rhs_imm);
2005 else
2006 __ Or(dst, lhs, rhs_reg);
2007 } else if (instruction->IsXor()) {
2008 if (use_imm)
2009 __ Xori(dst, lhs, rhs_imm);
2010 else
2011 __ Xor(dst, lhs, rhs_reg);
2012 } else if (instruction->IsAdd()) {
2013 if (use_imm)
2014 __ Addiu(dst, lhs, rhs_imm);
2015 else
2016 __ Addu(dst, lhs, rhs_reg);
2017 } else {
2018 DCHECK(instruction->IsSub());
2019 if (use_imm)
2020 __ Addiu(dst, lhs, -rhs_imm);
2021 else
2022 __ Subu(dst, lhs, rhs_reg);
2023 }
2024 break;
2025 }
2026
2027 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2029 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2030 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2031 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002032 Location rhs_location = locations->InAt(1);
2033 bool use_imm = rhs_location.IsConstant();
2034 if (!use_imm) {
2035 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2036 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2037 if (instruction->IsAnd()) {
2038 __ And(dst_low, lhs_low, rhs_low);
2039 __ And(dst_high, lhs_high, rhs_high);
2040 } else if (instruction->IsOr()) {
2041 __ Or(dst_low, lhs_low, rhs_low);
2042 __ Or(dst_high, lhs_high, rhs_high);
2043 } else if (instruction->IsXor()) {
2044 __ Xor(dst_low, lhs_low, rhs_low);
2045 __ Xor(dst_high, lhs_high, rhs_high);
2046 } else if (instruction->IsAdd()) {
2047 if (lhs_low == rhs_low) {
2048 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2049 __ Slt(TMP, lhs_low, ZERO);
2050 __ Addu(dst_low, lhs_low, rhs_low);
2051 } else {
2052 __ Addu(dst_low, lhs_low, rhs_low);
2053 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2054 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2055 }
2056 __ Addu(dst_high, lhs_high, rhs_high);
2057 __ Addu(dst_high, dst_high, TMP);
2058 } else {
2059 DCHECK(instruction->IsSub());
2060 __ Sltu(TMP, lhs_low, rhs_low);
2061 __ Subu(dst_low, lhs_low, rhs_low);
2062 __ Subu(dst_high, lhs_high, rhs_high);
2063 __ Subu(dst_high, dst_high, TMP);
2064 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002065 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002066 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2067 if (instruction->IsOr()) {
2068 uint32_t low = Low32Bits(value);
2069 uint32_t high = High32Bits(value);
2070 if (IsUint<16>(low)) {
2071 if (dst_low != lhs_low || low != 0) {
2072 __ Ori(dst_low, lhs_low, low);
2073 }
2074 } else {
2075 __ LoadConst32(TMP, low);
2076 __ Or(dst_low, lhs_low, TMP);
2077 }
2078 if (IsUint<16>(high)) {
2079 if (dst_high != lhs_high || high != 0) {
2080 __ Ori(dst_high, lhs_high, high);
2081 }
2082 } else {
2083 if (high != low) {
2084 __ LoadConst32(TMP, high);
2085 }
2086 __ Or(dst_high, lhs_high, TMP);
2087 }
2088 } else if (instruction->IsXor()) {
2089 uint32_t low = Low32Bits(value);
2090 uint32_t high = High32Bits(value);
2091 if (IsUint<16>(low)) {
2092 if (dst_low != lhs_low || low != 0) {
2093 __ Xori(dst_low, lhs_low, low);
2094 }
2095 } else {
2096 __ LoadConst32(TMP, low);
2097 __ Xor(dst_low, lhs_low, TMP);
2098 }
2099 if (IsUint<16>(high)) {
2100 if (dst_high != lhs_high || high != 0) {
2101 __ Xori(dst_high, lhs_high, high);
2102 }
2103 } else {
2104 if (high != low) {
2105 __ LoadConst32(TMP, high);
2106 }
2107 __ Xor(dst_high, lhs_high, TMP);
2108 }
2109 } else if (instruction->IsAnd()) {
2110 uint32_t low = Low32Bits(value);
2111 uint32_t high = High32Bits(value);
2112 if (IsUint<16>(low)) {
2113 __ Andi(dst_low, lhs_low, low);
2114 } else if (low != 0xFFFFFFFF) {
2115 __ LoadConst32(TMP, low);
2116 __ And(dst_low, lhs_low, TMP);
2117 } else if (dst_low != lhs_low) {
2118 __ Move(dst_low, lhs_low);
2119 }
2120 if (IsUint<16>(high)) {
2121 __ Andi(dst_high, lhs_high, high);
2122 } else if (high != 0xFFFFFFFF) {
2123 if (high != low) {
2124 __ LoadConst32(TMP, high);
2125 }
2126 __ And(dst_high, lhs_high, TMP);
2127 } else if (dst_high != lhs_high) {
2128 __ Move(dst_high, lhs_high);
2129 }
2130 } else {
2131 if (instruction->IsSub()) {
2132 value = -value;
2133 } else {
2134 DCHECK(instruction->IsAdd());
2135 }
2136 int32_t low = Low32Bits(value);
2137 int32_t high = High32Bits(value);
2138 if (IsInt<16>(low)) {
2139 if (dst_low != lhs_low || low != 0) {
2140 __ Addiu(dst_low, lhs_low, low);
2141 }
2142 if (low != 0) {
2143 __ Sltiu(AT, dst_low, low);
2144 }
2145 } else {
2146 __ LoadConst32(TMP, low);
2147 __ Addu(dst_low, lhs_low, TMP);
2148 __ Sltu(AT, dst_low, TMP);
2149 }
2150 if (IsInt<16>(high)) {
2151 if (dst_high != lhs_high || high != 0) {
2152 __ Addiu(dst_high, lhs_high, high);
2153 }
2154 } else {
2155 if (high != low) {
2156 __ LoadConst32(TMP, high);
2157 }
2158 __ Addu(dst_high, lhs_high, TMP);
2159 }
2160 if (low != 0) {
2161 __ Addu(dst_high, dst_high, AT);
2162 }
2163 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002164 }
2165 break;
2166 }
2167
2168 case Primitive::kPrimFloat:
2169 case Primitive::kPrimDouble: {
2170 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2171 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2172 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2173 if (instruction->IsAdd()) {
2174 if (type == Primitive::kPrimFloat) {
2175 __ AddS(dst, lhs, rhs);
2176 } else {
2177 __ AddD(dst, lhs, rhs);
2178 }
2179 } else {
2180 DCHECK(instruction->IsSub());
2181 if (type == Primitive::kPrimFloat) {
2182 __ SubS(dst, lhs, rhs);
2183 } else {
2184 __ SubD(dst, lhs, rhs);
2185 }
2186 }
2187 break;
2188 }
2189
2190 default:
2191 LOG(FATAL) << "Unexpected binary operation type " << type;
2192 }
2193}
2194
2195void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002196 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002197
2198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2199 Primitive::Type type = instr->GetResultType();
2200 switch (type) {
2201 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002202 locations->SetInAt(0, Location::RequiresRegister());
2203 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2205 break;
2206 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002207 locations->SetInAt(0, Location::RequiresRegister());
2208 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2209 locations->SetOut(Location::RequiresRegister());
2210 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002211 default:
2212 LOG(FATAL) << "Unexpected shift type " << type;
2213 }
2214}
2215
2216static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2217
2218void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002219 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002220 LocationSummary* locations = instr->GetLocations();
2221 Primitive::Type type = instr->GetType();
2222
2223 Location rhs_location = locations->InAt(1);
2224 bool use_imm = rhs_location.IsConstant();
2225 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2226 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002227 const uint32_t shift_mask =
2228 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002229 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002230 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2231 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232
2233 switch (type) {
2234 case Primitive::kPrimInt: {
2235 Register dst = locations->Out().AsRegister<Register>();
2236 Register lhs = locations->InAt(0).AsRegister<Register>();
2237 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002238 if (shift_value == 0) {
2239 if (dst != lhs) {
2240 __ Move(dst, lhs);
2241 }
2242 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243 __ Sll(dst, lhs, shift_value);
2244 } else if (instr->IsShr()) {
2245 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002246 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002248 } else {
2249 if (has_ins_rotr) {
2250 __ Rotr(dst, lhs, shift_value);
2251 } else {
2252 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2253 __ Srl(dst, lhs, shift_value);
2254 __ Or(dst, dst, TMP);
2255 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002256 }
2257 } else {
2258 if (instr->IsShl()) {
2259 __ Sllv(dst, lhs, rhs_reg);
2260 } else if (instr->IsShr()) {
2261 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002262 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002263 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002264 } else {
2265 if (has_ins_rotr) {
2266 __ Rotrv(dst, lhs, rhs_reg);
2267 } else {
2268 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002269 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2270 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2271 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2272 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2273 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002274 __ Sllv(TMP, lhs, TMP);
2275 __ Srlv(dst, lhs, rhs_reg);
2276 __ Or(dst, dst, TMP);
2277 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278 }
2279 }
2280 break;
2281 }
2282
2283 case Primitive::kPrimLong: {
2284 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2285 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2286 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2287 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2288 if (use_imm) {
2289 if (shift_value == 0) {
2290 codegen_->Move64(locations->Out(), locations->InAt(0));
2291 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002292 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002293 if (instr->IsShl()) {
2294 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2295 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2296 __ Sll(dst_low, lhs_low, shift_value);
2297 } else if (instr->IsShr()) {
2298 __ Srl(dst_low, lhs_low, shift_value);
2299 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2300 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002301 } else if (instr->IsUShr()) {
2302 __ Srl(dst_low, lhs_low, shift_value);
2303 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2304 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002305 } else {
2306 __ Srl(dst_low, lhs_low, shift_value);
2307 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2308 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002309 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002310 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002311 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002312 if (instr->IsShl()) {
2313 __ Sll(dst_low, lhs_low, shift_value);
2314 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2315 __ Sll(dst_high, lhs_high, shift_value);
2316 __ Or(dst_high, dst_high, TMP);
2317 } else if (instr->IsShr()) {
2318 __ Sra(dst_high, lhs_high, shift_value);
2319 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2320 __ Srl(dst_low, lhs_low, shift_value);
2321 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002322 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002323 __ Srl(dst_high, lhs_high, shift_value);
2324 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2325 __ Srl(dst_low, lhs_low, shift_value);
2326 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002327 } else {
2328 __ Srl(TMP, lhs_low, shift_value);
2329 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2330 __ Or(dst_low, dst_low, TMP);
2331 __ Srl(TMP, lhs_high, shift_value);
2332 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2333 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002334 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002335 }
2336 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002337 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002338 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002339 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002340 __ Move(dst_low, ZERO);
2341 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002342 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002343 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002344 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002345 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002347 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002348 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002349 // 64-bit rotation by 32 is just a swap.
2350 __ Move(dst_low, lhs_high);
2351 __ Move(dst_high, lhs_low);
2352 } else {
2353 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002354 __ Srl(dst_low, lhs_high, shift_value_high);
2355 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2356 __ Srl(dst_high, lhs_low, shift_value_high);
2357 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002358 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002359 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2360 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002361 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002362 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2363 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002364 __ Or(dst_high, dst_high, TMP);
2365 }
2366 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002367 }
2368 }
2369 } else {
2370 MipsLabel done;
2371 if (instr->IsShl()) {
2372 __ Sllv(dst_low, lhs_low, rhs_reg);
2373 __ Nor(AT, ZERO, rhs_reg);
2374 __ Srl(TMP, lhs_low, 1);
2375 __ Srlv(TMP, TMP, AT);
2376 __ Sllv(dst_high, lhs_high, rhs_reg);
2377 __ Or(dst_high, dst_high, TMP);
2378 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2379 __ Beqz(TMP, &done);
2380 __ Move(dst_high, dst_low);
2381 __ Move(dst_low, ZERO);
2382 } else if (instr->IsShr()) {
2383 __ Srav(dst_high, lhs_high, rhs_reg);
2384 __ Nor(AT, ZERO, rhs_reg);
2385 __ Sll(TMP, lhs_high, 1);
2386 __ Sllv(TMP, TMP, AT);
2387 __ Srlv(dst_low, lhs_low, rhs_reg);
2388 __ Or(dst_low, dst_low, TMP);
2389 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2390 __ Beqz(TMP, &done);
2391 __ Move(dst_low, dst_high);
2392 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002393 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002394 __ Srlv(dst_high, lhs_high, rhs_reg);
2395 __ Nor(AT, ZERO, rhs_reg);
2396 __ Sll(TMP, lhs_high, 1);
2397 __ Sllv(TMP, TMP, AT);
2398 __ Srlv(dst_low, lhs_low, rhs_reg);
2399 __ Or(dst_low, dst_low, TMP);
2400 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2401 __ Beqz(TMP, &done);
2402 __ Move(dst_low, dst_high);
2403 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002404 } else {
2405 __ Nor(AT, ZERO, rhs_reg);
2406 __ Srlv(TMP, lhs_low, rhs_reg);
2407 __ Sll(dst_low, lhs_high, 1);
2408 __ Sllv(dst_low, dst_low, AT);
2409 __ Or(dst_low, dst_low, TMP);
2410 __ Srlv(TMP, lhs_high, rhs_reg);
2411 __ Sll(dst_high, lhs_low, 1);
2412 __ Sllv(dst_high, dst_high, AT);
2413 __ Or(dst_high, dst_high, TMP);
2414 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2415 __ Beqz(TMP, &done);
2416 __ Move(TMP, dst_high);
2417 __ Move(dst_high, dst_low);
2418 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 }
2420 __ Bind(&done);
2421 }
2422 break;
2423 }
2424
2425 default:
2426 LOG(FATAL) << "Unexpected shift operation type " << type;
2427 }
2428}
2429
2430void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2431 HandleBinaryOp(instruction);
2432}
2433
2434void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2435 HandleBinaryOp(instruction);
2436}
2437
2438void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2439 HandleBinaryOp(instruction);
2440}
2441
2442void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2443 HandleBinaryOp(instruction);
2444}
2445
2446void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002447 Primitive::Type type = instruction->GetType();
2448 bool object_array_get_with_read_barrier =
2449 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002450 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002451 new (GetGraph()->GetArena()) LocationSummary(instruction,
2452 object_array_get_with_read_barrier
2453 ? LocationSummary::kCallOnSlowPath
2454 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002455 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2456 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2457 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002458 locations->SetInAt(0, Location::RequiresRegister());
2459 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002460 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002461 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2462 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002463 // The output overlaps in the case of an object array get with
2464 // read barriers enabled: we do not want the move to overwrite the
2465 // array's location, as we need it to emit the read barrier.
2466 locations->SetOut(Location::RequiresRegister(),
2467 object_array_get_with_read_barrier
2468 ? Location::kOutputOverlap
2469 : Location::kNoOutputOverlap);
2470 }
2471 // We need a temporary register for the read barrier marking slow
2472 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2473 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2474 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002475 }
2476}
2477
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002478static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2479 auto null_checker = [codegen, instruction]() {
2480 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002481 };
2482 return null_checker;
2483}
2484
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002485void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2486 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002487 Location obj_loc = locations->InAt(0);
2488 Register obj = obj_loc.AsRegister<Register>();
2489 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002490 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002491 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002492 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002493
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002494 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002495 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2496 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002497 switch (type) {
2498 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002499 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002500 if (index.IsConstant()) {
2501 size_t offset =
2502 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002503 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002504 } else {
2505 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002506 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 }
2508 break;
2509 }
2510
2511 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002512 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 if (index.IsConstant()) {
2514 size_t offset =
2515 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002516 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002517 } else {
2518 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002519 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002520 }
2521 break;
2522 }
2523
2524 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002525 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002526 if (index.IsConstant()) {
2527 size_t offset =
2528 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002529 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002530 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002531 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002532 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002533 }
2534 break;
2535 }
2536
2537 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002538 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002539 if (maybe_compressed_char_at) {
2540 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2541 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2542 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2543 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2544 "Expecting 0=compressed, 1=uncompressed");
2545 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002546 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002547 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2548 if (maybe_compressed_char_at) {
2549 MipsLabel uncompressed_load, done;
2550 __ Bnez(TMP, &uncompressed_load);
2551 __ LoadFromOffset(kLoadUnsignedByte,
2552 out,
2553 obj,
2554 data_offset + (const_index << TIMES_1));
2555 __ B(&done);
2556 __ Bind(&uncompressed_load);
2557 __ LoadFromOffset(kLoadUnsignedHalfword,
2558 out,
2559 obj,
2560 data_offset + (const_index << TIMES_2));
2561 __ Bind(&done);
2562 } else {
2563 __ LoadFromOffset(kLoadUnsignedHalfword,
2564 out,
2565 obj,
2566 data_offset + (const_index << TIMES_2),
2567 null_checker);
2568 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002569 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002570 Register index_reg = index.AsRegister<Register>();
2571 if (maybe_compressed_char_at) {
2572 MipsLabel uncompressed_load, done;
2573 __ Bnez(TMP, &uncompressed_load);
2574 __ Addu(TMP, obj, index_reg);
2575 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2576 __ B(&done);
2577 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002578 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002579 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2580 __ Bind(&done);
2581 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002582 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002583 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2584 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002585 }
2586 break;
2587 }
2588
Alexey Frunze15958152017-02-09 19:08:30 -08002589 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002590 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002591 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592 if (index.IsConstant()) {
2593 size_t offset =
2594 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002595 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002597 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002598 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002599 }
2600 break;
2601 }
2602
Alexey Frunze15958152017-02-09 19:08:30 -08002603 case Primitive::kPrimNot: {
2604 static_assert(
2605 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2606 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2607 // /* HeapReference<Object> */ out =
2608 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2609 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2610 Location temp = locations->GetTemp(0);
2611 // Note that a potential implicit null check is handled in this
2612 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2613 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2614 out_loc,
2615 obj,
2616 data_offset,
2617 index,
2618 temp,
2619 /* needs_null_check */ true);
2620 } else {
2621 Register out = out_loc.AsRegister<Register>();
2622 if (index.IsConstant()) {
2623 size_t offset =
2624 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2625 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2626 // If read barriers are enabled, emit read barriers other than
2627 // Baker's using a slow path (and also unpoison the loaded
2628 // reference, if heap poisoning is enabled).
2629 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2630 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002631 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002632 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2633 // If read barriers are enabled, emit read barriers other than
2634 // Baker's using a slow path (and also unpoison the loaded
2635 // reference, if heap poisoning is enabled).
2636 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2637 out_loc,
2638 out_loc,
2639 obj_loc,
2640 data_offset,
2641 index);
2642 }
2643 }
2644 break;
2645 }
2646
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002647 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002648 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002649 if (index.IsConstant()) {
2650 size_t offset =
2651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002652 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002653 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002654 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002655 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 }
2657 break;
2658 }
2659
2660 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002661 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002662 if (index.IsConstant()) {
2663 size_t offset =
2664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002665 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002666 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002667 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002668 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 }
2670 break;
2671 }
2672
2673 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002674 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002675 if (index.IsConstant()) {
2676 size_t offset =
2677 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002678 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002679 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002680 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002681 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002682 }
2683 break;
2684 }
2685
2686 case Primitive::kPrimVoid:
2687 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2688 UNREACHABLE();
2689 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690}
2691
2692void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2694 locations->SetInAt(0, Location::RequiresRegister());
2695 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2696}
2697
2698void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2699 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002700 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701 Register obj = locations->InAt(0).AsRegister<Register>();
2702 Register out = locations->Out().AsRegister<Register>();
2703 __ LoadFromOffset(kLoadWord, out, obj, offset);
2704 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002705 // Mask out compression flag from String's array length.
2706 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2707 __ Srl(out, out, 1u);
2708 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709}
2710
Alexey Frunzef58b2482016-09-02 22:14:06 -07002711Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2712 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2713 ? Location::ConstantLocation(instruction->AsConstant())
2714 : Location::RequiresRegister();
2715}
2716
2717Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2718 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2719 // We can store a non-zero float or double constant without first loading it into the FPU,
2720 // but we should only prefer this if the constant has a single use.
2721 if (instruction->IsConstant() &&
2722 (instruction->AsConstant()->IsZeroBitPattern() ||
2723 instruction->GetUses().HasExactlyOneElement())) {
2724 return Location::ConstantLocation(instruction->AsConstant());
2725 // Otherwise fall through and require an FPU register for the constant.
2726 }
2727 return Location::RequiresFpuRegister();
2728}
2729
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002730void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002731 Primitive::Type value_type = instruction->GetComponentType();
2732
2733 bool needs_write_barrier =
2734 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2735 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2736
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2738 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002739 may_need_runtime_call_for_type_check ?
2740 LocationSummary::kCallOnSlowPath :
2741 LocationSummary::kNoCall);
2742
2743 locations->SetInAt(0, Location::RequiresRegister());
2744 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2745 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2746 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002747 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002748 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2749 }
2750 if (needs_write_barrier) {
2751 // Temporary register for the write barrier.
2752 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002753 }
2754}
2755
2756void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2757 LocationSummary* locations = instruction->GetLocations();
2758 Register obj = locations->InAt(0).AsRegister<Register>();
2759 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002760 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002761 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002762 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002763 bool needs_write_barrier =
2764 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002765 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002766 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002767
2768 switch (value_type) {
2769 case Primitive::kPrimBoolean:
2770 case Primitive::kPrimByte: {
2771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002772 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002773 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002774 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002775 __ Addu(base_reg, obj, index.AsRegister<Register>());
2776 }
2777 if (value_location.IsConstant()) {
2778 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2779 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2780 } else {
2781 Register value = value_location.AsRegister<Register>();
2782 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002783 }
2784 break;
2785 }
2786
2787 case Primitive::kPrimShort:
2788 case Primitive::kPrimChar: {
2789 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002790 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002791 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002792 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002793 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002794 }
2795 if (value_location.IsConstant()) {
2796 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2797 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2798 } else {
2799 Register value = value_location.AsRegister<Register>();
2800 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002801 }
2802 break;
2803 }
2804
Alexey Frunze15958152017-02-09 19:08:30 -08002805 case Primitive::kPrimInt: {
2806 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2807 if (index.IsConstant()) {
2808 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2809 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002810 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002811 }
2812 if (value_location.IsConstant()) {
2813 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2814 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2815 } else {
2816 Register value = value_location.AsRegister<Register>();
2817 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2818 }
2819 break;
2820 }
2821
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002823 if (value_location.IsConstant()) {
2824 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002826 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002827 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002828 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002829 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002830 }
Alexey Frunze15958152017-02-09 19:08:30 -08002831 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2832 DCHECK_EQ(value, 0);
2833 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2834 DCHECK(!needs_write_barrier);
2835 DCHECK(!may_need_runtime_call_for_type_check);
2836 break;
2837 }
2838
2839 DCHECK(needs_write_barrier);
2840 Register value = value_location.AsRegister<Register>();
2841 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2842 Register temp2 = TMP; // Doesn't need to survive slow path.
2843 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2844 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2845 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2846 MipsLabel done;
2847 SlowPathCodeMIPS* slow_path = nullptr;
2848
2849 if (may_need_runtime_call_for_type_check) {
2850 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2851 codegen_->AddSlowPath(slow_path);
2852 if (instruction->GetValueCanBeNull()) {
2853 MipsLabel non_zero;
2854 __ Bnez(value, &non_zero);
2855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2856 if (index.IsConstant()) {
2857 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002858 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002859 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002860 }
Alexey Frunze15958152017-02-09 19:08:30 -08002861 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2862 __ B(&done);
2863 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002864 }
Alexey Frunze15958152017-02-09 19:08:30 -08002865
2866 // Note that when read barriers are enabled, the type checks
2867 // are performed without read barriers. This is fine, even in
2868 // the case where a class object is in the from-space after
2869 // the flip, as a comparison involving such a type would not
2870 // produce a false positive; it may of course produce a false
2871 // negative, in which case we would take the ArraySet slow
2872 // path.
2873
2874 // /* HeapReference<Class> */ temp1 = obj->klass_
2875 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2876 __ MaybeUnpoisonHeapReference(temp1);
2877
2878 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2879 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2880 // /* HeapReference<Class> */ temp2 = value->klass_
2881 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2882 // If heap poisoning is enabled, no need to unpoison `temp1`
2883 // nor `temp2`, as we are comparing two poisoned references.
2884
2885 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2886 MipsLabel do_put;
2887 __ Beq(temp1, temp2, &do_put);
2888 // If heap poisoning is enabled, the `temp1` reference has
2889 // not been unpoisoned yet; unpoison it now.
2890 __ MaybeUnpoisonHeapReference(temp1);
2891
2892 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2893 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2894 // If heap poisoning is enabled, no need to unpoison
2895 // `temp1`, as we are comparing against null below.
2896 __ Bnez(temp1, slow_path->GetEntryLabel());
2897 __ Bind(&do_put);
2898 } else {
2899 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2900 }
2901 }
2902
2903 Register source = value;
2904 if (kPoisonHeapReferences) {
2905 // Note that in the case where `value` is a null reference,
2906 // we do not enter this block, as a null reference does not
2907 // need poisoning.
2908 __ Move(temp1, value);
2909 __ PoisonHeapReference(temp1);
2910 source = temp1;
2911 }
2912
2913 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2914 if (index.IsConstant()) {
2915 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002916 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002917 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002918 }
2919 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2920
2921 if (!may_need_runtime_call_for_type_check) {
2922 codegen_->MaybeRecordImplicitNullCheck(instruction);
2923 }
2924
2925 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2926
2927 if (done.IsLinked()) {
2928 __ Bind(&done);
2929 }
2930
2931 if (slow_path != nullptr) {
2932 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 }
2934 break;
2935 }
2936
2937 case Primitive::kPrimLong: {
2938 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002939 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002940 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002941 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002942 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002943 }
2944 if (value_location.IsConstant()) {
2945 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2946 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2947 } else {
2948 Register value = value_location.AsRegisterPairLow<Register>();
2949 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002950 }
2951 break;
2952 }
2953
2954 case Primitive::kPrimFloat: {
2955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002956 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002957 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002958 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002959 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002960 }
2961 if (value_location.IsConstant()) {
2962 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2963 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2964 } else {
2965 FRegister value = value_location.AsFpuRegister<FRegister>();
2966 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002967 }
2968 break;
2969 }
2970
2971 case Primitive::kPrimDouble: {
2972 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002974 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002975 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002976 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002977 }
2978 if (value_location.IsConstant()) {
2979 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2980 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2981 } else {
2982 FRegister value = value_location.AsFpuRegister<FRegister>();
2983 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002984 }
2985 break;
2986 }
2987
2988 case Primitive::kPrimVoid:
2989 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2990 UNREACHABLE();
2991 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002992}
2993
2994void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002995 RegisterSet caller_saves = RegisterSet::Empty();
2996 InvokeRuntimeCallingConvention calling_convention;
2997 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2998 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2999 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003000 locations->SetInAt(0, Location::RequiresRegister());
3001 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003002}
3003
3004void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3005 LocationSummary* locations = instruction->GetLocations();
3006 BoundsCheckSlowPathMIPS* slow_path =
3007 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3008 codegen_->AddSlowPath(slow_path);
3009
3010 Register index = locations->InAt(0).AsRegister<Register>();
3011 Register length = locations->InAt(1).AsRegister<Register>();
3012
3013 // length is limited by the maximum positive signed 32-bit integer.
3014 // Unsigned comparison of length and index checks for index < 0
3015 // and for length <= index simultaneously.
3016 __ Bgeu(index, length, slow_path->GetEntryLabel());
3017}
3018
Alexey Frunze15958152017-02-09 19:08:30 -08003019// Temp is used for read barrier.
3020static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3021 if (kEmitCompilerReadBarrier &&
3022 (kUseBakerReadBarrier ||
3023 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3024 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3025 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3026 return 1;
3027 }
3028 return 0;
3029}
3030
3031// Extra temp is used for read barrier.
3032static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3033 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3034}
3035
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003036void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003037 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3038 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3039
3040 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3041 switch (type_check_kind) {
3042 case TypeCheckKind::kExactCheck:
3043 case TypeCheckKind::kAbstractClassCheck:
3044 case TypeCheckKind::kClassHierarchyCheck:
3045 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003046 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003047 ? LocationSummary::kCallOnSlowPath
3048 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3049 break;
3050 case TypeCheckKind::kArrayCheck:
3051 case TypeCheckKind::kUnresolvedCheck:
3052 case TypeCheckKind::kInterfaceCheck:
3053 call_kind = LocationSummary::kCallOnSlowPath;
3054 break;
3055 }
3056
3057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003058 locations->SetInAt(0, Location::RequiresRegister());
3059 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003060 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003061}
3062
3063void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003064 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003065 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003066 Location obj_loc = locations->InAt(0);
3067 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003068 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003069 Location temp_loc = locations->GetTemp(0);
3070 Register temp = temp_loc.AsRegister<Register>();
3071 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3072 DCHECK_LE(num_temps, 2u);
3073 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003074 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3075 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3076 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3077 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3078 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3079 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3080 const uint32_t object_array_data_offset =
3081 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3082 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003083
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003084 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3085 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3086 // read barriers is done for performance and code size reasons.
3087 bool is_type_check_slow_path_fatal = false;
3088 if (!kEmitCompilerReadBarrier) {
3089 is_type_check_slow_path_fatal =
3090 (type_check_kind == TypeCheckKind::kExactCheck ||
3091 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3092 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3093 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3094 !instruction->CanThrowIntoCatchBlock();
3095 }
3096 SlowPathCodeMIPS* slow_path =
3097 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3098 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003099 codegen_->AddSlowPath(slow_path);
3100
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003101 // Avoid this check if we know `obj` is not null.
3102 if (instruction->MustDoNullCheck()) {
3103 __ Beqz(obj, &done);
3104 }
3105
3106 switch (type_check_kind) {
3107 case TypeCheckKind::kExactCheck:
3108 case TypeCheckKind::kArrayCheck: {
3109 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003110 GenerateReferenceLoadTwoRegisters(instruction,
3111 temp_loc,
3112 obj_loc,
3113 class_offset,
3114 maybe_temp2_loc,
3115 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003116 // Jump to slow path for throwing the exception or doing a
3117 // more involved array check.
3118 __ Bne(temp, cls, slow_path->GetEntryLabel());
3119 break;
3120 }
3121
3122 case TypeCheckKind::kAbstractClassCheck: {
3123 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003124 GenerateReferenceLoadTwoRegisters(instruction,
3125 temp_loc,
3126 obj_loc,
3127 class_offset,
3128 maybe_temp2_loc,
3129 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003130 // If the class is abstract, we eagerly fetch the super class of the
3131 // object to avoid doing a comparison we know will fail.
3132 MipsLabel loop;
3133 __ Bind(&loop);
3134 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003135 GenerateReferenceLoadOneRegister(instruction,
3136 temp_loc,
3137 super_offset,
3138 maybe_temp2_loc,
3139 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003140 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3141 // exception.
3142 __ Beqz(temp, slow_path->GetEntryLabel());
3143 // Otherwise, compare the classes.
3144 __ Bne(temp, cls, &loop);
3145 break;
3146 }
3147
3148 case TypeCheckKind::kClassHierarchyCheck: {
3149 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003150 GenerateReferenceLoadTwoRegisters(instruction,
3151 temp_loc,
3152 obj_loc,
3153 class_offset,
3154 maybe_temp2_loc,
3155 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003156 // Walk over the class hierarchy to find a match.
3157 MipsLabel loop;
3158 __ Bind(&loop);
3159 __ Beq(temp, cls, &done);
3160 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003161 GenerateReferenceLoadOneRegister(instruction,
3162 temp_loc,
3163 super_offset,
3164 maybe_temp2_loc,
3165 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003166 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3167 // exception. Otherwise, jump to the beginning of the loop.
3168 __ Bnez(temp, &loop);
3169 __ B(slow_path->GetEntryLabel());
3170 break;
3171 }
3172
3173 case TypeCheckKind::kArrayObjectCheck: {
3174 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003175 GenerateReferenceLoadTwoRegisters(instruction,
3176 temp_loc,
3177 obj_loc,
3178 class_offset,
3179 maybe_temp2_loc,
3180 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003181 // Do an exact check.
3182 __ Beq(temp, cls, &done);
3183 // Otherwise, we need to check that the object's class is a non-primitive array.
3184 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003185 GenerateReferenceLoadOneRegister(instruction,
3186 temp_loc,
3187 component_offset,
3188 maybe_temp2_loc,
3189 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003190 // If the component type is null, jump to the slow path to throw the exception.
3191 __ Beqz(temp, slow_path->GetEntryLabel());
3192 // Otherwise, the object is indeed an array, further check that this component
3193 // type is not a primitive type.
3194 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3195 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3196 __ Bnez(temp, slow_path->GetEntryLabel());
3197 break;
3198 }
3199
3200 case TypeCheckKind::kUnresolvedCheck:
3201 // We always go into the type check slow path for the unresolved check case.
3202 // We cannot directly call the CheckCast runtime entry point
3203 // without resorting to a type checking slow path here (i.e. by
3204 // calling InvokeRuntime directly), as it would require to
3205 // assign fixed registers for the inputs of this HInstanceOf
3206 // instruction (following the runtime calling convention), which
3207 // might be cluttered by the potential first read barrier
3208 // emission at the beginning of this method.
3209 __ B(slow_path->GetEntryLabel());
3210 break;
3211
3212 case TypeCheckKind::kInterfaceCheck: {
3213 // Avoid read barriers to improve performance of the fast path. We can not get false
3214 // positives by doing this.
3215 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003216 GenerateReferenceLoadTwoRegisters(instruction,
3217 temp_loc,
3218 obj_loc,
3219 class_offset,
3220 maybe_temp2_loc,
3221 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003222 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003223 GenerateReferenceLoadTwoRegisters(instruction,
3224 temp_loc,
3225 temp_loc,
3226 iftable_offset,
3227 maybe_temp2_loc,
3228 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003229 // Iftable is never null.
3230 __ Lw(TMP, temp, array_length_offset);
3231 // Loop through the iftable and check if any class matches.
3232 MipsLabel loop;
3233 __ Bind(&loop);
3234 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3235 __ Beqz(TMP, slow_path->GetEntryLabel());
3236 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3237 __ MaybeUnpoisonHeapReference(AT);
3238 // Go to next interface.
3239 __ Addiu(TMP, TMP, -2);
3240 // Compare the classes and continue the loop if they do not match.
3241 __ Bne(AT, cls, &loop);
3242 break;
3243 }
3244 }
3245
3246 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247 __ Bind(slow_path->GetExitLabel());
3248}
3249
3250void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3251 LocationSummary* locations =
3252 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3253 locations->SetInAt(0, Location::RequiresRegister());
3254 if (check->HasUses()) {
3255 locations->SetOut(Location::SameAsFirstInput());
3256 }
3257}
3258
3259void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3260 // We assume the class is not null.
3261 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3262 check->GetLoadClass(),
3263 check,
3264 check->GetDexPc(),
3265 true);
3266 codegen_->AddSlowPath(slow_path);
3267 GenerateClassInitializationCheck(slow_path,
3268 check->GetLocations()->InAt(0).AsRegister<Register>());
3269}
3270
3271void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3272 Primitive::Type in_type = compare->InputAt(0)->GetType();
3273
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003274 LocationSummary* locations =
3275 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003276
3277 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003278 case Primitive::kPrimBoolean:
3279 case Primitive::kPrimByte:
3280 case Primitive::kPrimShort:
3281 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003282 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003283 locations->SetInAt(0, Location::RequiresRegister());
3284 locations->SetInAt(1, Location::RequiresRegister());
3285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3286 break;
3287
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003288 case Primitive::kPrimLong:
3289 locations->SetInAt(0, Location::RequiresRegister());
3290 locations->SetInAt(1, Location::RequiresRegister());
3291 // Output overlaps because it is written before doing the low comparison.
3292 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3293 break;
3294
3295 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003296 case Primitive::kPrimDouble:
3297 locations->SetInAt(0, Location::RequiresFpuRegister());
3298 locations->SetInAt(1, Location::RequiresFpuRegister());
3299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003300 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003301
3302 default:
3303 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3304 }
3305}
3306
3307void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3308 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003309 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003310 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003311 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003312
3313 // 0 if: left == right
3314 // 1 if: left > right
3315 // -1 if: left < right
3316 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003317 case Primitive::kPrimBoolean:
3318 case Primitive::kPrimByte:
3319 case Primitive::kPrimShort:
3320 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003321 case Primitive::kPrimInt: {
3322 Register lhs = locations->InAt(0).AsRegister<Register>();
3323 Register rhs = locations->InAt(1).AsRegister<Register>();
3324 __ Slt(TMP, lhs, rhs);
3325 __ Slt(res, rhs, lhs);
3326 __ Subu(res, res, TMP);
3327 break;
3328 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003329 case Primitive::kPrimLong: {
3330 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003331 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3332 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3333 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3334 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3335 // TODO: more efficient (direct) comparison with a constant.
3336 __ Slt(TMP, lhs_high, rhs_high);
3337 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3338 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3339 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3340 __ Sltu(TMP, lhs_low, rhs_low);
3341 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3342 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3343 __ Bind(&done);
3344 break;
3345 }
3346
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003347 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003348 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003349 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3350 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3351 MipsLabel done;
3352 if (isR6) {
3353 __ CmpEqS(FTMP, lhs, rhs);
3354 __ LoadConst32(res, 0);
3355 __ Bc1nez(FTMP, &done);
3356 if (gt_bias) {
3357 __ CmpLtS(FTMP, lhs, rhs);
3358 __ LoadConst32(res, -1);
3359 __ Bc1nez(FTMP, &done);
3360 __ LoadConst32(res, 1);
3361 } else {
3362 __ CmpLtS(FTMP, rhs, lhs);
3363 __ LoadConst32(res, 1);
3364 __ Bc1nez(FTMP, &done);
3365 __ LoadConst32(res, -1);
3366 }
3367 } else {
3368 if (gt_bias) {
3369 __ ColtS(0, lhs, rhs);
3370 __ LoadConst32(res, -1);
3371 __ Bc1t(0, &done);
3372 __ CeqS(0, lhs, rhs);
3373 __ LoadConst32(res, 1);
3374 __ Movt(res, ZERO, 0);
3375 } else {
3376 __ ColtS(0, rhs, lhs);
3377 __ LoadConst32(res, 1);
3378 __ Bc1t(0, &done);
3379 __ CeqS(0, lhs, rhs);
3380 __ LoadConst32(res, -1);
3381 __ Movt(res, ZERO, 0);
3382 }
3383 }
3384 __ Bind(&done);
3385 break;
3386 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003387 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003388 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003389 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3390 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3391 MipsLabel done;
3392 if (isR6) {
3393 __ CmpEqD(FTMP, lhs, rhs);
3394 __ LoadConst32(res, 0);
3395 __ Bc1nez(FTMP, &done);
3396 if (gt_bias) {
3397 __ CmpLtD(FTMP, lhs, rhs);
3398 __ LoadConst32(res, -1);
3399 __ Bc1nez(FTMP, &done);
3400 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003401 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003402 __ CmpLtD(FTMP, rhs, lhs);
3403 __ LoadConst32(res, 1);
3404 __ Bc1nez(FTMP, &done);
3405 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003406 }
3407 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003408 if (gt_bias) {
3409 __ ColtD(0, lhs, rhs);
3410 __ LoadConst32(res, -1);
3411 __ Bc1t(0, &done);
3412 __ CeqD(0, lhs, rhs);
3413 __ LoadConst32(res, 1);
3414 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003415 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003416 __ ColtD(0, rhs, lhs);
3417 __ LoadConst32(res, 1);
3418 __ Bc1t(0, &done);
3419 __ CeqD(0, lhs, rhs);
3420 __ LoadConst32(res, -1);
3421 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003422 }
3423 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003424 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003425 break;
3426 }
3427
3428 default:
3429 LOG(FATAL) << "Unimplemented compare type " << in_type;
3430 }
3431}
3432
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003433void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003435 switch (instruction->InputAt(0)->GetType()) {
3436 default:
3437 case Primitive::kPrimLong:
3438 locations->SetInAt(0, Location::RequiresRegister());
3439 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3440 break;
3441
3442 case Primitive::kPrimFloat:
3443 case Primitive::kPrimDouble:
3444 locations->SetInAt(0, Location::RequiresFpuRegister());
3445 locations->SetInAt(1, Location::RequiresFpuRegister());
3446 break;
3447 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003448 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3450 }
3451}
3452
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003453void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003454 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003455 return;
3456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003457
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003458 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003459 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003460
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003461 switch (type) {
3462 default:
3463 // Integer case.
3464 GenerateIntCompare(instruction->GetCondition(), locations);
3465 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003466
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003467 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003468 GenerateLongCompare(instruction->GetCondition(), locations);
3469 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003470
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 }
3476}
3477
Alexey Frunze7e99e052015-11-24 19:28:01 -08003478void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3479 DCHECK(instruction->IsDiv() || instruction->IsRem());
3480 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3481
3482 LocationSummary* locations = instruction->GetLocations();
3483 Location second = locations->InAt(1);
3484 DCHECK(second.IsConstant());
3485
3486 Register out = locations->Out().AsRegister<Register>();
3487 Register dividend = locations->InAt(0).AsRegister<Register>();
3488 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3489 DCHECK(imm == 1 || imm == -1);
3490
3491 if (instruction->IsRem()) {
3492 __ Move(out, ZERO);
3493 } else {
3494 if (imm == -1) {
3495 __ Subu(out, ZERO, dividend);
3496 } else if (out != dividend) {
3497 __ Move(out, dividend);
3498 }
3499 }
3500}
3501
3502void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3503 DCHECK(instruction->IsDiv() || instruction->IsRem());
3504 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3505
3506 LocationSummary* locations = instruction->GetLocations();
3507 Location second = locations->InAt(1);
3508 DCHECK(second.IsConstant());
3509
3510 Register out = locations->Out().AsRegister<Register>();
3511 Register dividend = locations->InAt(0).AsRegister<Register>();
3512 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003513 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003514 int ctz_imm = CTZ(abs_imm);
3515
3516 if (instruction->IsDiv()) {
3517 if (ctz_imm == 1) {
3518 // Fast path for division by +/-2, which is very common.
3519 __ Srl(TMP, dividend, 31);
3520 } else {
3521 __ Sra(TMP, dividend, 31);
3522 __ Srl(TMP, TMP, 32 - ctz_imm);
3523 }
3524 __ Addu(out, dividend, TMP);
3525 __ Sra(out, out, ctz_imm);
3526 if (imm < 0) {
3527 __ Subu(out, ZERO, out);
3528 }
3529 } else {
3530 if (ctz_imm == 1) {
3531 // Fast path for modulo +/-2, which is very common.
3532 __ Sra(TMP, dividend, 31);
3533 __ Subu(out, dividend, TMP);
3534 __ Andi(out, out, 1);
3535 __ Addu(out, out, TMP);
3536 } else {
3537 __ Sra(TMP, dividend, 31);
3538 __ Srl(TMP, TMP, 32 - ctz_imm);
3539 __ Addu(out, dividend, TMP);
3540 if (IsUint<16>(abs_imm - 1)) {
3541 __ Andi(out, out, abs_imm - 1);
3542 } else {
3543 __ Sll(out, out, 32 - ctz_imm);
3544 __ Srl(out, out, 32 - ctz_imm);
3545 }
3546 __ Subu(out, out, TMP);
3547 }
3548 }
3549}
3550
3551void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3552 DCHECK(instruction->IsDiv() || instruction->IsRem());
3553 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3554
3555 LocationSummary* locations = instruction->GetLocations();
3556 Location second = locations->InAt(1);
3557 DCHECK(second.IsConstant());
3558
3559 Register out = locations->Out().AsRegister<Register>();
3560 Register dividend = locations->InAt(0).AsRegister<Register>();
3561 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3562
3563 int64_t magic;
3564 int shift;
3565 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3566
3567 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3568
3569 __ LoadConst32(TMP, magic);
3570 if (isR6) {
3571 __ MuhR6(TMP, dividend, TMP);
3572 } else {
3573 __ MultR2(dividend, TMP);
3574 __ Mfhi(TMP);
3575 }
3576 if (imm > 0 && magic < 0) {
3577 __ Addu(TMP, TMP, dividend);
3578 } else if (imm < 0 && magic > 0) {
3579 __ Subu(TMP, TMP, dividend);
3580 }
3581
3582 if (shift != 0) {
3583 __ Sra(TMP, TMP, shift);
3584 }
3585
3586 if (instruction->IsDiv()) {
3587 __ Sra(out, TMP, 31);
3588 __ Subu(out, TMP, out);
3589 } else {
3590 __ Sra(AT, TMP, 31);
3591 __ Subu(AT, TMP, AT);
3592 __ LoadConst32(TMP, imm);
3593 if (isR6) {
3594 __ MulR6(TMP, AT, TMP);
3595 } else {
3596 __ MulR2(TMP, AT, TMP);
3597 }
3598 __ Subu(out, dividend, TMP);
3599 }
3600}
3601
3602void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3603 DCHECK(instruction->IsDiv() || instruction->IsRem());
3604 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3605
3606 LocationSummary* locations = instruction->GetLocations();
3607 Register out = locations->Out().AsRegister<Register>();
3608 Location second = locations->InAt(1);
3609
3610 if (second.IsConstant()) {
3611 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3612 if (imm == 0) {
3613 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3614 } else if (imm == 1 || imm == -1) {
3615 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003616 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003617 DivRemByPowerOfTwo(instruction);
3618 } else {
3619 DCHECK(imm <= -2 || imm >= 2);
3620 GenerateDivRemWithAnyConstant(instruction);
3621 }
3622 } else {
3623 Register dividend = locations->InAt(0).AsRegister<Register>();
3624 Register divisor = second.AsRegister<Register>();
3625 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3626 if (instruction->IsDiv()) {
3627 if (isR6) {
3628 __ DivR6(out, dividend, divisor);
3629 } else {
3630 __ DivR2(out, dividend, divisor);
3631 }
3632 } else {
3633 if (isR6) {
3634 __ ModR6(out, dividend, divisor);
3635 } else {
3636 __ ModR2(out, dividend, divisor);
3637 }
3638 }
3639 }
3640}
3641
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3643 Primitive::Type type = div->GetResultType();
3644 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003645 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003646 : LocationSummary::kNoCall;
3647
3648 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3649
3650 switch (type) {
3651 case Primitive::kPrimInt:
3652 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003653 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3655 break;
3656
3657 case Primitive::kPrimLong: {
3658 InvokeRuntimeCallingConvention calling_convention;
3659 locations->SetInAt(0, Location::RegisterPairLocation(
3660 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3661 locations->SetInAt(1, Location::RegisterPairLocation(
3662 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3663 locations->SetOut(calling_convention.GetReturnLocation(type));
3664 break;
3665 }
3666
3667 case Primitive::kPrimFloat:
3668 case Primitive::kPrimDouble:
3669 locations->SetInAt(0, Location::RequiresFpuRegister());
3670 locations->SetInAt(1, Location::RequiresFpuRegister());
3671 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3672 break;
3673
3674 default:
3675 LOG(FATAL) << "Unexpected div type " << type;
3676 }
3677}
3678
3679void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3680 Primitive::Type type = instruction->GetType();
3681 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682
3683 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003684 case Primitive::kPrimInt:
3685 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003688 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003689 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3690 break;
3691 }
3692 case Primitive::kPrimFloat:
3693 case Primitive::kPrimDouble: {
3694 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3695 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3696 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3697 if (type == Primitive::kPrimFloat) {
3698 __ DivS(dst, lhs, rhs);
3699 } else {
3700 __ DivD(dst, lhs, rhs);
3701 }
3702 break;
3703 }
3704 default:
3705 LOG(FATAL) << "Unexpected div type " << type;
3706 }
3707}
3708
3709void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003710 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003711 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003712}
3713
3714void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3715 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3716 codegen_->AddSlowPath(slow_path);
3717 Location value = instruction->GetLocations()->InAt(0);
3718 Primitive::Type type = instruction->GetType();
3719
3720 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003721 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003722 case Primitive::kPrimByte:
3723 case Primitive::kPrimChar:
3724 case Primitive::kPrimShort:
3725 case Primitive::kPrimInt: {
3726 if (value.IsConstant()) {
3727 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3728 __ B(slow_path->GetEntryLabel());
3729 } else {
3730 // A division by a non-null constant is valid. We don't need to perform
3731 // any check, so simply fall through.
3732 }
3733 } else {
3734 DCHECK(value.IsRegister()) << value;
3735 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3736 }
3737 break;
3738 }
3739 case Primitive::kPrimLong: {
3740 if (value.IsConstant()) {
3741 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3742 __ B(slow_path->GetEntryLabel());
3743 } else {
3744 // A division by a non-null constant is valid. We don't need to perform
3745 // any check, so simply fall through.
3746 }
3747 } else {
3748 DCHECK(value.IsRegisterPair()) << value;
3749 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3750 __ Beqz(TMP, slow_path->GetEntryLabel());
3751 }
3752 break;
3753 }
3754 default:
3755 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3756 }
3757}
3758
3759void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3760 LocationSummary* locations =
3761 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3762 locations->SetOut(Location::ConstantLocation(constant));
3763}
3764
3765void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3766 // Will be generated at use site.
3767}
3768
3769void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3770 exit->SetLocations(nullptr);
3771}
3772
3773void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3774}
3775
3776void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3777 LocationSummary* locations =
3778 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3779 locations->SetOut(Location::ConstantLocation(constant));
3780}
3781
3782void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3783 // Will be generated at use site.
3784}
3785
3786void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3787 got->SetLocations(nullptr);
3788}
3789
3790void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3791 DCHECK(!successor->IsExitBlock());
3792 HBasicBlock* block = got->GetBlock();
3793 HInstruction* previous = got->GetPrevious();
3794 HLoopInformation* info = block->GetLoopInformation();
3795
3796 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3797 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3798 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3799 return;
3800 }
3801 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3802 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3803 }
3804 if (!codegen_->GoesToNextBlock(block, successor)) {
3805 __ B(codegen_->GetLabelOf(successor));
3806 }
3807}
3808
3809void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3810 HandleGoto(got, got->GetSuccessor());
3811}
3812
3813void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3814 try_boundary->SetLocations(nullptr);
3815}
3816
3817void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3818 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3819 if (!successor->IsExitBlock()) {
3820 HandleGoto(try_boundary, successor);
3821 }
3822}
3823
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003824void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3825 LocationSummary* locations) {
3826 Register dst = locations->Out().AsRegister<Register>();
3827 Register lhs = locations->InAt(0).AsRegister<Register>();
3828 Location rhs_location = locations->InAt(1);
3829 Register rhs_reg = ZERO;
3830 int64_t rhs_imm = 0;
3831 bool use_imm = rhs_location.IsConstant();
3832 if (use_imm) {
3833 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3834 } else {
3835 rhs_reg = rhs_location.AsRegister<Register>();
3836 }
3837
3838 switch (cond) {
3839 case kCondEQ:
3840 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003841 if (use_imm && IsInt<16>(-rhs_imm)) {
3842 if (rhs_imm == 0) {
3843 if (cond == kCondEQ) {
3844 __ Sltiu(dst, lhs, 1);
3845 } else {
3846 __ Sltu(dst, ZERO, lhs);
3847 }
3848 } else {
3849 __ Addiu(dst, lhs, -rhs_imm);
3850 if (cond == kCondEQ) {
3851 __ Sltiu(dst, dst, 1);
3852 } else {
3853 __ Sltu(dst, ZERO, dst);
3854 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003855 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003856 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003857 if (use_imm && IsUint<16>(rhs_imm)) {
3858 __ Xori(dst, lhs, rhs_imm);
3859 } else {
3860 if (use_imm) {
3861 rhs_reg = TMP;
3862 __ LoadConst32(rhs_reg, rhs_imm);
3863 }
3864 __ Xor(dst, lhs, rhs_reg);
3865 }
3866 if (cond == kCondEQ) {
3867 __ Sltiu(dst, dst, 1);
3868 } else {
3869 __ Sltu(dst, ZERO, dst);
3870 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003871 }
3872 break;
3873
3874 case kCondLT:
3875 case kCondGE:
3876 if (use_imm && IsInt<16>(rhs_imm)) {
3877 __ Slti(dst, lhs, rhs_imm);
3878 } else {
3879 if (use_imm) {
3880 rhs_reg = TMP;
3881 __ LoadConst32(rhs_reg, rhs_imm);
3882 }
3883 __ Slt(dst, lhs, rhs_reg);
3884 }
3885 if (cond == kCondGE) {
3886 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3887 // only the slt instruction but no sge.
3888 __ Xori(dst, dst, 1);
3889 }
3890 break;
3891
3892 case kCondLE:
3893 case kCondGT:
3894 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3895 // Simulate lhs <= rhs via lhs < rhs + 1.
3896 __ Slti(dst, lhs, rhs_imm + 1);
3897 if (cond == kCondGT) {
3898 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3899 // only the slti instruction but no sgti.
3900 __ Xori(dst, dst, 1);
3901 }
3902 } else {
3903 if (use_imm) {
3904 rhs_reg = TMP;
3905 __ LoadConst32(rhs_reg, rhs_imm);
3906 }
3907 __ Slt(dst, rhs_reg, lhs);
3908 if (cond == kCondLE) {
3909 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3910 // only the slt instruction but no sle.
3911 __ Xori(dst, dst, 1);
3912 }
3913 }
3914 break;
3915
3916 case kCondB:
3917 case kCondAE:
3918 if (use_imm && IsInt<16>(rhs_imm)) {
3919 // Sltiu sign-extends its 16-bit immediate operand before
3920 // the comparison and thus lets us compare directly with
3921 // unsigned values in the ranges [0, 0x7fff] and
3922 // [0xffff8000, 0xffffffff].
3923 __ Sltiu(dst, lhs, rhs_imm);
3924 } else {
3925 if (use_imm) {
3926 rhs_reg = TMP;
3927 __ LoadConst32(rhs_reg, rhs_imm);
3928 }
3929 __ Sltu(dst, lhs, rhs_reg);
3930 }
3931 if (cond == kCondAE) {
3932 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3933 // only the sltu instruction but no sgeu.
3934 __ Xori(dst, dst, 1);
3935 }
3936 break;
3937
3938 case kCondBE:
3939 case kCondA:
3940 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3941 // Simulate lhs <= rhs via lhs < rhs + 1.
3942 // Note that this only works if rhs + 1 does not overflow
3943 // to 0, hence the check above.
3944 // Sltiu sign-extends its 16-bit immediate operand before
3945 // the comparison and thus lets us compare directly with
3946 // unsigned values in the ranges [0, 0x7fff] and
3947 // [0xffff8000, 0xffffffff].
3948 __ Sltiu(dst, lhs, rhs_imm + 1);
3949 if (cond == kCondA) {
3950 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3951 // only the sltiu instruction but no sgtiu.
3952 __ Xori(dst, dst, 1);
3953 }
3954 } else {
3955 if (use_imm) {
3956 rhs_reg = TMP;
3957 __ LoadConst32(rhs_reg, rhs_imm);
3958 }
3959 __ Sltu(dst, rhs_reg, lhs);
3960 if (cond == kCondBE) {
3961 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3962 // only the sltu instruction but no sleu.
3963 __ Xori(dst, dst, 1);
3964 }
3965 }
3966 break;
3967 }
3968}
3969
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003970bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3971 LocationSummary* input_locations,
3972 Register dst) {
3973 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3974 Location rhs_location = input_locations->InAt(1);
3975 Register rhs_reg = ZERO;
3976 int64_t rhs_imm = 0;
3977 bool use_imm = rhs_location.IsConstant();
3978 if (use_imm) {
3979 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3980 } else {
3981 rhs_reg = rhs_location.AsRegister<Register>();
3982 }
3983
3984 switch (cond) {
3985 case kCondEQ:
3986 case kCondNE:
3987 if (use_imm && IsInt<16>(-rhs_imm)) {
3988 __ Addiu(dst, lhs, -rhs_imm);
3989 } else if (use_imm && IsUint<16>(rhs_imm)) {
3990 __ Xori(dst, lhs, rhs_imm);
3991 } else {
3992 if (use_imm) {
3993 rhs_reg = TMP;
3994 __ LoadConst32(rhs_reg, rhs_imm);
3995 }
3996 __ Xor(dst, lhs, rhs_reg);
3997 }
3998 return (cond == kCondEQ);
3999
4000 case kCondLT:
4001 case kCondGE:
4002 if (use_imm && IsInt<16>(rhs_imm)) {
4003 __ Slti(dst, lhs, rhs_imm);
4004 } else {
4005 if (use_imm) {
4006 rhs_reg = TMP;
4007 __ LoadConst32(rhs_reg, rhs_imm);
4008 }
4009 __ Slt(dst, lhs, rhs_reg);
4010 }
4011 return (cond == kCondGE);
4012
4013 case kCondLE:
4014 case kCondGT:
4015 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4016 // Simulate lhs <= rhs via lhs < rhs + 1.
4017 __ Slti(dst, lhs, rhs_imm + 1);
4018 return (cond == kCondGT);
4019 } else {
4020 if (use_imm) {
4021 rhs_reg = TMP;
4022 __ LoadConst32(rhs_reg, rhs_imm);
4023 }
4024 __ Slt(dst, rhs_reg, lhs);
4025 return (cond == kCondLE);
4026 }
4027
4028 case kCondB:
4029 case kCondAE:
4030 if (use_imm && IsInt<16>(rhs_imm)) {
4031 // Sltiu sign-extends its 16-bit immediate operand before
4032 // the comparison and thus lets us compare directly with
4033 // unsigned values in the ranges [0, 0x7fff] and
4034 // [0xffff8000, 0xffffffff].
4035 __ Sltiu(dst, lhs, rhs_imm);
4036 } else {
4037 if (use_imm) {
4038 rhs_reg = TMP;
4039 __ LoadConst32(rhs_reg, rhs_imm);
4040 }
4041 __ Sltu(dst, lhs, rhs_reg);
4042 }
4043 return (cond == kCondAE);
4044
4045 case kCondBE:
4046 case kCondA:
4047 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4048 // Simulate lhs <= rhs via lhs < rhs + 1.
4049 // Note that this only works if rhs + 1 does not overflow
4050 // to 0, hence the check above.
4051 // Sltiu sign-extends its 16-bit immediate operand before
4052 // the comparison and thus lets us compare directly with
4053 // unsigned values in the ranges [0, 0x7fff] and
4054 // [0xffff8000, 0xffffffff].
4055 __ Sltiu(dst, lhs, rhs_imm + 1);
4056 return (cond == kCondA);
4057 } else {
4058 if (use_imm) {
4059 rhs_reg = TMP;
4060 __ LoadConst32(rhs_reg, rhs_imm);
4061 }
4062 __ Sltu(dst, rhs_reg, lhs);
4063 return (cond == kCondBE);
4064 }
4065 }
4066}
4067
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004068void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4069 LocationSummary* locations,
4070 MipsLabel* label) {
4071 Register lhs = locations->InAt(0).AsRegister<Register>();
4072 Location rhs_location = locations->InAt(1);
4073 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004074 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004075 bool use_imm = rhs_location.IsConstant();
4076 if (use_imm) {
4077 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4078 } else {
4079 rhs_reg = rhs_location.AsRegister<Register>();
4080 }
4081
4082 if (use_imm && rhs_imm == 0) {
4083 switch (cond) {
4084 case kCondEQ:
4085 case kCondBE: // <= 0 if zero
4086 __ Beqz(lhs, label);
4087 break;
4088 case kCondNE:
4089 case kCondA: // > 0 if non-zero
4090 __ Bnez(lhs, label);
4091 break;
4092 case kCondLT:
4093 __ Bltz(lhs, label);
4094 break;
4095 case kCondGE:
4096 __ Bgez(lhs, label);
4097 break;
4098 case kCondLE:
4099 __ Blez(lhs, label);
4100 break;
4101 case kCondGT:
4102 __ Bgtz(lhs, label);
4103 break;
4104 case kCondB: // always false
4105 break;
4106 case kCondAE: // always true
4107 __ B(label);
4108 break;
4109 }
4110 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004111 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4112 if (isR6 || !use_imm) {
4113 if (use_imm) {
4114 rhs_reg = TMP;
4115 __ LoadConst32(rhs_reg, rhs_imm);
4116 }
4117 switch (cond) {
4118 case kCondEQ:
4119 __ Beq(lhs, rhs_reg, label);
4120 break;
4121 case kCondNE:
4122 __ Bne(lhs, rhs_reg, label);
4123 break;
4124 case kCondLT:
4125 __ Blt(lhs, rhs_reg, label);
4126 break;
4127 case kCondGE:
4128 __ Bge(lhs, rhs_reg, label);
4129 break;
4130 case kCondLE:
4131 __ Bge(rhs_reg, lhs, label);
4132 break;
4133 case kCondGT:
4134 __ Blt(rhs_reg, lhs, label);
4135 break;
4136 case kCondB:
4137 __ Bltu(lhs, rhs_reg, label);
4138 break;
4139 case kCondAE:
4140 __ Bgeu(lhs, rhs_reg, label);
4141 break;
4142 case kCondBE:
4143 __ Bgeu(rhs_reg, lhs, label);
4144 break;
4145 case kCondA:
4146 __ Bltu(rhs_reg, lhs, label);
4147 break;
4148 }
4149 } else {
4150 // Special cases for more efficient comparison with constants on R2.
4151 switch (cond) {
4152 case kCondEQ:
4153 __ LoadConst32(TMP, rhs_imm);
4154 __ Beq(lhs, TMP, label);
4155 break;
4156 case kCondNE:
4157 __ LoadConst32(TMP, rhs_imm);
4158 __ Bne(lhs, TMP, label);
4159 break;
4160 case kCondLT:
4161 if (IsInt<16>(rhs_imm)) {
4162 __ Slti(TMP, lhs, rhs_imm);
4163 __ Bnez(TMP, label);
4164 } else {
4165 __ LoadConst32(TMP, rhs_imm);
4166 __ Blt(lhs, TMP, label);
4167 }
4168 break;
4169 case kCondGE:
4170 if (IsInt<16>(rhs_imm)) {
4171 __ Slti(TMP, lhs, rhs_imm);
4172 __ Beqz(TMP, label);
4173 } else {
4174 __ LoadConst32(TMP, rhs_imm);
4175 __ Bge(lhs, TMP, label);
4176 }
4177 break;
4178 case kCondLE:
4179 if (IsInt<16>(rhs_imm + 1)) {
4180 // Simulate lhs <= rhs via lhs < rhs + 1.
4181 __ Slti(TMP, lhs, rhs_imm + 1);
4182 __ Bnez(TMP, label);
4183 } else {
4184 __ LoadConst32(TMP, rhs_imm);
4185 __ Bge(TMP, lhs, label);
4186 }
4187 break;
4188 case kCondGT:
4189 if (IsInt<16>(rhs_imm + 1)) {
4190 // Simulate lhs > rhs via !(lhs < rhs + 1).
4191 __ Slti(TMP, lhs, rhs_imm + 1);
4192 __ Beqz(TMP, label);
4193 } else {
4194 __ LoadConst32(TMP, rhs_imm);
4195 __ Blt(TMP, lhs, label);
4196 }
4197 break;
4198 case kCondB:
4199 if (IsInt<16>(rhs_imm)) {
4200 __ Sltiu(TMP, lhs, rhs_imm);
4201 __ Bnez(TMP, label);
4202 } else {
4203 __ LoadConst32(TMP, rhs_imm);
4204 __ Bltu(lhs, TMP, label);
4205 }
4206 break;
4207 case kCondAE:
4208 if (IsInt<16>(rhs_imm)) {
4209 __ Sltiu(TMP, lhs, rhs_imm);
4210 __ Beqz(TMP, label);
4211 } else {
4212 __ LoadConst32(TMP, rhs_imm);
4213 __ Bgeu(lhs, TMP, label);
4214 }
4215 break;
4216 case kCondBE:
4217 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4218 // Simulate lhs <= rhs via lhs < rhs + 1.
4219 // Note that this only works if rhs + 1 does not overflow
4220 // to 0, hence the check above.
4221 __ Sltiu(TMP, lhs, rhs_imm + 1);
4222 __ Bnez(TMP, label);
4223 } else {
4224 __ LoadConst32(TMP, rhs_imm);
4225 __ Bgeu(TMP, lhs, label);
4226 }
4227 break;
4228 case kCondA:
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 __ Beqz(TMP, label);
4235 } else {
4236 __ LoadConst32(TMP, rhs_imm);
4237 __ Bltu(TMP, lhs, label);
4238 }
4239 break;
4240 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004241 }
4242 }
4243}
4244
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004245void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4246 LocationSummary* locations) {
4247 Register dst = locations->Out().AsRegister<Register>();
4248 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4249 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4250 Location rhs_location = locations->InAt(1);
4251 Register rhs_high = ZERO;
4252 Register rhs_low = ZERO;
4253 int64_t imm = 0;
4254 uint32_t imm_high = 0;
4255 uint32_t imm_low = 0;
4256 bool use_imm = rhs_location.IsConstant();
4257 if (use_imm) {
4258 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4259 imm_high = High32Bits(imm);
4260 imm_low = Low32Bits(imm);
4261 } else {
4262 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4263 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4264 }
4265 if (use_imm && imm == 0) {
4266 switch (cond) {
4267 case kCondEQ:
4268 case kCondBE: // <= 0 if zero
4269 __ Or(dst, lhs_high, lhs_low);
4270 __ Sltiu(dst, dst, 1);
4271 break;
4272 case kCondNE:
4273 case kCondA: // > 0 if non-zero
4274 __ Or(dst, lhs_high, lhs_low);
4275 __ Sltu(dst, ZERO, dst);
4276 break;
4277 case kCondLT:
4278 __ Slt(dst, lhs_high, ZERO);
4279 break;
4280 case kCondGE:
4281 __ Slt(dst, lhs_high, ZERO);
4282 __ Xori(dst, dst, 1);
4283 break;
4284 case kCondLE:
4285 __ Or(TMP, lhs_high, lhs_low);
4286 __ Sra(AT, lhs_high, 31);
4287 __ Sltu(dst, AT, TMP);
4288 __ Xori(dst, dst, 1);
4289 break;
4290 case kCondGT:
4291 __ Or(TMP, lhs_high, lhs_low);
4292 __ Sra(AT, lhs_high, 31);
4293 __ Sltu(dst, AT, TMP);
4294 break;
4295 case kCondB: // always false
4296 __ Andi(dst, dst, 0);
4297 break;
4298 case kCondAE: // always true
4299 __ Ori(dst, ZERO, 1);
4300 break;
4301 }
4302 } else if (use_imm) {
4303 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4304 switch (cond) {
4305 case kCondEQ:
4306 __ LoadConst32(TMP, imm_high);
4307 __ Xor(TMP, TMP, lhs_high);
4308 __ LoadConst32(AT, imm_low);
4309 __ Xor(AT, AT, lhs_low);
4310 __ Or(dst, TMP, AT);
4311 __ Sltiu(dst, dst, 1);
4312 break;
4313 case kCondNE:
4314 __ LoadConst32(TMP, imm_high);
4315 __ Xor(TMP, TMP, lhs_high);
4316 __ LoadConst32(AT, imm_low);
4317 __ Xor(AT, AT, lhs_low);
4318 __ Or(dst, TMP, AT);
4319 __ Sltu(dst, ZERO, dst);
4320 break;
4321 case kCondLT:
4322 case kCondGE:
4323 if (dst == lhs_low) {
4324 __ LoadConst32(TMP, imm_low);
4325 __ Sltu(dst, lhs_low, TMP);
4326 }
4327 __ LoadConst32(TMP, imm_high);
4328 __ Slt(AT, lhs_high, TMP);
4329 __ Slt(TMP, TMP, lhs_high);
4330 if (dst != lhs_low) {
4331 __ LoadConst32(dst, imm_low);
4332 __ Sltu(dst, lhs_low, dst);
4333 }
4334 __ Slt(dst, TMP, dst);
4335 __ Or(dst, dst, AT);
4336 if (cond == kCondGE) {
4337 __ Xori(dst, dst, 1);
4338 }
4339 break;
4340 case kCondGT:
4341 case kCondLE:
4342 if (dst == lhs_low) {
4343 __ LoadConst32(TMP, imm_low);
4344 __ Sltu(dst, TMP, lhs_low);
4345 }
4346 __ LoadConst32(TMP, imm_high);
4347 __ Slt(AT, TMP, lhs_high);
4348 __ Slt(TMP, lhs_high, TMP);
4349 if (dst != lhs_low) {
4350 __ LoadConst32(dst, imm_low);
4351 __ Sltu(dst, dst, lhs_low);
4352 }
4353 __ Slt(dst, TMP, dst);
4354 __ Or(dst, dst, AT);
4355 if (cond == kCondLE) {
4356 __ Xori(dst, dst, 1);
4357 }
4358 break;
4359 case kCondB:
4360 case kCondAE:
4361 if (dst == lhs_low) {
4362 __ LoadConst32(TMP, imm_low);
4363 __ Sltu(dst, lhs_low, TMP);
4364 }
4365 __ LoadConst32(TMP, imm_high);
4366 __ Sltu(AT, lhs_high, TMP);
4367 __ Sltu(TMP, TMP, lhs_high);
4368 if (dst != lhs_low) {
4369 __ LoadConst32(dst, imm_low);
4370 __ Sltu(dst, lhs_low, dst);
4371 }
4372 __ Slt(dst, TMP, dst);
4373 __ Or(dst, dst, AT);
4374 if (cond == kCondAE) {
4375 __ Xori(dst, dst, 1);
4376 }
4377 break;
4378 case kCondA:
4379 case kCondBE:
4380 if (dst == lhs_low) {
4381 __ LoadConst32(TMP, imm_low);
4382 __ Sltu(dst, TMP, lhs_low);
4383 }
4384 __ LoadConst32(TMP, imm_high);
4385 __ Sltu(AT, TMP, lhs_high);
4386 __ Sltu(TMP, lhs_high, TMP);
4387 if (dst != lhs_low) {
4388 __ LoadConst32(dst, imm_low);
4389 __ Sltu(dst, dst, lhs_low);
4390 }
4391 __ Slt(dst, TMP, dst);
4392 __ Or(dst, dst, AT);
4393 if (cond == kCondBE) {
4394 __ Xori(dst, dst, 1);
4395 }
4396 break;
4397 }
4398 } else {
4399 switch (cond) {
4400 case kCondEQ:
4401 __ Xor(TMP, lhs_high, rhs_high);
4402 __ Xor(AT, lhs_low, rhs_low);
4403 __ Or(dst, TMP, AT);
4404 __ Sltiu(dst, dst, 1);
4405 break;
4406 case kCondNE:
4407 __ Xor(TMP, lhs_high, rhs_high);
4408 __ Xor(AT, lhs_low, rhs_low);
4409 __ Or(dst, TMP, AT);
4410 __ Sltu(dst, ZERO, dst);
4411 break;
4412 case kCondLT:
4413 case kCondGE:
4414 __ Slt(TMP, rhs_high, lhs_high);
4415 __ Sltu(AT, lhs_low, rhs_low);
4416 __ Slt(TMP, TMP, AT);
4417 __ Slt(AT, lhs_high, rhs_high);
4418 __ Or(dst, AT, TMP);
4419 if (cond == kCondGE) {
4420 __ Xori(dst, dst, 1);
4421 }
4422 break;
4423 case kCondGT:
4424 case kCondLE:
4425 __ Slt(TMP, lhs_high, rhs_high);
4426 __ Sltu(AT, rhs_low, lhs_low);
4427 __ Slt(TMP, TMP, AT);
4428 __ Slt(AT, rhs_high, lhs_high);
4429 __ Or(dst, AT, TMP);
4430 if (cond == kCondLE) {
4431 __ Xori(dst, dst, 1);
4432 }
4433 break;
4434 case kCondB:
4435 case kCondAE:
4436 __ Sltu(TMP, rhs_high, lhs_high);
4437 __ Sltu(AT, lhs_low, rhs_low);
4438 __ Slt(TMP, TMP, AT);
4439 __ Sltu(AT, lhs_high, rhs_high);
4440 __ Or(dst, AT, TMP);
4441 if (cond == kCondAE) {
4442 __ Xori(dst, dst, 1);
4443 }
4444 break;
4445 case kCondA:
4446 case kCondBE:
4447 __ Sltu(TMP, lhs_high, rhs_high);
4448 __ Sltu(AT, rhs_low, lhs_low);
4449 __ Slt(TMP, TMP, AT);
4450 __ Sltu(AT, rhs_high, lhs_high);
4451 __ Or(dst, AT, TMP);
4452 if (cond == kCondBE) {
4453 __ Xori(dst, dst, 1);
4454 }
4455 break;
4456 }
4457 }
4458}
4459
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004460void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4461 LocationSummary* locations,
4462 MipsLabel* label) {
4463 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4464 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4465 Location rhs_location = locations->InAt(1);
4466 Register rhs_high = ZERO;
4467 Register rhs_low = ZERO;
4468 int64_t imm = 0;
4469 uint32_t imm_high = 0;
4470 uint32_t imm_low = 0;
4471 bool use_imm = rhs_location.IsConstant();
4472 if (use_imm) {
4473 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4474 imm_high = High32Bits(imm);
4475 imm_low = Low32Bits(imm);
4476 } else {
4477 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4478 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4479 }
4480
4481 if (use_imm && imm == 0) {
4482 switch (cond) {
4483 case kCondEQ:
4484 case kCondBE: // <= 0 if zero
4485 __ Or(TMP, lhs_high, lhs_low);
4486 __ Beqz(TMP, label);
4487 break;
4488 case kCondNE:
4489 case kCondA: // > 0 if non-zero
4490 __ Or(TMP, lhs_high, lhs_low);
4491 __ Bnez(TMP, label);
4492 break;
4493 case kCondLT:
4494 __ Bltz(lhs_high, label);
4495 break;
4496 case kCondGE:
4497 __ Bgez(lhs_high, label);
4498 break;
4499 case kCondLE:
4500 __ Or(TMP, lhs_high, lhs_low);
4501 __ Sra(AT, lhs_high, 31);
4502 __ Bgeu(AT, TMP, label);
4503 break;
4504 case kCondGT:
4505 __ Or(TMP, lhs_high, lhs_low);
4506 __ Sra(AT, lhs_high, 31);
4507 __ Bltu(AT, TMP, label);
4508 break;
4509 case kCondB: // always false
4510 break;
4511 case kCondAE: // always true
4512 __ B(label);
4513 break;
4514 }
4515 } else if (use_imm) {
4516 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4517 switch (cond) {
4518 case kCondEQ:
4519 __ LoadConst32(TMP, imm_high);
4520 __ Xor(TMP, TMP, lhs_high);
4521 __ LoadConst32(AT, imm_low);
4522 __ Xor(AT, AT, lhs_low);
4523 __ Or(TMP, TMP, AT);
4524 __ Beqz(TMP, label);
4525 break;
4526 case kCondNE:
4527 __ LoadConst32(TMP, imm_high);
4528 __ Xor(TMP, TMP, lhs_high);
4529 __ LoadConst32(AT, imm_low);
4530 __ Xor(AT, AT, lhs_low);
4531 __ Or(TMP, TMP, AT);
4532 __ Bnez(TMP, label);
4533 break;
4534 case kCondLT:
4535 __ LoadConst32(TMP, imm_high);
4536 __ Blt(lhs_high, TMP, label);
4537 __ Slt(TMP, TMP, lhs_high);
4538 __ LoadConst32(AT, imm_low);
4539 __ Sltu(AT, lhs_low, AT);
4540 __ Blt(TMP, AT, label);
4541 break;
4542 case kCondGE:
4543 __ LoadConst32(TMP, imm_high);
4544 __ Blt(TMP, lhs_high, label);
4545 __ Slt(TMP, lhs_high, TMP);
4546 __ LoadConst32(AT, imm_low);
4547 __ Sltu(AT, lhs_low, AT);
4548 __ Or(TMP, TMP, AT);
4549 __ Beqz(TMP, label);
4550 break;
4551 case kCondLE:
4552 __ LoadConst32(TMP, imm_high);
4553 __ Blt(lhs_high, TMP, label);
4554 __ Slt(TMP, TMP, lhs_high);
4555 __ LoadConst32(AT, imm_low);
4556 __ Sltu(AT, AT, lhs_low);
4557 __ Or(TMP, TMP, AT);
4558 __ Beqz(TMP, label);
4559 break;
4560 case kCondGT:
4561 __ LoadConst32(TMP, imm_high);
4562 __ Blt(TMP, lhs_high, label);
4563 __ Slt(TMP, lhs_high, TMP);
4564 __ LoadConst32(AT, imm_low);
4565 __ Sltu(AT, AT, lhs_low);
4566 __ Blt(TMP, AT, label);
4567 break;
4568 case kCondB:
4569 __ LoadConst32(TMP, imm_high);
4570 __ Bltu(lhs_high, TMP, label);
4571 __ Sltu(TMP, TMP, lhs_high);
4572 __ LoadConst32(AT, imm_low);
4573 __ Sltu(AT, lhs_low, AT);
4574 __ Blt(TMP, AT, label);
4575 break;
4576 case kCondAE:
4577 __ LoadConst32(TMP, imm_high);
4578 __ Bltu(TMP, lhs_high, label);
4579 __ Sltu(TMP, lhs_high, TMP);
4580 __ LoadConst32(AT, imm_low);
4581 __ Sltu(AT, lhs_low, AT);
4582 __ Or(TMP, TMP, AT);
4583 __ Beqz(TMP, label);
4584 break;
4585 case kCondBE:
4586 __ LoadConst32(TMP, imm_high);
4587 __ Bltu(lhs_high, TMP, label);
4588 __ Sltu(TMP, TMP, lhs_high);
4589 __ LoadConst32(AT, imm_low);
4590 __ Sltu(AT, AT, lhs_low);
4591 __ Or(TMP, TMP, AT);
4592 __ Beqz(TMP, label);
4593 break;
4594 case kCondA:
4595 __ LoadConst32(TMP, imm_high);
4596 __ Bltu(TMP, lhs_high, label);
4597 __ Sltu(TMP, lhs_high, TMP);
4598 __ LoadConst32(AT, imm_low);
4599 __ Sltu(AT, AT, lhs_low);
4600 __ Blt(TMP, AT, label);
4601 break;
4602 }
4603 } else {
4604 switch (cond) {
4605 case kCondEQ:
4606 __ Xor(TMP, lhs_high, rhs_high);
4607 __ Xor(AT, lhs_low, rhs_low);
4608 __ Or(TMP, TMP, AT);
4609 __ Beqz(TMP, label);
4610 break;
4611 case kCondNE:
4612 __ Xor(TMP, lhs_high, rhs_high);
4613 __ Xor(AT, lhs_low, rhs_low);
4614 __ Or(TMP, TMP, AT);
4615 __ Bnez(TMP, label);
4616 break;
4617 case kCondLT:
4618 __ Blt(lhs_high, rhs_high, label);
4619 __ Slt(TMP, rhs_high, lhs_high);
4620 __ Sltu(AT, lhs_low, rhs_low);
4621 __ Blt(TMP, AT, label);
4622 break;
4623 case kCondGE:
4624 __ Blt(rhs_high, lhs_high, label);
4625 __ Slt(TMP, lhs_high, rhs_high);
4626 __ Sltu(AT, lhs_low, rhs_low);
4627 __ Or(TMP, TMP, AT);
4628 __ Beqz(TMP, label);
4629 break;
4630 case kCondLE:
4631 __ Blt(lhs_high, rhs_high, label);
4632 __ Slt(TMP, rhs_high, lhs_high);
4633 __ Sltu(AT, rhs_low, lhs_low);
4634 __ Or(TMP, TMP, AT);
4635 __ Beqz(TMP, label);
4636 break;
4637 case kCondGT:
4638 __ Blt(rhs_high, lhs_high, label);
4639 __ Slt(TMP, lhs_high, rhs_high);
4640 __ Sltu(AT, rhs_low, lhs_low);
4641 __ Blt(TMP, AT, label);
4642 break;
4643 case kCondB:
4644 __ Bltu(lhs_high, rhs_high, label);
4645 __ Sltu(TMP, rhs_high, lhs_high);
4646 __ Sltu(AT, lhs_low, rhs_low);
4647 __ Blt(TMP, AT, label);
4648 break;
4649 case kCondAE:
4650 __ Bltu(rhs_high, lhs_high, label);
4651 __ Sltu(TMP, lhs_high, rhs_high);
4652 __ Sltu(AT, lhs_low, rhs_low);
4653 __ Or(TMP, TMP, AT);
4654 __ Beqz(TMP, label);
4655 break;
4656 case kCondBE:
4657 __ Bltu(lhs_high, rhs_high, label);
4658 __ Sltu(TMP, rhs_high, lhs_high);
4659 __ Sltu(AT, rhs_low, lhs_low);
4660 __ Or(TMP, TMP, AT);
4661 __ Beqz(TMP, label);
4662 break;
4663 case kCondA:
4664 __ Bltu(rhs_high, lhs_high, label);
4665 __ Sltu(TMP, lhs_high, rhs_high);
4666 __ Sltu(AT, rhs_low, lhs_low);
4667 __ Blt(TMP, AT, label);
4668 break;
4669 }
4670 }
4671}
4672
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004673void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4674 bool gt_bias,
4675 Primitive::Type type,
4676 LocationSummary* locations) {
4677 Register dst = locations->Out().AsRegister<Register>();
4678 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4679 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4680 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4681 if (type == Primitive::kPrimFloat) {
4682 if (isR6) {
4683 switch (cond) {
4684 case kCondEQ:
4685 __ CmpEqS(FTMP, lhs, rhs);
4686 __ Mfc1(dst, FTMP);
4687 __ Andi(dst, dst, 1);
4688 break;
4689 case kCondNE:
4690 __ CmpEqS(FTMP, lhs, rhs);
4691 __ Mfc1(dst, FTMP);
4692 __ Addiu(dst, dst, 1);
4693 break;
4694 case kCondLT:
4695 if (gt_bias) {
4696 __ CmpLtS(FTMP, lhs, rhs);
4697 } else {
4698 __ CmpUltS(FTMP, lhs, rhs);
4699 }
4700 __ Mfc1(dst, FTMP);
4701 __ Andi(dst, dst, 1);
4702 break;
4703 case kCondLE:
4704 if (gt_bias) {
4705 __ CmpLeS(FTMP, lhs, rhs);
4706 } else {
4707 __ CmpUleS(FTMP, lhs, rhs);
4708 }
4709 __ Mfc1(dst, FTMP);
4710 __ Andi(dst, dst, 1);
4711 break;
4712 case kCondGT:
4713 if (gt_bias) {
4714 __ CmpUltS(FTMP, rhs, lhs);
4715 } else {
4716 __ CmpLtS(FTMP, rhs, lhs);
4717 }
4718 __ Mfc1(dst, FTMP);
4719 __ Andi(dst, dst, 1);
4720 break;
4721 case kCondGE:
4722 if (gt_bias) {
4723 __ CmpUleS(FTMP, rhs, lhs);
4724 } else {
4725 __ CmpLeS(FTMP, rhs, lhs);
4726 }
4727 __ Mfc1(dst, FTMP);
4728 __ Andi(dst, dst, 1);
4729 break;
4730 default:
4731 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4732 UNREACHABLE();
4733 }
4734 } else {
4735 switch (cond) {
4736 case kCondEQ:
4737 __ CeqS(0, lhs, rhs);
4738 __ LoadConst32(dst, 1);
4739 __ Movf(dst, ZERO, 0);
4740 break;
4741 case kCondNE:
4742 __ CeqS(0, lhs, rhs);
4743 __ LoadConst32(dst, 1);
4744 __ Movt(dst, ZERO, 0);
4745 break;
4746 case kCondLT:
4747 if (gt_bias) {
4748 __ ColtS(0, lhs, rhs);
4749 } else {
4750 __ CultS(0, lhs, rhs);
4751 }
4752 __ LoadConst32(dst, 1);
4753 __ Movf(dst, ZERO, 0);
4754 break;
4755 case kCondLE:
4756 if (gt_bias) {
4757 __ ColeS(0, lhs, rhs);
4758 } else {
4759 __ CuleS(0, lhs, rhs);
4760 }
4761 __ LoadConst32(dst, 1);
4762 __ Movf(dst, ZERO, 0);
4763 break;
4764 case kCondGT:
4765 if (gt_bias) {
4766 __ CultS(0, rhs, lhs);
4767 } else {
4768 __ ColtS(0, rhs, lhs);
4769 }
4770 __ LoadConst32(dst, 1);
4771 __ Movf(dst, ZERO, 0);
4772 break;
4773 case kCondGE:
4774 if (gt_bias) {
4775 __ CuleS(0, rhs, lhs);
4776 } else {
4777 __ ColeS(0, rhs, lhs);
4778 }
4779 __ LoadConst32(dst, 1);
4780 __ Movf(dst, ZERO, 0);
4781 break;
4782 default:
4783 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4784 UNREACHABLE();
4785 }
4786 }
4787 } else {
4788 DCHECK_EQ(type, Primitive::kPrimDouble);
4789 if (isR6) {
4790 switch (cond) {
4791 case kCondEQ:
4792 __ CmpEqD(FTMP, lhs, rhs);
4793 __ Mfc1(dst, FTMP);
4794 __ Andi(dst, dst, 1);
4795 break;
4796 case kCondNE:
4797 __ CmpEqD(FTMP, lhs, rhs);
4798 __ Mfc1(dst, FTMP);
4799 __ Addiu(dst, dst, 1);
4800 break;
4801 case kCondLT:
4802 if (gt_bias) {
4803 __ CmpLtD(FTMP, lhs, rhs);
4804 } else {
4805 __ CmpUltD(FTMP, lhs, rhs);
4806 }
4807 __ Mfc1(dst, FTMP);
4808 __ Andi(dst, dst, 1);
4809 break;
4810 case kCondLE:
4811 if (gt_bias) {
4812 __ CmpLeD(FTMP, lhs, rhs);
4813 } else {
4814 __ CmpUleD(FTMP, lhs, rhs);
4815 }
4816 __ Mfc1(dst, FTMP);
4817 __ Andi(dst, dst, 1);
4818 break;
4819 case kCondGT:
4820 if (gt_bias) {
4821 __ CmpUltD(FTMP, rhs, lhs);
4822 } else {
4823 __ CmpLtD(FTMP, rhs, lhs);
4824 }
4825 __ Mfc1(dst, FTMP);
4826 __ Andi(dst, dst, 1);
4827 break;
4828 case kCondGE:
4829 if (gt_bias) {
4830 __ CmpUleD(FTMP, rhs, lhs);
4831 } else {
4832 __ CmpLeD(FTMP, rhs, lhs);
4833 }
4834 __ Mfc1(dst, FTMP);
4835 __ Andi(dst, dst, 1);
4836 break;
4837 default:
4838 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4839 UNREACHABLE();
4840 }
4841 } else {
4842 switch (cond) {
4843 case kCondEQ:
4844 __ CeqD(0, lhs, rhs);
4845 __ LoadConst32(dst, 1);
4846 __ Movf(dst, ZERO, 0);
4847 break;
4848 case kCondNE:
4849 __ CeqD(0, lhs, rhs);
4850 __ LoadConst32(dst, 1);
4851 __ Movt(dst, ZERO, 0);
4852 break;
4853 case kCondLT:
4854 if (gt_bias) {
4855 __ ColtD(0, lhs, rhs);
4856 } else {
4857 __ CultD(0, lhs, rhs);
4858 }
4859 __ LoadConst32(dst, 1);
4860 __ Movf(dst, ZERO, 0);
4861 break;
4862 case kCondLE:
4863 if (gt_bias) {
4864 __ ColeD(0, lhs, rhs);
4865 } else {
4866 __ CuleD(0, lhs, rhs);
4867 }
4868 __ LoadConst32(dst, 1);
4869 __ Movf(dst, ZERO, 0);
4870 break;
4871 case kCondGT:
4872 if (gt_bias) {
4873 __ CultD(0, rhs, lhs);
4874 } else {
4875 __ ColtD(0, rhs, lhs);
4876 }
4877 __ LoadConst32(dst, 1);
4878 __ Movf(dst, ZERO, 0);
4879 break;
4880 case kCondGE:
4881 if (gt_bias) {
4882 __ CuleD(0, rhs, lhs);
4883 } else {
4884 __ ColeD(0, rhs, lhs);
4885 }
4886 __ LoadConst32(dst, 1);
4887 __ Movf(dst, ZERO, 0);
4888 break;
4889 default:
4890 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4891 UNREACHABLE();
4892 }
4893 }
4894 }
4895}
4896
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004897bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4898 bool gt_bias,
4899 Primitive::Type type,
4900 LocationSummary* input_locations,
4901 int cc) {
4902 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4903 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4904 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4905 if (type == Primitive::kPrimFloat) {
4906 switch (cond) {
4907 case kCondEQ:
4908 __ CeqS(cc, lhs, rhs);
4909 return false;
4910 case kCondNE:
4911 __ CeqS(cc, lhs, rhs);
4912 return true;
4913 case kCondLT:
4914 if (gt_bias) {
4915 __ ColtS(cc, lhs, rhs);
4916 } else {
4917 __ CultS(cc, lhs, rhs);
4918 }
4919 return false;
4920 case kCondLE:
4921 if (gt_bias) {
4922 __ ColeS(cc, lhs, rhs);
4923 } else {
4924 __ CuleS(cc, lhs, rhs);
4925 }
4926 return false;
4927 case kCondGT:
4928 if (gt_bias) {
4929 __ CultS(cc, rhs, lhs);
4930 } else {
4931 __ ColtS(cc, rhs, lhs);
4932 }
4933 return false;
4934 case kCondGE:
4935 if (gt_bias) {
4936 __ CuleS(cc, rhs, lhs);
4937 } else {
4938 __ ColeS(cc, rhs, lhs);
4939 }
4940 return false;
4941 default:
4942 LOG(FATAL) << "Unexpected non-floating-point condition";
4943 UNREACHABLE();
4944 }
4945 } else {
4946 DCHECK_EQ(type, Primitive::kPrimDouble);
4947 switch (cond) {
4948 case kCondEQ:
4949 __ CeqD(cc, lhs, rhs);
4950 return false;
4951 case kCondNE:
4952 __ CeqD(cc, lhs, rhs);
4953 return true;
4954 case kCondLT:
4955 if (gt_bias) {
4956 __ ColtD(cc, lhs, rhs);
4957 } else {
4958 __ CultD(cc, lhs, rhs);
4959 }
4960 return false;
4961 case kCondLE:
4962 if (gt_bias) {
4963 __ ColeD(cc, lhs, rhs);
4964 } else {
4965 __ CuleD(cc, lhs, rhs);
4966 }
4967 return false;
4968 case kCondGT:
4969 if (gt_bias) {
4970 __ CultD(cc, rhs, lhs);
4971 } else {
4972 __ ColtD(cc, rhs, lhs);
4973 }
4974 return false;
4975 case kCondGE:
4976 if (gt_bias) {
4977 __ CuleD(cc, rhs, lhs);
4978 } else {
4979 __ ColeD(cc, rhs, lhs);
4980 }
4981 return false;
4982 default:
4983 LOG(FATAL) << "Unexpected non-floating-point condition";
4984 UNREACHABLE();
4985 }
4986 }
4987}
4988
4989bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4990 bool gt_bias,
4991 Primitive::Type type,
4992 LocationSummary* input_locations,
4993 FRegister dst) {
4994 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4995 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4996 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4997 if (type == Primitive::kPrimFloat) {
4998 switch (cond) {
4999 case kCondEQ:
5000 __ CmpEqS(dst, lhs, rhs);
5001 return false;
5002 case kCondNE:
5003 __ CmpEqS(dst, lhs, rhs);
5004 return true;
5005 case kCondLT:
5006 if (gt_bias) {
5007 __ CmpLtS(dst, lhs, rhs);
5008 } else {
5009 __ CmpUltS(dst, lhs, rhs);
5010 }
5011 return false;
5012 case kCondLE:
5013 if (gt_bias) {
5014 __ CmpLeS(dst, lhs, rhs);
5015 } else {
5016 __ CmpUleS(dst, lhs, rhs);
5017 }
5018 return false;
5019 case kCondGT:
5020 if (gt_bias) {
5021 __ CmpUltS(dst, rhs, lhs);
5022 } else {
5023 __ CmpLtS(dst, rhs, lhs);
5024 }
5025 return false;
5026 case kCondGE:
5027 if (gt_bias) {
5028 __ CmpUleS(dst, rhs, lhs);
5029 } else {
5030 __ CmpLeS(dst, rhs, lhs);
5031 }
5032 return false;
5033 default:
5034 LOG(FATAL) << "Unexpected non-floating-point condition";
5035 UNREACHABLE();
5036 }
5037 } else {
5038 DCHECK_EQ(type, Primitive::kPrimDouble);
5039 switch (cond) {
5040 case kCondEQ:
5041 __ CmpEqD(dst, lhs, rhs);
5042 return false;
5043 case kCondNE:
5044 __ CmpEqD(dst, lhs, rhs);
5045 return true;
5046 case kCondLT:
5047 if (gt_bias) {
5048 __ CmpLtD(dst, lhs, rhs);
5049 } else {
5050 __ CmpUltD(dst, lhs, rhs);
5051 }
5052 return false;
5053 case kCondLE:
5054 if (gt_bias) {
5055 __ CmpLeD(dst, lhs, rhs);
5056 } else {
5057 __ CmpUleD(dst, lhs, rhs);
5058 }
5059 return false;
5060 case kCondGT:
5061 if (gt_bias) {
5062 __ CmpUltD(dst, rhs, lhs);
5063 } else {
5064 __ CmpLtD(dst, rhs, lhs);
5065 }
5066 return false;
5067 case kCondGE:
5068 if (gt_bias) {
5069 __ CmpUleD(dst, rhs, lhs);
5070 } else {
5071 __ CmpLeD(dst, rhs, lhs);
5072 }
5073 return false;
5074 default:
5075 LOG(FATAL) << "Unexpected non-floating-point condition";
5076 UNREACHABLE();
5077 }
5078 }
5079}
5080
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005081void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5082 bool gt_bias,
5083 Primitive::Type type,
5084 LocationSummary* locations,
5085 MipsLabel* label) {
5086 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5087 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5088 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5089 if (type == Primitive::kPrimFloat) {
5090 if (isR6) {
5091 switch (cond) {
5092 case kCondEQ:
5093 __ CmpEqS(FTMP, lhs, rhs);
5094 __ Bc1nez(FTMP, label);
5095 break;
5096 case kCondNE:
5097 __ CmpEqS(FTMP, lhs, rhs);
5098 __ Bc1eqz(FTMP, label);
5099 break;
5100 case kCondLT:
5101 if (gt_bias) {
5102 __ CmpLtS(FTMP, lhs, rhs);
5103 } else {
5104 __ CmpUltS(FTMP, lhs, rhs);
5105 }
5106 __ Bc1nez(FTMP, label);
5107 break;
5108 case kCondLE:
5109 if (gt_bias) {
5110 __ CmpLeS(FTMP, lhs, rhs);
5111 } else {
5112 __ CmpUleS(FTMP, lhs, rhs);
5113 }
5114 __ Bc1nez(FTMP, label);
5115 break;
5116 case kCondGT:
5117 if (gt_bias) {
5118 __ CmpUltS(FTMP, rhs, lhs);
5119 } else {
5120 __ CmpLtS(FTMP, rhs, lhs);
5121 }
5122 __ Bc1nez(FTMP, label);
5123 break;
5124 case kCondGE:
5125 if (gt_bias) {
5126 __ CmpUleS(FTMP, rhs, lhs);
5127 } else {
5128 __ CmpLeS(FTMP, rhs, lhs);
5129 }
5130 __ Bc1nez(FTMP, label);
5131 break;
5132 default:
5133 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005134 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005135 }
5136 } else {
5137 switch (cond) {
5138 case kCondEQ:
5139 __ CeqS(0, lhs, rhs);
5140 __ Bc1t(0, label);
5141 break;
5142 case kCondNE:
5143 __ CeqS(0, lhs, rhs);
5144 __ Bc1f(0, label);
5145 break;
5146 case kCondLT:
5147 if (gt_bias) {
5148 __ ColtS(0, lhs, rhs);
5149 } else {
5150 __ CultS(0, lhs, rhs);
5151 }
5152 __ Bc1t(0, label);
5153 break;
5154 case kCondLE:
5155 if (gt_bias) {
5156 __ ColeS(0, lhs, rhs);
5157 } else {
5158 __ CuleS(0, lhs, rhs);
5159 }
5160 __ Bc1t(0, label);
5161 break;
5162 case kCondGT:
5163 if (gt_bias) {
5164 __ CultS(0, rhs, lhs);
5165 } else {
5166 __ ColtS(0, rhs, lhs);
5167 }
5168 __ Bc1t(0, label);
5169 break;
5170 case kCondGE:
5171 if (gt_bias) {
5172 __ CuleS(0, rhs, lhs);
5173 } else {
5174 __ ColeS(0, rhs, lhs);
5175 }
5176 __ Bc1t(0, label);
5177 break;
5178 default:
5179 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005180 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005181 }
5182 }
5183 } else {
5184 DCHECK_EQ(type, Primitive::kPrimDouble);
5185 if (isR6) {
5186 switch (cond) {
5187 case kCondEQ:
5188 __ CmpEqD(FTMP, lhs, rhs);
5189 __ Bc1nez(FTMP, label);
5190 break;
5191 case kCondNE:
5192 __ CmpEqD(FTMP, lhs, rhs);
5193 __ Bc1eqz(FTMP, label);
5194 break;
5195 case kCondLT:
5196 if (gt_bias) {
5197 __ CmpLtD(FTMP, lhs, rhs);
5198 } else {
5199 __ CmpUltD(FTMP, lhs, rhs);
5200 }
5201 __ Bc1nez(FTMP, label);
5202 break;
5203 case kCondLE:
5204 if (gt_bias) {
5205 __ CmpLeD(FTMP, lhs, rhs);
5206 } else {
5207 __ CmpUleD(FTMP, lhs, rhs);
5208 }
5209 __ Bc1nez(FTMP, label);
5210 break;
5211 case kCondGT:
5212 if (gt_bias) {
5213 __ CmpUltD(FTMP, rhs, lhs);
5214 } else {
5215 __ CmpLtD(FTMP, rhs, lhs);
5216 }
5217 __ Bc1nez(FTMP, label);
5218 break;
5219 case kCondGE:
5220 if (gt_bias) {
5221 __ CmpUleD(FTMP, rhs, lhs);
5222 } else {
5223 __ CmpLeD(FTMP, rhs, lhs);
5224 }
5225 __ Bc1nez(FTMP, label);
5226 break;
5227 default:
5228 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005229 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005230 }
5231 } else {
5232 switch (cond) {
5233 case kCondEQ:
5234 __ CeqD(0, lhs, rhs);
5235 __ Bc1t(0, label);
5236 break;
5237 case kCondNE:
5238 __ CeqD(0, lhs, rhs);
5239 __ Bc1f(0, label);
5240 break;
5241 case kCondLT:
5242 if (gt_bias) {
5243 __ ColtD(0, lhs, rhs);
5244 } else {
5245 __ CultD(0, lhs, rhs);
5246 }
5247 __ Bc1t(0, label);
5248 break;
5249 case kCondLE:
5250 if (gt_bias) {
5251 __ ColeD(0, lhs, rhs);
5252 } else {
5253 __ CuleD(0, lhs, rhs);
5254 }
5255 __ Bc1t(0, label);
5256 break;
5257 case kCondGT:
5258 if (gt_bias) {
5259 __ CultD(0, rhs, lhs);
5260 } else {
5261 __ ColtD(0, rhs, lhs);
5262 }
5263 __ Bc1t(0, label);
5264 break;
5265 case kCondGE:
5266 if (gt_bias) {
5267 __ CuleD(0, rhs, lhs);
5268 } else {
5269 __ ColeD(0, rhs, lhs);
5270 }
5271 __ Bc1t(0, label);
5272 break;
5273 default:
5274 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005275 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005276 }
5277 }
5278 }
5279}
5280
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005281void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005282 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005283 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005284 MipsLabel* false_target) {
5285 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005286
David Brazdil0debae72015-11-12 18:37:00 +00005287 if (true_target == nullptr && false_target == nullptr) {
5288 // Nothing to do. The code always falls through.
5289 return;
5290 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005291 // Constant condition, statically compared against "true" (integer value 1).
5292 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005293 if (true_target != nullptr) {
5294 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005295 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005296 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005297 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005298 if (false_target != nullptr) {
5299 __ B(false_target);
5300 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005301 }
David Brazdil0debae72015-11-12 18:37:00 +00005302 return;
5303 }
5304
5305 // The following code generates these patterns:
5306 // (1) true_target == nullptr && false_target != nullptr
5307 // - opposite condition true => branch to false_target
5308 // (2) true_target != nullptr && false_target == nullptr
5309 // - condition true => branch to true_target
5310 // (3) true_target != nullptr && false_target != nullptr
5311 // - condition true => branch to true_target
5312 // - branch to false_target
5313 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005314 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005315 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005316 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005317 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005318 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5319 } else {
5320 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5321 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005322 } else {
5323 // The condition instruction has not been materialized, use its inputs as
5324 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005325 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005326 Primitive::Type type = condition->InputAt(0)->GetType();
5327 LocationSummary* locations = cond->GetLocations();
5328 IfCondition if_cond = condition->GetCondition();
5329 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005330
David Brazdil0debae72015-11-12 18:37:00 +00005331 if (true_target == nullptr) {
5332 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005333 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005334 }
5335
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005336 switch (type) {
5337 default:
5338 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5339 break;
5340 case Primitive::kPrimLong:
5341 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5342 break;
5343 case Primitive::kPrimFloat:
5344 case Primitive::kPrimDouble:
5345 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5346 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005347 }
5348 }
David Brazdil0debae72015-11-12 18:37:00 +00005349
5350 // If neither branch falls through (case 3), the conditional branch to `true_target`
5351 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5352 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005353 __ B(false_target);
5354 }
5355}
5356
5357void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5358 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005359 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005360 locations->SetInAt(0, Location::RequiresRegister());
5361 }
5362}
5363
5364void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005365 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5366 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5367 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5368 nullptr : codegen_->GetLabelOf(true_successor);
5369 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5370 nullptr : codegen_->GetLabelOf(false_successor);
5371 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005372}
5373
5374void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5375 LocationSummary* locations = new (GetGraph()->GetArena())
5376 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005377 InvokeRuntimeCallingConvention calling_convention;
5378 RegisterSet caller_saves = RegisterSet::Empty();
5379 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5380 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005381 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005382 locations->SetInAt(0, Location::RequiresRegister());
5383 }
5384}
5385
5386void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005387 SlowPathCodeMIPS* slow_path =
5388 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005389 GenerateTestAndBranch(deoptimize,
5390 /* condition_input_index */ 0,
5391 slow_path->GetEntryLabel(),
5392 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005393}
5394
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005395// This function returns true if a conditional move can be generated for HSelect.
5396// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5397// branches and regular moves.
5398//
5399// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5400//
5401// While determining feasibility of a conditional move and setting inputs/outputs
5402// are two distinct tasks, this function does both because they share quite a bit
5403// of common logic.
5404static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5405 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5406 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5407 HCondition* condition = cond->AsCondition();
5408
5409 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5410 Primitive::Type dst_type = select->GetType();
5411
5412 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5413 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5414 bool is_true_value_zero_constant =
5415 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5416 bool is_false_value_zero_constant =
5417 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5418
5419 bool can_move_conditionally = false;
5420 bool use_const_for_false_in = false;
5421 bool use_const_for_true_in = false;
5422
5423 if (!cond->IsConstant()) {
5424 switch (cond_type) {
5425 default:
5426 switch (dst_type) {
5427 default:
5428 // Moving int on int condition.
5429 if (is_r6) {
5430 if (is_true_value_zero_constant) {
5431 // seleqz out_reg, false_reg, cond_reg
5432 can_move_conditionally = true;
5433 use_const_for_true_in = true;
5434 } else if (is_false_value_zero_constant) {
5435 // selnez out_reg, true_reg, cond_reg
5436 can_move_conditionally = true;
5437 use_const_for_false_in = true;
5438 } else if (materialized) {
5439 // Not materializing unmaterialized int conditions
5440 // to keep the instruction count low.
5441 // selnez AT, true_reg, cond_reg
5442 // seleqz TMP, false_reg, cond_reg
5443 // or out_reg, AT, TMP
5444 can_move_conditionally = true;
5445 }
5446 } else {
5447 // movn out_reg, true_reg/ZERO, cond_reg
5448 can_move_conditionally = true;
5449 use_const_for_true_in = is_true_value_zero_constant;
5450 }
5451 break;
5452 case Primitive::kPrimLong:
5453 // Moving long on int condition.
5454 if (is_r6) {
5455 if (is_true_value_zero_constant) {
5456 // seleqz out_reg_lo, false_reg_lo, cond_reg
5457 // seleqz out_reg_hi, false_reg_hi, cond_reg
5458 can_move_conditionally = true;
5459 use_const_for_true_in = true;
5460 } else if (is_false_value_zero_constant) {
5461 // selnez out_reg_lo, true_reg_lo, cond_reg
5462 // selnez out_reg_hi, true_reg_hi, cond_reg
5463 can_move_conditionally = true;
5464 use_const_for_false_in = true;
5465 }
5466 // Other long conditional moves would generate 6+ instructions,
5467 // which is too many.
5468 } else {
5469 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5470 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5471 can_move_conditionally = true;
5472 use_const_for_true_in = is_true_value_zero_constant;
5473 }
5474 break;
5475 case Primitive::kPrimFloat:
5476 case Primitive::kPrimDouble:
5477 // Moving float/double on int condition.
5478 if (is_r6) {
5479 if (materialized) {
5480 // Not materializing unmaterialized int conditions
5481 // to keep the instruction count low.
5482 can_move_conditionally = true;
5483 if (is_true_value_zero_constant) {
5484 // sltu TMP, ZERO, cond_reg
5485 // mtc1 TMP, temp_cond_reg
5486 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5487 use_const_for_true_in = true;
5488 } else if (is_false_value_zero_constant) {
5489 // sltu TMP, ZERO, cond_reg
5490 // mtc1 TMP, temp_cond_reg
5491 // selnez.fmt out_reg, true_reg, temp_cond_reg
5492 use_const_for_false_in = true;
5493 } else {
5494 // sltu TMP, ZERO, cond_reg
5495 // mtc1 TMP, temp_cond_reg
5496 // sel.fmt temp_cond_reg, false_reg, true_reg
5497 // mov.fmt out_reg, temp_cond_reg
5498 }
5499 }
5500 } else {
5501 // movn.fmt out_reg, true_reg, cond_reg
5502 can_move_conditionally = true;
5503 }
5504 break;
5505 }
5506 break;
5507 case Primitive::kPrimLong:
5508 // We don't materialize long comparison now
5509 // and use conditional branches instead.
5510 break;
5511 case Primitive::kPrimFloat:
5512 case Primitive::kPrimDouble:
5513 switch (dst_type) {
5514 default:
5515 // Moving int on float/double condition.
5516 if (is_r6) {
5517 if (is_true_value_zero_constant) {
5518 // mfc1 TMP, temp_cond_reg
5519 // seleqz out_reg, false_reg, TMP
5520 can_move_conditionally = true;
5521 use_const_for_true_in = true;
5522 } else if (is_false_value_zero_constant) {
5523 // mfc1 TMP, temp_cond_reg
5524 // selnez out_reg, true_reg, TMP
5525 can_move_conditionally = true;
5526 use_const_for_false_in = true;
5527 } else {
5528 // mfc1 TMP, temp_cond_reg
5529 // selnez AT, true_reg, TMP
5530 // seleqz TMP, false_reg, TMP
5531 // or out_reg, AT, TMP
5532 can_move_conditionally = true;
5533 }
5534 } else {
5535 // movt out_reg, true_reg/ZERO, cc
5536 can_move_conditionally = true;
5537 use_const_for_true_in = is_true_value_zero_constant;
5538 }
5539 break;
5540 case Primitive::kPrimLong:
5541 // Moving long on float/double condition.
5542 if (is_r6) {
5543 if (is_true_value_zero_constant) {
5544 // mfc1 TMP, temp_cond_reg
5545 // seleqz out_reg_lo, false_reg_lo, TMP
5546 // seleqz out_reg_hi, false_reg_hi, TMP
5547 can_move_conditionally = true;
5548 use_const_for_true_in = true;
5549 } else if (is_false_value_zero_constant) {
5550 // mfc1 TMP, temp_cond_reg
5551 // selnez out_reg_lo, true_reg_lo, TMP
5552 // selnez out_reg_hi, true_reg_hi, TMP
5553 can_move_conditionally = true;
5554 use_const_for_false_in = true;
5555 }
5556 // Other long conditional moves would generate 6+ instructions,
5557 // which is too many.
5558 } else {
5559 // movt out_reg_lo, true_reg_lo/ZERO, cc
5560 // movt out_reg_hi, true_reg_hi/ZERO, cc
5561 can_move_conditionally = true;
5562 use_const_for_true_in = is_true_value_zero_constant;
5563 }
5564 break;
5565 case Primitive::kPrimFloat:
5566 case Primitive::kPrimDouble:
5567 // Moving float/double on float/double condition.
5568 if (is_r6) {
5569 can_move_conditionally = true;
5570 if (is_true_value_zero_constant) {
5571 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5572 use_const_for_true_in = true;
5573 } else if (is_false_value_zero_constant) {
5574 // selnez.fmt out_reg, true_reg, temp_cond_reg
5575 use_const_for_false_in = true;
5576 } else {
5577 // sel.fmt temp_cond_reg, false_reg, true_reg
5578 // mov.fmt out_reg, temp_cond_reg
5579 }
5580 } else {
5581 // movt.fmt out_reg, true_reg, cc
5582 can_move_conditionally = true;
5583 }
5584 break;
5585 }
5586 break;
5587 }
5588 }
5589
5590 if (can_move_conditionally) {
5591 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5592 } else {
5593 DCHECK(!use_const_for_false_in);
5594 DCHECK(!use_const_for_true_in);
5595 }
5596
5597 if (locations_to_set != nullptr) {
5598 if (use_const_for_false_in) {
5599 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5600 } else {
5601 locations_to_set->SetInAt(0,
5602 Primitive::IsFloatingPointType(dst_type)
5603 ? Location::RequiresFpuRegister()
5604 : Location::RequiresRegister());
5605 }
5606 if (use_const_for_true_in) {
5607 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5608 } else {
5609 locations_to_set->SetInAt(1,
5610 Primitive::IsFloatingPointType(dst_type)
5611 ? Location::RequiresFpuRegister()
5612 : Location::RequiresRegister());
5613 }
5614 if (materialized) {
5615 locations_to_set->SetInAt(2, Location::RequiresRegister());
5616 }
5617 // On R6 we don't require the output to be the same as the
5618 // first input for conditional moves unlike on R2.
5619 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5620 if (is_out_same_as_first_in) {
5621 locations_to_set->SetOut(Location::SameAsFirstInput());
5622 } else {
5623 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5624 ? Location::RequiresFpuRegister()
5625 : Location::RequiresRegister());
5626 }
5627 }
5628
5629 return can_move_conditionally;
5630}
5631
5632void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5633 LocationSummary* locations = select->GetLocations();
5634 Location dst = locations->Out();
5635 Location src = locations->InAt(1);
5636 Register src_reg = ZERO;
5637 Register src_reg_high = ZERO;
5638 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5639 Register cond_reg = TMP;
5640 int cond_cc = 0;
5641 Primitive::Type cond_type = Primitive::kPrimInt;
5642 bool cond_inverted = false;
5643 Primitive::Type dst_type = select->GetType();
5644
5645 if (IsBooleanValueOrMaterializedCondition(cond)) {
5646 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5647 } else {
5648 HCondition* condition = cond->AsCondition();
5649 LocationSummary* cond_locations = cond->GetLocations();
5650 IfCondition if_cond = condition->GetCondition();
5651 cond_type = condition->InputAt(0)->GetType();
5652 switch (cond_type) {
5653 default:
5654 DCHECK_NE(cond_type, Primitive::kPrimLong);
5655 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5656 break;
5657 case Primitive::kPrimFloat:
5658 case Primitive::kPrimDouble:
5659 cond_inverted = MaterializeFpCompareR2(if_cond,
5660 condition->IsGtBias(),
5661 cond_type,
5662 cond_locations,
5663 cond_cc);
5664 break;
5665 }
5666 }
5667
5668 DCHECK(dst.Equals(locations->InAt(0)));
5669 if (src.IsRegister()) {
5670 src_reg = src.AsRegister<Register>();
5671 } else if (src.IsRegisterPair()) {
5672 src_reg = src.AsRegisterPairLow<Register>();
5673 src_reg_high = src.AsRegisterPairHigh<Register>();
5674 } else if (src.IsConstant()) {
5675 DCHECK(src.GetConstant()->IsZeroBitPattern());
5676 }
5677
5678 switch (cond_type) {
5679 default:
5680 switch (dst_type) {
5681 default:
5682 if (cond_inverted) {
5683 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5684 } else {
5685 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5686 }
5687 break;
5688 case Primitive::kPrimLong:
5689 if (cond_inverted) {
5690 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5691 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5692 } else {
5693 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5694 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5695 }
5696 break;
5697 case Primitive::kPrimFloat:
5698 if (cond_inverted) {
5699 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5700 } else {
5701 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5702 }
5703 break;
5704 case Primitive::kPrimDouble:
5705 if (cond_inverted) {
5706 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5707 } else {
5708 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5709 }
5710 break;
5711 }
5712 break;
5713 case Primitive::kPrimLong:
5714 LOG(FATAL) << "Unreachable";
5715 UNREACHABLE();
5716 case Primitive::kPrimFloat:
5717 case Primitive::kPrimDouble:
5718 switch (dst_type) {
5719 default:
5720 if (cond_inverted) {
5721 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5722 } else {
5723 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5724 }
5725 break;
5726 case Primitive::kPrimLong:
5727 if (cond_inverted) {
5728 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5729 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5730 } else {
5731 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5732 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5733 }
5734 break;
5735 case Primitive::kPrimFloat:
5736 if (cond_inverted) {
5737 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5738 } else {
5739 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5740 }
5741 break;
5742 case Primitive::kPrimDouble:
5743 if (cond_inverted) {
5744 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5745 } else {
5746 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5747 }
5748 break;
5749 }
5750 break;
5751 }
5752}
5753
5754void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5755 LocationSummary* locations = select->GetLocations();
5756 Location dst = locations->Out();
5757 Location false_src = locations->InAt(0);
5758 Location true_src = locations->InAt(1);
5759 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5760 Register cond_reg = TMP;
5761 FRegister fcond_reg = FTMP;
5762 Primitive::Type cond_type = Primitive::kPrimInt;
5763 bool cond_inverted = false;
5764 Primitive::Type dst_type = select->GetType();
5765
5766 if (IsBooleanValueOrMaterializedCondition(cond)) {
5767 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5768 } else {
5769 HCondition* condition = cond->AsCondition();
5770 LocationSummary* cond_locations = cond->GetLocations();
5771 IfCondition if_cond = condition->GetCondition();
5772 cond_type = condition->InputAt(0)->GetType();
5773 switch (cond_type) {
5774 default:
5775 DCHECK_NE(cond_type, Primitive::kPrimLong);
5776 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5777 break;
5778 case Primitive::kPrimFloat:
5779 case Primitive::kPrimDouble:
5780 cond_inverted = MaterializeFpCompareR6(if_cond,
5781 condition->IsGtBias(),
5782 cond_type,
5783 cond_locations,
5784 fcond_reg);
5785 break;
5786 }
5787 }
5788
5789 if (true_src.IsConstant()) {
5790 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5791 }
5792 if (false_src.IsConstant()) {
5793 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5794 }
5795
5796 switch (dst_type) {
5797 default:
5798 if (Primitive::IsFloatingPointType(cond_type)) {
5799 __ Mfc1(cond_reg, fcond_reg);
5800 }
5801 if (true_src.IsConstant()) {
5802 if (cond_inverted) {
5803 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5804 } else {
5805 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5806 }
5807 } else if (false_src.IsConstant()) {
5808 if (cond_inverted) {
5809 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5810 } else {
5811 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5812 }
5813 } else {
5814 DCHECK_NE(cond_reg, AT);
5815 if (cond_inverted) {
5816 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5817 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5818 } else {
5819 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5820 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5821 }
5822 __ Or(dst.AsRegister<Register>(), AT, TMP);
5823 }
5824 break;
5825 case Primitive::kPrimLong: {
5826 if (Primitive::IsFloatingPointType(cond_type)) {
5827 __ Mfc1(cond_reg, fcond_reg);
5828 }
5829 Register dst_lo = dst.AsRegisterPairLow<Register>();
5830 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5831 if (true_src.IsConstant()) {
5832 Register src_lo = false_src.AsRegisterPairLow<Register>();
5833 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5834 if (cond_inverted) {
5835 __ Selnez(dst_lo, src_lo, cond_reg);
5836 __ Selnez(dst_hi, src_hi, cond_reg);
5837 } else {
5838 __ Seleqz(dst_lo, src_lo, cond_reg);
5839 __ Seleqz(dst_hi, src_hi, cond_reg);
5840 }
5841 } else {
5842 DCHECK(false_src.IsConstant());
5843 Register src_lo = true_src.AsRegisterPairLow<Register>();
5844 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5845 if (cond_inverted) {
5846 __ Seleqz(dst_lo, src_lo, cond_reg);
5847 __ Seleqz(dst_hi, src_hi, cond_reg);
5848 } else {
5849 __ Selnez(dst_lo, src_lo, cond_reg);
5850 __ Selnez(dst_hi, src_hi, cond_reg);
5851 }
5852 }
5853 break;
5854 }
5855 case Primitive::kPrimFloat: {
5856 if (!Primitive::IsFloatingPointType(cond_type)) {
5857 // sel*.fmt tests bit 0 of the condition register, account for that.
5858 __ Sltu(TMP, ZERO, cond_reg);
5859 __ Mtc1(TMP, fcond_reg);
5860 }
5861 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5862 if (true_src.IsConstant()) {
5863 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5864 if (cond_inverted) {
5865 __ SelnezS(dst_reg, src_reg, fcond_reg);
5866 } else {
5867 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5868 }
5869 } else if (false_src.IsConstant()) {
5870 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5871 if (cond_inverted) {
5872 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5873 } else {
5874 __ SelnezS(dst_reg, src_reg, fcond_reg);
5875 }
5876 } else {
5877 if (cond_inverted) {
5878 __ SelS(fcond_reg,
5879 true_src.AsFpuRegister<FRegister>(),
5880 false_src.AsFpuRegister<FRegister>());
5881 } else {
5882 __ SelS(fcond_reg,
5883 false_src.AsFpuRegister<FRegister>(),
5884 true_src.AsFpuRegister<FRegister>());
5885 }
5886 __ MovS(dst_reg, fcond_reg);
5887 }
5888 break;
5889 }
5890 case Primitive::kPrimDouble: {
5891 if (!Primitive::IsFloatingPointType(cond_type)) {
5892 // sel*.fmt tests bit 0 of the condition register, account for that.
5893 __ Sltu(TMP, ZERO, cond_reg);
5894 __ Mtc1(TMP, fcond_reg);
5895 }
5896 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5897 if (true_src.IsConstant()) {
5898 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5899 if (cond_inverted) {
5900 __ SelnezD(dst_reg, src_reg, fcond_reg);
5901 } else {
5902 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5903 }
5904 } else if (false_src.IsConstant()) {
5905 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5906 if (cond_inverted) {
5907 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5908 } else {
5909 __ SelnezD(dst_reg, src_reg, fcond_reg);
5910 }
5911 } else {
5912 if (cond_inverted) {
5913 __ SelD(fcond_reg,
5914 true_src.AsFpuRegister<FRegister>(),
5915 false_src.AsFpuRegister<FRegister>());
5916 } else {
5917 __ SelD(fcond_reg,
5918 false_src.AsFpuRegister<FRegister>(),
5919 true_src.AsFpuRegister<FRegister>());
5920 }
5921 __ MovD(dst_reg, fcond_reg);
5922 }
5923 break;
5924 }
5925 }
5926}
5927
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005928void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5929 LocationSummary* locations = new (GetGraph()->GetArena())
5930 LocationSummary(flag, LocationSummary::kNoCall);
5931 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005932}
5933
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005934void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5935 __ LoadFromOffset(kLoadWord,
5936 flag->GetLocations()->Out().AsRegister<Register>(),
5937 SP,
5938 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005939}
5940
David Brazdil74eb1b22015-12-14 11:44:01 +00005941void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5942 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005943 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005944}
5945
5946void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005947 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5948 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5949 if (is_r6) {
5950 GenConditionalMoveR6(select);
5951 } else {
5952 GenConditionalMoveR2(select);
5953 }
5954 } else {
5955 LocationSummary* locations = select->GetLocations();
5956 MipsLabel false_target;
5957 GenerateTestAndBranch(select,
5958 /* condition_input_index */ 2,
5959 /* true_target */ nullptr,
5960 &false_target);
5961 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5962 __ Bind(&false_target);
5963 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005964}
5965
David Srbecky0cf44932015-12-09 14:09:59 +00005966void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5967 new (GetGraph()->GetArena()) LocationSummary(info);
5968}
5969
David Srbeckyd28f4a02016-03-14 17:14:24 +00005970void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5971 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005972}
5973
5974void CodeGeneratorMIPS::GenerateNop() {
5975 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005976}
5977
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005978void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5979 Primitive::Type field_type = field_info.GetFieldType();
5980 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5981 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08005982 bool object_field_get_with_read_barrier =
5983 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005984 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08005985 instruction,
5986 generate_volatile
5987 ? LocationSummary::kCallOnMainOnly
5988 : (object_field_get_with_read_barrier
5989 ? LocationSummary::kCallOnSlowPath
5990 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005991
Alexey Frunzec61c0762017-04-10 13:54:23 -07005992 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5993 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5994 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005995 locations->SetInAt(0, Location::RequiresRegister());
5996 if (generate_volatile) {
5997 InvokeRuntimeCallingConvention calling_convention;
5998 // need A0 to hold base + offset
5999 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6000 if (field_type == Primitive::kPrimLong) {
6001 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
6002 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006003 // Use Location::Any() to prevent situations when running out of available fp registers.
6004 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006005 // Need some temp core regs since FP results are returned in core registers
6006 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
6007 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6008 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6009 }
6010 } else {
6011 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6012 locations->SetOut(Location::RequiresFpuRegister());
6013 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006014 // The output overlaps in the case of an object field get with
6015 // read barriers enabled: we do not want the move to overwrite the
6016 // object's location, as we need it to emit the read barrier.
6017 locations->SetOut(Location::RequiresRegister(),
6018 object_field_get_with_read_barrier
6019 ? Location::kOutputOverlap
6020 : Location::kNoOutputOverlap);
6021 }
6022 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6023 // We need a temporary register for the read barrier marking slow
6024 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
6025 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006026 }
6027 }
6028}
6029
6030void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6031 const FieldInfo& field_info,
6032 uint32_t dex_pc) {
6033 Primitive::Type type = field_info.GetFieldType();
6034 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006035 Location obj_loc = locations->InAt(0);
6036 Register obj = obj_loc.AsRegister<Register>();
6037 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006038 LoadOperandType load_type = kLoadUnsignedByte;
6039 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006040 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006041 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006042
6043 switch (type) {
6044 case Primitive::kPrimBoolean:
6045 load_type = kLoadUnsignedByte;
6046 break;
6047 case Primitive::kPrimByte:
6048 load_type = kLoadSignedByte;
6049 break;
6050 case Primitive::kPrimShort:
6051 load_type = kLoadSignedHalfword;
6052 break;
6053 case Primitive::kPrimChar:
6054 load_type = kLoadUnsignedHalfword;
6055 break;
6056 case Primitive::kPrimInt:
6057 case Primitive::kPrimFloat:
6058 case Primitive::kPrimNot:
6059 load_type = kLoadWord;
6060 break;
6061 case Primitive::kPrimLong:
6062 case Primitive::kPrimDouble:
6063 load_type = kLoadDoubleword;
6064 break;
6065 case Primitive::kPrimVoid:
6066 LOG(FATAL) << "Unreachable type " << type;
6067 UNREACHABLE();
6068 }
6069
6070 if (is_volatile && load_type == kLoadDoubleword) {
6071 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006072 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006073 // Do implicit Null check
6074 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6075 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006076 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006077 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6078 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006079 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006080 if (dst_loc.IsFpuRegister()) {
6081 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006082 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006083 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006084 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006085 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006086 __ StoreToOffset(kStoreWord,
6087 locations->GetTemp(1).AsRegister<Register>(),
6088 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006089 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006090 __ StoreToOffset(kStoreWord,
6091 locations->GetTemp(2).AsRegister<Register>(),
6092 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006093 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006094 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006095 }
6096 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006097 if (type == Primitive::kPrimNot) {
6098 // /* HeapReference<Object> */ dst = *(obj + offset)
6099 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6100 Location temp_loc = locations->GetTemp(0);
6101 // Note that a potential implicit null check is handled in this
6102 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6103 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6104 dst_loc,
6105 obj,
6106 offset,
6107 temp_loc,
6108 /* needs_null_check */ true);
6109 if (is_volatile) {
6110 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6111 }
6112 } else {
6113 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6114 if (is_volatile) {
6115 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6116 }
6117 // If read barriers are enabled, emit read barriers other than
6118 // Baker's using a slow path (and also unpoison the loaded
6119 // reference, if heap poisoning is enabled).
6120 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6121 }
6122 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006123 Register dst;
6124 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006125 DCHECK(dst_loc.IsRegisterPair());
6126 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006127 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006128 DCHECK(dst_loc.IsRegister());
6129 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006130 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006131 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006132 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006133 DCHECK(dst_loc.IsFpuRegister());
6134 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006135 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006136 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006137 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006138 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006139 }
6140 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006141 }
6142
Alexey Frunze15958152017-02-09 19:08:30 -08006143 // Memory barriers, in the case of references, are handled in the
6144 // previous switch statement.
6145 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006146 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6147 }
6148}
6149
6150void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6151 Primitive::Type field_type = field_info.GetFieldType();
6152 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6153 bool generate_volatile = field_info.IsVolatile() && is_wide;
6154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006155 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006156
6157 locations->SetInAt(0, Location::RequiresRegister());
6158 if (generate_volatile) {
6159 InvokeRuntimeCallingConvention calling_convention;
6160 // need A0 to hold base + offset
6161 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6162 if (field_type == Primitive::kPrimLong) {
6163 locations->SetInAt(1, Location::RegisterPairLocation(
6164 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6165 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006166 // Use Location::Any() to prevent situations when running out of available fp registers.
6167 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006168 // Pass FP parameters in core registers.
6169 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6170 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6171 }
6172 } else {
6173 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006174 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006175 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006176 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006177 }
6178 }
6179}
6180
6181void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6182 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006183 uint32_t dex_pc,
6184 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006185 Primitive::Type type = field_info.GetFieldType();
6186 LocationSummary* locations = instruction->GetLocations();
6187 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006188 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006189 StoreOperandType store_type = kStoreByte;
6190 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006191 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006192 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006193 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006194
6195 switch (type) {
6196 case Primitive::kPrimBoolean:
6197 case Primitive::kPrimByte:
6198 store_type = kStoreByte;
6199 break;
6200 case Primitive::kPrimShort:
6201 case Primitive::kPrimChar:
6202 store_type = kStoreHalfword;
6203 break;
6204 case Primitive::kPrimInt:
6205 case Primitive::kPrimFloat:
6206 case Primitive::kPrimNot:
6207 store_type = kStoreWord;
6208 break;
6209 case Primitive::kPrimLong:
6210 case Primitive::kPrimDouble:
6211 store_type = kStoreDoubleword;
6212 break;
6213 case Primitive::kPrimVoid:
6214 LOG(FATAL) << "Unreachable type " << type;
6215 UNREACHABLE();
6216 }
6217
6218 if (is_volatile) {
6219 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6220 }
6221
6222 if (is_volatile && store_type == kStoreDoubleword) {
6223 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006224 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006225 // Do implicit Null check.
6226 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6227 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6228 if (type == Primitive::kPrimDouble) {
6229 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006230 if (value_location.IsFpuRegister()) {
6231 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6232 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006233 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006234 value_location.AsFpuRegister<FRegister>());
6235 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006236 __ LoadFromOffset(kLoadWord,
6237 locations->GetTemp(1).AsRegister<Register>(),
6238 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006239 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006240 __ LoadFromOffset(kLoadWord,
6241 locations->GetTemp(2).AsRegister<Register>(),
6242 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006243 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006244 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006245 DCHECK(value_location.IsConstant());
6246 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6247 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006248 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6249 locations->GetTemp(1).AsRegister<Register>(),
6250 value);
6251 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006252 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006253 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006254 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6255 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006256 if (value_location.IsConstant()) {
6257 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6258 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6259 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006260 Register src;
6261 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006262 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006263 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006264 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006265 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006266 if (kPoisonHeapReferences && needs_write_barrier) {
6267 // Note that in the case where `value` is a null reference,
6268 // we do not enter this block, as a null reference does not
6269 // need poisoning.
6270 DCHECK_EQ(type, Primitive::kPrimNot);
6271 __ PoisonHeapReference(TMP, src);
6272 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6273 } else {
6274 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6275 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006276 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006277 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006278 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006279 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006280 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006281 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006282 }
6283 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006284 }
6285
Alexey Frunzec061de12017-02-14 13:27:23 -08006286 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006287 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006288 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006289 }
6290
6291 if (is_volatile) {
6292 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6293 }
6294}
6295
6296void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6297 HandleFieldGet(instruction, instruction->GetFieldInfo());
6298}
6299
6300void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6301 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6302}
6303
6304void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6305 HandleFieldSet(instruction, instruction->GetFieldInfo());
6306}
6307
6308void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006309 HandleFieldSet(instruction,
6310 instruction->GetFieldInfo(),
6311 instruction->GetDexPc(),
6312 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006313}
6314
Alexey Frunze15958152017-02-09 19:08:30 -08006315void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6316 HInstruction* instruction,
6317 Location out,
6318 uint32_t offset,
6319 Location maybe_temp,
6320 ReadBarrierOption read_barrier_option) {
6321 Register out_reg = out.AsRegister<Register>();
6322 if (read_barrier_option == kWithReadBarrier) {
6323 CHECK(kEmitCompilerReadBarrier);
6324 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6325 if (kUseBakerReadBarrier) {
6326 // Load with fast path based Baker's read barrier.
6327 // /* HeapReference<Object> */ out = *(out + offset)
6328 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6329 out,
6330 out_reg,
6331 offset,
6332 maybe_temp,
6333 /* needs_null_check */ false);
6334 } else {
6335 // Load with slow path based read barrier.
6336 // Save the value of `out` into `maybe_temp` before overwriting it
6337 // in the following move operation, as we will need it for the
6338 // read barrier below.
6339 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6340 // /* HeapReference<Object> */ out = *(out + offset)
6341 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6342 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6343 }
6344 } else {
6345 // Plain load with no read barrier.
6346 // /* HeapReference<Object> */ out = *(out + offset)
6347 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6348 __ MaybeUnpoisonHeapReference(out_reg);
6349 }
6350}
6351
6352void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6353 HInstruction* instruction,
6354 Location out,
6355 Location obj,
6356 uint32_t offset,
6357 Location maybe_temp,
6358 ReadBarrierOption read_barrier_option) {
6359 Register out_reg = out.AsRegister<Register>();
6360 Register obj_reg = obj.AsRegister<Register>();
6361 if (read_barrier_option == kWithReadBarrier) {
6362 CHECK(kEmitCompilerReadBarrier);
6363 if (kUseBakerReadBarrier) {
6364 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6365 // Load with fast path based Baker's read barrier.
6366 // /* HeapReference<Object> */ out = *(obj + offset)
6367 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6368 out,
6369 obj_reg,
6370 offset,
6371 maybe_temp,
6372 /* needs_null_check */ false);
6373 } else {
6374 // Load with slow path based read barrier.
6375 // /* HeapReference<Object> */ out = *(obj + offset)
6376 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6377 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6378 }
6379 } else {
6380 // Plain load with no read barrier.
6381 // /* HeapReference<Object> */ out = *(obj + offset)
6382 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6383 __ MaybeUnpoisonHeapReference(out_reg);
6384 }
6385}
6386
6387void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6388 Location root,
6389 Register obj,
6390 uint32_t offset,
6391 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006392 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006393 if (read_barrier_option == kWithReadBarrier) {
6394 DCHECK(kEmitCompilerReadBarrier);
6395 if (kUseBakerReadBarrier) {
6396 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6397 // Baker's read barrier are used:
6398 //
6399 // root = obj.field;
6400 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6401 // if (temp != null) {
6402 // root = temp(root)
6403 // }
6404
6405 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6406 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6407 static_assert(
6408 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6409 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6410 "have different sizes.");
6411 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6412 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6413 "have different sizes.");
6414
6415 // Slow path marking the GC root `root`.
6416 Location temp = Location::RegisterLocation(T9);
6417 SlowPathCodeMIPS* slow_path =
6418 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6419 instruction,
6420 root,
6421 /*entrypoint*/ temp);
6422 codegen_->AddSlowPath(slow_path);
6423
6424 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6425 const int32_t entry_point_offset =
6426 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6427 // Loading the entrypoint does not require a load acquire since it is only changed when
6428 // threads are suspended or running a checkpoint.
6429 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6430 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6431 // checking GetIsGcMarking.
6432 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6433 __ Bind(slow_path->GetExitLabel());
6434 } else {
6435 // GC root loaded through a slow path for read barriers other
6436 // than Baker's.
6437 // /* GcRoot<mirror::Object>* */ root = obj + offset
6438 __ Addiu32(root_reg, obj, offset);
6439 // /* mirror::Object* */ root = root->Read()
6440 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6441 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006442 } else {
6443 // Plain GC root load with no read barrier.
6444 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6445 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6446 // Note that GC roots are not affected by heap poisoning, thus we
6447 // do not have to unpoison `root_reg` here.
6448 }
6449}
6450
Alexey Frunze15958152017-02-09 19:08:30 -08006451void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6452 Location ref,
6453 Register obj,
6454 uint32_t offset,
6455 Location temp,
6456 bool needs_null_check) {
6457 DCHECK(kEmitCompilerReadBarrier);
6458 DCHECK(kUseBakerReadBarrier);
6459
6460 // /* HeapReference<Object> */ ref = *(obj + offset)
6461 Location no_index = Location::NoLocation();
6462 ScaleFactor no_scale_factor = TIMES_1;
6463 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6464 ref,
6465 obj,
6466 offset,
6467 no_index,
6468 no_scale_factor,
6469 temp,
6470 needs_null_check);
6471}
6472
6473void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6474 Location ref,
6475 Register obj,
6476 uint32_t data_offset,
6477 Location index,
6478 Location temp,
6479 bool needs_null_check) {
6480 DCHECK(kEmitCompilerReadBarrier);
6481 DCHECK(kUseBakerReadBarrier);
6482
6483 static_assert(
6484 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6485 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6486 // /* HeapReference<Object> */ ref =
6487 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6488 ScaleFactor scale_factor = TIMES_4;
6489 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6490 ref,
6491 obj,
6492 data_offset,
6493 index,
6494 scale_factor,
6495 temp,
6496 needs_null_check);
6497}
6498
6499void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6500 Location ref,
6501 Register obj,
6502 uint32_t offset,
6503 Location index,
6504 ScaleFactor scale_factor,
6505 Location temp,
6506 bool needs_null_check,
6507 bool always_update_field) {
6508 DCHECK(kEmitCompilerReadBarrier);
6509 DCHECK(kUseBakerReadBarrier);
6510
6511 // In slow path based read barriers, the read barrier call is
6512 // inserted after the original load. However, in fast path based
6513 // Baker's read barriers, we need to perform the load of
6514 // mirror::Object::monitor_ *before* the original reference load.
6515 // This load-load ordering is required by the read barrier.
6516 // The fast path/slow path (for Baker's algorithm) should look like:
6517 //
6518 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6519 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6520 // HeapReference<Object> ref = *src; // Original reference load.
6521 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6522 // if (is_gray) {
6523 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6524 // }
6525 //
6526 // Note: the original implementation in ReadBarrier::Barrier is
6527 // slightly more complex as it performs additional checks that we do
6528 // not do here for performance reasons.
6529
6530 Register ref_reg = ref.AsRegister<Register>();
6531 Register temp_reg = temp.AsRegister<Register>();
6532 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6533
6534 // /* int32_t */ monitor = obj->monitor_
6535 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6536 if (needs_null_check) {
6537 MaybeRecordImplicitNullCheck(instruction);
6538 }
6539 // /* LockWord */ lock_word = LockWord(monitor)
6540 static_assert(sizeof(LockWord) == sizeof(int32_t),
6541 "art::LockWord and int32_t have different sizes.");
6542
6543 __ Sync(0); // Barrier to prevent load-load reordering.
6544
6545 // The actual reference load.
6546 if (index.IsValid()) {
6547 // Load types involving an "index": ArrayGet,
6548 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6549 // intrinsics.
6550 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6551 if (index.IsConstant()) {
6552 size_t computed_offset =
6553 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6554 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6555 } else {
6556 // Handle the special case of the
6557 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6558 // intrinsics, which use a register pair as index ("long
6559 // offset"), of which only the low part contains data.
6560 Register index_reg = index.IsRegisterPair()
6561 ? index.AsRegisterPairLow<Register>()
6562 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006563 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006564 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6565 }
6566 } else {
6567 // /* HeapReference<Object> */ ref = *(obj + offset)
6568 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6569 }
6570
6571 // Object* ref = ref_addr->AsMirrorPtr()
6572 __ MaybeUnpoisonHeapReference(ref_reg);
6573
6574 // Slow path marking the object `ref` when it is gray.
6575 SlowPathCodeMIPS* slow_path;
6576 if (always_update_field) {
6577 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6578 // of the form `obj + field_offset`, where `obj` is a register and
6579 // `field_offset` is a register pair (of which only the lower half
6580 // is used). Thus `offset` and `scale_factor` above are expected
6581 // to be null in this code path.
6582 DCHECK_EQ(offset, 0u);
6583 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6584 slow_path = new (GetGraph()->GetArena())
6585 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6586 ref,
6587 obj,
6588 /* field_offset */ index,
6589 temp_reg);
6590 } else {
6591 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6592 }
6593 AddSlowPath(slow_path);
6594
6595 // if (rb_state == ReadBarrier::GrayState())
6596 // ref = ReadBarrier::Mark(ref);
6597 // Given the numeric representation, it's enough to check the low bit of the
6598 // rb_state. We do that by shifting the bit into the sign bit (31) and
6599 // performing a branch on less than zero.
6600 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6601 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6602 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6603 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6604 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6605 __ Bind(slow_path->GetExitLabel());
6606}
6607
6608void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6609 Location out,
6610 Location ref,
6611 Location obj,
6612 uint32_t offset,
6613 Location index) {
6614 DCHECK(kEmitCompilerReadBarrier);
6615
6616 // Insert a slow path based read barrier *after* the reference load.
6617 //
6618 // If heap poisoning is enabled, the unpoisoning of the loaded
6619 // reference will be carried out by the runtime within the slow
6620 // path.
6621 //
6622 // Note that `ref` currently does not get unpoisoned (when heap
6623 // poisoning is enabled), which is alright as the `ref` argument is
6624 // not used by the artReadBarrierSlow entry point.
6625 //
6626 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6627 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6628 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6629 AddSlowPath(slow_path);
6630
6631 __ B(slow_path->GetEntryLabel());
6632 __ Bind(slow_path->GetExitLabel());
6633}
6634
6635void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6636 Location out,
6637 Location ref,
6638 Location obj,
6639 uint32_t offset,
6640 Location index) {
6641 if (kEmitCompilerReadBarrier) {
6642 // Baker's read barriers shall be handled by the fast path
6643 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6644 DCHECK(!kUseBakerReadBarrier);
6645 // If heap poisoning is enabled, unpoisoning will be taken care of
6646 // by the runtime within the slow path.
6647 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6648 } else if (kPoisonHeapReferences) {
6649 __ UnpoisonHeapReference(out.AsRegister<Register>());
6650 }
6651}
6652
6653void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6654 Location out,
6655 Location root) {
6656 DCHECK(kEmitCompilerReadBarrier);
6657
6658 // Insert a slow path based read barrier *after* the GC root load.
6659 //
6660 // Note that GC roots are not affected by heap poisoning, so we do
6661 // not need to do anything special for this here.
6662 SlowPathCodeMIPS* slow_path =
6663 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6664 AddSlowPath(slow_path);
6665
6666 __ B(slow_path->GetEntryLabel());
6667 __ Bind(slow_path->GetExitLabel());
6668}
6669
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006670void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006671 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6672 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07006673 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006674 switch (type_check_kind) {
6675 case TypeCheckKind::kExactCheck:
6676 case TypeCheckKind::kAbstractClassCheck:
6677 case TypeCheckKind::kClassHierarchyCheck:
6678 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006679 call_kind =
6680 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006681 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006682 break;
6683 case TypeCheckKind::kArrayCheck:
6684 case TypeCheckKind::kUnresolvedCheck:
6685 case TypeCheckKind::kInterfaceCheck:
6686 call_kind = LocationSummary::kCallOnSlowPath;
6687 break;
6688 }
6689
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006691 if (baker_read_barrier_slow_path) {
6692 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6693 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006694 locations->SetInAt(0, Location::RequiresRegister());
6695 locations->SetInAt(1, Location::RequiresRegister());
6696 // The output does overlap inputs.
6697 // Note that TypeCheckSlowPathMIPS uses this register too.
6698 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006699 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006700}
6701
6702void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006703 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006704 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006705 Location obj_loc = locations->InAt(0);
6706 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006708 Location out_loc = locations->Out();
6709 Register out = out_loc.AsRegister<Register>();
6710 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6711 DCHECK_LE(num_temps, 1u);
6712 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006713 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6714 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6715 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6716 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006717 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006718 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006719
6720 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006721 // Avoid this check if we know `obj` is not null.
6722 if (instruction->MustDoNullCheck()) {
6723 __ Move(out, ZERO);
6724 __ Beqz(obj, &done);
6725 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006726
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006727 switch (type_check_kind) {
6728 case TypeCheckKind::kExactCheck: {
6729 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006730 GenerateReferenceLoadTwoRegisters(instruction,
6731 out_loc,
6732 obj_loc,
6733 class_offset,
6734 maybe_temp_loc,
6735 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006736 // Classes must be equal for the instanceof to succeed.
6737 __ Xor(out, out, cls);
6738 __ Sltiu(out, out, 1);
6739 break;
6740 }
6741
6742 case TypeCheckKind::kAbstractClassCheck: {
6743 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006744 GenerateReferenceLoadTwoRegisters(instruction,
6745 out_loc,
6746 obj_loc,
6747 class_offset,
6748 maybe_temp_loc,
6749 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006750 // If the class is abstract, we eagerly fetch the super class of the
6751 // object to avoid doing a comparison we know will fail.
6752 MipsLabel loop;
6753 __ Bind(&loop);
6754 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006755 GenerateReferenceLoadOneRegister(instruction,
6756 out_loc,
6757 super_offset,
6758 maybe_temp_loc,
6759 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006760 // If `out` is null, we use it for the result, and jump to `done`.
6761 __ Beqz(out, &done);
6762 __ Bne(out, cls, &loop);
6763 __ LoadConst32(out, 1);
6764 break;
6765 }
6766
6767 case TypeCheckKind::kClassHierarchyCheck: {
6768 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006769 GenerateReferenceLoadTwoRegisters(instruction,
6770 out_loc,
6771 obj_loc,
6772 class_offset,
6773 maybe_temp_loc,
6774 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006775 // Walk over the class hierarchy to find a match.
6776 MipsLabel loop, success;
6777 __ Bind(&loop);
6778 __ Beq(out, cls, &success);
6779 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006780 GenerateReferenceLoadOneRegister(instruction,
6781 out_loc,
6782 super_offset,
6783 maybe_temp_loc,
6784 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006785 __ Bnez(out, &loop);
6786 // If `out` is null, we use it for the result, and jump to `done`.
6787 __ B(&done);
6788 __ Bind(&success);
6789 __ LoadConst32(out, 1);
6790 break;
6791 }
6792
6793 case TypeCheckKind::kArrayObjectCheck: {
6794 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006795 GenerateReferenceLoadTwoRegisters(instruction,
6796 out_loc,
6797 obj_loc,
6798 class_offset,
6799 maybe_temp_loc,
6800 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006801 // Do an exact check.
6802 MipsLabel success;
6803 __ Beq(out, cls, &success);
6804 // Otherwise, we need to check that the object's class is a non-primitive array.
6805 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006806 GenerateReferenceLoadOneRegister(instruction,
6807 out_loc,
6808 component_offset,
6809 maybe_temp_loc,
6810 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006811 // If `out` is null, we use it for the result, and jump to `done`.
6812 __ Beqz(out, &done);
6813 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6814 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6815 __ Sltiu(out, out, 1);
6816 __ B(&done);
6817 __ Bind(&success);
6818 __ LoadConst32(out, 1);
6819 break;
6820 }
6821
6822 case TypeCheckKind::kArrayCheck: {
6823 // No read barrier since the slow path will retry upon failure.
6824 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006825 GenerateReferenceLoadTwoRegisters(instruction,
6826 out_loc,
6827 obj_loc,
6828 class_offset,
6829 maybe_temp_loc,
6830 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006831 DCHECK(locations->OnlyCallsOnSlowPath());
6832 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6833 /* is_fatal */ false);
6834 codegen_->AddSlowPath(slow_path);
6835 __ Bne(out, cls, slow_path->GetEntryLabel());
6836 __ LoadConst32(out, 1);
6837 break;
6838 }
6839
6840 case TypeCheckKind::kUnresolvedCheck:
6841 case TypeCheckKind::kInterfaceCheck: {
6842 // Note that we indeed only call on slow path, but we always go
6843 // into the slow path for the unresolved and interface check
6844 // cases.
6845 //
6846 // We cannot directly call the InstanceofNonTrivial runtime
6847 // entry point without resorting to a type checking slow path
6848 // here (i.e. by calling InvokeRuntime directly), as it would
6849 // require to assign fixed registers for the inputs of this
6850 // HInstanceOf instruction (following the runtime calling
6851 // convention), which might be cluttered by the potential first
6852 // read barrier emission at the beginning of this method.
6853 //
6854 // TODO: Introduce a new runtime entry point taking the object
6855 // to test (instead of its class) as argument, and let it deal
6856 // with the read barrier issues. This will let us refactor this
6857 // case of the `switch` code as it was previously (with a direct
6858 // call to the runtime not using a type checking slow path).
6859 // This should also be beneficial for the other cases above.
6860 DCHECK(locations->OnlyCallsOnSlowPath());
6861 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6862 /* is_fatal */ false);
6863 codegen_->AddSlowPath(slow_path);
6864 __ B(slow_path->GetEntryLabel());
6865 break;
6866 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006867 }
6868
6869 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006870
6871 if (slow_path != nullptr) {
6872 __ Bind(slow_path->GetExitLabel());
6873 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006874}
6875
6876void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6877 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6878 locations->SetOut(Location::ConstantLocation(constant));
6879}
6880
6881void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6882 // Will be generated at use site.
6883}
6884
6885void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6887 locations->SetOut(Location::ConstantLocation(constant));
6888}
6889
6890void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6891 // Will be generated at use site.
6892}
6893
6894void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6895 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6896 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6897}
6898
6899void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6900 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006901 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006902 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006903 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006904}
6905
6906void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6907 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6908 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006909 Location receiver = invoke->GetLocations()->InAt(0);
6910 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006911 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006912
6913 // Set the hidden argument.
6914 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6915 invoke->GetDexMethodIndex());
6916
6917 // temp = object->GetClass();
6918 if (receiver.IsStackSlot()) {
6919 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6920 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6921 } else {
6922 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6923 }
6924 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006925 // Instead of simply (possibly) unpoisoning `temp` here, we should
6926 // emit a read barrier for the previous class reference load.
6927 // However this is not required in practice, as this is an
6928 // intermediate/temporary reference and because the current
6929 // concurrent copying collector keeps the from-space memory
6930 // intact/accessible until the end of the marking phase (the
6931 // concurrent copying collector may not in the future).
6932 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006933 __ LoadFromOffset(kLoadWord, temp, temp,
6934 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6935 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006936 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006937 // temp = temp->GetImtEntryAt(method_offset);
6938 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6939 // T9 = temp->GetEntryPoint();
6940 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6941 // T9();
6942 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006943 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006944 DCHECK(!codegen_->IsLeafMethod());
6945 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6946}
6947
6948void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006949 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6950 if (intrinsic.TryDispatch(invoke)) {
6951 return;
6952 }
6953
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006954 HandleInvoke(invoke);
6955}
6956
6957void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006958 // Explicit clinit checks triggered by static invokes must have been pruned by
6959 // art::PrepareForRegisterAllocation.
6960 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006961
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006962 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01006963 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006964
Chris Larsen701566a2015-10-27 15:29:13 -07006965 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6966 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006967 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6968 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6969 }
Chris Larsen701566a2015-10-27 15:29:13 -07006970 return;
6971 }
6972
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006973 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006974
6975 // Add the extra input register if either the dex cache array base register
6976 // or the PC-relative base register for accessing literals is needed.
6977 if (has_extra_input) {
6978 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
6979 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006980}
6981
Orion Hodsonac141392017-01-13 11:53:47 +00006982void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6983 HandleInvoke(invoke);
6984}
6985
6986void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6987 codegen_->GenerateInvokePolymorphicCall(invoke);
6988}
6989
Chris Larsen701566a2015-10-27 15:29:13 -07006990static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006991 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07006992 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
6993 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006994 return true;
6995 }
6996 return false;
6997}
6998
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006999HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007000 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007001 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007002 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007003 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
7004 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007005 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007006 bool is_r6 = GetInstructionSetFeatures().IsR6();
7007 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007008 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007009 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007010 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007011 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007012 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007013 case HLoadString::LoadKind::kBootImageAddress:
7014 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007015 case HLoadString::LoadKind::kJitTableAddress:
7016 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007017 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007018 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007019 case HLoadString::LoadKind::kDexCacheViaMethod:
7020 fallback_load = false;
7021 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007022 }
7023 if (fallback_load) {
7024 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
7025 }
7026 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007027}
7028
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007029HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7030 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007031 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007032 // is incompatible with it.
7033 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007034 bool is_r6 = GetInstructionSetFeatures().IsR6();
7035 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007036 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007037 case HLoadClass::LoadKind::kInvalid:
7038 LOG(FATAL) << "UNREACHABLE";
7039 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007040 case HLoadClass::LoadKind::kReferrersClass:
7041 fallback_load = false;
7042 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007043 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007044 case HLoadClass::LoadKind::kBssEntry:
7045 DCHECK(!Runtime::Current()->UseJitCompilation());
7046 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007047 case HLoadClass::LoadKind::kBootImageAddress:
7048 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007049 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007050 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007051 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007052 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007053 case HLoadClass::LoadKind::kDexCacheViaMethod:
7054 fallback_load = false;
7055 break;
7056 }
7057 if (fallback_load) {
7058 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
7059 }
7060 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007061}
7062
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007063Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7064 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007065 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007066 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7067 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7068 if (!invoke->GetLocations()->Intrinsified()) {
7069 return location.AsRegister<Register>();
7070 }
7071 // For intrinsics we allow any location, so it may be on the stack.
7072 if (!location.IsRegister()) {
7073 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7074 return temp;
7075 }
7076 // For register locations, check if the register was saved. If so, get it from the stack.
7077 // Note: There is a chance that the register was saved but not overwritten, so we could
7078 // save one load. However, since this is just an intrinsic slow path we prefer this
7079 // simple and more robust approach rather that trying to determine if that's the case.
7080 SlowPathCode* slow_path = GetCurrentSlowPath();
7081 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7082 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7083 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7084 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7085 return temp;
7086 }
7087 return location.AsRegister<Register>();
7088}
7089
Vladimir Markodc151b22015-10-15 18:02:30 +01007090HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7091 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007092 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007093 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007094 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007095 // is incompatible with it.
7096 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007097 bool is_r6 = GetInstructionSetFeatures().IsR6();
7098 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007099 switch (dispatch_info.method_load_kind) {
Vladimir Marko65979462017-05-19 17:25:12 +01007100 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01007101 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007102 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01007103 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007104 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01007105 break;
7106 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007107 if (fallback_load) {
7108 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7109 dispatch_info.method_load_data = 0;
7110 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007111 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007112}
7113
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007114void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7115 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007116 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007117 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7118 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007119 bool is_r6 = GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01007120 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007121 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7122 : ZERO;
7123
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007124 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007125 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007126 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007127 uint32_t offset =
7128 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007129 __ LoadFromOffset(kLoadWord,
7130 temp.AsRegister<Register>(),
7131 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007132 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007133 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007134 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007135 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007136 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007137 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007138 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7139 DCHECK(GetCompilerOptions().IsBootImage());
7140 PcRelativePatchInfo* info = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7141 bool reordering = __ SetReorder(false);
7142 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunze6079dca2017-05-28 19:10:28 -07007143 EmitPcRelativeAddressPlaceholderHigh(info, TMP, base_reg);
7144 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678);
Vladimir Marko65979462017-05-19 17:25:12 +01007145 __ SetReorder(reordering);
7146 break;
7147 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007148 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7149 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7150 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007151 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
7152 if (is_r6) {
7153 uint32_t offset = invoke->GetDexCacheArrayOffset();
7154 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7155 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
7156 bool reordering = __ SetReorder(false);
7157 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
7158 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
7159 __ SetReorder(reordering);
7160 } else {
7161 HMipsDexCacheArraysBase* base =
7162 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
7163 int32_t offset =
7164 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
7165 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7166 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007167 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007168 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007169 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007170 Register reg = temp.AsRegister<Register>();
7171 Register method_reg;
7172 if (current_method.IsRegister()) {
7173 method_reg = current_method.AsRegister<Register>();
7174 } else {
7175 // TODO: use the appropriate DCHECK() here if possible.
7176 // DCHECK(invoke->GetLocations()->Intrinsified());
7177 DCHECK(!current_method.IsValid());
7178 method_reg = reg;
7179 __ Lw(reg, SP, kCurrentMethodStackOffset);
7180 }
7181
7182 // temp = temp->dex_cache_resolved_methods_;
7183 __ LoadFromOffset(kLoadWord,
7184 reg,
7185 method_reg,
7186 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007187 // temp = temp[index_in_cache];
7188 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7189 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007190 __ LoadFromOffset(kLoadWord,
7191 reg,
7192 reg,
7193 CodeGenerator::GetCachePointerOffset(index_in_cache));
7194 break;
7195 }
7196 }
7197
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007198 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007200 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007201 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007202 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7203 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007204 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007205 T9,
7206 callee_method.AsRegister<Register>(),
7207 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007208 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007209 // T9()
7210 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007211 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007212 break;
7213 }
7214 DCHECK(!IsLeafMethod());
7215}
7216
7217void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007218 // Explicit clinit checks triggered by static invokes must have been pruned by
7219 // art::PrepareForRegisterAllocation.
7220 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007221
7222 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7223 return;
7224 }
7225
7226 LocationSummary* locations = invoke->GetLocations();
7227 codegen_->GenerateStaticOrDirectCall(invoke,
7228 locations->HasTemps()
7229 ? locations->GetTemp(0)
7230 : Location::NoLocation());
7231 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7232}
7233
Chris Larsen3acee732015-11-18 13:31:08 -08007234void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007235 // Use the calling convention instead of the location of the receiver, as
7236 // intrinsics may have put the receiver in a different register. In the intrinsics
7237 // slow path, the arguments have been moved to the right place, so here we are
7238 // guaranteed that the receiver is the first register of the calling convention.
7239 InvokeDexCallingConvention calling_convention;
7240 Register receiver = calling_convention.GetRegisterAt(0);
7241
Chris Larsen3acee732015-11-18 13:31:08 -08007242 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007243 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7244 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7245 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007246 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007247
7248 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007249 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007250 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007251 // Instead of simply (possibly) unpoisoning `temp` here, we should
7252 // emit a read barrier for the previous class reference load.
7253 // However this is not required in practice, as this is an
7254 // intermediate/temporary reference and because the current
7255 // concurrent copying collector keeps the from-space memory
7256 // intact/accessible until the end of the marking phase (the
7257 // concurrent copying collector may not in the future).
7258 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007259 // temp = temp->GetMethodAt(method_offset);
7260 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7261 // T9 = temp->GetEntryPoint();
7262 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7263 // T9();
7264 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007265 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08007266}
7267
7268void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7269 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7270 return;
7271 }
7272
7273 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007274 DCHECK(!codegen_->IsLeafMethod());
7275 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7276}
7277
7278void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007279 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7280 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007281 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007282 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7283 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007284 return;
7285 }
Vladimir Marko41559982017-01-06 14:04:23 +00007286 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007287 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007288 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7289 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007290 ? LocationSummary::kCallOnSlowPath
7291 : LocationSummary::kNoCall;
7292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007293 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7294 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7295 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007296 switch (load_kind) {
7297 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007298 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007299 case HLoadClass::LoadKind::kBootImageAddress:
7300 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007301 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007302 break;
7303 }
7304 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007305 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007306 locations->SetInAt(0, Location::RequiresRegister());
7307 break;
7308 default:
7309 break;
7310 }
7311 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007312 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7313 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7314 // Rely on the type resolution or initialization and marking to save everything we need.
7315 // Request a temp to hold the BSS entry location for the slow path on R2
7316 // (no benefit for R6).
7317 if (!isR6) {
7318 locations->AddTemp(Location::RequiresRegister());
7319 }
7320 RegisterSet caller_saves = RegisterSet::Empty();
7321 InvokeRuntimeCallingConvention calling_convention;
7322 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7323 locations->SetCustomSlowPathCallerSaves(caller_saves);
7324 } else {
7325 // For non-Baker read barriers we have a temp-clobbering call.
7326 }
7327 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007328}
7329
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007330// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7331// move.
7332void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007333 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7334 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7335 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007336 return;
7337 }
Vladimir Marko41559982017-01-06 14:04:23 +00007338 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007339
Vladimir Marko41559982017-01-06 14:04:23 +00007340 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007341 Location out_loc = locations->Out();
7342 Register out = out_loc.AsRegister<Register>();
7343 Register base_or_current_method_reg;
7344 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7345 switch (load_kind) {
7346 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007347 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007348 case HLoadClass::LoadKind::kBootImageAddress:
7349 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007350 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7351 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007352 case HLoadClass::LoadKind::kReferrersClass:
7353 case HLoadClass::LoadKind::kDexCacheViaMethod:
7354 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7355 break;
7356 default:
7357 base_or_current_method_reg = ZERO;
7358 break;
7359 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007360
Alexey Frunze15958152017-02-09 19:08:30 -08007361 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7362 ? kWithoutReadBarrier
7363 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007364 bool generate_null_check = false;
7365 switch (load_kind) {
7366 case HLoadClass::LoadKind::kReferrersClass: {
7367 DCHECK(!cls->CanCallRuntime());
7368 DCHECK(!cls->MustGenerateClinitCheck());
7369 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7370 GenerateGcRootFieldLoad(cls,
7371 out_loc,
7372 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007373 ArtMethod::DeclaringClassOffset().Int32Value(),
7374 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007375 break;
7376 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007377 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007378 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007379 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007380 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7381 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007382 bool reordering = __ SetReorder(false);
7383 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7384 __ Addiu(out, out, /* placeholder */ 0x5678);
7385 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007386 break;
7387 }
7388 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007389 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007390 uint32_t address = dchecked_integral_cast<uint32_t>(
7391 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7392 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007393 __ LoadLiteral(out,
7394 base_or_current_method_reg,
7395 codegen_->DeduplicateBootImageAddressLiteral(address));
7396 break;
7397 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007398 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007399 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007400 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007401 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7402 if (isR6 || non_baker_read_barrier) {
7403 bool reordering = __ SetReorder(false);
7404 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7405 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
7406 __ SetReorder(reordering);
7407 } else {
7408 // On R2 save the BSS entry address in a temporary register instead of
7409 // recalculating it in the slow path.
7410 Register temp = locations->GetTemp(0).AsRegister<Register>();
7411 bool reordering = __ SetReorder(false);
7412 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7413 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7414 __ SetReorder(reordering);
7415 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
7416 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007417 generate_null_check = true;
7418 break;
7419 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007420 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007421 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7422 cls->GetTypeIndex(),
7423 cls->GetClass());
7424 bool reordering = __ SetReorder(false);
7425 __ Bind(&info->high_label);
7426 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007427 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007428 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007429 break;
7430 }
Vladimir Marko41559982017-01-06 14:04:23 +00007431 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007432 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007433 LOG(FATAL) << "UNREACHABLE";
7434 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007435 }
7436
7437 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7438 DCHECK(cls->CanCallRuntime());
7439 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7440 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7441 codegen_->AddSlowPath(slow_path);
7442 if (generate_null_check) {
7443 __ Beqz(out, slow_path->GetEntryLabel());
7444 }
7445 if (cls->MustGenerateClinitCheck()) {
7446 GenerateClassInitializationCheck(slow_path, out);
7447 } else {
7448 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007449 }
7450 }
7451}
7452
7453static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007454 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007455}
7456
7457void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7458 LocationSummary* locations =
7459 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7460 locations->SetOut(Location::RequiresRegister());
7461}
7462
7463void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7464 Register out = load->GetLocations()->Out().AsRegister<Register>();
7465 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7466}
7467
7468void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7469 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7470}
7471
7472void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7473 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7474}
7475
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007476void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007477 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007479 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007480 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007481 switch (load_kind) {
7482 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007483 case HLoadString::LoadKind::kBootImageAddress:
7484 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007485 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007486 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007487 break;
7488 }
7489 FALLTHROUGH_INTENDED;
7490 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007491 case HLoadString::LoadKind::kDexCacheViaMethod:
7492 locations->SetInAt(0, Location::RequiresRegister());
7493 break;
7494 default:
7495 break;
7496 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007497 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
7498 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007499 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007500 } else {
7501 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007502 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7503 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7504 // Rely on the pResolveString and marking to save everything we need.
7505 // Request a temp to hold the BSS entry location for the slow path on R2
7506 // (no benefit for R6).
7507 if (!isR6) {
7508 locations->AddTemp(Location::RequiresRegister());
7509 }
7510 RegisterSet caller_saves = RegisterSet::Empty();
7511 InvokeRuntimeCallingConvention calling_convention;
7512 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7513 locations->SetCustomSlowPathCallerSaves(caller_saves);
7514 } else {
7515 // For non-Baker read barriers we have a temp-clobbering call.
7516 }
7517 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007518 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007519}
7520
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007521// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7522// move.
7523void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007524 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007525 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007526 Location out_loc = locations->Out();
7527 Register out = out_loc.AsRegister<Register>();
7528 Register base_or_current_method_reg;
7529 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7530 switch (load_kind) {
7531 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007532 case HLoadString::LoadKind::kBootImageAddress:
7533 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007534 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007535 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7536 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007537 default:
7538 base_or_current_method_reg = ZERO;
7539 break;
7540 }
7541
7542 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007543 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007544 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007545 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007546 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007547 bool reordering = __ SetReorder(false);
7548 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7549 __ Addiu(out, out, /* placeholder */ 0x5678);
7550 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007551 return; // No dex cache slow path.
7552 }
7553 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007554 uint32_t address = dchecked_integral_cast<uint32_t>(
7555 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7556 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007557 __ LoadLiteral(out,
7558 base_or_current_method_reg,
7559 codegen_->DeduplicateBootImageAddressLiteral(address));
7560 return; // No dex cache slow path.
7561 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007562 case HLoadString::LoadKind::kBssEntry: {
7563 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7564 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007565 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007566 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7567 if (isR6 || non_baker_read_barrier) {
7568 bool reordering = __ SetReorder(false);
7569 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7570 GenerateGcRootFieldLoad(load,
7571 out_loc,
7572 out,
7573 /* placeholder */ 0x5678,
7574 kCompilerReadBarrierOption);
7575 __ SetReorder(reordering);
7576 } else {
7577 // On R2 save the BSS entry address in a temporary register instead of
7578 // recalculating it in the slow path.
7579 Register temp = locations->GetTemp(0).AsRegister<Register>();
7580 bool reordering = __ SetReorder(false);
7581 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7582 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7583 __ SetReorder(reordering);
7584 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7585 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007586 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7587 codegen_->AddSlowPath(slow_path);
7588 __ Beqz(out, slow_path->GetEntryLabel());
7589 __ Bind(slow_path->GetExitLabel());
7590 return;
7591 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007592 case HLoadString::LoadKind::kJitTableAddress: {
7593 CodeGeneratorMIPS::JitPatchInfo* info =
7594 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7595 load->GetStringIndex(),
7596 load->GetString());
7597 bool reordering = __ SetReorder(false);
7598 __ Bind(&info->high_label);
7599 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007600 GenerateGcRootFieldLoad(load,
7601 out_loc,
7602 out,
7603 /* placeholder */ 0x5678,
7604 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007605 __ SetReorder(reordering);
7606 return;
7607 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007608 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007609 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007610 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007611
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007612 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007613 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7614 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007615 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007616 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007617 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7618 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007619}
7620
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007621void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7623 locations->SetOut(Location::ConstantLocation(constant));
7624}
7625
7626void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7627 // Will be generated at use site.
7628}
7629
7630void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7631 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007632 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007633 InvokeRuntimeCallingConvention calling_convention;
7634 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7635}
7636
7637void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7638 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007639 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007640 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7641 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007642 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007643 }
7644 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7645}
7646
7647void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7648 LocationSummary* locations =
7649 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7650 switch (mul->GetResultType()) {
7651 case Primitive::kPrimInt:
7652 case Primitive::kPrimLong:
7653 locations->SetInAt(0, Location::RequiresRegister());
7654 locations->SetInAt(1, Location::RequiresRegister());
7655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7656 break;
7657
7658 case Primitive::kPrimFloat:
7659 case Primitive::kPrimDouble:
7660 locations->SetInAt(0, Location::RequiresFpuRegister());
7661 locations->SetInAt(1, Location::RequiresFpuRegister());
7662 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7663 break;
7664
7665 default:
7666 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7667 }
7668}
7669
7670void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7671 Primitive::Type type = instruction->GetType();
7672 LocationSummary* locations = instruction->GetLocations();
7673 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7674
7675 switch (type) {
7676 case Primitive::kPrimInt: {
7677 Register dst = locations->Out().AsRegister<Register>();
7678 Register lhs = locations->InAt(0).AsRegister<Register>();
7679 Register rhs = locations->InAt(1).AsRegister<Register>();
7680
7681 if (isR6) {
7682 __ MulR6(dst, lhs, rhs);
7683 } else {
7684 __ MulR2(dst, lhs, rhs);
7685 }
7686 break;
7687 }
7688 case Primitive::kPrimLong: {
7689 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7690 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7691 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7692 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7693 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7694 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7695
7696 // Extra checks to protect caused by the existance of A1_A2.
7697 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7698 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7699 DCHECK_NE(dst_high, lhs_low);
7700 DCHECK_NE(dst_high, rhs_low);
7701
7702 // A_B * C_D
7703 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7704 // dst_lo: [ low(B*D) ]
7705 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7706
7707 if (isR6) {
7708 __ MulR6(TMP, lhs_high, rhs_low);
7709 __ MulR6(dst_high, lhs_low, rhs_high);
7710 __ Addu(dst_high, dst_high, TMP);
7711 __ MuhuR6(TMP, lhs_low, rhs_low);
7712 __ Addu(dst_high, dst_high, TMP);
7713 __ MulR6(dst_low, lhs_low, rhs_low);
7714 } else {
7715 __ MulR2(TMP, lhs_high, rhs_low);
7716 __ MulR2(dst_high, lhs_low, rhs_high);
7717 __ Addu(dst_high, dst_high, TMP);
7718 __ MultuR2(lhs_low, rhs_low);
7719 __ Mfhi(TMP);
7720 __ Addu(dst_high, dst_high, TMP);
7721 __ Mflo(dst_low);
7722 }
7723 break;
7724 }
7725 case Primitive::kPrimFloat:
7726 case Primitive::kPrimDouble: {
7727 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7728 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7729 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7730 if (type == Primitive::kPrimFloat) {
7731 __ MulS(dst, lhs, rhs);
7732 } else {
7733 __ MulD(dst, lhs, rhs);
7734 }
7735 break;
7736 }
7737 default:
7738 LOG(FATAL) << "Unexpected mul type " << type;
7739 }
7740}
7741
7742void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7743 LocationSummary* locations =
7744 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7745 switch (neg->GetResultType()) {
7746 case Primitive::kPrimInt:
7747 case Primitive::kPrimLong:
7748 locations->SetInAt(0, Location::RequiresRegister());
7749 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7750 break;
7751
7752 case Primitive::kPrimFloat:
7753 case Primitive::kPrimDouble:
7754 locations->SetInAt(0, Location::RequiresFpuRegister());
7755 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7756 break;
7757
7758 default:
7759 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7760 }
7761}
7762
7763void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7764 Primitive::Type type = instruction->GetType();
7765 LocationSummary* locations = instruction->GetLocations();
7766
7767 switch (type) {
7768 case Primitive::kPrimInt: {
7769 Register dst = locations->Out().AsRegister<Register>();
7770 Register src = locations->InAt(0).AsRegister<Register>();
7771 __ Subu(dst, ZERO, src);
7772 break;
7773 }
7774 case Primitive::kPrimLong: {
7775 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7776 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7777 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7778 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7779 __ Subu(dst_low, ZERO, src_low);
7780 __ Sltu(TMP, ZERO, dst_low);
7781 __ Subu(dst_high, ZERO, src_high);
7782 __ Subu(dst_high, dst_high, TMP);
7783 break;
7784 }
7785 case Primitive::kPrimFloat:
7786 case Primitive::kPrimDouble: {
7787 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7788 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7789 if (type == Primitive::kPrimFloat) {
7790 __ NegS(dst, src);
7791 } else {
7792 __ NegD(dst, src);
7793 }
7794 break;
7795 }
7796 default:
7797 LOG(FATAL) << "Unexpected neg type " << type;
7798 }
7799}
7800
7801void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7802 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007804 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007805 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007806 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7807 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007808}
7809
7810void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007811 // Note: if heap poisoning is enabled, the entry point takes care
7812 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007813 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7814 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007815}
7816
7817void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7818 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007820 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007821 if (instruction->IsStringAlloc()) {
7822 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7823 } else {
7824 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007825 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007826 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7827}
7828
7829void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007830 // Note: if heap poisoning is enabled, the entry point takes care
7831 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007832 if (instruction->IsStringAlloc()) {
7833 // String is allocated through StringFactory. Call NewEmptyString entry point.
7834 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007835 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007836 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7837 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7838 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007839 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007840 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7841 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007842 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007843 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007844 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007845}
7846
7847void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7849 locations->SetInAt(0, Location::RequiresRegister());
7850 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7851}
7852
7853void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7854 Primitive::Type type = instruction->GetType();
7855 LocationSummary* locations = instruction->GetLocations();
7856
7857 switch (type) {
7858 case Primitive::kPrimInt: {
7859 Register dst = locations->Out().AsRegister<Register>();
7860 Register src = locations->InAt(0).AsRegister<Register>();
7861 __ Nor(dst, src, ZERO);
7862 break;
7863 }
7864
7865 case Primitive::kPrimLong: {
7866 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7867 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7868 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7869 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7870 __ Nor(dst_high, src_high, ZERO);
7871 __ Nor(dst_low, src_low, ZERO);
7872 break;
7873 }
7874
7875 default:
7876 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7877 }
7878}
7879
7880void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7881 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7882 locations->SetInAt(0, Location::RequiresRegister());
7883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7884}
7885
7886void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7887 LocationSummary* locations = instruction->GetLocations();
7888 __ Xori(locations->Out().AsRegister<Register>(),
7889 locations->InAt(0).AsRegister<Register>(),
7890 1);
7891}
7892
7893void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007894 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7895 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007896}
7897
Calin Juravle2ae48182016-03-16 14:05:09 +00007898void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7899 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007900 return;
7901 }
7902 Location obj = instruction->GetLocations()->InAt(0);
7903
7904 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007905 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007906}
7907
Calin Juravle2ae48182016-03-16 14:05:09 +00007908void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007909 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007910 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007911
7912 Location obj = instruction->GetLocations()->InAt(0);
7913
7914 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7915}
7916
7917void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007918 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007919}
7920
7921void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7922 HandleBinaryOp(instruction);
7923}
7924
7925void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7926 HandleBinaryOp(instruction);
7927}
7928
7929void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7930 LOG(FATAL) << "Unreachable";
7931}
7932
7933void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7934 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7935}
7936
7937void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7939 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7940 if (location.IsStackSlot()) {
7941 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7942 } else if (location.IsDoubleStackSlot()) {
7943 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7944 }
7945 locations->SetOut(location);
7946}
7947
7948void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7949 ATTRIBUTE_UNUSED) {
7950 // Nothing to do, the parameter is already at its location.
7951}
7952
7953void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
7954 LocationSummary* locations =
7955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7956 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
7957}
7958
7959void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
7960 ATTRIBUTE_UNUSED) {
7961 // Nothing to do, the method is already at its location.
7962}
7963
7964void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
7965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01007966 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007967 locations->SetInAt(i, Location::Any());
7968 }
7969 locations->SetOut(Location::Any());
7970}
7971
7972void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
7973 LOG(FATAL) << "Unreachable";
7974}
7975
7976void LocationsBuilderMIPS::VisitRem(HRem* rem) {
7977 Primitive::Type type = rem->GetResultType();
7978 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007979 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
7981
7982 switch (type) {
7983 case Primitive::kPrimInt:
7984 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08007985 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7987 break;
7988
7989 case Primitive::kPrimLong: {
7990 InvokeRuntimeCallingConvention calling_convention;
7991 locations->SetInAt(0, Location::RegisterPairLocation(
7992 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7993 locations->SetInAt(1, Location::RegisterPairLocation(
7994 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
7995 locations->SetOut(calling_convention.GetReturnLocation(type));
7996 break;
7997 }
7998
7999 case Primitive::kPrimFloat:
8000 case Primitive::kPrimDouble: {
8001 InvokeRuntimeCallingConvention calling_convention;
8002 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8003 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8004 locations->SetOut(calling_convention.GetReturnLocation(type));
8005 break;
8006 }
8007
8008 default:
8009 LOG(FATAL) << "Unexpected rem type " << type;
8010 }
8011}
8012
8013void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
8014 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008015
8016 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08008017 case Primitive::kPrimInt:
8018 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008019 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008020 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008021 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008022 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8023 break;
8024 }
8025 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008026 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008027 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008028 break;
8029 }
8030 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008031 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008032 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008033 break;
8034 }
8035 default:
8036 LOG(FATAL) << "Unexpected rem type " << type;
8037 }
8038}
8039
Igor Murashkind01745e2017-04-05 16:40:31 -07008040void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8041 constructor_fence->SetLocations(nullptr);
8042}
8043
8044void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8045 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8046 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8047}
8048
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008049void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8050 memory_barrier->SetLocations(nullptr);
8051}
8052
8053void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8054 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8055}
8056
8057void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8059 Primitive::Type return_type = ret->InputAt(0)->GetType();
8060 locations->SetInAt(0, MipsReturnLocation(return_type));
8061}
8062
8063void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8064 codegen_->GenerateFrameExit();
8065}
8066
8067void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8068 ret->SetLocations(nullptr);
8069}
8070
8071void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8072 codegen_->GenerateFrameExit();
8073}
8074
Alexey Frunze92d90602015-12-18 18:16:36 -08008075void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8076 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008077}
8078
Alexey Frunze92d90602015-12-18 18:16:36 -08008079void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8080 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008081}
8082
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008083void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8084 HandleShift(shl);
8085}
8086
8087void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8088 HandleShift(shl);
8089}
8090
8091void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8092 HandleShift(shr);
8093}
8094
8095void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8096 HandleShift(shr);
8097}
8098
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008099void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8100 HandleBinaryOp(instruction);
8101}
8102
8103void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8104 HandleBinaryOp(instruction);
8105}
8106
8107void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8108 HandleFieldGet(instruction, instruction->GetFieldInfo());
8109}
8110
8111void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8112 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8113}
8114
8115void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8116 HandleFieldSet(instruction, instruction->GetFieldInfo());
8117}
8118
8119void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008120 HandleFieldSet(instruction,
8121 instruction->GetFieldInfo(),
8122 instruction->GetDexPc(),
8123 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008124}
8125
8126void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8127 HUnresolvedInstanceFieldGet* instruction) {
8128 FieldAccessCallingConventionMIPS calling_convention;
8129 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8130 instruction->GetFieldType(),
8131 calling_convention);
8132}
8133
8134void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8135 HUnresolvedInstanceFieldGet* instruction) {
8136 FieldAccessCallingConventionMIPS calling_convention;
8137 codegen_->GenerateUnresolvedFieldAccess(instruction,
8138 instruction->GetFieldType(),
8139 instruction->GetFieldIndex(),
8140 instruction->GetDexPc(),
8141 calling_convention);
8142}
8143
8144void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8145 HUnresolvedInstanceFieldSet* instruction) {
8146 FieldAccessCallingConventionMIPS calling_convention;
8147 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8148 instruction->GetFieldType(),
8149 calling_convention);
8150}
8151
8152void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8153 HUnresolvedInstanceFieldSet* instruction) {
8154 FieldAccessCallingConventionMIPS calling_convention;
8155 codegen_->GenerateUnresolvedFieldAccess(instruction,
8156 instruction->GetFieldType(),
8157 instruction->GetFieldIndex(),
8158 instruction->GetDexPc(),
8159 calling_convention);
8160}
8161
8162void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8163 HUnresolvedStaticFieldGet* instruction) {
8164 FieldAccessCallingConventionMIPS calling_convention;
8165 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8166 instruction->GetFieldType(),
8167 calling_convention);
8168}
8169
8170void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8171 HUnresolvedStaticFieldGet* instruction) {
8172 FieldAccessCallingConventionMIPS calling_convention;
8173 codegen_->GenerateUnresolvedFieldAccess(instruction,
8174 instruction->GetFieldType(),
8175 instruction->GetFieldIndex(),
8176 instruction->GetDexPc(),
8177 calling_convention);
8178}
8179
8180void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8181 HUnresolvedStaticFieldSet* instruction) {
8182 FieldAccessCallingConventionMIPS calling_convention;
8183 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8184 instruction->GetFieldType(),
8185 calling_convention);
8186}
8187
8188void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8189 HUnresolvedStaticFieldSet* instruction) {
8190 FieldAccessCallingConventionMIPS calling_convention;
8191 codegen_->GenerateUnresolvedFieldAccess(instruction,
8192 instruction->GetFieldType(),
8193 instruction->GetFieldIndex(),
8194 instruction->GetDexPc(),
8195 calling_convention);
8196}
8197
8198void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008199 LocationSummary* locations =
8200 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01008201 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008202}
8203
8204void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8205 HBasicBlock* block = instruction->GetBlock();
8206 if (block->GetLoopInformation() != nullptr) {
8207 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8208 // The back edge will generate the suspend check.
8209 return;
8210 }
8211 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8212 // The goto will generate the suspend check.
8213 return;
8214 }
8215 GenerateSuspendCheck(instruction, nullptr);
8216}
8217
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008218void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8219 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008220 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008221 InvokeRuntimeCallingConvention calling_convention;
8222 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8223}
8224
8225void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008226 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008227 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8228}
8229
8230void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8231 Primitive::Type input_type = conversion->GetInputType();
8232 Primitive::Type result_type = conversion->GetResultType();
8233 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008234 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008235
8236 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8237 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8238 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8239 }
8240
8241 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008242 if (!isR6 &&
8243 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8244 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008245 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008246 }
8247
8248 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8249
8250 if (call_kind == LocationSummary::kNoCall) {
8251 if (Primitive::IsFloatingPointType(input_type)) {
8252 locations->SetInAt(0, Location::RequiresFpuRegister());
8253 } else {
8254 locations->SetInAt(0, Location::RequiresRegister());
8255 }
8256
8257 if (Primitive::IsFloatingPointType(result_type)) {
8258 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8259 } else {
8260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8261 }
8262 } else {
8263 InvokeRuntimeCallingConvention calling_convention;
8264
8265 if (Primitive::IsFloatingPointType(input_type)) {
8266 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8267 } else {
8268 DCHECK_EQ(input_type, Primitive::kPrimLong);
8269 locations->SetInAt(0, Location::RegisterPairLocation(
8270 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8271 }
8272
8273 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8274 }
8275}
8276
8277void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8278 LocationSummary* locations = conversion->GetLocations();
8279 Primitive::Type result_type = conversion->GetResultType();
8280 Primitive::Type input_type = conversion->GetInputType();
8281 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008282 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008283
8284 DCHECK_NE(input_type, result_type);
8285
8286 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8287 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8288 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8289 Register src = locations->InAt(0).AsRegister<Register>();
8290
Alexey Frunzea871ef12016-06-27 15:20:11 -07008291 if (dst_low != src) {
8292 __ Move(dst_low, src);
8293 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008294 __ Sra(dst_high, src, 31);
8295 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8296 Register dst = locations->Out().AsRegister<Register>();
8297 Register src = (input_type == Primitive::kPrimLong)
8298 ? locations->InAt(0).AsRegisterPairLow<Register>()
8299 : locations->InAt(0).AsRegister<Register>();
8300
8301 switch (result_type) {
8302 case Primitive::kPrimChar:
8303 __ Andi(dst, src, 0xFFFF);
8304 break;
8305 case Primitive::kPrimByte:
8306 if (has_sign_extension) {
8307 __ Seb(dst, src);
8308 } else {
8309 __ Sll(dst, src, 24);
8310 __ Sra(dst, dst, 24);
8311 }
8312 break;
8313 case Primitive::kPrimShort:
8314 if (has_sign_extension) {
8315 __ Seh(dst, src);
8316 } else {
8317 __ Sll(dst, src, 16);
8318 __ Sra(dst, dst, 16);
8319 }
8320 break;
8321 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008322 if (dst != src) {
8323 __ Move(dst, src);
8324 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008325 break;
8326
8327 default:
8328 LOG(FATAL) << "Unexpected type conversion from " << input_type
8329 << " to " << result_type;
8330 }
8331 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008332 if (input_type == Primitive::kPrimLong) {
8333 if (isR6) {
8334 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8335 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8336 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8337 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8338 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8339 __ Mtc1(src_low, FTMP);
8340 __ Mthc1(src_high, FTMP);
8341 if (result_type == Primitive::kPrimFloat) {
8342 __ Cvtsl(dst, FTMP);
8343 } else {
8344 __ Cvtdl(dst, FTMP);
8345 }
8346 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008347 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8348 : kQuickL2d;
8349 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008350 if (result_type == Primitive::kPrimFloat) {
8351 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8352 } else {
8353 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8354 }
8355 }
8356 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008357 Register src = locations->InAt(0).AsRegister<Register>();
8358 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8359 __ Mtc1(src, FTMP);
8360 if (result_type == Primitive::kPrimFloat) {
8361 __ Cvtsw(dst, FTMP);
8362 } else {
8363 __ Cvtdw(dst, FTMP);
8364 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008365 }
8366 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8367 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008368
8369 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8370 // value of the output type if the input is outside of the range after the truncation or
8371 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8372 // results. This matches the desired float/double-to-int/long conversion exactly.
8373 //
8374 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8375 // value when the input is either a NaN or is outside of the range of the output type
8376 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8377 // the same result.
8378 //
8379 // The code takes care of the different behaviors by first comparing the input to the
8380 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8381 // If the input is greater than or equal to the minimum, it procedes to the truncate
8382 // instruction, which will handle such an input the same way irrespective of NAN2008.
8383 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8384 // in order to return either zero or the minimum value.
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008385 if (result_type == Primitive::kPrimLong) {
8386 if (isR6) {
8387 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8388 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8389 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8390 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8391 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008392
8393 if (input_type == Primitive::kPrimFloat) {
8394 __ TruncLS(FTMP, src);
8395 } else {
8396 __ TruncLD(FTMP, src);
8397 }
8398 __ Mfc1(dst_low, FTMP);
8399 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008400 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008401 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8402 : kQuickD2l;
8403 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008404 if (input_type == Primitive::kPrimFloat) {
8405 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8406 } else {
8407 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8408 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008409 }
8410 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008411 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8412 Register dst = locations->Out().AsRegister<Register>();
8413 MipsLabel truncate;
8414 MipsLabel done;
8415
Lena Djokicf4e23a82017-05-09 15:43:45 +02008416 if (!isR6) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008417 if (input_type == Primitive::kPrimFloat) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008418 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8419 __ LoadConst32(TMP, min_val);
8420 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008421 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008422 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8423 __ LoadConst32(TMP, High32Bits(min_val));
8424 __ Mtc1(ZERO, FTMP);
8425 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008426 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008427
8428 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008429 __ ColeS(0, FTMP, src);
8430 } else {
8431 __ ColeD(0, FTMP, src);
8432 }
8433 __ Bc1t(0, &truncate);
8434
8435 if (input_type == Primitive::kPrimFloat) {
8436 __ CeqS(0, src, src);
8437 } else {
8438 __ CeqD(0, src, src);
8439 }
8440 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8441 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008442
8443 __ B(&done);
8444
8445 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008446 }
8447
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008448 if (input_type == Primitive::kPrimFloat) {
8449 __ TruncWS(FTMP, src);
8450 } else {
8451 __ TruncWD(FTMP, src);
8452 }
8453 __ Mfc1(dst, FTMP);
8454
Lena Djokicf4e23a82017-05-09 15:43:45 +02008455 if (!isR6) {
8456 __ Bind(&done);
8457 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008458 }
8459 } else if (Primitive::IsFloatingPointType(result_type) &&
8460 Primitive::IsFloatingPointType(input_type)) {
8461 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8462 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8463 if (result_type == Primitive::kPrimFloat) {
8464 __ Cvtsd(dst, src);
8465 } else {
8466 __ Cvtds(dst, src);
8467 }
8468 } else {
8469 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8470 << " to " << result_type;
8471 }
8472}
8473
8474void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8475 HandleShift(ushr);
8476}
8477
8478void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8479 HandleShift(ushr);
8480}
8481
8482void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8483 HandleBinaryOp(instruction);
8484}
8485
8486void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8487 HandleBinaryOp(instruction);
8488}
8489
8490void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8491 // Nothing to do, this should be removed during prepare for register allocator.
8492 LOG(FATAL) << "Unreachable";
8493}
8494
8495void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8496 // Nothing to do, this should be removed during prepare for register allocator.
8497 LOG(FATAL) << "Unreachable";
8498}
8499
8500void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008501 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008502}
8503
8504void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008505 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008506}
8507
8508void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008509 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008510}
8511
8512void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008513 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008514}
8515
8516void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008517 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008518}
8519
8520void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008521 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008522}
8523
8524void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008525 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008526}
8527
8528void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008529 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008530}
8531
8532void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008533 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008534}
8535
8536void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008537 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538}
8539
8540void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008541 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008542}
8543
8544void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008545 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008546}
8547
8548void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008549 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008550}
8551
8552void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008553 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008554}
8555
8556void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008557 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008558}
8559
8560void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008561 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008562}
8563
8564void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008565 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008566}
8567
8568void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008569 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008570}
8571
8572void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008573 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008574}
8575
8576void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008577 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008578}
8579
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008580void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8581 LocationSummary* locations =
8582 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8583 locations->SetInAt(0, Location::RequiresRegister());
8584}
8585
Alexey Frunze96b66822016-09-10 02:32:44 -07008586void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8587 int32_t lower_bound,
8588 uint32_t num_entries,
8589 HBasicBlock* switch_block,
8590 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008591 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008592 Register temp_reg = TMP;
8593 __ Addiu32(temp_reg, value_reg, -lower_bound);
8594 // Jump to default if index is negative
8595 // Note: We don't check the case that index is positive while value < lower_bound, because in
8596 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8597 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8598
Alexey Frunze96b66822016-09-10 02:32:44 -07008599 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008600 // Jump to successors[0] if value == lower_bound.
8601 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8602 int32_t last_index = 0;
8603 for (; num_entries - last_index > 2; last_index += 2) {
8604 __ Addiu(temp_reg, temp_reg, -2);
8605 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8606 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8607 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8608 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8609 }
8610 if (num_entries - last_index == 2) {
8611 // The last missing case_value.
8612 __ Addiu(temp_reg, temp_reg, -1);
8613 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008614 }
8615
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008616 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008617 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008618 __ B(codegen_->GetLabelOf(default_block));
8619 }
8620}
8621
Alexey Frunze96b66822016-09-10 02:32:44 -07008622void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8623 Register constant_area,
8624 int32_t lower_bound,
8625 uint32_t num_entries,
8626 HBasicBlock* switch_block,
8627 HBasicBlock* default_block) {
8628 // Create a jump table.
8629 std::vector<MipsLabel*> labels(num_entries);
8630 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8631 for (uint32_t i = 0; i < num_entries; i++) {
8632 labels[i] = codegen_->GetLabelOf(successors[i]);
8633 }
8634 JumpTable* table = __ CreateJumpTable(std::move(labels));
8635
8636 // Is the value in range?
8637 __ Addiu32(TMP, value_reg, -lower_bound);
8638 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8639 __ Sltiu(AT, TMP, num_entries);
8640 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8641 } else {
8642 __ LoadConst32(AT, num_entries);
8643 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8644 }
8645
8646 // We are in the range of the table.
8647 // Load the target address from the jump table, indexing by the value.
8648 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008649 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008650 __ Lw(TMP, TMP, 0);
8651 // Compute the absolute target address by adding the table start address
8652 // (the table contains offsets to targets relative to its start).
8653 __ Addu(TMP, TMP, AT);
8654 // And jump.
8655 __ Jr(TMP);
8656 __ NopIfNoReordering();
8657}
8658
8659void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8660 int32_t lower_bound = switch_instr->GetStartValue();
8661 uint32_t num_entries = switch_instr->GetNumEntries();
8662 LocationSummary* locations = switch_instr->GetLocations();
8663 Register value_reg = locations->InAt(0).AsRegister<Register>();
8664 HBasicBlock* switch_block = switch_instr->GetBlock();
8665 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8666
8667 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8668 num_entries > kPackedSwitchJumpTableThreshold) {
8669 // R6 uses PC-relative addressing to access the jump table.
8670 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8671 // the jump table and it is implemented by changing HPackedSwitch to
8672 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8673 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8674 GenTableBasedPackedSwitch(value_reg,
8675 ZERO,
8676 lower_bound,
8677 num_entries,
8678 switch_block,
8679 default_block);
8680 } else {
8681 GenPackedSwitchWithCompares(value_reg,
8682 lower_bound,
8683 num_entries,
8684 switch_block,
8685 default_block);
8686 }
8687}
8688
8689void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8690 LocationSummary* locations =
8691 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8692 locations->SetInAt(0, Location::RequiresRegister());
8693 // Constant area pointer (HMipsComputeBaseMethodAddress).
8694 locations->SetInAt(1, Location::RequiresRegister());
8695}
8696
8697void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8698 int32_t lower_bound = switch_instr->GetStartValue();
8699 uint32_t num_entries = switch_instr->GetNumEntries();
8700 LocationSummary* locations = switch_instr->GetLocations();
8701 Register value_reg = locations->InAt(0).AsRegister<Register>();
8702 Register constant_area = locations->InAt(1).AsRegister<Register>();
8703 HBasicBlock* switch_block = switch_instr->GetBlock();
8704 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8705
8706 // This is an R2-only path. HPackedSwitch has been changed to
8707 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8708 // required to address the jump table relative to PC.
8709 GenTableBasedPackedSwitch(value_reg,
8710 constant_area,
8711 lower_bound,
8712 num_entries,
8713 switch_block,
8714 default_block);
8715}
8716
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008717void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8718 HMipsComputeBaseMethodAddress* insn) {
8719 LocationSummary* locations =
8720 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8721 locations->SetOut(Location::RequiresRegister());
8722}
8723
8724void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8725 HMipsComputeBaseMethodAddress* insn) {
8726 LocationSummary* locations = insn->GetLocations();
8727 Register reg = locations->Out().AsRegister<Register>();
8728
8729 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8730
8731 // Generate a dummy PC-relative call to obtain PC.
8732 __ Nal();
8733 // Grab the return address off RA.
8734 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008735 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008736
8737 // Remember this offset (the obtained PC value) for later use with constant area.
8738 __ BindPcRelBaseLabel();
8739}
8740
8741void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8742 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8743 locations->SetOut(Location::RequiresRegister());
8744}
8745
8746void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8747 Register reg = base->GetLocations()->Out().AsRegister<Register>();
8748 CodeGeneratorMIPS::PcRelativePatchInfo* info =
8749 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008750 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8751 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008752 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008753 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
8754 __ Addiu(reg, reg, /* placeholder */ 0x5678);
8755 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008756}
8757
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008758void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8759 // The trampoline uses the same calling convention as dex calling conventions,
8760 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8761 // the method_idx.
8762 HandleInvoke(invoke);
8763}
8764
8765void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8766 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8767}
8768
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008769void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8770 LocationSummary* locations =
8771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8772 locations->SetInAt(0, Location::RequiresRegister());
8773 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008774}
8775
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008776void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8777 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008778 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008779 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008780 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008781 __ LoadFromOffset(kLoadWord,
8782 locations->Out().AsRegister<Register>(),
8783 locations->InAt(0).AsRegister<Register>(),
8784 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008785 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008786 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008787 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008788 __ LoadFromOffset(kLoadWord,
8789 locations->Out().AsRegister<Register>(),
8790 locations->InAt(0).AsRegister<Register>(),
8791 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008792 __ LoadFromOffset(kLoadWord,
8793 locations->Out().AsRegister<Register>(),
8794 locations->Out().AsRegister<Register>(),
8795 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008796 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008797}
8798
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008799#undef __
8800#undef QUICK_ENTRY_POINT
8801
8802} // namespace mips
8803} // namespace art