blob: e9870acff428e41d33e862c72596c6f617c8ad1a [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)),
1064 boot_image_string_patches_(StringReferenceValueComparator(),
1065 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1066 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1067 boot_image_type_patches_(TypeReferenceValueComparator(),
1068 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1069 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001070 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001071 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1072 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001073 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001074 // Save RA (containing the return address) to mimic Quick.
1075 AddAllocatedRegister(Location::RegisterLocation(RA));
1076}
1077
1078#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001079// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1080#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001081#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001082
1083void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1084 // Ensure that we fix up branches.
1085 __ FinalizeCode();
1086
1087 // Adjust native pc offsets in stack maps.
1088 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001089 uint32_t old_position =
1090 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001091 uint32_t new_position = __ GetAdjustedPosition(old_position);
1092 DCHECK_GE(new_position, old_position);
1093 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1094 }
1095
1096 // Adjust pc offsets for the disassembly information.
1097 if (disasm_info_ != nullptr) {
1098 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1099 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1100 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1101 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1102 it.second.start = __ GetAdjustedPosition(it.second.start);
1103 it.second.end = __ GetAdjustedPosition(it.second.end);
1104 }
1105 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1106 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1107 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1108 }
1109 }
1110
1111 CodeGenerator::Finalize(allocator);
1112}
1113
1114MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1115 return codegen_->GetAssembler();
1116}
1117
1118void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1119 DCHECK_LT(index, moves_.size());
1120 MoveOperands* move = moves_[index];
1121 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1122}
1123
1124void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1125 DCHECK_LT(index, moves_.size());
1126 MoveOperands* move = moves_[index];
1127 Primitive::Type type = move->GetType();
1128 Location loc1 = move->GetDestination();
1129 Location loc2 = move->GetSource();
1130
1131 DCHECK(!loc1.IsConstant());
1132 DCHECK(!loc2.IsConstant());
1133
1134 if (loc1.Equals(loc2)) {
1135 return;
1136 }
1137
1138 if (loc1.IsRegister() && loc2.IsRegister()) {
1139 // Swap 2 GPRs.
1140 Register r1 = loc1.AsRegister<Register>();
1141 Register r2 = loc2.AsRegister<Register>();
1142 __ Move(TMP, r2);
1143 __ Move(r2, r1);
1144 __ Move(r1, TMP);
1145 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1146 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1147 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1148 if (type == Primitive::kPrimFloat) {
1149 __ MovS(FTMP, f2);
1150 __ MovS(f2, f1);
1151 __ MovS(f1, FTMP);
1152 } else {
1153 DCHECK_EQ(type, Primitive::kPrimDouble);
1154 __ MovD(FTMP, f2);
1155 __ MovD(f2, f1);
1156 __ MovD(f1, FTMP);
1157 }
1158 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1159 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1160 // Swap FPR and GPR.
1161 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1162 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1163 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001164 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001165 __ Move(TMP, r2);
1166 __ Mfc1(r2, f1);
1167 __ Mtc1(TMP, f1);
1168 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1169 // Swap 2 GPR register pairs.
1170 Register r1 = loc1.AsRegisterPairLow<Register>();
1171 Register r2 = loc2.AsRegisterPairLow<Register>();
1172 __ Move(TMP, r2);
1173 __ Move(r2, r1);
1174 __ Move(r1, TMP);
1175 r1 = loc1.AsRegisterPairHigh<Register>();
1176 r2 = loc2.AsRegisterPairHigh<Register>();
1177 __ Move(TMP, r2);
1178 __ Move(r2, r1);
1179 __ Move(r1, TMP);
1180 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1181 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1182 // Swap FPR and GPR register pair.
1183 DCHECK_EQ(type, Primitive::kPrimDouble);
1184 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1185 : loc2.AsFpuRegister<FRegister>();
1186 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1187 : loc2.AsRegisterPairLow<Register>();
1188 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1189 : loc2.AsRegisterPairHigh<Register>();
1190 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1191 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1192 // unpredictable and the following mfch1 will fail.
1193 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001194 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001195 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001196 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001197 __ Move(r2_l, TMP);
1198 __ Move(r2_h, AT);
1199 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1200 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1201 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1202 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001203 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1204 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001205 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1206 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001207 __ Move(TMP, reg);
1208 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1209 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1210 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1211 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1212 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1213 : loc2.AsRegisterPairLow<Register>();
1214 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1215 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001216 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001217 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1218 : loc2.GetHighStackIndex(kMipsWordSize);
1219 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001220 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001221 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001222 __ Move(TMP, reg_h);
1223 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1224 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001225 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1226 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1227 : loc2.AsFpuRegister<FRegister>();
1228 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1229 if (type == Primitive::kPrimFloat) {
1230 __ MovS(FTMP, reg);
1231 __ LoadSFromOffset(reg, SP, offset);
1232 __ StoreSToOffset(FTMP, SP, offset);
1233 } else {
1234 DCHECK_EQ(type, Primitive::kPrimDouble);
1235 __ MovD(FTMP, reg);
1236 __ LoadDFromOffset(reg, SP, offset);
1237 __ StoreDToOffset(FTMP, SP, offset);
1238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239 } else {
1240 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1241 }
1242}
1243
1244void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1245 __ Pop(static_cast<Register>(reg));
1246}
1247
1248void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1249 __ Push(static_cast<Register>(reg));
1250}
1251
1252void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1253 // Allocate a scratch register other than TMP, if available.
1254 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1255 // automatically unspilled when the scratch scope object is destroyed).
1256 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1257 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1258 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1259 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1260 __ LoadFromOffset(kLoadWord,
1261 Register(ensure_scratch.GetRegister()),
1262 SP,
1263 index1 + stack_offset);
1264 __ LoadFromOffset(kLoadWord,
1265 TMP,
1266 SP,
1267 index2 + stack_offset);
1268 __ StoreToOffset(kStoreWord,
1269 Register(ensure_scratch.GetRegister()),
1270 SP,
1271 index2 + stack_offset);
1272 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1273 }
1274}
1275
Alexey Frunze73296a72016-06-03 22:51:46 -07001276void CodeGeneratorMIPS::ComputeSpillMask() {
1277 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1278 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1279 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1280 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1281 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1282 // within the stack frame.
1283 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1284 core_spill_mask_ |= (1 << ZERO);
1285 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001286}
1287
1288bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001289 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001290 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1291 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1292 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001293 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001294}
1295
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001296static dwarf::Reg DWARFReg(Register reg) {
1297 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1298}
1299
1300// TODO: mapping of floating-point registers to DWARF.
1301
1302void CodeGeneratorMIPS::GenerateFrameEntry() {
1303 __ Bind(&frame_entry_label_);
1304
1305 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1306
1307 if (do_overflow_check) {
1308 __ LoadFromOffset(kLoadWord,
1309 ZERO,
1310 SP,
1311 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1312 RecordPcInfo(nullptr, 0);
1313 }
1314
1315 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001316 CHECK_EQ(fpu_spill_mask_, 0u);
1317 CHECK_EQ(core_spill_mask_, 1u << RA);
1318 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001319 return;
1320 }
1321
1322 // Make sure the frame size isn't unreasonably large.
1323 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1324 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1325 }
1326
1327 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001330 __ IncreaseFrameSize(ofs);
1331
Alexey Frunze73296a72016-06-03 22:51:46 -07001332 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1333 Register reg = static_cast<Register>(MostSignificantBit(mask));
1334 mask ^= 1u << reg;
1335 ofs -= kMipsWordSize;
1336 // The ZERO register is only included for alignment.
1337 if (reg != ZERO) {
1338 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339 __ cfi().RelOffset(DWARFReg(reg), ofs);
1340 }
1341 }
1342
Alexey Frunze73296a72016-06-03 22:51:46 -07001343 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1344 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1345 mask ^= 1u << reg;
1346 ofs -= kMipsDoublewordSize;
1347 __ StoreDToOffset(reg, SP, ofs);
1348 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001349 }
1350
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001351 // Save the current method if we need it. Note that we do not
1352 // do this in HCurrentMethod, as the instruction might have been removed
1353 // in the SSA graph.
1354 if (RequiresCurrentMethod()) {
1355 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1356 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001357
1358 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1359 // Initialize should deoptimize flag to 0.
1360 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1361 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362}
1363
1364void CodeGeneratorMIPS::GenerateFrameExit() {
1365 __ cfi().RememberState();
1366
1367 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001369
Alexey Frunze73296a72016-06-03 22:51:46 -07001370 // For better instruction scheduling restore RA before other registers.
1371 uint32_t ofs = GetFrameSize();
1372 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1373 Register reg = static_cast<Register>(MostSignificantBit(mask));
1374 mask ^= 1u << reg;
1375 ofs -= kMipsWordSize;
1376 // The ZERO register is only included for alignment.
1377 if (reg != ZERO) {
1378 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379 __ cfi().Restore(DWARFReg(reg));
1380 }
1381 }
1382
Alexey Frunze73296a72016-06-03 22:51:46 -07001383 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1384 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1385 mask ^= 1u << reg;
1386 ofs -= kMipsDoublewordSize;
1387 __ LoadDFromOffset(reg, SP, ofs);
1388 // TODO: __ cfi().Restore(DWARFReg(reg));
1389 }
1390
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001391 size_t frame_size = GetFrameSize();
1392 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1393 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1394 bool reordering = __ SetReorder(false);
1395 if (exchange) {
1396 __ Jr(RA);
1397 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1398 } else {
1399 __ DecreaseFrameSize(frame_size);
1400 __ Jr(RA);
1401 __ Nop(); // In delay slot.
1402 }
1403 __ SetReorder(reordering);
1404 } else {
1405 __ Jr(RA);
1406 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001407 }
1408
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001409 __ cfi().RestoreState();
1410 __ cfi().DefCFAOffset(GetFrameSize());
1411}
1412
1413void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1414 __ Bind(GetLabelOf(block));
1415}
1416
1417void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1418 if (src.Equals(dst)) {
1419 return;
1420 }
1421
1422 if (src.IsConstant()) {
1423 MoveConstant(dst, src.GetConstant());
1424 } else {
1425 if (Primitive::Is64BitType(dst_type)) {
1426 Move64(dst, src);
1427 } else {
1428 Move32(dst, src);
1429 }
1430 }
1431}
1432
1433void CodeGeneratorMIPS::Move32(Location destination, Location source) {
1434 if (source.Equals(destination)) {
1435 return;
1436 }
1437
1438 if (destination.IsRegister()) {
1439 if (source.IsRegister()) {
1440 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1441 } else if (source.IsFpuRegister()) {
1442 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1443 } else {
1444 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1445 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
1446 }
1447 } else if (destination.IsFpuRegister()) {
1448 if (source.IsRegister()) {
1449 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1450 } else if (source.IsFpuRegister()) {
1451 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1452 } else {
1453 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1454 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1455 }
1456 } else {
1457 DCHECK(destination.IsStackSlot()) << destination;
1458 if (source.IsRegister()) {
1459 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
1460 } else if (source.IsFpuRegister()) {
1461 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1462 } else {
1463 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1464 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1465 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1466 }
1467 }
1468}
1469
1470void CodeGeneratorMIPS::Move64(Location destination, Location source) {
1471 if (source.Equals(destination)) {
1472 return;
1473 }
1474
1475 if (destination.IsRegisterPair()) {
1476 if (source.IsRegisterPair()) {
1477 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1478 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1479 } else if (source.IsFpuRegister()) {
1480 Register dst_high = destination.AsRegisterPairHigh<Register>();
1481 Register dst_low = destination.AsRegisterPairLow<Register>();
1482 FRegister src = source.AsFpuRegister<FRegister>();
1483 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001484 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001485 } else {
1486 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1487 int32_t off = source.GetStackIndex();
1488 Register r = destination.AsRegisterPairLow<Register>();
1489 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1490 }
1491 } else if (destination.IsFpuRegister()) {
1492 if (source.IsRegisterPair()) {
1493 FRegister dst = destination.AsFpuRegister<FRegister>();
1494 Register src_high = source.AsRegisterPairHigh<Register>();
1495 Register src_low = source.AsRegisterPairLow<Register>();
1496 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001497 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001498 } else if (source.IsFpuRegister()) {
1499 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1500 } else {
1501 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1502 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1503 }
1504 } else {
1505 DCHECK(destination.IsDoubleStackSlot()) << destination;
1506 int32_t off = destination.GetStackIndex();
1507 if (source.IsRegisterPair()) {
1508 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
1509 } else if (source.IsFpuRegister()) {
1510 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
1511 } else {
1512 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1513 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1514 __ StoreToOffset(kStoreWord, TMP, SP, off);
1515 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1516 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
1517 }
1518 }
1519}
1520
1521void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1522 if (c->IsIntConstant() || c->IsNullConstant()) {
1523 // Move 32 bit constant.
1524 int32_t value = GetInt32ValueOf(c);
1525 if (destination.IsRegister()) {
1526 Register dst = destination.AsRegister<Register>();
1527 __ LoadConst32(dst, value);
1528 } else {
1529 DCHECK(destination.IsStackSlot())
1530 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001531 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001532 }
1533 } else if (c->IsLongConstant()) {
1534 // Move 64 bit constant.
1535 int64_t value = GetInt64ValueOf(c);
1536 if (destination.IsRegisterPair()) {
1537 Register r_h = destination.AsRegisterPairHigh<Register>();
1538 Register r_l = destination.AsRegisterPairLow<Register>();
1539 __ LoadConst64(r_h, r_l, value);
1540 } else {
1541 DCHECK(destination.IsDoubleStackSlot())
1542 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001543 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001544 }
1545 } else if (c->IsFloatConstant()) {
1546 // Move 32 bit float constant.
1547 int32_t value = GetInt32ValueOf(c);
1548 if (destination.IsFpuRegister()) {
1549 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1550 } else {
1551 DCHECK(destination.IsStackSlot())
1552 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001553 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001554 }
1555 } else {
1556 // Move 64 bit double constant.
1557 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1558 int64_t value = GetInt64ValueOf(c);
1559 if (destination.IsFpuRegister()) {
1560 FRegister fd = destination.AsFpuRegister<FRegister>();
1561 __ LoadDConst64(fd, value, TMP);
1562 } else {
1563 DCHECK(destination.IsDoubleStackSlot())
1564 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001565 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001566 }
1567 }
1568}
1569
1570void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1571 DCHECK(destination.IsRegister());
1572 Register dst = destination.AsRegister<Register>();
1573 __ LoadConst32(dst, value);
1574}
1575
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1577 if (location.IsRegister()) {
1578 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001579 } else if (location.IsRegisterPair()) {
1580 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1581 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001582 } else {
1583 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1584 }
1585}
1586
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1588inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1589 const ArenaDeque<PcRelativePatchInfo>& infos,
1590 ArenaVector<LinkerPatch>* linker_patches) {
1591 for (const PcRelativePatchInfo& info : infos) {
1592 const DexFile& dex_file = info.target_dex_file;
1593 size_t offset_or_index = info.offset_or_index;
1594 DCHECK(info.high_label.IsBound());
1595 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1596 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1597 // the assembler's base label used for PC-relative addressing.
1598 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1599 ? __ GetLabelLocation(&info.pc_rel_label)
1600 : __ GetPcRelBaseLabelLocation();
1601 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1602 }
1603}
1604
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001605void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1606 DCHECK(linker_patches->empty());
1607 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001608 pc_relative_dex_cache_patches_.size() +
1609 pc_relative_string_patches_.size() +
1610 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001611 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001612 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001613 boot_image_type_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001614 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001615 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1616 linker_patches);
1617 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001618 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001619 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1620 linker_patches);
1621 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001622 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1623 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001624 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1625 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001626 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001627 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1628 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001629 for (const auto& entry : boot_image_string_patches_) {
1630 const StringReference& target_string = entry.first;
1631 Literal* literal = entry.second;
1632 DCHECK(literal->GetLabel()->IsBound());
1633 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1634 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1635 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001636 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001637 }
1638 for (const auto& entry : boot_image_type_patches_) {
1639 const TypeReference& target_type = entry.first;
1640 Literal* literal = entry.second;
1641 DCHECK(literal->GetLabel()->IsBound());
1642 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1643 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1644 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001645 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001646 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001647 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001648}
1649
1650CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001651 const DexFile& dex_file, dex::StringIndex string_index) {
1652 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001653}
1654
1655CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001656 const DexFile& dex_file, dex::TypeIndex type_index) {
1657 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001658}
1659
Vladimir Marko1998cd02017-01-13 13:02:58 +00001660CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1661 const DexFile& dex_file, dex::TypeIndex type_index) {
1662 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1663}
1664
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001665CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1666 const DexFile& dex_file, uint32_t element_offset) {
1667 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1668}
1669
1670CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1671 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1672 patches->emplace_back(dex_file, offset_or_index);
1673 return &patches->back();
1674}
1675
Alexey Frunze06a46c42016-07-19 15:00:40 -07001676Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1677 return map->GetOrCreate(
1678 value,
1679 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1680}
1681
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001682Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1683 MethodToLiteralMap* map) {
1684 return map->GetOrCreate(
1685 target_method,
1686 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1687}
1688
Alexey Frunze06a46c42016-07-19 15:00:40 -07001689Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001690 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001691 return boot_image_string_patches_.GetOrCreate(
1692 StringReference(&dex_file, string_index),
1693 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1694}
1695
1696Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001697 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001698 return boot_image_type_patches_.GetOrCreate(
1699 TypeReference(&dex_file, type_index),
1700 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1701}
1702
1703Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001704 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001705}
1706
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001707void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1708 Register out,
1709 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001710 if (GetInstructionSetFeatures().IsR6()) {
1711 DCHECK_EQ(base, ZERO);
1712 __ Bind(&info->high_label);
1713 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001714 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001715 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001716 } else {
1717 // If base is ZERO, emit NAL to obtain the actual base.
1718 if (base == ZERO) {
1719 // Generate a dummy PC-relative call to obtain PC.
1720 __ Nal();
1721 }
1722 __ Bind(&info->high_label);
1723 __ Lui(out, /* placeholder */ 0x1234);
1724 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1725 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1726 if (base == ZERO) {
1727 __ Bind(&info->pc_rel_label);
1728 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001729 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001730 __ Addu(out, out, (base == ZERO) ? RA : base);
1731 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001732 // The immediately following instruction will add the sign-extended low half of the 32-bit
1733 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001734}
1735
Alexey Frunze627c1a02017-01-30 19:28:14 -08001736CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1737 const DexFile& dex_file,
1738 dex::StringIndex dex_index,
1739 Handle<mirror::String> handle) {
1740 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1741 reinterpret_cast64<uint64_t>(handle.GetReference()));
1742 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1743 return &jit_string_patches_.back();
1744}
1745
1746CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1747 const DexFile& dex_file,
1748 dex::TypeIndex dex_index,
1749 Handle<mirror::Class> handle) {
1750 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1751 reinterpret_cast64<uint64_t>(handle.GetReference()));
1752 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1753 return &jit_class_patches_.back();
1754}
1755
1756void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1757 const uint8_t* roots_data,
1758 const CodeGeneratorMIPS::JitPatchInfo& info,
1759 uint64_t index_in_table) const {
1760 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1761 uintptr_t address =
1762 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1763 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1764 // lui reg, addr32_high
1765 DCHECK_EQ(code[literal_offset + 0], 0x34);
1766 DCHECK_EQ(code[literal_offset + 1], 0x12);
1767 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1768 DCHECK_EQ(code[literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001769 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001770 DCHECK_EQ(code[literal_offset + 4], 0x78);
1771 DCHECK_EQ(code[literal_offset + 5], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001772 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001773 // lui reg, addr32_high
1774 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1775 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001776 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001777 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1778 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1779}
1780
1781void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1782 for (const JitPatchInfo& info : jit_string_patches_) {
1783 const auto& it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1784 dex::StringIndex(info.index)));
1785 DCHECK(it != jit_string_roots_.end());
1786 PatchJitRootUse(code, roots_data, info, it->second);
1787 }
1788 for (const JitPatchInfo& info : jit_class_patches_) {
1789 const auto& it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1790 dex::TypeIndex(info.index)));
1791 DCHECK(it != jit_class_roots_.end());
1792 PatchJitRootUse(code, roots_data, info, it->second);
1793 }
1794}
1795
Goran Jakovljevice114da22016-12-26 14:21:43 +01001796void CodeGeneratorMIPS::MarkGCCard(Register object,
1797 Register value,
1798 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001799 MipsLabel done;
1800 Register card = AT;
1801 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001802 if (value_can_be_null) {
1803 __ Beqz(value, &done);
1804 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001805 __ LoadFromOffset(kLoadWord,
1806 card,
1807 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001808 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001809 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1810 __ Addu(temp, card, temp);
1811 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001812 if (value_can_be_null) {
1813 __ Bind(&done);
1814 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001815}
1816
David Brazdil58282f42016-01-14 12:45:10 +00001817void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001818 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1819 blocked_core_registers_[ZERO] = true;
1820 blocked_core_registers_[K0] = true;
1821 blocked_core_registers_[K1] = true;
1822 blocked_core_registers_[GP] = true;
1823 blocked_core_registers_[SP] = true;
1824 blocked_core_registers_[RA] = true;
1825
1826 // AT and TMP(T8) are used as temporary/scratch registers
1827 // (similar to how AT is used by MIPS assemblers).
1828 blocked_core_registers_[AT] = true;
1829 blocked_core_registers_[TMP] = true;
1830 blocked_fpu_registers_[FTMP] = true;
1831
1832 // Reserve suspend and thread registers.
1833 blocked_core_registers_[S0] = true;
1834 blocked_core_registers_[TR] = true;
1835
1836 // Reserve T9 for function calls
1837 blocked_core_registers_[T9] = true;
1838
1839 // Reserve odd-numbered FPU registers.
1840 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1841 blocked_fpu_registers_[i] = true;
1842 }
1843
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001844 if (GetGraph()->IsDebuggable()) {
1845 // Stubs do not save callee-save floating point registers. If the graph
1846 // is debuggable, we need to deal with these registers differently. For
1847 // now, just block them.
1848 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1849 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1850 }
1851 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852}
1853
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1855 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1856 return kMipsWordSize;
1857}
1858
1859size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1860 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1861 return kMipsWordSize;
1862}
1863
1864size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1865 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1866 return kMipsDoublewordSize;
1867}
1868
1869size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1870 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1871 return kMipsDoublewordSize;
1872}
1873
1874void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001875 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876}
1877
1878void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001879 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880}
1881
Serban Constantinescufca16662016-07-14 09:21:59 +01001882constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1883
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1885 HInstruction* instruction,
1886 uint32_t dex_pc,
1887 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001888 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001889 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1890 IsDirectEntrypoint(entrypoint));
1891 if (EntrypointRequiresStackMap(entrypoint)) {
1892 RecordPcInfo(instruction, dex_pc, slow_path);
1893 }
1894}
1895
1896void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1897 HInstruction* instruction,
1898 SlowPathCode* slow_path,
1899 bool direct) {
1900 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1901 GenerateInvokeRuntime(entry_point_offset, direct);
1902}
1903
1904void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001905 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001906 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001907 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001908 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 // Reserve argument space on stack (for $a0-$a3) for
1910 // entrypoints that directly reference native implementations.
1911 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001912 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001913 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001914 } else {
1915 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001917 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001918}
1919
1920void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1921 Register class_reg) {
1922 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1923 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1924 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1925 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1926 __ Sync(0);
1927 __ Bind(slow_path->GetExitLabel());
1928}
1929
1930void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1931 __ Sync(0); // Only stype 0 is supported.
1932}
1933
1934void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1935 HBasicBlock* successor) {
1936 SuspendCheckSlowPathMIPS* slow_path =
1937 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1938 codegen_->AddSlowPath(slow_path);
1939
1940 __ LoadFromOffset(kLoadUnsignedHalfword,
1941 TMP,
1942 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001943 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001944 if (successor == nullptr) {
1945 __ Bnez(TMP, slow_path->GetEntryLabel());
1946 __ Bind(slow_path->GetReturnLabel());
1947 } else {
1948 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1949 __ B(slow_path->GetEntryLabel());
1950 // slow_path will return to GetLabelOf(successor).
1951 }
1952}
1953
1954InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1955 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001956 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001957 assembler_(codegen->GetAssembler()),
1958 codegen_(codegen) {}
1959
1960void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1961 DCHECK_EQ(instruction->InputCount(), 2U);
1962 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1963 Primitive::Type type = instruction->GetResultType();
1964 switch (type) {
1965 case Primitive::kPrimInt: {
1966 locations->SetInAt(0, Location::RequiresRegister());
1967 HInstruction* right = instruction->InputAt(1);
1968 bool can_use_imm = false;
1969 if (right->IsConstant()) {
1970 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1971 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1972 can_use_imm = IsUint<16>(imm);
1973 } else if (instruction->IsAdd()) {
1974 can_use_imm = IsInt<16>(imm);
1975 } else {
1976 DCHECK(instruction->IsSub());
1977 can_use_imm = IsInt<16>(-imm);
1978 }
1979 }
1980 if (can_use_imm)
1981 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1982 else
1983 locations->SetInAt(1, Location::RequiresRegister());
1984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1985 break;
1986 }
1987
1988 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001989 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001990 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1991 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001992 break;
1993 }
1994
1995 case Primitive::kPrimFloat:
1996 case Primitive::kPrimDouble:
1997 DCHECK(instruction->IsAdd() || instruction->IsSub());
1998 locations->SetInAt(0, Location::RequiresFpuRegister());
1999 locations->SetInAt(1, Location::RequiresFpuRegister());
2000 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2001 break;
2002
2003 default:
2004 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2005 }
2006}
2007
2008void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2009 Primitive::Type type = instruction->GetType();
2010 LocationSummary* locations = instruction->GetLocations();
2011
2012 switch (type) {
2013 case Primitive::kPrimInt: {
2014 Register dst = locations->Out().AsRegister<Register>();
2015 Register lhs = locations->InAt(0).AsRegister<Register>();
2016 Location rhs_location = locations->InAt(1);
2017
2018 Register rhs_reg = ZERO;
2019 int32_t rhs_imm = 0;
2020 bool use_imm = rhs_location.IsConstant();
2021 if (use_imm) {
2022 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2023 } else {
2024 rhs_reg = rhs_location.AsRegister<Register>();
2025 }
2026
2027 if (instruction->IsAnd()) {
2028 if (use_imm)
2029 __ Andi(dst, lhs, rhs_imm);
2030 else
2031 __ And(dst, lhs, rhs_reg);
2032 } else if (instruction->IsOr()) {
2033 if (use_imm)
2034 __ Ori(dst, lhs, rhs_imm);
2035 else
2036 __ Or(dst, lhs, rhs_reg);
2037 } else if (instruction->IsXor()) {
2038 if (use_imm)
2039 __ Xori(dst, lhs, rhs_imm);
2040 else
2041 __ Xor(dst, lhs, rhs_reg);
2042 } else if (instruction->IsAdd()) {
2043 if (use_imm)
2044 __ Addiu(dst, lhs, rhs_imm);
2045 else
2046 __ Addu(dst, lhs, rhs_reg);
2047 } else {
2048 DCHECK(instruction->IsSub());
2049 if (use_imm)
2050 __ Addiu(dst, lhs, -rhs_imm);
2051 else
2052 __ Subu(dst, lhs, rhs_reg);
2053 }
2054 break;
2055 }
2056
2057 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002058 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2059 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2060 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2061 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002062 Location rhs_location = locations->InAt(1);
2063 bool use_imm = rhs_location.IsConstant();
2064 if (!use_imm) {
2065 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2066 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2067 if (instruction->IsAnd()) {
2068 __ And(dst_low, lhs_low, rhs_low);
2069 __ And(dst_high, lhs_high, rhs_high);
2070 } else if (instruction->IsOr()) {
2071 __ Or(dst_low, lhs_low, rhs_low);
2072 __ Or(dst_high, lhs_high, rhs_high);
2073 } else if (instruction->IsXor()) {
2074 __ Xor(dst_low, lhs_low, rhs_low);
2075 __ Xor(dst_high, lhs_high, rhs_high);
2076 } else if (instruction->IsAdd()) {
2077 if (lhs_low == rhs_low) {
2078 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2079 __ Slt(TMP, lhs_low, ZERO);
2080 __ Addu(dst_low, lhs_low, rhs_low);
2081 } else {
2082 __ Addu(dst_low, lhs_low, rhs_low);
2083 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2084 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2085 }
2086 __ Addu(dst_high, lhs_high, rhs_high);
2087 __ Addu(dst_high, dst_high, TMP);
2088 } else {
2089 DCHECK(instruction->IsSub());
2090 __ Sltu(TMP, lhs_low, rhs_low);
2091 __ Subu(dst_low, lhs_low, rhs_low);
2092 __ Subu(dst_high, lhs_high, rhs_high);
2093 __ Subu(dst_high, dst_high, TMP);
2094 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002095 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002096 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2097 if (instruction->IsOr()) {
2098 uint32_t low = Low32Bits(value);
2099 uint32_t high = High32Bits(value);
2100 if (IsUint<16>(low)) {
2101 if (dst_low != lhs_low || low != 0) {
2102 __ Ori(dst_low, lhs_low, low);
2103 }
2104 } else {
2105 __ LoadConst32(TMP, low);
2106 __ Or(dst_low, lhs_low, TMP);
2107 }
2108 if (IsUint<16>(high)) {
2109 if (dst_high != lhs_high || high != 0) {
2110 __ Ori(dst_high, lhs_high, high);
2111 }
2112 } else {
2113 if (high != low) {
2114 __ LoadConst32(TMP, high);
2115 }
2116 __ Or(dst_high, lhs_high, TMP);
2117 }
2118 } else if (instruction->IsXor()) {
2119 uint32_t low = Low32Bits(value);
2120 uint32_t high = High32Bits(value);
2121 if (IsUint<16>(low)) {
2122 if (dst_low != lhs_low || low != 0) {
2123 __ Xori(dst_low, lhs_low, low);
2124 }
2125 } else {
2126 __ LoadConst32(TMP, low);
2127 __ Xor(dst_low, lhs_low, TMP);
2128 }
2129 if (IsUint<16>(high)) {
2130 if (dst_high != lhs_high || high != 0) {
2131 __ Xori(dst_high, lhs_high, high);
2132 }
2133 } else {
2134 if (high != low) {
2135 __ LoadConst32(TMP, high);
2136 }
2137 __ Xor(dst_high, lhs_high, TMP);
2138 }
2139 } else if (instruction->IsAnd()) {
2140 uint32_t low = Low32Bits(value);
2141 uint32_t high = High32Bits(value);
2142 if (IsUint<16>(low)) {
2143 __ Andi(dst_low, lhs_low, low);
2144 } else if (low != 0xFFFFFFFF) {
2145 __ LoadConst32(TMP, low);
2146 __ And(dst_low, lhs_low, TMP);
2147 } else if (dst_low != lhs_low) {
2148 __ Move(dst_low, lhs_low);
2149 }
2150 if (IsUint<16>(high)) {
2151 __ Andi(dst_high, lhs_high, high);
2152 } else if (high != 0xFFFFFFFF) {
2153 if (high != low) {
2154 __ LoadConst32(TMP, high);
2155 }
2156 __ And(dst_high, lhs_high, TMP);
2157 } else if (dst_high != lhs_high) {
2158 __ Move(dst_high, lhs_high);
2159 }
2160 } else {
2161 if (instruction->IsSub()) {
2162 value = -value;
2163 } else {
2164 DCHECK(instruction->IsAdd());
2165 }
2166 int32_t low = Low32Bits(value);
2167 int32_t high = High32Bits(value);
2168 if (IsInt<16>(low)) {
2169 if (dst_low != lhs_low || low != 0) {
2170 __ Addiu(dst_low, lhs_low, low);
2171 }
2172 if (low != 0) {
2173 __ Sltiu(AT, dst_low, low);
2174 }
2175 } else {
2176 __ LoadConst32(TMP, low);
2177 __ Addu(dst_low, lhs_low, TMP);
2178 __ Sltu(AT, dst_low, TMP);
2179 }
2180 if (IsInt<16>(high)) {
2181 if (dst_high != lhs_high || high != 0) {
2182 __ Addiu(dst_high, lhs_high, high);
2183 }
2184 } else {
2185 if (high != low) {
2186 __ LoadConst32(TMP, high);
2187 }
2188 __ Addu(dst_high, lhs_high, TMP);
2189 }
2190 if (low != 0) {
2191 __ Addu(dst_high, dst_high, AT);
2192 }
2193 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002194 }
2195 break;
2196 }
2197
2198 case Primitive::kPrimFloat:
2199 case Primitive::kPrimDouble: {
2200 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2201 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2202 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2203 if (instruction->IsAdd()) {
2204 if (type == Primitive::kPrimFloat) {
2205 __ AddS(dst, lhs, rhs);
2206 } else {
2207 __ AddD(dst, lhs, rhs);
2208 }
2209 } else {
2210 DCHECK(instruction->IsSub());
2211 if (type == Primitive::kPrimFloat) {
2212 __ SubS(dst, lhs, rhs);
2213 } else {
2214 __ SubD(dst, lhs, rhs);
2215 }
2216 }
2217 break;
2218 }
2219
2220 default:
2221 LOG(FATAL) << "Unexpected binary operation type " << type;
2222 }
2223}
2224
2225void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002226 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002227
2228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2229 Primitive::Type type = instr->GetResultType();
2230 switch (type) {
2231 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002232 locations->SetInAt(0, Location::RequiresRegister());
2233 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2235 break;
2236 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002237 locations->SetInAt(0, Location::RequiresRegister());
2238 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2239 locations->SetOut(Location::RequiresRegister());
2240 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002241 default:
2242 LOG(FATAL) << "Unexpected shift type " << type;
2243 }
2244}
2245
2246static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2247
2248void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002249 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250 LocationSummary* locations = instr->GetLocations();
2251 Primitive::Type type = instr->GetType();
2252
2253 Location rhs_location = locations->InAt(1);
2254 bool use_imm = rhs_location.IsConstant();
2255 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2256 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002257 const uint32_t shift_mask =
2258 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002259 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002260 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2261 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262
2263 switch (type) {
2264 case Primitive::kPrimInt: {
2265 Register dst = locations->Out().AsRegister<Register>();
2266 Register lhs = locations->InAt(0).AsRegister<Register>();
2267 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002268 if (shift_value == 0) {
2269 if (dst != lhs) {
2270 __ Move(dst, lhs);
2271 }
2272 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273 __ Sll(dst, lhs, shift_value);
2274 } else if (instr->IsShr()) {
2275 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002276 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002278 } else {
2279 if (has_ins_rotr) {
2280 __ Rotr(dst, lhs, shift_value);
2281 } else {
2282 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2283 __ Srl(dst, lhs, shift_value);
2284 __ Or(dst, dst, TMP);
2285 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002286 }
2287 } else {
2288 if (instr->IsShl()) {
2289 __ Sllv(dst, lhs, rhs_reg);
2290 } else if (instr->IsShr()) {
2291 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002292 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002293 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002294 } else {
2295 if (has_ins_rotr) {
2296 __ Rotrv(dst, lhs, rhs_reg);
2297 } else {
2298 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002299 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2300 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2301 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2302 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2303 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002304 __ Sllv(TMP, lhs, TMP);
2305 __ Srlv(dst, lhs, rhs_reg);
2306 __ Or(dst, dst, TMP);
2307 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002308 }
2309 }
2310 break;
2311 }
2312
2313 case Primitive::kPrimLong: {
2314 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2315 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2316 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2317 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2318 if (use_imm) {
2319 if (shift_value == 0) {
2320 codegen_->Move64(locations->Out(), locations->InAt(0));
2321 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002322 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002323 if (instr->IsShl()) {
2324 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2325 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2326 __ Sll(dst_low, lhs_low, shift_value);
2327 } else if (instr->IsShr()) {
2328 __ Srl(dst_low, lhs_low, shift_value);
2329 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2330 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002331 } else if (instr->IsUShr()) {
2332 __ Srl(dst_low, lhs_low, shift_value);
2333 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2334 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002335 } else {
2336 __ Srl(dst_low, lhs_low, shift_value);
2337 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2338 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002340 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002342 if (instr->IsShl()) {
2343 __ Sll(dst_low, lhs_low, shift_value);
2344 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2345 __ Sll(dst_high, lhs_high, shift_value);
2346 __ Or(dst_high, dst_high, TMP);
2347 } else if (instr->IsShr()) {
2348 __ Sra(dst_high, lhs_high, shift_value);
2349 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2350 __ Srl(dst_low, lhs_low, shift_value);
2351 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002352 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002353 __ Srl(dst_high, lhs_high, shift_value);
2354 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2355 __ Srl(dst_low, lhs_low, shift_value);
2356 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002357 } else {
2358 __ Srl(TMP, lhs_low, shift_value);
2359 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2360 __ Or(dst_low, dst_low, TMP);
2361 __ Srl(TMP, lhs_high, shift_value);
2362 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2363 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002364 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 }
2366 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002367 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002369 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 __ Move(dst_low, ZERO);
2371 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002372 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002375 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002377 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002378 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002379 // 64-bit rotation by 32 is just a swap.
2380 __ Move(dst_low, lhs_high);
2381 __ Move(dst_high, lhs_low);
2382 } else {
2383 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002384 __ Srl(dst_low, lhs_high, shift_value_high);
2385 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2386 __ Srl(dst_high, lhs_low, shift_value_high);
2387 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002388 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002389 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2390 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002391 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002392 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2393 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002394 __ Or(dst_high, dst_high, TMP);
2395 }
2396 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397 }
2398 }
2399 } else {
2400 MipsLabel done;
2401 if (instr->IsShl()) {
2402 __ Sllv(dst_low, lhs_low, rhs_reg);
2403 __ Nor(AT, ZERO, rhs_reg);
2404 __ Srl(TMP, lhs_low, 1);
2405 __ Srlv(TMP, TMP, AT);
2406 __ Sllv(dst_high, lhs_high, rhs_reg);
2407 __ Or(dst_high, dst_high, TMP);
2408 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2409 __ Beqz(TMP, &done);
2410 __ Move(dst_high, dst_low);
2411 __ Move(dst_low, ZERO);
2412 } else if (instr->IsShr()) {
2413 __ Srav(dst_high, lhs_high, rhs_reg);
2414 __ Nor(AT, ZERO, rhs_reg);
2415 __ Sll(TMP, lhs_high, 1);
2416 __ Sllv(TMP, TMP, AT);
2417 __ Srlv(dst_low, lhs_low, rhs_reg);
2418 __ Or(dst_low, dst_low, TMP);
2419 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2420 __ Beqz(TMP, &done);
2421 __ Move(dst_low, dst_high);
2422 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002423 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424 __ Srlv(dst_high, lhs_high, rhs_reg);
2425 __ Nor(AT, ZERO, rhs_reg);
2426 __ Sll(TMP, lhs_high, 1);
2427 __ Sllv(TMP, TMP, AT);
2428 __ Srlv(dst_low, lhs_low, rhs_reg);
2429 __ Or(dst_low, dst_low, TMP);
2430 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2431 __ Beqz(TMP, &done);
2432 __ Move(dst_low, dst_high);
2433 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 } else {
2435 __ Nor(AT, ZERO, rhs_reg);
2436 __ Srlv(TMP, lhs_low, rhs_reg);
2437 __ Sll(dst_low, lhs_high, 1);
2438 __ Sllv(dst_low, dst_low, AT);
2439 __ Or(dst_low, dst_low, TMP);
2440 __ Srlv(TMP, lhs_high, rhs_reg);
2441 __ Sll(dst_high, lhs_low, 1);
2442 __ Sllv(dst_high, dst_high, AT);
2443 __ Or(dst_high, dst_high, TMP);
2444 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2445 __ Beqz(TMP, &done);
2446 __ Move(TMP, dst_high);
2447 __ Move(dst_high, dst_low);
2448 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002449 }
2450 __ Bind(&done);
2451 }
2452 break;
2453 }
2454
2455 default:
2456 LOG(FATAL) << "Unexpected shift operation type " << type;
2457 }
2458}
2459
2460void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2461 HandleBinaryOp(instruction);
2462}
2463
2464void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2465 HandleBinaryOp(instruction);
2466}
2467
2468void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2469 HandleBinaryOp(instruction);
2470}
2471
2472void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2473 HandleBinaryOp(instruction);
2474}
2475
2476void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002477 Primitive::Type type = instruction->GetType();
2478 bool object_array_get_with_read_barrier =
2479 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002480 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002481 new (GetGraph()->GetArena()) LocationSummary(instruction,
2482 object_array_get_with_read_barrier
2483 ? LocationSummary::kCallOnSlowPath
2484 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002485 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2486 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2487 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 locations->SetInAt(0, Location::RequiresRegister());
2489 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002490 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002491 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2492 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002493 // The output overlaps in the case of an object array get with
2494 // read barriers enabled: we do not want the move to overwrite the
2495 // array's location, as we need it to emit the read barrier.
2496 locations->SetOut(Location::RequiresRegister(),
2497 object_array_get_with_read_barrier
2498 ? Location::kOutputOverlap
2499 : Location::kNoOutputOverlap);
2500 }
2501 // We need a temporary register for the read barrier marking slow
2502 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2503 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2504 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002505 }
2506}
2507
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002508static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2509 auto null_checker = [codegen, instruction]() {
2510 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002511 };
2512 return null_checker;
2513}
2514
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002515void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2516 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002517 Location obj_loc = locations->InAt(0);
2518 Register obj = obj_loc.AsRegister<Register>();
2519 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002520 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002521 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002522 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002523
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002524 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002525 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2526 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002527 switch (type) {
2528 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002529 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002530 if (index.IsConstant()) {
2531 size_t offset =
2532 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002533 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002534 } else {
2535 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002536 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002537 }
2538 break;
2539 }
2540
2541 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002542 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002543 if (index.IsConstant()) {
2544 size_t offset =
2545 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002546 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002547 } else {
2548 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002549 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002550 }
2551 break;
2552 }
2553
2554 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002555 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002556 if (index.IsConstant()) {
2557 size_t offset =
2558 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002559 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002560 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002561 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002562 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002563 }
2564 break;
2565 }
2566
2567 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002568 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002569 if (maybe_compressed_char_at) {
2570 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2571 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2572 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2573 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2574 "Expecting 0=compressed, 1=uncompressed");
2575 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002576 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002577 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2578 if (maybe_compressed_char_at) {
2579 MipsLabel uncompressed_load, done;
2580 __ Bnez(TMP, &uncompressed_load);
2581 __ LoadFromOffset(kLoadUnsignedByte,
2582 out,
2583 obj,
2584 data_offset + (const_index << TIMES_1));
2585 __ B(&done);
2586 __ Bind(&uncompressed_load);
2587 __ LoadFromOffset(kLoadUnsignedHalfword,
2588 out,
2589 obj,
2590 data_offset + (const_index << TIMES_2));
2591 __ Bind(&done);
2592 } else {
2593 __ LoadFromOffset(kLoadUnsignedHalfword,
2594 out,
2595 obj,
2596 data_offset + (const_index << TIMES_2),
2597 null_checker);
2598 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002599 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002600 Register index_reg = index.AsRegister<Register>();
2601 if (maybe_compressed_char_at) {
2602 MipsLabel uncompressed_load, done;
2603 __ Bnez(TMP, &uncompressed_load);
2604 __ Addu(TMP, obj, index_reg);
2605 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2606 __ B(&done);
2607 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002608 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002609 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2610 __ Bind(&done);
2611 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002612 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002613 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2614 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002615 }
2616 break;
2617 }
2618
Alexey Frunze15958152017-02-09 19:08:30 -08002619 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002621 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002622 if (index.IsConstant()) {
2623 size_t offset =
2624 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002625 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002626 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002627 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002628 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629 }
2630 break;
2631 }
2632
Alexey Frunze15958152017-02-09 19:08:30 -08002633 case Primitive::kPrimNot: {
2634 static_assert(
2635 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2636 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2637 // /* HeapReference<Object> */ out =
2638 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2639 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2640 Location temp = locations->GetTemp(0);
2641 // Note that a potential implicit null check is handled in this
2642 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2643 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2644 out_loc,
2645 obj,
2646 data_offset,
2647 index,
2648 temp,
2649 /* needs_null_check */ true);
2650 } else {
2651 Register out = out_loc.AsRegister<Register>();
2652 if (index.IsConstant()) {
2653 size_t offset =
2654 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2655 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2656 // If read barriers are enabled, emit read barriers other than
2657 // Baker's using a slow path (and also unpoison the loaded
2658 // reference, if heap poisoning is enabled).
2659 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2660 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002661 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002662 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2663 // If read barriers are enabled, emit read barriers other than
2664 // Baker's using a slow path (and also unpoison the loaded
2665 // reference, if heap poisoning is enabled).
2666 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2667 out_loc,
2668 out_loc,
2669 obj_loc,
2670 data_offset,
2671 index);
2672 }
2673 }
2674 break;
2675 }
2676
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002678 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002679 if (index.IsConstant()) {
2680 size_t offset =
2681 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002682 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002683 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002684 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002685 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002686 }
2687 break;
2688 }
2689
2690 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002691 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 if (index.IsConstant()) {
2693 size_t offset =
2694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002695 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002697 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002698 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699 }
2700 break;
2701 }
2702
2703 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002704 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002705 if (index.IsConstant()) {
2706 size_t offset =
2707 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002708 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002709 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002710 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002711 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002712 }
2713 break;
2714 }
2715
2716 case Primitive::kPrimVoid:
2717 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2718 UNREACHABLE();
2719 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002720}
2721
2722void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2724 locations->SetInAt(0, Location::RequiresRegister());
2725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2726}
2727
2728void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2729 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002730 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002731 Register obj = locations->InAt(0).AsRegister<Register>();
2732 Register out = locations->Out().AsRegister<Register>();
2733 __ LoadFromOffset(kLoadWord, out, obj, offset);
2734 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002735 // Mask out compression flag from String's array length.
2736 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2737 __ Srl(out, out, 1u);
2738 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002739}
2740
Alexey Frunzef58b2482016-09-02 22:14:06 -07002741Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2742 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2743 ? Location::ConstantLocation(instruction->AsConstant())
2744 : Location::RequiresRegister();
2745}
2746
2747Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2748 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2749 // We can store a non-zero float or double constant without first loading it into the FPU,
2750 // but we should only prefer this if the constant has a single use.
2751 if (instruction->IsConstant() &&
2752 (instruction->AsConstant()->IsZeroBitPattern() ||
2753 instruction->GetUses().HasExactlyOneElement())) {
2754 return Location::ConstantLocation(instruction->AsConstant());
2755 // Otherwise fall through and require an FPU register for the constant.
2756 }
2757 return Location::RequiresFpuRegister();
2758}
2759
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002760void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002761 Primitive::Type value_type = instruction->GetComponentType();
2762
2763 bool needs_write_barrier =
2764 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2765 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2766
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2768 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002769 may_need_runtime_call_for_type_check ?
2770 LocationSummary::kCallOnSlowPath :
2771 LocationSummary::kNoCall);
2772
2773 locations->SetInAt(0, Location::RequiresRegister());
2774 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2775 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2776 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002777 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002778 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2779 }
2780 if (needs_write_barrier) {
2781 // Temporary register for the write barrier.
2782 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002783 }
2784}
2785
2786void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2787 LocationSummary* locations = instruction->GetLocations();
2788 Register obj = locations->InAt(0).AsRegister<Register>();
2789 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002790 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002792 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002793 bool needs_write_barrier =
2794 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002795 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002796 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797
2798 switch (value_type) {
2799 case Primitive::kPrimBoolean:
2800 case Primitive::kPrimByte: {
2801 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002802 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002803 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002804 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002805 __ Addu(base_reg, obj, index.AsRegister<Register>());
2806 }
2807 if (value_location.IsConstant()) {
2808 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2809 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2810 } else {
2811 Register value = value_location.AsRegister<Register>();
2812 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 }
2814 break;
2815 }
2816
2817 case Primitive::kPrimShort:
2818 case Primitive::kPrimChar: {
2819 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002821 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002823 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002824 }
2825 if (value_location.IsConstant()) {
2826 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2827 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2828 } else {
2829 Register value = value_location.AsRegister<Register>();
2830 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002831 }
2832 break;
2833 }
2834
Alexey Frunze15958152017-02-09 19:08:30 -08002835 case Primitive::kPrimInt: {
2836 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2837 if (index.IsConstant()) {
2838 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2839 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002840 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002841 }
2842 if (value_location.IsConstant()) {
2843 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2844 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2845 } else {
2846 Register value = value_location.AsRegister<Register>();
2847 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2848 }
2849 break;
2850 }
2851
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002852 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002853 if (value_location.IsConstant()) {
2854 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002856 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002857 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002858 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002859 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860 }
Alexey Frunze15958152017-02-09 19:08:30 -08002861 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2862 DCHECK_EQ(value, 0);
2863 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2864 DCHECK(!needs_write_barrier);
2865 DCHECK(!may_need_runtime_call_for_type_check);
2866 break;
2867 }
2868
2869 DCHECK(needs_write_barrier);
2870 Register value = value_location.AsRegister<Register>();
2871 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2872 Register temp2 = TMP; // Doesn't need to survive slow path.
2873 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2874 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2875 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2876 MipsLabel done;
2877 SlowPathCodeMIPS* slow_path = nullptr;
2878
2879 if (may_need_runtime_call_for_type_check) {
2880 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2881 codegen_->AddSlowPath(slow_path);
2882 if (instruction->GetValueCanBeNull()) {
2883 MipsLabel non_zero;
2884 __ Bnez(value, &non_zero);
2885 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2886 if (index.IsConstant()) {
2887 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002888 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002889 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002890 }
Alexey Frunze15958152017-02-09 19:08:30 -08002891 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2892 __ B(&done);
2893 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002894 }
Alexey Frunze15958152017-02-09 19:08:30 -08002895
2896 // Note that when read barriers are enabled, the type checks
2897 // are performed without read barriers. This is fine, even in
2898 // the case where a class object is in the from-space after
2899 // the flip, as a comparison involving such a type would not
2900 // produce a false positive; it may of course produce a false
2901 // negative, in which case we would take the ArraySet slow
2902 // path.
2903
2904 // /* HeapReference<Class> */ temp1 = obj->klass_
2905 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2906 __ MaybeUnpoisonHeapReference(temp1);
2907
2908 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2909 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2910 // /* HeapReference<Class> */ temp2 = value->klass_
2911 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2912 // If heap poisoning is enabled, no need to unpoison `temp1`
2913 // nor `temp2`, as we are comparing two poisoned references.
2914
2915 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2916 MipsLabel do_put;
2917 __ Beq(temp1, temp2, &do_put);
2918 // If heap poisoning is enabled, the `temp1` reference has
2919 // not been unpoisoned yet; unpoison it now.
2920 __ MaybeUnpoisonHeapReference(temp1);
2921
2922 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2923 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2924 // If heap poisoning is enabled, no need to unpoison
2925 // `temp1`, as we are comparing against null below.
2926 __ Bnez(temp1, slow_path->GetEntryLabel());
2927 __ Bind(&do_put);
2928 } else {
2929 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2930 }
2931 }
2932
2933 Register source = value;
2934 if (kPoisonHeapReferences) {
2935 // Note that in the case where `value` is a null reference,
2936 // we do not enter this block, as a null reference does not
2937 // need poisoning.
2938 __ Move(temp1, value);
2939 __ PoisonHeapReference(temp1);
2940 source = temp1;
2941 }
2942
2943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2944 if (index.IsConstant()) {
2945 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002946 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002947 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002948 }
2949 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2950
2951 if (!may_need_runtime_call_for_type_check) {
2952 codegen_->MaybeRecordImplicitNullCheck(instruction);
2953 }
2954
2955 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2956
2957 if (done.IsLinked()) {
2958 __ Bind(&done);
2959 }
2960
2961 if (slow_path != nullptr) {
2962 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 }
2964 break;
2965 }
2966
2967 case Primitive::kPrimLong: {
2968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002969 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002970 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002971 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002972 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002973 }
2974 if (value_location.IsConstant()) {
2975 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2976 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2977 } else {
2978 Register value = value_location.AsRegisterPairLow<Register>();
2979 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002980 }
2981 break;
2982 }
2983
2984 case Primitive::kPrimFloat: {
2985 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002986 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002987 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002988 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002989 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002990 }
2991 if (value_location.IsConstant()) {
2992 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2993 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2994 } else {
2995 FRegister value = value_location.AsFpuRegister<FRegister>();
2996 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002997 }
2998 break;
2999 }
3000
3001 case Primitive::kPrimDouble: {
3002 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003003 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003004 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003005 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003006 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003007 }
3008 if (value_location.IsConstant()) {
3009 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3010 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3011 } else {
3012 FRegister value = value_location.AsFpuRegister<FRegister>();
3013 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003014 }
3015 break;
3016 }
3017
3018 case Primitive::kPrimVoid:
3019 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3020 UNREACHABLE();
3021 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003022}
3023
3024void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003025 RegisterSet caller_saves = RegisterSet::Empty();
3026 InvokeRuntimeCallingConvention calling_convention;
3027 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3028 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3029 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003030 locations->SetInAt(0, Location::RequiresRegister());
3031 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003032}
3033
3034void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3035 LocationSummary* locations = instruction->GetLocations();
3036 BoundsCheckSlowPathMIPS* slow_path =
3037 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3038 codegen_->AddSlowPath(slow_path);
3039
3040 Register index = locations->InAt(0).AsRegister<Register>();
3041 Register length = locations->InAt(1).AsRegister<Register>();
3042
3043 // length is limited by the maximum positive signed 32-bit integer.
3044 // Unsigned comparison of length and index checks for index < 0
3045 // and for length <= index simultaneously.
3046 __ Bgeu(index, length, slow_path->GetEntryLabel());
3047}
3048
Alexey Frunze15958152017-02-09 19:08:30 -08003049// Temp is used for read barrier.
3050static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3051 if (kEmitCompilerReadBarrier &&
3052 (kUseBakerReadBarrier ||
3053 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3054 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3055 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3056 return 1;
3057 }
3058 return 0;
3059}
3060
3061// Extra temp is used for read barrier.
3062static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3063 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3064}
3065
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003066void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003067 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3068 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3069
3070 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3071 switch (type_check_kind) {
3072 case TypeCheckKind::kExactCheck:
3073 case TypeCheckKind::kAbstractClassCheck:
3074 case TypeCheckKind::kClassHierarchyCheck:
3075 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003076 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003077 ? LocationSummary::kCallOnSlowPath
3078 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3079 break;
3080 case TypeCheckKind::kArrayCheck:
3081 case TypeCheckKind::kUnresolvedCheck:
3082 case TypeCheckKind::kInterfaceCheck:
3083 call_kind = LocationSummary::kCallOnSlowPath;
3084 break;
3085 }
3086
3087 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003088 locations->SetInAt(0, Location::RequiresRegister());
3089 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003090 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003091}
3092
3093void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003094 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003095 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003096 Location obj_loc = locations->InAt(0);
3097 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003098 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003099 Location temp_loc = locations->GetTemp(0);
3100 Register temp = temp_loc.AsRegister<Register>();
3101 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3102 DCHECK_LE(num_temps, 2u);
3103 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003104 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3105 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3106 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3107 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3108 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3109 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3110 const uint32_t object_array_data_offset =
3111 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3112 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003113
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003114 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3115 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3116 // read barriers is done for performance and code size reasons.
3117 bool is_type_check_slow_path_fatal = false;
3118 if (!kEmitCompilerReadBarrier) {
3119 is_type_check_slow_path_fatal =
3120 (type_check_kind == TypeCheckKind::kExactCheck ||
3121 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3122 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3123 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3124 !instruction->CanThrowIntoCatchBlock();
3125 }
3126 SlowPathCodeMIPS* slow_path =
3127 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3128 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003129 codegen_->AddSlowPath(slow_path);
3130
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003131 // Avoid this check if we know `obj` is not null.
3132 if (instruction->MustDoNullCheck()) {
3133 __ Beqz(obj, &done);
3134 }
3135
3136 switch (type_check_kind) {
3137 case TypeCheckKind::kExactCheck:
3138 case TypeCheckKind::kArrayCheck: {
3139 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003140 GenerateReferenceLoadTwoRegisters(instruction,
3141 temp_loc,
3142 obj_loc,
3143 class_offset,
3144 maybe_temp2_loc,
3145 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003146 // Jump to slow path for throwing the exception or doing a
3147 // more involved array check.
3148 __ Bne(temp, cls, slow_path->GetEntryLabel());
3149 break;
3150 }
3151
3152 case TypeCheckKind::kAbstractClassCheck: {
3153 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003154 GenerateReferenceLoadTwoRegisters(instruction,
3155 temp_loc,
3156 obj_loc,
3157 class_offset,
3158 maybe_temp2_loc,
3159 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003160 // If the class is abstract, we eagerly fetch the super class of the
3161 // object to avoid doing a comparison we know will fail.
3162 MipsLabel loop;
3163 __ Bind(&loop);
3164 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003165 GenerateReferenceLoadOneRegister(instruction,
3166 temp_loc,
3167 super_offset,
3168 maybe_temp2_loc,
3169 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003170 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3171 // exception.
3172 __ Beqz(temp, slow_path->GetEntryLabel());
3173 // Otherwise, compare the classes.
3174 __ Bne(temp, cls, &loop);
3175 break;
3176 }
3177
3178 case TypeCheckKind::kClassHierarchyCheck: {
3179 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003180 GenerateReferenceLoadTwoRegisters(instruction,
3181 temp_loc,
3182 obj_loc,
3183 class_offset,
3184 maybe_temp2_loc,
3185 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003186 // Walk over the class hierarchy to find a match.
3187 MipsLabel loop;
3188 __ Bind(&loop);
3189 __ Beq(temp, cls, &done);
3190 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003191 GenerateReferenceLoadOneRegister(instruction,
3192 temp_loc,
3193 super_offset,
3194 maybe_temp2_loc,
3195 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003196 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3197 // exception. Otherwise, jump to the beginning of the loop.
3198 __ Bnez(temp, &loop);
3199 __ B(slow_path->GetEntryLabel());
3200 break;
3201 }
3202
3203 case TypeCheckKind::kArrayObjectCheck: {
3204 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003205 GenerateReferenceLoadTwoRegisters(instruction,
3206 temp_loc,
3207 obj_loc,
3208 class_offset,
3209 maybe_temp2_loc,
3210 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003211 // Do an exact check.
3212 __ Beq(temp, cls, &done);
3213 // Otherwise, we need to check that the object's class is a non-primitive array.
3214 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003215 GenerateReferenceLoadOneRegister(instruction,
3216 temp_loc,
3217 component_offset,
3218 maybe_temp2_loc,
3219 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003220 // If the component type is null, jump to the slow path to throw the exception.
3221 __ Beqz(temp, slow_path->GetEntryLabel());
3222 // Otherwise, the object is indeed an array, further check that this component
3223 // type is not a primitive type.
3224 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3225 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3226 __ Bnez(temp, slow_path->GetEntryLabel());
3227 break;
3228 }
3229
3230 case TypeCheckKind::kUnresolvedCheck:
3231 // We always go into the type check slow path for the unresolved check case.
3232 // We cannot directly call the CheckCast runtime entry point
3233 // without resorting to a type checking slow path here (i.e. by
3234 // calling InvokeRuntime directly), as it would require to
3235 // assign fixed registers for the inputs of this HInstanceOf
3236 // instruction (following the runtime calling convention), which
3237 // might be cluttered by the potential first read barrier
3238 // emission at the beginning of this method.
3239 __ B(slow_path->GetEntryLabel());
3240 break;
3241
3242 case TypeCheckKind::kInterfaceCheck: {
3243 // Avoid read barriers to improve performance of the fast path. We can not get false
3244 // positives by doing this.
3245 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003246 GenerateReferenceLoadTwoRegisters(instruction,
3247 temp_loc,
3248 obj_loc,
3249 class_offset,
3250 maybe_temp2_loc,
3251 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003252 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003253 GenerateReferenceLoadTwoRegisters(instruction,
3254 temp_loc,
3255 temp_loc,
3256 iftable_offset,
3257 maybe_temp2_loc,
3258 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003259 // Iftable is never null.
3260 __ Lw(TMP, temp, array_length_offset);
3261 // Loop through the iftable and check if any class matches.
3262 MipsLabel loop;
3263 __ Bind(&loop);
3264 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3265 __ Beqz(TMP, slow_path->GetEntryLabel());
3266 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3267 __ MaybeUnpoisonHeapReference(AT);
3268 // Go to next interface.
3269 __ Addiu(TMP, TMP, -2);
3270 // Compare the classes and continue the loop if they do not match.
3271 __ Bne(AT, cls, &loop);
3272 break;
3273 }
3274 }
3275
3276 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003277 __ Bind(slow_path->GetExitLabel());
3278}
3279
3280void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3281 LocationSummary* locations =
3282 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3283 locations->SetInAt(0, Location::RequiresRegister());
3284 if (check->HasUses()) {
3285 locations->SetOut(Location::SameAsFirstInput());
3286 }
3287}
3288
3289void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3290 // We assume the class is not null.
3291 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3292 check->GetLoadClass(),
3293 check,
3294 check->GetDexPc(),
3295 true);
3296 codegen_->AddSlowPath(slow_path);
3297 GenerateClassInitializationCheck(slow_path,
3298 check->GetLocations()->InAt(0).AsRegister<Register>());
3299}
3300
3301void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3302 Primitive::Type in_type = compare->InputAt(0)->GetType();
3303
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003304 LocationSummary* locations =
3305 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003306
3307 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003308 case Primitive::kPrimBoolean:
3309 case Primitive::kPrimByte:
3310 case Primitive::kPrimShort:
3311 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003312 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003313 locations->SetInAt(0, Location::RequiresRegister());
3314 locations->SetInAt(1, Location::RequiresRegister());
3315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3316 break;
3317
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003318 case Primitive::kPrimLong:
3319 locations->SetInAt(0, Location::RequiresRegister());
3320 locations->SetInAt(1, Location::RequiresRegister());
3321 // Output overlaps because it is written before doing the low comparison.
3322 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3323 break;
3324
3325 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003326 case Primitive::kPrimDouble:
3327 locations->SetInAt(0, Location::RequiresFpuRegister());
3328 locations->SetInAt(1, Location::RequiresFpuRegister());
3329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003330 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003331
3332 default:
3333 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3334 }
3335}
3336
3337void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3338 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003339 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003340 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003341 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003342
3343 // 0 if: left == right
3344 // 1 if: left > right
3345 // -1 if: left < right
3346 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003347 case Primitive::kPrimBoolean:
3348 case Primitive::kPrimByte:
3349 case Primitive::kPrimShort:
3350 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003351 case Primitive::kPrimInt: {
3352 Register lhs = locations->InAt(0).AsRegister<Register>();
3353 Register rhs = locations->InAt(1).AsRegister<Register>();
3354 __ Slt(TMP, lhs, rhs);
3355 __ Slt(res, rhs, lhs);
3356 __ Subu(res, res, TMP);
3357 break;
3358 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003359 case Primitive::kPrimLong: {
3360 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003361 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3362 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3363 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3364 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3365 // TODO: more efficient (direct) comparison with a constant.
3366 __ Slt(TMP, lhs_high, rhs_high);
3367 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3368 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3369 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3370 __ Sltu(TMP, lhs_low, rhs_low);
3371 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3372 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3373 __ Bind(&done);
3374 break;
3375 }
3376
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003377 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003378 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003379 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3380 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3381 MipsLabel done;
3382 if (isR6) {
3383 __ CmpEqS(FTMP, lhs, rhs);
3384 __ LoadConst32(res, 0);
3385 __ Bc1nez(FTMP, &done);
3386 if (gt_bias) {
3387 __ CmpLtS(FTMP, lhs, rhs);
3388 __ LoadConst32(res, -1);
3389 __ Bc1nez(FTMP, &done);
3390 __ LoadConst32(res, 1);
3391 } else {
3392 __ CmpLtS(FTMP, rhs, lhs);
3393 __ LoadConst32(res, 1);
3394 __ Bc1nez(FTMP, &done);
3395 __ LoadConst32(res, -1);
3396 }
3397 } else {
3398 if (gt_bias) {
3399 __ ColtS(0, lhs, rhs);
3400 __ LoadConst32(res, -1);
3401 __ Bc1t(0, &done);
3402 __ CeqS(0, lhs, rhs);
3403 __ LoadConst32(res, 1);
3404 __ Movt(res, ZERO, 0);
3405 } else {
3406 __ ColtS(0, rhs, lhs);
3407 __ LoadConst32(res, 1);
3408 __ Bc1t(0, &done);
3409 __ CeqS(0, lhs, rhs);
3410 __ LoadConst32(res, -1);
3411 __ Movt(res, ZERO, 0);
3412 }
3413 }
3414 __ Bind(&done);
3415 break;
3416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003417 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003418 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003419 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3420 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3421 MipsLabel done;
3422 if (isR6) {
3423 __ CmpEqD(FTMP, lhs, rhs);
3424 __ LoadConst32(res, 0);
3425 __ Bc1nez(FTMP, &done);
3426 if (gt_bias) {
3427 __ CmpLtD(FTMP, lhs, rhs);
3428 __ LoadConst32(res, -1);
3429 __ Bc1nez(FTMP, &done);
3430 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003431 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003432 __ CmpLtD(FTMP, rhs, lhs);
3433 __ LoadConst32(res, 1);
3434 __ Bc1nez(FTMP, &done);
3435 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003436 }
3437 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003438 if (gt_bias) {
3439 __ ColtD(0, lhs, rhs);
3440 __ LoadConst32(res, -1);
3441 __ Bc1t(0, &done);
3442 __ CeqD(0, lhs, rhs);
3443 __ LoadConst32(res, 1);
3444 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003445 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003446 __ ColtD(0, rhs, lhs);
3447 __ LoadConst32(res, 1);
3448 __ Bc1t(0, &done);
3449 __ CeqD(0, lhs, rhs);
3450 __ LoadConst32(res, -1);
3451 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003452 }
3453 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003454 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003455 break;
3456 }
3457
3458 default:
3459 LOG(FATAL) << "Unimplemented compare type " << in_type;
3460 }
3461}
3462
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003463void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003464 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003465 switch (instruction->InputAt(0)->GetType()) {
3466 default:
3467 case Primitive::kPrimLong:
3468 locations->SetInAt(0, Location::RequiresRegister());
3469 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3470 break;
3471
3472 case Primitive::kPrimFloat:
3473 case Primitive::kPrimDouble:
3474 locations->SetInAt(0, Location::RequiresFpuRegister());
3475 locations->SetInAt(1, Location::RequiresFpuRegister());
3476 break;
3477 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003478 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3480 }
3481}
3482
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003483void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003484 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003485 return;
3486 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003487
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003488 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003489 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003490
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003491 switch (type) {
3492 default:
3493 // Integer case.
3494 GenerateIntCompare(instruction->GetCondition(), locations);
3495 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003497 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003498 GenerateLongCompare(instruction->GetCondition(), locations);
3499 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003500
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003501 case Primitive::kPrimFloat:
3502 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003503 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3504 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003505 }
3506}
3507
Alexey Frunze7e99e052015-11-24 19:28:01 -08003508void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3509 DCHECK(instruction->IsDiv() || instruction->IsRem());
3510 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3511
3512 LocationSummary* locations = instruction->GetLocations();
3513 Location second = locations->InAt(1);
3514 DCHECK(second.IsConstant());
3515
3516 Register out = locations->Out().AsRegister<Register>();
3517 Register dividend = locations->InAt(0).AsRegister<Register>();
3518 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3519 DCHECK(imm == 1 || imm == -1);
3520
3521 if (instruction->IsRem()) {
3522 __ Move(out, ZERO);
3523 } else {
3524 if (imm == -1) {
3525 __ Subu(out, ZERO, dividend);
3526 } else if (out != dividend) {
3527 __ Move(out, dividend);
3528 }
3529 }
3530}
3531
3532void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3533 DCHECK(instruction->IsDiv() || instruction->IsRem());
3534 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3535
3536 LocationSummary* locations = instruction->GetLocations();
3537 Location second = locations->InAt(1);
3538 DCHECK(second.IsConstant());
3539
3540 Register out = locations->Out().AsRegister<Register>();
3541 Register dividend = locations->InAt(0).AsRegister<Register>();
3542 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003543 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003544 int ctz_imm = CTZ(abs_imm);
3545
3546 if (instruction->IsDiv()) {
3547 if (ctz_imm == 1) {
3548 // Fast path for division by +/-2, which is very common.
3549 __ Srl(TMP, dividend, 31);
3550 } else {
3551 __ Sra(TMP, dividend, 31);
3552 __ Srl(TMP, TMP, 32 - ctz_imm);
3553 }
3554 __ Addu(out, dividend, TMP);
3555 __ Sra(out, out, ctz_imm);
3556 if (imm < 0) {
3557 __ Subu(out, ZERO, out);
3558 }
3559 } else {
3560 if (ctz_imm == 1) {
3561 // Fast path for modulo +/-2, which is very common.
3562 __ Sra(TMP, dividend, 31);
3563 __ Subu(out, dividend, TMP);
3564 __ Andi(out, out, 1);
3565 __ Addu(out, out, TMP);
3566 } else {
3567 __ Sra(TMP, dividend, 31);
3568 __ Srl(TMP, TMP, 32 - ctz_imm);
3569 __ Addu(out, dividend, TMP);
3570 if (IsUint<16>(abs_imm - 1)) {
3571 __ Andi(out, out, abs_imm - 1);
3572 } else {
3573 __ Sll(out, out, 32 - ctz_imm);
3574 __ Srl(out, out, 32 - ctz_imm);
3575 }
3576 __ Subu(out, out, TMP);
3577 }
3578 }
3579}
3580
3581void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3582 DCHECK(instruction->IsDiv() || instruction->IsRem());
3583 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3584
3585 LocationSummary* locations = instruction->GetLocations();
3586 Location second = locations->InAt(1);
3587 DCHECK(second.IsConstant());
3588
3589 Register out = locations->Out().AsRegister<Register>();
3590 Register dividend = locations->InAt(0).AsRegister<Register>();
3591 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3592
3593 int64_t magic;
3594 int shift;
3595 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3596
3597 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3598
3599 __ LoadConst32(TMP, magic);
3600 if (isR6) {
3601 __ MuhR6(TMP, dividend, TMP);
3602 } else {
3603 __ MultR2(dividend, TMP);
3604 __ Mfhi(TMP);
3605 }
3606 if (imm > 0 && magic < 0) {
3607 __ Addu(TMP, TMP, dividend);
3608 } else if (imm < 0 && magic > 0) {
3609 __ Subu(TMP, TMP, dividend);
3610 }
3611
3612 if (shift != 0) {
3613 __ Sra(TMP, TMP, shift);
3614 }
3615
3616 if (instruction->IsDiv()) {
3617 __ Sra(out, TMP, 31);
3618 __ Subu(out, TMP, out);
3619 } else {
3620 __ Sra(AT, TMP, 31);
3621 __ Subu(AT, TMP, AT);
3622 __ LoadConst32(TMP, imm);
3623 if (isR6) {
3624 __ MulR6(TMP, AT, TMP);
3625 } else {
3626 __ MulR2(TMP, AT, TMP);
3627 }
3628 __ Subu(out, dividend, TMP);
3629 }
3630}
3631
3632void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3633 DCHECK(instruction->IsDiv() || instruction->IsRem());
3634 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3635
3636 LocationSummary* locations = instruction->GetLocations();
3637 Register out = locations->Out().AsRegister<Register>();
3638 Location second = locations->InAt(1);
3639
3640 if (second.IsConstant()) {
3641 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3642 if (imm == 0) {
3643 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3644 } else if (imm == 1 || imm == -1) {
3645 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003646 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003647 DivRemByPowerOfTwo(instruction);
3648 } else {
3649 DCHECK(imm <= -2 || imm >= 2);
3650 GenerateDivRemWithAnyConstant(instruction);
3651 }
3652 } else {
3653 Register dividend = locations->InAt(0).AsRegister<Register>();
3654 Register divisor = second.AsRegister<Register>();
3655 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3656 if (instruction->IsDiv()) {
3657 if (isR6) {
3658 __ DivR6(out, dividend, divisor);
3659 } else {
3660 __ DivR2(out, dividend, divisor);
3661 }
3662 } else {
3663 if (isR6) {
3664 __ ModR6(out, dividend, divisor);
3665 } else {
3666 __ ModR2(out, dividend, divisor);
3667 }
3668 }
3669 }
3670}
3671
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003672void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3673 Primitive::Type type = div->GetResultType();
3674 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003675 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003676 : LocationSummary::kNoCall;
3677
3678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3679
3680 switch (type) {
3681 case Primitive::kPrimInt:
3682 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003683 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003684 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3685 break;
3686
3687 case Primitive::kPrimLong: {
3688 InvokeRuntimeCallingConvention calling_convention;
3689 locations->SetInAt(0, Location::RegisterPairLocation(
3690 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3691 locations->SetInAt(1, Location::RegisterPairLocation(
3692 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3693 locations->SetOut(calling_convention.GetReturnLocation(type));
3694 break;
3695 }
3696
3697 case Primitive::kPrimFloat:
3698 case Primitive::kPrimDouble:
3699 locations->SetInAt(0, Location::RequiresFpuRegister());
3700 locations->SetInAt(1, Location::RequiresFpuRegister());
3701 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3702 break;
3703
3704 default:
3705 LOG(FATAL) << "Unexpected div type " << type;
3706 }
3707}
3708
3709void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3710 Primitive::Type type = instruction->GetType();
3711 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003712
3713 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003714 case Primitive::kPrimInt:
3715 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003716 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003717 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003718 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003719 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3720 break;
3721 }
3722 case Primitive::kPrimFloat:
3723 case Primitive::kPrimDouble: {
3724 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3725 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3726 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3727 if (type == Primitive::kPrimFloat) {
3728 __ DivS(dst, lhs, rhs);
3729 } else {
3730 __ DivD(dst, lhs, rhs);
3731 }
3732 break;
3733 }
3734 default:
3735 LOG(FATAL) << "Unexpected div type " << type;
3736 }
3737}
3738
3739void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003740 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003741 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003742}
3743
3744void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3745 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3746 codegen_->AddSlowPath(slow_path);
3747 Location value = instruction->GetLocations()->InAt(0);
3748 Primitive::Type type = instruction->GetType();
3749
3750 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003751 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003752 case Primitive::kPrimByte:
3753 case Primitive::kPrimChar:
3754 case Primitive::kPrimShort:
3755 case Primitive::kPrimInt: {
3756 if (value.IsConstant()) {
3757 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3758 __ B(slow_path->GetEntryLabel());
3759 } else {
3760 // A division by a non-null constant is valid. We don't need to perform
3761 // any check, so simply fall through.
3762 }
3763 } else {
3764 DCHECK(value.IsRegister()) << value;
3765 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3766 }
3767 break;
3768 }
3769 case Primitive::kPrimLong: {
3770 if (value.IsConstant()) {
3771 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3772 __ B(slow_path->GetEntryLabel());
3773 } else {
3774 // A division by a non-null constant is valid. We don't need to perform
3775 // any check, so simply fall through.
3776 }
3777 } else {
3778 DCHECK(value.IsRegisterPair()) << value;
3779 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3780 __ Beqz(TMP, slow_path->GetEntryLabel());
3781 }
3782 break;
3783 }
3784 default:
3785 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3786 }
3787}
3788
3789void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3790 LocationSummary* locations =
3791 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3792 locations->SetOut(Location::ConstantLocation(constant));
3793}
3794
3795void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3796 // Will be generated at use site.
3797}
3798
3799void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3800 exit->SetLocations(nullptr);
3801}
3802
3803void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3804}
3805
3806void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3807 LocationSummary* locations =
3808 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3809 locations->SetOut(Location::ConstantLocation(constant));
3810}
3811
3812void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3813 // Will be generated at use site.
3814}
3815
3816void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3817 got->SetLocations(nullptr);
3818}
3819
3820void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3821 DCHECK(!successor->IsExitBlock());
3822 HBasicBlock* block = got->GetBlock();
3823 HInstruction* previous = got->GetPrevious();
3824 HLoopInformation* info = block->GetLoopInformation();
3825
3826 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3827 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3828 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3829 return;
3830 }
3831 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3832 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3833 }
3834 if (!codegen_->GoesToNextBlock(block, successor)) {
3835 __ B(codegen_->GetLabelOf(successor));
3836 }
3837}
3838
3839void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3840 HandleGoto(got, got->GetSuccessor());
3841}
3842
3843void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3844 try_boundary->SetLocations(nullptr);
3845}
3846
3847void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3848 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3849 if (!successor->IsExitBlock()) {
3850 HandleGoto(try_boundary, successor);
3851 }
3852}
3853
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003854void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3855 LocationSummary* locations) {
3856 Register dst = locations->Out().AsRegister<Register>();
3857 Register lhs = locations->InAt(0).AsRegister<Register>();
3858 Location rhs_location = locations->InAt(1);
3859 Register rhs_reg = ZERO;
3860 int64_t rhs_imm = 0;
3861 bool use_imm = rhs_location.IsConstant();
3862 if (use_imm) {
3863 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3864 } else {
3865 rhs_reg = rhs_location.AsRegister<Register>();
3866 }
3867
3868 switch (cond) {
3869 case kCondEQ:
3870 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003871 if (use_imm && IsInt<16>(-rhs_imm)) {
3872 if (rhs_imm == 0) {
3873 if (cond == kCondEQ) {
3874 __ Sltiu(dst, lhs, 1);
3875 } else {
3876 __ Sltu(dst, ZERO, lhs);
3877 }
3878 } else {
3879 __ Addiu(dst, lhs, -rhs_imm);
3880 if (cond == kCondEQ) {
3881 __ Sltiu(dst, dst, 1);
3882 } else {
3883 __ Sltu(dst, ZERO, dst);
3884 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003885 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003886 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003887 if (use_imm && IsUint<16>(rhs_imm)) {
3888 __ Xori(dst, lhs, rhs_imm);
3889 } else {
3890 if (use_imm) {
3891 rhs_reg = TMP;
3892 __ LoadConst32(rhs_reg, rhs_imm);
3893 }
3894 __ Xor(dst, lhs, rhs_reg);
3895 }
3896 if (cond == kCondEQ) {
3897 __ Sltiu(dst, dst, 1);
3898 } else {
3899 __ Sltu(dst, ZERO, dst);
3900 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003901 }
3902 break;
3903
3904 case kCondLT:
3905 case kCondGE:
3906 if (use_imm && IsInt<16>(rhs_imm)) {
3907 __ Slti(dst, lhs, rhs_imm);
3908 } else {
3909 if (use_imm) {
3910 rhs_reg = TMP;
3911 __ LoadConst32(rhs_reg, rhs_imm);
3912 }
3913 __ Slt(dst, lhs, rhs_reg);
3914 }
3915 if (cond == kCondGE) {
3916 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3917 // only the slt instruction but no sge.
3918 __ Xori(dst, dst, 1);
3919 }
3920 break;
3921
3922 case kCondLE:
3923 case kCondGT:
3924 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3925 // Simulate lhs <= rhs via lhs < rhs + 1.
3926 __ Slti(dst, lhs, rhs_imm + 1);
3927 if (cond == kCondGT) {
3928 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3929 // only the slti instruction but no sgti.
3930 __ Xori(dst, dst, 1);
3931 }
3932 } else {
3933 if (use_imm) {
3934 rhs_reg = TMP;
3935 __ LoadConst32(rhs_reg, rhs_imm);
3936 }
3937 __ Slt(dst, rhs_reg, lhs);
3938 if (cond == kCondLE) {
3939 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3940 // only the slt instruction but no sle.
3941 __ Xori(dst, dst, 1);
3942 }
3943 }
3944 break;
3945
3946 case kCondB:
3947 case kCondAE:
3948 if (use_imm && IsInt<16>(rhs_imm)) {
3949 // Sltiu sign-extends its 16-bit immediate operand before
3950 // the comparison and thus lets us compare directly with
3951 // unsigned values in the ranges [0, 0x7fff] and
3952 // [0xffff8000, 0xffffffff].
3953 __ Sltiu(dst, lhs, rhs_imm);
3954 } else {
3955 if (use_imm) {
3956 rhs_reg = TMP;
3957 __ LoadConst32(rhs_reg, rhs_imm);
3958 }
3959 __ Sltu(dst, lhs, rhs_reg);
3960 }
3961 if (cond == kCondAE) {
3962 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3963 // only the sltu instruction but no sgeu.
3964 __ Xori(dst, dst, 1);
3965 }
3966 break;
3967
3968 case kCondBE:
3969 case kCondA:
3970 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3971 // Simulate lhs <= rhs via lhs < rhs + 1.
3972 // Note that this only works if rhs + 1 does not overflow
3973 // to 0, hence the check above.
3974 // Sltiu sign-extends its 16-bit immediate operand before
3975 // the comparison and thus lets us compare directly with
3976 // unsigned values in the ranges [0, 0x7fff] and
3977 // [0xffff8000, 0xffffffff].
3978 __ Sltiu(dst, lhs, rhs_imm + 1);
3979 if (cond == kCondA) {
3980 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3981 // only the sltiu instruction but no sgtiu.
3982 __ Xori(dst, dst, 1);
3983 }
3984 } else {
3985 if (use_imm) {
3986 rhs_reg = TMP;
3987 __ LoadConst32(rhs_reg, rhs_imm);
3988 }
3989 __ Sltu(dst, rhs_reg, lhs);
3990 if (cond == kCondBE) {
3991 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3992 // only the sltu instruction but no sleu.
3993 __ Xori(dst, dst, 1);
3994 }
3995 }
3996 break;
3997 }
3998}
3999
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004000bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4001 LocationSummary* input_locations,
4002 Register dst) {
4003 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4004 Location rhs_location = input_locations->InAt(1);
4005 Register rhs_reg = ZERO;
4006 int64_t rhs_imm = 0;
4007 bool use_imm = rhs_location.IsConstant();
4008 if (use_imm) {
4009 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4010 } else {
4011 rhs_reg = rhs_location.AsRegister<Register>();
4012 }
4013
4014 switch (cond) {
4015 case kCondEQ:
4016 case kCondNE:
4017 if (use_imm && IsInt<16>(-rhs_imm)) {
4018 __ Addiu(dst, lhs, -rhs_imm);
4019 } else if (use_imm && IsUint<16>(rhs_imm)) {
4020 __ Xori(dst, lhs, rhs_imm);
4021 } else {
4022 if (use_imm) {
4023 rhs_reg = TMP;
4024 __ LoadConst32(rhs_reg, rhs_imm);
4025 }
4026 __ Xor(dst, lhs, rhs_reg);
4027 }
4028 return (cond == kCondEQ);
4029
4030 case kCondLT:
4031 case kCondGE:
4032 if (use_imm && IsInt<16>(rhs_imm)) {
4033 __ Slti(dst, lhs, rhs_imm);
4034 } else {
4035 if (use_imm) {
4036 rhs_reg = TMP;
4037 __ LoadConst32(rhs_reg, rhs_imm);
4038 }
4039 __ Slt(dst, lhs, rhs_reg);
4040 }
4041 return (cond == kCondGE);
4042
4043 case kCondLE:
4044 case kCondGT:
4045 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4046 // Simulate lhs <= rhs via lhs < rhs + 1.
4047 __ Slti(dst, lhs, rhs_imm + 1);
4048 return (cond == kCondGT);
4049 } else {
4050 if (use_imm) {
4051 rhs_reg = TMP;
4052 __ LoadConst32(rhs_reg, rhs_imm);
4053 }
4054 __ Slt(dst, rhs_reg, lhs);
4055 return (cond == kCondLE);
4056 }
4057
4058 case kCondB:
4059 case kCondAE:
4060 if (use_imm && IsInt<16>(rhs_imm)) {
4061 // Sltiu sign-extends its 16-bit immediate operand before
4062 // the comparison and thus lets us compare directly with
4063 // unsigned values in the ranges [0, 0x7fff] and
4064 // [0xffff8000, 0xffffffff].
4065 __ Sltiu(dst, lhs, rhs_imm);
4066 } else {
4067 if (use_imm) {
4068 rhs_reg = TMP;
4069 __ LoadConst32(rhs_reg, rhs_imm);
4070 }
4071 __ Sltu(dst, lhs, rhs_reg);
4072 }
4073 return (cond == kCondAE);
4074
4075 case kCondBE:
4076 case kCondA:
4077 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4078 // Simulate lhs <= rhs via lhs < rhs + 1.
4079 // Note that this only works if rhs + 1 does not overflow
4080 // to 0, hence the check above.
4081 // Sltiu sign-extends its 16-bit immediate operand before
4082 // the comparison and thus lets us compare directly with
4083 // unsigned values in the ranges [0, 0x7fff] and
4084 // [0xffff8000, 0xffffffff].
4085 __ Sltiu(dst, lhs, rhs_imm + 1);
4086 return (cond == kCondA);
4087 } else {
4088 if (use_imm) {
4089 rhs_reg = TMP;
4090 __ LoadConst32(rhs_reg, rhs_imm);
4091 }
4092 __ Sltu(dst, rhs_reg, lhs);
4093 return (cond == kCondBE);
4094 }
4095 }
4096}
4097
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004098void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4099 LocationSummary* locations,
4100 MipsLabel* label) {
4101 Register lhs = locations->InAt(0).AsRegister<Register>();
4102 Location rhs_location = locations->InAt(1);
4103 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004104 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004105 bool use_imm = rhs_location.IsConstant();
4106 if (use_imm) {
4107 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4108 } else {
4109 rhs_reg = rhs_location.AsRegister<Register>();
4110 }
4111
4112 if (use_imm && rhs_imm == 0) {
4113 switch (cond) {
4114 case kCondEQ:
4115 case kCondBE: // <= 0 if zero
4116 __ Beqz(lhs, label);
4117 break;
4118 case kCondNE:
4119 case kCondA: // > 0 if non-zero
4120 __ Bnez(lhs, label);
4121 break;
4122 case kCondLT:
4123 __ Bltz(lhs, label);
4124 break;
4125 case kCondGE:
4126 __ Bgez(lhs, label);
4127 break;
4128 case kCondLE:
4129 __ Blez(lhs, label);
4130 break;
4131 case kCondGT:
4132 __ Bgtz(lhs, label);
4133 break;
4134 case kCondB: // always false
4135 break;
4136 case kCondAE: // always true
4137 __ B(label);
4138 break;
4139 }
4140 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004141 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4142 if (isR6 || !use_imm) {
4143 if (use_imm) {
4144 rhs_reg = TMP;
4145 __ LoadConst32(rhs_reg, rhs_imm);
4146 }
4147 switch (cond) {
4148 case kCondEQ:
4149 __ Beq(lhs, rhs_reg, label);
4150 break;
4151 case kCondNE:
4152 __ Bne(lhs, rhs_reg, label);
4153 break;
4154 case kCondLT:
4155 __ Blt(lhs, rhs_reg, label);
4156 break;
4157 case kCondGE:
4158 __ Bge(lhs, rhs_reg, label);
4159 break;
4160 case kCondLE:
4161 __ Bge(rhs_reg, lhs, label);
4162 break;
4163 case kCondGT:
4164 __ Blt(rhs_reg, lhs, label);
4165 break;
4166 case kCondB:
4167 __ Bltu(lhs, rhs_reg, label);
4168 break;
4169 case kCondAE:
4170 __ Bgeu(lhs, rhs_reg, label);
4171 break;
4172 case kCondBE:
4173 __ Bgeu(rhs_reg, lhs, label);
4174 break;
4175 case kCondA:
4176 __ Bltu(rhs_reg, lhs, label);
4177 break;
4178 }
4179 } else {
4180 // Special cases for more efficient comparison with constants on R2.
4181 switch (cond) {
4182 case kCondEQ:
4183 __ LoadConst32(TMP, rhs_imm);
4184 __ Beq(lhs, TMP, label);
4185 break;
4186 case kCondNE:
4187 __ LoadConst32(TMP, rhs_imm);
4188 __ Bne(lhs, TMP, label);
4189 break;
4190 case kCondLT:
4191 if (IsInt<16>(rhs_imm)) {
4192 __ Slti(TMP, lhs, rhs_imm);
4193 __ Bnez(TMP, label);
4194 } else {
4195 __ LoadConst32(TMP, rhs_imm);
4196 __ Blt(lhs, TMP, label);
4197 }
4198 break;
4199 case kCondGE:
4200 if (IsInt<16>(rhs_imm)) {
4201 __ Slti(TMP, lhs, rhs_imm);
4202 __ Beqz(TMP, label);
4203 } else {
4204 __ LoadConst32(TMP, rhs_imm);
4205 __ Bge(lhs, TMP, label);
4206 }
4207 break;
4208 case kCondLE:
4209 if (IsInt<16>(rhs_imm + 1)) {
4210 // Simulate lhs <= rhs via lhs < rhs + 1.
4211 __ Slti(TMP, lhs, rhs_imm + 1);
4212 __ Bnez(TMP, label);
4213 } else {
4214 __ LoadConst32(TMP, rhs_imm);
4215 __ Bge(TMP, lhs, label);
4216 }
4217 break;
4218 case kCondGT:
4219 if (IsInt<16>(rhs_imm + 1)) {
4220 // Simulate lhs > rhs via !(lhs < rhs + 1).
4221 __ Slti(TMP, lhs, rhs_imm + 1);
4222 __ Beqz(TMP, label);
4223 } else {
4224 __ LoadConst32(TMP, rhs_imm);
4225 __ Blt(TMP, lhs, label);
4226 }
4227 break;
4228 case kCondB:
4229 if (IsInt<16>(rhs_imm)) {
4230 __ Sltiu(TMP, lhs, rhs_imm);
4231 __ Bnez(TMP, label);
4232 } else {
4233 __ LoadConst32(TMP, rhs_imm);
4234 __ Bltu(lhs, TMP, label);
4235 }
4236 break;
4237 case kCondAE:
4238 if (IsInt<16>(rhs_imm)) {
4239 __ Sltiu(TMP, lhs, rhs_imm);
4240 __ Beqz(TMP, label);
4241 } else {
4242 __ LoadConst32(TMP, rhs_imm);
4243 __ Bgeu(lhs, TMP, label);
4244 }
4245 break;
4246 case kCondBE:
4247 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4248 // Simulate lhs <= rhs via lhs < rhs + 1.
4249 // Note that this only works if rhs + 1 does not overflow
4250 // to 0, hence the check above.
4251 __ Sltiu(TMP, lhs, rhs_imm + 1);
4252 __ Bnez(TMP, label);
4253 } else {
4254 __ LoadConst32(TMP, rhs_imm);
4255 __ Bgeu(TMP, lhs, label);
4256 }
4257 break;
4258 case kCondA:
4259 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4260 // Simulate lhs > rhs via !(lhs < rhs + 1).
4261 // Note that this only works if rhs + 1 does not overflow
4262 // to 0, hence the check above.
4263 __ Sltiu(TMP, lhs, rhs_imm + 1);
4264 __ Beqz(TMP, label);
4265 } else {
4266 __ LoadConst32(TMP, rhs_imm);
4267 __ Bltu(TMP, lhs, label);
4268 }
4269 break;
4270 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004271 }
4272 }
4273}
4274
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004275void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4276 LocationSummary* locations) {
4277 Register dst = locations->Out().AsRegister<Register>();
4278 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4279 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4280 Location rhs_location = locations->InAt(1);
4281 Register rhs_high = ZERO;
4282 Register rhs_low = ZERO;
4283 int64_t imm = 0;
4284 uint32_t imm_high = 0;
4285 uint32_t imm_low = 0;
4286 bool use_imm = rhs_location.IsConstant();
4287 if (use_imm) {
4288 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4289 imm_high = High32Bits(imm);
4290 imm_low = Low32Bits(imm);
4291 } else {
4292 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4293 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4294 }
4295 if (use_imm && imm == 0) {
4296 switch (cond) {
4297 case kCondEQ:
4298 case kCondBE: // <= 0 if zero
4299 __ Or(dst, lhs_high, lhs_low);
4300 __ Sltiu(dst, dst, 1);
4301 break;
4302 case kCondNE:
4303 case kCondA: // > 0 if non-zero
4304 __ Or(dst, lhs_high, lhs_low);
4305 __ Sltu(dst, ZERO, dst);
4306 break;
4307 case kCondLT:
4308 __ Slt(dst, lhs_high, ZERO);
4309 break;
4310 case kCondGE:
4311 __ Slt(dst, lhs_high, ZERO);
4312 __ Xori(dst, dst, 1);
4313 break;
4314 case kCondLE:
4315 __ Or(TMP, lhs_high, lhs_low);
4316 __ Sra(AT, lhs_high, 31);
4317 __ Sltu(dst, AT, TMP);
4318 __ Xori(dst, dst, 1);
4319 break;
4320 case kCondGT:
4321 __ Or(TMP, lhs_high, lhs_low);
4322 __ Sra(AT, lhs_high, 31);
4323 __ Sltu(dst, AT, TMP);
4324 break;
4325 case kCondB: // always false
4326 __ Andi(dst, dst, 0);
4327 break;
4328 case kCondAE: // always true
4329 __ Ori(dst, ZERO, 1);
4330 break;
4331 }
4332 } else if (use_imm) {
4333 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4334 switch (cond) {
4335 case kCondEQ:
4336 __ LoadConst32(TMP, imm_high);
4337 __ Xor(TMP, TMP, lhs_high);
4338 __ LoadConst32(AT, imm_low);
4339 __ Xor(AT, AT, lhs_low);
4340 __ Or(dst, TMP, AT);
4341 __ Sltiu(dst, dst, 1);
4342 break;
4343 case kCondNE:
4344 __ LoadConst32(TMP, imm_high);
4345 __ Xor(TMP, TMP, lhs_high);
4346 __ LoadConst32(AT, imm_low);
4347 __ Xor(AT, AT, lhs_low);
4348 __ Or(dst, TMP, AT);
4349 __ Sltu(dst, ZERO, dst);
4350 break;
4351 case kCondLT:
4352 case kCondGE:
4353 if (dst == lhs_low) {
4354 __ LoadConst32(TMP, imm_low);
4355 __ Sltu(dst, lhs_low, TMP);
4356 }
4357 __ LoadConst32(TMP, imm_high);
4358 __ Slt(AT, lhs_high, TMP);
4359 __ Slt(TMP, TMP, lhs_high);
4360 if (dst != lhs_low) {
4361 __ LoadConst32(dst, imm_low);
4362 __ Sltu(dst, lhs_low, dst);
4363 }
4364 __ Slt(dst, TMP, dst);
4365 __ Or(dst, dst, AT);
4366 if (cond == kCondGE) {
4367 __ Xori(dst, dst, 1);
4368 }
4369 break;
4370 case kCondGT:
4371 case kCondLE:
4372 if (dst == lhs_low) {
4373 __ LoadConst32(TMP, imm_low);
4374 __ Sltu(dst, TMP, lhs_low);
4375 }
4376 __ LoadConst32(TMP, imm_high);
4377 __ Slt(AT, TMP, lhs_high);
4378 __ Slt(TMP, lhs_high, TMP);
4379 if (dst != lhs_low) {
4380 __ LoadConst32(dst, imm_low);
4381 __ Sltu(dst, dst, lhs_low);
4382 }
4383 __ Slt(dst, TMP, dst);
4384 __ Or(dst, dst, AT);
4385 if (cond == kCondLE) {
4386 __ Xori(dst, dst, 1);
4387 }
4388 break;
4389 case kCondB:
4390 case kCondAE:
4391 if (dst == lhs_low) {
4392 __ LoadConst32(TMP, imm_low);
4393 __ Sltu(dst, lhs_low, TMP);
4394 }
4395 __ LoadConst32(TMP, imm_high);
4396 __ Sltu(AT, lhs_high, TMP);
4397 __ Sltu(TMP, TMP, lhs_high);
4398 if (dst != lhs_low) {
4399 __ LoadConst32(dst, imm_low);
4400 __ Sltu(dst, lhs_low, dst);
4401 }
4402 __ Slt(dst, TMP, dst);
4403 __ Or(dst, dst, AT);
4404 if (cond == kCondAE) {
4405 __ Xori(dst, dst, 1);
4406 }
4407 break;
4408 case kCondA:
4409 case kCondBE:
4410 if (dst == lhs_low) {
4411 __ LoadConst32(TMP, imm_low);
4412 __ Sltu(dst, TMP, lhs_low);
4413 }
4414 __ LoadConst32(TMP, imm_high);
4415 __ Sltu(AT, TMP, lhs_high);
4416 __ Sltu(TMP, lhs_high, TMP);
4417 if (dst != lhs_low) {
4418 __ LoadConst32(dst, imm_low);
4419 __ Sltu(dst, dst, lhs_low);
4420 }
4421 __ Slt(dst, TMP, dst);
4422 __ Or(dst, dst, AT);
4423 if (cond == kCondBE) {
4424 __ Xori(dst, dst, 1);
4425 }
4426 break;
4427 }
4428 } else {
4429 switch (cond) {
4430 case kCondEQ:
4431 __ Xor(TMP, lhs_high, rhs_high);
4432 __ Xor(AT, lhs_low, rhs_low);
4433 __ Or(dst, TMP, AT);
4434 __ Sltiu(dst, dst, 1);
4435 break;
4436 case kCondNE:
4437 __ Xor(TMP, lhs_high, rhs_high);
4438 __ Xor(AT, lhs_low, rhs_low);
4439 __ Or(dst, TMP, AT);
4440 __ Sltu(dst, ZERO, dst);
4441 break;
4442 case kCondLT:
4443 case kCondGE:
4444 __ Slt(TMP, rhs_high, lhs_high);
4445 __ Sltu(AT, lhs_low, rhs_low);
4446 __ Slt(TMP, TMP, AT);
4447 __ Slt(AT, lhs_high, rhs_high);
4448 __ Or(dst, AT, TMP);
4449 if (cond == kCondGE) {
4450 __ Xori(dst, dst, 1);
4451 }
4452 break;
4453 case kCondGT:
4454 case kCondLE:
4455 __ Slt(TMP, lhs_high, rhs_high);
4456 __ Sltu(AT, rhs_low, lhs_low);
4457 __ Slt(TMP, TMP, AT);
4458 __ Slt(AT, rhs_high, lhs_high);
4459 __ Or(dst, AT, TMP);
4460 if (cond == kCondLE) {
4461 __ Xori(dst, dst, 1);
4462 }
4463 break;
4464 case kCondB:
4465 case kCondAE:
4466 __ Sltu(TMP, rhs_high, lhs_high);
4467 __ Sltu(AT, lhs_low, rhs_low);
4468 __ Slt(TMP, TMP, AT);
4469 __ Sltu(AT, lhs_high, rhs_high);
4470 __ Or(dst, AT, TMP);
4471 if (cond == kCondAE) {
4472 __ Xori(dst, dst, 1);
4473 }
4474 break;
4475 case kCondA:
4476 case kCondBE:
4477 __ Sltu(TMP, lhs_high, rhs_high);
4478 __ Sltu(AT, rhs_low, lhs_low);
4479 __ Slt(TMP, TMP, AT);
4480 __ Sltu(AT, rhs_high, lhs_high);
4481 __ Or(dst, AT, TMP);
4482 if (cond == kCondBE) {
4483 __ Xori(dst, dst, 1);
4484 }
4485 break;
4486 }
4487 }
4488}
4489
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004490void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4491 LocationSummary* locations,
4492 MipsLabel* label) {
4493 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4494 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4495 Location rhs_location = locations->InAt(1);
4496 Register rhs_high = ZERO;
4497 Register rhs_low = ZERO;
4498 int64_t imm = 0;
4499 uint32_t imm_high = 0;
4500 uint32_t imm_low = 0;
4501 bool use_imm = rhs_location.IsConstant();
4502 if (use_imm) {
4503 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4504 imm_high = High32Bits(imm);
4505 imm_low = Low32Bits(imm);
4506 } else {
4507 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4508 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4509 }
4510
4511 if (use_imm && imm == 0) {
4512 switch (cond) {
4513 case kCondEQ:
4514 case kCondBE: // <= 0 if zero
4515 __ Or(TMP, lhs_high, lhs_low);
4516 __ Beqz(TMP, label);
4517 break;
4518 case kCondNE:
4519 case kCondA: // > 0 if non-zero
4520 __ Or(TMP, lhs_high, lhs_low);
4521 __ Bnez(TMP, label);
4522 break;
4523 case kCondLT:
4524 __ Bltz(lhs_high, label);
4525 break;
4526 case kCondGE:
4527 __ Bgez(lhs_high, label);
4528 break;
4529 case kCondLE:
4530 __ Or(TMP, lhs_high, lhs_low);
4531 __ Sra(AT, lhs_high, 31);
4532 __ Bgeu(AT, TMP, label);
4533 break;
4534 case kCondGT:
4535 __ Or(TMP, lhs_high, lhs_low);
4536 __ Sra(AT, lhs_high, 31);
4537 __ Bltu(AT, TMP, label);
4538 break;
4539 case kCondB: // always false
4540 break;
4541 case kCondAE: // always true
4542 __ B(label);
4543 break;
4544 }
4545 } else if (use_imm) {
4546 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4547 switch (cond) {
4548 case kCondEQ:
4549 __ LoadConst32(TMP, imm_high);
4550 __ Xor(TMP, TMP, lhs_high);
4551 __ LoadConst32(AT, imm_low);
4552 __ Xor(AT, AT, lhs_low);
4553 __ Or(TMP, TMP, AT);
4554 __ Beqz(TMP, label);
4555 break;
4556 case kCondNE:
4557 __ LoadConst32(TMP, imm_high);
4558 __ Xor(TMP, TMP, lhs_high);
4559 __ LoadConst32(AT, imm_low);
4560 __ Xor(AT, AT, lhs_low);
4561 __ Or(TMP, TMP, AT);
4562 __ Bnez(TMP, label);
4563 break;
4564 case kCondLT:
4565 __ LoadConst32(TMP, imm_high);
4566 __ Blt(lhs_high, TMP, label);
4567 __ Slt(TMP, TMP, lhs_high);
4568 __ LoadConst32(AT, imm_low);
4569 __ Sltu(AT, lhs_low, AT);
4570 __ Blt(TMP, AT, label);
4571 break;
4572 case kCondGE:
4573 __ LoadConst32(TMP, imm_high);
4574 __ Blt(TMP, lhs_high, label);
4575 __ Slt(TMP, lhs_high, TMP);
4576 __ LoadConst32(AT, imm_low);
4577 __ Sltu(AT, lhs_low, AT);
4578 __ Or(TMP, TMP, AT);
4579 __ Beqz(TMP, label);
4580 break;
4581 case kCondLE:
4582 __ LoadConst32(TMP, imm_high);
4583 __ Blt(lhs_high, TMP, label);
4584 __ Slt(TMP, TMP, lhs_high);
4585 __ LoadConst32(AT, imm_low);
4586 __ Sltu(AT, AT, lhs_low);
4587 __ Or(TMP, TMP, AT);
4588 __ Beqz(TMP, label);
4589 break;
4590 case kCondGT:
4591 __ LoadConst32(TMP, imm_high);
4592 __ Blt(TMP, lhs_high, label);
4593 __ Slt(TMP, lhs_high, TMP);
4594 __ LoadConst32(AT, imm_low);
4595 __ Sltu(AT, AT, lhs_low);
4596 __ Blt(TMP, AT, label);
4597 break;
4598 case kCondB:
4599 __ LoadConst32(TMP, imm_high);
4600 __ Bltu(lhs_high, TMP, label);
4601 __ Sltu(TMP, TMP, lhs_high);
4602 __ LoadConst32(AT, imm_low);
4603 __ Sltu(AT, lhs_low, AT);
4604 __ Blt(TMP, AT, label);
4605 break;
4606 case kCondAE:
4607 __ LoadConst32(TMP, imm_high);
4608 __ Bltu(TMP, lhs_high, label);
4609 __ Sltu(TMP, lhs_high, TMP);
4610 __ LoadConst32(AT, imm_low);
4611 __ Sltu(AT, lhs_low, AT);
4612 __ Or(TMP, TMP, AT);
4613 __ Beqz(TMP, label);
4614 break;
4615 case kCondBE:
4616 __ LoadConst32(TMP, imm_high);
4617 __ Bltu(lhs_high, TMP, label);
4618 __ Sltu(TMP, TMP, lhs_high);
4619 __ LoadConst32(AT, imm_low);
4620 __ Sltu(AT, AT, lhs_low);
4621 __ Or(TMP, TMP, AT);
4622 __ Beqz(TMP, label);
4623 break;
4624 case kCondA:
4625 __ LoadConst32(TMP, imm_high);
4626 __ Bltu(TMP, lhs_high, label);
4627 __ Sltu(TMP, lhs_high, TMP);
4628 __ LoadConst32(AT, imm_low);
4629 __ Sltu(AT, AT, lhs_low);
4630 __ Blt(TMP, AT, label);
4631 break;
4632 }
4633 } else {
4634 switch (cond) {
4635 case kCondEQ:
4636 __ Xor(TMP, lhs_high, rhs_high);
4637 __ Xor(AT, lhs_low, rhs_low);
4638 __ Or(TMP, TMP, AT);
4639 __ Beqz(TMP, label);
4640 break;
4641 case kCondNE:
4642 __ Xor(TMP, lhs_high, rhs_high);
4643 __ Xor(AT, lhs_low, rhs_low);
4644 __ Or(TMP, TMP, AT);
4645 __ Bnez(TMP, label);
4646 break;
4647 case kCondLT:
4648 __ Blt(lhs_high, rhs_high, label);
4649 __ Slt(TMP, rhs_high, lhs_high);
4650 __ Sltu(AT, lhs_low, rhs_low);
4651 __ Blt(TMP, AT, label);
4652 break;
4653 case kCondGE:
4654 __ Blt(rhs_high, lhs_high, label);
4655 __ Slt(TMP, lhs_high, rhs_high);
4656 __ Sltu(AT, lhs_low, rhs_low);
4657 __ Or(TMP, TMP, AT);
4658 __ Beqz(TMP, label);
4659 break;
4660 case kCondLE:
4661 __ Blt(lhs_high, rhs_high, label);
4662 __ Slt(TMP, rhs_high, lhs_high);
4663 __ Sltu(AT, rhs_low, lhs_low);
4664 __ Or(TMP, TMP, AT);
4665 __ Beqz(TMP, label);
4666 break;
4667 case kCondGT:
4668 __ Blt(rhs_high, lhs_high, label);
4669 __ Slt(TMP, lhs_high, rhs_high);
4670 __ Sltu(AT, rhs_low, lhs_low);
4671 __ Blt(TMP, AT, label);
4672 break;
4673 case kCondB:
4674 __ Bltu(lhs_high, rhs_high, label);
4675 __ Sltu(TMP, rhs_high, lhs_high);
4676 __ Sltu(AT, lhs_low, rhs_low);
4677 __ Blt(TMP, AT, label);
4678 break;
4679 case kCondAE:
4680 __ Bltu(rhs_high, lhs_high, label);
4681 __ Sltu(TMP, lhs_high, rhs_high);
4682 __ Sltu(AT, lhs_low, rhs_low);
4683 __ Or(TMP, TMP, AT);
4684 __ Beqz(TMP, label);
4685 break;
4686 case kCondBE:
4687 __ Bltu(lhs_high, rhs_high, label);
4688 __ Sltu(TMP, rhs_high, lhs_high);
4689 __ Sltu(AT, rhs_low, lhs_low);
4690 __ Or(TMP, TMP, AT);
4691 __ Beqz(TMP, label);
4692 break;
4693 case kCondA:
4694 __ Bltu(rhs_high, lhs_high, label);
4695 __ Sltu(TMP, lhs_high, rhs_high);
4696 __ Sltu(AT, rhs_low, lhs_low);
4697 __ Blt(TMP, AT, label);
4698 break;
4699 }
4700 }
4701}
4702
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004703void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4704 bool gt_bias,
4705 Primitive::Type type,
4706 LocationSummary* locations) {
4707 Register dst = locations->Out().AsRegister<Register>();
4708 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4709 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4710 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4711 if (type == Primitive::kPrimFloat) {
4712 if (isR6) {
4713 switch (cond) {
4714 case kCondEQ:
4715 __ CmpEqS(FTMP, lhs, rhs);
4716 __ Mfc1(dst, FTMP);
4717 __ Andi(dst, dst, 1);
4718 break;
4719 case kCondNE:
4720 __ CmpEqS(FTMP, lhs, rhs);
4721 __ Mfc1(dst, FTMP);
4722 __ Addiu(dst, dst, 1);
4723 break;
4724 case kCondLT:
4725 if (gt_bias) {
4726 __ CmpLtS(FTMP, lhs, rhs);
4727 } else {
4728 __ CmpUltS(FTMP, lhs, rhs);
4729 }
4730 __ Mfc1(dst, FTMP);
4731 __ Andi(dst, dst, 1);
4732 break;
4733 case kCondLE:
4734 if (gt_bias) {
4735 __ CmpLeS(FTMP, lhs, rhs);
4736 } else {
4737 __ CmpUleS(FTMP, lhs, rhs);
4738 }
4739 __ Mfc1(dst, FTMP);
4740 __ Andi(dst, dst, 1);
4741 break;
4742 case kCondGT:
4743 if (gt_bias) {
4744 __ CmpUltS(FTMP, rhs, lhs);
4745 } else {
4746 __ CmpLtS(FTMP, rhs, lhs);
4747 }
4748 __ Mfc1(dst, FTMP);
4749 __ Andi(dst, dst, 1);
4750 break;
4751 case kCondGE:
4752 if (gt_bias) {
4753 __ CmpUleS(FTMP, rhs, lhs);
4754 } else {
4755 __ CmpLeS(FTMP, rhs, lhs);
4756 }
4757 __ Mfc1(dst, FTMP);
4758 __ Andi(dst, dst, 1);
4759 break;
4760 default:
4761 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4762 UNREACHABLE();
4763 }
4764 } else {
4765 switch (cond) {
4766 case kCondEQ:
4767 __ CeqS(0, lhs, rhs);
4768 __ LoadConst32(dst, 1);
4769 __ Movf(dst, ZERO, 0);
4770 break;
4771 case kCondNE:
4772 __ CeqS(0, lhs, rhs);
4773 __ LoadConst32(dst, 1);
4774 __ Movt(dst, ZERO, 0);
4775 break;
4776 case kCondLT:
4777 if (gt_bias) {
4778 __ ColtS(0, lhs, rhs);
4779 } else {
4780 __ CultS(0, lhs, rhs);
4781 }
4782 __ LoadConst32(dst, 1);
4783 __ Movf(dst, ZERO, 0);
4784 break;
4785 case kCondLE:
4786 if (gt_bias) {
4787 __ ColeS(0, lhs, rhs);
4788 } else {
4789 __ CuleS(0, lhs, rhs);
4790 }
4791 __ LoadConst32(dst, 1);
4792 __ Movf(dst, ZERO, 0);
4793 break;
4794 case kCondGT:
4795 if (gt_bias) {
4796 __ CultS(0, rhs, lhs);
4797 } else {
4798 __ ColtS(0, rhs, lhs);
4799 }
4800 __ LoadConst32(dst, 1);
4801 __ Movf(dst, ZERO, 0);
4802 break;
4803 case kCondGE:
4804 if (gt_bias) {
4805 __ CuleS(0, rhs, lhs);
4806 } else {
4807 __ ColeS(0, rhs, lhs);
4808 }
4809 __ LoadConst32(dst, 1);
4810 __ Movf(dst, ZERO, 0);
4811 break;
4812 default:
4813 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4814 UNREACHABLE();
4815 }
4816 }
4817 } else {
4818 DCHECK_EQ(type, Primitive::kPrimDouble);
4819 if (isR6) {
4820 switch (cond) {
4821 case kCondEQ:
4822 __ CmpEqD(FTMP, lhs, rhs);
4823 __ Mfc1(dst, FTMP);
4824 __ Andi(dst, dst, 1);
4825 break;
4826 case kCondNE:
4827 __ CmpEqD(FTMP, lhs, rhs);
4828 __ Mfc1(dst, FTMP);
4829 __ Addiu(dst, dst, 1);
4830 break;
4831 case kCondLT:
4832 if (gt_bias) {
4833 __ CmpLtD(FTMP, lhs, rhs);
4834 } else {
4835 __ CmpUltD(FTMP, lhs, rhs);
4836 }
4837 __ Mfc1(dst, FTMP);
4838 __ Andi(dst, dst, 1);
4839 break;
4840 case kCondLE:
4841 if (gt_bias) {
4842 __ CmpLeD(FTMP, lhs, rhs);
4843 } else {
4844 __ CmpUleD(FTMP, lhs, rhs);
4845 }
4846 __ Mfc1(dst, FTMP);
4847 __ Andi(dst, dst, 1);
4848 break;
4849 case kCondGT:
4850 if (gt_bias) {
4851 __ CmpUltD(FTMP, rhs, lhs);
4852 } else {
4853 __ CmpLtD(FTMP, rhs, lhs);
4854 }
4855 __ Mfc1(dst, FTMP);
4856 __ Andi(dst, dst, 1);
4857 break;
4858 case kCondGE:
4859 if (gt_bias) {
4860 __ CmpUleD(FTMP, rhs, lhs);
4861 } else {
4862 __ CmpLeD(FTMP, rhs, lhs);
4863 }
4864 __ Mfc1(dst, FTMP);
4865 __ Andi(dst, dst, 1);
4866 break;
4867 default:
4868 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4869 UNREACHABLE();
4870 }
4871 } else {
4872 switch (cond) {
4873 case kCondEQ:
4874 __ CeqD(0, lhs, rhs);
4875 __ LoadConst32(dst, 1);
4876 __ Movf(dst, ZERO, 0);
4877 break;
4878 case kCondNE:
4879 __ CeqD(0, lhs, rhs);
4880 __ LoadConst32(dst, 1);
4881 __ Movt(dst, ZERO, 0);
4882 break;
4883 case kCondLT:
4884 if (gt_bias) {
4885 __ ColtD(0, lhs, rhs);
4886 } else {
4887 __ CultD(0, lhs, rhs);
4888 }
4889 __ LoadConst32(dst, 1);
4890 __ Movf(dst, ZERO, 0);
4891 break;
4892 case kCondLE:
4893 if (gt_bias) {
4894 __ ColeD(0, lhs, rhs);
4895 } else {
4896 __ CuleD(0, lhs, rhs);
4897 }
4898 __ LoadConst32(dst, 1);
4899 __ Movf(dst, ZERO, 0);
4900 break;
4901 case kCondGT:
4902 if (gt_bias) {
4903 __ CultD(0, rhs, lhs);
4904 } else {
4905 __ ColtD(0, rhs, lhs);
4906 }
4907 __ LoadConst32(dst, 1);
4908 __ Movf(dst, ZERO, 0);
4909 break;
4910 case kCondGE:
4911 if (gt_bias) {
4912 __ CuleD(0, rhs, lhs);
4913 } else {
4914 __ ColeD(0, rhs, lhs);
4915 }
4916 __ LoadConst32(dst, 1);
4917 __ Movf(dst, ZERO, 0);
4918 break;
4919 default:
4920 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4921 UNREACHABLE();
4922 }
4923 }
4924 }
4925}
4926
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004927bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4928 bool gt_bias,
4929 Primitive::Type type,
4930 LocationSummary* input_locations,
4931 int cc) {
4932 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4933 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4934 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4935 if (type == Primitive::kPrimFloat) {
4936 switch (cond) {
4937 case kCondEQ:
4938 __ CeqS(cc, lhs, rhs);
4939 return false;
4940 case kCondNE:
4941 __ CeqS(cc, lhs, rhs);
4942 return true;
4943 case kCondLT:
4944 if (gt_bias) {
4945 __ ColtS(cc, lhs, rhs);
4946 } else {
4947 __ CultS(cc, lhs, rhs);
4948 }
4949 return false;
4950 case kCondLE:
4951 if (gt_bias) {
4952 __ ColeS(cc, lhs, rhs);
4953 } else {
4954 __ CuleS(cc, lhs, rhs);
4955 }
4956 return false;
4957 case kCondGT:
4958 if (gt_bias) {
4959 __ CultS(cc, rhs, lhs);
4960 } else {
4961 __ ColtS(cc, rhs, lhs);
4962 }
4963 return false;
4964 case kCondGE:
4965 if (gt_bias) {
4966 __ CuleS(cc, rhs, lhs);
4967 } else {
4968 __ ColeS(cc, rhs, lhs);
4969 }
4970 return false;
4971 default:
4972 LOG(FATAL) << "Unexpected non-floating-point condition";
4973 UNREACHABLE();
4974 }
4975 } else {
4976 DCHECK_EQ(type, Primitive::kPrimDouble);
4977 switch (cond) {
4978 case kCondEQ:
4979 __ CeqD(cc, lhs, rhs);
4980 return false;
4981 case kCondNE:
4982 __ CeqD(cc, lhs, rhs);
4983 return true;
4984 case kCondLT:
4985 if (gt_bias) {
4986 __ ColtD(cc, lhs, rhs);
4987 } else {
4988 __ CultD(cc, lhs, rhs);
4989 }
4990 return false;
4991 case kCondLE:
4992 if (gt_bias) {
4993 __ ColeD(cc, lhs, rhs);
4994 } else {
4995 __ CuleD(cc, lhs, rhs);
4996 }
4997 return false;
4998 case kCondGT:
4999 if (gt_bias) {
5000 __ CultD(cc, rhs, lhs);
5001 } else {
5002 __ ColtD(cc, rhs, lhs);
5003 }
5004 return false;
5005 case kCondGE:
5006 if (gt_bias) {
5007 __ CuleD(cc, rhs, lhs);
5008 } else {
5009 __ ColeD(cc, rhs, lhs);
5010 }
5011 return false;
5012 default:
5013 LOG(FATAL) << "Unexpected non-floating-point condition";
5014 UNREACHABLE();
5015 }
5016 }
5017}
5018
5019bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5020 bool gt_bias,
5021 Primitive::Type type,
5022 LocationSummary* input_locations,
5023 FRegister dst) {
5024 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5025 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5026 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
5027 if (type == Primitive::kPrimFloat) {
5028 switch (cond) {
5029 case kCondEQ:
5030 __ CmpEqS(dst, lhs, rhs);
5031 return false;
5032 case kCondNE:
5033 __ CmpEqS(dst, lhs, rhs);
5034 return true;
5035 case kCondLT:
5036 if (gt_bias) {
5037 __ CmpLtS(dst, lhs, rhs);
5038 } else {
5039 __ CmpUltS(dst, lhs, rhs);
5040 }
5041 return false;
5042 case kCondLE:
5043 if (gt_bias) {
5044 __ CmpLeS(dst, lhs, rhs);
5045 } else {
5046 __ CmpUleS(dst, lhs, rhs);
5047 }
5048 return false;
5049 case kCondGT:
5050 if (gt_bias) {
5051 __ CmpUltS(dst, rhs, lhs);
5052 } else {
5053 __ CmpLtS(dst, rhs, lhs);
5054 }
5055 return false;
5056 case kCondGE:
5057 if (gt_bias) {
5058 __ CmpUleS(dst, rhs, lhs);
5059 } else {
5060 __ CmpLeS(dst, rhs, lhs);
5061 }
5062 return false;
5063 default:
5064 LOG(FATAL) << "Unexpected non-floating-point condition";
5065 UNREACHABLE();
5066 }
5067 } else {
5068 DCHECK_EQ(type, Primitive::kPrimDouble);
5069 switch (cond) {
5070 case kCondEQ:
5071 __ CmpEqD(dst, lhs, rhs);
5072 return false;
5073 case kCondNE:
5074 __ CmpEqD(dst, lhs, rhs);
5075 return true;
5076 case kCondLT:
5077 if (gt_bias) {
5078 __ CmpLtD(dst, lhs, rhs);
5079 } else {
5080 __ CmpUltD(dst, lhs, rhs);
5081 }
5082 return false;
5083 case kCondLE:
5084 if (gt_bias) {
5085 __ CmpLeD(dst, lhs, rhs);
5086 } else {
5087 __ CmpUleD(dst, lhs, rhs);
5088 }
5089 return false;
5090 case kCondGT:
5091 if (gt_bias) {
5092 __ CmpUltD(dst, rhs, lhs);
5093 } else {
5094 __ CmpLtD(dst, rhs, lhs);
5095 }
5096 return false;
5097 case kCondGE:
5098 if (gt_bias) {
5099 __ CmpUleD(dst, rhs, lhs);
5100 } else {
5101 __ CmpLeD(dst, rhs, lhs);
5102 }
5103 return false;
5104 default:
5105 LOG(FATAL) << "Unexpected non-floating-point condition";
5106 UNREACHABLE();
5107 }
5108 }
5109}
5110
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005111void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5112 bool gt_bias,
5113 Primitive::Type type,
5114 LocationSummary* locations,
5115 MipsLabel* label) {
5116 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5117 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5118 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5119 if (type == Primitive::kPrimFloat) {
5120 if (isR6) {
5121 switch (cond) {
5122 case kCondEQ:
5123 __ CmpEqS(FTMP, lhs, rhs);
5124 __ Bc1nez(FTMP, label);
5125 break;
5126 case kCondNE:
5127 __ CmpEqS(FTMP, lhs, rhs);
5128 __ Bc1eqz(FTMP, label);
5129 break;
5130 case kCondLT:
5131 if (gt_bias) {
5132 __ CmpLtS(FTMP, lhs, rhs);
5133 } else {
5134 __ CmpUltS(FTMP, lhs, rhs);
5135 }
5136 __ Bc1nez(FTMP, label);
5137 break;
5138 case kCondLE:
5139 if (gt_bias) {
5140 __ CmpLeS(FTMP, lhs, rhs);
5141 } else {
5142 __ CmpUleS(FTMP, lhs, rhs);
5143 }
5144 __ Bc1nez(FTMP, label);
5145 break;
5146 case kCondGT:
5147 if (gt_bias) {
5148 __ CmpUltS(FTMP, rhs, lhs);
5149 } else {
5150 __ CmpLtS(FTMP, rhs, lhs);
5151 }
5152 __ Bc1nez(FTMP, label);
5153 break;
5154 case kCondGE:
5155 if (gt_bias) {
5156 __ CmpUleS(FTMP, rhs, lhs);
5157 } else {
5158 __ CmpLeS(FTMP, rhs, lhs);
5159 }
5160 __ Bc1nez(FTMP, label);
5161 break;
5162 default:
5163 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005164 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005165 }
5166 } else {
5167 switch (cond) {
5168 case kCondEQ:
5169 __ CeqS(0, lhs, rhs);
5170 __ Bc1t(0, label);
5171 break;
5172 case kCondNE:
5173 __ CeqS(0, lhs, rhs);
5174 __ Bc1f(0, label);
5175 break;
5176 case kCondLT:
5177 if (gt_bias) {
5178 __ ColtS(0, lhs, rhs);
5179 } else {
5180 __ CultS(0, lhs, rhs);
5181 }
5182 __ Bc1t(0, label);
5183 break;
5184 case kCondLE:
5185 if (gt_bias) {
5186 __ ColeS(0, lhs, rhs);
5187 } else {
5188 __ CuleS(0, lhs, rhs);
5189 }
5190 __ Bc1t(0, label);
5191 break;
5192 case kCondGT:
5193 if (gt_bias) {
5194 __ CultS(0, rhs, lhs);
5195 } else {
5196 __ ColtS(0, rhs, lhs);
5197 }
5198 __ Bc1t(0, label);
5199 break;
5200 case kCondGE:
5201 if (gt_bias) {
5202 __ CuleS(0, rhs, lhs);
5203 } else {
5204 __ ColeS(0, rhs, lhs);
5205 }
5206 __ Bc1t(0, label);
5207 break;
5208 default:
5209 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005210 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005211 }
5212 }
5213 } else {
5214 DCHECK_EQ(type, Primitive::kPrimDouble);
5215 if (isR6) {
5216 switch (cond) {
5217 case kCondEQ:
5218 __ CmpEqD(FTMP, lhs, rhs);
5219 __ Bc1nez(FTMP, label);
5220 break;
5221 case kCondNE:
5222 __ CmpEqD(FTMP, lhs, rhs);
5223 __ Bc1eqz(FTMP, label);
5224 break;
5225 case kCondLT:
5226 if (gt_bias) {
5227 __ CmpLtD(FTMP, lhs, rhs);
5228 } else {
5229 __ CmpUltD(FTMP, lhs, rhs);
5230 }
5231 __ Bc1nez(FTMP, label);
5232 break;
5233 case kCondLE:
5234 if (gt_bias) {
5235 __ CmpLeD(FTMP, lhs, rhs);
5236 } else {
5237 __ CmpUleD(FTMP, lhs, rhs);
5238 }
5239 __ Bc1nez(FTMP, label);
5240 break;
5241 case kCondGT:
5242 if (gt_bias) {
5243 __ CmpUltD(FTMP, rhs, lhs);
5244 } else {
5245 __ CmpLtD(FTMP, rhs, lhs);
5246 }
5247 __ Bc1nez(FTMP, label);
5248 break;
5249 case kCondGE:
5250 if (gt_bias) {
5251 __ CmpUleD(FTMP, rhs, lhs);
5252 } else {
5253 __ CmpLeD(FTMP, rhs, lhs);
5254 }
5255 __ Bc1nez(FTMP, label);
5256 break;
5257 default:
5258 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005259 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005260 }
5261 } else {
5262 switch (cond) {
5263 case kCondEQ:
5264 __ CeqD(0, lhs, rhs);
5265 __ Bc1t(0, label);
5266 break;
5267 case kCondNE:
5268 __ CeqD(0, lhs, rhs);
5269 __ Bc1f(0, label);
5270 break;
5271 case kCondLT:
5272 if (gt_bias) {
5273 __ ColtD(0, lhs, rhs);
5274 } else {
5275 __ CultD(0, lhs, rhs);
5276 }
5277 __ Bc1t(0, label);
5278 break;
5279 case kCondLE:
5280 if (gt_bias) {
5281 __ ColeD(0, lhs, rhs);
5282 } else {
5283 __ CuleD(0, lhs, rhs);
5284 }
5285 __ Bc1t(0, label);
5286 break;
5287 case kCondGT:
5288 if (gt_bias) {
5289 __ CultD(0, rhs, lhs);
5290 } else {
5291 __ ColtD(0, rhs, lhs);
5292 }
5293 __ Bc1t(0, label);
5294 break;
5295 case kCondGE:
5296 if (gt_bias) {
5297 __ CuleD(0, rhs, lhs);
5298 } else {
5299 __ ColeD(0, rhs, lhs);
5300 }
5301 __ Bc1t(0, label);
5302 break;
5303 default:
5304 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005305 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005306 }
5307 }
5308 }
5309}
5310
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005311void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005312 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005313 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005314 MipsLabel* false_target) {
5315 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005316
David Brazdil0debae72015-11-12 18:37:00 +00005317 if (true_target == nullptr && false_target == nullptr) {
5318 // Nothing to do. The code always falls through.
5319 return;
5320 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005321 // Constant condition, statically compared against "true" (integer value 1).
5322 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005323 if (true_target != nullptr) {
5324 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005325 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005326 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005327 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005328 if (false_target != nullptr) {
5329 __ B(false_target);
5330 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005331 }
David Brazdil0debae72015-11-12 18:37:00 +00005332 return;
5333 }
5334
5335 // The following code generates these patterns:
5336 // (1) true_target == nullptr && false_target != nullptr
5337 // - opposite condition true => branch to false_target
5338 // (2) true_target != nullptr && false_target == nullptr
5339 // - condition true => branch to true_target
5340 // (3) true_target != nullptr && false_target != nullptr
5341 // - condition true => branch to true_target
5342 // - branch to false_target
5343 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005344 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005345 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005346 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005347 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005348 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5349 } else {
5350 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5351 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005352 } else {
5353 // The condition instruction has not been materialized, use its inputs as
5354 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005355 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005356 Primitive::Type type = condition->InputAt(0)->GetType();
5357 LocationSummary* locations = cond->GetLocations();
5358 IfCondition if_cond = condition->GetCondition();
5359 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005360
David Brazdil0debae72015-11-12 18:37:00 +00005361 if (true_target == nullptr) {
5362 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005363 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005364 }
5365
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005366 switch (type) {
5367 default:
5368 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5369 break;
5370 case Primitive::kPrimLong:
5371 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5372 break;
5373 case Primitive::kPrimFloat:
5374 case Primitive::kPrimDouble:
5375 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5376 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005377 }
5378 }
David Brazdil0debae72015-11-12 18:37:00 +00005379
5380 // If neither branch falls through (case 3), the conditional branch to `true_target`
5381 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5382 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005383 __ B(false_target);
5384 }
5385}
5386
5387void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005389 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005390 locations->SetInAt(0, Location::RequiresRegister());
5391 }
5392}
5393
5394void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005395 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5396 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5397 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5398 nullptr : codegen_->GetLabelOf(true_successor);
5399 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5400 nullptr : codegen_->GetLabelOf(false_successor);
5401 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005402}
5403
5404void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5405 LocationSummary* locations = new (GetGraph()->GetArena())
5406 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005407 InvokeRuntimeCallingConvention calling_convention;
5408 RegisterSet caller_saves = RegisterSet::Empty();
5409 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5410 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005411 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005412 locations->SetInAt(0, Location::RequiresRegister());
5413 }
5414}
5415
5416void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005417 SlowPathCodeMIPS* slow_path =
5418 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005419 GenerateTestAndBranch(deoptimize,
5420 /* condition_input_index */ 0,
5421 slow_path->GetEntryLabel(),
5422 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005423}
5424
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005425// This function returns true if a conditional move can be generated for HSelect.
5426// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5427// branches and regular moves.
5428//
5429// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5430//
5431// While determining feasibility of a conditional move and setting inputs/outputs
5432// are two distinct tasks, this function does both because they share quite a bit
5433// of common logic.
5434static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5435 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5436 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5437 HCondition* condition = cond->AsCondition();
5438
5439 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5440 Primitive::Type dst_type = select->GetType();
5441
5442 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5443 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5444 bool is_true_value_zero_constant =
5445 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5446 bool is_false_value_zero_constant =
5447 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5448
5449 bool can_move_conditionally = false;
5450 bool use_const_for_false_in = false;
5451 bool use_const_for_true_in = false;
5452
5453 if (!cond->IsConstant()) {
5454 switch (cond_type) {
5455 default:
5456 switch (dst_type) {
5457 default:
5458 // Moving int on int condition.
5459 if (is_r6) {
5460 if (is_true_value_zero_constant) {
5461 // seleqz out_reg, false_reg, cond_reg
5462 can_move_conditionally = true;
5463 use_const_for_true_in = true;
5464 } else if (is_false_value_zero_constant) {
5465 // selnez out_reg, true_reg, cond_reg
5466 can_move_conditionally = true;
5467 use_const_for_false_in = true;
5468 } else if (materialized) {
5469 // Not materializing unmaterialized int conditions
5470 // to keep the instruction count low.
5471 // selnez AT, true_reg, cond_reg
5472 // seleqz TMP, false_reg, cond_reg
5473 // or out_reg, AT, TMP
5474 can_move_conditionally = true;
5475 }
5476 } else {
5477 // movn out_reg, true_reg/ZERO, cond_reg
5478 can_move_conditionally = true;
5479 use_const_for_true_in = is_true_value_zero_constant;
5480 }
5481 break;
5482 case Primitive::kPrimLong:
5483 // Moving long on int condition.
5484 if (is_r6) {
5485 if (is_true_value_zero_constant) {
5486 // seleqz out_reg_lo, false_reg_lo, cond_reg
5487 // seleqz out_reg_hi, false_reg_hi, cond_reg
5488 can_move_conditionally = true;
5489 use_const_for_true_in = true;
5490 } else if (is_false_value_zero_constant) {
5491 // selnez out_reg_lo, true_reg_lo, cond_reg
5492 // selnez out_reg_hi, true_reg_hi, cond_reg
5493 can_move_conditionally = true;
5494 use_const_for_false_in = true;
5495 }
5496 // Other long conditional moves would generate 6+ instructions,
5497 // which is too many.
5498 } else {
5499 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5500 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5501 can_move_conditionally = true;
5502 use_const_for_true_in = is_true_value_zero_constant;
5503 }
5504 break;
5505 case Primitive::kPrimFloat:
5506 case Primitive::kPrimDouble:
5507 // Moving float/double on int condition.
5508 if (is_r6) {
5509 if (materialized) {
5510 // Not materializing unmaterialized int conditions
5511 // to keep the instruction count low.
5512 can_move_conditionally = true;
5513 if (is_true_value_zero_constant) {
5514 // sltu TMP, ZERO, cond_reg
5515 // mtc1 TMP, temp_cond_reg
5516 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5517 use_const_for_true_in = true;
5518 } else if (is_false_value_zero_constant) {
5519 // sltu TMP, ZERO, cond_reg
5520 // mtc1 TMP, temp_cond_reg
5521 // selnez.fmt out_reg, true_reg, temp_cond_reg
5522 use_const_for_false_in = true;
5523 } else {
5524 // sltu TMP, ZERO, cond_reg
5525 // mtc1 TMP, temp_cond_reg
5526 // sel.fmt temp_cond_reg, false_reg, true_reg
5527 // mov.fmt out_reg, temp_cond_reg
5528 }
5529 }
5530 } else {
5531 // movn.fmt out_reg, true_reg, cond_reg
5532 can_move_conditionally = true;
5533 }
5534 break;
5535 }
5536 break;
5537 case Primitive::kPrimLong:
5538 // We don't materialize long comparison now
5539 // and use conditional branches instead.
5540 break;
5541 case Primitive::kPrimFloat:
5542 case Primitive::kPrimDouble:
5543 switch (dst_type) {
5544 default:
5545 // Moving int on float/double condition.
5546 if (is_r6) {
5547 if (is_true_value_zero_constant) {
5548 // mfc1 TMP, temp_cond_reg
5549 // seleqz out_reg, false_reg, TMP
5550 can_move_conditionally = true;
5551 use_const_for_true_in = true;
5552 } else if (is_false_value_zero_constant) {
5553 // mfc1 TMP, temp_cond_reg
5554 // selnez out_reg, true_reg, TMP
5555 can_move_conditionally = true;
5556 use_const_for_false_in = true;
5557 } else {
5558 // mfc1 TMP, temp_cond_reg
5559 // selnez AT, true_reg, TMP
5560 // seleqz TMP, false_reg, TMP
5561 // or out_reg, AT, TMP
5562 can_move_conditionally = true;
5563 }
5564 } else {
5565 // movt out_reg, true_reg/ZERO, cc
5566 can_move_conditionally = true;
5567 use_const_for_true_in = is_true_value_zero_constant;
5568 }
5569 break;
5570 case Primitive::kPrimLong:
5571 // Moving long on float/double condition.
5572 if (is_r6) {
5573 if (is_true_value_zero_constant) {
5574 // mfc1 TMP, temp_cond_reg
5575 // seleqz out_reg_lo, false_reg_lo, TMP
5576 // seleqz out_reg_hi, false_reg_hi, TMP
5577 can_move_conditionally = true;
5578 use_const_for_true_in = true;
5579 } else if (is_false_value_zero_constant) {
5580 // mfc1 TMP, temp_cond_reg
5581 // selnez out_reg_lo, true_reg_lo, TMP
5582 // selnez out_reg_hi, true_reg_hi, TMP
5583 can_move_conditionally = true;
5584 use_const_for_false_in = true;
5585 }
5586 // Other long conditional moves would generate 6+ instructions,
5587 // which is too many.
5588 } else {
5589 // movt out_reg_lo, true_reg_lo/ZERO, cc
5590 // movt out_reg_hi, true_reg_hi/ZERO, cc
5591 can_move_conditionally = true;
5592 use_const_for_true_in = is_true_value_zero_constant;
5593 }
5594 break;
5595 case Primitive::kPrimFloat:
5596 case Primitive::kPrimDouble:
5597 // Moving float/double on float/double condition.
5598 if (is_r6) {
5599 can_move_conditionally = true;
5600 if (is_true_value_zero_constant) {
5601 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5602 use_const_for_true_in = true;
5603 } else if (is_false_value_zero_constant) {
5604 // selnez.fmt out_reg, true_reg, temp_cond_reg
5605 use_const_for_false_in = true;
5606 } else {
5607 // sel.fmt temp_cond_reg, false_reg, true_reg
5608 // mov.fmt out_reg, temp_cond_reg
5609 }
5610 } else {
5611 // movt.fmt out_reg, true_reg, cc
5612 can_move_conditionally = true;
5613 }
5614 break;
5615 }
5616 break;
5617 }
5618 }
5619
5620 if (can_move_conditionally) {
5621 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5622 } else {
5623 DCHECK(!use_const_for_false_in);
5624 DCHECK(!use_const_for_true_in);
5625 }
5626
5627 if (locations_to_set != nullptr) {
5628 if (use_const_for_false_in) {
5629 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5630 } else {
5631 locations_to_set->SetInAt(0,
5632 Primitive::IsFloatingPointType(dst_type)
5633 ? Location::RequiresFpuRegister()
5634 : Location::RequiresRegister());
5635 }
5636 if (use_const_for_true_in) {
5637 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5638 } else {
5639 locations_to_set->SetInAt(1,
5640 Primitive::IsFloatingPointType(dst_type)
5641 ? Location::RequiresFpuRegister()
5642 : Location::RequiresRegister());
5643 }
5644 if (materialized) {
5645 locations_to_set->SetInAt(2, Location::RequiresRegister());
5646 }
5647 // On R6 we don't require the output to be the same as the
5648 // first input for conditional moves unlike on R2.
5649 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5650 if (is_out_same_as_first_in) {
5651 locations_to_set->SetOut(Location::SameAsFirstInput());
5652 } else {
5653 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5654 ? Location::RequiresFpuRegister()
5655 : Location::RequiresRegister());
5656 }
5657 }
5658
5659 return can_move_conditionally;
5660}
5661
5662void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5663 LocationSummary* locations = select->GetLocations();
5664 Location dst = locations->Out();
5665 Location src = locations->InAt(1);
5666 Register src_reg = ZERO;
5667 Register src_reg_high = ZERO;
5668 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5669 Register cond_reg = TMP;
5670 int cond_cc = 0;
5671 Primitive::Type cond_type = Primitive::kPrimInt;
5672 bool cond_inverted = false;
5673 Primitive::Type dst_type = select->GetType();
5674
5675 if (IsBooleanValueOrMaterializedCondition(cond)) {
5676 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5677 } else {
5678 HCondition* condition = cond->AsCondition();
5679 LocationSummary* cond_locations = cond->GetLocations();
5680 IfCondition if_cond = condition->GetCondition();
5681 cond_type = condition->InputAt(0)->GetType();
5682 switch (cond_type) {
5683 default:
5684 DCHECK_NE(cond_type, Primitive::kPrimLong);
5685 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5686 break;
5687 case Primitive::kPrimFloat:
5688 case Primitive::kPrimDouble:
5689 cond_inverted = MaterializeFpCompareR2(if_cond,
5690 condition->IsGtBias(),
5691 cond_type,
5692 cond_locations,
5693 cond_cc);
5694 break;
5695 }
5696 }
5697
5698 DCHECK(dst.Equals(locations->InAt(0)));
5699 if (src.IsRegister()) {
5700 src_reg = src.AsRegister<Register>();
5701 } else if (src.IsRegisterPair()) {
5702 src_reg = src.AsRegisterPairLow<Register>();
5703 src_reg_high = src.AsRegisterPairHigh<Register>();
5704 } else if (src.IsConstant()) {
5705 DCHECK(src.GetConstant()->IsZeroBitPattern());
5706 }
5707
5708 switch (cond_type) {
5709 default:
5710 switch (dst_type) {
5711 default:
5712 if (cond_inverted) {
5713 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5714 } else {
5715 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5716 }
5717 break;
5718 case Primitive::kPrimLong:
5719 if (cond_inverted) {
5720 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5721 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5722 } else {
5723 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5724 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5725 }
5726 break;
5727 case Primitive::kPrimFloat:
5728 if (cond_inverted) {
5729 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5730 } else {
5731 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5732 }
5733 break;
5734 case Primitive::kPrimDouble:
5735 if (cond_inverted) {
5736 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5737 } else {
5738 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5739 }
5740 break;
5741 }
5742 break;
5743 case Primitive::kPrimLong:
5744 LOG(FATAL) << "Unreachable";
5745 UNREACHABLE();
5746 case Primitive::kPrimFloat:
5747 case Primitive::kPrimDouble:
5748 switch (dst_type) {
5749 default:
5750 if (cond_inverted) {
5751 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5752 } else {
5753 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5754 }
5755 break;
5756 case Primitive::kPrimLong:
5757 if (cond_inverted) {
5758 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5759 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5760 } else {
5761 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5762 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5763 }
5764 break;
5765 case Primitive::kPrimFloat:
5766 if (cond_inverted) {
5767 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5768 } else {
5769 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5770 }
5771 break;
5772 case Primitive::kPrimDouble:
5773 if (cond_inverted) {
5774 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5775 } else {
5776 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5777 }
5778 break;
5779 }
5780 break;
5781 }
5782}
5783
5784void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5785 LocationSummary* locations = select->GetLocations();
5786 Location dst = locations->Out();
5787 Location false_src = locations->InAt(0);
5788 Location true_src = locations->InAt(1);
5789 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5790 Register cond_reg = TMP;
5791 FRegister fcond_reg = FTMP;
5792 Primitive::Type cond_type = Primitive::kPrimInt;
5793 bool cond_inverted = false;
5794 Primitive::Type dst_type = select->GetType();
5795
5796 if (IsBooleanValueOrMaterializedCondition(cond)) {
5797 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5798 } else {
5799 HCondition* condition = cond->AsCondition();
5800 LocationSummary* cond_locations = cond->GetLocations();
5801 IfCondition if_cond = condition->GetCondition();
5802 cond_type = condition->InputAt(0)->GetType();
5803 switch (cond_type) {
5804 default:
5805 DCHECK_NE(cond_type, Primitive::kPrimLong);
5806 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5807 break;
5808 case Primitive::kPrimFloat:
5809 case Primitive::kPrimDouble:
5810 cond_inverted = MaterializeFpCompareR6(if_cond,
5811 condition->IsGtBias(),
5812 cond_type,
5813 cond_locations,
5814 fcond_reg);
5815 break;
5816 }
5817 }
5818
5819 if (true_src.IsConstant()) {
5820 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5821 }
5822 if (false_src.IsConstant()) {
5823 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5824 }
5825
5826 switch (dst_type) {
5827 default:
5828 if (Primitive::IsFloatingPointType(cond_type)) {
5829 __ Mfc1(cond_reg, fcond_reg);
5830 }
5831 if (true_src.IsConstant()) {
5832 if (cond_inverted) {
5833 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5834 } else {
5835 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5836 }
5837 } else if (false_src.IsConstant()) {
5838 if (cond_inverted) {
5839 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5840 } else {
5841 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5842 }
5843 } else {
5844 DCHECK_NE(cond_reg, AT);
5845 if (cond_inverted) {
5846 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5847 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5848 } else {
5849 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5850 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5851 }
5852 __ Or(dst.AsRegister<Register>(), AT, TMP);
5853 }
5854 break;
5855 case Primitive::kPrimLong: {
5856 if (Primitive::IsFloatingPointType(cond_type)) {
5857 __ Mfc1(cond_reg, fcond_reg);
5858 }
5859 Register dst_lo = dst.AsRegisterPairLow<Register>();
5860 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5861 if (true_src.IsConstant()) {
5862 Register src_lo = false_src.AsRegisterPairLow<Register>();
5863 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5864 if (cond_inverted) {
5865 __ Selnez(dst_lo, src_lo, cond_reg);
5866 __ Selnez(dst_hi, src_hi, cond_reg);
5867 } else {
5868 __ Seleqz(dst_lo, src_lo, cond_reg);
5869 __ Seleqz(dst_hi, src_hi, cond_reg);
5870 }
5871 } else {
5872 DCHECK(false_src.IsConstant());
5873 Register src_lo = true_src.AsRegisterPairLow<Register>();
5874 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5875 if (cond_inverted) {
5876 __ Seleqz(dst_lo, src_lo, cond_reg);
5877 __ Seleqz(dst_hi, src_hi, cond_reg);
5878 } else {
5879 __ Selnez(dst_lo, src_lo, cond_reg);
5880 __ Selnez(dst_hi, src_hi, cond_reg);
5881 }
5882 }
5883 break;
5884 }
5885 case Primitive::kPrimFloat: {
5886 if (!Primitive::IsFloatingPointType(cond_type)) {
5887 // sel*.fmt tests bit 0 of the condition register, account for that.
5888 __ Sltu(TMP, ZERO, cond_reg);
5889 __ Mtc1(TMP, fcond_reg);
5890 }
5891 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5892 if (true_src.IsConstant()) {
5893 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5894 if (cond_inverted) {
5895 __ SelnezS(dst_reg, src_reg, fcond_reg);
5896 } else {
5897 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5898 }
5899 } else if (false_src.IsConstant()) {
5900 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5901 if (cond_inverted) {
5902 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5903 } else {
5904 __ SelnezS(dst_reg, src_reg, fcond_reg);
5905 }
5906 } else {
5907 if (cond_inverted) {
5908 __ SelS(fcond_reg,
5909 true_src.AsFpuRegister<FRegister>(),
5910 false_src.AsFpuRegister<FRegister>());
5911 } else {
5912 __ SelS(fcond_reg,
5913 false_src.AsFpuRegister<FRegister>(),
5914 true_src.AsFpuRegister<FRegister>());
5915 }
5916 __ MovS(dst_reg, fcond_reg);
5917 }
5918 break;
5919 }
5920 case Primitive::kPrimDouble: {
5921 if (!Primitive::IsFloatingPointType(cond_type)) {
5922 // sel*.fmt tests bit 0 of the condition register, account for that.
5923 __ Sltu(TMP, ZERO, cond_reg);
5924 __ Mtc1(TMP, fcond_reg);
5925 }
5926 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5927 if (true_src.IsConstant()) {
5928 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5929 if (cond_inverted) {
5930 __ SelnezD(dst_reg, src_reg, fcond_reg);
5931 } else {
5932 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5933 }
5934 } else if (false_src.IsConstant()) {
5935 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5936 if (cond_inverted) {
5937 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5938 } else {
5939 __ SelnezD(dst_reg, src_reg, fcond_reg);
5940 }
5941 } else {
5942 if (cond_inverted) {
5943 __ SelD(fcond_reg,
5944 true_src.AsFpuRegister<FRegister>(),
5945 false_src.AsFpuRegister<FRegister>());
5946 } else {
5947 __ SelD(fcond_reg,
5948 false_src.AsFpuRegister<FRegister>(),
5949 true_src.AsFpuRegister<FRegister>());
5950 }
5951 __ MovD(dst_reg, fcond_reg);
5952 }
5953 break;
5954 }
5955 }
5956}
5957
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005958void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5959 LocationSummary* locations = new (GetGraph()->GetArena())
5960 LocationSummary(flag, LocationSummary::kNoCall);
5961 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005962}
5963
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005964void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5965 __ LoadFromOffset(kLoadWord,
5966 flag->GetLocations()->Out().AsRegister<Register>(),
5967 SP,
5968 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005969}
5970
David Brazdil74eb1b22015-12-14 11:44:01 +00005971void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5972 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005973 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005974}
5975
5976void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005977 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5978 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5979 if (is_r6) {
5980 GenConditionalMoveR6(select);
5981 } else {
5982 GenConditionalMoveR2(select);
5983 }
5984 } else {
5985 LocationSummary* locations = select->GetLocations();
5986 MipsLabel false_target;
5987 GenerateTestAndBranch(select,
5988 /* condition_input_index */ 2,
5989 /* true_target */ nullptr,
5990 &false_target);
5991 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5992 __ Bind(&false_target);
5993 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005994}
5995
David Srbecky0cf44932015-12-09 14:09:59 +00005996void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5997 new (GetGraph()->GetArena()) LocationSummary(info);
5998}
5999
David Srbeckyd28f4a02016-03-14 17:14:24 +00006000void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6001 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006002}
6003
6004void CodeGeneratorMIPS::GenerateNop() {
6005 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006006}
6007
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006008void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
6009 Primitive::Type field_type = field_info.GetFieldType();
6010 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6011 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006012 bool object_field_get_with_read_barrier =
6013 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006015 instruction,
6016 generate_volatile
6017 ? LocationSummary::kCallOnMainOnly
6018 : (object_field_get_with_read_barrier
6019 ? LocationSummary::kCallOnSlowPath
6020 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006021
Alexey Frunzec61c0762017-04-10 13:54:23 -07006022 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6023 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6024 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006025 locations->SetInAt(0, Location::RequiresRegister());
6026 if (generate_volatile) {
6027 InvokeRuntimeCallingConvention calling_convention;
6028 // need A0 to hold base + offset
6029 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6030 if (field_type == Primitive::kPrimLong) {
6031 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
6032 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006033 // Use Location::Any() to prevent situations when running out of available fp registers.
6034 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006035 // Need some temp core regs since FP results are returned in core registers
6036 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
6037 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6038 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6039 }
6040 } else {
6041 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6042 locations->SetOut(Location::RequiresFpuRegister());
6043 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006044 // The output overlaps in the case of an object field get with
6045 // read barriers enabled: we do not want the move to overwrite the
6046 // object's location, as we need it to emit the read barrier.
6047 locations->SetOut(Location::RequiresRegister(),
6048 object_field_get_with_read_barrier
6049 ? Location::kOutputOverlap
6050 : Location::kNoOutputOverlap);
6051 }
6052 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6053 // We need a temporary register for the read barrier marking slow
6054 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
6055 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006056 }
6057 }
6058}
6059
6060void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6061 const FieldInfo& field_info,
6062 uint32_t dex_pc) {
6063 Primitive::Type type = field_info.GetFieldType();
6064 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006065 Location obj_loc = locations->InAt(0);
6066 Register obj = obj_loc.AsRegister<Register>();
6067 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006068 LoadOperandType load_type = kLoadUnsignedByte;
6069 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006070 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006071 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006072
6073 switch (type) {
6074 case Primitive::kPrimBoolean:
6075 load_type = kLoadUnsignedByte;
6076 break;
6077 case Primitive::kPrimByte:
6078 load_type = kLoadSignedByte;
6079 break;
6080 case Primitive::kPrimShort:
6081 load_type = kLoadSignedHalfword;
6082 break;
6083 case Primitive::kPrimChar:
6084 load_type = kLoadUnsignedHalfword;
6085 break;
6086 case Primitive::kPrimInt:
6087 case Primitive::kPrimFloat:
6088 case Primitive::kPrimNot:
6089 load_type = kLoadWord;
6090 break;
6091 case Primitive::kPrimLong:
6092 case Primitive::kPrimDouble:
6093 load_type = kLoadDoubleword;
6094 break;
6095 case Primitive::kPrimVoid:
6096 LOG(FATAL) << "Unreachable type " << type;
6097 UNREACHABLE();
6098 }
6099
6100 if (is_volatile && load_type == kLoadDoubleword) {
6101 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006102 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006103 // Do implicit Null check
6104 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6105 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006106 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006107 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6108 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006109 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006110 if (dst_loc.IsFpuRegister()) {
6111 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006112 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006113 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006114 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006115 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006116 __ StoreToOffset(kStoreWord,
6117 locations->GetTemp(1).AsRegister<Register>(),
6118 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006119 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006120 __ StoreToOffset(kStoreWord,
6121 locations->GetTemp(2).AsRegister<Register>(),
6122 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006123 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006124 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006125 }
6126 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006127 if (type == Primitive::kPrimNot) {
6128 // /* HeapReference<Object> */ dst = *(obj + offset)
6129 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6130 Location temp_loc = locations->GetTemp(0);
6131 // Note that a potential implicit null check is handled in this
6132 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6133 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6134 dst_loc,
6135 obj,
6136 offset,
6137 temp_loc,
6138 /* needs_null_check */ true);
6139 if (is_volatile) {
6140 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6141 }
6142 } else {
6143 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6144 if (is_volatile) {
6145 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6146 }
6147 // If read barriers are enabled, emit read barriers other than
6148 // Baker's using a slow path (and also unpoison the loaded
6149 // reference, if heap poisoning is enabled).
6150 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6151 }
6152 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153 Register dst;
6154 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006155 DCHECK(dst_loc.IsRegisterPair());
6156 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006157 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006158 DCHECK(dst_loc.IsRegister());
6159 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006160 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006161 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006162 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006163 DCHECK(dst_loc.IsFpuRegister());
6164 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006165 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006166 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006167 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006168 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006169 }
6170 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006171 }
6172
Alexey Frunze15958152017-02-09 19:08:30 -08006173 // Memory barriers, in the case of references, are handled in the
6174 // previous switch statement.
6175 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006176 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6177 }
6178}
6179
6180void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6181 Primitive::Type field_type = field_info.GetFieldType();
6182 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6183 bool generate_volatile = field_info.IsVolatile() && is_wide;
6184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006185 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006186
6187 locations->SetInAt(0, Location::RequiresRegister());
6188 if (generate_volatile) {
6189 InvokeRuntimeCallingConvention calling_convention;
6190 // need A0 to hold base + offset
6191 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6192 if (field_type == Primitive::kPrimLong) {
6193 locations->SetInAt(1, Location::RegisterPairLocation(
6194 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6195 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006196 // Use Location::Any() to prevent situations when running out of available fp registers.
6197 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006198 // Pass FP parameters in core registers.
6199 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6200 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6201 }
6202 } else {
6203 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006204 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006205 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006206 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006207 }
6208 }
6209}
6210
6211void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6212 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006213 uint32_t dex_pc,
6214 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006215 Primitive::Type type = field_info.GetFieldType();
6216 LocationSummary* locations = instruction->GetLocations();
6217 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006218 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006219 StoreOperandType store_type = kStoreByte;
6220 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006221 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006222 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006223 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006224
6225 switch (type) {
6226 case Primitive::kPrimBoolean:
6227 case Primitive::kPrimByte:
6228 store_type = kStoreByte;
6229 break;
6230 case Primitive::kPrimShort:
6231 case Primitive::kPrimChar:
6232 store_type = kStoreHalfword;
6233 break;
6234 case Primitive::kPrimInt:
6235 case Primitive::kPrimFloat:
6236 case Primitive::kPrimNot:
6237 store_type = kStoreWord;
6238 break;
6239 case Primitive::kPrimLong:
6240 case Primitive::kPrimDouble:
6241 store_type = kStoreDoubleword;
6242 break;
6243 case Primitive::kPrimVoid:
6244 LOG(FATAL) << "Unreachable type " << type;
6245 UNREACHABLE();
6246 }
6247
6248 if (is_volatile) {
6249 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6250 }
6251
6252 if (is_volatile && store_type == kStoreDoubleword) {
6253 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006254 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006255 // Do implicit Null check.
6256 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6257 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6258 if (type == Primitive::kPrimDouble) {
6259 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006260 if (value_location.IsFpuRegister()) {
6261 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6262 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006263 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006264 value_location.AsFpuRegister<FRegister>());
6265 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006266 __ LoadFromOffset(kLoadWord,
6267 locations->GetTemp(1).AsRegister<Register>(),
6268 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006269 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006270 __ LoadFromOffset(kLoadWord,
6271 locations->GetTemp(2).AsRegister<Register>(),
6272 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006273 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006274 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006275 DCHECK(value_location.IsConstant());
6276 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6277 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006278 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6279 locations->GetTemp(1).AsRegister<Register>(),
6280 value);
6281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006282 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006283 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006284 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6285 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006286 if (value_location.IsConstant()) {
6287 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6288 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6289 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006290 Register src;
6291 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006292 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006293 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006294 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006296 if (kPoisonHeapReferences && needs_write_barrier) {
6297 // Note that in the case where `value` is a null reference,
6298 // we do not enter this block, as a null reference does not
6299 // need poisoning.
6300 DCHECK_EQ(type, Primitive::kPrimNot);
6301 __ PoisonHeapReference(TMP, src);
6302 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6303 } else {
6304 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6305 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006306 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006307 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006308 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006309 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006310 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006311 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006312 }
6313 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006314 }
6315
Alexey Frunzec061de12017-02-14 13:27:23 -08006316 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006317 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006318 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006319 }
6320
6321 if (is_volatile) {
6322 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6323 }
6324}
6325
6326void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6327 HandleFieldGet(instruction, instruction->GetFieldInfo());
6328}
6329
6330void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6331 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6332}
6333
6334void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6335 HandleFieldSet(instruction, instruction->GetFieldInfo());
6336}
6337
6338void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006339 HandleFieldSet(instruction,
6340 instruction->GetFieldInfo(),
6341 instruction->GetDexPc(),
6342 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006343}
6344
Alexey Frunze15958152017-02-09 19:08:30 -08006345void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6346 HInstruction* instruction,
6347 Location out,
6348 uint32_t offset,
6349 Location maybe_temp,
6350 ReadBarrierOption read_barrier_option) {
6351 Register out_reg = out.AsRegister<Register>();
6352 if (read_barrier_option == kWithReadBarrier) {
6353 CHECK(kEmitCompilerReadBarrier);
6354 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6355 if (kUseBakerReadBarrier) {
6356 // Load with fast path based Baker's read barrier.
6357 // /* HeapReference<Object> */ out = *(out + offset)
6358 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6359 out,
6360 out_reg,
6361 offset,
6362 maybe_temp,
6363 /* needs_null_check */ false);
6364 } else {
6365 // Load with slow path based read barrier.
6366 // Save the value of `out` into `maybe_temp` before overwriting it
6367 // in the following move operation, as we will need it for the
6368 // read barrier below.
6369 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6370 // /* HeapReference<Object> */ out = *(out + offset)
6371 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6372 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6373 }
6374 } else {
6375 // Plain load with no read barrier.
6376 // /* HeapReference<Object> */ out = *(out + offset)
6377 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6378 __ MaybeUnpoisonHeapReference(out_reg);
6379 }
6380}
6381
6382void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6383 HInstruction* instruction,
6384 Location out,
6385 Location obj,
6386 uint32_t offset,
6387 Location maybe_temp,
6388 ReadBarrierOption read_barrier_option) {
6389 Register out_reg = out.AsRegister<Register>();
6390 Register obj_reg = obj.AsRegister<Register>();
6391 if (read_barrier_option == kWithReadBarrier) {
6392 CHECK(kEmitCompilerReadBarrier);
6393 if (kUseBakerReadBarrier) {
6394 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6395 // Load with fast path based Baker's read barrier.
6396 // /* HeapReference<Object> */ out = *(obj + offset)
6397 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6398 out,
6399 obj_reg,
6400 offset,
6401 maybe_temp,
6402 /* needs_null_check */ false);
6403 } else {
6404 // Load with slow path based read barrier.
6405 // /* HeapReference<Object> */ out = *(obj + offset)
6406 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6407 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6408 }
6409 } else {
6410 // Plain load with no read barrier.
6411 // /* HeapReference<Object> */ out = *(obj + offset)
6412 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6413 __ MaybeUnpoisonHeapReference(out_reg);
6414 }
6415}
6416
6417void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6418 Location root,
6419 Register obj,
6420 uint32_t offset,
6421 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006422 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006423 if (read_barrier_option == kWithReadBarrier) {
6424 DCHECK(kEmitCompilerReadBarrier);
6425 if (kUseBakerReadBarrier) {
6426 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6427 // Baker's read barrier are used:
6428 //
6429 // root = obj.field;
6430 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6431 // if (temp != null) {
6432 // root = temp(root)
6433 // }
6434
6435 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6436 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6437 static_assert(
6438 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6439 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6440 "have different sizes.");
6441 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6442 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6443 "have different sizes.");
6444
6445 // Slow path marking the GC root `root`.
6446 Location temp = Location::RegisterLocation(T9);
6447 SlowPathCodeMIPS* slow_path =
6448 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6449 instruction,
6450 root,
6451 /*entrypoint*/ temp);
6452 codegen_->AddSlowPath(slow_path);
6453
6454 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6455 const int32_t entry_point_offset =
6456 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6457 // Loading the entrypoint does not require a load acquire since it is only changed when
6458 // threads are suspended or running a checkpoint.
6459 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6460 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6461 // checking GetIsGcMarking.
6462 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6463 __ Bind(slow_path->GetExitLabel());
6464 } else {
6465 // GC root loaded through a slow path for read barriers other
6466 // than Baker's.
6467 // /* GcRoot<mirror::Object>* */ root = obj + offset
6468 __ Addiu32(root_reg, obj, offset);
6469 // /* mirror::Object* */ root = root->Read()
6470 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6471 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006472 } else {
6473 // Plain GC root load with no read barrier.
6474 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6475 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6476 // Note that GC roots are not affected by heap poisoning, thus we
6477 // do not have to unpoison `root_reg` here.
6478 }
6479}
6480
Alexey Frunze15958152017-02-09 19:08:30 -08006481void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6482 Location ref,
6483 Register obj,
6484 uint32_t offset,
6485 Location temp,
6486 bool needs_null_check) {
6487 DCHECK(kEmitCompilerReadBarrier);
6488 DCHECK(kUseBakerReadBarrier);
6489
6490 // /* HeapReference<Object> */ ref = *(obj + offset)
6491 Location no_index = Location::NoLocation();
6492 ScaleFactor no_scale_factor = TIMES_1;
6493 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6494 ref,
6495 obj,
6496 offset,
6497 no_index,
6498 no_scale_factor,
6499 temp,
6500 needs_null_check);
6501}
6502
6503void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6504 Location ref,
6505 Register obj,
6506 uint32_t data_offset,
6507 Location index,
6508 Location temp,
6509 bool needs_null_check) {
6510 DCHECK(kEmitCompilerReadBarrier);
6511 DCHECK(kUseBakerReadBarrier);
6512
6513 static_assert(
6514 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6515 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6516 // /* HeapReference<Object> */ ref =
6517 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6518 ScaleFactor scale_factor = TIMES_4;
6519 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6520 ref,
6521 obj,
6522 data_offset,
6523 index,
6524 scale_factor,
6525 temp,
6526 needs_null_check);
6527}
6528
6529void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6530 Location ref,
6531 Register obj,
6532 uint32_t offset,
6533 Location index,
6534 ScaleFactor scale_factor,
6535 Location temp,
6536 bool needs_null_check,
6537 bool always_update_field) {
6538 DCHECK(kEmitCompilerReadBarrier);
6539 DCHECK(kUseBakerReadBarrier);
6540
6541 // In slow path based read barriers, the read barrier call is
6542 // inserted after the original load. However, in fast path based
6543 // Baker's read barriers, we need to perform the load of
6544 // mirror::Object::monitor_ *before* the original reference load.
6545 // This load-load ordering is required by the read barrier.
6546 // The fast path/slow path (for Baker's algorithm) should look like:
6547 //
6548 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6549 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6550 // HeapReference<Object> ref = *src; // Original reference load.
6551 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6552 // if (is_gray) {
6553 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6554 // }
6555 //
6556 // Note: the original implementation in ReadBarrier::Barrier is
6557 // slightly more complex as it performs additional checks that we do
6558 // not do here for performance reasons.
6559
6560 Register ref_reg = ref.AsRegister<Register>();
6561 Register temp_reg = temp.AsRegister<Register>();
6562 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6563
6564 // /* int32_t */ monitor = obj->monitor_
6565 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6566 if (needs_null_check) {
6567 MaybeRecordImplicitNullCheck(instruction);
6568 }
6569 // /* LockWord */ lock_word = LockWord(monitor)
6570 static_assert(sizeof(LockWord) == sizeof(int32_t),
6571 "art::LockWord and int32_t have different sizes.");
6572
6573 __ Sync(0); // Barrier to prevent load-load reordering.
6574
6575 // The actual reference load.
6576 if (index.IsValid()) {
6577 // Load types involving an "index": ArrayGet,
6578 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6579 // intrinsics.
6580 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6581 if (index.IsConstant()) {
6582 size_t computed_offset =
6583 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6584 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6585 } else {
6586 // Handle the special case of the
6587 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6588 // intrinsics, which use a register pair as index ("long
6589 // offset"), of which only the low part contains data.
6590 Register index_reg = index.IsRegisterPair()
6591 ? index.AsRegisterPairLow<Register>()
6592 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006593 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006594 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6595 }
6596 } else {
6597 // /* HeapReference<Object> */ ref = *(obj + offset)
6598 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6599 }
6600
6601 // Object* ref = ref_addr->AsMirrorPtr()
6602 __ MaybeUnpoisonHeapReference(ref_reg);
6603
6604 // Slow path marking the object `ref` when it is gray.
6605 SlowPathCodeMIPS* slow_path;
6606 if (always_update_field) {
6607 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6608 // of the form `obj + field_offset`, where `obj` is a register and
6609 // `field_offset` is a register pair (of which only the lower half
6610 // is used). Thus `offset` and `scale_factor` above are expected
6611 // to be null in this code path.
6612 DCHECK_EQ(offset, 0u);
6613 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6614 slow_path = new (GetGraph()->GetArena())
6615 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6616 ref,
6617 obj,
6618 /* field_offset */ index,
6619 temp_reg);
6620 } else {
6621 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6622 }
6623 AddSlowPath(slow_path);
6624
6625 // if (rb_state == ReadBarrier::GrayState())
6626 // ref = ReadBarrier::Mark(ref);
6627 // Given the numeric representation, it's enough to check the low bit of the
6628 // rb_state. We do that by shifting the bit into the sign bit (31) and
6629 // performing a branch on less than zero.
6630 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6631 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6632 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6633 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6634 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6635 __ Bind(slow_path->GetExitLabel());
6636}
6637
6638void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6639 Location out,
6640 Location ref,
6641 Location obj,
6642 uint32_t offset,
6643 Location index) {
6644 DCHECK(kEmitCompilerReadBarrier);
6645
6646 // Insert a slow path based read barrier *after* the reference load.
6647 //
6648 // If heap poisoning is enabled, the unpoisoning of the loaded
6649 // reference will be carried out by the runtime within the slow
6650 // path.
6651 //
6652 // Note that `ref` currently does not get unpoisoned (when heap
6653 // poisoning is enabled), which is alright as the `ref` argument is
6654 // not used by the artReadBarrierSlow entry point.
6655 //
6656 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6657 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6658 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6659 AddSlowPath(slow_path);
6660
6661 __ B(slow_path->GetEntryLabel());
6662 __ Bind(slow_path->GetExitLabel());
6663}
6664
6665void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6666 Location out,
6667 Location ref,
6668 Location obj,
6669 uint32_t offset,
6670 Location index) {
6671 if (kEmitCompilerReadBarrier) {
6672 // Baker's read barriers shall be handled by the fast path
6673 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6674 DCHECK(!kUseBakerReadBarrier);
6675 // If heap poisoning is enabled, unpoisoning will be taken care of
6676 // by the runtime within the slow path.
6677 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6678 } else if (kPoisonHeapReferences) {
6679 __ UnpoisonHeapReference(out.AsRegister<Register>());
6680 }
6681}
6682
6683void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6684 Location out,
6685 Location root) {
6686 DCHECK(kEmitCompilerReadBarrier);
6687
6688 // Insert a slow path based read barrier *after* the GC root load.
6689 //
6690 // Note that GC roots are not affected by heap poisoning, so we do
6691 // not need to do anything special for this here.
6692 SlowPathCodeMIPS* slow_path =
6693 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6694 AddSlowPath(slow_path);
6695
6696 __ B(slow_path->GetEntryLabel());
6697 __ Bind(slow_path->GetExitLabel());
6698}
6699
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006700void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006701 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6702 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07006703 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006704 switch (type_check_kind) {
6705 case TypeCheckKind::kExactCheck:
6706 case TypeCheckKind::kAbstractClassCheck:
6707 case TypeCheckKind::kClassHierarchyCheck:
6708 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006709 call_kind =
6710 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006711 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006712 break;
6713 case TypeCheckKind::kArrayCheck:
6714 case TypeCheckKind::kUnresolvedCheck:
6715 case TypeCheckKind::kInterfaceCheck:
6716 call_kind = LocationSummary::kCallOnSlowPath;
6717 break;
6718 }
6719
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006721 if (baker_read_barrier_slow_path) {
6722 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6723 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724 locations->SetInAt(0, Location::RequiresRegister());
6725 locations->SetInAt(1, Location::RequiresRegister());
6726 // The output does overlap inputs.
6727 // Note that TypeCheckSlowPathMIPS uses this register too.
6728 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006729 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006730}
6731
6732void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006733 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006735 Location obj_loc = locations->InAt(0);
6736 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006737 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006738 Location out_loc = locations->Out();
6739 Register out = out_loc.AsRegister<Register>();
6740 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6741 DCHECK_LE(num_temps, 1u);
6742 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006743 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6744 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6745 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6746 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006747 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006748 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006749
6750 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006751 // Avoid this check if we know `obj` is not null.
6752 if (instruction->MustDoNullCheck()) {
6753 __ Move(out, ZERO);
6754 __ Beqz(obj, &done);
6755 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006756
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006757 switch (type_check_kind) {
6758 case TypeCheckKind::kExactCheck: {
6759 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006760 GenerateReferenceLoadTwoRegisters(instruction,
6761 out_loc,
6762 obj_loc,
6763 class_offset,
6764 maybe_temp_loc,
6765 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006766 // Classes must be equal for the instanceof to succeed.
6767 __ Xor(out, out, cls);
6768 __ Sltiu(out, out, 1);
6769 break;
6770 }
6771
6772 case TypeCheckKind::kAbstractClassCheck: {
6773 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006774 GenerateReferenceLoadTwoRegisters(instruction,
6775 out_loc,
6776 obj_loc,
6777 class_offset,
6778 maybe_temp_loc,
6779 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006780 // If the class is abstract, we eagerly fetch the super class of the
6781 // object to avoid doing a comparison we know will fail.
6782 MipsLabel loop;
6783 __ Bind(&loop);
6784 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006785 GenerateReferenceLoadOneRegister(instruction,
6786 out_loc,
6787 super_offset,
6788 maybe_temp_loc,
6789 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006790 // If `out` is null, we use it for the result, and jump to `done`.
6791 __ Beqz(out, &done);
6792 __ Bne(out, cls, &loop);
6793 __ LoadConst32(out, 1);
6794 break;
6795 }
6796
6797 case TypeCheckKind::kClassHierarchyCheck: {
6798 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006799 GenerateReferenceLoadTwoRegisters(instruction,
6800 out_loc,
6801 obj_loc,
6802 class_offset,
6803 maybe_temp_loc,
6804 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006805 // Walk over the class hierarchy to find a match.
6806 MipsLabel loop, success;
6807 __ Bind(&loop);
6808 __ Beq(out, cls, &success);
6809 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006810 GenerateReferenceLoadOneRegister(instruction,
6811 out_loc,
6812 super_offset,
6813 maybe_temp_loc,
6814 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006815 __ Bnez(out, &loop);
6816 // If `out` is null, we use it for the result, and jump to `done`.
6817 __ B(&done);
6818 __ Bind(&success);
6819 __ LoadConst32(out, 1);
6820 break;
6821 }
6822
6823 case TypeCheckKind::kArrayObjectCheck: {
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 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006831 // Do an exact check.
6832 MipsLabel success;
6833 __ Beq(out, cls, &success);
6834 // Otherwise, we need to check that the object's class is a non-primitive array.
6835 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006836 GenerateReferenceLoadOneRegister(instruction,
6837 out_loc,
6838 component_offset,
6839 maybe_temp_loc,
6840 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006841 // If `out` is null, we use it for the result, and jump to `done`.
6842 __ Beqz(out, &done);
6843 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6844 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6845 __ Sltiu(out, out, 1);
6846 __ B(&done);
6847 __ Bind(&success);
6848 __ LoadConst32(out, 1);
6849 break;
6850 }
6851
6852 case TypeCheckKind::kArrayCheck: {
6853 // No read barrier since the slow path will retry upon failure.
6854 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006855 GenerateReferenceLoadTwoRegisters(instruction,
6856 out_loc,
6857 obj_loc,
6858 class_offset,
6859 maybe_temp_loc,
6860 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006861 DCHECK(locations->OnlyCallsOnSlowPath());
6862 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6863 /* is_fatal */ false);
6864 codegen_->AddSlowPath(slow_path);
6865 __ Bne(out, cls, slow_path->GetEntryLabel());
6866 __ LoadConst32(out, 1);
6867 break;
6868 }
6869
6870 case TypeCheckKind::kUnresolvedCheck:
6871 case TypeCheckKind::kInterfaceCheck: {
6872 // Note that we indeed only call on slow path, but we always go
6873 // into the slow path for the unresolved and interface check
6874 // cases.
6875 //
6876 // We cannot directly call the InstanceofNonTrivial runtime
6877 // entry point without resorting to a type checking slow path
6878 // here (i.e. by calling InvokeRuntime directly), as it would
6879 // require to assign fixed registers for the inputs of this
6880 // HInstanceOf instruction (following the runtime calling
6881 // convention), which might be cluttered by the potential first
6882 // read barrier emission at the beginning of this method.
6883 //
6884 // TODO: Introduce a new runtime entry point taking the object
6885 // to test (instead of its class) as argument, and let it deal
6886 // with the read barrier issues. This will let us refactor this
6887 // case of the `switch` code as it was previously (with a direct
6888 // call to the runtime not using a type checking slow path).
6889 // This should also be beneficial for the other cases above.
6890 DCHECK(locations->OnlyCallsOnSlowPath());
6891 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6892 /* is_fatal */ false);
6893 codegen_->AddSlowPath(slow_path);
6894 __ B(slow_path->GetEntryLabel());
6895 break;
6896 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006897 }
6898
6899 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006900
6901 if (slow_path != nullptr) {
6902 __ Bind(slow_path->GetExitLabel());
6903 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006904}
6905
6906void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6908 locations->SetOut(Location::ConstantLocation(constant));
6909}
6910
6911void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6912 // Will be generated at use site.
6913}
6914
6915void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6916 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6917 locations->SetOut(Location::ConstantLocation(constant));
6918}
6919
6920void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6921 // Will be generated at use site.
6922}
6923
6924void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6925 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6926 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6927}
6928
6929void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6930 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006931 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006932 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006933 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006934}
6935
6936void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6937 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6938 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006939 Location receiver = invoke->GetLocations()->InAt(0);
6940 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006941 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006942
6943 // Set the hidden argument.
6944 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6945 invoke->GetDexMethodIndex());
6946
6947 // temp = object->GetClass();
6948 if (receiver.IsStackSlot()) {
6949 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6950 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6951 } else {
6952 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6953 }
6954 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006955 // Instead of simply (possibly) unpoisoning `temp` here, we should
6956 // emit a read barrier for the previous class reference load.
6957 // However this is not required in practice, as this is an
6958 // intermediate/temporary reference and because the current
6959 // concurrent copying collector keeps the from-space memory
6960 // intact/accessible until the end of the marking phase (the
6961 // concurrent copying collector may not in the future).
6962 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006963 __ LoadFromOffset(kLoadWord, temp, temp,
6964 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6965 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006966 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006967 // temp = temp->GetImtEntryAt(method_offset);
6968 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6969 // T9 = temp->GetEntryPoint();
6970 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6971 // T9();
6972 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006973 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006974 DCHECK(!codegen_->IsLeafMethod());
6975 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6976}
6977
6978void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006979 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6980 if (intrinsic.TryDispatch(invoke)) {
6981 return;
6982 }
6983
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006984 HandleInvoke(invoke);
6985}
6986
6987void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006988 // Explicit clinit checks triggered by static invokes must have been pruned by
6989 // art::PrepareForRegisterAllocation.
6990 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006991
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006992 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6993 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006994
Chris Larsen701566a2015-10-27 15:29:13 -07006995 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6996 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006997 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6998 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6999 }
Chris Larsen701566a2015-10-27 15:29:13 -07007000 return;
7001 }
7002
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007003 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007004
7005 // Add the extra input register if either the dex cache array base register
7006 // or the PC-relative base register for accessing literals is needed.
7007 if (has_extra_input) {
7008 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7009 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007010}
7011
Orion Hodsonac141392017-01-13 11:53:47 +00007012void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7013 HandleInvoke(invoke);
7014}
7015
7016void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7017 codegen_->GenerateInvokePolymorphicCall(invoke);
7018}
7019
Chris Larsen701566a2015-10-27 15:29:13 -07007020static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007021 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007022 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7023 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007024 return true;
7025 }
7026 return false;
7027}
7028
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007029HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007030 HLoadString::LoadKind desired_string_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.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007033 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
7034 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007035 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007036 bool is_r6 = GetInstructionSetFeatures().IsR6();
7037 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007038 switch (desired_string_load_kind) {
7039 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7040 DCHECK(!GetCompilerOptions().GetCompilePic());
7041 break;
7042 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
7043 DCHECK(GetCompilerOptions().GetCompilePic());
7044 break;
7045 case HLoadString::LoadKind::kBootImageAddress:
7046 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007047 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007048 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007049 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007050 case HLoadString::LoadKind::kJitTableAddress:
7051 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007052 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007053 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007054 case HLoadString::LoadKind::kDexCacheViaMethod:
7055 fallback_load = false;
7056 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007057 }
7058 if (fallback_load) {
7059 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
7060 }
7061 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007062}
7063
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007064HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7065 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007066 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007067 // is incompatible with it.
7068 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007069 bool is_r6 = GetInstructionSetFeatures().IsR6();
7070 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007071 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007072 case HLoadClass::LoadKind::kInvalid:
7073 LOG(FATAL) << "UNREACHABLE";
7074 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007075 case HLoadClass::LoadKind::kReferrersClass:
7076 fallback_load = false;
7077 break;
7078 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
7079 DCHECK(!GetCompilerOptions().GetCompilePic());
7080 break;
7081 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
7082 DCHECK(GetCompilerOptions().GetCompilePic());
7083 break;
7084 case HLoadClass::LoadKind::kBootImageAddress:
7085 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007086 case HLoadClass::LoadKind::kBssEntry:
7087 DCHECK(!Runtime::Current()->UseJitCompilation());
7088 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007089 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007090 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007091 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007092 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007093 case HLoadClass::LoadKind::kDexCacheViaMethod:
7094 fallback_load = false;
7095 break;
7096 }
7097 if (fallback_load) {
7098 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
7099 }
7100 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007101}
7102
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007103Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7104 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007105 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007106 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7107 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7108 if (!invoke->GetLocations()->Intrinsified()) {
7109 return location.AsRegister<Register>();
7110 }
7111 // For intrinsics we allow any location, so it may be on the stack.
7112 if (!location.IsRegister()) {
7113 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7114 return temp;
7115 }
7116 // For register locations, check if the register was saved. If so, get it from the stack.
7117 // Note: There is a chance that the register was saved but not overwritten, so we could
7118 // save one load. However, since this is just an intrinsic slow path we prefer this
7119 // simple and more robust approach rather that trying to determine if that's the case.
7120 SlowPathCode* slow_path = GetCurrentSlowPath();
7121 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7122 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7123 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7124 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7125 return temp;
7126 }
7127 return location.AsRegister<Register>();
7128}
7129
Vladimir Markodc151b22015-10-15 18:02:30 +01007130HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7131 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007132 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007133 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007134 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007135 // is incompatible with it.
7136 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007137 bool is_r6 = GetInstructionSetFeatures().IsR6();
7138 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007139 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007140 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007141 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01007142 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007143 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01007144 break;
7145 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007146 if (fallback_load) {
7147 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7148 dispatch_info.method_load_data = 0;
7149 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007150 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007151}
7152
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007153void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7154 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007155 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007156 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7157 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007158 bool is_r6 = GetInstructionSetFeatures().IsR6();
7159 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007160 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7161 : ZERO;
7162
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007163 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007164 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007165 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007166 uint32_t offset =
7167 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007168 __ LoadFromOffset(kLoadWord,
7169 temp.AsRegister<Register>(),
7170 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007171 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007172 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007173 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007174 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007175 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007176 break;
7177 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7178 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7179 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007180 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
7181 if (is_r6) {
7182 uint32_t offset = invoke->GetDexCacheArrayOffset();
7183 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7184 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
7185 bool reordering = __ SetReorder(false);
7186 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
7187 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
7188 __ SetReorder(reordering);
7189 } else {
7190 HMipsDexCacheArraysBase* base =
7191 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
7192 int32_t offset =
7193 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
7194 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7195 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007196 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007197 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007198 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199 Register reg = temp.AsRegister<Register>();
7200 Register method_reg;
7201 if (current_method.IsRegister()) {
7202 method_reg = current_method.AsRegister<Register>();
7203 } else {
7204 // TODO: use the appropriate DCHECK() here if possible.
7205 // DCHECK(invoke->GetLocations()->Intrinsified());
7206 DCHECK(!current_method.IsValid());
7207 method_reg = reg;
7208 __ Lw(reg, SP, kCurrentMethodStackOffset);
7209 }
7210
7211 // temp = temp->dex_cache_resolved_methods_;
7212 __ LoadFromOffset(kLoadWord,
7213 reg,
7214 method_reg,
7215 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007216 // temp = temp[index_in_cache];
7217 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7218 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007219 __ LoadFromOffset(kLoadWord,
7220 reg,
7221 reg,
7222 CodeGenerator::GetCachePointerOffset(index_in_cache));
7223 break;
7224 }
7225 }
7226
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007227 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007228 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007229 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007230 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007231 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7232 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007233 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007234 T9,
7235 callee_method.AsRegister<Register>(),
7236 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007237 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007238 // T9()
7239 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007240 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007241 break;
7242 }
7243 DCHECK(!IsLeafMethod());
7244}
7245
7246void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007247 // Explicit clinit checks triggered by static invokes must have been pruned by
7248 // art::PrepareForRegisterAllocation.
7249 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007250
7251 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7252 return;
7253 }
7254
7255 LocationSummary* locations = invoke->GetLocations();
7256 codegen_->GenerateStaticOrDirectCall(invoke,
7257 locations->HasTemps()
7258 ? locations->GetTemp(0)
7259 : Location::NoLocation());
7260 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7261}
7262
Chris Larsen3acee732015-11-18 13:31:08 -08007263void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007264 // Use the calling convention instead of the location of the receiver, as
7265 // intrinsics may have put the receiver in a different register. In the intrinsics
7266 // slow path, the arguments have been moved to the right place, so here we are
7267 // guaranteed that the receiver is the first register of the calling convention.
7268 InvokeDexCallingConvention calling_convention;
7269 Register receiver = calling_convention.GetRegisterAt(0);
7270
Chris Larsen3acee732015-11-18 13:31:08 -08007271 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007272 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7273 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7274 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007275 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007276
7277 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007278 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007279 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007280 // Instead of simply (possibly) unpoisoning `temp` here, we should
7281 // emit a read barrier for the previous class reference load.
7282 // However this is not required in practice, as this is an
7283 // intermediate/temporary reference and because the current
7284 // concurrent copying collector keeps the from-space memory
7285 // intact/accessible until the end of the marking phase (the
7286 // concurrent copying collector may not in the future).
7287 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007288 // temp = temp->GetMethodAt(method_offset);
7289 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7290 // T9 = temp->GetEntryPoint();
7291 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7292 // T9();
7293 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007294 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08007295}
7296
7297void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7298 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7299 return;
7300 }
7301
7302 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007303 DCHECK(!codegen_->IsLeafMethod());
7304 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7305}
7306
7307void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007308 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7309 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007310 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007311 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7312 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007313 return;
7314 }
Vladimir Marko41559982017-01-06 14:04:23 +00007315 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007316 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007317 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7318 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007319 ? LocationSummary::kCallOnSlowPath
7320 : LocationSummary::kNoCall;
7321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007322 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7323 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7324 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007325 switch (load_kind) {
7326 // We need an extra register for PC-relative literals on R2.
7327 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007328 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007329 case HLoadClass::LoadKind::kBootImageAddress:
7330 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007331 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007332 break;
7333 }
7334 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007335 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007336 locations->SetInAt(0, Location::RequiresRegister());
7337 break;
7338 default:
7339 break;
7340 }
7341 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007342 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7343 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7344 // Rely on the type resolution or initialization and marking to save everything we need.
7345 // Request a temp to hold the BSS entry location for the slow path on R2
7346 // (no benefit for R6).
7347 if (!isR6) {
7348 locations->AddTemp(Location::RequiresRegister());
7349 }
7350 RegisterSet caller_saves = RegisterSet::Empty();
7351 InvokeRuntimeCallingConvention calling_convention;
7352 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7353 locations->SetCustomSlowPathCallerSaves(caller_saves);
7354 } else {
7355 // For non-Baker read barriers we have a temp-clobbering call.
7356 }
7357 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007358}
7359
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007360// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7361// move.
7362void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007363 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7364 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7365 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007366 return;
7367 }
Vladimir Marko41559982017-01-06 14:04:23 +00007368 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007369
Vladimir Marko41559982017-01-06 14:04:23 +00007370 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007371 Location out_loc = locations->Out();
7372 Register out = out_loc.AsRegister<Register>();
7373 Register base_or_current_method_reg;
7374 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7375 switch (load_kind) {
7376 // We need an extra register for PC-relative literals on R2.
7377 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007378 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007379 case HLoadClass::LoadKind::kBootImageAddress:
7380 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007381 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7382 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007383 case HLoadClass::LoadKind::kReferrersClass:
7384 case HLoadClass::LoadKind::kDexCacheViaMethod:
7385 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7386 break;
7387 default:
7388 base_or_current_method_reg = ZERO;
7389 break;
7390 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007391
Alexey Frunze15958152017-02-09 19:08:30 -08007392 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7393 ? kWithoutReadBarrier
7394 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007395 bool generate_null_check = false;
7396 switch (load_kind) {
7397 case HLoadClass::LoadKind::kReferrersClass: {
7398 DCHECK(!cls->CanCallRuntime());
7399 DCHECK(!cls->MustGenerateClinitCheck());
7400 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7401 GenerateGcRootFieldLoad(cls,
7402 out_loc,
7403 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007404 ArtMethod::DeclaringClassOffset().Int32Value(),
7405 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007406 break;
7407 }
7408 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007409 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007410 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007411 __ LoadLiteral(out,
7412 base_or_current_method_reg,
7413 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
7414 cls->GetTypeIndex()));
7415 break;
7416 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007417 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007418 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007419 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7420 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007421 bool reordering = __ SetReorder(false);
7422 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7423 __ Addiu(out, out, /* placeholder */ 0x5678);
7424 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007425 break;
7426 }
7427 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007428 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007429 uint32_t address = dchecked_integral_cast<uint32_t>(
7430 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7431 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007432 __ LoadLiteral(out,
7433 base_or_current_method_reg,
7434 codegen_->DeduplicateBootImageAddressLiteral(address));
7435 break;
7436 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007437 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007438 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007439 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007440 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7441 if (isR6 || non_baker_read_barrier) {
7442 bool reordering = __ SetReorder(false);
7443 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7444 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
7445 __ SetReorder(reordering);
7446 } else {
7447 // On R2 save the BSS entry address in a temporary register instead of
7448 // recalculating it in the slow path.
7449 Register temp = locations->GetTemp(0).AsRegister<Register>();
7450 bool reordering = __ SetReorder(false);
7451 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7452 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7453 __ SetReorder(reordering);
7454 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
7455 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007456 generate_null_check = true;
7457 break;
7458 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007459 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007460 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7461 cls->GetTypeIndex(),
7462 cls->GetClass());
7463 bool reordering = __ SetReorder(false);
7464 __ Bind(&info->high_label);
7465 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007466 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007467 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007468 break;
7469 }
Vladimir Marko41559982017-01-06 14:04:23 +00007470 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007471 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007472 LOG(FATAL) << "UNREACHABLE";
7473 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007474 }
7475
7476 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7477 DCHECK(cls->CanCallRuntime());
7478 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7479 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7480 codegen_->AddSlowPath(slow_path);
7481 if (generate_null_check) {
7482 __ Beqz(out, slow_path->GetEntryLabel());
7483 }
7484 if (cls->MustGenerateClinitCheck()) {
7485 GenerateClassInitializationCheck(slow_path, out);
7486 } else {
7487 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007488 }
7489 }
7490}
7491
7492static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007493 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007494}
7495
7496void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7497 LocationSummary* locations =
7498 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7499 locations->SetOut(Location::RequiresRegister());
7500}
7501
7502void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7503 Register out = load->GetLocations()->Out().AsRegister<Register>();
7504 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7505}
7506
7507void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7508 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7509}
7510
7511void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7512 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7513}
7514
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007515void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007516 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007518 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007519 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007520 switch (load_kind) {
7521 // We need an extra register for PC-relative literals on R2.
7522 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7523 case HLoadString::LoadKind::kBootImageAddress:
7524 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007525 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007526 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007527 break;
7528 }
7529 FALLTHROUGH_INTENDED;
7530 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007531 case HLoadString::LoadKind::kDexCacheViaMethod:
7532 locations->SetInAt(0, Location::RequiresRegister());
7533 break;
7534 default:
7535 break;
7536 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007537 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
7538 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007539 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007540 } else {
7541 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007542 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7543 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7544 // Rely on the pResolveString and marking to save everything we need.
7545 // Request a temp to hold the BSS entry location for the slow path on R2
7546 // (no benefit for R6).
7547 if (!isR6) {
7548 locations->AddTemp(Location::RequiresRegister());
7549 }
7550 RegisterSet caller_saves = RegisterSet::Empty();
7551 InvokeRuntimeCallingConvention calling_convention;
7552 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7553 locations->SetCustomSlowPathCallerSaves(caller_saves);
7554 } else {
7555 // For non-Baker read barriers we have a temp-clobbering call.
7556 }
7557 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007558 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007559}
7560
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007561// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7562// move.
7563void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007564 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007565 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007566 Location out_loc = locations->Out();
7567 Register out = out_loc.AsRegister<Register>();
7568 Register base_or_current_method_reg;
7569 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7570 switch (load_kind) {
7571 // We need an extra register for PC-relative literals on R2.
7572 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7573 case HLoadString::LoadKind::kBootImageAddress:
7574 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007575 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007576 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7577 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007578 default:
7579 base_or_current_method_reg = ZERO;
7580 break;
7581 }
7582
7583 switch (load_kind) {
7584 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007585 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007586 __ LoadLiteral(out,
7587 base_or_current_method_reg,
7588 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7589 load->GetStringIndex()));
7590 return; // No dex cache slow path.
7591 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007592 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007593 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007594 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007595 bool reordering = __ SetReorder(false);
7596 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7597 __ Addiu(out, out, /* placeholder */ 0x5678);
7598 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007599 return; // No dex cache slow path.
7600 }
7601 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007602 uint32_t address = dchecked_integral_cast<uint32_t>(
7603 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7604 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007605 __ LoadLiteral(out,
7606 base_or_current_method_reg,
7607 codegen_->DeduplicateBootImageAddressLiteral(address));
7608 return; // No dex cache slow path.
7609 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007610 case HLoadString::LoadKind::kBssEntry: {
7611 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7612 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007613 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007614 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7615 if (isR6 || non_baker_read_barrier) {
7616 bool reordering = __ SetReorder(false);
7617 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7618 GenerateGcRootFieldLoad(load,
7619 out_loc,
7620 out,
7621 /* placeholder */ 0x5678,
7622 kCompilerReadBarrierOption);
7623 __ SetReorder(reordering);
7624 } else {
7625 // On R2 save the BSS entry address in a temporary register instead of
7626 // recalculating it in the slow path.
7627 Register temp = locations->GetTemp(0).AsRegister<Register>();
7628 bool reordering = __ SetReorder(false);
7629 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7630 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7631 __ SetReorder(reordering);
7632 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7633 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007634 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7635 codegen_->AddSlowPath(slow_path);
7636 __ Beqz(out, slow_path->GetEntryLabel());
7637 __ Bind(slow_path->GetExitLabel());
7638 return;
7639 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007640 case HLoadString::LoadKind::kJitTableAddress: {
7641 CodeGeneratorMIPS::JitPatchInfo* info =
7642 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7643 load->GetStringIndex(),
7644 load->GetString());
7645 bool reordering = __ SetReorder(false);
7646 __ Bind(&info->high_label);
7647 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007648 GenerateGcRootFieldLoad(load,
7649 out_loc,
7650 out,
7651 /* placeholder */ 0x5678,
7652 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007653 __ SetReorder(reordering);
7654 return;
7655 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007656 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007657 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007658 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007659
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007660 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007661 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7662 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007663 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007664 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007665 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7666 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007667}
7668
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007669void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7670 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7671 locations->SetOut(Location::ConstantLocation(constant));
7672}
7673
7674void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7675 // Will be generated at use site.
7676}
7677
7678void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7679 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007681 InvokeRuntimeCallingConvention calling_convention;
7682 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7683}
7684
7685void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7686 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007687 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007688 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7689 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007690 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007691 }
7692 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7693}
7694
7695void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7696 LocationSummary* locations =
7697 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7698 switch (mul->GetResultType()) {
7699 case Primitive::kPrimInt:
7700 case Primitive::kPrimLong:
7701 locations->SetInAt(0, Location::RequiresRegister());
7702 locations->SetInAt(1, Location::RequiresRegister());
7703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7704 break;
7705
7706 case Primitive::kPrimFloat:
7707 case Primitive::kPrimDouble:
7708 locations->SetInAt(0, Location::RequiresFpuRegister());
7709 locations->SetInAt(1, Location::RequiresFpuRegister());
7710 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7711 break;
7712
7713 default:
7714 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7715 }
7716}
7717
7718void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7719 Primitive::Type type = instruction->GetType();
7720 LocationSummary* locations = instruction->GetLocations();
7721 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7722
7723 switch (type) {
7724 case Primitive::kPrimInt: {
7725 Register dst = locations->Out().AsRegister<Register>();
7726 Register lhs = locations->InAt(0).AsRegister<Register>();
7727 Register rhs = locations->InAt(1).AsRegister<Register>();
7728
7729 if (isR6) {
7730 __ MulR6(dst, lhs, rhs);
7731 } else {
7732 __ MulR2(dst, lhs, rhs);
7733 }
7734 break;
7735 }
7736 case Primitive::kPrimLong: {
7737 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7738 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7739 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7740 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7741 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7742 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7743
7744 // Extra checks to protect caused by the existance of A1_A2.
7745 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7746 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7747 DCHECK_NE(dst_high, lhs_low);
7748 DCHECK_NE(dst_high, rhs_low);
7749
7750 // A_B * C_D
7751 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7752 // dst_lo: [ low(B*D) ]
7753 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7754
7755 if (isR6) {
7756 __ MulR6(TMP, lhs_high, rhs_low);
7757 __ MulR6(dst_high, lhs_low, rhs_high);
7758 __ Addu(dst_high, dst_high, TMP);
7759 __ MuhuR6(TMP, lhs_low, rhs_low);
7760 __ Addu(dst_high, dst_high, TMP);
7761 __ MulR6(dst_low, lhs_low, rhs_low);
7762 } else {
7763 __ MulR2(TMP, lhs_high, rhs_low);
7764 __ MulR2(dst_high, lhs_low, rhs_high);
7765 __ Addu(dst_high, dst_high, TMP);
7766 __ MultuR2(lhs_low, rhs_low);
7767 __ Mfhi(TMP);
7768 __ Addu(dst_high, dst_high, TMP);
7769 __ Mflo(dst_low);
7770 }
7771 break;
7772 }
7773 case Primitive::kPrimFloat:
7774 case Primitive::kPrimDouble: {
7775 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7776 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7777 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7778 if (type == Primitive::kPrimFloat) {
7779 __ MulS(dst, lhs, rhs);
7780 } else {
7781 __ MulD(dst, lhs, rhs);
7782 }
7783 break;
7784 }
7785 default:
7786 LOG(FATAL) << "Unexpected mul type " << type;
7787 }
7788}
7789
7790void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7791 LocationSummary* locations =
7792 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7793 switch (neg->GetResultType()) {
7794 case Primitive::kPrimInt:
7795 case Primitive::kPrimLong:
7796 locations->SetInAt(0, Location::RequiresRegister());
7797 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7798 break;
7799
7800 case Primitive::kPrimFloat:
7801 case Primitive::kPrimDouble:
7802 locations->SetInAt(0, Location::RequiresFpuRegister());
7803 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7804 break;
7805
7806 default:
7807 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7808 }
7809}
7810
7811void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7812 Primitive::Type type = instruction->GetType();
7813 LocationSummary* locations = instruction->GetLocations();
7814
7815 switch (type) {
7816 case Primitive::kPrimInt: {
7817 Register dst = locations->Out().AsRegister<Register>();
7818 Register src = locations->InAt(0).AsRegister<Register>();
7819 __ Subu(dst, ZERO, src);
7820 break;
7821 }
7822 case Primitive::kPrimLong: {
7823 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7824 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7825 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7826 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7827 __ Subu(dst_low, ZERO, src_low);
7828 __ Sltu(TMP, ZERO, dst_low);
7829 __ Subu(dst_high, ZERO, src_high);
7830 __ Subu(dst_high, dst_high, TMP);
7831 break;
7832 }
7833 case Primitive::kPrimFloat:
7834 case Primitive::kPrimDouble: {
7835 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7836 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7837 if (type == Primitive::kPrimFloat) {
7838 __ NegS(dst, src);
7839 } else {
7840 __ NegD(dst, src);
7841 }
7842 break;
7843 }
7844 default:
7845 LOG(FATAL) << "Unexpected neg type " << type;
7846 }
7847}
7848
7849void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7850 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007851 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007852 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007853 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007854 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7855 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007856}
7857
7858void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007859 // Note: if heap poisoning is enabled, the entry point takes care
7860 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007861 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7862 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007863}
7864
7865void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7866 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007868 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007869 if (instruction->IsStringAlloc()) {
7870 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7871 } else {
7872 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007873 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007874 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7875}
7876
7877void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007878 // Note: if heap poisoning is enabled, the entry point takes care
7879 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007880 if (instruction->IsStringAlloc()) {
7881 // String is allocated through StringFactory. Call NewEmptyString entry point.
7882 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007883 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007884 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7885 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7886 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007887 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007888 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7889 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007890 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007891 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007892 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007893}
7894
7895void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7897 locations->SetInAt(0, Location::RequiresRegister());
7898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7899}
7900
7901void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7902 Primitive::Type type = instruction->GetType();
7903 LocationSummary* locations = instruction->GetLocations();
7904
7905 switch (type) {
7906 case Primitive::kPrimInt: {
7907 Register dst = locations->Out().AsRegister<Register>();
7908 Register src = locations->InAt(0).AsRegister<Register>();
7909 __ Nor(dst, src, ZERO);
7910 break;
7911 }
7912
7913 case Primitive::kPrimLong: {
7914 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7915 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7916 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7917 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7918 __ Nor(dst_high, src_high, ZERO);
7919 __ Nor(dst_low, src_low, ZERO);
7920 break;
7921 }
7922
7923 default:
7924 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7925 }
7926}
7927
7928void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7930 locations->SetInAt(0, Location::RequiresRegister());
7931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7932}
7933
7934void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7935 LocationSummary* locations = instruction->GetLocations();
7936 __ Xori(locations->Out().AsRegister<Register>(),
7937 locations->InAt(0).AsRegister<Register>(),
7938 1);
7939}
7940
7941void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007942 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7943 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007944}
7945
Calin Juravle2ae48182016-03-16 14:05:09 +00007946void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7947 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007948 return;
7949 }
7950 Location obj = instruction->GetLocations()->InAt(0);
7951
7952 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007953 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007954}
7955
Calin Juravle2ae48182016-03-16 14:05:09 +00007956void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007957 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007958 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007959
7960 Location obj = instruction->GetLocations()->InAt(0);
7961
7962 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7963}
7964
7965void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007966 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007967}
7968
7969void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7970 HandleBinaryOp(instruction);
7971}
7972
7973void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7974 HandleBinaryOp(instruction);
7975}
7976
7977void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7978 LOG(FATAL) << "Unreachable";
7979}
7980
7981void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7982 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7983}
7984
7985void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7986 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7987 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7988 if (location.IsStackSlot()) {
7989 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7990 } else if (location.IsDoubleStackSlot()) {
7991 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7992 }
7993 locations->SetOut(location);
7994}
7995
7996void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7997 ATTRIBUTE_UNUSED) {
7998 // Nothing to do, the parameter is already at its location.
7999}
8000
8001void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8002 LocationSummary* locations =
8003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8004 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8005}
8006
8007void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8008 ATTRIBUTE_UNUSED) {
8009 // Nothing to do, the method is already at its location.
8010}
8011
8012void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8013 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008014 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008015 locations->SetInAt(i, Location::Any());
8016 }
8017 locations->SetOut(Location::Any());
8018}
8019
8020void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8021 LOG(FATAL) << "Unreachable";
8022}
8023
8024void LocationsBuilderMIPS::VisitRem(HRem* rem) {
8025 Primitive::Type type = rem->GetResultType();
8026 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008027 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8029
8030 switch (type) {
8031 case Primitive::kPrimInt:
8032 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008033 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8035 break;
8036
8037 case Primitive::kPrimLong: {
8038 InvokeRuntimeCallingConvention calling_convention;
8039 locations->SetInAt(0, Location::RegisterPairLocation(
8040 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8041 locations->SetInAt(1, Location::RegisterPairLocation(
8042 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8043 locations->SetOut(calling_convention.GetReturnLocation(type));
8044 break;
8045 }
8046
8047 case Primitive::kPrimFloat:
8048 case Primitive::kPrimDouble: {
8049 InvokeRuntimeCallingConvention calling_convention;
8050 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8051 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8052 locations->SetOut(calling_convention.GetReturnLocation(type));
8053 break;
8054 }
8055
8056 default:
8057 LOG(FATAL) << "Unexpected rem type " << type;
8058 }
8059}
8060
8061void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
8062 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008063
8064 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08008065 case Primitive::kPrimInt:
8066 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008067 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008068 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008069 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008070 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8071 break;
8072 }
8073 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008074 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008075 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008076 break;
8077 }
8078 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008079 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008080 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008081 break;
8082 }
8083 default:
8084 LOG(FATAL) << "Unexpected rem type " << type;
8085 }
8086}
8087
Igor Murashkind01745e2017-04-05 16:40:31 -07008088void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8089 constructor_fence->SetLocations(nullptr);
8090}
8091
8092void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8093 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8094 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8095}
8096
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008097void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8098 memory_barrier->SetLocations(nullptr);
8099}
8100
8101void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8102 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8103}
8104
8105void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8107 Primitive::Type return_type = ret->InputAt(0)->GetType();
8108 locations->SetInAt(0, MipsReturnLocation(return_type));
8109}
8110
8111void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8112 codegen_->GenerateFrameExit();
8113}
8114
8115void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8116 ret->SetLocations(nullptr);
8117}
8118
8119void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8120 codegen_->GenerateFrameExit();
8121}
8122
Alexey Frunze92d90602015-12-18 18:16:36 -08008123void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8124 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008125}
8126
Alexey Frunze92d90602015-12-18 18:16:36 -08008127void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8128 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008129}
8130
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008131void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8132 HandleShift(shl);
8133}
8134
8135void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8136 HandleShift(shl);
8137}
8138
8139void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8140 HandleShift(shr);
8141}
8142
8143void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8144 HandleShift(shr);
8145}
8146
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008147void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8148 HandleBinaryOp(instruction);
8149}
8150
8151void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8152 HandleBinaryOp(instruction);
8153}
8154
8155void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8156 HandleFieldGet(instruction, instruction->GetFieldInfo());
8157}
8158
8159void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8160 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8161}
8162
8163void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8164 HandleFieldSet(instruction, instruction->GetFieldInfo());
8165}
8166
8167void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008168 HandleFieldSet(instruction,
8169 instruction->GetFieldInfo(),
8170 instruction->GetDexPc(),
8171 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008172}
8173
8174void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8175 HUnresolvedInstanceFieldGet* instruction) {
8176 FieldAccessCallingConventionMIPS calling_convention;
8177 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8178 instruction->GetFieldType(),
8179 calling_convention);
8180}
8181
8182void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8183 HUnresolvedInstanceFieldGet* instruction) {
8184 FieldAccessCallingConventionMIPS calling_convention;
8185 codegen_->GenerateUnresolvedFieldAccess(instruction,
8186 instruction->GetFieldType(),
8187 instruction->GetFieldIndex(),
8188 instruction->GetDexPc(),
8189 calling_convention);
8190}
8191
8192void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8193 HUnresolvedInstanceFieldSet* instruction) {
8194 FieldAccessCallingConventionMIPS calling_convention;
8195 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8196 instruction->GetFieldType(),
8197 calling_convention);
8198}
8199
8200void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8201 HUnresolvedInstanceFieldSet* instruction) {
8202 FieldAccessCallingConventionMIPS calling_convention;
8203 codegen_->GenerateUnresolvedFieldAccess(instruction,
8204 instruction->GetFieldType(),
8205 instruction->GetFieldIndex(),
8206 instruction->GetDexPc(),
8207 calling_convention);
8208}
8209
8210void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8211 HUnresolvedStaticFieldGet* instruction) {
8212 FieldAccessCallingConventionMIPS calling_convention;
8213 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8214 instruction->GetFieldType(),
8215 calling_convention);
8216}
8217
8218void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8219 HUnresolvedStaticFieldGet* instruction) {
8220 FieldAccessCallingConventionMIPS calling_convention;
8221 codegen_->GenerateUnresolvedFieldAccess(instruction,
8222 instruction->GetFieldType(),
8223 instruction->GetFieldIndex(),
8224 instruction->GetDexPc(),
8225 calling_convention);
8226}
8227
8228void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8229 HUnresolvedStaticFieldSet* instruction) {
8230 FieldAccessCallingConventionMIPS calling_convention;
8231 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8232 instruction->GetFieldType(),
8233 calling_convention);
8234}
8235
8236void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8237 HUnresolvedStaticFieldSet* instruction) {
8238 FieldAccessCallingConventionMIPS calling_convention;
8239 codegen_->GenerateUnresolvedFieldAccess(instruction,
8240 instruction->GetFieldType(),
8241 instruction->GetFieldIndex(),
8242 instruction->GetDexPc(),
8243 calling_convention);
8244}
8245
8246void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008247 LocationSummary* locations =
8248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01008249 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008250}
8251
8252void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8253 HBasicBlock* block = instruction->GetBlock();
8254 if (block->GetLoopInformation() != nullptr) {
8255 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8256 // The back edge will generate the suspend check.
8257 return;
8258 }
8259 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8260 // The goto will generate the suspend check.
8261 return;
8262 }
8263 GenerateSuspendCheck(instruction, nullptr);
8264}
8265
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008266void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8267 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008269 InvokeRuntimeCallingConvention calling_convention;
8270 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8271}
8272
8273void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008274 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008275 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8276}
8277
8278void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8279 Primitive::Type input_type = conversion->GetInputType();
8280 Primitive::Type result_type = conversion->GetResultType();
8281 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008282 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008283
8284 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8285 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8286 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8287 }
8288
8289 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008290 if (!isR6 &&
8291 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8292 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008293 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008294 }
8295
8296 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8297
8298 if (call_kind == LocationSummary::kNoCall) {
8299 if (Primitive::IsFloatingPointType(input_type)) {
8300 locations->SetInAt(0, Location::RequiresFpuRegister());
8301 } else {
8302 locations->SetInAt(0, Location::RequiresRegister());
8303 }
8304
8305 if (Primitive::IsFloatingPointType(result_type)) {
8306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8307 } else {
8308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8309 }
8310 } else {
8311 InvokeRuntimeCallingConvention calling_convention;
8312
8313 if (Primitive::IsFloatingPointType(input_type)) {
8314 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8315 } else {
8316 DCHECK_EQ(input_type, Primitive::kPrimLong);
8317 locations->SetInAt(0, Location::RegisterPairLocation(
8318 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8319 }
8320
8321 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8322 }
8323}
8324
8325void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8326 LocationSummary* locations = conversion->GetLocations();
8327 Primitive::Type result_type = conversion->GetResultType();
8328 Primitive::Type input_type = conversion->GetInputType();
8329 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008330 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331
8332 DCHECK_NE(input_type, result_type);
8333
8334 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8335 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8336 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8337 Register src = locations->InAt(0).AsRegister<Register>();
8338
Alexey Frunzea871ef12016-06-27 15:20:11 -07008339 if (dst_low != src) {
8340 __ Move(dst_low, src);
8341 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342 __ Sra(dst_high, src, 31);
8343 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8344 Register dst = locations->Out().AsRegister<Register>();
8345 Register src = (input_type == Primitive::kPrimLong)
8346 ? locations->InAt(0).AsRegisterPairLow<Register>()
8347 : locations->InAt(0).AsRegister<Register>();
8348
8349 switch (result_type) {
8350 case Primitive::kPrimChar:
8351 __ Andi(dst, src, 0xFFFF);
8352 break;
8353 case Primitive::kPrimByte:
8354 if (has_sign_extension) {
8355 __ Seb(dst, src);
8356 } else {
8357 __ Sll(dst, src, 24);
8358 __ Sra(dst, dst, 24);
8359 }
8360 break;
8361 case Primitive::kPrimShort:
8362 if (has_sign_extension) {
8363 __ Seh(dst, src);
8364 } else {
8365 __ Sll(dst, src, 16);
8366 __ Sra(dst, dst, 16);
8367 }
8368 break;
8369 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008370 if (dst != src) {
8371 __ Move(dst, src);
8372 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008373 break;
8374
8375 default:
8376 LOG(FATAL) << "Unexpected type conversion from " << input_type
8377 << " to " << result_type;
8378 }
8379 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008380 if (input_type == Primitive::kPrimLong) {
8381 if (isR6) {
8382 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8383 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8384 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8385 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8386 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8387 __ Mtc1(src_low, FTMP);
8388 __ Mthc1(src_high, FTMP);
8389 if (result_type == Primitive::kPrimFloat) {
8390 __ Cvtsl(dst, FTMP);
8391 } else {
8392 __ Cvtdl(dst, FTMP);
8393 }
8394 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008395 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8396 : kQuickL2d;
8397 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008398 if (result_type == Primitive::kPrimFloat) {
8399 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8400 } else {
8401 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8402 }
8403 }
8404 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008405 Register src = locations->InAt(0).AsRegister<Register>();
8406 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8407 __ Mtc1(src, FTMP);
8408 if (result_type == Primitive::kPrimFloat) {
8409 __ Cvtsw(dst, FTMP);
8410 } else {
8411 __ Cvtdw(dst, FTMP);
8412 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008413 }
8414 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8415 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008416 if (result_type == Primitive::kPrimLong) {
8417 if (isR6) {
8418 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8419 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8420 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8421 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8422 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8423 MipsLabel truncate;
8424 MipsLabel done;
8425
8426 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8427 // value when the input is either a NaN or is outside of the range of the output type
8428 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8429 // the same result.
8430 //
8431 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8432 // value of the output type if the input is outside of the range after the truncation or
8433 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8434 // results. This matches the desired float/double-to-int/long conversion exactly.
8435 //
8436 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
8437 //
8438 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8439 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8440 // even though it must be NAN2008=1 on R6.
8441 //
8442 // The code takes care of the different behaviors by first comparing the input to the
8443 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8444 // If the input is greater than or equal to the minimum, it procedes to the truncate
8445 // instruction, which will handle such an input the same way irrespective of NAN2008.
8446 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8447 // in order to return either zero or the minimum value.
8448 //
8449 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8450 // truncate instruction for MIPS64R6.
8451 if (input_type == Primitive::kPrimFloat) {
8452 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
8453 __ LoadConst32(TMP, min_val);
8454 __ Mtc1(TMP, FTMP);
8455 __ CmpLeS(FTMP, FTMP, src);
8456 } else {
8457 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
8458 __ LoadConst32(TMP, High32Bits(min_val));
8459 __ Mtc1(ZERO, FTMP);
8460 __ Mthc1(TMP, FTMP);
8461 __ CmpLeD(FTMP, FTMP, src);
8462 }
8463
8464 __ Bc1nez(FTMP, &truncate);
8465
8466 if (input_type == Primitive::kPrimFloat) {
8467 __ CmpEqS(FTMP, src, src);
8468 } else {
8469 __ CmpEqD(FTMP, src, src);
8470 }
8471 __ Move(dst_low, ZERO);
8472 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
8473 __ Mfc1(TMP, FTMP);
8474 __ And(dst_high, dst_high, TMP);
8475
8476 __ B(&done);
8477
8478 __ Bind(&truncate);
8479
8480 if (input_type == Primitive::kPrimFloat) {
8481 __ TruncLS(FTMP, src);
8482 } else {
8483 __ TruncLD(FTMP, src);
8484 }
8485 __ Mfc1(dst_low, FTMP);
8486 __ Mfhc1(dst_high, FTMP);
8487
8488 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008489 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008490 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8491 : kQuickD2l;
8492 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008493 if (input_type == Primitive::kPrimFloat) {
8494 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8495 } else {
8496 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8497 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008498 }
8499 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008500 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8501 Register dst = locations->Out().AsRegister<Register>();
8502 MipsLabel truncate;
8503 MipsLabel done;
8504
8505 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8506 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8507 // even though it must be NAN2008=1 on R6.
8508 //
8509 // For details see the large comment above for the truncation of float/double to long on R6.
8510 //
8511 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8512 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008513 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008514 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8515 __ LoadConst32(TMP, min_val);
8516 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008517 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008518 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8519 __ LoadConst32(TMP, High32Bits(min_val));
8520 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07008521 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008522 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008523
8524 if (isR6) {
8525 if (input_type == Primitive::kPrimFloat) {
8526 __ CmpLeS(FTMP, FTMP, src);
8527 } else {
8528 __ CmpLeD(FTMP, FTMP, src);
8529 }
8530 __ Bc1nez(FTMP, &truncate);
8531
8532 if (input_type == Primitive::kPrimFloat) {
8533 __ CmpEqS(FTMP, src, src);
8534 } else {
8535 __ CmpEqD(FTMP, src, src);
8536 }
8537 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8538 __ Mfc1(TMP, FTMP);
8539 __ And(dst, dst, TMP);
8540 } else {
8541 if (input_type == Primitive::kPrimFloat) {
8542 __ ColeS(0, FTMP, src);
8543 } else {
8544 __ ColeD(0, FTMP, src);
8545 }
8546 __ Bc1t(0, &truncate);
8547
8548 if (input_type == Primitive::kPrimFloat) {
8549 __ CeqS(0, src, src);
8550 } else {
8551 __ CeqD(0, src, src);
8552 }
8553 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8554 __ Movf(dst, ZERO, 0);
8555 }
8556
8557 __ B(&done);
8558
8559 __ Bind(&truncate);
8560
8561 if (input_type == Primitive::kPrimFloat) {
8562 __ TruncWS(FTMP, src);
8563 } else {
8564 __ TruncWD(FTMP, src);
8565 }
8566 __ Mfc1(dst, FTMP);
8567
8568 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008569 }
8570 } else if (Primitive::IsFloatingPointType(result_type) &&
8571 Primitive::IsFloatingPointType(input_type)) {
8572 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8573 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8574 if (result_type == Primitive::kPrimFloat) {
8575 __ Cvtsd(dst, src);
8576 } else {
8577 __ Cvtds(dst, src);
8578 }
8579 } else {
8580 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8581 << " to " << result_type;
8582 }
8583}
8584
8585void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8586 HandleShift(ushr);
8587}
8588
8589void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8590 HandleShift(ushr);
8591}
8592
8593void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8594 HandleBinaryOp(instruction);
8595}
8596
8597void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8598 HandleBinaryOp(instruction);
8599}
8600
8601void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8602 // Nothing to do, this should be removed during prepare for register allocator.
8603 LOG(FATAL) << "Unreachable";
8604}
8605
8606void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8607 // Nothing to do, this should be removed during prepare for register allocator.
8608 LOG(FATAL) << "Unreachable";
8609}
8610
8611void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008612 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008613}
8614
8615void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008616 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008617}
8618
8619void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008620 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008621}
8622
8623void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008624 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008625}
8626
8627void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008628 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008629}
8630
8631void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008632 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008633}
8634
8635void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008636 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008637}
8638
8639void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008640 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008641}
8642
8643void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008644 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008645}
8646
8647void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008648 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008649}
8650
8651void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008652 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008653}
8654
8655void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008656 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008657}
8658
8659void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008660 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008661}
8662
8663void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008664 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008665}
8666
8667void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008668 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008669}
8670
8671void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008672 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008673}
8674
8675void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008676 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008677}
8678
8679void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008680 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008681}
8682
8683void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008684 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008685}
8686
8687void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008688 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008689}
8690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008691void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8692 LocationSummary* locations =
8693 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8694 locations->SetInAt(0, Location::RequiresRegister());
8695}
8696
Alexey Frunze96b66822016-09-10 02:32:44 -07008697void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8698 int32_t lower_bound,
8699 uint32_t num_entries,
8700 HBasicBlock* switch_block,
8701 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008702 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008703 Register temp_reg = TMP;
8704 __ Addiu32(temp_reg, value_reg, -lower_bound);
8705 // Jump to default if index is negative
8706 // Note: We don't check the case that index is positive while value < lower_bound, because in
8707 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8708 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8709
Alexey Frunze96b66822016-09-10 02:32:44 -07008710 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008711 // Jump to successors[0] if value == lower_bound.
8712 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8713 int32_t last_index = 0;
8714 for (; num_entries - last_index > 2; last_index += 2) {
8715 __ Addiu(temp_reg, temp_reg, -2);
8716 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8717 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8718 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8719 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8720 }
8721 if (num_entries - last_index == 2) {
8722 // The last missing case_value.
8723 __ Addiu(temp_reg, temp_reg, -1);
8724 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725 }
8726
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008727 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008728 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008729 __ B(codegen_->GetLabelOf(default_block));
8730 }
8731}
8732
Alexey Frunze96b66822016-09-10 02:32:44 -07008733void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8734 Register constant_area,
8735 int32_t lower_bound,
8736 uint32_t num_entries,
8737 HBasicBlock* switch_block,
8738 HBasicBlock* default_block) {
8739 // Create a jump table.
8740 std::vector<MipsLabel*> labels(num_entries);
8741 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8742 for (uint32_t i = 0; i < num_entries; i++) {
8743 labels[i] = codegen_->GetLabelOf(successors[i]);
8744 }
8745 JumpTable* table = __ CreateJumpTable(std::move(labels));
8746
8747 // Is the value in range?
8748 __ Addiu32(TMP, value_reg, -lower_bound);
8749 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8750 __ Sltiu(AT, TMP, num_entries);
8751 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8752 } else {
8753 __ LoadConst32(AT, num_entries);
8754 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8755 }
8756
8757 // We are in the range of the table.
8758 // Load the target address from the jump table, indexing by the value.
8759 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008760 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008761 __ Lw(TMP, TMP, 0);
8762 // Compute the absolute target address by adding the table start address
8763 // (the table contains offsets to targets relative to its start).
8764 __ Addu(TMP, TMP, AT);
8765 // And jump.
8766 __ Jr(TMP);
8767 __ NopIfNoReordering();
8768}
8769
8770void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8771 int32_t lower_bound = switch_instr->GetStartValue();
8772 uint32_t num_entries = switch_instr->GetNumEntries();
8773 LocationSummary* locations = switch_instr->GetLocations();
8774 Register value_reg = locations->InAt(0).AsRegister<Register>();
8775 HBasicBlock* switch_block = switch_instr->GetBlock();
8776 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8777
8778 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8779 num_entries > kPackedSwitchJumpTableThreshold) {
8780 // R6 uses PC-relative addressing to access the jump table.
8781 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8782 // the jump table and it is implemented by changing HPackedSwitch to
8783 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8784 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8785 GenTableBasedPackedSwitch(value_reg,
8786 ZERO,
8787 lower_bound,
8788 num_entries,
8789 switch_block,
8790 default_block);
8791 } else {
8792 GenPackedSwitchWithCompares(value_reg,
8793 lower_bound,
8794 num_entries,
8795 switch_block,
8796 default_block);
8797 }
8798}
8799
8800void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8801 LocationSummary* locations =
8802 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8803 locations->SetInAt(0, Location::RequiresRegister());
8804 // Constant area pointer (HMipsComputeBaseMethodAddress).
8805 locations->SetInAt(1, Location::RequiresRegister());
8806}
8807
8808void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8809 int32_t lower_bound = switch_instr->GetStartValue();
8810 uint32_t num_entries = switch_instr->GetNumEntries();
8811 LocationSummary* locations = switch_instr->GetLocations();
8812 Register value_reg = locations->InAt(0).AsRegister<Register>();
8813 Register constant_area = locations->InAt(1).AsRegister<Register>();
8814 HBasicBlock* switch_block = switch_instr->GetBlock();
8815 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8816
8817 // This is an R2-only path. HPackedSwitch has been changed to
8818 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8819 // required to address the jump table relative to PC.
8820 GenTableBasedPackedSwitch(value_reg,
8821 constant_area,
8822 lower_bound,
8823 num_entries,
8824 switch_block,
8825 default_block);
8826}
8827
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008828void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8829 HMipsComputeBaseMethodAddress* insn) {
8830 LocationSummary* locations =
8831 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8832 locations->SetOut(Location::RequiresRegister());
8833}
8834
8835void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8836 HMipsComputeBaseMethodAddress* insn) {
8837 LocationSummary* locations = insn->GetLocations();
8838 Register reg = locations->Out().AsRegister<Register>();
8839
8840 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8841
8842 // Generate a dummy PC-relative call to obtain PC.
8843 __ Nal();
8844 // Grab the return address off RA.
8845 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008846 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008847
8848 // Remember this offset (the obtained PC value) for later use with constant area.
8849 __ BindPcRelBaseLabel();
8850}
8851
8852void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8854 locations->SetOut(Location::RequiresRegister());
8855}
8856
8857void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8858 Register reg = base->GetLocations()->Out().AsRegister<Register>();
8859 CodeGeneratorMIPS::PcRelativePatchInfo* info =
8860 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008861 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8862 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008863 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008864 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
8865 __ Addiu(reg, reg, /* placeholder */ 0x5678);
8866 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008867}
8868
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008869void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8870 // The trampoline uses the same calling convention as dex calling conventions,
8871 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8872 // the method_idx.
8873 HandleInvoke(invoke);
8874}
8875
8876void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8877 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8878}
8879
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008880void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8881 LocationSummary* locations =
8882 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8883 locations->SetInAt(0, Location::RequiresRegister());
8884 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008885}
8886
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008887void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8888 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008889 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008890 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008891 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008892 __ LoadFromOffset(kLoadWord,
8893 locations->Out().AsRegister<Register>(),
8894 locations->InAt(0).AsRegister<Register>(),
8895 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008896 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008897 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008898 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008899 __ LoadFromOffset(kLoadWord,
8900 locations->Out().AsRegister<Register>(),
8901 locations->InAt(0).AsRegister<Register>(),
8902 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008903 __ LoadFromOffset(kLoadWord,
8904 locations->Out().AsRegister<Register>(),
8905 locations->Out().AsRegister<Register>(),
8906 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008907 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008908}
8909
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008910#undef __
8911#undef QUICK_ENTRY_POINT
8912
8913} // namespace mips
8914} // namespace art