blob: 3a7108b52e9da95e015a0a6f0ece43be5703fa0a [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();
3490 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003491 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003492
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003493 switch (type) {
3494 default:
3495 // Integer case.
3496 GenerateIntCompare(instruction->GetCondition(), locations);
3497 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003498
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003499 case Primitive::kPrimLong:
3500 // TODO: don't use branches.
3501 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003502 break;
3503
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003504 case Primitive::kPrimFloat:
3505 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003506 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3507 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003508 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003509
3510 // Convert the branches into the result.
3511 MipsLabel done;
3512
3513 // False case: result = 0.
3514 __ LoadConst32(dst, 0);
3515 __ B(&done);
3516
3517 // True case: result = 1.
3518 __ Bind(&true_label);
3519 __ LoadConst32(dst, 1);
3520 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003521}
3522
Alexey Frunze7e99e052015-11-24 19:28:01 -08003523void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3524 DCHECK(instruction->IsDiv() || instruction->IsRem());
3525 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3526
3527 LocationSummary* locations = instruction->GetLocations();
3528 Location second = locations->InAt(1);
3529 DCHECK(second.IsConstant());
3530
3531 Register out = locations->Out().AsRegister<Register>();
3532 Register dividend = locations->InAt(0).AsRegister<Register>();
3533 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3534 DCHECK(imm == 1 || imm == -1);
3535
3536 if (instruction->IsRem()) {
3537 __ Move(out, ZERO);
3538 } else {
3539 if (imm == -1) {
3540 __ Subu(out, ZERO, dividend);
3541 } else if (out != dividend) {
3542 __ Move(out, dividend);
3543 }
3544 }
3545}
3546
3547void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3548 DCHECK(instruction->IsDiv() || instruction->IsRem());
3549 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3550
3551 LocationSummary* locations = instruction->GetLocations();
3552 Location second = locations->InAt(1);
3553 DCHECK(second.IsConstant());
3554
3555 Register out = locations->Out().AsRegister<Register>();
3556 Register dividend = locations->InAt(0).AsRegister<Register>();
3557 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003558 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003559 int ctz_imm = CTZ(abs_imm);
3560
3561 if (instruction->IsDiv()) {
3562 if (ctz_imm == 1) {
3563 // Fast path for division by +/-2, which is very common.
3564 __ Srl(TMP, dividend, 31);
3565 } else {
3566 __ Sra(TMP, dividend, 31);
3567 __ Srl(TMP, TMP, 32 - ctz_imm);
3568 }
3569 __ Addu(out, dividend, TMP);
3570 __ Sra(out, out, ctz_imm);
3571 if (imm < 0) {
3572 __ Subu(out, ZERO, out);
3573 }
3574 } else {
3575 if (ctz_imm == 1) {
3576 // Fast path for modulo +/-2, which is very common.
3577 __ Sra(TMP, dividend, 31);
3578 __ Subu(out, dividend, TMP);
3579 __ Andi(out, out, 1);
3580 __ Addu(out, out, TMP);
3581 } else {
3582 __ Sra(TMP, dividend, 31);
3583 __ Srl(TMP, TMP, 32 - ctz_imm);
3584 __ Addu(out, dividend, TMP);
3585 if (IsUint<16>(abs_imm - 1)) {
3586 __ Andi(out, out, abs_imm - 1);
3587 } else {
3588 __ Sll(out, out, 32 - ctz_imm);
3589 __ Srl(out, out, 32 - ctz_imm);
3590 }
3591 __ Subu(out, out, TMP);
3592 }
3593 }
3594}
3595
3596void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3597 DCHECK(instruction->IsDiv() || instruction->IsRem());
3598 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3599
3600 LocationSummary* locations = instruction->GetLocations();
3601 Location second = locations->InAt(1);
3602 DCHECK(second.IsConstant());
3603
3604 Register out = locations->Out().AsRegister<Register>();
3605 Register dividend = locations->InAt(0).AsRegister<Register>();
3606 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3607
3608 int64_t magic;
3609 int shift;
3610 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3611
3612 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3613
3614 __ LoadConst32(TMP, magic);
3615 if (isR6) {
3616 __ MuhR6(TMP, dividend, TMP);
3617 } else {
3618 __ MultR2(dividend, TMP);
3619 __ Mfhi(TMP);
3620 }
3621 if (imm > 0 && magic < 0) {
3622 __ Addu(TMP, TMP, dividend);
3623 } else if (imm < 0 && magic > 0) {
3624 __ Subu(TMP, TMP, dividend);
3625 }
3626
3627 if (shift != 0) {
3628 __ Sra(TMP, TMP, shift);
3629 }
3630
3631 if (instruction->IsDiv()) {
3632 __ Sra(out, TMP, 31);
3633 __ Subu(out, TMP, out);
3634 } else {
3635 __ Sra(AT, TMP, 31);
3636 __ Subu(AT, TMP, AT);
3637 __ LoadConst32(TMP, imm);
3638 if (isR6) {
3639 __ MulR6(TMP, AT, TMP);
3640 } else {
3641 __ MulR2(TMP, AT, TMP);
3642 }
3643 __ Subu(out, dividend, TMP);
3644 }
3645}
3646
3647void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3648 DCHECK(instruction->IsDiv() || instruction->IsRem());
3649 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3650
3651 LocationSummary* locations = instruction->GetLocations();
3652 Register out = locations->Out().AsRegister<Register>();
3653 Location second = locations->InAt(1);
3654
3655 if (second.IsConstant()) {
3656 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3657 if (imm == 0) {
3658 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3659 } else if (imm == 1 || imm == -1) {
3660 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003661 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003662 DivRemByPowerOfTwo(instruction);
3663 } else {
3664 DCHECK(imm <= -2 || imm >= 2);
3665 GenerateDivRemWithAnyConstant(instruction);
3666 }
3667 } else {
3668 Register dividend = locations->InAt(0).AsRegister<Register>();
3669 Register divisor = second.AsRegister<Register>();
3670 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3671 if (instruction->IsDiv()) {
3672 if (isR6) {
3673 __ DivR6(out, dividend, divisor);
3674 } else {
3675 __ DivR2(out, dividend, divisor);
3676 }
3677 } else {
3678 if (isR6) {
3679 __ ModR6(out, dividend, divisor);
3680 } else {
3681 __ ModR2(out, dividend, divisor);
3682 }
3683 }
3684 }
3685}
3686
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3688 Primitive::Type type = div->GetResultType();
3689 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003690 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003691 : LocationSummary::kNoCall;
3692
3693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3694
3695 switch (type) {
3696 case Primitive::kPrimInt:
3697 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003698 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3700 break;
3701
3702 case Primitive::kPrimLong: {
3703 InvokeRuntimeCallingConvention calling_convention;
3704 locations->SetInAt(0, Location::RegisterPairLocation(
3705 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3706 locations->SetInAt(1, Location::RegisterPairLocation(
3707 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3708 locations->SetOut(calling_convention.GetReturnLocation(type));
3709 break;
3710 }
3711
3712 case Primitive::kPrimFloat:
3713 case Primitive::kPrimDouble:
3714 locations->SetInAt(0, Location::RequiresFpuRegister());
3715 locations->SetInAt(1, Location::RequiresFpuRegister());
3716 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3717 break;
3718
3719 default:
3720 LOG(FATAL) << "Unexpected div type " << type;
3721 }
3722}
3723
3724void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3725 Primitive::Type type = instruction->GetType();
3726 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003727
3728 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003729 case Primitive::kPrimInt:
3730 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003731 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003732 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003733 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003734 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3735 break;
3736 }
3737 case Primitive::kPrimFloat:
3738 case Primitive::kPrimDouble: {
3739 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3740 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3741 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3742 if (type == Primitive::kPrimFloat) {
3743 __ DivS(dst, lhs, rhs);
3744 } else {
3745 __ DivD(dst, lhs, rhs);
3746 }
3747 break;
3748 }
3749 default:
3750 LOG(FATAL) << "Unexpected div type " << type;
3751 }
3752}
3753
3754void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003755 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003756 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003757}
3758
3759void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3760 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3761 codegen_->AddSlowPath(slow_path);
3762 Location value = instruction->GetLocations()->InAt(0);
3763 Primitive::Type type = instruction->GetType();
3764
3765 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003766 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003767 case Primitive::kPrimByte:
3768 case Primitive::kPrimChar:
3769 case Primitive::kPrimShort:
3770 case Primitive::kPrimInt: {
3771 if (value.IsConstant()) {
3772 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3773 __ B(slow_path->GetEntryLabel());
3774 } else {
3775 // A division by a non-null constant is valid. We don't need to perform
3776 // any check, so simply fall through.
3777 }
3778 } else {
3779 DCHECK(value.IsRegister()) << value;
3780 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3781 }
3782 break;
3783 }
3784 case Primitive::kPrimLong: {
3785 if (value.IsConstant()) {
3786 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3787 __ B(slow_path->GetEntryLabel());
3788 } else {
3789 // A division by a non-null constant is valid. We don't need to perform
3790 // any check, so simply fall through.
3791 }
3792 } else {
3793 DCHECK(value.IsRegisterPair()) << value;
3794 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3795 __ Beqz(TMP, slow_path->GetEntryLabel());
3796 }
3797 break;
3798 }
3799 default:
3800 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3801 }
3802}
3803
3804void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3805 LocationSummary* locations =
3806 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3807 locations->SetOut(Location::ConstantLocation(constant));
3808}
3809
3810void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3811 // Will be generated at use site.
3812}
3813
3814void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3815 exit->SetLocations(nullptr);
3816}
3817
3818void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3819}
3820
3821void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3822 LocationSummary* locations =
3823 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3824 locations->SetOut(Location::ConstantLocation(constant));
3825}
3826
3827void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3828 // Will be generated at use site.
3829}
3830
3831void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3832 got->SetLocations(nullptr);
3833}
3834
3835void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3836 DCHECK(!successor->IsExitBlock());
3837 HBasicBlock* block = got->GetBlock();
3838 HInstruction* previous = got->GetPrevious();
3839 HLoopInformation* info = block->GetLoopInformation();
3840
3841 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3842 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3843 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3844 return;
3845 }
3846 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3847 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3848 }
3849 if (!codegen_->GoesToNextBlock(block, successor)) {
3850 __ B(codegen_->GetLabelOf(successor));
3851 }
3852}
3853
3854void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3855 HandleGoto(got, got->GetSuccessor());
3856}
3857
3858void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3859 try_boundary->SetLocations(nullptr);
3860}
3861
3862void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3863 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3864 if (!successor->IsExitBlock()) {
3865 HandleGoto(try_boundary, successor);
3866 }
3867}
3868
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003869void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3870 LocationSummary* locations) {
3871 Register dst = locations->Out().AsRegister<Register>();
3872 Register lhs = locations->InAt(0).AsRegister<Register>();
3873 Location rhs_location = locations->InAt(1);
3874 Register rhs_reg = ZERO;
3875 int64_t rhs_imm = 0;
3876 bool use_imm = rhs_location.IsConstant();
3877 if (use_imm) {
3878 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3879 } else {
3880 rhs_reg = rhs_location.AsRegister<Register>();
3881 }
3882
3883 switch (cond) {
3884 case kCondEQ:
3885 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003886 if (use_imm && IsInt<16>(-rhs_imm)) {
3887 if (rhs_imm == 0) {
3888 if (cond == kCondEQ) {
3889 __ Sltiu(dst, lhs, 1);
3890 } else {
3891 __ Sltu(dst, ZERO, lhs);
3892 }
3893 } else {
3894 __ Addiu(dst, lhs, -rhs_imm);
3895 if (cond == kCondEQ) {
3896 __ Sltiu(dst, dst, 1);
3897 } else {
3898 __ Sltu(dst, ZERO, dst);
3899 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003900 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003901 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003902 if (use_imm && IsUint<16>(rhs_imm)) {
3903 __ Xori(dst, lhs, rhs_imm);
3904 } else {
3905 if (use_imm) {
3906 rhs_reg = TMP;
3907 __ LoadConst32(rhs_reg, rhs_imm);
3908 }
3909 __ Xor(dst, lhs, rhs_reg);
3910 }
3911 if (cond == kCondEQ) {
3912 __ Sltiu(dst, dst, 1);
3913 } else {
3914 __ Sltu(dst, ZERO, dst);
3915 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003916 }
3917 break;
3918
3919 case kCondLT:
3920 case kCondGE:
3921 if (use_imm && IsInt<16>(rhs_imm)) {
3922 __ Slti(dst, lhs, rhs_imm);
3923 } else {
3924 if (use_imm) {
3925 rhs_reg = TMP;
3926 __ LoadConst32(rhs_reg, rhs_imm);
3927 }
3928 __ Slt(dst, lhs, rhs_reg);
3929 }
3930 if (cond == kCondGE) {
3931 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3932 // only the slt instruction but no sge.
3933 __ Xori(dst, dst, 1);
3934 }
3935 break;
3936
3937 case kCondLE:
3938 case kCondGT:
3939 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3940 // Simulate lhs <= rhs via lhs < rhs + 1.
3941 __ Slti(dst, lhs, rhs_imm + 1);
3942 if (cond == kCondGT) {
3943 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3944 // only the slti instruction but no sgti.
3945 __ Xori(dst, dst, 1);
3946 }
3947 } else {
3948 if (use_imm) {
3949 rhs_reg = TMP;
3950 __ LoadConst32(rhs_reg, rhs_imm);
3951 }
3952 __ Slt(dst, rhs_reg, lhs);
3953 if (cond == kCondLE) {
3954 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3955 // only the slt instruction but no sle.
3956 __ Xori(dst, dst, 1);
3957 }
3958 }
3959 break;
3960
3961 case kCondB:
3962 case kCondAE:
3963 if (use_imm && IsInt<16>(rhs_imm)) {
3964 // Sltiu sign-extends its 16-bit immediate operand before
3965 // the comparison and thus lets us compare directly with
3966 // unsigned values in the ranges [0, 0x7fff] and
3967 // [0xffff8000, 0xffffffff].
3968 __ Sltiu(dst, lhs, rhs_imm);
3969 } else {
3970 if (use_imm) {
3971 rhs_reg = TMP;
3972 __ LoadConst32(rhs_reg, rhs_imm);
3973 }
3974 __ Sltu(dst, lhs, rhs_reg);
3975 }
3976 if (cond == kCondAE) {
3977 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3978 // only the sltu instruction but no sgeu.
3979 __ Xori(dst, dst, 1);
3980 }
3981 break;
3982
3983 case kCondBE:
3984 case kCondA:
3985 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3986 // Simulate lhs <= rhs via lhs < rhs + 1.
3987 // Note that this only works if rhs + 1 does not overflow
3988 // to 0, hence the check above.
3989 // Sltiu sign-extends its 16-bit immediate operand before
3990 // the comparison and thus lets us compare directly with
3991 // unsigned values in the ranges [0, 0x7fff] and
3992 // [0xffff8000, 0xffffffff].
3993 __ Sltiu(dst, lhs, rhs_imm + 1);
3994 if (cond == kCondA) {
3995 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3996 // only the sltiu instruction but no sgtiu.
3997 __ Xori(dst, dst, 1);
3998 }
3999 } else {
4000 if (use_imm) {
4001 rhs_reg = TMP;
4002 __ LoadConst32(rhs_reg, rhs_imm);
4003 }
4004 __ Sltu(dst, rhs_reg, lhs);
4005 if (cond == kCondBE) {
4006 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4007 // only the sltu instruction but no sleu.
4008 __ Xori(dst, dst, 1);
4009 }
4010 }
4011 break;
4012 }
4013}
4014
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004015bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4016 LocationSummary* input_locations,
4017 Register dst) {
4018 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4019 Location rhs_location = input_locations->InAt(1);
4020 Register rhs_reg = ZERO;
4021 int64_t rhs_imm = 0;
4022 bool use_imm = rhs_location.IsConstant();
4023 if (use_imm) {
4024 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4025 } else {
4026 rhs_reg = rhs_location.AsRegister<Register>();
4027 }
4028
4029 switch (cond) {
4030 case kCondEQ:
4031 case kCondNE:
4032 if (use_imm && IsInt<16>(-rhs_imm)) {
4033 __ Addiu(dst, lhs, -rhs_imm);
4034 } else if (use_imm && IsUint<16>(rhs_imm)) {
4035 __ Xori(dst, lhs, rhs_imm);
4036 } else {
4037 if (use_imm) {
4038 rhs_reg = TMP;
4039 __ LoadConst32(rhs_reg, rhs_imm);
4040 }
4041 __ Xor(dst, lhs, rhs_reg);
4042 }
4043 return (cond == kCondEQ);
4044
4045 case kCondLT:
4046 case kCondGE:
4047 if (use_imm && IsInt<16>(rhs_imm)) {
4048 __ Slti(dst, lhs, rhs_imm);
4049 } else {
4050 if (use_imm) {
4051 rhs_reg = TMP;
4052 __ LoadConst32(rhs_reg, rhs_imm);
4053 }
4054 __ Slt(dst, lhs, rhs_reg);
4055 }
4056 return (cond == kCondGE);
4057
4058 case kCondLE:
4059 case kCondGT:
4060 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4061 // Simulate lhs <= rhs via lhs < rhs + 1.
4062 __ Slti(dst, lhs, rhs_imm + 1);
4063 return (cond == kCondGT);
4064 } else {
4065 if (use_imm) {
4066 rhs_reg = TMP;
4067 __ LoadConst32(rhs_reg, rhs_imm);
4068 }
4069 __ Slt(dst, rhs_reg, lhs);
4070 return (cond == kCondLE);
4071 }
4072
4073 case kCondB:
4074 case kCondAE:
4075 if (use_imm && IsInt<16>(rhs_imm)) {
4076 // Sltiu sign-extends its 16-bit immediate operand before
4077 // the comparison and thus lets us compare directly with
4078 // unsigned values in the ranges [0, 0x7fff] and
4079 // [0xffff8000, 0xffffffff].
4080 __ Sltiu(dst, lhs, rhs_imm);
4081 } else {
4082 if (use_imm) {
4083 rhs_reg = TMP;
4084 __ LoadConst32(rhs_reg, rhs_imm);
4085 }
4086 __ Sltu(dst, lhs, rhs_reg);
4087 }
4088 return (cond == kCondAE);
4089
4090 case kCondBE:
4091 case kCondA:
4092 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4093 // Simulate lhs <= rhs via lhs < rhs + 1.
4094 // Note that this only works if rhs + 1 does not overflow
4095 // to 0, hence the check above.
4096 // Sltiu sign-extends its 16-bit immediate operand before
4097 // the comparison and thus lets us compare directly with
4098 // unsigned values in the ranges [0, 0x7fff] and
4099 // [0xffff8000, 0xffffffff].
4100 __ Sltiu(dst, lhs, rhs_imm + 1);
4101 return (cond == kCondA);
4102 } else {
4103 if (use_imm) {
4104 rhs_reg = TMP;
4105 __ LoadConst32(rhs_reg, rhs_imm);
4106 }
4107 __ Sltu(dst, rhs_reg, lhs);
4108 return (cond == kCondBE);
4109 }
4110 }
4111}
4112
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004113void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4114 LocationSummary* locations,
4115 MipsLabel* label) {
4116 Register lhs = locations->InAt(0).AsRegister<Register>();
4117 Location rhs_location = locations->InAt(1);
4118 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004119 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004120 bool use_imm = rhs_location.IsConstant();
4121 if (use_imm) {
4122 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4123 } else {
4124 rhs_reg = rhs_location.AsRegister<Register>();
4125 }
4126
4127 if (use_imm && rhs_imm == 0) {
4128 switch (cond) {
4129 case kCondEQ:
4130 case kCondBE: // <= 0 if zero
4131 __ Beqz(lhs, label);
4132 break;
4133 case kCondNE:
4134 case kCondA: // > 0 if non-zero
4135 __ Bnez(lhs, label);
4136 break;
4137 case kCondLT:
4138 __ Bltz(lhs, label);
4139 break;
4140 case kCondGE:
4141 __ Bgez(lhs, label);
4142 break;
4143 case kCondLE:
4144 __ Blez(lhs, label);
4145 break;
4146 case kCondGT:
4147 __ Bgtz(lhs, label);
4148 break;
4149 case kCondB: // always false
4150 break;
4151 case kCondAE: // always true
4152 __ B(label);
4153 break;
4154 }
4155 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004156 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4157 if (isR6 || !use_imm) {
4158 if (use_imm) {
4159 rhs_reg = TMP;
4160 __ LoadConst32(rhs_reg, rhs_imm);
4161 }
4162 switch (cond) {
4163 case kCondEQ:
4164 __ Beq(lhs, rhs_reg, label);
4165 break;
4166 case kCondNE:
4167 __ Bne(lhs, rhs_reg, label);
4168 break;
4169 case kCondLT:
4170 __ Blt(lhs, rhs_reg, label);
4171 break;
4172 case kCondGE:
4173 __ Bge(lhs, rhs_reg, label);
4174 break;
4175 case kCondLE:
4176 __ Bge(rhs_reg, lhs, label);
4177 break;
4178 case kCondGT:
4179 __ Blt(rhs_reg, lhs, label);
4180 break;
4181 case kCondB:
4182 __ Bltu(lhs, rhs_reg, label);
4183 break;
4184 case kCondAE:
4185 __ Bgeu(lhs, rhs_reg, label);
4186 break;
4187 case kCondBE:
4188 __ Bgeu(rhs_reg, lhs, label);
4189 break;
4190 case kCondA:
4191 __ Bltu(rhs_reg, lhs, label);
4192 break;
4193 }
4194 } else {
4195 // Special cases for more efficient comparison with constants on R2.
4196 switch (cond) {
4197 case kCondEQ:
4198 __ LoadConst32(TMP, rhs_imm);
4199 __ Beq(lhs, TMP, label);
4200 break;
4201 case kCondNE:
4202 __ LoadConst32(TMP, rhs_imm);
4203 __ Bne(lhs, TMP, label);
4204 break;
4205 case kCondLT:
4206 if (IsInt<16>(rhs_imm)) {
4207 __ Slti(TMP, lhs, rhs_imm);
4208 __ Bnez(TMP, label);
4209 } else {
4210 __ LoadConst32(TMP, rhs_imm);
4211 __ Blt(lhs, TMP, label);
4212 }
4213 break;
4214 case kCondGE:
4215 if (IsInt<16>(rhs_imm)) {
4216 __ Slti(TMP, lhs, rhs_imm);
4217 __ Beqz(TMP, label);
4218 } else {
4219 __ LoadConst32(TMP, rhs_imm);
4220 __ Bge(lhs, TMP, label);
4221 }
4222 break;
4223 case kCondLE:
4224 if (IsInt<16>(rhs_imm + 1)) {
4225 // Simulate lhs <= rhs via lhs < rhs + 1.
4226 __ Slti(TMP, lhs, rhs_imm + 1);
4227 __ Bnez(TMP, label);
4228 } else {
4229 __ LoadConst32(TMP, rhs_imm);
4230 __ Bge(TMP, lhs, label);
4231 }
4232 break;
4233 case kCondGT:
4234 if (IsInt<16>(rhs_imm + 1)) {
4235 // Simulate lhs > rhs via !(lhs < rhs + 1).
4236 __ Slti(TMP, lhs, rhs_imm + 1);
4237 __ Beqz(TMP, label);
4238 } else {
4239 __ LoadConst32(TMP, rhs_imm);
4240 __ Blt(TMP, lhs, label);
4241 }
4242 break;
4243 case kCondB:
4244 if (IsInt<16>(rhs_imm)) {
4245 __ Sltiu(TMP, lhs, rhs_imm);
4246 __ Bnez(TMP, label);
4247 } else {
4248 __ LoadConst32(TMP, rhs_imm);
4249 __ Bltu(lhs, TMP, label);
4250 }
4251 break;
4252 case kCondAE:
4253 if (IsInt<16>(rhs_imm)) {
4254 __ Sltiu(TMP, lhs, rhs_imm);
4255 __ Beqz(TMP, label);
4256 } else {
4257 __ LoadConst32(TMP, rhs_imm);
4258 __ Bgeu(lhs, TMP, label);
4259 }
4260 break;
4261 case kCondBE:
4262 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4263 // Simulate lhs <= rhs via lhs < rhs + 1.
4264 // Note that this only works if rhs + 1 does not overflow
4265 // to 0, hence the check above.
4266 __ Sltiu(TMP, lhs, rhs_imm + 1);
4267 __ Bnez(TMP, label);
4268 } else {
4269 __ LoadConst32(TMP, rhs_imm);
4270 __ Bgeu(TMP, lhs, label);
4271 }
4272 break;
4273 case kCondA:
4274 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4275 // Simulate lhs > rhs via !(lhs < rhs + 1).
4276 // Note that this only works if rhs + 1 does not overflow
4277 // to 0, hence the check above.
4278 __ Sltiu(TMP, lhs, rhs_imm + 1);
4279 __ Beqz(TMP, label);
4280 } else {
4281 __ LoadConst32(TMP, rhs_imm);
4282 __ Bltu(TMP, lhs, label);
4283 }
4284 break;
4285 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004286 }
4287 }
4288}
4289
4290void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4291 LocationSummary* locations,
4292 MipsLabel* label) {
4293 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4294 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4295 Location rhs_location = locations->InAt(1);
4296 Register rhs_high = ZERO;
4297 Register rhs_low = ZERO;
4298 int64_t imm = 0;
4299 uint32_t imm_high = 0;
4300 uint32_t imm_low = 0;
4301 bool use_imm = rhs_location.IsConstant();
4302 if (use_imm) {
4303 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4304 imm_high = High32Bits(imm);
4305 imm_low = Low32Bits(imm);
4306 } else {
4307 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4308 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4309 }
4310
4311 if (use_imm && imm == 0) {
4312 switch (cond) {
4313 case kCondEQ:
4314 case kCondBE: // <= 0 if zero
4315 __ Or(TMP, lhs_high, lhs_low);
4316 __ Beqz(TMP, label);
4317 break;
4318 case kCondNE:
4319 case kCondA: // > 0 if non-zero
4320 __ Or(TMP, lhs_high, lhs_low);
4321 __ Bnez(TMP, label);
4322 break;
4323 case kCondLT:
4324 __ Bltz(lhs_high, label);
4325 break;
4326 case kCondGE:
4327 __ Bgez(lhs_high, label);
4328 break;
4329 case kCondLE:
4330 __ Or(TMP, lhs_high, lhs_low);
4331 __ Sra(AT, lhs_high, 31);
4332 __ Bgeu(AT, TMP, label);
4333 break;
4334 case kCondGT:
4335 __ Or(TMP, lhs_high, lhs_low);
4336 __ Sra(AT, lhs_high, 31);
4337 __ Bltu(AT, TMP, label);
4338 break;
4339 case kCondB: // always false
4340 break;
4341 case kCondAE: // always true
4342 __ B(label);
4343 break;
4344 }
4345 } else if (use_imm) {
4346 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4347 switch (cond) {
4348 case kCondEQ:
4349 __ LoadConst32(TMP, imm_high);
4350 __ Xor(TMP, TMP, lhs_high);
4351 __ LoadConst32(AT, imm_low);
4352 __ Xor(AT, AT, lhs_low);
4353 __ Or(TMP, TMP, AT);
4354 __ Beqz(TMP, label);
4355 break;
4356 case kCondNE:
4357 __ LoadConst32(TMP, imm_high);
4358 __ Xor(TMP, TMP, lhs_high);
4359 __ LoadConst32(AT, imm_low);
4360 __ Xor(AT, AT, lhs_low);
4361 __ Or(TMP, TMP, AT);
4362 __ Bnez(TMP, label);
4363 break;
4364 case kCondLT:
4365 __ LoadConst32(TMP, imm_high);
4366 __ Blt(lhs_high, TMP, label);
4367 __ Slt(TMP, TMP, lhs_high);
4368 __ LoadConst32(AT, imm_low);
4369 __ Sltu(AT, lhs_low, AT);
4370 __ Blt(TMP, AT, label);
4371 break;
4372 case kCondGE:
4373 __ LoadConst32(TMP, imm_high);
4374 __ Blt(TMP, lhs_high, label);
4375 __ Slt(TMP, lhs_high, TMP);
4376 __ LoadConst32(AT, imm_low);
4377 __ Sltu(AT, lhs_low, AT);
4378 __ Or(TMP, TMP, AT);
4379 __ Beqz(TMP, label);
4380 break;
4381 case kCondLE:
4382 __ LoadConst32(TMP, imm_high);
4383 __ Blt(lhs_high, TMP, label);
4384 __ Slt(TMP, TMP, lhs_high);
4385 __ LoadConst32(AT, imm_low);
4386 __ Sltu(AT, AT, lhs_low);
4387 __ Or(TMP, TMP, AT);
4388 __ Beqz(TMP, label);
4389 break;
4390 case kCondGT:
4391 __ LoadConst32(TMP, imm_high);
4392 __ Blt(TMP, lhs_high, label);
4393 __ Slt(TMP, lhs_high, TMP);
4394 __ LoadConst32(AT, imm_low);
4395 __ Sltu(AT, AT, lhs_low);
4396 __ Blt(TMP, AT, label);
4397 break;
4398 case kCondB:
4399 __ LoadConst32(TMP, imm_high);
4400 __ Bltu(lhs_high, TMP, label);
4401 __ Sltu(TMP, TMP, lhs_high);
4402 __ LoadConst32(AT, imm_low);
4403 __ Sltu(AT, lhs_low, AT);
4404 __ Blt(TMP, AT, label);
4405 break;
4406 case kCondAE:
4407 __ LoadConst32(TMP, imm_high);
4408 __ Bltu(TMP, lhs_high, label);
4409 __ Sltu(TMP, lhs_high, TMP);
4410 __ LoadConst32(AT, imm_low);
4411 __ Sltu(AT, lhs_low, AT);
4412 __ Or(TMP, TMP, AT);
4413 __ Beqz(TMP, label);
4414 break;
4415 case kCondBE:
4416 __ LoadConst32(TMP, imm_high);
4417 __ Bltu(lhs_high, TMP, label);
4418 __ Sltu(TMP, TMP, lhs_high);
4419 __ LoadConst32(AT, imm_low);
4420 __ Sltu(AT, AT, lhs_low);
4421 __ Or(TMP, TMP, AT);
4422 __ Beqz(TMP, label);
4423 break;
4424 case kCondA:
4425 __ LoadConst32(TMP, imm_high);
4426 __ Bltu(TMP, lhs_high, label);
4427 __ Sltu(TMP, lhs_high, TMP);
4428 __ LoadConst32(AT, imm_low);
4429 __ Sltu(AT, AT, lhs_low);
4430 __ Blt(TMP, AT, label);
4431 break;
4432 }
4433 } else {
4434 switch (cond) {
4435 case kCondEQ:
4436 __ Xor(TMP, lhs_high, rhs_high);
4437 __ Xor(AT, lhs_low, rhs_low);
4438 __ Or(TMP, TMP, AT);
4439 __ Beqz(TMP, label);
4440 break;
4441 case kCondNE:
4442 __ Xor(TMP, lhs_high, rhs_high);
4443 __ Xor(AT, lhs_low, rhs_low);
4444 __ Or(TMP, TMP, AT);
4445 __ Bnez(TMP, label);
4446 break;
4447 case kCondLT:
4448 __ Blt(lhs_high, rhs_high, label);
4449 __ Slt(TMP, rhs_high, lhs_high);
4450 __ Sltu(AT, lhs_low, rhs_low);
4451 __ Blt(TMP, AT, label);
4452 break;
4453 case kCondGE:
4454 __ Blt(rhs_high, lhs_high, label);
4455 __ Slt(TMP, lhs_high, rhs_high);
4456 __ Sltu(AT, lhs_low, rhs_low);
4457 __ Or(TMP, TMP, AT);
4458 __ Beqz(TMP, label);
4459 break;
4460 case kCondLE:
4461 __ Blt(lhs_high, rhs_high, label);
4462 __ Slt(TMP, rhs_high, lhs_high);
4463 __ Sltu(AT, rhs_low, lhs_low);
4464 __ Or(TMP, TMP, AT);
4465 __ Beqz(TMP, label);
4466 break;
4467 case kCondGT:
4468 __ Blt(rhs_high, lhs_high, label);
4469 __ Slt(TMP, lhs_high, rhs_high);
4470 __ Sltu(AT, rhs_low, lhs_low);
4471 __ Blt(TMP, AT, label);
4472 break;
4473 case kCondB:
4474 __ Bltu(lhs_high, rhs_high, label);
4475 __ Sltu(TMP, rhs_high, lhs_high);
4476 __ Sltu(AT, lhs_low, rhs_low);
4477 __ Blt(TMP, AT, label);
4478 break;
4479 case kCondAE:
4480 __ Bltu(rhs_high, lhs_high, label);
4481 __ Sltu(TMP, lhs_high, rhs_high);
4482 __ Sltu(AT, lhs_low, rhs_low);
4483 __ Or(TMP, TMP, AT);
4484 __ Beqz(TMP, label);
4485 break;
4486 case kCondBE:
4487 __ Bltu(lhs_high, rhs_high, label);
4488 __ Sltu(TMP, rhs_high, lhs_high);
4489 __ Sltu(AT, rhs_low, lhs_low);
4490 __ Or(TMP, TMP, AT);
4491 __ Beqz(TMP, label);
4492 break;
4493 case kCondA:
4494 __ Bltu(rhs_high, lhs_high, label);
4495 __ Sltu(TMP, lhs_high, rhs_high);
4496 __ Sltu(AT, rhs_low, lhs_low);
4497 __ Blt(TMP, AT, label);
4498 break;
4499 }
4500 }
4501}
4502
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004503void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4504 bool gt_bias,
4505 Primitive::Type type,
4506 LocationSummary* locations) {
4507 Register dst = locations->Out().AsRegister<Register>();
4508 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4509 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4510 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4511 if (type == Primitive::kPrimFloat) {
4512 if (isR6) {
4513 switch (cond) {
4514 case kCondEQ:
4515 __ CmpEqS(FTMP, lhs, rhs);
4516 __ Mfc1(dst, FTMP);
4517 __ Andi(dst, dst, 1);
4518 break;
4519 case kCondNE:
4520 __ CmpEqS(FTMP, lhs, rhs);
4521 __ Mfc1(dst, FTMP);
4522 __ Addiu(dst, dst, 1);
4523 break;
4524 case kCondLT:
4525 if (gt_bias) {
4526 __ CmpLtS(FTMP, lhs, rhs);
4527 } else {
4528 __ CmpUltS(FTMP, lhs, rhs);
4529 }
4530 __ Mfc1(dst, FTMP);
4531 __ Andi(dst, dst, 1);
4532 break;
4533 case kCondLE:
4534 if (gt_bias) {
4535 __ CmpLeS(FTMP, lhs, rhs);
4536 } else {
4537 __ CmpUleS(FTMP, lhs, rhs);
4538 }
4539 __ Mfc1(dst, FTMP);
4540 __ Andi(dst, dst, 1);
4541 break;
4542 case kCondGT:
4543 if (gt_bias) {
4544 __ CmpUltS(FTMP, rhs, lhs);
4545 } else {
4546 __ CmpLtS(FTMP, rhs, lhs);
4547 }
4548 __ Mfc1(dst, FTMP);
4549 __ Andi(dst, dst, 1);
4550 break;
4551 case kCondGE:
4552 if (gt_bias) {
4553 __ CmpUleS(FTMP, rhs, lhs);
4554 } else {
4555 __ CmpLeS(FTMP, rhs, lhs);
4556 }
4557 __ Mfc1(dst, FTMP);
4558 __ Andi(dst, dst, 1);
4559 break;
4560 default:
4561 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4562 UNREACHABLE();
4563 }
4564 } else {
4565 switch (cond) {
4566 case kCondEQ:
4567 __ CeqS(0, lhs, rhs);
4568 __ LoadConst32(dst, 1);
4569 __ Movf(dst, ZERO, 0);
4570 break;
4571 case kCondNE:
4572 __ CeqS(0, lhs, rhs);
4573 __ LoadConst32(dst, 1);
4574 __ Movt(dst, ZERO, 0);
4575 break;
4576 case kCondLT:
4577 if (gt_bias) {
4578 __ ColtS(0, lhs, rhs);
4579 } else {
4580 __ CultS(0, lhs, rhs);
4581 }
4582 __ LoadConst32(dst, 1);
4583 __ Movf(dst, ZERO, 0);
4584 break;
4585 case kCondLE:
4586 if (gt_bias) {
4587 __ ColeS(0, lhs, rhs);
4588 } else {
4589 __ CuleS(0, lhs, rhs);
4590 }
4591 __ LoadConst32(dst, 1);
4592 __ Movf(dst, ZERO, 0);
4593 break;
4594 case kCondGT:
4595 if (gt_bias) {
4596 __ CultS(0, rhs, lhs);
4597 } else {
4598 __ ColtS(0, rhs, lhs);
4599 }
4600 __ LoadConst32(dst, 1);
4601 __ Movf(dst, ZERO, 0);
4602 break;
4603 case kCondGE:
4604 if (gt_bias) {
4605 __ CuleS(0, rhs, lhs);
4606 } else {
4607 __ ColeS(0, rhs, lhs);
4608 }
4609 __ LoadConst32(dst, 1);
4610 __ Movf(dst, ZERO, 0);
4611 break;
4612 default:
4613 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4614 UNREACHABLE();
4615 }
4616 }
4617 } else {
4618 DCHECK_EQ(type, Primitive::kPrimDouble);
4619 if (isR6) {
4620 switch (cond) {
4621 case kCondEQ:
4622 __ CmpEqD(FTMP, lhs, rhs);
4623 __ Mfc1(dst, FTMP);
4624 __ Andi(dst, dst, 1);
4625 break;
4626 case kCondNE:
4627 __ CmpEqD(FTMP, lhs, rhs);
4628 __ Mfc1(dst, FTMP);
4629 __ Addiu(dst, dst, 1);
4630 break;
4631 case kCondLT:
4632 if (gt_bias) {
4633 __ CmpLtD(FTMP, lhs, rhs);
4634 } else {
4635 __ CmpUltD(FTMP, lhs, rhs);
4636 }
4637 __ Mfc1(dst, FTMP);
4638 __ Andi(dst, dst, 1);
4639 break;
4640 case kCondLE:
4641 if (gt_bias) {
4642 __ CmpLeD(FTMP, lhs, rhs);
4643 } else {
4644 __ CmpUleD(FTMP, lhs, rhs);
4645 }
4646 __ Mfc1(dst, FTMP);
4647 __ Andi(dst, dst, 1);
4648 break;
4649 case kCondGT:
4650 if (gt_bias) {
4651 __ CmpUltD(FTMP, rhs, lhs);
4652 } else {
4653 __ CmpLtD(FTMP, rhs, lhs);
4654 }
4655 __ Mfc1(dst, FTMP);
4656 __ Andi(dst, dst, 1);
4657 break;
4658 case kCondGE:
4659 if (gt_bias) {
4660 __ CmpUleD(FTMP, rhs, lhs);
4661 } else {
4662 __ CmpLeD(FTMP, rhs, lhs);
4663 }
4664 __ Mfc1(dst, FTMP);
4665 __ Andi(dst, dst, 1);
4666 break;
4667 default:
4668 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4669 UNREACHABLE();
4670 }
4671 } else {
4672 switch (cond) {
4673 case kCondEQ:
4674 __ CeqD(0, lhs, rhs);
4675 __ LoadConst32(dst, 1);
4676 __ Movf(dst, ZERO, 0);
4677 break;
4678 case kCondNE:
4679 __ CeqD(0, lhs, rhs);
4680 __ LoadConst32(dst, 1);
4681 __ Movt(dst, ZERO, 0);
4682 break;
4683 case kCondLT:
4684 if (gt_bias) {
4685 __ ColtD(0, lhs, rhs);
4686 } else {
4687 __ CultD(0, lhs, rhs);
4688 }
4689 __ LoadConst32(dst, 1);
4690 __ Movf(dst, ZERO, 0);
4691 break;
4692 case kCondLE:
4693 if (gt_bias) {
4694 __ ColeD(0, lhs, rhs);
4695 } else {
4696 __ CuleD(0, lhs, rhs);
4697 }
4698 __ LoadConst32(dst, 1);
4699 __ Movf(dst, ZERO, 0);
4700 break;
4701 case kCondGT:
4702 if (gt_bias) {
4703 __ CultD(0, rhs, lhs);
4704 } else {
4705 __ ColtD(0, rhs, lhs);
4706 }
4707 __ LoadConst32(dst, 1);
4708 __ Movf(dst, ZERO, 0);
4709 break;
4710 case kCondGE:
4711 if (gt_bias) {
4712 __ CuleD(0, rhs, lhs);
4713 } else {
4714 __ ColeD(0, rhs, lhs);
4715 }
4716 __ LoadConst32(dst, 1);
4717 __ Movf(dst, ZERO, 0);
4718 break;
4719 default:
4720 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4721 UNREACHABLE();
4722 }
4723 }
4724 }
4725}
4726
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004727bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4728 bool gt_bias,
4729 Primitive::Type type,
4730 LocationSummary* input_locations,
4731 int cc) {
4732 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4733 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4734 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4735 if (type == Primitive::kPrimFloat) {
4736 switch (cond) {
4737 case kCondEQ:
4738 __ CeqS(cc, lhs, rhs);
4739 return false;
4740 case kCondNE:
4741 __ CeqS(cc, lhs, rhs);
4742 return true;
4743 case kCondLT:
4744 if (gt_bias) {
4745 __ ColtS(cc, lhs, rhs);
4746 } else {
4747 __ CultS(cc, lhs, rhs);
4748 }
4749 return false;
4750 case kCondLE:
4751 if (gt_bias) {
4752 __ ColeS(cc, lhs, rhs);
4753 } else {
4754 __ CuleS(cc, lhs, rhs);
4755 }
4756 return false;
4757 case kCondGT:
4758 if (gt_bias) {
4759 __ CultS(cc, rhs, lhs);
4760 } else {
4761 __ ColtS(cc, rhs, lhs);
4762 }
4763 return false;
4764 case kCondGE:
4765 if (gt_bias) {
4766 __ CuleS(cc, rhs, lhs);
4767 } else {
4768 __ ColeS(cc, rhs, lhs);
4769 }
4770 return false;
4771 default:
4772 LOG(FATAL) << "Unexpected non-floating-point condition";
4773 UNREACHABLE();
4774 }
4775 } else {
4776 DCHECK_EQ(type, Primitive::kPrimDouble);
4777 switch (cond) {
4778 case kCondEQ:
4779 __ CeqD(cc, lhs, rhs);
4780 return false;
4781 case kCondNE:
4782 __ CeqD(cc, lhs, rhs);
4783 return true;
4784 case kCondLT:
4785 if (gt_bias) {
4786 __ ColtD(cc, lhs, rhs);
4787 } else {
4788 __ CultD(cc, lhs, rhs);
4789 }
4790 return false;
4791 case kCondLE:
4792 if (gt_bias) {
4793 __ ColeD(cc, lhs, rhs);
4794 } else {
4795 __ CuleD(cc, lhs, rhs);
4796 }
4797 return false;
4798 case kCondGT:
4799 if (gt_bias) {
4800 __ CultD(cc, rhs, lhs);
4801 } else {
4802 __ ColtD(cc, rhs, lhs);
4803 }
4804 return false;
4805 case kCondGE:
4806 if (gt_bias) {
4807 __ CuleD(cc, rhs, lhs);
4808 } else {
4809 __ ColeD(cc, rhs, lhs);
4810 }
4811 return false;
4812 default:
4813 LOG(FATAL) << "Unexpected non-floating-point condition";
4814 UNREACHABLE();
4815 }
4816 }
4817}
4818
4819bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4820 bool gt_bias,
4821 Primitive::Type type,
4822 LocationSummary* input_locations,
4823 FRegister dst) {
4824 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4825 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4826 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4827 if (type == Primitive::kPrimFloat) {
4828 switch (cond) {
4829 case kCondEQ:
4830 __ CmpEqS(dst, lhs, rhs);
4831 return false;
4832 case kCondNE:
4833 __ CmpEqS(dst, lhs, rhs);
4834 return true;
4835 case kCondLT:
4836 if (gt_bias) {
4837 __ CmpLtS(dst, lhs, rhs);
4838 } else {
4839 __ CmpUltS(dst, lhs, rhs);
4840 }
4841 return false;
4842 case kCondLE:
4843 if (gt_bias) {
4844 __ CmpLeS(dst, lhs, rhs);
4845 } else {
4846 __ CmpUleS(dst, lhs, rhs);
4847 }
4848 return false;
4849 case kCondGT:
4850 if (gt_bias) {
4851 __ CmpUltS(dst, rhs, lhs);
4852 } else {
4853 __ CmpLtS(dst, rhs, lhs);
4854 }
4855 return false;
4856 case kCondGE:
4857 if (gt_bias) {
4858 __ CmpUleS(dst, rhs, lhs);
4859 } else {
4860 __ CmpLeS(dst, rhs, lhs);
4861 }
4862 return false;
4863 default:
4864 LOG(FATAL) << "Unexpected non-floating-point condition";
4865 UNREACHABLE();
4866 }
4867 } else {
4868 DCHECK_EQ(type, Primitive::kPrimDouble);
4869 switch (cond) {
4870 case kCondEQ:
4871 __ CmpEqD(dst, lhs, rhs);
4872 return false;
4873 case kCondNE:
4874 __ CmpEqD(dst, lhs, rhs);
4875 return true;
4876 case kCondLT:
4877 if (gt_bias) {
4878 __ CmpLtD(dst, lhs, rhs);
4879 } else {
4880 __ CmpUltD(dst, lhs, rhs);
4881 }
4882 return false;
4883 case kCondLE:
4884 if (gt_bias) {
4885 __ CmpLeD(dst, lhs, rhs);
4886 } else {
4887 __ CmpUleD(dst, lhs, rhs);
4888 }
4889 return false;
4890 case kCondGT:
4891 if (gt_bias) {
4892 __ CmpUltD(dst, rhs, lhs);
4893 } else {
4894 __ CmpLtD(dst, rhs, lhs);
4895 }
4896 return false;
4897 case kCondGE:
4898 if (gt_bias) {
4899 __ CmpUleD(dst, rhs, lhs);
4900 } else {
4901 __ CmpLeD(dst, rhs, lhs);
4902 }
4903 return false;
4904 default:
4905 LOG(FATAL) << "Unexpected non-floating-point condition";
4906 UNREACHABLE();
4907 }
4908 }
4909}
4910
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004911void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
4912 bool gt_bias,
4913 Primitive::Type type,
4914 LocationSummary* locations,
4915 MipsLabel* label) {
4916 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4917 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4918 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4919 if (type == Primitive::kPrimFloat) {
4920 if (isR6) {
4921 switch (cond) {
4922 case kCondEQ:
4923 __ CmpEqS(FTMP, lhs, rhs);
4924 __ Bc1nez(FTMP, label);
4925 break;
4926 case kCondNE:
4927 __ CmpEqS(FTMP, lhs, rhs);
4928 __ Bc1eqz(FTMP, label);
4929 break;
4930 case kCondLT:
4931 if (gt_bias) {
4932 __ CmpLtS(FTMP, lhs, rhs);
4933 } else {
4934 __ CmpUltS(FTMP, lhs, rhs);
4935 }
4936 __ Bc1nez(FTMP, label);
4937 break;
4938 case kCondLE:
4939 if (gt_bias) {
4940 __ CmpLeS(FTMP, lhs, rhs);
4941 } else {
4942 __ CmpUleS(FTMP, lhs, rhs);
4943 }
4944 __ Bc1nez(FTMP, label);
4945 break;
4946 case kCondGT:
4947 if (gt_bias) {
4948 __ CmpUltS(FTMP, rhs, lhs);
4949 } else {
4950 __ CmpLtS(FTMP, rhs, lhs);
4951 }
4952 __ Bc1nez(FTMP, label);
4953 break;
4954 case kCondGE:
4955 if (gt_bias) {
4956 __ CmpUleS(FTMP, rhs, lhs);
4957 } else {
4958 __ CmpLeS(FTMP, rhs, lhs);
4959 }
4960 __ Bc1nez(FTMP, label);
4961 break;
4962 default:
4963 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004964 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004965 }
4966 } else {
4967 switch (cond) {
4968 case kCondEQ:
4969 __ CeqS(0, lhs, rhs);
4970 __ Bc1t(0, label);
4971 break;
4972 case kCondNE:
4973 __ CeqS(0, lhs, rhs);
4974 __ Bc1f(0, label);
4975 break;
4976 case kCondLT:
4977 if (gt_bias) {
4978 __ ColtS(0, lhs, rhs);
4979 } else {
4980 __ CultS(0, lhs, rhs);
4981 }
4982 __ Bc1t(0, label);
4983 break;
4984 case kCondLE:
4985 if (gt_bias) {
4986 __ ColeS(0, lhs, rhs);
4987 } else {
4988 __ CuleS(0, lhs, rhs);
4989 }
4990 __ Bc1t(0, label);
4991 break;
4992 case kCondGT:
4993 if (gt_bias) {
4994 __ CultS(0, rhs, lhs);
4995 } else {
4996 __ ColtS(0, rhs, lhs);
4997 }
4998 __ Bc1t(0, label);
4999 break;
5000 case kCondGE:
5001 if (gt_bias) {
5002 __ CuleS(0, rhs, lhs);
5003 } else {
5004 __ ColeS(0, rhs, lhs);
5005 }
5006 __ Bc1t(0, label);
5007 break;
5008 default:
5009 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005010 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005011 }
5012 }
5013 } else {
5014 DCHECK_EQ(type, Primitive::kPrimDouble);
5015 if (isR6) {
5016 switch (cond) {
5017 case kCondEQ:
5018 __ CmpEqD(FTMP, lhs, rhs);
5019 __ Bc1nez(FTMP, label);
5020 break;
5021 case kCondNE:
5022 __ CmpEqD(FTMP, lhs, rhs);
5023 __ Bc1eqz(FTMP, label);
5024 break;
5025 case kCondLT:
5026 if (gt_bias) {
5027 __ CmpLtD(FTMP, lhs, rhs);
5028 } else {
5029 __ CmpUltD(FTMP, lhs, rhs);
5030 }
5031 __ Bc1nez(FTMP, label);
5032 break;
5033 case kCondLE:
5034 if (gt_bias) {
5035 __ CmpLeD(FTMP, lhs, rhs);
5036 } else {
5037 __ CmpUleD(FTMP, lhs, rhs);
5038 }
5039 __ Bc1nez(FTMP, label);
5040 break;
5041 case kCondGT:
5042 if (gt_bias) {
5043 __ CmpUltD(FTMP, rhs, lhs);
5044 } else {
5045 __ CmpLtD(FTMP, rhs, lhs);
5046 }
5047 __ Bc1nez(FTMP, label);
5048 break;
5049 case kCondGE:
5050 if (gt_bias) {
5051 __ CmpUleD(FTMP, rhs, lhs);
5052 } else {
5053 __ CmpLeD(FTMP, rhs, lhs);
5054 }
5055 __ Bc1nez(FTMP, label);
5056 break;
5057 default:
5058 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005059 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005060 }
5061 } else {
5062 switch (cond) {
5063 case kCondEQ:
5064 __ CeqD(0, lhs, rhs);
5065 __ Bc1t(0, label);
5066 break;
5067 case kCondNE:
5068 __ CeqD(0, lhs, rhs);
5069 __ Bc1f(0, label);
5070 break;
5071 case kCondLT:
5072 if (gt_bias) {
5073 __ ColtD(0, lhs, rhs);
5074 } else {
5075 __ CultD(0, lhs, rhs);
5076 }
5077 __ Bc1t(0, label);
5078 break;
5079 case kCondLE:
5080 if (gt_bias) {
5081 __ ColeD(0, lhs, rhs);
5082 } else {
5083 __ CuleD(0, lhs, rhs);
5084 }
5085 __ Bc1t(0, label);
5086 break;
5087 case kCondGT:
5088 if (gt_bias) {
5089 __ CultD(0, rhs, lhs);
5090 } else {
5091 __ ColtD(0, rhs, lhs);
5092 }
5093 __ Bc1t(0, label);
5094 break;
5095 case kCondGE:
5096 if (gt_bias) {
5097 __ CuleD(0, rhs, lhs);
5098 } else {
5099 __ ColeD(0, rhs, lhs);
5100 }
5101 __ Bc1t(0, label);
5102 break;
5103 default:
5104 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005105 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005106 }
5107 }
5108 }
5109}
5110
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005111void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005112 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005113 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005114 MipsLabel* false_target) {
5115 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005116
David Brazdil0debae72015-11-12 18:37:00 +00005117 if (true_target == nullptr && false_target == nullptr) {
5118 // Nothing to do. The code always falls through.
5119 return;
5120 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005121 // Constant condition, statically compared against "true" (integer value 1).
5122 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005123 if (true_target != nullptr) {
5124 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005125 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005127 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005128 if (false_target != nullptr) {
5129 __ B(false_target);
5130 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005131 }
David Brazdil0debae72015-11-12 18:37:00 +00005132 return;
5133 }
5134
5135 // The following code generates these patterns:
5136 // (1) true_target == nullptr && false_target != nullptr
5137 // - opposite condition true => branch to false_target
5138 // (2) true_target != nullptr && false_target == nullptr
5139 // - condition true => branch to true_target
5140 // (3) true_target != nullptr && false_target != nullptr
5141 // - condition true => branch to true_target
5142 // - branch to false_target
5143 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005145 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005146 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005147 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005148 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5149 } else {
5150 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5151 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005152 } else {
5153 // The condition instruction has not been materialized, use its inputs as
5154 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005155 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005156 Primitive::Type type = condition->InputAt(0)->GetType();
5157 LocationSummary* locations = cond->GetLocations();
5158 IfCondition if_cond = condition->GetCondition();
5159 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005160
David Brazdil0debae72015-11-12 18:37:00 +00005161 if (true_target == nullptr) {
5162 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005163 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005164 }
5165
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005166 switch (type) {
5167 default:
5168 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5169 break;
5170 case Primitive::kPrimLong:
5171 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5172 break;
5173 case Primitive::kPrimFloat:
5174 case Primitive::kPrimDouble:
5175 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5176 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005177 }
5178 }
David Brazdil0debae72015-11-12 18:37:00 +00005179
5180 // If neither branch falls through (case 3), the conditional branch to `true_target`
5181 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5182 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005183 __ B(false_target);
5184 }
5185}
5186
5187void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5188 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005189 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005190 locations->SetInAt(0, Location::RequiresRegister());
5191 }
5192}
5193
5194void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005195 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5196 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5197 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5198 nullptr : codegen_->GetLabelOf(true_successor);
5199 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5200 nullptr : codegen_->GetLabelOf(false_successor);
5201 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005202}
5203
5204void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5205 LocationSummary* locations = new (GetGraph()->GetArena())
5206 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005207 InvokeRuntimeCallingConvention calling_convention;
5208 RegisterSet caller_saves = RegisterSet::Empty();
5209 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5210 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005211 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005212 locations->SetInAt(0, Location::RequiresRegister());
5213 }
5214}
5215
5216void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005217 SlowPathCodeMIPS* slow_path =
5218 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005219 GenerateTestAndBranch(deoptimize,
5220 /* condition_input_index */ 0,
5221 slow_path->GetEntryLabel(),
5222 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005223}
5224
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005225// This function returns true if a conditional move can be generated for HSelect.
5226// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5227// branches and regular moves.
5228//
5229// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5230//
5231// While determining feasibility of a conditional move and setting inputs/outputs
5232// are two distinct tasks, this function does both because they share quite a bit
5233// of common logic.
5234static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5235 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5236 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5237 HCondition* condition = cond->AsCondition();
5238
5239 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5240 Primitive::Type dst_type = select->GetType();
5241
5242 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5243 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5244 bool is_true_value_zero_constant =
5245 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5246 bool is_false_value_zero_constant =
5247 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5248
5249 bool can_move_conditionally = false;
5250 bool use_const_for_false_in = false;
5251 bool use_const_for_true_in = false;
5252
5253 if (!cond->IsConstant()) {
5254 switch (cond_type) {
5255 default:
5256 switch (dst_type) {
5257 default:
5258 // Moving int on int condition.
5259 if (is_r6) {
5260 if (is_true_value_zero_constant) {
5261 // seleqz out_reg, false_reg, cond_reg
5262 can_move_conditionally = true;
5263 use_const_for_true_in = true;
5264 } else if (is_false_value_zero_constant) {
5265 // selnez out_reg, true_reg, cond_reg
5266 can_move_conditionally = true;
5267 use_const_for_false_in = true;
5268 } else if (materialized) {
5269 // Not materializing unmaterialized int conditions
5270 // to keep the instruction count low.
5271 // selnez AT, true_reg, cond_reg
5272 // seleqz TMP, false_reg, cond_reg
5273 // or out_reg, AT, TMP
5274 can_move_conditionally = true;
5275 }
5276 } else {
5277 // movn out_reg, true_reg/ZERO, cond_reg
5278 can_move_conditionally = true;
5279 use_const_for_true_in = is_true_value_zero_constant;
5280 }
5281 break;
5282 case Primitive::kPrimLong:
5283 // Moving long on int condition.
5284 if (is_r6) {
5285 if (is_true_value_zero_constant) {
5286 // seleqz out_reg_lo, false_reg_lo, cond_reg
5287 // seleqz out_reg_hi, false_reg_hi, cond_reg
5288 can_move_conditionally = true;
5289 use_const_for_true_in = true;
5290 } else if (is_false_value_zero_constant) {
5291 // selnez out_reg_lo, true_reg_lo, cond_reg
5292 // selnez out_reg_hi, true_reg_hi, cond_reg
5293 can_move_conditionally = true;
5294 use_const_for_false_in = true;
5295 }
5296 // Other long conditional moves would generate 6+ instructions,
5297 // which is too many.
5298 } else {
5299 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5300 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5301 can_move_conditionally = true;
5302 use_const_for_true_in = is_true_value_zero_constant;
5303 }
5304 break;
5305 case Primitive::kPrimFloat:
5306 case Primitive::kPrimDouble:
5307 // Moving float/double on int condition.
5308 if (is_r6) {
5309 if (materialized) {
5310 // Not materializing unmaterialized int conditions
5311 // to keep the instruction count low.
5312 can_move_conditionally = true;
5313 if (is_true_value_zero_constant) {
5314 // sltu TMP, ZERO, cond_reg
5315 // mtc1 TMP, temp_cond_reg
5316 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5317 use_const_for_true_in = true;
5318 } else if (is_false_value_zero_constant) {
5319 // sltu TMP, ZERO, cond_reg
5320 // mtc1 TMP, temp_cond_reg
5321 // selnez.fmt out_reg, true_reg, temp_cond_reg
5322 use_const_for_false_in = true;
5323 } else {
5324 // sltu TMP, ZERO, cond_reg
5325 // mtc1 TMP, temp_cond_reg
5326 // sel.fmt temp_cond_reg, false_reg, true_reg
5327 // mov.fmt out_reg, temp_cond_reg
5328 }
5329 }
5330 } else {
5331 // movn.fmt out_reg, true_reg, cond_reg
5332 can_move_conditionally = true;
5333 }
5334 break;
5335 }
5336 break;
5337 case Primitive::kPrimLong:
5338 // We don't materialize long comparison now
5339 // and use conditional branches instead.
5340 break;
5341 case Primitive::kPrimFloat:
5342 case Primitive::kPrimDouble:
5343 switch (dst_type) {
5344 default:
5345 // Moving int on float/double condition.
5346 if (is_r6) {
5347 if (is_true_value_zero_constant) {
5348 // mfc1 TMP, temp_cond_reg
5349 // seleqz out_reg, false_reg, TMP
5350 can_move_conditionally = true;
5351 use_const_for_true_in = true;
5352 } else if (is_false_value_zero_constant) {
5353 // mfc1 TMP, temp_cond_reg
5354 // selnez out_reg, true_reg, TMP
5355 can_move_conditionally = true;
5356 use_const_for_false_in = true;
5357 } else {
5358 // mfc1 TMP, temp_cond_reg
5359 // selnez AT, true_reg, TMP
5360 // seleqz TMP, false_reg, TMP
5361 // or out_reg, AT, TMP
5362 can_move_conditionally = true;
5363 }
5364 } else {
5365 // movt out_reg, true_reg/ZERO, cc
5366 can_move_conditionally = true;
5367 use_const_for_true_in = is_true_value_zero_constant;
5368 }
5369 break;
5370 case Primitive::kPrimLong:
5371 // Moving long on float/double condition.
5372 if (is_r6) {
5373 if (is_true_value_zero_constant) {
5374 // mfc1 TMP, temp_cond_reg
5375 // seleqz out_reg_lo, false_reg_lo, TMP
5376 // seleqz out_reg_hi, false_reg_hi, TMP
5377 can_move_conditionally = true;
5378 use_const_for_true_in = true;
5379 } else if (is_false_value_zero_constant) {
5380 // mfc1 TMP, temp_cond_reg
5381 // selnez out_reg_lo, true_reg_lo, TMP
5382 // selnez out_reg_hi, true_reg_hi, TMP
5383 can_move_conditionally = true;
5384 use_const_for_false_in = true;
5385 }
5386 // Other long conditional moves would generate 6+ instructions,
5387 // which is too many.
5388 } else {
5389 // movt out_reg_lo, true_reg_lo/ZERO, cc
5390 // movt out_reg_hi, true_reg_hi/ZERO, cc
5391 can_move_conditionally = true;
5392 use_const_for_true_in = is_true_value_zero_constant;
5393 }
5394 break;
5395 case Primitive::kPrimFloat:
5396 case Primitive::kPrimDouble:
5397 // Moving float/double on float/double condition.
5398 if (is_r6) {
5399 can_move_conditionally = true;
5400 if (is_true_value_zero_constant) {
5401 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5402 use_const_for_true_in = true;
5403 } else if (is_false_value_zero_constant) {
5404 // selnez.fmt out_reg, true_reg, temp_cond_reg
5405 use_const_for_false_in = true;
5406 } else {
5407 // sel.fmt temp_cond_reg, false_reg, true_reg
5408 // mov.fmt out_reg, temp_cond_reg
5409 }
5410 } else {
5411 // movt.fmt out_reg, true_reg, cc
5412 can_move_conditionally = true;
5413 }
5414 break;
5415 }
5416 break;
5417 }
5418 }
5419
5420 if (can_move_conditionally) {
5421 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5422 } else {
5423 DCHECK(!use_const_for_false_in);
5424 DCHECK(!use_const_for_true_in);
5425 }
5426
5427 if (locations_to_set != nullptr) {
5428 if (use_const_for_false_in) {
5429 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5430 } else {
5431 locations_to_set->SetInAt(0,
5432 Primitive::IsFloatingPointType(dst_type)
5433 ? Location::RequiresFpuRegister()
5434 : Location::RequiresRegister());
5435 }
5436 if (use_const_for_true_in) {
5437 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5438 } else {
5439 locations_to_set->SetInAt(1,
5440 Primitive::IsFloatingPointType(dst_type)
5441 ? Location::RequiresFpuRegister()
5442 : Location::RequiresRegister());
5443 }
5444 if (materialized) {
5445 locations_to_set->SetInAt(2, Location::RequiresRegister());
5446 }
5447 // On R6 we don't require the output to be the same as the
5448 // first input for conditional moves unlike on R2.
5449 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5450 if (is_out_same_as_first_in) {
5451 locations_to_set->SetOut(Location::SameAsFirstInput());
5452 } else {
5453 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5454 ? Location::RequiresFpuRegister()
5455 : Location::RequiresRegister());
5456 }
5457 }
5458
5459 return can_move_conditionally;
5460}
5461
5462void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5463 LocationSummary* locations = select->GetLocations();
5464 Location dst = locations->Out();
5465 Location src = locations->InAt(1);
5466 Register src_reg = ZERO;
5467 Register src_reg_high = ZERO;
5468 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5469 Register cond_reg = TMP;
5470 int cond_cc = 0;
5471 Primitive::Type cond_type = Primitive::kPrimInt;
5472 bool cond_inverted = false;
5473 Primitive::Type dst_type = select->GetType();
5474
5475 if (IsBooleanValueOrMaterializedCondition(cond)) {
5476 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5477 } else {
5478 HCondition* condition = cond->AsCondition();
5479 LocationSummary* cond_locations = cond->GetLocations();
5480 IfCondition if_cond = condition->GetCondition();
5481 cond_type = condition->InputAt(0)->GetType();
5482 switch (cond_type) {
5483 default:
5484 DCHECK_NE(cond_type, Primitive::kPrimLong);
5485 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5486 break;
5487 case Primitive::kPrimFloat:
5488 case Primitive::kPrimDouble:
5489 cond_inverted = MaterializeFpCompareR2(if_cond,
5490 condition->IsGtBias(),
5491 cond_type,
5492 cond_locations,
5493 cond_cc);
5494 break;
5495 }
5496 }
5497
5498 DCHECK(dst.Equals(locations->InAt(0)));
5499 if (src.IsRegister()) {
5500 src_reg = src.AsRegister<Register>();
5501 } else if (src.IsRegisterPair()) {
5502 src_reg = src.AsRegisterPairLow<Register>();
5503 src_reg_high = src.AsRegisterPairHigh<Register>();
5504 } else if (src.IsConstant()) {
5505 DCHECK(src.GetConstant()->IsZeroBitPattern());
5506 }
5507
5508 switch (cond_type) {
5509 default:
5510 switch (dst_type) {
5511 default:
5512 if (cond_inverted) {
5513 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5514 } else {
5515 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5516 }
5517 break;
5518 case Primitive::kPrimLong:
5519 if (cond_inverted) {
5520 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5521 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5522 } else {
5523 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5524 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5525 }
5526 break;
5527 case Primitive::kPrimFloat:
5528 if (cond_inverted) {
5529 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5530 } else {
5531 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5532 }
5533 break;
5534 case Primitive::kPrimDouble:
5535 if (cond_inverted) {
5536 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5537 } else {
5538 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5539 }
5540 break;
5541 }
5542 break;
5543 case Primitive::kPrimLong:
5544 LOG(FATAL) << "Unreachable";
5545 UNREACHABLE();
5546 case Primitive::kPrimFloat:
5547 case Primitive::kPrimDouble:
5548 switch (dst_type) {
5549 default:
5550 if (cond_inverted) {
5551 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5552 } else {
5553 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5554 }
5555 break;
5556 case Primitive::kPrimLong:
5557 if (cond_inverted) {
5558 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5559 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5560 } else {
5561 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5562 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5563 }
5564 break;
5565 case Primitive::kPrimFloat:
5566 if (cond_inverted) {
5567 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5568 } else {
5569 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5570 }
5571 break;
5572 case Primitive::kPrimDouble:
5573 if (cond_inverted) {
5574 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5575 } else {
5576 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5577 }
5578 break;
5579 }
5580 break;
5581 }
5582}
5583
5584void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5585 LocationSummary* locations = select->GetLocations();
5586 Location dst = locations->Out();
5587 Location false_src = locations->InAt(0);
5588 Location true_src = locations->InAt(1);
5589 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5590 Register cond_reg = TMP;
5591 FRegister fcond_reg = FTMP;
5592 Primitive::Type cond_type = Primitive::kPrimInt;
5593 bool cond_inverted = false;
5594 Primitive::Type dst_type = select->GetType();
5595
5596 if (IsBooleanValueOrMaterializedCondition(cond)) {
5597 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5598 } else {
5599 HCondition* condition = cond->AsCondition();
5600 LocationSummary* cond_locations = cond->GetLocations();
5601 IfCondition if_cond = condition->GetCondition();
5602 cond_type = condition->InputAt(0)->GetType();
5603 switch (cond_type) {
5604 default:
5605 DCHECK_NE(cond_type, Primitive::kPrimLong);
5606 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5607 break;
5608 case Primitive::kPrimFloat:
5609 case Primitive::kPrimDouble:
5610 cond_inverted = MaterializeFpCompareR6(if_cond,
5611 condition->IsGtBias(),
5612 cond_type,
5613 cond_locations,
5614 fcond_reg);
5615 break;
5616 }
5617 }
5618
5619 if (true_src.IsConstant()) {
5620 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5621 }
5622 if (false_src.IsConstant()) {
5623 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5624 }
5625
5626 switch (dst_type) {
5627 default:
5628 if (Primitive::IsFloatingPointType(cond_type)) {
5629 __ Mfc1(cond_reg, fcond_reg);
5630 }
5631 if (true_src.IsConstant()) {
5632 if (cond_inverted) {
5633 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5634 } else {
5635 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5636 }
5637 } else if (false_src.IsConstant()) {
5638 if (cond_inverted) {
5639 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5640 } else {
5641 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5642 }
5643 } else {
5644 DCHECK_NE(cond_reg, AT);
5645 if (cond_inverted) {
5646 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5647 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5648 } else {
5649 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5650 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5651 }
5652 __ Or(dst.AsRegister<Register>(), AT, TMP);
5653 }
5654 break;
5655 case Primitive::kPrimLong: {
5656 if (Primitive::IsFloatingPointType(cond_type)) {
5657 __ Mfc1(cond_reg, fcond_reg);
5658 }
5659 Register dst_lo = dst.AsRegisterPairLow<Register>();
5660 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5661 if (true_src.IsConstant()) {
5662 Register src_lo = false_src.AsRegisterPairLow<Register>();
5663 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5664 if (cond_inverted) {
5665 __ Selnez(dst_lo, src_lo, cond_reg);
5666 __ Selnez(dst_hi, src_hi, cond_reg);
5667 } else {
5668 __ Seleqz(dst_lo, src_lo, cond_reg);
5669 __ Seleqz(dst_hi, src_hi, cond_reg);
5670 }
5671 } else {
5672 DCHECK(false_src.IsConstant());
5673 Register src_lo = true_src.AsRegisterPairLow<Register>();
5674 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5675 if (cond_inverted) {
5676 __ Seleqz(dst_lo, src_lo, cond_reg);
5677 __ Seleqz(dst_hi, src_hi, cond_reg);
5678 } else {
5679 __ Selnez(dst_lo, src_lo, cond_reg);
5680 __ Selnez(dst_hi, src_hi, cond_reg);
5681 }
5682 }
5683 break;
5684 }
5685 case Primitive::kPrimFloat: {
5686 if (!Primitive::IsFloatingPointType(cond_type)) {
5687 // sel*.fmt tests bit 0 of the condition register, account for that.
5688 __ Sltu(TMP, ZERO, cond_reg);
5689 __ Mtc1(TMP, fcond_reg);
5690 }
5691 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5692 if (true_src.IsConstant()) {
5693 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5694 if (cond_inverted) {
5695 __ SelnezS(dst_reg, src_reg, fcond_reg);
5696 } else {
5697 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5698 }
5699 } else if (false_src.IsConstant()) {
5700 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5701 if (cond_inverted) {
5702 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5703 } else {
5704 __ SelnezS(dst_reg, src_reg, fcond_reg);
5705 }
5706 } else {
5707 if (cond_inverted) {
5708 __ SelS(fcond_reg,
5709 true_src.AsFpuRegister<FRegister>(),
5710 false_src.AsFpuRegister<FRegister>());
5711 } else {
5712 __ SelS(fcond_reg,
5713 false_src.AsFpuRegister<FRegister>(),
5714 true_src.AsFpuRegister<FRegister>());
5715 }
5716 __ MovS(dst_reg, fcond_reg);
5717 }
5718 break;
5719 }
5720 case Primitive::kPrimDouble: {
5721 if (!Primitive::IsFloatingPointType(cond_type)) {
5722 // sel*.fmt tests bit 0 of the condition register, account for that.
5723 __ Sltu(TMP, ZERO, cond_reg);
5724 __ Mtc1(TMP, fcond_reg);
5725 }
5726 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5727 if (true_src.IsConstant()) {
5728 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5729 if (cond_inverted) {
5730 __ SelnezD(dst_reg, src_reg, fcond_reg);
5731 } else {
5732 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5733 }
5734 } else if (false_src.IsConstant()) {
5735 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5736 if (cond_inverted) {
5737 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5738 } else {
5739 __ SelnezD(dst_reg, src_reg, fcond_reg);
5740 }
5741 } else {
5742 if (cond_inverted) {
5743 __ SelD(fcond_reg,
5744 true_src.AsFpuRegister<FRegister>(),
5745 false_src.AsFpuRegister<FRegister>());
5746 } else {
5747 __ SelD(fcond_reg,
5748 false_src.AsFpuRegister<FRegister>(),
5749 true_src.AsFpuRegister<FRegister>());
5750 }
5751 __ MovD(dst_reg, fcond_reg);
5752 }
5753 break;
5754 }
5755 }
5756}
5757
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005758void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5759 LocationSummary* locations = new (GetGraph()->GetArena())
5760 LocationSummary(flag, LocationSummary::kNoCall);
5761 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005762}
5763
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005764void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5765 __ LoadFromOffset(kLoadWord,
5766 flag->GetLocations()->Out().AsRegister<Register>(),
5767 SP,
5768 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005769}
5770
David Brazdil74eb1b22015-12-14 11:44:01 +00005771void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005773 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005774}
5775
5776void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005777 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5778 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5779 if (is_r6) {
5780 GenConditionalMoveR6(select);
5781 } else {
5782 GenConditionalMoveR2(select);
5783 }
5784 } else {
5785 LocationSummary* locations = select->GetLocations();
5786 MipsLabel false_target;
5787 GenerateTestAndBranch(select,
5788 /* condition_input_index */ 2,
5789 /* true_target */ nullptr,
5790 &false_target);
5791 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5792 __ Bind(&false_target);
5793 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005794}
5795
David Srbecky0cf44932015-12-09 14:09:59 +00005796void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5797 new (GetGraph()->GetArena()) LocationSummary(info);
5798}
5799
David Srbeckyd28f4a02016-03-14 17:14:24 +00005800void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5801 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005802}
5803
5804void CodeGeneratorMIPS::GenerateNop() {
5805 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005806}
5807
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005808void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5809 Primitive::Type field_type = field_info.GetFieldType();
5810 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5811 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08005812 bool object_field_get_with_read_barrier =
5813 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08005815 instruction,
5816 generate_volatile
5817 ? LocationSummary::kCallOnMainOnly
5818 : (object_field_get_with_read_barrier
5819 ? LocationSummary::kCallOnSlowPath
5820 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005821
Alexey Frunzec61c0762017-04-10 13:54:23 -07005822 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5823 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5824 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005825 locations->SetInAt(0, Location::RequiresRegister());
5826 if (generate_volatile) {
5827 InvokeRuntimeCallingConvention calling_convention;
5828 // need A0 to hold base + offset
5829 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5830 if (field_type == Primitive::kPrimLong) {
5831 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
5832 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005833 // Use Location::Any() to prevent situations when running out of available fp registers.
5834 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005835 // Need some temp core regs since FP results are returned in core registers
5836 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
5837 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
5838 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
5839 }
5840 } else {
5841 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5842 locations->SetOut(Location::RequiresFpuRegister());
5843 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005844 // The output overlaps in the case of an object field get with
5845 // read barriers enabled: we do not want the move to overwrite the
5846 // object's location, as we need it to emit the read barrier.
5847 locations->SetOut(Location::RequiresRegister(),
5848 object_field_get_with_read_barrier
5849 ? Location::kOutputOverlap
5850 : Location::kNoOutputOverlap);
5851 }
5852 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5853 // We need a temporary register for the read barrier marking slow
5854 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
5855 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005856 }
5857 }
5858}
5859
5860void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
5861 const FieldInfo& field_info,
5862 uint32_t dex_pc) {
5863 Primitive::Type type = field_info.GetFieldType();
5864 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005865 Location obj_loc = locations->InAt(0);
5866 Register obj = obj_loc.AsRegister<Register>();
5867 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005868 LoadOperandType load_type = kLoadUnsignedByte;
5869 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005870 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005871 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005872
5873 switch (type) {
5874 case Primitive::kPrimBoolean:
5875 load_type = kLoadUnsignedByte;
5876 break;
5877 case Primitive::kPrimByte:
5878 load_type = kLoadSignedByte;
5879 break;
5880 case Primitive::kPrimShort:
5881 load_type = kLoadSignedHalfword;
5882 break;
5883 case Primitive::kPrimChar:
5884 load_type = kLoadUnsignedHalfword;
5885 break;
5886 case Primitive::kPrimInt:
5887 case Primitive::kPrimFloat:
5888 case Primitive::kPrimNot:
5889 load_type = kLoadWord;
5890 break;
5891 case Primitive::kPrimLong:
5892 case Primitive::kPrimDouble:
5893 load_type = kLoadDoubleword;
5894 break;
5895 case Primitive::kPrimVoid:
5896 LOG(FATAL) << "Unreachable type " << type;
5897 UNREACHABLE();
5898 }
5899
5900 if (is_volatile && load_type == kLoadDoubleword) {
5901 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005902 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005903 // Do implicit Null check
5904 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5905 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01005906 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005907 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
5908 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005909 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08005910 if (dst_loc.IsFpuRegister()) {
5911 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005912 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08005913 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005914 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005915 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005916 __ StoreToOffset(kStoreWord,
5917 locations->GetTemp(1).AsRegister<Register>(),
5918 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005919 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005920 __ StoreToOffset(kStoreWord,
5921 locations->GetTemp(2).AsRegister<Register>(),
5922 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005923 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005924 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005925 }
5926 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005927 if (type == Primitive::kPrimNot) {
5928 // /* HeapReference<Object> */ dst = *(obj + offset)
5929 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5930 Location temp_loc = locations->GetTemp(0);
5931 // Note that a potential implicit null check is handled in this
5932 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
5933 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5934 dst_loc,
5935 obj,
5936 offset,
5937 temp_loc,
5938 /* needs_null_check */ true);
5939 if (is_volatile) {
5940 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5941 }
5942 } else {
5943 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
5944 if (is_volatile) {
5945 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5946 }
5947 // If read barriers are enabled, emit read barriers other than
5948 // Baker's using a slow path (and also unpoison the loaded
5949 // reference, if heap poisoning is enabled).
5950 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
5951 }
5952 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005953 Register dst;
5954 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08005955 DCHECK(dst_loc.IsRegisterPair());
5956 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005957 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005958 DCHECK(dst_loc.IsRegister());
5959 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005960 }
Alexey Frunze2923db72016-08-20 01:55:47 -07005961 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005962 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005963 DCHECK(dst_loc.IsFpuRegister());
5964 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005965 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005966 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005967 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005968 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005969 }
5970 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005971 }
5972
Alexey Frunze15958152017-02-09 19:08:30 -08005973 // Memory barriers, in the case of references, are handled in the
5974 // previous switch statement.
5975 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005976 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5977 }
5978}
5979
5980void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5981 Primitive::Type field_type = field_info.GetFieldType();
5982 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5983 bool generate_volatile = field_info.IsVolatile() && is_wide;
5984 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005985 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005986
5987 locations->SetInAt(0, Location::RequiresRegister());
5988 if (generate_volatile) {
5989 InvokeRuntimeCallingConvention calling_convention;
5990 // need A0 to hold base + offset
5991 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5992 if (field_type == Primitive::kPrimLong) {
5993 locations->SetInAt(1, Location::RegisterPairLocation(
5994 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5995 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005996 // Use Location::Any() to prevent situations when running out of available fp registers.
5997 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005998 // Pass FP parameters in core registers.
5999 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6000 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6001 }
6002 } else {
6003 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006004 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006005 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006006 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006007 }
6008 }
6009}
6010
6011void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6012 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006013 uint32_t dex_pc,
6014 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006015 Primitive::Type type = field_info.GetFieldType();
6016 LocationSummary* locations = instruction->GetLocations();
6017 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006018 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006019 StoreOperandType store_type = kStoreByte;
6020 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006021 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006022 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006023 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006024
6025 switch (type) {
6026 case Primitive::kPrimBoolean:
6027 case Primitive::kPrimByte:
6028 store_type = kStoreByte;
6029 break;
6030 case Primitive::kPrimShort:
6031 case Primitive::kPrimChar:
6032 store_type = kStoreHalfword;
6033 break;
6034 case Primitive::kPrimInt:
6035 case Primitive::kPrimFloat:
6036 case Primitive::kPrimNot:
6037 store_type = kStoreWord;
6038 break;
6039 case Primitive::kPrimLong:
6040 case Primitive::kPrimDouble:
6041 store_type = kStoreDoubleword;
6042 break;
6043 case Primitive::kPrimVoid:
6044 LOG(FATAL) << "Unreachable type " << type;
6045 UNREACHABLE();
6046 }
6047
6048 if (is_volatile) {
6049 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6050 }
6051
6052 if (is_volatile && store_type == kStoreDoubleword) {
6053 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006054 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006055 // Do implicit Null check.
6056 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6057 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6058 if (type == Primitive::kPrimDouble) {
6059 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006060 if (value_location.IsFpuRegister()) {
6061 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6062 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006063 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006064 value_location.AsFpuRegister<FRegister>());
6065 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006066 __ LoadFromOffset(kLoadWord,
6067 locations->GetTemp(1).AsRegister<Register>(),
6068 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006069 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006070 __ LoadFromOffset(kLoadWord,
6071 locations->GetTemp(2).AsRegister<Register>(),
6072 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006073 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006074 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006075 DCHECK(value_location.IsConstant());
6076 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6077 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006078 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6079 locations->GetTemp(1).AsRegister<Register>(),
6080 value);
6081 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006082 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006083 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006084 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6085 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006086 if (value_location.IsConstant()) {
6087 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6088 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6089 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006090 Register src;
6091 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006092 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006093 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006094 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006095 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006096 if (kPoisonHeapReferences && needs_write_barrier) {
6097 // Note that in the case where `value` is a null reference,
6098 // we do not enter this block, as a null reference does not
6099 // need poisoning.
6100 DCHECK_EQ(type, Primitive::kPrimNot);
6101 __ PoisonHeapReference(TMP, src);
6102 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6103 } else {
6104 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6105 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006106 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006107 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006108 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006109 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006110 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006111 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006112 }
6113 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006114 }
6115
Alexey Frunzec061de12017-02-14 13:27:23 -08006116 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006117 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006118 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006119 }
6120
6121 if (is_volatile) {
6122 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6123 }
6124}
6125
6126void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6127 HandleFieldGet(instruction, instruction->GetFieldInfo());
6128}
6129
6130void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6131 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6132}
6133
6134void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6135 HandleFieldSet(instruction, instruction->GetFieldInfo());
6136}
6137
6138void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006139 HandleFieldSet(instruction,
6140 instruction->GetFieldInfo(),
6141 instruction->GetDexPc(),
6142 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006143}
6144
Alexey Frunze15958152017-02-09 19:08:30 -08006145void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6146 HInstruction* instruction,
6147 Location out,
6148 uint32_t offset,
6149 Location maybe_temp,
6150 ReadBarrierOption read_barrier_option) {
6151 Register out_reg = out.AsRegister<Register>();
6152 if (read_barrier_option == kWithReadBarrier) {
6153 CHECK(kEmitCompilerReadBarrier);
6154 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6155 if (kUseBakerReadBarrier) {
6156 // Load with fast path based Baker's read barrier.
6157 // /* HeapReference<Object> */ out = *(out + offset)
6158 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6159 out,
6160 out_reg,
6161 offset,
6162 maybe_temp,
6163 /* needs_null_check */ false);
6164 } else {
6165 // Load with slow path based read barrier.
6166 // Save the value of `out` into `maybe_temp` before overwriting it
6167 // in the following move operation, as we will need it for the
6168 // read barrier below.
6169 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6170 // /* HeapReference<Object> */ out = *(out + offset)
6171 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6172 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6173 }
6174 } else {
6175 // Plain load with no read barrier.
6176 // /* HeapReference<Object> */ out = *(out + offset)
6177 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6178 __ MaybeUnpoisonHeapReference(out_reg);
6179 }
6180}
6181
6182void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6183 HInstruction* instruction,
6184 Location out,
6185 Location obj,
6186 uint32_t offset,
6187 Location maybe_temp,
6188 ReadBarrierOption read_barrier_option) {
6189 Register out_reg = out.AsRegister<Register>();
6190 Register obj_reg = obj.AsRegister<Register>();
6191 if (read_barrier_option == kWithReadBarrier) {
6192 CHECK(kEmitCompilerReadBarrier);
6193 if (kUseBakerReadBarrier) {
6194 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6195 // Load with fast path based Baker's read barrier.
6196 // /* HeapReference<Object> */ out = *(obj + offset)
6197 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6198 out,
6199 obj_reg,
6200 offset,
6201 maybe_temp,
6202 /* needs_null_check */ false);
6203 } else {
6204 // Load with slow path based read barrier.
6205 // /* HeapReference<Object> */ out = *(obj + offset)
6206 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6207 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6208 }
6209 } else {
6210 // Plain load with no read barrier.
6211 // /* HeapReference<Object> */ out = *(obj + offset)
6212 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6213 __ MaybeUnpoisonHeapReference(out_reg);
6214 }
6215}
6216
6217void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6218 Location root,
6219 Register obj,
6220 uint32_t offset,
6221 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006222 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006223 if (read_barrier_option == kWithReadBarrier) {
6224 DCHECK(kEmitCompilerReadBarrier);
6225 if (kUseBakerReadBarrier) {
6226 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6227 // Baker's read barrier are used:
6228 //
6229 // root = obj.field;
6230 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6231 // if (temp != null) {
6232 // root = temp(root)
6233 // }
6234
6235 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6236 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6237 static_assert(
6238 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6239 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6240 "have different sizes.");
6241 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6242 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6243 "have different sizes.");
6244
6245 // Slow path marking the GC root `root`.
6246 Location temp = Location::RegisterLocation(T9);
6247 SlowPathCodeMIPS* slow_path =
6248 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6249 instruction,
6250 root,
6251 /*entrypoint*/ temp);
6252 codegen_->AddSlowPath(slow_path);
6253
6254 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6255 const int32_t entry_point_offset =
6256 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6257 // Loading the entrypoint does not require a load acquire since it is only changed when
6258 // threads are suspended or running a checkpoint.
6259 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6260 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6261 // checking GetIsGcMarking.
6262 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6263 __ Bind(slow_path->GetExitLabel());
6264 } else {
6265 // GC root loaded through a slow path for read barriers other
6266 // than Baker's.
6267 // /* GcRoot<mirror::Object>* */ root = obj + offset
6268 __ Addiu32(root_reg, obj, offset);
6269 // /* mirror::Object* */ root = root->Read()
6270 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6271 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006272 } else {
6273 // Plain GC root load with no read barrier.
6274 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6275 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6276 // Note that GC roots are not affected by heap poisoning, thus we
6277 // do not have to unpoison `root_reg` here.
6278 }
6279}
6280
Alexey Frunze15958152017-02-09 19:08:30 -08006281void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6282 Location ref,
6283 Register obj,
6284 uint32_t offset,
6285 Location temp,
6286 bool needs_null_check) {
6287 DCHECK(kEmitCompilerReadBarrier);
6288 DCHECK(kUseBakerReadBarrier);
6289
6290 // /* HeapReference<Object> */ ref = *(obj + offset)
6291 Location no_index = Location::NoLocation();
6292 ScaleFactor no_scale_factor = TIMES_1;
6293 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6294 ref,
6295 obj,
6296 offset,
6297 no_index,
6298 no_scale_factor,
6299 temp,
6300 needs_null_check);
6301}
6302
6303void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6304 Location ref,
6305 Register obj,
6306 uint32_t data_offset,
6307 Location index,
6308 Location temp,
6309 bool needs_null_check) {
6310 DCHECK(kEmitCompilerReadBarrier);
6311 DCHECK(kUseBakerReadBarrier);
6312
6313 static_assert(
6314 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6315 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6316 // /* HeapReference<Object> */ ref =
6317 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6318 ScaleFactor scale_factor = TIMES_4;
6319 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6320 ref,
6321 obj,
6322 data_offset,
6323 index,
6324 scale_factor,
6325 temp,
6326 needs_null_check);
6327}
6328
6329void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6330 Location ref,
6331 Register obj,
6332 uint32_t offset,
6333 Location index,
6334 ScaleFactor scale_factor,
6335 Location temp,
6336 bool needs_null_check,
6337 bool always_update_field) {
6338 DCHECK(kEmitCompilerReadBarrier);
6339 DCHECK(kUseBakerReadBarrier);
6340
6341 // In slow path based read barriers, the read barrier call is
6342 // inserted after the original load. However, in fast path based
6343 // Baker's read barriers, we need to perform the load of
6344 // mirror::Object::monitor_ *before* the original reference load.
6345 // This load-load ordering is required by the read barrier.
6346 // The fast path/slow path (for Baker's algorithm) should look like:
6347 //
6348 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6349 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6350 // HeapReference<Object> ref = *src; // Original reference load.
6351 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6352 // if (is_gray) {
6353 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6354 // }
6355 //
6356 // Note: the original implementation in ReadBarrier::Barrier is
6357 // slightly more complex as it performs additional checks that we do
6358 // not do here for performance reasons.
6359
6360 Register ref_reg = ref.AsRegister<Register>();
6361 Register temp_reg = temp.AsRegister<Register>();
6362 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6363
6364 // /* int32_t */ monitor = obj->monitor_
6365 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6366 if (needs_null_check) {
6367 MaybeRecordImplicitNullCheck(instruction);
6368 }
6369 // /* LockWord */ lock_word = LockWord(monitor)
6370 static_assert(sizeof(LockWord) == sizeof(int32_t),
6371 "art::LockWord and int32_t have different sizes.");
6372
6373 __ Sync(0); // Barrier to prevent load-load reordering.
6374
6375 // The actual reference load.
6376 if (index.IsValid()) {
6377 // Load types involving an "index": ArrayGet,
6378 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6379 // intrinsics.
6380 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6381 if (index.IsConstant()) {
6382 size_t computed_offset =
6383 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6384 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6385 } else {
6386 // Handle the special case of the
6387 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6388 // intrinsics, which use a register pair as index ("long
6389 // offset"), of which only the low part contains data.
6390 Register index_reg = index.IsRegisterPair()
6391 ? index.AsRegisterPairLow<Register>()
6392 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006393 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006394 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6395 }
6396 } else {
6397 // /* HeapReference<Object> */ ref = *(obj + offset)
6398 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6399 }
6400
6401 // Object* ref = ref_addr->AsMirrorPtr()
6402 __ MaybeUnpoisonHeapReference(ref_reg);
6403
6404 // Slow path marking the object `ref` when it is gray.
6405 SlowPathCodeMIPS* slow_path;
6406 if (always_update_field) {
6407 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6408 // of the form `obj + field_offset`, where `obj` is a register and
6409 // `field_offset` is a register pair (of which only the lower half
6410 // is used). Thus `offset` and `scale_factor` above are expected
6411 // to be null in this code path.
6412 DCHECK_EQ(offset, 0u);
6413 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6414 slow_path = new (GetGraph()->GetArena())
6415 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6416 ref,
6417 obj,
6418 /* field_offset */ index,
6419 temp_reg);
6420 } else {
6421 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6422 }
6423 AddSlowPath(slow_path);
6424
6425 // if (rb_state == ReadBarrier::GrayState())
6426 // ref = ReadBarrier::Mark(ref);
6427 // Given the numeric representation, it's enough to check the low bit of the
6428 // rb_state. We do that by shifting the bit into the sign bit (31) and
6429 // performing a branch on less than zero.
6430 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6431 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6432 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6433 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6434 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6435 __ Bind(slow_path->GetExitLabel());
6436}
6437
6438void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6439 Location out,
6440 Location ref,
6441 Location obj,
6442 uint32_t offset,
6443 Location index) {
6444 DCHECK(kEmitCompilerReadBarrier);
6445
6446 // Insert a slow path based read barrier *after* the reference load.
6447 //
6448 // If heap poisoning is enabled, the unpoisoning of the loaded
6449 // reference will be carried out by the runtime within the slow
6450 // path.
6451 //
6452 // Note that `ref` currently does not get unpoisoned (when heap
6453 // poisoning is enabled), which is alright as the `ref` argument is
6454 // not used by the artReadBarrierSlow entry point.
6455 //
6456 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6457 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6458 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6459 AddSlowPath(slow_path);
6460
6461 __ B(slow_path->GetEntryLabel());
6462 __ Bind(slow_path->GetExitLabel());
6463}
6464
6465void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6466 Location out,
6467 Location ref,
6468 Location obj,
6469 uint32_t offset,
6470 Location index) {
6471 if (kEmitCompilerReadBarrier) {
6472 // Baker's read barriers shall be handled by the fast path
6473 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6474 DCHECK(!kUseBakerReadBarrier);
6475 // If heap poisoning is enabled, unpoisoning will be taken care of
6476 // by the runtime within the slow path.
6477 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6478 } else if (kPoisonHeapReferences) {
6479 __ UnpoisonHeapReference(out.AsRegister<Register>());
6480 }
6481}
6482
6483void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6484 Location out,
6485 Location root) {
6486 DCHECK(kEmitCompilerReadBarrier);
6487
6488 // Insert a slow path based read barrier *after* the GC root load.
6489 //
6490 // Note that GC roots are not affected by heap poisoning, so we do
6491 // not need to do anything special for this here.
6492 SlowPathCodeMIPS* slow_path =
6493 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6494 AddSlowPath(slow_path);
6495
6496 __ B(slow_path->GetEntryLabel());
6497 __ Bind(slow_path->GetExitLabel());
6498}
6499
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006500void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006501 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6502 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07006503 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006504 switch (type_check_kind) {
6505 case TypeCheckKind::kExactCheck:
6506 case TypeCheckKind::kAbstractClassCheck:
6507 case TypeCheckKind::kClassHierarchyCheck:
6508 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006509 call_kind =
6510 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006511 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006512 break;
6513 case TypeCheckKind::kArrayCheck:
6514 case TypeCheckKind::kUnresolvedCheck:
6515 case TypeCheckKind::kInterfaceCheck:
6516 call_kind = LocationSummary::kCallOnSlowPath;
6517 break;
6518 }
6519
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006521 if (baker_read_barrier_slow_path) {
6522 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6523 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006524 locations->SetInAt(0, Location::RequiresRegister());
6525 locations->SetInAt(1, Location::RequiresRegister());
6526 // The output does overlap inputs.
6527 // Note that TypeCheckSlowPathMIPS uses this register too.
6528 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006529 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006530}
6531
6532void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006533 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006534 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006535 Location obj_loc = locations->InAt(0);
6536 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006537 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006538 Location out_loc = locations->Out();
6539 Register out = out_loc.AsRegister<Register>();
6540 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6541 DCHECK_LE(num_temps, 1u);
6542 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006543 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6544 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6545 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6546 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006547 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006548 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006549
6550 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006551 // Avoid this check if we know `obj` is not null.
6552 if (instruction->MustDoNullCheck()) {
6553 __ Move(out, ZERO);
6554 __ Beqz(obj, &done);
6555 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006556
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006557 switch (type_check_kind) {
6558 case TypeCheckKind::kExactCheck: {
6559 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006560 GenerateReferenceLoadTwoRegisters(instruction,
6561 out_loc,
6562 obj_loc,
6563 class_offset,
6564 maybe_temp_loc,
6565 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006566 // Classes must be equal for the instanceof to succeed.
6567 __ Xor(out, out, cls);
6568 __ Sltiu(out, out, 1);
6569 break;
6570 }
6571
6572 case TypeCheckKind::kAbstractClassCheck: {
6573 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006574 GenerateReferenceLoadTwoRegisters(instruction,
6575 out_loc,
6576 obj_loc,
6577 class_offset,
6578 maybe_temp_loc,
6579 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006580 // If the class is abstract, we eagerly fetch the super class of the
6581 // object to avoid doing a comparison we know will fail.
6582 MipsLabel loop;
6583 __ Bind(&loop);
6584 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006585 GenerateReferenceLoadOneRegister(instruction,
6586 out_loc,
6587 super_offset,
6588 maybe_temp_loc,
6589 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006590 // If `out` is null, we use it for the result, and jump to `done`.
6591 __ Beqz(out, &done);
6592 __ Bne(out, cls, &loop);
6593 __ LoadConst32(out, 1);
6594 break;
6595 }
6596
6597 case TypeCheckKind::kClassHierarchyCheck: {
6598 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006599 GenerateReferenceLoadTwoRegisters(instruction,
6600 out_loc,
6601 obj_loc,
6602 class_offset,
6603 maybe_temp_loc,
6604 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006605 // Walk over the class hierarchy to find a match.
6606 MipsLabel loop, success;
6607 __ Bind(&loop);
6608 __ Beq(out, cls, &success);
6609 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006610 GenerateReferenceLoadOneRegister(instruction,
6611 out_loc,
6612 super_offset,
6613 maybe_temp_loc,
6614 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006615 __ Bnez(out, &loop);
6616 // If `out` is null, we use it for the result, and jump to `done`.
6617 __ B(&done);
6618 __ Bind(&success);
6619 __ LoadConst32(out, 1);
6620 break;
6621 }
6622
6623 case TypeCheckKind::kArrayObjectCheck: {
6624 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006625 GenerateReferenceLoadTwoRegisters(instruction,
6626 out_loc,
6627 obj_loc,
6628 class_offset,
6629 maybe_temp_loc,
6630 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006631 // Do an exact check.
6632 MipsLabel success;
6633 __ Beq(out, cls, &success);
6634 // Otherwise, we need to check that the object's class is a non-primitive array.
6635 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006636 GenerateReferenceLoadOneRegister(instruction,
6637 out_loc,
6638 component_offset,
6639 maybe_temp_loc,
6640 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006641 // If `out` is null, we use it for the result, and jump to `done`.
6642 __ Beqz(out, &done);
6643 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6644 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6645 __ Sltiu(out, out, 1);
6646 __ B(&done);
6647 __ Bind(&success);
6648 __ LoadConst32(out, 1);
6649 break;
6650 }
6651
6652 case TypeCheckKind::kArrayCheck: {
6653 // No read barrier since the slow path will retry upon failure.
6654 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006655 GenerateReferenceLoadTwoRegisters(instruction,
6656 out_loc,
6657 obj_loc,
6658 class_offset,
6659 maybe_temp_loc,
6660 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006661 DCHECK(locations->OnlyCallsOnSlowPath());
6662 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6663 /* is_fatal */ false);
6664 codegen_->AddSlowPath(slow_path);
6665 __ Bne(out, cls, slow_path->GetEntryLabel());
6666 __ LoadConst32(out, 1);
6667 break;
6668 }
6669
6670 case TypeCheckKind::kUnresolvedCheck:
6671 case TypeCheckKind::kInterfaceCheck: {
6672 // Note that we indeed only call on slow path, but we always go
6673 // into the slow path for the unresolved and interface check
6674 // cases.
6675 //
6676 // We cannot directly call the InstanceofNonTrivial runtime
6677 // entry point without resorting to a type checking slow path
6678 // here (i.e. by calling InvokeRuntime directly), as it would
6679 // require to assign fixed registers for the inputs of this
6680 // HInstanceOf instruction (following the runtime calling
6681 // convention), which might be cluttered by the potential first
6682 // read barrier emission at the beginning of this method.
6683 //
6684 // TODO: Introduce a new runtime entry point taking the object
6685 // to test (instead of its class) as argument, and let it deal
6686 // with the read barrier issues. This will let us refactor this
6687 // case of the `switch` code as it was previously (with a direct
6688 // call to the runtime not using a type checking slow path).
6689 // This should also be beneficial for the other cases above.
6690 DCHECK(locations->OnlyCallsOnSlowPath());
6691 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6692 /* is_fatal */ false);
6693 codegen_->AddSlowPath(slow_path);
6694 __ B(slow_path->GetEntryLabel());
6695 break;
6696 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006697 }
6698
6699 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006700
6701 if (slow_path != nullptr) {
6702 __ Bind(slow_path->GetExitLabel());
6703 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006704}
6705
6706void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6707 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6708 locations->SetOut(Location::ConstantLocation(constant));
6709}
6710
6711void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6712 // Will be generated at use site.
6713}
6714
6715void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6716 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6717 locations->SetOut(Location::ConstantLocation(constant));
6718}
6719
6720void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6721 // Will be generated at use site.
6722}
6723
6724void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6725 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6726 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6727}
6728
6729void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6730 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006731 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006733 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734}
6735
6736void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6737 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6738 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739 Location receiver = invoke->GetLocations()->InAt(0);
6740 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006741 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006742
6743 // Set the hidden argument.
6744 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6745 invoke->GetDexMethodIndex());
6746
6747 // temp = object->GetClass();
6748 if (receiver.IsStackSlot()) {
6749 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6750 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6751 } else {
6752 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6753 }
6754 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006755 // Instead of simply (possibly) unpoisoning `temp` here, we should
6756 // emit a read barrier for the previous class reference load.
6757 // However this is not required in practice, as this is an
6758 // intermediate/temporary reference and because the current
6759 // concurrent copying collector keeps the from-space memory
6760 // intact/accessible until the end of the marking phase (the
6761 // concurrent copying collector may not in the future).
6762 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006763 __ LoadFromOffset(kLoadWord, temp, temp,
6764 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6765 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006766 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006767 // temp = temp->GetImtEntryAt(method_offset);
6768 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6769 // T9 = temp->GetEntryPoint();
6770 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6771 // T9();
6772 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006773 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006774 DCHECK(!codegen_->IsLeafMethod());
6775 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6776}
6777
6778void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006779 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6780 if (intrinsic.TryDispatch(invoke)) {
6781 return;
6782 }
6783
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006784 HandleInvoke(invoke);
6785}
6786
6787void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006788 // Explicit clinit checks triggered by static invokes must have been pruned by
6789 // art::PrepareForRegisterAllocation.
6790 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006791
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006792 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6793 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006794
Chris Larsen701566a2015-10-27 15:29:13 -07006795 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6796 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006797 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6798 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6799 }
Chris Larsen701566a2015-10-27 15:29:13 -07006800 return;
6801 }
6802
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006803 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006804
6805 // Add the extra input register if either the dex cache array base register
6806 // or the PC-relative base register for accessing literals is needed.
6807 if (has_extra_input) {
6808 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
6809 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006810}
6811
Orion Hodsonac141392017-01-13 11:53:47 +00006812void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6813 HandleInvoke(invoke);
6814}
6815
6816void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6817 codegen_->GenerateInvokePolymorphicCall(invoke);
6818}
6819
Chris Larsen701566a2015-10-27 15:29:13 -07006820static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006821 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07006822 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
6823 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006824 return true;
6825 }
6826 return false;
6827}
6828
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006829HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07006830 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006831 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006832 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006833 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
6834 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07006835 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006836 bool is_r6 = GetInstructionSetFeatures().IsR6();
6837 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006838 switch (desired_string_load_kind) {
6839 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6840 DCHECK(!GetCompilerOptions().GetCompilePic());
6841 break;
6842 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6843 DCHECK(GetCompilerOptions().GetCompilePic());
6844 break;
6845 case HLoadString::LoadKind::kBootImageAddress:
6846 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006847 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006848 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006849 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006850 case HLoadString::LoadKind::kJitTableAddress:
6851 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006852 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006853 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006854 case HLoadString::LoadKind::kDexCacheViaMethod:
6855 fallback_load = false;
6856 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006857 }
6858 if (fallback_load) {
6859 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
6860 }
6861 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006862}
6863
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006864HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
6865 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006866 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006867 // is incompatible with it.
6868 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006869 bool is_r6 = GetInstructionSetFeatures().IsR6();
6870 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006871 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006872 case HLoadClass::LoadKind::kInvalid:
6873 LOG(FATAL) << "UNREACHABLE";
6874 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07006875 case HLoadClass::LoadKind::kReferrersClass:
6876 fallback_load = false;
6877 break;
6878 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6879 DCHECK(!GetCompilerOptions().GetCompilePic());
6880 break;
6881 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6882 DCHECK(GetCompilerOptions().GetCompilePic());
6883 break;
6884 case HLoadClass::LoadKind::kBootImageAddress:
6885 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006886 case HLoadClass::LoadKind::kBssEntry:
6887 DCHECK(!Runtime::Current()->UseJitCompilation());
6888 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006889 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006890 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006891 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006892 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006893 case HLoadClass::LoadKind::kDexCacheViaMethod:
6894 fallback_load = false;
6895 break;
6896 }
6897 if (fallback_load) {
6898 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
6899 }
6900 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006901}
6902
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006903Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6904 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006905 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006906 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6907 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6908 if (!invoke->GetLocations()->Intrinsified()) {
6909 return location.AsRegister<Register>();
6910 }
6911 // For intrinsics we allow any location, so it may be on the stack.
6912 if (!location.IsRegister()) {
6913 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6914 return temp;
6915 }
6916 // For register locations, check if the register was saved. If so, get it from the stack.
6917 // Note: There is a chance that the register was saved but not overwritten, so we could
6918 // save one load. However, since this is just an intrinsic slow path we prefer this
6919 // simple and more robust approach rather that trying to determine if that's the case.
6920 SlowPathCode* slow_path = GetCurrentSlowPath();
6921 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6922 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6923 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6924 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6925 return temp;
6926 }
6927 return location.AsRegister<Register>();
6928}
6929
Vladimir Markodc151b22015-10-15 18:02:30 +01006930HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
6931 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006932 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006933 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006934 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006935 // is incompatible with it.
6936 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006937 bool is_r6 = GetInstructionSetFeatures().IsR6();
6938 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006939 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006940 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006941 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01006942 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006943 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01006944 break;
6945 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006946 if (fallback_load) {
6947 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6948 dispatch_info.method_load_data = 0;
6949 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006950 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006951}
6952
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006953void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
6954 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006955 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006956 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
6957 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006958 bool is_r6 = GetInstructionSetFeatures().IsR6();
6959 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006960 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
6961 : ZERO;
6962
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006963 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006964 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006965 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006966 uint32_t offset =
6967 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006968 __ LoadFromOffset(kLoadWord,
6969 temp.AsRegister<Register>(),
6970 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006971 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006972 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006973 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006974 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006975 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006976 break;
6977 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6978 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6979 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006980 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
6981 if (is_r6) {
6982 uint32_t offset = invoke->GetDexCacheArrayOffset();
6983 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6984 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
6985 bool reordering = __ SetReorder(false);
6986 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
6987 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
6988 __ SetReorder(reordering);
6989 } else {
6990 HMipsDexCacheArraysBase* base =
6991 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
6992 int32_t offset =
6993 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
6994 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6995 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006996 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006997 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006998 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006999 Register reg = temp.AsRegister<Register>();
7000 Register method_reg;
7001 if (current_method.IsRegister()) {
7002 method_reg = current_method.AsRegister<Register>();
7003 } else {
7004 // TODO: use the appropriate DCHECK() here if possible.
7005 // DCHECK(invoke->GetLocations()->Intrinsified());
7006 DCHECK(!current_method.IsValid());
7007 method_reg = reg;
7008 __ Lw(reg, SP, kCurrentMethodStackOffset);
7009 }
7010
7011 // temp = temp->dex_cache_resolved_methods_;
7012 __ LoadFromOffset(kLoadWord,
7013 reg,
7014 method_reg,
7015 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007016 // temp = temp[index_in_cache];
7017 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7018 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007019 __ LoadFromOffset(kLoadWord,
7020 reg,
7021 reg,
7022 CodeGenerator::GetCachePointerOffset(index_in_cache));
7023 break;
7024 }
7025 }
7026
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007027 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007028 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007029 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007030 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007031 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7032 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007033 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007034 T9,
7035 callee_method.AsRegister<Register>(),
7036 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007037 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007038 // T9()
7039 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007040 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007041 break;
7042 }
7043 DCHECK(!IsLeafMethod());
7044}
7045
7046void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007047 // Explicit clinit checks triggered by static invokes must have been pruned by
7048 // art::PrepareForRegisterAllocation.
7049 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007050
7051 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7052 return;
7053 }
7054
7055 LocationSummary* locations = invoke->GetLocations();
7056 codegen_->GenerateStaticOrDirectCall(invoke,
7057 locations->HasTemps()
7058 ? locations->GetTemp(0)
7059 : Location::NoLocation());
7060 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7061}
7062
Chris Larsen3acee732015-11-18 13:31:08 -08007063void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007064 // Use the calling convention instead of the location of the receiver, as
7065 // intrinsics may have put the receiver in a different register. In the intrinsics
7066 // slow path, the arguments have been moved to the right place, so here we are
7067 // guaranteed that the receiver is the first register of the calling convention.
7068 InvokeDexCallingConvention calling_convention;
7069 Register receiver = calling_convention.GetRegisterAt(0);
7070
Chris Larsen3acee732015-11-18 13:31:08 -08007071 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007072 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7073 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7074 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007075 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007076
7077 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007078 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007079 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007080 // Instead of simply (possibly) unpoisoning `temp` here, we should
7081 // emit a read barrier for the previous class reference load.
7082 // However this is not required in practice, as this is an
7083 // intermediate/temporary reference and because the current
7084 // concurrent copying collector keeps the from-space memory
7085 // intact/accessible until the end of the marking phase (the
7086 // concurrent copying collector may not in the future).
7087 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007088 // temp = temp->GetMethodAt(method_offset);
7089 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7090 // T9 = temp->GetEntryPoint();
7091 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7092 // T9();
7093 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007094 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08007095}
7096
7097void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7098 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7099 return;
7100 }
7101
7102 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007103 DCHECK(!codegen_->IsLeafMethod());
7104 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7105}
7106
7107void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007108 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7109 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007110 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007111 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7112 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007113 return;
7114 }
Vladimir Marko41559982017-01-06 14:04:23 +00007115 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007116 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007117 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7118 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007119 ? LocationSummary::kCallOnSlowPath
7120 : LocationSummary::kNoCall;
7121 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007122 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7123 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7124 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007125 switch (load_kind) {
7126 // We need an extra register for PC-relative literals on R2.
7127 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007128 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007129 case HLoadClass::LoadKind::kBootImageAddress:
7130 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007131 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007132 break;
7133 }
7134 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007135 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007136 locations->SetInAt(0, Location::RequiresRegister());
7137 break;
7138 default:
7139 break;
7140 }
7141 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007142 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7143 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7144 // Rely on the type resolution or initialization and marking to save everything we need.
7145 // Request a temp to hold the BSS entry location for the slow path on R2
7146 // (no benefit for R6).
7147 if (!isR6) {
7148 locations->AddTemp(Location::RequiresRegister());
7149 }
7150 RegisterSet caller_saves = RegisterSet::Empty();
7151 InvokeRuntimeCallingConvention calling_convention;
7152 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7153 locations->SetCustomSlowPathCallerSaves(caller_saves);
7154 } else {
7155 // For non-Baker read barriers we have a temp-clobbering call.
7156 }
7157 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007158}
7159
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007160// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7161// move.
7162void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007163 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7164 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7165 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007166 return;
7167 }
Vladimir Marko41559982017-01-06 14:04:23 +00007168 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007169
Vladimir Marko41559982017-01-06 14:04:23 +00007170 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007171 Location out_loc = locations->Out();
7172 Register out = out_loc.AsRegister<Register>();
7173 Register base_or_current_method_reg;
7174 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7175 switch (load_kind) {
7176 // We need an extra register for PC-relative literals on R2.
7177 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007178 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007179 case HLoadClass::LoadKind::kBootImageAddress:
7180 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007181 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7182 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007183 case HLoadClass::LoadKind::kReferrersClass:
7184 case HLoadClass::LoadKind::kDexCacheViaMethod:
7185 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7186 break;
7187 default:
7188 base_or_current_method_reg = ZERO;
7189 break;
7190 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007191
Alexey Frunze15958152017-02-09 19:08:30 -08007192 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7193 ? kWithoutReadBarrier
7194 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007195 bool generate_null_check = false;
7196 switch (load_kind) {
7197 case HLoadClass::LoadKind::kReferrersClass: {
7198 DCHECK(!cls->CanCallRuntime());
7199 DCHECK(!cls->MustGenerateClinitCheck());
7200 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7201 GenerateGcRootFieldLoad(cls,
7202 out_loc,
7203 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007204 ArtMethod::DeclaringClassOffset().Int32Value(),
7205 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007206 break;
7207 }
7208 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007209 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007210 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007211 __ LoadLiteral(out,
7212 base_or_current_method_reg,
7213 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
7214 cls->GetTypeIndex()));
7215 break;
7216 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007217 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007218 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007219 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7220 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007221 bool reordering = __ SetReorder(false);
7222 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7223 __ Addiu(out, out, /* placeholder */ 0x5678);
7224 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007225 break;
7226 }
7227 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007228 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007229 uint32_t address = dchecked_integral_cast<uint32_t>(
7230 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7231 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007232 __ LoadLiteral(out,
7233 base_or_current_method_reg,
7234 codegen_->DeduplicateBootImageAddressLiteral(address));
7235 break;
7236 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007237 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007238 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007239 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007240 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7241 if (isR6 || non_baker_read_barrier) {
7242 bool reordering = __ SetReorder(false);
7243 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7244 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
7245 __ SetReorder(reordering);
7246 } else {
7247 // On R2 save the BSS entry address in a temporary register instead of
7248 // recalculating it in the slow path.
7249 Register temp = locations->GetTemp(0).AsRegister<Register>();
7250 bool reordering = __ SetReorder(false);
7251 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7252 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7253 __ SetReorder(reordering);
7254 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
7255 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007256 generate_null_check = true;
7257 break;
7258 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007259 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007260 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7261 cls->GetTypeIndex(),
7262 cls->GetClass());
7263 bool reordering = __ SetReorder(false);
7264 __ Bind(&info->high_label);
7265 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007266 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007267 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007268 break;
7269 }
Vladimir Marko41559982017-01-06 14:04:23 +00007270 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007271 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007272 LOG(FATAL) << "UNREACHABLE";
7273 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007274 }
7275
7276 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7277 DCHECK(cls->CanCallRuntime());
7278 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7279 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7280 codegen_->AddSlowPath(slow_path);
7281 if (generate_null_check) {
7282 __ Beqz(out, slow_path->GetEntryLabel());
7283 }
7284 if (cls->MustGenerateClinitCheck()) {
7285 GenerateClassInitializationCheck(slow_path, out);
7286 } else {
7287 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007288 }
7289 }
7290}
7291
7292static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007293 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007294}
7295
7296void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7297 LocationSummary* locations =
7298 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7299 locations->SetOut(Location::RequiresRegister());
7300}
7301
7302void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7303 Register out = load->GetLocations()->Out().AsRegister<Register>();
7304 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7305}
7306
7307void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7308 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7309}
7310
7311void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7312 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7313}
7314
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007315void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007316 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007318 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007319 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007320 switch (load_kind) {
7321 // We need an extra register for PC-relative literals on R2.
7322 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7323 case HLoadString::LoadKind::kBootImageAddress:
7324 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007325 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007326 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007327 break;
7328 }
7329 FALLTHROUGH_INTENDED;
7330 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007331 case HLoadString::LoadKind::kDexCacheViaMethod:
7332 locations->SetInAt(0, Location::RequiresRegister());
7333 break;
7334 default:
7335 break;
7336 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007337 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
7338 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007339 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007340 } else {
7341 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007342 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7343 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7344 // Rely on the pResolveString 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 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007358 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007359}
7360
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007361// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7362// move.
7363void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007364 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007365 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007366 Location out_loc = locations->Out();
7367 Register out = out_loc.AsRegister<Register>();
7368 Register base_or_current_method_reg;
7369 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7370 switch (load_kind) {
7371 // We need an extra register for PC-relative literals on R2.
7372 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7373 case HLoadString::LoadKind::kBootImageAddress:
7374 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007375 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007376 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7377 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007378 default:
7379 base_or_current_method_reg = ZERO;
7380 break;
7381 }
7382
7383 switch (load_kind) {
7384 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007385 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007386 __ LoadLiteral(out,
7387 base_or_current_method_reg,
7388 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7389 load->GetStringIndex()));
7390 return; // No dex cache slow path.
7391 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007392 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007393 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007394 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007395 bool reordering = __ SetReorder(false);
7396 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7397 __ Addiu(out, out, /* placeholder */ 0x5678);
7398 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007399 return; // No dex cache slow path.
7400 }
7401 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007402 uint32_t address = dchecked_integral_cast<uint32_t>(
7403 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7404 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007405 __ LoadLiteral(out,
7406 base_or_current_method_reg,
7407 codegen_->DeduplicateBootImageAddressLiteral(address));
7408 return; // No dex cache slow path.
7409 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007410 case HLoadString::LoadKind::kBssEntry: {
7411 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7412 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007413 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007414 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7415 if (isR6 || non_baker_read_barrier) {
7416 bool reordering = __ SetReorder(false);
7417 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7418 GenerateGcRootFieldLoad(load,
7419 out_loc,
7420 out,
7421 /* placeholder */ 0x5678,
7422 kCompilerReadBarrierOption);
7423 __ SetReorder(reordering);
7424 } else {
7425 // On R2 save the BSS entry address in a temporary register instead of
7426 // recalculating it in the slow path.
7427 Register temp = locations->GetTemp(0).AsRegister<Register>();
7428 bool reordering = __ SetReorder(false);
7429 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7430 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7431 __ SetReorder(reordering);
7432 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7433 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007434 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7435 codegen_->AddSlowPath(slow_path);
7436 __ Beqz(out, slow_path->GetEntryLabel());
7437 __ Bind(slow_path->GetExitLabel());
7438 return;
7439 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007440 case HLoadString::LoadKind::kJitTableAddress: {
7441 CodeGeneratorMIPS::JitPatchInfo* info =
7442 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7443 load->GetStringIndex(),
7444 load->GetString());
7445 bool reordering = __ SetReorder(false);
7446 __ Bind(&info->high_label);
7447 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007448 GenerateGcRootFieldLoad(load,
7449 out_loc,
7450 out,
7451 /* placeholder */ 0x5678,
7452 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007453 __ SetReorder(reordering);
7454 return;
7455 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007456 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007457 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007458 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007459
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007460 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007461 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7462 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007463 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007464 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007465 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7466 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007467}
7468
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007469void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7470 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7471 locations->SetOut(Location::ConstantLocation(constant));
7472}
7473
7474void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7475 // Will be generated at use site.
7476}
7477
7478void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7479 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007481 InvokeRuntimeCallingConvention calling_convention;
7482 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7483}
7484
7485void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7486 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007487 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007488 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7489 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007490 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007491 }
7492 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7493}
7494
7495void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7496 LocationSummary* locations =
7497 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7498 switch (mul->GetResultType()) {
7499 case Primitive::kPrimInt:
7500 case Primitive::kPrimLong:
7501 locations->SetInAt(0, Location::RequiresRegister());
7502 locations->SetInAt(1, Location::RequiresRegister());
7503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7504 break;
7505
7506 case Primitive::kPrimFloat:
7507 case Primitive::kPrimDouble:
7508 locations->SetInAt(0, Location::RequiresFpuRegister());
7509 locations->SetInAt(1, Location::RequiresFpuRegister());
7510 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7511 break;
7512
7513 default:
7514 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7515 }
7516}
7517
7518void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7519 Primitive::Type type = instruction->GetType();
7520 LocationSummary* locations = instruction->GetLocations();
7521 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7522
7523 switch (type) {
7524 case Primitive::kPrimInt: {
7525 Register dst = locations->Out().AsRegister<Register>();
7526 Register lhs = locations->InAt(0).AsRegister<Register>();
7527 Register rhs = locations->InAt(1).AsRegister<Register>();
7528
7529 if (isR6) {
7530 __ MulR6(dst, lhs, rhs);
7531 } else {
7532 __ MulR2(dst, lhs, rhs);
7533 }
7534 break;
7535 }
7536 case Primitive::kPrimLong: {
7537 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7538 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7539 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7540 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7541 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7542 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7543
7544 // Extra checks to protect caused by the existance of A1_A2.
7545 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7546 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7547 DCHECK_NE(dst_high, lhs_low);
7548 DCHECK_NE(dst_high, rhs_low);
7549
7550 // A_B * C_D
7551 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7552 // dst_lo: [ low(B*D) ]
7553 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7554
7555 if (isR6) {
7556 __ MulR6(TMP, lhs_high, rhs_low);
7557 __ MulR6(dst_high, lhs_low, rhs_high);
7558 __ Addu(dst_high, dst_high, TMP);
7559 __ MuhuR6(TMP, lhs_low, rhs_low);
7560 __ Addu(dst_high, dst_high, TMP);
7561 __ MulR6(dst_low, lhs_low, rhs_low);
7562 } else {
7563 __ MulR2(TMP, lhs_high, rhs_low);
7564 __ MulR2(dst_high, lhs_low, rhs_high);
7565 __ Addu(dst_high, dst_high, TMP);
7566 __ MultuR2(lhs_low, rhs_low);
7567 __ Mfhi(TMP);
7568 __ Addu(dst_high, dst_high, TMP);
7569 __ Mflo(dst_low);
7570 }
7571 break;
7572 }
7573 case Primitive::kPrimFloat:
7574 case Primitive::kPrimDouble: {
7575 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7576 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7577 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7578 if (type == Primitive::kPrimFloat) {
7579 __ MulS(dst, lhs, rhs);
7580 } else {
7581 __ MulD(dst, lhs, rhs);
7582 }
7583 break;
7584 }
7585 default:
7586 LOG(FATAL) << "Unexpected mul type " << type;
7587 }
7588}
7589
7590void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7591 LocationSummary* locations =
7592 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7593 switch (neg->GetResultType()) {
7594 case Primitive::kPrimInt:
7595 case Primitive::kPrimLong:
7596 locations->SetInAt(0, Location::RequiresRegister());
7597 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7598 break;
7599
7600 case Primitive::kPrimFloat:
7601 case Primitive::kPrimDouble:
7602 locations->SetInAt(0, Location::RequiresFpuRegister());
7603 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7604 break;
7605
7606 default:
7607 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7608 }
7609}
7610
7611void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7612 Primitive::Type type = instruction->GetType();
7613 LocationSummary* locations = instruction->GetLocations();
7614
7615 switch (type) {
7616 case Primitive::kPrimInt: {
7617 Register dst = locations->Out().AsRegister<Register>();
7618 Register src = locations->InAt(0).AsRegister<Register>();
7619 __ Subu(dst, ZERO, src);
7620 break;
7621 }
7622 case Primitive::kPrimLong: {
7623 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7624 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7625 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7626 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7627 __ Subu(dst_low, ZERO, src_low);
7628 __ Sltu(TMP, ZERO, dst_low);
7629 __ Subu(dst_high, ZERO, src_high);
7630 __ Subu(dst_high, dst_high, TMP);
7631 break;
7632 }
7633 case Primitive::kPrimFloat:
7634 case Primitive::kPrimDouble: {
7635 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7636 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7637 if (type == Primitive::kPrimFloat) {
7638 __ NegS(dst, src);
7639 } else {
7640 __ NegD(dst, src);
7641 }
7642 break;
7643 }
7644 default:
7645 LOG(FATAL) << "Unexpected neg type " << type;
7646 }
7647}
7648
7649void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7650 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007651 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007652 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007653 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007654 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7655 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007656}
7657
7658void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007659 // Note: if heap poisoning is enabled, the entry point takes care
7660 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007661 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7662 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007663}
7664
7665void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7666 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007668 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007669 if (instruction->IsStringAlloc()) {
7670 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7671 } else {
7672 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007673 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007674 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7675}
7676
7677void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007678 // Note: if heap poisoning is enabled, the entry point takes care
7679 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007680 if (instruction->IsStringAlloc()) {
7681 // String is allocated through StringFactory. Call NewEmptyString entry point.
7682 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007683 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007684 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7685 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7686 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007687 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007688 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7689 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007690 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007691 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007692 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007693}
7694
7695void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7696 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7697 locations->SetInAt(0, Location::RequiresRegister());
7698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7699}
7700
7701void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7702 Primitive::Type type = instruction->GetType();
7703 LocationSummary* locations = instruction->GetLocations();
7704
7705 switch (type) {
7706 case Primitive::kPrimInt: {
7707 Register dst = locations->Out().AsRegister<Register>();
7708 Register src = locations->InAt(0).AsRegister<Register>();
7709 __ Nor(dst, src, ZERO);
7710 break;
7711 }
7712
7713 case Primitive::kPrimLong: {
7714 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7715 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7716 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7717 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7718 __ Nor(dst_high, src_high, ZERO);
7719 __ Nor(dst_low, src_low, ZERO);
7720 break;
7721 }
7722
7723 default:
7724 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7725 }
7726}
7727
7728void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7729 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7730 locations->SetInAt(0, Location::RequiresRegister());
7731 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7732}
7733
7734void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7735 LocationSummary* locations = instruction->GetLocations();
7736 __ Xori(locations->Out().AsRegister<Register>(),
7737 locations->InAt(0).AsRegister<Register>(),
7738 1);
7739}
7740
7741void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007742 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7743 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007744}
7745
Calin Juravle2ae48182016-03-16 14:05:09 +00007746void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7747 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007748 return;
7749 }
7750 Location obj = instruction->GetLocations()->InAt(0);
7751
7752 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007753 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007754}
7755
Calin Juravle2ae48182016-03-16 14:05:09 +00007756void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007757 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007758 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007759
7760 Location obj = instruction->GetLocations()->InAt(0);
7761
7762 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7763}
7764
7765void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007766 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007767}
7768
7769void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7770 HandleBinaryOp(instruction);
7771}
7772
7773void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7774 HandleBinaryOp(instruction);
7775}
7776
7777void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7778 LOG(FATAL) << "Unreachable";
7779}
7780
7781void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7782 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7783}
7784
7785void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7786 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7787 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7788 if (location.IsStackSlot()) {
7789 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7790 } else if (location.IsDoubleStackSlot()) {
7791 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7792 }
7793 locations->SetOut(location);
7794}
7795
7796void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7797 ATTRIBUTE_UNUSED) {
7798 // Nothing to do, the parameter is already at its location.
7799}
7800
7801void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
7802 LocationSummary* locations =
7803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7804 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
7805}
7806
7807void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
7808 ATTRIBUTE_UNUSED) {
7809 // Nothing to do, the method is already at its location.
7810}
7811
7812void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
7813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01007814 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007815 locations->SetInAt(i, Location::Any());
7816 }
7817 locations->SetOut(Location::Any());
7818}
7819
7820void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
7821 LOG(FATAL) << "Unreachable";
7822}
7823
7824void LocationsBuilderMIPS::VisitRem(HRem* rem) {
7825 Primitive::Type type = rem->GetResultType();
7826 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007827 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007828 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
7829
7830 switch (type) {
7831 case Primitive::kPrimInt:
7832 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08007833 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007834 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7835 break;
7836
7837 case Primitive::kPrimLong: {
7838 InvokeRuntimeCallingConvention calling_convention;
7839 locations->SetInAt(0, Location::RegisterPairLocation(
7840 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7841 locations->SetInAt(1, Location::RegisterPairLocation(
7842 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
7843 locations->SetOut(calling_convention.GetReturnLocation(type));
7844 break;
7845 }
7846
7847 case Primitive::kPrimFloat:
7848 case Primitive::kPrimDouble: {
7849 InvokeRuntimeCallingConvention calling_convention;
7850 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
7851 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
7852 locations->SetOut(calling_convention.GetReturnLocation(type));
7853 break;
7854 }
7855
7856 default:
7857 LOG(FATAL) << "Unexpected rem type " << type;
7858 }
7859}
7860
7861void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
7862 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007863
7864 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08007865 case Primitive::kPrimInt:
7866 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007867 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007868 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007869 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007870 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
7871 break;
7872 }
7873 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007874 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007875 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007876 break;
7877 }
7878 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007879 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007880 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007881 break;
7882 }
7883 default:
7884 LOG(FATAL) << "Unexpected rem type " << type;
7885 }
7886}
7887
Igor Murashkind01745e2017-04-05 16:40:31 -07007888void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
7889 constructor_fence->SetLocations(nullptr);
7890}
7891
7892void InstructionCodeGeneratorMIPS::VisitConstructorFence(
7893 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
7894 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
7895}
7896
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007897void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7898 memory_barrier->SetLocations(nullptr);
7899}
7900
7901void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7902 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7903}
7904
7905void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
7906 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
7907 Primitive::Type return_type = ret->InputAt(0)->GetType();
7908 locations->SetInAt(0, MipsReturnLocation(return_type));
7909}
7910
7911void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7912 codegen_->GenerateFrameExit();
7913}
7914
7915void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
7916 ret->SetLocations(nullptr);
7917}
7918
7919void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7920 codegen_->GenerateFrameExit();
7921}
7922
Alexey Frunze92d90602015-12-18 18:16:36 -08007923void LocationsBuilderMIPS::VisitRor(HRor* ror) {
7924 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007925}
7926
Alexey Frunze92d90602015-12-18 18:16:36 -08007927void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
7928 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007929}
7930
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007931void LocationsBuilderMIPS::VisitShl(HShl* shl) {
7932 HandleShift(shl);
7933}
7934
7935void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
7936 HandleShift(shl);
7937}
7938
7939void LocationsBuilderMIPS::VisitShr(HShr* shr) {
7940 HandleShift(shr);
7941}
7942
7943void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
7944 HandleShift(shr);
7945}
7946
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007947void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
7948 HandleBinaryOp(instruction);
7949}
7950
7951void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
7952 HandleBinaryOp(instruction);
7953}
7954
7955void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7956 HandleFieldGet(instruction, instruction->GetFieldInfo());
7957}
7958
7959void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7960 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
7961}
7962
7963void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7964 HandleFieldSet(instruction, instruction->GetFieldInfo());
7965}
7966
7967void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01007968 HandleFieldSet(instruction,
7969 instruction->GetFieldInfo(),
7970 instruction->GetDexPc(),
7971 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007972}
7973
7974void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
7975 HUnresolvedInstanceFieldGet* instruction) {
7976 FieldAccessCallingConventionMIPS calling_convention;
7977 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7978 instruction->GetFieldType(),
7979 calling_convention);
7980}
7981
7982void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
7983 HUnresolvedInstanceFieldGet* instruction) {
7984 FieldAccessCallingConventionMIPS calling_convention;
7985 codegen_->GenerateUnresolvedFieldAccess(instruction,
7986 instruction->GetFieldType(),
7987 instruction->GetFieldIndex(),
7988 instruction->GetDexPc(),
7989 calling_convention);
7990}
7991
7992void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
7993 HUnresolvedInstanceFieldSet* instruction) {
7994 FieldAccessCallingConventionMIPS calling_convention;
7995 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7996 instruction->GetFieldType(),
7997 calling_convention);
7998}
7999
8000void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8001 HUnresolvedInstanceFieldSet* instruction) {
8002 FieldAccessCallingConventionMIPS calling_convention;
8003 codegen_->GenerateUnresolvedFieldAccess(instruction,
8004 instruction->GetFieldType(),
8005 instruction->GetFieldIndex(),
8006 instruction->GetDexPc(),
8007 calling_convention);
8008}
8009
8010void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8011 HUnresolvedStaticFieldGet* instruction) {
8012 FieldAccessCallingConventionMIPS calling_convention;
8013 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8014 instruction->GetFieldType(),
8015 calling_convention);
8016}
8017
8018void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8019 HUnresolvedStaticFieldGet* instruction) {
8020 FieldAccessCallingConventionMIPS calling_convention;
8021 codegen_->GenerateUnresolvedFieldAccess(instruction,
8022 instruction->GetFieldType(),
8023 instruction->GetFieldIndex(),
8024 instruction->GetDexPc(),
8025 calling_convention);
8026}
8027
8028void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8029 HUnresolvedStaticFieldSet* instruction) {
8030 FieldAccessCallingConventionMIPS calling_convention;
8031 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8032 instruction->GetFieldType(),
8033 calling_convention);
8034}
8035
8036void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8037 HUnresolvedStaticFieldSet* instruction) {
8038 FieldAccessCallingConventionMIPS calling_convention;
8039 codegen_->GenerateUnresolvedFieldAccess(instruction,
8040 instruction->GetFieldType(),
8041 instruction->GetFieldIndex(),
8042 instruction->GetDexPc(),
8043 calling_convention);
8044}
8045
8046void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008047 LocationSummary* locations =
8048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01008049 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008050}
8051
8052void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8053 HBasicBlock* block = instruction->GetBlock();
8054 if (block->GetLoopInformation() != nullptr) {
8055 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8056 // The back edge will generate the suspend check.
8057 return;
8058 }
8059 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8060 // The goto will generate the suspend check.
8061 return;
8062 }
8063 GenerateSuspendCheck(instruction, nullptr);
8064}
8065
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008066void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8067 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008069 InvokeRuntimeCallingConvention calling_convention;
8070 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8071}
8072
8073void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008074 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008075 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8076}
8077
8078void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8079 Primitive::Type input_type = conversion->GetInputType();
8080 Primitive::Type result_type = conversion->GetResultType();
8081 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008082 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008083
8084 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8085 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8086 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8087 }
8088
8089 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008090 if (!isR6 &&
8091 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8092 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008093 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008094 }
8095
8096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8097
8098 if (call_kind == LocationSummary::kNoCall) {
8099 if (Primitive::IsFloatingPointType(input_type)) {
8100 locations->SetInAt(0, Location::RequiresFpuRegister());
8101 } else {
8102 locations->SetInAt(0, Location::RequiresRegister());
8103 }
8104
8105 if (Primitive::IsFloatingPointType(result_type)) {
8106 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8107 } else {
8108 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8109 }
8110 } else {
8111 InvokeRuntimeCallingConvention calling_convention;
8112
8113 if (Primitive::IsFloatingPointType(input_type)) {
8114 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8115 } else {
8116 DCHECK_EQ(input_type, Primitive::kPrimLong);
8117 locations->SetInAt(0, Location::RegisterPairLocation(
8118 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8119 }
8120
8121 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8122 }
8123}
8124
8125void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8126 LocationSummary* locations = conversion->GetLocations();
8127 Primitive::Type result_type = conversion->GetResultType();
8128 Primitive::Type input_type = conversion->GetInputType();
8129 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008130 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008131
8132 DCHECK_NE(input_type, result_type);
8133
8134 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8135 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8136 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8137 Register src = locations->InAt(0).AsRegister<Register>();
8138
Alexey Frunzea871ef12016-06-27 15:20:11 -07008139 if (dst_low != src) {
8140 __ Move(dst_low, src);
8141 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008142 __ Sra(dst_high, src, 31);
8143 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8144 Register dst = locations->Out().AsRegister<Register>();
8145 Register src = (input_type == Primitive::kPrimLong)
8146 ? locations->InAt(0).AsRegisterPairLow<Register>()
8147 : locations->InAt(0).AsRegister<Register>();
8148
8149 switch (result_type) {
8150 case Primitive::kPrimChar:
8151 __ Andi(dst, src, 0xFFFF);
8152 break;
8153 case Primitive::kPrimByte:
8154 if (has_sign_extension) {
8155 __ Seb(dst, src);
8156 } else {
8157 __ Sll(dst, src, 24);
8158 __ Sra(dst, dst, 24);
8159 }
8160 break;
8161 case Primitive::kPrimShort:
8162 if (has_sign_extension) {
8163 __ Seh(dst, src);
8164 } else {
8165 __ Sll(dst, src, 16);
8166 __ Sra(dst, dst, 16);
8167 }
8168 break;
8169 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008170 if (dst != src) {
8171 __ Move(dst, src);
8172 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008173 break;
8174
8175 default:
8176 LOG(FATAL) << "Unexpected type conversion from " << input_type
8177 << " to " << result_type;
8178 }
8179 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008180 if (input_type == Primitive::kPrimLong) {
8181 if (isR6) {
8182 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8183 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8184 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8185 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8186 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8187 __ Mtc1(src_low, FTMP);
8188 __ Mthc1(src_high, FTMP);
8189 if (result_type == Primitive::kPrimFloat) {
8190 __ Cvtsl(dst, FTMP);
8191 } else {
8192 __ Cvtdl(dst, FTMP);
8193 }
8194 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008195 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8196 : kQuickL2d;
8197 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008198 if (result_type == Primitive::kPrimFloat) {
8199 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8200 } else {
8201 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8202 }
8203 }
8204 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008205 Register src = locations->InAt(0).AsRegister<Register>();
8206 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8207 __ Mtc1(src, FTMP);
8208 if (result_type == Primitive::kPrimFloat) {
8209 __ Cvtsw(dst, FTMP);
8210 } else {
8211 __ Cvtdw(dst, FTMP);
8212 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008213 }
8214 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8215 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008216 if (result_type == Primitive::kPrimLong) {
8217 if (isR6) {
8218 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8219 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8220 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8221 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8222 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8223 MipsLabel truncate;
8224 MipsLabel done;
8225
8226 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8227 // value when the input is either a NaN or is outside of the range of the output type
8228 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8229 // the same result.
8230 //
8231 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8232 // value of the output type if the input is outside of the range after the truncation or
8233 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8234 // results. This matches the desired float/double-to-int/long conversion exactly.
8235 //
8236 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
8237 //
8238 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8239 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8240 // even though it must be NAN2008=1 on R6.
8241 //
8242 // The code takes care of the different behaviors by first comparing the input to the
8243 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8244 // If the input is greater than or equal to the minimum, it procedes to the truncate
8245 // instruction, which will handle such an input the same way irrespective of NAN2008.
8246 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8247 // in order to return either zero or the minimum value.
8248 //
8249 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8250 // truncate instruction for MIPS64R6.
8251 if (input_type == Primitive::kPrimFloat) {
8252 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
8253 __ LoadConst32(TMP, min_val);
8254 __ Mtc1(TMP, FTMP);
8255 __ CmpLeS(FTMP, FTMP, src);
8256 } else {
8257 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
8258 __ LoadConst32(TMP, High32Bits(min_val));
8259 __ Mtc1(ZERO, FTMP);
8260 __ Mthc1(TMP, FTMP);
8261 __ CmpLeD(FTMP, FTMP, src);
8262 }
8263
8264 __ Bc1nez(FTMP, &truncate);
8265
8266 if (input_type == Primitive::kPrimFloat) {
8267 __ CmpEqS(FTMP, src, src);
8268 } else {
8269 __ CmpEqD(FTMP, src, src);
8270 }
8271 __ Move(dst_low, ZERO);
8272 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
8273 __ Mfc1(TMP, FTMP);
8274 __ And(dst_high, dst_high, TMP);
8275
8276 __ B(&done);
8277
8278 __ Bind(&truncate);
8279
8280 if (input_type == Primitive::kPrimFloat) {
8281 __ TruncLS(FTMP, src);
8282 } else {
8283 __ TruncLD(FTMP, src);
8284 }
8285 __ Mfc1(dst_low, FTMP);
8286 __ Mfhc1(dst_high, FTMP);
8287
8288 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008289 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008290 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8291 : kQuickD2l;
8292 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008293 if (input_type == Primitive::kPrimFloat) {
8294 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8295 } else {
8296 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8297 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008298 }
8299 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008300 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8301 Register dst = locations->Out().AsRegister<Register>();
8302 MipsLabel truncate;
8303 MipsLabel done;
8304
8305 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8306 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8307 // even though it must be NAN2008=1 on R6.
8308 //
8309 // For details see the large comment above for the truncation of float/double to long on R6.
8310 //
8311 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8312 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008313 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008314 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8315 __ LoadConst32(TMP, min_val);
8316 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008317 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008318 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8319 __ LoadConst32(TMP, High32Bits(min_val));
8320 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07008321 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008322 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008323
8324 if (isR6) {
8325 if (input_type == Primitive::kPrimFloat) {
8326 __ CmpLeS(FTMP, FTMP, src);
8327 } else {
8328 __ CmpLeD(FTMP, FTMP, src);
8329 }
8330 __ Bc1nez(FTMP, &truncate);
8331
8332 if (input_type == Primitive::kPrimFloat) {
8333 __ CmpEqS(FTMP, src, src);
8334 } else {
8335 __ CmpEqD(FTMP, src, src);
8336 }
8337 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8338 __ Mfc1(TMP, FTMP);
8339 __ And(dst, dst, TMP);
8340 } else {
8341 if (input_type == Primitive::kPrimFloat) {
8342 __ ColeS(0, FTMP, src);
8343 } else {
8344 __ ColeD(0, FTMP, src);
8345 }
8346 __ Bc1t(0, &truncate);
8347
8348 if (input_type == Primitive::kPrimFloat) {
8349 __ CeqS(0, src, src);
8350 } else {
8351 __ CeqD(0, src, src);
8352 }
8353 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8354 __ Movf(dst, ZERO, 0);
8355 }
8356
8357 __ B(&done);
8358
8359 __ Bind(&truncate);
8360
8361 if (input_type == Primitive::kPrimFloat) {
8362 __ TruncWS(FTMP, src);
8363 } else {
8364 __ TruncWD(FTMP, src);
8365 }
8366 __ Mfc1(dst, FTMP);
8367
8368 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008369 }
8370 } else if (Primitive::IsFloatingPointType(result_type) &&
8371 Primitive::IsFloatingPointType(input_type)) {
8372 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8373 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8374 if (result_type == Primitive::kPrimFloat) {
8375 __ Cvtsd(dst, src);
8376 } else {
8377 __ Cvtds(dst, src);
8378 }
8379 } else {
8380 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8381 << " to " << result_type;
8382 }
8383}
8384
8385void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8386 HandleShift(ushr);
8387}
8388
8389void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8390 HandleShift(ushr);
8391}
8392
8393void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8394 HandleBinaryOp(instruction);
8395}
8396
8397void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8398 HandleBinaryOp(instruction);
8399}
8400
8401void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8402 // Nothing to do, this should be removed during prepare for register allocator.
8403 LOG(FATAL) << "Unreachable";
8404}
8405
8406void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8407 // Nothing to do, this should be removed during prepare for register allocator.
8408 LOG(FATAL) << "Unreachable";
8409}
8410
8411void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008412 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008413}
8414
8415void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008416 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008417}
8418
8419void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008420 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008421}
8422
8423void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008424 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008425}
8426
8427void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008428 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008429}
8430
8431void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008432 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008433}
8434
8435void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008436 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008437}
8438
8439void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008440 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008441}
8442
8443void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008444 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008445}
8446
8447void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008448 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008449}
8450
8451void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008452 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008453}
8454
8455void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008456 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008457}
8458
8459void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008460 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008461}
8462
8463void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008464 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008465}
8466
8467void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008468 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008469}
8470
8471void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008472 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008473}
8474
8475void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008476 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008477}
8478
8479void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008480 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008481}
8482
8483void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008484 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485}
8486
8487void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008488 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008489}
8490
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008491void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8492 LocationSummary* locations =
8493 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8494 locations->SetInAt(0, Location::RequiresRegister());
8495}
8496
Alexey Frunze96b66822016-09-10 02:32:44 -07008497void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8498 int32_t lower_bound,
8499 uint32_t num_entries,
8500 HBasicBlock* switch_block,
8501 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008502 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008503 Register temp_reg = TMP;
8504 __ Addiu32(temp_reg, value_reg, -lower_bound);
8505 // Jump to default if index is negative
8506 // Note: We don't check the case that index is positive while value < lower_bound, because in
8507 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8508 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8509
Alexey Frunze96b66822016-09-10 02:32:44 -07008510 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008511 // Jump to successors[0] if value == lower_bound.
8512 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8513 int32_t last_index = 0;
8514 for (; num_entries - last_index > 2; last_index += 2) {
8515 __ Addiu(temp_reg, temp_reg, -2);
8516 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8517 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8518 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8519 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8520 }
8521 if (num_entries - last_index == 2) {
8522 // The last missing case_value.
8523 __ Addiu(temp_reg, temp_reg, -1);
8524 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008525 }
8526
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008527 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008528 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008529 __ B(codegen_->GetLabelOf(default_block));
8530 }
8531}
8532
Alexey Frunze96b66822016-09-10 02:32:44 -07008533void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8534 Register constant_area,
8535 int32_t lower_bound,
8536 uint32_t num_entries,
8537 HBasicBlock* switch_block,
8538 HBasicBlock* default_block) {
8539 // Create a jump table.
8540 std::vector<MipsLabel*> labels(num_entries);
8541 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8542 for (uint32_t i = 0; i < num_entries; i++) {
8543 labels[i] = codegen_->GetLabelOf(successors[i]);
8544 }
8545 JumpTable* table = __ CreateJumpTable(std::move(labels));
8546
8547 // Is the value in range?
8548 __ Addiu32(TMP, value_reg, -lower_bound);
8549 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8550 __ Sltiu(AT, TMP, num_entries);
8551 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8552 } else {
8553 __ LoadConst32(AT, num_entries);
8554 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8555 }
8556
8557 // We are in the range of the table.
8558 // Load the target address from the jump table, indexing by the value.
8559 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008560 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008561 __ Lw(TMP, TMP, 0);
8562 // Compute the absolute target address by adding the table start address
8563 // (the table contains offsets to targets relative to its start).
8564 __ Addu(TMP, TMP, AT);
8565 // And jump.
8566 __ Jr(TMP);
8567 __ NopIfNoReordering();
8568}
8569
8570void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8571 int32_t lower_bound = switch_instr->GetStartValue();
8572 uint32_t num_entries = switch_instr->GetNumEntries();
8573 LocationSummary* locations = switch_instr->GetLocations();
8574 Register value_reg = locations->InAt(0).AsRegister<Register>();
8575 HBasicBlock* switch_block = switch_instr->GetBlock();
8576 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8577
8578 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8579 num_entries > kPackedSwitchJumpTableThreshold) {
8580 // R6 uses PC-relative addressing to access the jump table.
8581 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8582 // the jump table and it is implemented by changing HPackedSwitch to
8583 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8584 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8585 GenTableBasedPackedSwitch(value_reg,
8586 ZERO,
8587 lower_bound,
8588 num_entries,
8589 switch_block,
8590 default_block);
8591 } else {
8592 GenPackedSwitchWithCompares(value_reg,
8593 lower_bound,
8594 num_entries,
8595 switch_block,
8596 default_block);
8597 }
8598}
8599
8600void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8601 LocationSummary* locations =
8602 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8603 locations->SetInAt(0, Location::RequiresRegister());
8604 // Constant area pointer (HMipsComputeBaseMethodAddress).
8605 locations->SetInAt(1, Location::RequiresRegister());
8606}
8607
8608void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8609 int32_t lower_bound = switch_instr->GetStartValue();
8610 uint32_t num_entries = switch_instr->GetNumEntries();
8611 LocationSummary* locations = switch_instr->GetLocations();
8612 Register value_reg = locations->InAt(0).AsRegister<Register>();
8613 Register constant_area = locations->InAt(1).AsRegister<Register>();
8614 HBasicBlock* switch_block = switch_instr->GetBlock();
8615 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8616
8617 // This is an R2-only path. HPackedSwitch has been changed to
8618 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8619 // required to address the jump table relative to PC.
8620 GenTableBasedPackedSwitch(value_reg,
8621 constant_area,
8622 lower_bound,
8623 num_entries,
8624 switch_block,
8625 default_block);
8626}
8627
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008628void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8629 HMipsComputeBaseMethodAddress* insn) {
8630 LocationSummary* locations =
8631 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8632 locations->SetOut(Location::RequiresRegister());
8633}
8634
8635void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8636 HMipsComputeBaseMethodAddress* insn) {
8637 LocationSummary* locations = insn->GetLocations();
8638 Register reg = locations->Out().AsRegister<Register>();
8639
8640 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8641
8642 // Generate a dummy PC-relative call to obtain PC.
8643 __ Nal();
8644 // Grab the return address off RA.
8645 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008646 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008647
8648 // Remember this offset (the obtained PC value) for later use with constant area.
8649 __ BindPcRelBaseLabel();
8650}
8651
8652void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8654 locations->SetOut(Location::RequiresRegister());
8655}
8656
8657void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8658 Register reg = base->GetLocations()->Out().AsRegister<Register>();
8659 CodeGeneratorMIPS::PcRelativePatchInfo* info =
8660 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008661 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8662 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008663 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008664 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
8665 __ Addiu(reg, reg, /* placeholder */ 0x5678);
8666 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008667}
8668
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008669void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8670 // The trampoline uses the same calling convention as dex calling conventions,
8671 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8672 // the method_idx.
8673 HandleInvoke(invoke);
8674}
8675
8676void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8677 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8678}
8679
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008680void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8681 LocationSummary* locations =
8682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8683 locations->SetInAt(0, Location::RequiresRegister());
8684 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008685}
8686
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008687void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8688 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008689 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008690 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008691 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008692 __ LoadFromOffset(kLoadWord,
8693 locations->Out().AsRegister<Register>(),
8694 locations->InAt(0).AsRegister<Register>(),
8695 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008696 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008697 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008698 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008699 __ LoadFromOffset(kLoadWord,
8700 locations->Out().AsRegister<Register>(),
8701 locations->InAt(0).AsRegister<Register>(),
8702 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008703 __ LoadFromOffset(kLoadWord,
8704 locations->Out().AsRegister<Register>(),
8705 locations->Out().AsRegister<Register>(),
8706 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008707 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008708}
8709
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710#undef __
8711#undef QUICK_ENTRY_POINT
8712
8713} // namespace mips
8714} // namespace art