blob: a95eb5246046322a052f4dd741bf287ef21dd754 [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());
Serban Constantinescufca16662016-07-14 09:21:59 +0100496 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000497 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498 }
499
500 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
501
502 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200503 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
504};
505
Alexey Frunze15958152017-02-09 19:08:30 -0800506class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
507 public:
508 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
509
510 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
511 LocationSummary* locations = instruction_->GetLocations();
512 __ Bind(GetEntryLabel());
513 SaveLiveRegisters(codegen, locations);
514
515 InvokeRuntimeCallingConvention calling_convention;
516 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
517 parallel_move.AddMove(
518 locations->InAt(0),
519 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
520 Primitive::kPrimNot,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(1),
524 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
525 Primitive::kPrimInt,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(2),
529 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
530 Primitive::kPrimNot,
531 nullptr);
532 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
533
534 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
535 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
536 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
537 RestoreLiveRegisters(codegen, locations);
538 __ B(GetExitLabel());
539 }
540
541 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
542
543 private:
544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
545};
546
547// Slow path marking an object reference `ref` during a read
548// barrier. The field `obj.field` in the object `obj` holding this
549// reference does not get updated by this slow path after marking (see
550// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
551//
552// This means that after the execution of this slow path, `ref` will
553// always be up-to-date, but `obj.field` may not; i.e., after the
554// flip, `ref` will be a to-space reference, but `obj.field` will
555// probably still be a from-space reference (unless it gets updated by
556// another thread, or if another thread installed another object
557// reference (different from `ref`) in `obj.field`).
558//
559// If `entrypoint` is a valid location it is assumed to already be
560// holding the entrypoint. The case where the entrypoint is passed in
561// is for the GcRoot read barrier.
562class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
563 public:
564 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
565 Location ref,
566 Location entrypoint = Location::NoLocation())
567 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
568 DCHECK(kEmitCompilerReadBarrier);
569 }
570
571 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
572
573 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
574 LocationSummary* locations = instruction_->GetLocations();
575 Register ref_reg = ref_.AsRegister<Register>();
576 DCHECK(locations->CanCall());
577 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
578 DCHECK(instruction_->IsInstanceFieldGet() ||
579 instruction_->IsStaticFieldGet() ||
580 instruction_->IsArrayGet() ||
581 instruction_->IsArraySet() ||
582 instruction_->IsLoadClass() ||
583 instruction_->IsLoadString() ||
584 instruction_->IsInstanceOf() ||
585 instruction_->IsCheckCast() ||
586 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
587 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
588 << "Unexpected instruction in read barrier marking slow path: "
589 << instruction_->DebugName();
590
591 __ Bind(GetEntryLabel());
592 // No need to save live registers; it's taken care of by the
593 // entrypoint. Also, there is no need to update the stack mask,
594 // as this runtime call will not trigger a garbage collection.
595 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
596 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
597 (S2 <= ref_reg && ref_reg <= S7) ||
598 (ref_reg == FP)) << ref_reg;
599 // "Compact" slow path, saving two moves.
600 //
601 // Instead of using the standard runtime calling convention (input
602 // and output in A0 and V0 respectively):
603 //
604 // A0 <- ref
605 // V0 <- ReadBarrierMark(A0)
606 // ref <- V0
607 //
608 // we just use rX (the register containing `ref`) as input and output
609 // of a dedicated entrypoint:
610 //
611 // rX <- ReadBarrierMarkRegX(rX)
612 //
613 if (entrypoint_.IsValid()) {
614 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
615 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
616 __ Jalr(entrypoint_.AsRegister<Register>());
617 __ NopIfNoReordering();
618 } else {
619 int32_t entry_point_offset =
620 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
621 // This runtime call does not require a stack map.
622 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
623 instruction_,
624 this,
625 /* direct */ false);
626 }
627 __ B(GetExitLabel());
628 }
629
630 private:
631 // The location (register) of the marked object reference.
632 const Location ref_;
633
634 // The location of the entrypoint if already loaded.
635 const Location entrypoint_;
636
637 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
638};
639
640// Slow path marking an object reference `ref` during a read barrier,
641// and if needed, atomically updating the field `obj.field` in the
642// object `obj` holding this reference after marking (contrary to
643// ReadBarrierMarkSlowPathMIPS above, which never tries to update
644// `obj.field`).
645//
646// This means that after the execution of this slow path, both `ref`
647// and `obj.field` will be up-to-date; i.e., after the flip, both will
648// hold the same to-space reference (unless another thread installed
649// another object reference (different from `ref`) in `obj.field`).
650class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
651 public:
652 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
653 Location ref,
654 Register obj,
655 Location field_offset,
656 Register temp1)
657 : SlowPathCodeMIPS(instruction),
658 ref_(ref),
659 obj_(obj),
660 field_offset_(field_offset),
661 temp1_(temp1) {
662 DCHECK(kEmitCompilerReadBarrier);
663 }
664
665 const char* GetDescription() const OVERRIDE {
666 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
667 }
668
669 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
670 LocationSummary* locations = instruction_->GetLocations();
671 Register ref_reg = ref_.AsRegister<Register>();
672 DCHECK(locations->CanCall());
673 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
674 // This slow path is only used by the UnsafeCASObject intrinsic.
675 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
676 << "Unexpected instruction in read barrier marking and field updating slow path: "
677 << instruction_->DebugName();
678 DCHECK(instruction_->GetLocations()->Intrinsified());
679 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
680 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
681
682 __ Bind(GetEntryLabel());
683
684 // Save the old reference.
685 // Note that we cannot use AT or TMP to save the old reference, as those
686 // are used by the code that follows, but we need the old reference after
687 // the call to the ReadBarrierMarkRegX entry point.
688 DCHECK_NE(temp1_, AT);
689 DCHECK_NE(temp1_, TMP);
690 __ Move(temp1_, ref_reg);
691
692 // No need to save live registers; it's taken care of by the
693 // entrypoint. Also, there is no need to update the stack mask,
694 // as this runtime call will not trigger a garbage collection.
695 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
696 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
697 (S2 <= ref_reg && ref_reg <= S7) ||
698 (ref_reg == FP)) << ref_reg;
699 // "Compact" slow path, saving two moves.
700 //
701 // Instead of using the standard runtime calling convention (input
702 // and output in A0 and V0 respectively):
703 //
704 // A0 <- ref
705 // V0 <- ReadBarrierMark(A0)
706 // ref <- V0
707 //
708 // we just use rX (the register containing `ref`) as input and output
709 // of a dedicated entrypoint:
710 //
711 // rX <- ReadBarrierMarkRegX(rX)
712 //
713 int32_t entry_point_offset =
714 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
715 // This runtime call does not require a stack map.
716 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
717 instruction_,
718 this,
719 /* direct */ false);
720
721 // If the new reference is different from the old reference,
722 // update the field in the holder (`*(obj_ + field_offset_)`).
723 //
724 // Note that this field could also hold a different object, if
725 // another thread had concurrently changed it. In that case, the
726 // the compare-and-set (CAS) loop below would abort, leaving the
727 // field as-is.
728 MipsLabel done;
729 __ Beq(temp1_, ref_reg, &done);
730
731 // Update the the holder's field atomically. This may fail if
732 // mutator updates before us, but it's OK. This is achieved
733 // using a strong compare-and-set (CAS) operation with relaxed
734 // memory synchronization ordering, where the expected value is
735 // the old reference and the desired value is the new reference.
736
737 // Convenience aliases.
738 Register base = obj_;
739 // The UnsafeCASObject intrinsic uses a register pair as field
740 // offset ("long offset"), of which only the low part contains
741 // data.
742 Register offset = field_offset_.AsRegisterPairLow<Register>();
743 Register expected = temp1_;
744 Register value = ref_reg;
745 Register tmp_ptr = TMP; // Pointer to actual memory.
746 Register tmp = AT; // Value in memory.
747
748 __ Addu(tmp_ptr, base, offset);
749
750 if (kPoisonHeapReferences) {
751 __ PoisonHeapReference(expected);
752 // Do not poison `value` if it is the same register as
753 // `expected`, which has just been poisoned.
754 if (value != expected) {
755 __ PoisonHeapReference(value);
756 }
757 }
758
759 // do {
760 // tmp = [r_ptr] - expected;
761 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
762
763 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
764 MipsLabel loop_head, exit_loop;
765 __ Bind(&loop_head);
766 if (is_r6) {
767 __ LlR6(tmp, tmp_ptr);
768 } else {
769 __ LlR2(tmp, tmp_ptr);
770 }
771 __ Bne(tmp, expected, &exit_loop);
772 __ Move(tmp, value);
773 if (is_r6) {
774 __ ScR6(tmp, tmp_ptr);
775 } else {
776 __ ScR2(tmp, tmp_ptr);
777 }
778 __ Beqz(tmp, &loop_head);
779 __ Bind(&exit_loop);
780
781 if (kPoisonHeapReferences) {
782 __ UnpoisonHeapReference(expected);
783 // Do not unpoison `value` if it is the same register as
784 // `expected`, which has just been unpoisoned.
785 if (value != expected) {
786 __ UnpoisonHeapReference(value);
787 }
788 }
789
790 __ Bind(&done);
791 __ B(GetExitLabel());
792 }
793
794 private:
795 // The location (register) of the marked object reference.
796 const Location ref_;
797 // The register containing the object holding the marked object reference field.
798 const Register obj_;
799 // The location of the offset of the marked reference field within `obj_`.
800 Location field_offset_;
801
802 const Register temp1_;
803
804 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
805};
806
807// Slow path generating a read barrier for a heap reference.
808class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
809 public:
810 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
811 Location out,
812 Location ref,
813 Location obj,
814 uint32_t offset,
815 Location index)
816 : SlowPathCodeMIPS(instruction),
817 out_(out),
818 ref_(ref),
819 obj_(obj),
820 offset_(offset),
821 index_(index) {
822 DCHECK(kEmitCompilerReadBarrier);
823 // If `obj` is equal to `out` or `ref`, it means the initial object
824 // has been overwritten by (or after) the heap object reference load
825 // to be instrumented, e.g.:
826 //
827 // __ LoadFromOffset(kLoadWord, out, out, offset);
828 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
829 //
830 // In that case, we have lost the information about the original
831 // object, and the emitted read barrier cannot work properly.
832 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
833 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
834 }
835
836 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
837 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
838 LocationSummary* locations = instruction_->GetLocations();
839 Register reg_out = out_.AsRegister<Register>();
840 DCHECK(locations->CanCall());
841 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
842 DCHECK(instruction_->IsInstanceFieldGet() ||
843 instruction_->IsStaticFieldGet() ||
844 instruction_->IsArrayGet() ||
845 instruction_->IsInstanceOf() ||
846 instruction_->IsCheckCast() ||
847 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
848 << "Unexpected instruction in read barrier for heap reference slow path: "
849 << instruction_->DebugName();
850
851 __ Bind(GetEntryLabel());
852 SaveLiveRegisters(codegen, locations);
853
854 // We may have to change the index's value, but as `index_` is a
855 // constant member (like other "inputs" of this slow path),
856 // introduce a copy of it, `index`.
857 Location index = index_;
858 if (index_.IsValid()) {
859 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
860 if (instruction_->IsArrayGet()) {
861 // Compute the actual memory offset and store it in `index`.
862 Register index_reg = index_.AsRegister<Register>();
863 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
864 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
865 // We are about to change the value of `index_reg` (see the
866 // calls to art::mips::MipsAssembler::Sll and
867 // art::mips::MipsAssembler::Addiu32 below), but it has
868 // not been saved by the previous call to
869 // art::SlowPathCode::SaveLiveRegisters, as it is a
870 // callee-save register --
871 // art::SlowPathCode::SaveLiveRegisters does not consider
872 // callee-save registers, as it has been designed with the
873 // assumption that callee-save registers are supposed to be
874 // handled by the called function. So, as a callee-save
875 // register, `index_reg` _would_ eventually be saved onto
876 // the stack, but it would be too late: we would have
877 // changed its value earlier. Therefore, we manually save
878 // it here into another freely available register,
879 // `free_reg`, chosen of course among the caller-save
880 // registers (as a callee-save `free_reg` register would
881 // exhibit the same problem).
882 //
883 // Note we could have requested a temporary register from
884 // the register allocator instead; but we prefer not to, as
885 // this is a slow path, and we know we can find a
886 // caller-save register that is available.
887 Register free_reg = FindAvailableCallerSaveRegister(codegen);
888 __ Move(free_reg, index_reg);
889 index_reg = free_reg;
890 index = Location::RegisterLocation(index_reg);
891 } else {
892 // The initial register stored in `index_` has already been
893 // saved in the call to art::SlowPathCode::SaveLiveRegisters
894 // (as it is not a callee-save register), so we can freely
895 // use it.
896 }
897 // Shifting the index value contained in `index_reg` by the scale
898 // factor (2) cannot overflow in practice, as the runtime is
899 // unable to allocate object arrays with a size larger than
900 // 2^26 - 1 (that is, 2^28 - 4 bytes).
901 __ Sll(index_reg, index_reg, TIMES_4);
902 static_assert(
903 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
904 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
905 __ Addiu32(index_reg, index_reg, offset_);
906 } else {
907 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
908 // intrinsics, `index_` is not shifted by a scale factor of 2
909 // (as in the case of ArrayGet), as it is actually an offset
910 // to an object field within an object.
911 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
912 DCHECK(instruction_->GetLocations()->Intrinsified());
913 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
914 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
915 << instruction_->AsInvoke()->GetIntrinsic();
916 DCHECK_EQ(offset_, 0U);
917 DCHECK(index_.IsRegisterPair());
918 // UnsafeGet's offset location is a register pair, the low
919 // part contains the correct offset.
920 index = index_.ToLow();
921 }
922 }
923
924 // We're moving two or three locations to locations that could
925 // overlap, so we need a parallel move resolver.
926 InvokeRuntimeCallingConvention calling_convention;
927 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
928 parallel_move.AddMove(ref_,
929 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
930 Primitive::kPrimNot,
931 nullptr);
932 parallel_move.AddMove(obj_,
933 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
934 Primitive::kPrimNot,
935 nullptr);
936 if (index.IsValid()) {
937 parallel_move.AddMove(index,
938 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
939 Primitive::kPrimInt,
940 nullptr);
941 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
942 } else {
943 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
944 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
945 }
946 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
947 instruction_,
948 instruction_->GetDexPc(),
949 this);
950 CheckEntrypointTypes<
951 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
952 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
953
954 RestoreLiveRegisters(codegen, locations);
955 __ B(GetExitLabel());
956 }
957
958 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
959
960 private:
961 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
962 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
963 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
964 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
965 if (i != ref &&
966 i != obj &&
967 !codegen->IsCoreCalleeSaveRegister(i) &&
968 !codegen->IsBlockedCoreRegister(i)) {
969 return static_cast<Register>(i);
970 }
971 }
972 // We shall never fail to find a free caller-save register, as
973 // there are more than two core caller-save registers on MIPS
974 // (meaning it is possible to find one which is different from
975 // `ref` and `obj`).
976 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
977 LOG(FATAL) << "Could not find a free caller-save register";
978 UNREACHABLE();
979 }
980
981 const Location out_;
982 const Location ref_;
983 const Location obj_;
984 const uint32_t offset_;
985 // An additional location containing an index to an array.
986 // Only used for HArrayGet and the UnsafeGetObject &
987 // UnsafeGetObjectVolatile intrinsics.
988 const Location index_;
989
990 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
991};
992
993// Slow path generating a read barrier for a GC root.
994class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
995 public:
996 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
997 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
998 DCHECK(kEmitCompilerReadBarrier);
999 }
1000
1001 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1002 LocationSummary* locations = instruction_->GetLocations();
1003 Register reg_out = out_.AsRegister<Register>();
1004 DCHECK(locations->CanCall());
1005 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1006 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1007 << "Unexpected instruction in read barrier for GC root slow path: "
1008 << instruction_->DebugName();
1009
1010 __ Bind(GetEntryLabel());
1011 SaveLiveRegisters(codegen, locations);
1012
1013 InvokeRuntimeCallingConvention calling_convention;
1014 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
1015 mips_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
1016 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1017 instruction_,
1018 instruction_->GetDexPc(),
1019 this);
1020 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1021 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
1022
1023 RestoreLiveRegisters(codegen, locations);
1024 __ B(GetExitLabel());
1025 }
1026
1027 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1028
1029 private:
1030 const Location out_;
1031 const Location root_;
1032
1033 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1034};
1035
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001036CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1037 const MipsInstructionSetFeatures& isa_features,
1038 const CompilerOptions& compiler_options,
1039 OptimizingCompilerStats* stats)
1040 : CodeGenerator(graph,
1041 kNumberOfCoreRegisters,
1042 kNumberOfFRegisters,
1043 kNumberOfRegisterPairs,
1044 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1045 arraysize(kCoreCalleeSaves)),
1046 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1047 arraysize(kFpuCalleeSaves)),
1048 compiler_options,
1049 stats),
1050 block_labels_(nullptr),
1051 location_builder_(graph, this),
1052 instruction_visitor_(graph, this),
1053 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001054 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001055 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001056 uint32_literals_(std::less<uint32_t>(),
1057 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001058 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1059 boot_image_string_patches_(StringReferenceValueComparator(),
1060 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1061 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1062 boot_image_type_patches_(TypeReferenceValueComparator(),
1063 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1064 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001065 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001066 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1067 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001068 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001069 // Save RA (containing the return address) to mimic Quick.
1070 AddAllocatedRegister(Location::RegisterLocation(RA));
1071}
1072
1073#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001074// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1075#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001076#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001077
1078void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1079 // Ensure that we fix up branches.
1080 __ FinalizeCode();
1081
1082 // Adjust native pc offsets in stack maps.
1083 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001084 uint32_t old_position =
1085 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001086 uint32_t new_position = __ GetAdjustedPosition(old_position);
1087 DCHECK_GE(new_position, old_position);
1088 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1089 }
1090
1091 // Adjust pc offsets for the disassembly information.
1092 if (disasm_info_ != nullptr) {
1093 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1094 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1095 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1096 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1097 it.second.start = __ GetAdjustedPosition(it.second.start);
1098 it.second.end = __ GetAdjustedPosition(it.second.end);
1099 }
1100 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1101 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1102 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1103 }
1104 }
1105
1106 CodeGenerator::Finalize(allocator);
1107}
1108
1109MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1110 return codegen_->GetAssembler();
1111}
1112
1113void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1114 DCHECK_LT(index, moves_.size());
1115 MoveOperands* move = moves_[index];
1116 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1117}
1118
1119void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1120 DCHECK_LT(index, moves_.size());
1121 MoveOperands* move = moves_[index];
1122 Primitive::Type type = move->GetType();
1123 Location loc1 = move->GetDestination();
1124 Location loc2 = move->GetSource();
1125
1126 DCHECK(!loc1.IsConstant());
1127 DCHECK(!loc2.IsConstant());
1128
1129 if (loc1.Equals(loc2)) {
1130 return;
1131 }
1132
1133 if (loc1.IsRegister() && loc2.IsRegister()) {
1134 // Swap 2 GPRs.
1135 Register r1 = loc1.AsRegister<Register>();
1136 Register r2 = loc2.AsRegister<Register>();
1137 __ Move(TMP, r2);
1138 __ Move(r2, r1);
1139 __ Move(r1, TMP);
1140 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1141 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1142 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1143 if (type == Primitive::kPrimFloat) {
1144 __ MovS(FTMP, f2);
1145 __ MovS(f2, f1);
1146 __ MovS(f1, FTMP);
1147 } else {
1148 DCHECK_EQ(type, Primitive::kPrimDouble);
1149 __ MovD(FTMP, f2);
1150 __ MovD(f2, f1);
1151 __ MovD(f1, FTMP);
1152 }
1153 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1154 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1155 // Swap FPR and GPR.
1156 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1157 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1158 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001159 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001160 __ Move(TMP, r2);
1161 __ Mfc1(r2, f1);
1162 __ Mtc1(TMP, f1);
1163 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1164 // Swap 2 GPR register pairs.
1165 Register r1 = loc1.AsRegisterPairLow<Register>();
1166 Register r2 = loc2.AsRegisterPairLow<Register>();
1167 __ Move(TMP, r2);
1168 __ Move(r2, r1);
1169 __ Move(r1, TMP);
1170 r1 = loc1.AsRegisterPairHigh<Register>();
1171 r2 = loc2.AsRegisterPairHigh<Register>();
1172 __ Move(TMP, r2);
1173 __ Move(r2, r1);
1174 __ Move(r1, TMP);
1175 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1176 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1177 // Swap FPR and GPR register pair.
1178 DCHECK_EQ(type, Primitive::kPrimDouble);
1179 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1180 : loc2.AsFpuRegister<FRegister>();
1181 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1182 : loc2.AsRegisterPairLow<Register>();
1183 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1184 : loc2.AsRegisterPairHigh<Register>();
1185 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1186 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1187 // unpredictable and the following mfch1 will fail.
1188 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001189 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001190 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001191 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001192 __ Move(r2_l, TMP);
1193 __ Move(r2_h, AT);
1194 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1195 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1196 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1197 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001198 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1199 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001200 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1201 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001202 __ Move(TMP, reg);
1203 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1204 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1205 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1206 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1207 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1208 : loc2.AsRegisterPairLow<Register>();
1209 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1210 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001211 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001212 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1213 : loc2.GetHighStackIndex(kMipsWordSize);
1214 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001215 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001216 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001217 __ Move(TMP, reg_h);
1218 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1219 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001220 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1221 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1222 : loc2.AsFpuRegister<FRegister>();
1223 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1224 if (type == Primitive::kPrimFloat) {
1225 __ MovS(FTMP, reg);
1226 __ LoadSFromOffset(reg, SP, offset);
1227 __ StoreSToOffset(FTMP, SP, offset);
1228 } else {
1229 DCHECK_EQ(type, Primitive::kPrimDouble);
1230 __ MovD(FTMP, reg);
1231 __ LoadDFromOffset(reg, SP, offset);
1232 __ StoreDToOffset(FTMP, SP, offset);
1233 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234 } else {
1235 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1236 }
1237}
1238
1239void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1240 __ Pop(static_cast<Register>(reg));
1241}
1242
1243void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1244 __ Push(static_cast<Register>(reg));
1245}
1246
1247void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1248 // Allocate a scratch register other than TMP, if available.
1249 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1250 // automatically unspilled when the scratch scope object is destroyed).
1251 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1252 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1253 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1254 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1255 __ LoadFromOffset(kLoadWord,
1256 Register(ensure_scratch.GetRegister()),
1257 SP,
1258 index1 + stack_offset);
1259 __ LoadFromOffset(kLoadWord,
1260 TMP,
1261 SP,
1262 index2 + stack_offset);
1263 __ StoreToOffset(kStoreWord,
1264 Register(ensure_scratch.GetRegister()),
1265 SP,
1266 index2 + stack_offset);
1267 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1268 }
1269}
1270
Alexey Frunze73296a72016-06-03 22:51:46 -07001271void CodeGeneratorMIPS::ComputeSpillMask() {
1272 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1273 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1274 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1275 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1276 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1277 // within the stack frame.
1278 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1279 core_spill_mask_ |= (1 << ZERO);
1280 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001281}
1282
1283bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001284 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001285 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1286 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1287 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001288 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001289}
1290
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291static dwarf::Reg DWARFReg(Register reg) {
1292 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1293}
1294
1295// TODO: mapping of floating-point registers to DWARF.
1296
1297void CodeGeneratorMIPS::GenerateFrameEntry() {
1298 __ Bind(&frame_entry_label_);
1299
1300 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1301
1302 if (do_overflow_check) {
1303 __ LoadFromOffset(kLoadWord,
1304 ZERO,
1305 SP,
1306 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1307 RecordPcInfo(nullptr, 0);
1308 }
1309
1310 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001311 CHECK_EQ(fpu_spill_mask_, 0u);
1312 CHECK_EQ(core_spill_mask_, 1u << RA);
1313 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314 return;
1315 }
1316
1317 // Make sure the frame size isn't unreasonably large.
1318 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1319 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1320 }
1321
1322 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001323
Alexey Frunze73296a72016-06-03 22:51:46 -07001324 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 __ IncreaseFrameSize(ofs);
1326
Alexey Frunze73296a72016-06-03 22:51:46 -07001327 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1328 Register reg = static_cast<Register>(MostSignificantBit(mask));
1329 mask ^= 1u << reg;
1330 ofs -= kMipsWordSize;
1331 // The ZERO register is only included for alignment.
1332 if (reg != ZERO) {
1333 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001334 __ cfi().RelOffset(DWARFReg(reg), ofs);
1335 }
1336 }
1337
Alexey Frunze73296a72016-06-03 22:51:46 -07001338 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1339 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1340 mask ^= 1u << reg;
1341 ofs -= kMipsDoublewordSize;
1342 __ StoreDToOffset(reg, SP, ofs);
1343 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001344 }
1345
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001346 // Save the current method if we need it. Note that we do not
1347 // do this in HCurrentMethod, as the instruction might have been removed
1348 // in the SSA graph.
1349 if (RequiresCurrentMethod()) {
1350 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1351 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001352
1353 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1354 // Initialize should deoptimize flag to 0.
1355 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001357}
1358
1359void CodeGeneratorMIPS::GenerateFrameExit() {
1360 __ cfi().RememberState();
1361
1362 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001363 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001364
Alexey Frunze73296a72016-06-03 22:51:46 -07001365 // For better instruction scheduling restore RA before other registers.
1366 uint32_t ofs = GetFrameSize();
1367 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1368 Register reg = static_cast<Register>(MostSignificantBit(mask));
1369 mask ^= 1u << reg;
1370 ofs -= kMipsWordSize;
1371 // The ZERO register is only included for alignment.
1372 if (reg != ZERO) {
1373 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001374 __ cfi().Restore(DWARFReg(reg));
1375 }
1376 }
1377
Alexey Frunze73296a72016-06-03 22:51:46 -07001378 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1379 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1380 mask ^= 1u << reg;
1381 ofs -= kMipsDoublewordSize;
1382 __ LoadDFromOffset(reg, SP, ofs);
1383 // TODO: __ cfi().Restore(DWARFReg(reg));
1384 }
1385
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001386 size_t frame_size = GetFrameSize();
1387 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1388 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1389 bool reordering = __ SetReorder(false);
1390 if (exchange) {
1391 __ Jr(RA);
1392 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1393 } else {
1394 __ DecreaseFrameSize(frame_size);
1395 __ Jr(RA);
1396 __ Nop(); // In delay slot.
1397 }
1398 __ SetReorder(reordering);
1399 } else {
1400 __ Jr(RA);
1401 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001402 }
1403
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001404 __ cfi().RestoreState();
1405 __ cfi().DefCFAOffset(GetFrameSize());
1406}
1407
1408void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1409 __ Bind(GetLabelOf(block));
1410}
1411
1412void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1413 if (src.Equals(dst)) {
1414 return;
1415 }
1416
1417 if (src.IsConstant()) {
1418 MoveConstant(dst, src.GetConstant());
1419 } else {
1420 if (Primitive::Is64BitType(dst_type)) {
1421 Move64(dst, src);
1422 } else {
1423 Move32(dst, src);
1424 }
1425 }
1426}
1427
1428void CodeGeneratorMIPS::Move32(Location destination, Location source) {
1429 if (source.Equals(destination)) {
1430 return;
1431 }
1432
1433 if (destination.IsRegister()) {
1434 if (source.IsRegister()) {
1435 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1436 } else if (source.IsFpuRegister()) {
1437 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1438 } else {
1439 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1440 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
1441 }
1442 } else if (destination.IsFpuRegister()) {
1443 if (source.IsRegister()) {
1444 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1445 } else if (source.IsFpuRegister()) {
1446 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1447 } else {
1448 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1449 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1450 }
1451 } else {
1452 DCHECK(destination.IsStackSlot()) << destination;
1453 if (source.IsRegister()) {
1454 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
1455 } else if (source.IsFpuRegister()) {
1456 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1457 } else {
1458 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1459 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1460 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1461 }
1462 }
1463}
1464
1465void CodeGeneratorMIPS::Move64(Location destination, Location source) {
1466 if (source.Equals(destination)) {
1467 return;
1468 }
1469
1470 if (destination.IsRegisterPair()) {
1471 if (source.IsRegisterPair()) {
1472 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1473 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1474 } else if (source.IsFpuRegister()) {
1475 Register dst_high = destination.AsRegisterPairHigh<Register>();
1476 Register dst_low = destination.AsRegisterPairLow<Register>();
1477 FRegister src = source.AsFpuRegister<FRegister>();
1478 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001479 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001480 } else {
1481 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1482 int32_t off = source.GetStackIndex();
1483 Register r = destination.AsRegisterPairLow<Register>();
1484 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1485 }
1486 } else if (destination.IsFpuRegister()) {
1487 if (source.IsRegisterPair()) {
1488 FRegister dst = destination.AsFpuRegister<FRegister>();
1489 Register src_high = source.AsRegisterPairHigh<Register>();
1490 Register src_low = source.AsRegisterPairLow<Register>();
1491 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001492 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001493 } else if (source.IsFpuRegister()) {
1494 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1495 } else {
1496 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1497 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1498 }
1499 } else {
1500 DCHECK(destination.IsDoubleStackSlot()) << destination;
1501 int32_t off = destination.GetStackIndex();
1502 if (source.IsRegisterPair()) {
1503 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
1504 } else if (source.IsFpuRegister()) {
1505 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
1506 } else {
1507 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1508 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1509 __ StoreToOffset(kStoreWord, TMP, SP, off);
1510 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1511 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
1512 }
1513 }
1514}
1515
1516void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1517 if (c->IsIntConstant() || c->IsNullConstant()) {
1518 // Move 32 bit constant.
1519 int32_t value = GetInt32ValueOf(c);
1520 if (destination.IsRegister()) {
1521 Register dst = destination.AsRegister<Register>();
1522 __ LoadConst32(dst, value);
1523 } else {
1524 DCHECK(destination.IsStackSlot())
1525 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001526 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001527 }
1528 } else if (c->IsLongConstant()) {
1529 // Move 64 bit constant.
1530 int64_t value = GetInt64ValueOf(c);
1531 if (destination.IsRegisterPair()) {
1532 Register r_h = destination.AsRegisterPairHigh<Register>();
1533 Register r_l = destination.AsRegisterPairLow<Register>();
1534 __ LoadConst64(r_h, r_l, value);
1535 } else {
1536 DCHECK(destination.IsDoubleStackSlot())
1537 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001538 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001539 }
1540 } else if (c->IsFloatConstant()) {
1541 // Move 32 bit float constant.
1542 int32_t value = GetInt32ValueOf(c);
1543 if (destination.IsFpuRegister()) {
1544 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1545 } else {
1546 DCHECK(destination.IsStackSlot())
1547 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001548 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001549 }
1550 } else {
1551 // Move 64 bit double constant.
1552 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1553 int64_t value = GetInt64ValueOf(c);
1554 if (destination.IsFpuRegister()) {
1555 FRegister fd = destination.AsFpuRegister<FRegister>();
1556 __ LoadDConst64(fd, value, TMP);
1557 } else {
1558 DCHECK(destination.IsDoubleStackSlot())
1559 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001560 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001561 }
1562 }
1563}
1564
1565void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1566 DCHECK(destination.IsRegister());
1567 Register dst = destination.AsRegister<Register>();
1568 __ LoadConst32(dst, value);
1569}
1570
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001571void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1572 if (location.IsRegister()) {
1573 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001574 } else if (location.IsRegisterPair()) {
1575 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1576 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001577 } else {
1578 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1579 }
1580}
1581
Vladimir Markoaad75c62016-10-03 08:46:48 +00001582template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1583inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1584 const ArenaDeque<PcRelativePatchInfo>& infos,
1585 ArenaVector<LinkerPatch>* linker_patches) {
1586 for (const PcRelativePatchInfo& info : infos) {
1587 const DexFile& dex_file = info.target_dex_file;
1588 size_t offset_or_index = info.offset_or_index;
1589 DCHECK(info.high_label.IsBound());
1590 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1591 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1592 // the assembler's base label used for PC-relative addressing.
1593 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1594 ? __ GetLabelLocation(&info.pc_rel_label)
1595 : __ GetPcRelBaseLabelLocation();
1596 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1597 }
1598}
1599
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001600void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1601 DCHECK(linker_patches->empty());
1602 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001603 pc_relative_dex_cache_patches_.size() +
1604 pc_relative_string_patches_.size() +
1605 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001606 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001607 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001608 boot_image_type_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001609 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001610 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1611 linker_patches);
1612 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001613 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001614 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1615 linker_patches);
1616 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001617 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1618 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001619 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1620 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001621 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001622 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1623 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001624 for (const auto& entry : boot_image_string_patches_) {
1625 const StringReference& target_string = entry.first;
1626 Literal* literal = entry.second;
1627 DCHECK(literal->GetLabel()->IsBound());
1628 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1629 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1630 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001631 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001632 }
1633 for (const auto& entry : boot_image_type_patches_) {
1634 const TypeReference& target_type = entry.first;
1635 Literal* literal = entry.second;
1636 DCHECK(literal->GetLabel()->IsBound());
1637 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1638 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1639 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001640 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001641 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001642 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001643}
1644
1645CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001646 const DexFile& dex_file, dex::StringIndex string_index) {
1647 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001648}
1649
1650CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001651 const DexFile& dex_file, dex::TypeIndex type_index) {
1652 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001653}
1654
Vladimir Marko1998cd02017-01-13 13:02:58 +00001655CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1656 const DexFile& dex_file, dex::TypeIndex type_index) {
1657 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1658}
1659
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001660CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1661 const DexFile& dex_file, uint32_t element_offset) {
1662 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1663}
1664
1665CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1666 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1667 patches->emplace_back(dex_file, offset_or_index);
1668 return &patches->back();
1669}
1670
Alexey Frunze06a46c42016-07-19 15:00:40 -07001671Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1672 return map->GetOrCreate(
1673 value,
1674 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1675}
1676
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001677Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1678 MethodToLiteralMap* map) {
1679 return map->GetOrCreate(
1680 target_method,
1681 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1682}
1683
Alexey Frunze06a46c42016-07-19 15:00:40 -07001684Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001685 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001686 return boot_image_string_patches_.GetOrCreate(
1687 StringReference(&dex_file, string_index),
1688 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1689}
1690
1691Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001692 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001693 return boot_image_type_patches_.GetOrCreate(
1694 TypeReference(&dex_file, type_index),
1695 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1696}
1697
1698Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001699 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001700}
1701
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001702void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1703 Register out,
1704 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001705 if (GetInstructionSetFeatures().IsR6()) {
1706 DCHECK_EQ(base, ZERO);
1707 __ Bind(&info->high_label);
1708 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001709 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001710 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001711 } else {
1712 // If base is ZERO, emit NAL to obtain the actual base.
1713 if (base == ZERO) {
1714 // Generate a dummy PC-relative call to obtain PC.
1715 __ Nal();
1716 }
1717 __ Bind(&info->high_label);
1718 __ Lui(out, /* placeholder */ 0x1234);
1719 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1720 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1721 if (base == ZERO) {
1722 __ Bind(&info->pc_rel_label);
1723 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001724 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001725 __ Addu(out, out, (base == ZERO) ? RA : base);
1726 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001727 // The immediately following instruction will add the sign-extended low half of the 32-bit
1728 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001729}
1730
Alexey Frunze627c1a02017-01-30 19:28:14 -08001731CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1732 const DexFile& dex_file,
1733 dex::StringIndex dex_index,
1734 Handle<mirror::String> handle) {
1735 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1736 reinterpret_cast64<uint64_t>(handle.GetReference()));
1737 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1738 return &jit_string_patches_.back();
1739}
1740
1741CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1742 const DexFile& dex_file,
1743 dex::TypeIndex dex_index,
1744 Handle<mirror::Class> handle) {
1745 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1746 reinterpret_cast64<uint64_t>(handle.GetReference()));
1747 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1748 return &jit_class_patches_.back();
1749}
1750
1751void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1752 const uint8_t* roots_data,
1753 const CodeGeneratorMIPS::JitPatchInfo& info,
1754 uint64_t index_in_table) const {
1755 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1756 uintptr_t address =
1757 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1758 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1759 // lui reg, addr32_high
1760 DCHECK_EQ(code[literal_offset + 0], 0x34);
1761 DCHECK_EQ(code[literal_offset + 1], 0x12);
1762 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1763 DCHECK_EQ(code[literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001764 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001765 DCHECK_EQ(code[literal_offset + 4], 0x78);
1766 DCHECK_EQ(code[literal_offset + 5], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001767 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001768 // lui reg, addr32_high
1769 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1770 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001771 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001772 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1773 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1774}
1775
1776void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1777 for (const JitPatchInfo& info : jit_string_patches_) {
1778 const auto& it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1779 dex::StringIndex(info.index)));
1780 DCHECK(it != jit_string_roots_.end());
1781 PatchJitRootUse(code, roots_data, info, it->second);
1782 }
1783 for (const JitPatchInfo& info : jit_class_patches_) {
1784 const auto& it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1785 dex::TypeIndex(info.index)));
1786 DCHECK(it != jit_class_roots_.end());
1787 PatchJitRootUse(code, roots_data, info, it->second);
1788 }
1789}
1790
Goran Jakovljevice114da22016-12-26 14:21:43 +01001791void CodeGeneratorMIPS::MarkGCCard(Register object,
1792 Register value,
1793 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001794 MipsLabel done;
1795 Register card = AT;
1796 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001797 if (value_can_be_null) {
1798 __ Beqz(value, &done);
1799 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001800 __ LoadFromOffset(kLoadWord,
1801 card,
1802 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001803 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001804 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1805 __ Addu(temp, card, temp);
1806 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001807 if (value_can_be_null) {
1808 __ Bind(&done);
1809 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001810}
1811
David Brazdil58282f42016-01-14 12:45:10 +00001812void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001813 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1814 blocked_core_registers_[ZERO] = true;
1815 blocked_core_registers_[K0] = true;
1816 blocked_core_registers_[K1] = true;
1817 blocked_core_registers_[GP] = true;
1818 blocked_core_registers_[SP] = true;
1819 blocked_core_registers_[RA] = true;
1820
1821 // AT and TMP(T8) are used as temporary/scratch registers
1822 // (similar to how AT is used by MIPS assemblers).
1823 blocked_core_registers_[AT] = true;
1824 blocked_core_registers_[TMP] = true;
1825 blocked_fpu_registers_[FTMP] = true;
1826
1827 // Reserve suspend and thread registers.
1828 blocked_core_registers_[S0] = true;
1829 blocked_core_registers_[TR] = true;
1830
1831 // Reserve T9 for function calls
1832 blocked_core_registers_[T9] = true;
1833
1834 // Reserve odd-numbered FPU registers.
1835 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1836 blocked_fpu_registers_[i] = true;
1837 }
1838
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001839 if (GetGraph()->IsDebuggable()) {
1840 // Stubs do not save callee-save floating point registers. If the graph
1841 // is debuggable, we need to deal with these registers differently. For
1842 // now, just block them.
1843 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1844 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1845 }
1846 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001847}
1848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1850 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1851 return kMipsWordSize;
1852}
1853
1854size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1855 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1856 return kMipsWordSize;
1857}
1858
1859size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1860 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1861 return kMipsDoublewordSize;
1862}
1863
1864size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1865 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1866 return kMipsDoublewordSize;
1867}
1868
1869void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001870 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871}
1872
1873void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001874 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001875}
1876
Serban Constantinescufca16662016-07-14 09:21:59 +01001877constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1878
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1880 HInstruction* instruction,
1881 uint32_t dex_pc,
1882 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001883 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001884 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1885 IsDirectEntrypoint(entrypoint));
1886 if (EntrypointRequiresStackMap(entrypoint)) {
1887 RecordPcInfo(instruction, dex_pc, slow_path);
1888 }
1889}
1890
1891void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1892 HInstruction* instruction,
1893 SlowPathCode* slow_path,
1894 bool direct) {
1895 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1896 GenerateInvokeRuntime(entry_point_offset, direct);
1897}
1898
1899void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001900 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001901 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001902 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001903 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001904 // Reserve argument space on stack (for $a0-$a3) for
1905 // entrypoints that directly reference native implementations.
1906 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001907 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001909 } else {
1910 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001911 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001912 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001913}
1914
1915void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1916 Register class_reg) {
1917 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1918 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1919 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1920 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1921 __ Sync(0);
1922 __ Bind(slow_path->GetExitLabel());
1923}
1924
1925void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1926 __ Sync(0); // Only stype 0 is supported.
1927}
1928
1929void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1930 HBasicBlock* successor) {
1931 SuspendCheckSlowPathMIPS* slow_path =
1932 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1933 codegen_->AddSlowPath(slow_path);
1934
1935 __ LoadFromOffset(kLoadUnsignedHalfword,
1936 TMP,
1937 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001938 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001939 if (successor == nullptr) {
1940 __ Bnez(TMP, slow_path->GetEntryLabel());
1941 __ Bind(slow_path->GetReturnLabel());
1942 } else {
1943 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1944 __ B(slow_path->GetEntryLabel());
1945 // slow_path will return to GetLabelOf(successor).
1946 }
1947}
1948
1949InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1950 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001951 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 assembler_(codegen->GetAssembler()),
1953 codegen_(codegen) {}
1954
1955void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1956 DCHECK_EQ(instruction->InputCount(), 2U);
1957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1958 Primitive::Type type = instruction->GetResultType();
1959 switch (type) {
1960 case Primitive::kPrimInt: {
1961 locations->SetInAt(0, Location::RequiresRegister());
1962 HInstruction* right = instruction->InputAt(1);
1963 bool can_use_imm = false;
1964 if (right->IsConstant()) {
1965 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1966 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1967 can_use_imm = IsUint<16>(imm);
1968 } else if (instruction->IsAdd()) {
1969 can_use_imm = IsInt<16>(imm);
1970 } else {
1971 DCHECK(instruction->IsSub());
1972 can_use_imm = IsInt<16>(-imm);
1973 }
1974 }
1975 if (can_use_imm)
1976 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1977 else
1978 locations->SetInAt(1, Location::RequiresRegister());
1979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1980 break;
1981 }
1982
1983 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001985 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987 break;
1988 }
1989
1990 case Primitive::kPrimFloat:
1991 case Primitive::kPrimDouble:
1992 DCHECK(instruction->IsAdd() || instruction->IsSub());
1993 locations->SetInAt(0, Location::RequiresFpuRegister());
1994 locations->SetInAt(1, Location::RequiresFpuRegister());
1995 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1996 break;
1997
1998 default:
1999 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2000 }
2001}
2002
2003void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2004 Primitive::Type type = instruction->GetType();
2005 LocationSummary* locations = instruction->GetLocations();
2006
2007 switch (type) {
2008 case Primitive::kPrimInt: {
2009 Register dst = locations->Out().AsRegister<Register>();
2010 Register lhs = locations->InAt(0).AsRegister<Register>();
2011 Location rhs_location = locations->InAt(1);
2012
2013 Register rhs_reg = ZERO;
2014 int32_t rhs_imm = 0;
2015 bool use_imm = rhs_location.IsConstant();
2016 if (use_imm) {
2017 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2018 } else {
2019 rhs_reg = rhs_location.AsRegister<Register>();
2020 }
2021
2022 if (instruction->IsAnd()) {
2023 if (use_imm)
2024 __ Andi(dst, lhs, rhs_imm);
2025 else
2026 __ And(dst, lhs, rhs_reg);
2027 } else if (instruction->IsOr()) {
2028 if (use_imm)
2029 __ Ori(dst, lhs, rhs_imm);
2030 else
2031 __ Or(dst, lhs, rhs_reg);
2032 } else if (instruction->IsXor()) {
2033 if (use_imm)
2034 __ Xori(dst, lhs, rhs_imm);
2035 else
2036 __ Xor(dst, lhs, rhs_reg);
2037 } else if (instruction->IsAdd()) {
2038 if (use_imm)
2039 __ Addiu(dst, lhs, rhs_imm);
2040 else
2041 __ Addu(dst, lhs, rhs_reg);
2042 } else {
2043 DCHECK(instruction->IsSub());
2044 if (use_imm)
2045 __ Addiu(dst, lhs, -rhs_imm);
2046 else
2047 __ Subu(dst, lhs, rhs_reg);
2048 }
2049 break;
2050 }
2051
2052 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002053 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2054 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2055 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2056 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002057 Location rhs_location = locations->InAt(1);
2058 bool use_imm = rhs_location.IsConstant();
2059 if (!use_imm) {
2060 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2061 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2062 if (instruction->IsAnd()) {
2063 __ And(dst_low, lhs_low, rhs_low);
2064 __ And(dst_high, lhs_high, rhs_high);
2065 } else if (instruction->IsOr()) {
2066 __ Or(dst_low, lhs_low, rhs_low);
2067 __ Or(dst_high, lhs_high, rhs_high);
2068 } else if (instruction->IsXor()) {
2069 __ Xor(dst_low, lhs_low, rhs_low);
2070 __ Xor(dst_high, lhs_high, rhs_high);
2071 } else if (instruction->IsAdd()) {
2072 if (lhs_low == rhs_low) {
2073 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2074 __ Slt(TMP, lhs_low, ZERO);
2075 __ Addu(dst_low, lhs_low, rhs_low);
2076 } else {
2077 __ Addu(dst_low, lhs_low, rhs_low);
2078 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2079 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2080 }
2081 __ Addu(dst_high, lhs_high, rhs_high);
2082 __ Addu(dst_high, dst_high, TMP);
2083 } else {
2084 DCHECK(instruction->IsSub());
2085 __ Sltu(TMP, lhs_low, rhs_low);
2086 __ Subu(dst_low, lhs_low, rhs_low);
2087 __ Subu(dst_high, lhs_high, rhs_high);
2088 __ Subu(dst_high, dst_high, TMP);
2089 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002090 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002091 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2092 if (instruction->IsOr()) {
2093 uint32_t low = Low32Bits(value);
2094 uint32_t high = High32Bits(value);
2095 if (IsUint<16>(low)) {
2096 if (dst_low != lhs_low || low != 0) {
2097 __ Ori(dst_low, lhs_low, low);
2098 }
2099 } else {
2100 __ LoadConst32(TMP, low);
2101 __ Or(dst_low, lhs_low, TMP);
2102 }
2103 if (IsUint<16>(high)) {
2104 if (dst_high != lhs_high || high != 0) {
2105 __ Ori(dst_high, lhs_high, high);
2106 }
2107 } else {
2108 if (high != low) {
2109 __ LoadConst32(TMP, high);
2110 }
2111 __ Or(dst_high, lhs_high, TMP);
2112 }
2113 } else if (instruction->IsXor()) {
2114 uint32_t low = Low32Bits(value);
2115 uint32_t high = High32Bits(value);
2116 if (IsUint<16>(low)) {
2117 if (dst_low != lhs_low || low != 0) {
2118 __ Xori(dst_low, lhs_low, low);
2119 }
2120 } else {
2121 __ LoadConst32(TMP, low);
2122 __ Xor(dst_low, lhs_low, TMP);
2123 }
2124 if (IsUint<16>(high)) {
2125 if (dst_high != lhs_high || high != 0) {
2126 __ Xori(dst_high, lhs_high, high);
2127 }
2128 } else {
2129 if (high != low) {
2130 __ LoadConst32(TMP, high);
2131 }
2132 __ Xor(dst_high, lhs_high, TMP);
2133 }
2134 } else if (instruction->IsAnd()) {
2135 uint32_t low = Low32Bits(value);
2136 uint32_t high = High32Bits(value);
2137 if (IsUint<16>(low)) {
2138 __ Andi(dst_low, lhs_low, low);
2139 } else if (low != 0xFFFFFFFF) {
2140 __ LoadConst32(TMP, low);
2141 __ And(dst_low, lhs_low, TMP);
2142 } else if (dst_low != lhs_low) {
2143 __ Move(dst_low, lhs_low);
2144 }
2145 if (IsUint<16>(high)) {
2146 __ Andi(dst_high, lhs_high, high);
2147 } else if (high != 0xFFFFFFFF) {
2148 if (high != low) {
2149 __ LoadConst32(TMP, high);
2150 }
2151 __ And(dst_high, lhs_high, TMP);
2152 } else if (dst_high != lhs_high) {
2153 __ Move(dst_high, lhs_high);
2154 }
2155 } else {
2156 if (instruction->IsSub()) {
2157 value = -value;
2158 } else {
2159 DCHECK(instruction->IsAdd());
2160 }
2161 int32_t low = Low32Bits(value);
2162 int32_t high = High32Bits(value);
2163 if (IsInt<16>(low)) {
2164 if (dst_low != lhs_low || low != 0) {
2165 __ Addiu(dst_low, lhs_low, low);
2166 }
2167 if (low != 0) {
2168 __ Sltiu(AT, dst_low, low);
2169 }
2170 } else {
2171 __ LoadConst32(TMP, low);
2172 __ Addu(dst_low, lhs_low, TMP);
2173 __ Sltu(AT, dst_low, TMP);
2174 }
2175 if (IsInt<16>(high)) {
2176 if (dst_high != lhs_high || high != 0) {
2177 __ Addiu(dst_high, lhs_high, high);
2178 }
2179 } else {
2180 if (high != low) {
2181 __ LoadConst32(TMP, high);
2182 }
2183 __ Addu(dst_high, lhs_high, TMP);
2184 }
2185 if (low != 0) {
2186 __ Addu(dst_high, dst_high, AT);
2187 }
2188 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002189 }
2190 break;
2191 }
2192
2193 case Primitive::kPrimFloat:
2194 case Primitive::kPrimDouble: {
2195 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2196 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2197 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2198 if (instruction->IsAdd()) {
2199 if (type == Primitive::kPrimFloat) {
2200 __ AddS(dst, lhs, rhs);
2201 } else {
2202 __ AddD(dst, lhs, rhs);
2203 }
2204 } else {
2205 DCHECK(instruction->IsSub());
2206 if (type == Primitive::kPrimFloat) {
2207 __ SubS(dst, lhs, rhs);
2208 } else {
2209 __ SubD(dst, lhs, rhs);
2210 }
2211 }
2212 break;
2213 }
2214
2215 default:
2216 LOG(FATAL) << "Unexpected binary operation type " << type;
2217 }
2218}
2219
2220void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002221 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002222
2223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2224 Primitive::Type type = instr->GetResultType();
2225 switch (type) {
2226 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002227 locations->SetInAt(0, Location::RequiresRegister());
2228 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2230 break;
2231 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232 locations->SetInAt(0, Location::RequiresRegister());
2233 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2234 locations->SetOut(Location::RequiresRegister());
2235 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002236 default:
2237 LOG(FATAL) << "Unexpected shift type " << type;
2238 }
2239}
2240
2241static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2242
2243void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002244 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002245 LocationSummary* locations = instr->GetLocations();
2246 Primitive::Type type = instr->GetType();
2247
2248 Location rhs_location = locations->InAt(1);
2249 bool use_imm = rhs_location.IsConstant();
2250 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2251 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002252 const uint32_t shift_mask =
2253 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002254 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002255 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2256 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002257
2258 switch (type) {
2259 case Primitive::kPrimInt: {
2260 Register dst = locations->Out().AsRegister<Register>();
2261 Register lhs = locations->InAt(0).AsRegister<Register>();
2262 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002263 if (shift_value == 0) {
2264 if (dst != lhs) {
2265 __ Move(dst, lhs);
2266 }
2267 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268 __ Sll(dst, lhs, shift_value);
2269 } else if (instr->IsShr()) {
2270 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002271 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002273 } else {
2274 if (has_ins_rotr) {
2275 __ Rotr(dst, lhs, shift_value);
2276 } else {
2277 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2278 __ Srl(dst, lhs, shift_value);
2279 __ Or(dst, dst, TMP);
2280 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002281 }
2282 } else {
2283 if (instr->IsShl()) {
2284 __ Sllv(dst, lhs, rhs_reg);
2285 } else if (instr->IsShr()) {
2286 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002287 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002289 } else {
2290 if (has_ins_rotr) {
2291 __ Rotrv(dst, lhs, rhs_reg);
2292 } else {
2293 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002294 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2295 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2296 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2297 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2298 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002299 __ Sllv(TMP, lhs, TMP);
2300 __ Srlv(dst, lhs, rhs_reg);
2301 __ Or(dst, dst, TMP);
2302 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 }
2304 }
2305 break;
2306 }
2307
2308 case Primitive::kPrimLong: {
2309 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2310 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2311 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2312 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2313 if (use_imm) {
2314 if (shift_value == 0) {
2315 codegen_->Move64(locations->Out(), locations->InAt(0));
2316 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002317 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002318 if (instr->IsShl()) {
2319 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2320 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2321 __ Sll(dst_low, lhs_low, shift_value);
2322 } else if (instr->IsShr()) {
2323 __ Srl(dst_low, lhs_low, shift_value);
2324 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2325 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002326 } else if (instr->IsUShr()) {
2327 __ Srl(dst_low, lhs_low, shift_value);
2328 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2329 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002330 } else {
2331 __ Srl(dst_low, lhs_low, shift_value);
2332 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2333 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002334 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002335 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002336 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002337 if (instr->IsShl()) {
2338 __ Sll(dst_low, lhs_low, shift_value);
2339 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2340 __ Sll(dst_high, lhs_high, shift_value);
2341 __ Or(dst_high, dst_high, TMP);
2342 } else if (instr->IsShr()) {
2343 __ Sra(dst_high, lhs_high, shift_value);
2344 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2345 __ Srl(dst_low, lhs_low, shift_value);
2346 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002347 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002348 __ Srl(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 {
2353 __ Srl(TMP, lhs_low, shift_value);
2354 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2355 __ Or(dst_low, dst_low, TMP);
2356 __ Srl(TMP, lhs_high, shift_value);
2357 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2358 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002359 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002360 }
2361 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002362 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002363 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002364 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 __ Move(dst_low, ZERO);
2366 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002367 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002369 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002370 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002371 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002372 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002373 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 // 64-bit rotation by 32 is just a swap.
2375 __ Move(dst_low, lhs_high);
2376 __ Move(dst_high, lhs_low);
2377 } else {
2378 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002379 __ Srl(dst_low, lhs_high, shift_value_high);
2380 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2381 __ Srl(dst_high, lhs_low, shift_value_high);
2382 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002383 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002384 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2385 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002386 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002387 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2388 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002389 __ Or(dst_high, dst_high, TMP);
2390 }
2391 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 }
2393 }
2394 } else {
2395 MipsLabel done;
2396 if (instr->IsShl()) {
2397 __ Sllv(dst_low, lhs_low, rhs_reg);
2398 __ Nor(AT, ZERO, rhs_reg);
2399 __ Srl(TMP, lhs_low, 1);
2400 __ Srlv(TMP, TMP, AT);
2401 __ Sllv(dst_high, lhs_high, rhs_reg);
2402 __ Or(dst_high, dst_high, TMP);
2403 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2404 __ Beqz(TMP, &done);
2405 __ Move(dst_high, dst_low);
2406 __ Move(dst_low, ZERO);
2407 } else if (instr->IsShr()) {
2408 __ Srav(dst_high, lhs_high, rhs_reg);
2409 __ Nor(AT, ZERO, rhs_reg);
2410 __ Sll(TMP, lhs_high, 1);
2411 __ Sllv(TMP, TMP, AT);
2412 __ Srlv(dst_low, lhs_low, rhs_reg);
2413 __ Or(dst_low, dst_low, TMP);
2414 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2415 __ Beqz(TMP, &done);
2416 __ Move(dst_low, dst_high);
2417 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002418 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 __ Srlv(dst_high, lhs_high, rhs_reg);
2420 __ Nor(AT, ZERO, rhs_reg);
2421 __ Sll(TMP, lhs_high, 1);
2422 __ Sllv(TMP, TMP, AT);
2423 __ Srlv(dst_low, lhs_low, rhs_reg);
2424 __ Or(dst_low, dst_low, TMP);
2425 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2426 __ Beqz(TMP, &done);
2427 __ Move(dst_low, dst_high);
2428 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002429 } else {
2430 __ Nor(AT, ZERO, rhs_reg);
2431 __ Srlv(TMP, lhs_low, rhs_reg);
2432 __ Sll(dst_low, lhs_high, 1);
2433 __ Sllv(dst_low, dst_low, AT);
2434 __ Or(dst_low, dst_low, TMP);
2435 __ Srlv(TMP, lhs_high, rhs_reg);
2436 __ Sll(dst_high, lhs_low, 1);
2437 __ Sllv(dst_high, dst_high, AT);
2438 __ Or(dst_high, dst_high, TMP);
2439 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2440 __ Beqz(TMP, &done);
2441 __ Move(TMP, dst_high);
2442 __ Move(dst_high, dst_low);
2443 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002444 }
2445 __ Bind(&done);
2446 }
2447 break;
2448 }
2449
2450 default:
2451 LOG(FATAL) << "Unexpected shift operation type " << type;
2452 }
2453}
2454
2455void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2456 HandleBinaryOp(instruction);
2457}
2458
2459void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2460 HandleBinaryOp(instruction);
2461}
2462
2463void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2464 HandleBinaryOp(instruction);
2465}
2466
2467void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2468 HandleBinaryOp(instruction);
2469}
2470
2471void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002472 Primitive::Type type = instruction->GetType();
2473 bool object_array_get_with_read_barrier =
2474 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002475 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002476 new (GetGraph()->GetArena()) LocationSummary(instruction,
2477 object_array_get_with_read_barrier
2478 ? LocationSummary::kCallOnSlowPath
2479 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002480 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2481 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2482 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002483 locations->SetInAt(0, Location::RequiresRegister());
2484 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002485 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2487 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002488 // The output overlaps in the case of an object array get with
2489 // read barriers enabled: we do not want the move to overwrite the
2490 // array's location, as we need it to emit the read barrier.
2491 locations->SetOut(Location::RequiresRegister(),
2492 object_array_get_with_read_barrier
2493 ? Location::kOutputOverlap
2494 : Location::kNoOutputOverlap);
2495 }
2496 // We need a temporary register for the read barrier marking slow
2497 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2498 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2499 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002500 }
2501}
2502
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002503static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2504 auto null_checker = [codegen, instruction]() {
2505 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002506 };
2507 return null_checker;
2508}
2509
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002510void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2511 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002512 Location obj_loc = locations->InAt(0);
2513 Register obj = obj_loc.AsRegister<Register>();
2514 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002515 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002516 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002517 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002518
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002519 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002520 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2521 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002522 switch (type) {
2523 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002524 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002525 if (index.IsConstant()) {
2526 size_t offset =
2527 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002528 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002529 } else {
2530 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002531 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002532 }
2533 break;
2534 }
2535
2536 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002537 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002538 if (index.IsConstant()) {
2539 size_t offset =
2540 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002541 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002542 } else {
2543 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002544 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002545 }
2546 break;
2547 }
2548
2549 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002550 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551 if (index.IsConstant()) {
2552 size_t offset =
2553 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002554 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002555 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002556 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002557 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002558 }
2559 break;
2560 }
2561
2562 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002563 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002564 if (maybe_compressed_char_at) {
2565 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2566 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2567 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2568 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2569 "Expecting 0=compressed, 1=uncompressed");
2570 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002572 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2573 if (maybe_compressed_char_at) {
2574 MipsLabel uncompressed_load, done;
2575 __ Bnez(TMP, &uncompressed_load);
2576 __ LoadFromOffset(kLoadUnsignedByte,
2577 out,
2578 obj,
2579 data_offset + (const_index << TIMES_1));
2580 __ B(&done);
2581 __ Bind(&uncompressed_load);
2582 __ LoadFromOffset(kLoadUnsignedHalfword,
2583 out,
2584 obj,
2585 data_offset + (const_index << TIMES_2));
2586 __ Bind(&done);
2587 } else {
2588 __ LoadFromOffset(kLoadUnsignedHalfword,
2589 out,
2590 obj,
2591 data_offset + (const_index << TIMES_2),
2592 null_checker);
2593 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002594 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002595 Register index_reg = index.AsRegister<Register>();
2596 if (maybe_compressed_char_at) {
2597 MipsLabel uncompressed_load, done;
2598 __ Bnez(TMP, &uncompressed_load);
2599 __ Addu(TMP, obj, index_reg);
2600 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2601 __ B(&done);
2602 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002603 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002604 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2605 __ Bind(&done);
2606 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002607 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002608 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2609 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002610 }
2611 break;
2612 }
2613
Alexey Frunze15958152017-02-09 19:08:30 -08002614 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002615 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002616 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 if (index.IsConstant()) {
2618 size_t offset =
2619 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002620 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002621 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002622 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002623 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 }
2625 break;
2626 }
2627
Alexey Frunze15958152017-02-09 19:08:30 -08002628 case Primitive::kPrimNot: {
2629 static_assert(
2630 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2631 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2632 // /* HeapReference<Object> */ out =
2633 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2634 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2635 Location temp = locations->GetTemp(0);
2636 // Note that a potential implicit null check is handled in this
2637 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2638 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2639 out_loc,
2640 obj,
2641 data_offset,
2642 index,
2643 temp,
2644 /* needs_null_check */ true);
2645 } else {
2646 Register out = out_loc.AsRegister<Register>();
2647 if (index.IsConstant()) {
2648 size_t offset =
2649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2650 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2651 // If read barriers are enabled, emit read barriers other than
2652 // Baker's using a slow path (and also unpoison the loaded
2653 // reference, if heap poisoning is enabled).
2654 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2655 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002656 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002657 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2658 // If read barriers are enabled, emit read barriers other than
2659 // Baker's using a slow path (and also unpoison the loaded
2660 // reference, if heap poisoning is enabled).
2661 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2662 out_loc,
2663 out_loc,
2664 obj_loc,
2665 data_offset,
2666 index);
2667 }
2668 }
2669 break;
2670 }
2671
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002673 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002674 if (index.IsConstant()) {
2675 size_t offset =
2676 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002677 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002678 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002679 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002680 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 }
2682 break;
2683 }
2684
2685 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002686 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002687 if (index.IsConstant()) {
2688 size_t offset =
2689 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002690 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002691 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002692 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002693 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694 }
2695 break;
2696 }
2697
2698 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002699 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002700 if (index.IsConstant()) {
2701 size_t offset =
2702 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002703 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002704 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002705 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002706 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 }
2708 break;
2709 }
2710
2711 case Primitive::kPrimVoid:
2712 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2713 UNREACHABLE();
2714 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002715}
2716
2717void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2719 locations->SetInAt(0, Location::RequiresRegister());
2720 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2721}
2722
2723void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2724 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002725 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002726 Register obj = locations->InAt(0).AsRegister<Register>();
2727 Register out = locations->Out().AsRegister<Register>();
2728 __ LoadFromOffset(kLoadWord, out, obj, offset);
2729 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002730 // Mask out compression flag from String's array length.
2731 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2732 __ Srl(out, out, 1u);
2733 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002734}
2735
Alexey Frunzef58b2482016-09-02 22:14:06 -07002736Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2737 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2738 ? Location::ConstantLocation(instruction->AsConstant())
2739 : Location::RequiresRegister();
2740}
2741
2742Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2743 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2744 // We can store a non-zero float or double constant without first loading it into the FPU,
2745 // but we should only prefer this if the constant has a single use.
2746 if (instruction->IsConstant() &&
2747 (instruction->AsConstant()->IsZeroBitPattern() ||
2748 instruction->GetUses().HasExactlyOneElement())) {
2749 return Location::ConstantLocation(instruction->AsConstant());
2750 // Otherwise fall through and require an FPU register for the constant.
2751 }
2752 return Location::RequiresFpuRegister();
2753}
2754
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002755void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002756 Primitive::Type value_type = instruction->GetComponentType();
2757
2758 bool needs_write_barrier =
2759 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2760 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2761
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002762 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2763 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002764 may_need_runtime_call_for_type_check ?
2765 LocationSummary::kCallOnSlowPath :
2766 LocationSummary::kNoCall);
2767
2768 locations->SetInAt(0, Location::RequiresRegister());
2769 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2770 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2771 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002772 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002773 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2774 }
2775 if (needs_write_barrier) {
2776 // Temporary register for the write barrier.
2777 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002778 }
2779}
2780
2781void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2782 LocationSummary* locations = instruction->GetLocations();
2783 Register obj = locations->InAt(0).AsRegister<Register>();
2784 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002785 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002786 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002787 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002788 bool needs_write_barrier =
2789 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002790 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002791 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002792
2793 switch (value_type) {
2794 case Primitive::kPrimBoolean:
2795 case Primitive::kPrimByte: {
2796 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002798 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002799 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002800 __ Addu(base_reg, obj, index.AsRegister<Register>());
2801 }
2802 if (value_location.IsConstant()) {
2803 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2804 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2805 } else {
2806 Register value = value_location.AsRegister<Register>();
2807 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002808 }
2809 break;
2810 }
2811
2812 case Primitive::kPrimShort:
2813 case Primitive::kPrimChar: {
2814 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002816 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002818 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002819 }
2820 if (value_location.IsConstant()) {
2821 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2822 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2823 } else {
2824 Register value = value_location.AsRegister<Register>();
2825 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002826 }
2827 break;
2828 }
2829
Alexey Frunze15958152017-02-09 19:08:30 -08002830 case Primitive::kPrimInt: {
2831 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2832 if (index.IsConstant()) {
2833 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2834 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002835 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002836 }
2837 if (value_location.IsConstant()) {
2838 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2839 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2840 } else {
2841 Register value = value_location.AsRegister<Register>();
2842 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2843 }
2844 break;
2845 }
2846
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002847 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002848 if (value_location.IsConstant()) {
2849 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002851 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002852 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002853 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002854 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002855 }
Alexey Frunze15958152017-02-09 19:08:30 -08002856 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2857 DCHECK_EQ(value, 0);
2858 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2859 DCHECK(!needs_write_barrier);
2860 DCHECK(!may_need_runtime_call_for_type_check);
2861 break;
2862 }
2863
2864 DCHECK(needs_write_barrier);
2865 Register value = value_location.AsRegister<Register>();
2866 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2867 Register temp2 = TMP; // Doesn't need to survive slow path.
2868 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2869 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2870 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2871 MipsLabel done;
2872 SlowPathCodeMIPS* slow_path = nullptr;
2873
2874 if (may_need_runtime_call_for_type_check) {
2875 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2876 codegen_->AddSlowPath(slow_path);
2877 if (instruction->GetValueCanBeNull()) {
2878 MipsLabel non_zero;
2879 __ Bnez(value, &non_zero);
2880 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2881 if (index.IsConstant()) {
2882 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002883 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002884 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002885 }
Alexey Frunze15958152017-02-09 19:08:30 -08002886 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2887 __ B(&done);
2888 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002889 }
Alexey Frunze15958152017-02-09 19:08:30 -08002890
2891 // Note that when read barriers are enabled, the type checks
2892 // are performed without read barriers. This is fine, even in
2893 // the case where a class object is in the from-space after
2894 // the flip, as a comparison involving such a type would not
2895 // produce a false positive; it may of course produce a false
2896 // negative, in which case we would take the ArraySet slow
2897 // path.
2898
2899 // /* HeapReference<Class> */ temp1 = obj->klass_
2900 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2901 __ MaybeUnpoisonHeapReference(temp1);
2902
2903 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2904 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2905 // /* HeapReference<Class> */ temp2 = value->klass_
2906 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2907 // If heap poisoning is enabled, no need to unpoison `temp1`
2908 // nor `temp2`, as we are comparing two poisoned references.
2909
2910 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2911 MipsLabel do_put;
2912 __ Beq(temp1, temp2, &do_put);
2913 // If heap poisoning is enabled, the `temp1` reference has
2914 // not been unpoisoned yet; unpoison it now.
2915 __ MaybeUnpoisonHeapReference(temp1);
2916
2917 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2918 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2919 // If heap poisoning is enabled, no need to unpoison
2920 // `temp1`, as we are comparing against null below.
2921 __ Bnez(temp1, slow_path->GetEntryLabel());
2922 __ Bind(&do_put);
2923 } else {
2924 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2925 }
2926 }
2927
2928 Register source = value;
2929 if (kPoisonHeapReferences) {
2930 // Note that in the case where `value` is a null reference,
2931 // we do not enter this block, as a null reference does not
2932 // need poisoning.
2933 __ Move(temp1, value);
2934 __ PoisonHeapReference(temp1);
2935 source = temp1;
2936 }
2937
2938 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2939 if (index.IsConstant()) {
2940 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002941 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002942 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002943 }
2944 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2945
2946 if (!may_need_runtime_call_for_type_check) {
2947 codegen_->MaybeRecordImplicitNullCheck(instruction);
2948 }
2949
2950 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2951
2952 if (done.IsLinked()) {
2953 __ Bind(&done);
2954 }
2955
2956 if (slow_path != nullptr) {
2957 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002958 }
2959 break;
2960 }
2961
2962 case Primitive::kPrimLong: {
2963 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002964 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002965 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002966 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002967 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002968 }
2969 if (value_location.IsConstant()) {
2970 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2971 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2972 } else {
2973 Register value = value_location.AsRegisterPairLow<Register>();
2974 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002975 }
2976 break;
2977 }
2978
2979 case Primitive::kPrimFloat: {
2980 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002982 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002983 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002984 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002985 }
2986 if (value_location.IsConstant()) {
2987 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2988 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2989 } else {
2990 FRegister value = value_location.AsFpuRegister<FRegister>();
2991 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002992 }
2993 break;
2994 }
2995
2996 case Primitive::kPrimDouble: {
2997 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002998 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002999 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003000 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003001 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003002 }
3003 if (value_location.IsConstant()) {
3004 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3005 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3006 } else {
3007 FRegister value = value_location.AsFpuRegister<FRegister>();
3008 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003009 }
3010 break;
3011 }
3012
3013 case Primitive::kPrimVoid:
3014 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3015 UNREACHABLE();
3016 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003017}
3018
3019void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003020 RegisterSet caller_saves = RegisterSet::Empty();
3021 InvokeRuntimeCallingConvention calling_convention;
3022 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3023 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3024 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003025 locations->SetInAt(0, Location::RequiresRegister());
3026 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003027}
3028
3029void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3030 LocationSummary* locations = instruction->GetLocations();
3031 BoundsCheckSlowPathMIPS* slow_path =
3032 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3033 codegen_->AddSlowPath(slow_path);
3034
3035 Register index = locations->InAt(0).AsRegister<Register>();
3036 Register length = locations->InAt(1).AsRegister<Register>();
3037
3038 // length is limited by the maximum positive signed 32-bit integer.
3039 // Unsigned comparison of length and index checks for index < 0
3040 // and for length <= index simultaneously.
3041 __ Bgeu(index, length, slow_path->GetEntryLabel());
3042}
3043
Alexey Frunze15958152017-02-09 19:08:30 -08003044// Temp is used for read barrier.
3045static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3046 if (kEmitCompilerReadBarrier &&
3047 (kUseBakerReadBarrier ||
3048 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3049 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3050 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3051 return 1;
3052 }
3053 return 0;
3054}
3055
3056// Extra temp is used for read barrier.
3057static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3058 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3059}
3060
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003061void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003062 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3063 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3064
3065 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3066 switch (type_check_kind) {
3067 case TypeCheckKind::kExactCheck:
3068 case TypeCheckKind::kAbstractClassCheck:
3069 case TypeCheckKind::kClassHierarchyCheck:
3070 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003071 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003072 ? LocationSummary::kCallOnSlowPath
3073 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3074 break;
3075 case TypeCheckKind::kArrayCheck:
3076 case TypeCheckKind::kUnresolvedCheck:
3077 case TypeCheckKind::kInterfaceCheck:
3078 call_kind = LocationSummary::kCallOnSlowPath;
3079 break;
3080 }
3081
3082 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003083 locations->SetInAt(0, Location::RequiresRegister());
3084 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003085 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003086}
3087
3088void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003089 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003090 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003091 Location obj_loc = locations->InAt(0);
3092 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003094 Location temp_loc = locations->GetTemp(0);
3095 Register temp = temp_loc.AsRegister<Register>();
3096 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3097 DCHECK_LE(num_temps, 2u);
3098 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003099 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3100 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3101 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3102 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3103 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3104 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3105 const uint32_t object_array_data_offset =
3106 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3107 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003109 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3110 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3111 // read barriers is done for performance and code size reasons.
3112 bool is_type_check_slow_path_fatal = false;
3113 if (!kEmitCompilerReadBarrier) {
3114 is_type_check_slow_path_fatal =
3115 (type_check_kind == TypeCheckKind::kExactCheck ||
3116 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3117 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3118 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3119 !instruction->CanThrowIntoCatchBlock();
3120 }
3121 SlowPathCodeMIPS* slow_path =
3122 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3123 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003124 codegen_->AddSlowPath(slow_path);
3125
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003126 // Avoid this check if we know `obj` is not null.
3127 if (instruction->MustDoNullCheck()) {
3128 __ Beqz(obj, &done);
3129 }
3130
3131 switch (type_check_kind) {
3132 case TypeCheckKind::kExactCheck:
3133 case TypeCheckKind::kArrayCheck: {
3134 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003135 GenerateReferenceLoadTwoRegisters(instruction,
3136 temp_loc,
3137 obj_loc,
3138 class_offset,
3139 maybe_temp2_loc,
3140 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003141 // Jump to slow path for throwing the exception or doing a
3142 // more involved array check.
3143 __ Bne(temp, cls, slow_path->GetEntryLabel());
3144 break;
3145 }
3146
3147 case TypeCheckKind::kAbstractClassCheck: {
3148 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003149 GenerateReferenceLoadTwoRegisters(instruction,
3150 temp_loc,
3151 obj_loc,
3152 class_offset,
3153 maybe_temp2_loc,
3154 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003155 // If the class is abstract, we eagerly fetch the super class of the
3156 // object to avoid doing a comparison we know will fail.
3157 MipsLabel loop;
3158 __ Bind(&loop);
3159 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003160 GenerateReferenceLoadOneRegister(instruction,
3161 temp_loc,
3162 super_offset,
3163 maybe_temp2_loc,
3164 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003165 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3166 // exception.
3167 __ Beqz(temp, slow_path->GetEntryLabel());
3168 // Otherwise, compare the classes.
3169 __ Bne(temp, cls, &loop);
3170 break;
3171 }
3172
3173 case TypeCheckKind::kClassHierarchyCheck: {
3174 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003175 GenerateReferenceLoadTwoRegisters(instruction,
3176 temp_loc,
3177 obj_loc,
3178 class_offset,
3179 maybe_temp2_loc,
3180 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003181 // Walk over the class hierarchy to find a match.
3182 MipsLabel loop;
3183 __ Bind(&loop);
3184 __ Beq(temp, cls, &done);
3185 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003186 GenerateReferenceLoadOneRegister(instruction,
3187 temp_loc,
3188 super_offset,
3189 maybe_temp2_loc,
3190 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003191 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3192 // exception. Otherwise, jump to the beginning of the loop.
3193 __ Bnez(temp, &loop);
3194 __ B(slow_path->GetEntryLabel());
3195 break;
3196 }
3197
3198 case TypeCheckKind::kArrayObjectCheck: {
3199 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003200 GenerateReferenceLoadTwoRegisters(instruction,
3201 temp_loc,
3202 obj_loc,
3203 class_offset,
3204 maybe_temp2_loc,
3205 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003206 // Do an exact check.
3207 __ Beq(temp, cls, &done);
3208 // Otherwise, we need to check that the object's class is a non-primitive array.
3209 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003210 GenerateReferenceLoadOneRegister(instruction,
3211 temp_loc,
3212 component_offset,
3213 maybe_temp2_loc,
3214 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003215 // If the component type is null, jump to the slow path to throw the exception.
3216 __ Beqz(temp, slow_path->GetEntryLabel());
3217 // Otherwise, the object is indeed an array, further check that this component
3218 // type is not a primitive type.
3219 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3220 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3221 __ Bnez(temp, slow_path->GetEntryLabel());
3222 break;
3223 }
3224
3225 case TypeCheckKind::kUnresolvedCheck:
3226 // We always go into the type check slow path for the unresolved check case.
3227 // We cannot directly call the CheckCast runtime entry point
3228 // without resorting to a type checking slow path here (i.e. by
3229 // calling InvokeRuntime directly), as it would require to
3230 // assign fixed registers for the inputs of this HInstanceOf
3231 // instruction (following the runtime calling convention), which
3232 // might be cluttered by the potential first read barrier
3233 // emission at the beginning of this method.
3234 __ B(slow_path->GetEntryLabel());
3235 break;
3236
3237 case TypeCheckKind::kInterfaceCheck: {
3238 // Avoid read barriers to improve performance of the fast path. We can not get false
3239 // positives by doing this.
3240 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003241 GenerateReferenceLoadTwoRegisters(instruction,
3242 temp_loc,
3243 obj_loc,
3244 class_offset,
3245 maybe_temp2_loc,
3246 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003247 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003248 GenerateReferenceLoadTwoRegisters(instruction,
3249 temp_loc,
3250 temp_loc,
3251 iftable_offset,
3252 maybe_temp2_loc,
3253 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003254 // Iftable is never null.
3255 __ Lw(TMP, temp, array_length_offset);
3256 // Loop through the iftable and check if any class matches.
3257 MipsLabel loop;
3258 __ Bind(&loop);
3259 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3260 __ Beqz(TMP, slow_path->GetEntryLabel());
3261 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3262 __ MaybeUnpoisonHeapReference(AT);
3263 // Go to next interface.
3264 __ Addiu(TMP, TMP, -2);
3265 // Compare the classes and continue the loop if they do not match.
3266 __ Bne(AT, cls, &loop);
3267 break;
3268 }
3269 }
3270
3271 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003272 __ Bind(slow_path->GetExitLabel());
3273}
3274
3275void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3278 locations->SetInAt(0, Location::RequiresRegister());
3279 if (check->HasUses()) {
3280 locations->SetOut(Location::SameAsFirstInput());
3281 }
3282}
3283
3284void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3285 // We assume the class is not null.
3286 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3287 check->GetLoadClass(),
3288 check,
3289 check->GetDexPc(),
3290 true);
3291 codegen_->AddSlowPath(slow_path);
3292 GenerateClassInitializationCheck(slow_path,
3293 check->GetLocations()->InAt(0).AsRegister<Register>());
3294}
3295
3296void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3297 Primitive::Type in_type = compare->InputAt(0)->GetType();
3298
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003299 LocationSummary* locations =
3300 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003301
3302 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003303 case Primitive::kPrimBoolean:
3304 case Primitive::kPrimByte:
3305 case Primitive::kPrimShort:
3306 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003307 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003308 locations->SetInAt(0, Location::RequiresRegister());
3309 locations->SetInAt(1, Location::RequiresRegister());
3310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3311 break;
3312
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003313 case Primitive::kPrimLong:
3314 locations->SetInAt(0, Location::RequiresRegister());
3315 locations->SetInAt(1, Location::RequiresRegister());
3316 // Output overlaps because it is written before doing the low comparison.
3317 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3318 break;
3319
3320 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003321 case Primitive::kPrimDouble:
3322 locations->SetInAt(0, Location::RequiresFpuRegister());
3323 locations->SetInAt(1, Location::RequiresFpuRegister());
3324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003325 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003326
3327 default:
3328 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3329 }
3330}
3331
3332void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3333 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003334 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003335 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003336 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003337
3338 // 0 if: left == right
3339 // 1 if: left > right
3340 // -1 if: left < right
3341 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003342 case Primitive::kPrimBoolean:
3343 case Primitive::kPrimByte:
3344 case Primitive::kPrimShort:
3345 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003346 case Primitive::kPrimInt: {
3347 Register lhs = locations->InAt(0).AsRegister<Register>();
3348 Register rhs = locations->InAt(1).AsRegister<Register>();
3349 __ Slt(TMP, lhs, rhs);
3350 __ Slt(res, rhs, lhs);
3351 __ Subu(res, res, TMP);
3352 break;
3353 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003354 case Primitive::kPrimLong: {
3355 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003356 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3357 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3358 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3359 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3360 // TODO: more efficient (direct) comparison with a constant.
3361 __ Slt(TMP, lhs_high, rhs_high);
3362 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3363 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3364 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3365 __ Sltu(TMP, lhs_low, rhs_low);
3366 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3367 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3368 __ Bind(&done);
3369 break;
3370 }
3371
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003372 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003373 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003374 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3375 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3376 MipsLabel done;
3377 if (isR6) {
3378 __ CmpEqS(FTMP, lhs, rhs);
3379 __ LoadConst32(res, 0);
3380 __ Bc1nez(FTMP, &done);
3381 if (gt_bias) {
3382 __ CmpLtS(FTMP, lhs, rhs);
3383 __ LoadConst32(res, -1);
3384 __ Bc1nez(FTMP, &done);
3385 __ LoadConst32(res, 1);
3386 } else {
3387 __ CmpLtS(FTMP, rhs, lhs);
3388 __ LoadConst32(res, 1);
3389 __ Bc1nez(FTMP, &done);
3390 __ LoadConst32(res, -1);
3391 }
3392 } else {
3393 if (gt_bias) {
3394 __ ColtS(0, lhs, rhs);
3395 __ LoadConst32(res, -1);
3396 __ Bc1t(0, &done);
3397 __ CeqS(0, lhs, rhs);
3398 __ LoadConst32(res, 1);
3399 __ Movt(res, ZERO, 0);
3400 } else {
3401 __ ColtS(0, rhs, lhs);
3402 __ LoadConst32(res, 1);
3403 __ Bc1t(0, &done);
3404 __ CeqS(0, lhs, rhs);
3405 __ LoadConst32(res, -1);
3406 __ Movt(res, ZERO, 0);
3407 }
3408 }
3409 __ Bind(&done);
3410 break;
3411 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003412 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003413 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003414 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3415 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3416 MipsLabel done;
3417 if (isR6) {
3418 __ CmpEqD(FTMP, lhs, rhs);
3419 __ LoadConst32(res, 0);
3420 __ Bc1nez(FTMP, &done);
3421 if (gt_bias) {
3422 __ CmpLtD(FTMP, lhs, rhs);
3423 __ LoadConst32(res, -1);
3424 __ Bc1nez(FTMP, &done);
3425 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003426 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003427 __ CmpLtD(FTMP, rhs, lhs);
3428 __ LoadConst32(res, 1);
3429 __ Bc1nez(FTMP, &done);
3430 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003431 }
3432 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003433 if (gt_bias) {
3434 __ ColtD(0, lhs, rhs);
3435 __ LoadConst32(res, -1);
3436 __ Bc1t(0, &done);
3437 __ CeqD(0, lhs, rhs);
3438 __ LoadConst32(res, 1);
3439 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003440 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003441 __ ColtD(0, rhs, lhs);
3442 __ LoadConst32(res, 1);
3443 __ Bc1t(0, &done);
3444 __ CeqD(0, lhs, rhs);
3445 __ LoadConst32(res, -1);
3446 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003447 }
3448 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003449 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003450 break;
3451 }
3452
3453 default:
3454 LOG(FATAL) << "Unimplemented compare type " << in_type;
3455 }
3456}
3457
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003458void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003460 switch (instruction->InputAt(0)->GetType()) {
3461 default:
3462 case Primitive::kPrimLong:
3463 locations->SetInAt(0, Location::RequiresRegister());
3464 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3465 break;
3466
3467 case Primitive::kPrimFloat:
3468 case Primitive::kPrimDouble:
3469 locations->SetInAt(0, Location::RequiresFpuRegister());
3470 locations->SetInAt(1, Location::RequiresFpuRegister());
3471 break;
3472 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003473 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3475 }
3476}
3477
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003478void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003479 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003480 return;
3481 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003483 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003484 LocationSummary* locations = instruction->GetLocations();
3485 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003486 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003487
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003488 switch (type) {
3489 default:
3490 // Integer case.
3491 GenerateIntCompare(instruction->GetCondition(), locations);
3492 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003493
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003494 case Primitive::kPrimLong:
3495 // TODO: don't use branches.
3496 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003497 break;
3498
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003499 case Primitive::kPrimFloat:
3500 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003501 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3502 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003503 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003504
3505 // Convert the branches into the result.
3506 MipsLabel done;
3507
3508 // False case: result = 0.
3509 __ LoadConst32(dst, 0);
3510 __ B(&done);
3511
3512 // True case: result = 1.
3513 __ Bind(&true_label);
3514 __ LoadConst32(dst, 1);
3515 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003516}
3517
Alexey Frunze7e99e052015-11-24 19:28:01 -08003518void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3519 DCHECK(instruction->IsDiv() || instruction->IsRem());
3520 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3521
3522 LocationSummary* locations = instruction->GetLocations();
3523 Location second = locations->InAt(1);
3524 DCHECK(second.IsConstant());
3525
3526 Register out = locations->Out().AsRegister<Register>();
3527 Register dividend = locations->InAt(0).AsRegister<Register>();
3528 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3529 DCHECK(imm == 1 || imm == -1);
3530
3531 if (instruction->IsRem()) {
3532 __ Move(out, ZERO);
3533 } else {
3534 if (imm == -1) {
3535 __ Subu(out, ZERO, dividend);
3536 } else if (out != dividend) {
3537 __ Move(out, dividend);
3538 }
3539 }
3540}
3541
3542void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3543 DCHECK(instruction->IsDiv() || instruction->IsRem());
3544 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3545
3546 LocationSummary* locations = instruction->GetLocations();
3547 Location second = locations->InAt(1);
3548 DCHECK(second.IsConstant());
3549
3550 Register out = locations->Out().AsRegister<Register>();
3551 Register dividend = locations->InAt(0).AsRegister<Register>();
3552 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003553 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003554 int ctz_imm = CTZ(abs_imm);
3555
3556 if (instruction->IsDiv()) {
3557 if (ctz_imm == 1) {
3558 // Fast path for division by +/-2, which is very common.
3559 __ Srl(TMP, dividend, 31);
3560 } else {
3561 __ Sra(TMP, dividend, 31);
3562 __ Srl(TMP, TMP, 32 - ctz_imm);
3563 }
3564 __ Addu(out, dividend, TMP);
3565 __ Sra(out, out, ctz_imm);
3566 if (imm < 0) {
3567 __ Subu(out, ZERO, out);
3568 }
3569 } else {
3570 if (ctz_imm == 1) {
3571 // Fast path for modulo +/-2, which is very common.
3572 __ Sra(TMP, dividend, 31);
3573 __ Subu(out, dividend, TMP);
3574 __ Andi(out, out, 1);
3575 __ Addu(out, out, TMP);
3576 } else {
3577 __ Sra(TMP, dividend, 31);
3578 __ Srl(TMP, TMP, 32 - ctz_imm);
3579 __ Addu(out, dividend, TMP);
3580 if (IsUint<16>(abs_imm - 1)) {
3581 __ Andi(out, out, abs_imm - 1);
3582 } else {
3583 __ Sll(out, out, 32 - ctz_imm);
3584 __ Srl(out, out, 32 - ctz_imm);
3585 }
3586 __ Subu(out, out, TMP);
3587 }
3588 }
3589}
3590
3591void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3592 DCHECK(instruction->IsDiv() || instruction->IsRem());
3593 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3594
3595 LocationSummary* locations = instruction->GetLocations();
3596 Location second = locations->InAt(1);
3597 DCHECK(second.IsConstant());
3598
3599 Register out = locations->Out().AsRegister<Register>();
3600 Register dividend = locations->InAt(0).AsRegister<Register>();
3601 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3602
3603 int64_t magic;
3604 int shift;
3605 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3606
3607 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3608
3609 __ LoadConst32(TMP, magic);
3610 if (isR6) {
3611 __ MuhR6(TMP, dividend, TMP);
3612 } else {
3613 __ MultR2(dividend, TMP);
3614 __ Mfhi(TMP);
3615 }
3616 if (imm > 0 && magic < 0) {
3617 __ Addu(TMP, TMP, dividend);
3618 } else if (imm < 0 && magic > 0) {
3619 __ Subu(TMP, TMP, dividend);
3620 }
3621
3622 if (shift != 0) {
3623 __ Sra(TMP, TMP, shift);
3624 }
3625
3626 if (instruction->IsDiv()) {
3627 __ Sra(out, TMP, 31);
3628 __ Subu(out, TMP, out);
3629 } else {
3630 __ Sra(AT, TMP, 31);
3631 __ Subu(AT, TMP, AT);
3632 __ LoadConst32(TMP, imm);
3633 if (isR6) {
3634 __ MulR6(TMP, AT, TMP);
3635 } else {
3636 __ MulR2(TMP, AT, TMP);
3637 }
3638 __ Subu(out, dividend, TMP);
3639 }
3640}
3641
3642void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3643 DCHECK(instruction->IsDiv() || instruction->IsRem());
3644 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3645
3646 LocationSummary* locations = instruction->GetLocations();
3647 Register out = locations->Out().AsRegister<Register>();
3648 Location second = locations->InAt(1);
3649
3650 if (second.IsConstant()) {
3651 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3652 if (imm == 0) {
3653 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3654 } else if (imm == 1 || imm == -1) {
3655 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003656 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003657 DivRemByPowerOfTwo(instruction);
3658 } else {
3659 DCHECK(imm <= -2 || imm >= 2);
3660 GenerateDivRemWithAnyConstant(instruction);
3661 }
3662 } else {
3663 Register dividend = locations->InAt(0).AsRegister<Register>();
3664 Register divisor = second.AsRegister<Register>();
3665 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3666 if (instruction->IsDiv()) {
3667 if (isR6) {
3668 __ DivR6(out, dividend, divisor);
3669 } else {
3670 __ DivR2(out, dividend, divisor);
3671 }
3672 } else {
3673 if (isR6) {
3674 __ ModR6(out, dividend, divisor);
3675 } else {
3676 __ ModR2(out, dividend, divisor);
3677 }
3678 }
3679 }
3680}
3681
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3683 Primitive::Type type = div->GetResultType();
3684 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003685 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686 : LocationSummary::kNoCall;
3687
3688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3689
3690 switch (type) {
3691 case Primitive::kPrimInt:
3692 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003693 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3695 break;
3696
3697 case Primitive::kPrimLong: {
3698 InvokeRuntimeCallingConvention calling_convention;
3699 locations->SetInAt(0, Location::RegisterPairLocation(
3700 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3701 locations->SetInAt(1, Location::RegisterPairLocation(
3702 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3703 locations->SetOut(calling_convention.GetReturnLocation(type));
3704 break;
3705 }
3706
3707 case Primitive::kPrimFloat:
3708 case Primitive::kPrimDouble:
3709 locations->SetInAt(0, Location::RequiresFpuRegister());
3710 locations->SetInAt(1, Location::RequiresFpuRegister());
3711 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3712 break;
3713
3714 default:
3715 LOG(FATAL) << "Unexpected div type " << type;
3716 }
3717}
3718
3719void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3720 Primitive::Type type = instruction->GetType();
3721 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003722
3723 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003724 case Primitive::kPrimInt:
3725 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003726 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003727 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003728 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003729 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3730 break;
3731 }
3732 case Primitive::kPrimFloat:
3733 case Primitive::kPrimDouble: {
3734 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3735 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3736 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3737 if (type == Primitive::kPrimFloat) {
3738 __ DivS(dst, lhs, rhs);
3739 } else {
3740 __ DivD(dst, lhs, rhs);
3741 }
3742 break;
3743 }
3744 default:
3745 LOG(FATAL) << "Unexpected div type " << type;
3746 }
3747}
3748
3749void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003750 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003751 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003752}
3753
3754void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3755 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3756 codegen_->AddSlowPath(slow_path);
3757 Location value = instruction->GetLocations()->InAt(0);
3758 Primitive::Type type = instruction->GetType();
3759
3760 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003761 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003762 case Primitive::kPrimByte:
3763 case Primitive::kPrimChar:
3764 case Primitive::kPrimShort:
3765 case Primitive::kPrimInt: {
3766 if (value.IsConstant()) {
3767 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3768 __ B(slow_path->GetEntryLabel());
3769 } else {
3770 // A division by a non-null constant is valid. We don't need to perform
3771 // any check, so simply fall through.
3772 }
3773 } else {
3774 DCHECK(value.IsRegister()) << value;
3775 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3776 }
3777 break;
3778 }
3779 case Primitive::kPrimLong: {
3780 if (value.IsConstant()) {
3781 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3782 __ B(slow_path->GetEntryLabel());
3783 } else {
3784 // A division by a non-null constant is valid. We don't need to perform
3785 // any check, so simply fall through.
3786 }
3787 } else {
3788 DCHECK(value.IsRegisterPair()) << value;
3789 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3790 __ Beqz(TMP, slow_path->GetEntryLabel());
3791 }
3792 break;
3793 }
3794 default:
3795 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3796 }
3797}
3798
3799void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3800 LocationSummary* locations =
3801 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3802 locations->SetOut(Location::ConstantLocation(constant));
3803}
3804
3805void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3806 // Will be generated at use site.
3807}
3808
3809void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3810 exit->SetLocations(nullptr);
3811}
3812
3813void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3814}
3815
3816void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3817 LocationSummary* locations =
3818 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3819 locations->SetOut(Location::ConstantLocation(constant));
3820}
3821
3822void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3823 // Will be generated at use site.
3824}
3825
3826void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3827 got->SetLocations(nullptr);
3828}
3829
3830void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3831 DCHECK(!successor->IsExitBlock());
3832 HBasicBlock* block = got->GetBlock();
3833 HInstruction* previous = got->GetPrevious();
3834 HLoopInformation* info = block->GetLoopInformation();
3835
3836 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3837 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3838 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3839 return;
3840 }
3841 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3842 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3843 }
3844 if (!codegen_->GoesToNextBlock(block, successor)) {
3845 __ B(codegen_->GetLabelOf(successor));
3846 }
3847}
3848
3849void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3850 HandleGoto(got, got->GetSuccessor());
3851}
3852
3853void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3854 try_boundary->SetLocations(nullptr);
3855}
3856
3857void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3858 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3859 if (!successor->IsExitBlock()) {
3860 HandleGoto(try_boundary, successor);
3861 }
3862}
3863
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003864void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3865 LocationSummary* locations) {
3866 Register dst = locations->Out().AsRegister<Register>();
3867 Register lhs = locations->InAt(0).AsRegister<Register>();
3868 Location rhs_location = locations->InAt(1);
3869 Register rhs_reg = ZERO;
3870 int64_t rhs_imm = 0;
3871 bool use_imm = rhs_location.IsConstant();
3872 if (use_imm) {
3873 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3874 } else {
3875 rhs_reg = rhs_location.AsRegister<Register>();
3876 }
3877
3878 switch (cond) {
3879 case kCondEQ:
3880 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003881 if (use_imm && IsInt<16>(-rhs_imm)) {
3882 if (rhs_imm == 0) {
3883 if (cond == kCondEQ) {
3884 __ Sltiu(dst, lhs, 1);
3885 } else {
3886 __ Sltu(dst, ZERO, lhs);
3887 }
3888 } else {
3889 __ Addiu(dst, lhs, -rhs_imm);
3890 if (cond == kCondEQ) {
3891 __ Sltiu(dst, dst, 1);
3892 } else {
3893 __ Sltu(dst, ZERO, dst);
3894 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003895 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003896 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003897 if (use_imm && IsUint<16>(rhs_imm)) {
3898 __ Xori(dst, lhs, rhs_imm);
3899 } else {
3900 if (use_imm) {
3901 rhs_reg = TMP;
3902 __ LoadConst32(rhs_reg, rhs_imm);
3903 }
3904 __ Xor(dst, lhs, rhs_reg);
3905 }
3906 if (cond == kCondEQ) {
3907 __ Sltiu(dst, dst, 1);
3908 } else {
3909 __ Sltu(dst, ZERO, dst);
3910 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003911 }
3912 break;
3913
3914 case kCondLT:
3915 case kCondGE:
3916 if (use_imm && IsInt<16>(rhs_imm)) {
3917 __ Slti(dst, lhs, rhs_imm);
3918 } else {
3919 if (use_imm) {
3920 rhs_reg = TMP;
3921 __ LoadConst32(rhs_reg, rhs_imm);
3922 }
3923 __ Slt(dst, lhs, rhs_reg);
3924 }
3925 if (cond == kCondGE) {
3926 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3927 // only the slt instruction but no sge.
3928 __ Xori(dst, dst, 1);
3929 }
3930 break;
3931
3932 case kCondLE:
3933 case kCondGT:
3934 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3935 // Simulate lhs <= rhs via lhs < rhs + 1.
3936 __ Slti(dst, lhs, rhs_imm + 1);
3937 if (cond == kCondGT) {
3938 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3939 // only the slti instruction but no sgti.
3940 __ Xori(dst, dst, 1);
3941 }
3942 } else {
3943 if (use_imm) {
3944 rhs_reg = TMP;
3945 __ LoadConst32(rhs_reg, rhs_imm);
3946 }
3947 __ Slt(dst, rhs_reg, lhs);
3948 if (cond == kCondLE) {
3949 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3950 // only the slt instruction but no sle.
3951 __ Xori(dst, dst, 1);
3952 }
3953 }
3954 break;
3955
3956 case kCondB:
3957 case kCondAE:
3958 if (use_imm && IsInt<16>(rhs_imm)) {
3959 // Sltiu sign-extends its 16-bit immediate operand before
3960 // the comparison and thus lets us compare directly with
3961 // unsigned values in the ranges [0, 0x7fff] and
3962 // [0xffff8000, 0xffffffff].
3963 __ Sltiu(dst, lhs, rhs_imm);
3964 } else {
3965 if (use_imm) {
3966 rhs_reg = TMP;
3967 __ LoadConst32(rhs_reg, rhs_imm);
3968 }
3969 __ Sltu(dst, lhs, rhs_reg);
3970 }
3971 if (cond == kCondAE) {
3972 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3973 // only the sltu instruction but no sgeu.
3974 __ Xori(dst, dst, 1);
3975 }
3976 break;
3977
3978 case kCondBE:
3979 case kCondA:
3980 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3981 // Simulate lhs <= rhs via lhs < rhs + 1.
3982 // Note that this only works if rhs + 1 does not overflow
3983 // to 0, hence the check above.
3984 // Sltiu sign-extends its 16-bit immediate operand before
3985 // the comparison and thus lets us compare directly with
3986 // unsigned values in the ranges [0, 0x7fff] and
3987 // [0xffff8000, 0xffffffff].
3988 __ Sltiu(dst, lhs, rhs_imm + 1);
3989 if (cond == kCondA) {
3990 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3991 // only the sltiu instruction but no sgtiu.
3992 __ Xori(dst, dst, 1);
3993 }
3994 } else {
3995 if (use_imm) {
3996 rhs_reg = TMP;
3997 __ LoadConst32(rhs_reg, rhs_imm);
3998 }
3999 __ Sltu(dst, rhs_reg, lhs);
4000 if (cond == kCondBE) {
4001 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4002 // only the sltu instruction but no sleu.
4003 __ Xori(dst, dst, 1);
4004 }
4005 }
4006 break;
4007 }
4008}
4009
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004010bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4011 LocationSummary* input_locations,
4012 Register dst) {
4013 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4014 Location rhs_location = input_locations->InAt(1);
4015 Register rhs_reg = ZERO;
4016 int64_t rhs_imm = 0;
4017 bool use_imm = rhs_location.IsConstant();
4018 if (use_imm) {
4019 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4020 } else {
4021 rhs_reg = rhs_location.AsRegister<Register>();
4022 }
4023
4024 switch (cond) {
4025 case kCondEQ:
4026 case kCondNE:
4027 if (use_imm && IsInt<16>(-rhs_imm)) {
4028 __ Addiu(dst, lhs, -rhs_imm);
4029 } else if (use_imm && IsUint<16>(rhs_imm)) {
4030 __ Xori(dst, lhs, rhs_imm);
4031 } else {
4032 if (use_imm) {
4033 rhs_reg = TMP;
4034 __ LoadConst32(rhs_reg, rhs_imm);
4035 }
4036 __ Xor(dst, lhs, rhs_reg);
4037 }
4038 return (cond == kCondEQ);
4039
4040 case kCondLT:
4041 case kCondGE:
4042 if (use_imm && IsInt<16>(rhs_imm)) {
4043 __ Slti(dst, lhs, rhs_imm);
4044 } else {
4045 if (use_imm) {
4046 rhs_reg = TMP;
4047 __ LoadConst32(rhs_reg, rhs_imm);
4048 }
4049 __ Slt(dst, lhs, rhs_reg);
4050 }
4051 return (cond == kCondGE);
4052
4053 case kCondLE:
4054 case kCondGT:
4055 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4056 // Simulate lhs <= rhs via lhs < rhs + 1.
4057 __ Slti(dst, lhs, rhs_imm + 1);
4058 return (cond == kCondGT);
4059 } else {
4060 if (use_imm) {
4061 rhs_reg = TMP;
4062 __ LoadConst32(rhs_reg, rhs_imm);
4063 }
4064 __ Slt(dst, rhs_reg, lhs);
4065 return (cond == kCondLE);
4066 }
4067
4068 case kCondB:
4069 case kCondAE:
4070 if (use_imm && IsInt<16>(rhs_imm)) {
4071 // Sltiu sign-extends its 16-bit immediate operand before
4072 // the comparison and thus lets us compare directly with
4073 // unsigned values in the ranges [0, 0x7fff] and
4074 // [0xffff8000, 0xffffffff].
4075 __ Sltiu(dst, lhs, rhs_imm);
4076 } else {
4077 if (use_imm) {
4078 rhs_reg = TMP;
4079 __ LoadConst32(rhs_reg, rhs_imm);
4080 }
4081 __ Sltu(dst, lhs, rhs_reg);
4082 }
4083 return (cond == kCondAE);
4084
4085 case kCondBE:
4086 case kCondA:
4087 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4088 // Simulate lhs <= rhs via lhs < rhs + 1.
4089 // Note that this only works if rhs + 1 does not overflow
4090 // to 0, hence the check above.
4091 // Sltiu sign-extends its 16-bit immediate operand before
4092 // the comparison and thus lets us compare directly with
4093 // unsigned values in the ranges [0, 0x7fff] and
4094 // [0xffff8000, 0xffffffff].
4095 __ Sltiu(dst, lhs, rhs_imm + 1);
4096 return (cond == kCondA);
4097 } else {
4098 if (use_imm) {
4099 rhs_reg = TMP;
4100 __ LoadConst32(rhs_reg, rhs_imm);
4101 }
4102 __ Sltu(dst, rhs_reg, lhs);
4103 return (cond == kCondBE);
4104 }
4105 }
4106}
4107
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004108void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4109 LocationSummary* locations,
4110 MipsLabel* label) {
4111 Register lhs = locations->InAt(0).AsRegister<Register>();
4112 Location rhs_location = locations->InAt(1);
4113 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004114 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004115 bool use_imm = rhs_location.IsConstant();
4116 if (use_imm) {
4117 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4118 } else {
4119 rhs_reg = rhs_location.AsRegister<Register>();
4120 }
4121
4122 if (use_imm && rhs_imm == 0) {
4123 switch (cond) {
4124 case kCondEQ:
4125 case kCondBE: // <= 0 if zero
4126 __ Beqz(lhs, label);
4127 break;
4128 case kCondNE:
4129 case kCondA: // > 0 if non-zero
4130 __ Bnez(lhs, label);
4131 break;
4132 case kCondLT:
4133 __ Bltz(lhs, label);
4134 break;
4135 case kCondGE:
4136 __ Bgez(lhs, label);
4137 break;
4138 case kCondLE:
4139 __ Blez(lhs, label);
4140 break;
4141 case kCondGT:
4142 __ Bgtz(lhs, label);
4143 break;
4144 case kCondB: // always false
4145 break;
4146 case kCondAE: // always true
4147 __ B(label);
4148 break;
4149 }
4150 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004151 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4152 if (isR6 || !use_imm) {
4153 if (use_imm) {
4154 rhs_reg = TMP;
4155 __ LoadConst32(rhs_reg, rhs_imm);
4156 }
4157 switch (cond) {
4158 case kCondEQ:
4159 __ Beq(lhs, rhs_reg, label);
4160 break;
4161 case kCondNE:
4162 __ Bne(lhs, rhs_reg, label);
4163 break;
4164 case kCondLT:
4165 __ Blt(lhs, rhs_reg, label);
4166 break;
4167 case kCondGE:
4168 __ Bge(lhs, rhs_reg, label);
4169 break;
4170 case kCondLE:
4171 __ Bge(rhs_reg, lhs, label);
4172 break;
4173 case kCondGT:
4174 __ Blt(rhs_reg, lhs, label);
4175 break;
4176 case kCondB:
4177 __ Bltu(lhs, rhs_reg, label);
4178 break;
4179 case kCondAE:
4180 __ Bgeu(lhs, rhs_reg, label);
4181 break;
4182 case kCondBE:
4183 __ Bgeu(rhs_reg, lhs, label);
4184 break;
4185 case kCondA:
4186 __ Bltu(rhs_reg, lhs, label);
4187 break;
4188 }
4189 } else {
4190 // Special cases for more efficient comparison with constants on R2.
4191 switch (cond) {
4192 case kCondEQ:
4193 __ LoadConst32(TMP, rhs_imm);
4194 __ Beq(lhs, TMP, label);
4195 break;
4196 case kCondNE:
4197 __ LoadConst32(TMP, rhs_imm);
4198 __ Bne(lhs, TMP, label);
4199 break;
4200 case kCondLT:
4201 if (IsInt<16>(rhs_imm)) {
4202 __ Slti(TMP, lhs, rhs_imm);
4203 __ Bnez(TMP, label);
4204 } else {
4205 __ LoadConst32(TMP, rhs_imm);
4206 __ Blt(lhs, TMP, label);
4207 }
4208 break;
4209 case kCondGE:
4210 if (IsInt<16>(rhs_imm)) {
4211 __ Slti(TMP, lhs, rhs_imm);
4212 __ Beqz(TMP, label);
4213 } else {
4214 __ LoadConst32(TMP, rhs_imm);
4215 __ Bge(lhs, TMP, label);
4216 }
4217 break;
4218 case kCondLE:
4219 if (IsInt<16>(rhs_imm + 1)) {
4220 // Simulate lhs <= rhs via lhs < rhs + 1.
4221 __ Slti(TMP, lhs, rhs_imm + 1);
4222 __ Bnez(TMP, label);
4223 } else {
4224 __ LoadConst32(TMP, rhs_imm);
4225 __ Bge(TMP, lhs, label);
4226 }
4227 break;
4228 case kCondGT:
4229 if (IsInt<16>(rhs_imm + 1)) {
4230 // Simulate lhs > rhs via !(lhs < rhs + 1).
4231 __ Slti(TMP, lhs, rhs_imm + 1);
4232 __ Beqz(TMP, label);
4233 } else {
4234 __ LoadConst32(TMP, rhs_imm);
4235 __ Blt(TMP, lhs, label);
4236 }
4237 break;
4238 case kCondB:
4239 if (IsInt<16>(rhs_imm)) {
4240 __ Sltiu(TMP, lhs, rhs_imm);
4241 __ Bnez(TMP, label);
4242 } else {
4243 __ LoadConst32(TMP, rhs_imm);
4244 __ Bltu(lhs, TMP, label);
4245 }
4246 break;
4247 case kCondAE:
4248 if (IsInt<16>(rhs_imm)) {
4249 __ Sltiu(TMP, lhs, rhs_imm);
4250 __ Beqz(TMP, label);
4251 } else {
4252 __ LoadConst32(TMP, rhs_imm);
4253 __ Bgeu(lhs, TMP, label);
4254 }
4255 break;
4256 case kCondBE:
4257 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4258 // Simulate lhs <= rhs via lhs < rhs + 1.
4259 // Note that this only works if rhs + 1 does not overflow
4260 // to 0, hence the check above.
4261 __ Sltiu(TMP, lhs, rhs_imm + 1);
4262 __ Bnez(TMP, label);
4263 } else {
4264 __ LoadConst32(TMP, rhs_imm);
4265 __ Bgeu(TMP, lhs, label);
4266 }
4267 break;
4268 case kCondA:
4269 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4270 // Simulate lhs > rhs via !(lhs < rhs + 1).
4271 // Note that this only works if rhs + 1 does not overflow
4272 // to 0, hence the check above.
4273 __ Sltiu(TMP, lhs, rhs_imm + 1);
4274 __ Beqz(TMP, label);
4275 } else {
4276 __ LoadConst32(TMP, rhs_imm);
4277 __ Bltu(TMP, lhs, label);
4278 }
4279 break;
4280 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004281 }
4282 }
4283}
4284
4285void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4286 LocationSummary* locations,
4287 MipsLabel* label) {
4288 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4289 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4290 Location rhs_location = locations->InAt(1);
4291 Register rhs_high = ZERO;
4292 Register rhs_low = ZERO;
4293 int64_t imm = 0;
4294 uint32_t imm_high = 0;
4295 uint32_t imm_low = 0;
4296 bool use_imm = rhs_location.IsConstant();
4297 if (use_imm) {
4298 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4299 imm_high = High32Bits(imm);
4300 imm_low = Low32Bits(imm);
4301 } else {
4302 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4303 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4304 }
4305
4306 if (use_imm && imm == 0) {
4307 switch (cond) {
4308 case kCondEQ:
4309 case kCondBE: // <= 0 if zero
4310 __ Or(TMP, lhs_high, lhs_low);
4311 __ Beqz(TMP, label);
4312 break;
4313 case kCondNE:
4314 case kCondA: // > 0 if non-zero
4315 __ Or(TMP, lhs_high, lhs_low);
4316 __ Bnez(TMP, label);
4317 break;
4318 case kCondLT:
4319 __ Bltz(lhs_high, label);
4320 break;
4321 case kCondGE:
4322 __ Bgez(lhs_high, label);
4323 break;
4324 case kCondLE:
4325 __ Or(TMP, lhs_high, lhs_low);
4326 __ Sra(AT, lhs_high, 31);
4327 __ Bgeu(AT, TMP, label);
4328 break;
4329 case kCondGT:
4330 __ Or(TMP, lhs_high, lhs_low);
4331 __ Sra(AT, lhs_high, 31);
4332 __ Bltu(AT, TMP, label);
4333 break;
4334 case kCondB: // always false
4335 break;
4336 case kCondAE: // always true
4337 __ B(label);
4338 break;
4339 }
4340 } else if (use_imm) {
4341 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4342 switch (cond) {
4343 case kCondEQ:
4344 __ LoadConst32(TMP, imm_high);
4345 __ Xor(TMP, TMP, lhs_high);
4346 __ LoadConst32(AT, imm_low);
4347 __ Xor(AT, AT, lhs_low);
4348 __ Or(TMP, TMP, AT);
4349 __ Beqz(TMP, label);
4350 break;
4351 case kCondNE:
4352 __ LoadConst32(TMP, imm_high);
4353 __ Xor(TMP, TMP, lhs_high);
4354 __ LoadConst32(AT, imm_low);
4355 __ Xor(AT, AT, lhs_low);
4356 __ Or(TMP, TMP, AT);
4357 __ Bnez(TMP, label);
4358 break;
4359 case kCondLT:
4360 __ LoadConst32(TMP, imm_high);
4361 __ Blt(lhs_high, TMP, label);
4362 __ Slt(TMP, TMP, lhs_high);
4363 __ LoadConst32(AT, imm_low);
4364 __ Sltu(AT, lhs_low, AT);
4365 __ Blt(TMP, AT, label);
4366 break;
4367 case kCondGE:
4368 __ LoadConst32(TMP, imm_high);
4369 __ Blt(TMP, lhs_high, label);
4370 __ Slt(TMP, lhs_high, TMP);
4371 __ LoadConst32(AT, imm_low);
4372 __ Sltu(AT, lhs_low, AT);
4373 __ Or(TMP, TMP, AT);
4374 __ Beqz(TMP, label);
4375 break;
4376 case kCondLE:
4377 __ LoadConst32(TMP, imm_high);
4378 __ Blt(lhs_high, TMP, label);
4379 __ Slt(TMP, TMP, lhs_high);
4380 __ LoadConst32(AT, imm_low);
4381 __ Sltu(AT, AT, lhs_low);
4382 __ Or(TMP, TMP, AT);
4383 __ Beqz(TMP, label);
4384 break;
4385 case kCondGT:
4386 __ LoadConst32(TMP, imm_high);
4387 __ Blt(TMP, lhs_high, label);
4388 __ Slt(TMP, lhs_high, TMP);
4389 __ LoadConst32(AT, imm_low);
4390 __ Sltu(AT, AT, lhs_low);
4391 __ Blt(TMP, AT, label);
4392 break;
4393 case kCondB:
4394 __ LoadConst32(TMP, imm_high);
4395 __ Bltu(lhs_high, TMP, label);
4396 __ Sltu(TMP, TMP, lhs_high);
4397 __ LoadConst32(AT, imm_low);
4398 __ Sltu(AT, lhs_low, AT);
4399 __ Blt(TMP, AT, label);
4400 break;
4401 case kCondAE:
4402 __ LoadConst32(TMP, imm_high);
4403 __ Bltu(TMP, lhs_high, label);
4404 __ Sltu(TMP, lhs_high, TMP);
4405 __ LoadConst32(AT, imm_low);
4406 __ Sltu(AT, lhs_low, AT);
4407 __ Or(TMP, TMP, AT);
4408 __ Beqz(TMP, label);
4409 break;
4410 case kCondBE:
4411 __ LoadConst32(TMP, imm_high);
4412 __ Bltu(lhs_high, TMP, label);
4413 __ Sltu(TMP, TMP, lhs_high);
4414 __ LoadConst32(AT, imm_low);
4415 __ Sltu(AT, AT, lhs_low);
4416 __ Or(TMP, TMP, AT);
4417 __ Beqz(TMP, label);
4418 break;
4419 case kCondA:
4420 __ LoadConst32(TMP, imm_high);
4421 __ Bltu(TMP, lhs_high, label);
4422 __ Sltu(TMP, lhs_high, TMP);
4423 __ LoadConst32(AT, imm_low);
4424 __ Sltu(AT, AT, lhs_low);
4425 __ Blt(TMP, AT, label);
4426 break;
4427 }
4428 } else {
4429 switch (cond) {
4430 case kCondEQ:
4431 __ Xor(TMP, lhs_high, rhs_high);
4432 __ Xor(AT, lhs_low, rhs_low);
4433 __ Or(TMP, TMP, AT);
4434 __ Beqz(TMP, label);
4435 break;
4436 case kCondNE:
4437 __ Xor(TMP, lhs_high, rhs_high);
4438 __ Xor(AT, lhs_low, rhs_low);
4439 __ Or(TMP, TMP, AT);
4440 __ Bnez(TMP, label);
4441 break;
4442 case kCondLT:
4443 __ Blt(lhs_high, rhs_high, label);
4444 __ Slt(TMP, rhs_high, lhs_high);
4445 __ Sltu(AT, lhs_low, rhs_low);
4446 __ Blt(TMP, AT, label);
4447 break;
4448 case kCondGE:
4449 __ Blt(rhs_high, lhs_high, label);
4450 __ Slt(TMP, lhs_high, rhs_high);
4451 __ Sltu(AT, lhs_low, rhs_low);
4452 __ Or(TMP, TMP, AT);
4453 __ Beqz(TMP, label);
4454 break;
4455 case kCondLE:
4456 __ Blt(lhs_high, rhs_high, label);
4457 __ Slt(TMP, rhs_high, lhs_high);
4458 __ Sltu(AT, rhs_low, lhs_low);
4459 __ Or(TMP, TMP, AT);
4460 __ Beqz(TMP, label);
4461 break;
4462 case kCondGT:
4463 __ Blt(rhs_high, lhs_high, label);
4464 __ Slt(TMP, lhs_high, rhs_high);
4465 __ Sltu(AT, rhs_low, lhs_low);
4466 __ Blt(TMP, AT, label);
4467 break;
4468 case kCondB:
4469 __ Bltu(lhs_high, rhs_high, label);
4470 __ Sltu(TMP, rhs_high, lhs_high);
4471 __ Sltu(AT, lhs_low, rhs_low);
4472 __ Blt(TMP, AT, label);
4473 break;
4474 case kCondAE:
4475 __ Bltu(rhs_high, lhs_high, label);
4476 __ Sltu(TMP, lhs_high, rhs_high);
4477 __ Sltu(AT, lhs_low, rhs_low);
4478 __ Or(TMP, TMP, AT);
4479 __ Beqz(TMP, label);
4480 break;
4481 case kCondBE:
4482 __ Bltu(lhs_high, rhs_high, label);
4483 __ Sltu(TMP, rhs_high, lhs_high);
4484 __ Sltu(AT, rhs_low, lhs_low);
4485 __ Or(TMP, TMP, AT);
4486 __ Beqz(TMP, label);
4487 break;
4488 case kCondA:
4489 __ Bltu(rhs_high, lhs_high, label);
4490 __ Sltu(TMP, lhs_high, rhs_high);
4491 __ Sltu(AT, rhs_low, lhs_low);
4492 __ Blt(TMP, AT, label);
4493 break;
4494 }
4495 }
4496}
4497
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004498void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4499 bool gt_bias,
4500 Primitive::Type type,
4501 LocationSummary* locations) {
4502 Register dst = locations->Out().AsRegister<Register>();
4503 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4504 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4505 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4506 if (type == Primitive::kPrimFloat) {
4507 if (isR6) {
4508 switch (cond) {
4509 case kCondEQ:
4510 __ CmpEqS(FTMP, lhs, rhs);
4511 __ Mfc1(dst, FTMP);
4512 __ Andi(dst, dst, 1);
4513 break;
4514 case kCondNE:
4515 __ CmpEqS(FTMP, lhs, rhs);
4516 __ Mfc1(dst, FTMP);
4517 __ Addiu(dst, dst, 1);
4518 break;
4519 case kCondLT:
4520 if (gt_bias) {
4521 __ CmpLtS(FTMP, lhs, rhs);
4522 } else {
4523 __ CmpUltS(FTMP, lhs, rhs);
4524 }
4525 __ Mfc1(dst, FTMP);
4526 __ Andi(dst, dst, 1);
4527 break;
4528 case kCondLE:
4529 if (gt_bias) {
4530 __ CmpLeS(FTMP, lhs, rhs);
4531 } else {
4532 __ CmpUleS(FTMP, lhs, rhs);
4533 }
4534 __ Mfc1(dst, FTMP);
4535 __ Andi(dst, dst, 1);
4536 break;
4537 case kCondGT:
4538 if (gt_bias) {
4539 __ CmpUltS(FTMP, rhs, lhs);
4540 } else {
4541 __ CmpLtS(FTMP, rhs, lhs);
4542 }
4543 __ Mfc1(dst, FTMP);
4544 __ Andi(dst, dst, 1);
4545 break;
4546 case kCondGE:
4547 if (gt_bias) {
4548 __ CmpUleS(FTMP, rhs, lhs);
4549 } else {
4550 __ CmpLeS(FTMP, rhs, lhs);
4551 }
4552 __ Mfc1(dst, FTMP);
4553 __ Andi(dst, dst, 1);
4554 break;
4555 default:
4556 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4557 UNREACHABLE();
4558 }
4559 } else {
4560 switch (cond) {
4561 case kCondEQ:
4562 __ CeqS(0, lhs, rhs);
4563 __ LoadConst32(dst, 1);
4564 __ Movf(dst, ZERO, 0);
4565 break;
4566 case kCondNE:
4567 __ CeqS(0, lhs, rhs);
4568 __ LoadConst32(dst, 1);
4569 __ Movt(dst, ZERO, 0);
4570 break;
4571 case kCondLT:
4572 if (gt_bias) {
4573 __ ColtS(0, lhs, rhs);
4574 } else {
4575 __ CultS(0, lhs, rhs);
4576 }
4577 __ LoadConst32(dst, 1);
4578 __ Movf(dst, ZERO, 0);
4579 break;
4580 case kCondLE:
4581 if (gt_bias) {
4582 __ ColeS(0, lhs, rhs);
4583 } else {
4584 __ CuleS(0, lhs, rhs);
4585 }
4586 __ LoadConst32(dst, 1);
4587 __ Movf(dst, ZERO, 0);
4588 break;
4589 case kCondGT:
4590 if (gt_bias) {
4591 __ CultS(0, rhs, lhs);
4592 } else {
4593 __ ColtS(0, rhs, lhs);
4594 }
4595 __ LoadConst32(dst, 1);
4596 __ Movf(dst, ZERO, 0);
4597 break;
4598 case kCondGE:
4599 if (gt_bias) {
4600 __ CuleS(0, rhs, lhs);
4601 } else {
4602 __ ColeS(0, rhs, lhs);
4603 }
4604 __ LoadConst32(dst, 1);
4605 __ Movf(dst, ZERO, 0);
4606 break;
4607 default:
4608 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4609 UNREACHABLE();
4610 }
4611 }
4612 } else {
4613 DCHECK_EQ(type, Primitive::kPrimDouble);
4614 if (isR6) {
4615 switch (cond) {
4616 case kCondEQ:
4617 __ CmpEqD(FTMP, lhs, rhs);
4618 __ Mfc1(dst, FTMP);
4619 __ Andi(dst, dst, 1);
4620 break;
4621 case kCondNE:
4622 __ CmpEqD(FTMP, lhs, rhs);
4623 __ Mfc1(dst, FTMP);
4624 __ Addiu(dst, dst, 1);
4625 break;
4626 case kCondLT:
4627 if (gt_bias) {
4628 __ CmpLtD(FTMP, lhs, rhs);
4629 } else {
4630 __ CmpUltD(FTMP, lhs, rhs);
4631 }
4632 __ Mfc1(dst, FTMP);
4633 __ Andi(dst, dst, 1);
4634 break;
4635 case kCondLE:
4636 if (gt_bias) {
4637 __ CmpLeD(FTMP, lhs, rhs);
4638 } else {
4639 __ CmpUleD(FTMP, lhs, rhs);
4640 }
4641 __ Mfc1(dst, FTMP);
4642 __ Andi(dst, dst, 1);
4643 break;
4644 case kCondGT:
4645 if (gt_bias) {
4646 __ CmpUltD(FTMP, rhs, lhs);
4647 } else {
4648 __ CmpLtD(FTMP, rhs, lhs);
4649 }
4650 __ Mfc1(dst, FTMP);
4651 __ Andi(dst, dst, 1);
4652 break;
4653 case kCondGE:
4654 if (gt_bias) {
4655 __ CmpUleD(FTMP, rhs, lhs);
4656 } else {
4657 __ CmpLeD(FTMP, rhs, lhs);
4658 }
4659 __ Mfc1(dst, FTMP);
4660 __ Andi(dst, dst, 1);
4661 break;
4662 default:
4663 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4664 UNREACHABLE();
4665 }
4666 } else {
4667 switch (cond) {
4668 case kCondEQ:
4669 __ CeqD(0, lhs, rhs);
4670 __ LoadConst32(dst, 1);
4671 __ Movf(dst, ZERO, 0);
4672 break;
4673 case kCondNE:
4674 __ CeqD(0, lhs, rhs);
4675 __ LoadConst32(dst, 1);
4676 __ Movt(dst, ZERO, 0);
4677 break;
4678 case kCondLT:
4679 if (gt_bias) {
4680 __ ColtD(0, lhs, rhs);
4681 } else {
4682 __ CultD(0, lhs, rhs);
4683 }
4684 __ LoadConst32(dst, 1);
4685 __ Movf(dst, ZERO, 0);
4686 break;
4687 case kCondLE:
4688 if (gt_bias) {
4689 __ ColeD(0, lhs, rhs);
4690 } else {
4691 __ CuleD(0, lhs, rhs);
4692 }
4693 __ LoadConst32(dst, 1);
4694 __ Movf(dst, ZERO, 0);
4695 break;
4696 case kCondGT:
4697 if (gt_bias) {
4698 __ CultD(0, rhs, lhs);
4699 } else {
4700 __ ColtD(0, rhs, lhs);
4701 }
4702 __ LoadConst32(dst, 1);
4703 __ Movf(dst, ZERO, 0);
4704 break;
4705 case kCondGE:
4706 if (gt_bias) {
4707 __ CuleD(0, rhs, lhs);
4708 } else {
4709 __ ColeD(0, rhs, lhs);
4710 }
4711 __ LoadConst32(dst, 1);
4712 __ Movf(dst, ZERO, 0);
4713 break;
4714 default:
4715 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4716 UNREACHABLE();
4717 }
4718 }
4719 }
4720}
4721
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004722bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4723 bool gt_bias,
4724 Primitive::Type type,
4725 LocationSummary* input_locations,
4726 int cc) {
4727 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4728 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4729 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4730 if (type == Primitive::kPrimFloat) {
4731 switch (cond) {
4732 case kCondEQ:
4733 __ CeqS(cc, lhs, rhs);
4734 return false;
4735 case kCondNE:
4736 __ CeqS(cc, lhs, rhs);
4737 return true;
4738 case kCondLT:
4739 if (gt_bias) {
4740 __ ColtS(cc, lhs, rhs);
4741 } else {
4742 __ CultS(cc, lhs, rhs);
4743 }
4744 return false;
4745 case kCondLE:
4746 if (gt_bias) {
4747 __ ColeS(cc, lhs, rhs);
4748 } else {
4749 __ CuleS(cc, lhs, rhs);
4750 }
4751 return false;
4752 case kCondGT:
4753 if (gt_bias) {
4754 __ CultS(cc, rhs, lhs);
4755 } else {
4756 __ ColtS(cc, rhs, lhs);
4757 }
4758 return false;
4759 case kCondGE:
4760 if (gt_bias) {
4761 __ CuleS(cc, rhs, lhs);
4762 } else {
4763 __ ColeS(cc, rhs, lhs);
4764 }
4765 return false;
4766 default:
4767 LOG(FATAL) << "Unexpected non-floating-point condition";
4768 UNREACHABLE();
4769 }
4770 } else {
4771 DCHECK_EQ(type, Primitive::kPrimDouble);
4772 switch (cond) {
4773 case kCondEQ:
4774 __ CeqD(cc, lhs, rhs);
4775 return false;
4776 case kCondNE:
4777 __ CeqD(cc, lhs, rhs);
4778 return true;
4779 case kCondLT:
4780 if (gt_bias) {
4781 __ ColtD(cc, lhs, rhs);
4782 } else {
4783 __ CultD(cc, lhs, rhs);
4784 }
4785 return false;
4786 case kCondLE:
4787 if (gt_bias) {
4788 __ ColeD(cc, lhs, rhs);
4789 } else {
4790 __ CuleD(cc, lhs, rhs);
4791 }
4792 return false;
4793 case kCondGT:
4794 if (gt_bias) {
4795 __ CultD(cc, rhs, lhs);
4796 } else {
4797 __ ColtD(cc, rhs, lhs);
4798 }
4799 return false;
4800 case kCondGE:
4801 if (gt_bias) {
4802 __ CuleD(cc, rhs, lhs);
4803 } else {
4804 __ ColeD(cc, rhs, lhs);
4805 }
4806 return false;
4807 default:
4808 LOG(FATAL) << "Unexpected non-floating-point condition";
4809 UNREACHABLE();
4810 }
4811 }
4812}
4813
4814bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4815 bool gt_bias,
4816 Primitive::Type type,
4817 LocationSummary* input_locations,
4818 FRegister dst) {
4819 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4820 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4821 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4822 if (type == Primitive::kPrimFloat) {
4823 switch (cond) {
4824 case kCondEQ:
4825 __ CmpEqS(dst, lhs, rhs);
4826 return false;
4827 case kCondNE:
4828 __ CmpEqS(dst, lhs, rhs);
4829 return true;
4830 case kCondLT:
4831 if (gt_bias) {
4832 __ CmpLtS(dst, lhs, rhs);
4833 } else {
4834 __ CmpUltS(dst, lhs, rhs);
4835 }
4836 return false;
4837 case kCondLE:
4838 if (gt_bias) {
4839 __ CmpLeS(dst, lhs, rhs);
4840 } else {
4841 __ CmpUleS(dst, lhs, rhs);
4842 }
4843 return false;
4844 case kCondGT:
4845 if (gt_bias) {
4846 __ CmpUltS(dst, rhs, lhs);
4847 } else {
4848 __ CmpLtS(dst, rhs, lhs);
4849 }
4850 return false;
4851 case kCondGE:
4852 if (gt_bias) {
4853 __ CmpUleS(dst, rhs, lhs);
4854 } else {
4855 __ CmpLeS(dst, rhs, lhs);
4856 }
4857 return false;
4858 default:
4859 LOG(FATAL) << "Unexpected non-floating-point condition";
4860 UNREACHABLE();
4861 }
4862 } else {
4863 DCHECK_EQ(type, Primitive::kPrimDouble);
4864 switch (cond) {
4865 case kCondEQ:
4866 __ CmpEqD(dst, lhs, rhs);
4867 return false;
4868 case kCondNE:
4869 __ CmpEqD(dst, lhs, rhs);
4870 return true;
4871 case kCondLT:
4872 if (gt_bias) {
4873 __ CmpLtD(dst, lhs, rhs);
4874 } else {
4875 __ CmpUltD(dst, lhs, rhs);
4876 }
4877 return false;
4878 case kCondLE:
4879 if (gt_bias) {
4880 __ CmpLeD(dst, lhs, rhs);
4881 } else {
4882 __ CmpUleD(dst, lhs, rhs);
4883 }
4884 return false;
4885 case kCondGT:
4886 if (gt_bias) {
4887 __ CmpUltD(dst, rhs, lhs);
4888 } else {
4889 __ CmpLtD(dst, rhs, lhs);
4890 }
4891 return false;
4892 case kCondGE:
4893 if (gt_bias) {
4894 __ CmpUleD(dst, rhs, lhs);
4895 } else {
4896 __ CmpLeD(dst, rhs, lhs);
4897 }
4898 return false;
4899 default:
4900 LOG(FATAL) << "Unexpected non-floating-point condition";
4901 UNREACHABLE();
4902 }
4903 }
4904}
4905
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004906void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
4907 bool gt_bias,
4908 Primitive::Type type,
4909 LocationSummary* locations,
4910 MipsLabel* label) {
4911 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4912 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4913 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4914 if (type == Primitive::kPrimFloat) {
4915 if (isR6) {
4916 switch (cond) {
4917 case kCondEQ:
4918 __ CmpEqS(FTMP, lhs, rhs);
4919 __ Bc1nez(FTMP, label);
4920 break;
4921 case kCondNE:
4922 __ CmpEqS(FTMP, lhs, rhs);
4923 __ Bc1eqz(FTMP, label);
4924 break;
4925 case kCondLT:
4926 if (gt_bias) {
4927 __ CmpLtS(FTMP, lhs, rhs);
4928 } else {
4929 __ CmpUltS(FTMP, lhs, rhs);
4930 }
4931 __ Bc1nez(FTMP, label);
4932 break;
4933 case kCondLE:
4934 if (gt_bias) {
4935 __ CmpLeS(FTMP, lhs, rhs);
4936 } else {
4937 __ CmpUleS(FTMP, lhs, rhs);
4938 }
4939 __ Bc1nez(FTMP, label);
4940 break;
4941 case kCondGT:
4942 if (gt_bias) {
4943 __ CmpUltS(FTMP, rhs, lhs);
4944 } else {
4945 __ CmpLtS(FTMP, rhs, lhs);
4946 }
4947 __ Bc1nez(FTMP, label);
4948 break;
4949 case kCondGE:
4950 if (gt_bias) {
4951 __ CmpUleS(FTMP, rhs, lhs);
4952 } else {
4953 __ CmpLeS(FTMP, rhs, lhs);
4954 }
4955 __ Bc1nez(FTMP, label);
4956 break;
4957 default:
4958 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004959 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004960 }
4961 } else {
4962 switch (cond) {
4963 case kCondEQ:
4964 __ CeqS(0, lhs, rhs);
4965 __ Bc1t(0, label);
4966 break;
4967 case kCondNE:
4968 __ CeqS(0, lhs, rhs);
4969 __ Bc1f(0, label);
4970 break;
4971 case kCondLT:
4972 if (gt_bias) {
4973 __ ColtS(0, lhs, rhs);
4974 } else {
4975 __ CultS(0, lhs, rhs);
4976 }
4977 __ Bc1t(0, label);
4978 break;
4979 case kCondLE:
4980 if (gt_bias) {
4981 __ ColeS(0, lhs, rhs);
4982 } else {
4983 __ CuleS(0, lhs, rhs);
4984 }
4985 __ Bc1t(0, label);
4986 break;
4987 case kCondGT:
4988 if (gt_bias) {
4989 __ CultS(0, rhs, lhs);
4990 } else {
4991 __ ColtS(0, rhs, lhs);
4992 }
4993 __ Bc1t(0, label);
4994 break;
4995 case kCondGE:
4996 if (gt_bias) {
4997 __ CuleS(0, rhs, lhs);
4998 } else {
4999 __ ColeS(0, rhs, lhs);
5000 }
5001 __ Bc1t(0, label);
5002 break;
5003 default:
5004 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005005 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005006 }
5007 }
5008 } else {
5009 DCHECK_EQ(type, Primitive::kPrimDouble);
5010 if (isR6) {
5011 switch (cond) {
5012 case kCondEQ:
5013 __ CmpEqD(FTMP, lhs, rhs);
5014 __ Bc1nez(FTMP, label);
5015 break;
5016 case kCondNE:
5017 __ CmpEqD(FTMP, lhs, rhs);
5018 __ Bc1eqz(FTMP, label);
5019 break;
5020 case kCondLT:
5021 if (gt_bias) {
5022 __ CmpLtD(FTMP, lhs, rhs);
5023 } else {
5024 __ CmpUltD(FTMP, lhs, rhs);
5025 }
5026 __ Bc1nez(FTMP, label);
5027 break;
5028 case kCondLE:
5029 if (gt_bias) {
5030 __ CmpLeD(FTMP, lhs, rhs);
5031 } else {
5032 __ CmpUleD(FTMP, lhs, rhs);
5033 }
5034 __ Bc1nez(FTMP, label);
5035 break;
5036 case kCondGT:
5037 if (gt_bias) {
5038 __ CmpUltD(FTMP, rhs, lhs);
5039 } else {
5040 __ CmpLtD(FTMP, rhs, lhs);
5041 }
5042 __ Bc1nez(FTMP, label);
5043 break;
5044 case kCondGE:
5045 if (gt_bias) {
5046 __ CmpUleD(FTMP, rhs, lhs);
5047 } else {
5048 __ CmpLeD(FTMP, rhs, lhs);
5049 }
5050 __ Bc1nez(FTMP, label);
5051 break;
5052 default:
5053 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005054 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005055 }
5056 } else {
5057 switch (cond) {
5058 case kCondEQ:
5059 __ CeqD(0, lhs, rhs);
5060 __ Bc1t(0, label);
5061 break;
5062 case kCondNE:
5063 __ CeqD(0, lhs, rhs);
5064 __ Bc1f(0, label);
5065 break;
5066 case kCondLT:
5067 if (gt_bias) {
5068 __ ColtD(0, lhs, rhs);
5069 } else {
5070 __ CultD(0, lhs, rhs);
5071 }
5072 __ Bc1t(0, label);
5073 break;
5074 case kCondLE:
5075 if (gt_bias) {
5076 __ ColeD(0, lhs, rhs);
5077 } else {
5078 __ CuleD(0, lhs, rhs);
5079 }
5080 __ Bc1t(0, label);
5081 break;
5082 case kCondGT:
5083 if (gt_bias) {
5084 __ CultD(0, rhs, lhs);
5085 } else {
5086 __ ColtD(0, rhs, lhs);
5087 }
5088 __ Bc1t(0, label);
5089 break;
5090 case kCondGE:
5091 if (gt_bias) {
5092 __ CuleD(0, rhs, lhs);
5093 } else {
5094 __ ColeD(0, rhs, lhs);
5095 }
5096 __ Bc1t(0, label);
5097 break;
5098 default:
5099 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005100 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005101 }
5102 }
5103 }
5104}
5105
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005106void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005107 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005108 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005109 MipsLabel* false_target) {
5110 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005111
David Brazdil0debae72015-11-12 18:37:00 +00005112 if (true_target == nullptr && false_target == nullptr) {
5113 // Nothing to do. The code always falls through.
5114 return;
5115 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005116 // Constant condition, statically compared against "true" (integer value 1).
5117 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005118 if (true_target != nullptr) {
5119 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005120 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005121 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005122 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005123 if (false_target != nullptr) {
5124 __ B(false_target);
5125 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126 }
David Brazdil0debae72015-11-12 18:37:00 +00005127 return;
5128 }
5129
5130 // The following code generates these patterns:
5131 // (1) true_target == nullptr && false_target != nullptr
5132 // - opposite condition true => branch to false_target
5133 // (2) true_target != nullptr && false_target == nullptr
5134 // - condition true => branch to true_target
5135 // (3) true_target != nullptr && false_target != nullptr
5136 // - condition true => branch to true_target
5137 // - branch to false_target
5138 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005139 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005140 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005141 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005142 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005143 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5144 } else {
5145 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5146 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005147 } else {
5148 // The condition instruction has not been materialized, use its inputs as
5149 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005150 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005151 Primitive::Type type = condition->InputAt(0)->GetType();
5152 LocationSummary* locations = cond->GetLocations();
5153 IfCondition if_cond = condition->GetCondition();
5154 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005155
David Brazdil0debae72015-11-12 18:37:00 +00005156 if (true_target == nullptr) {
5157 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005158 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005159 }
5160
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005161 switch (type) {
5162 default:
5163 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5164 break;
5165 case Primitive::kPrimLong:
5166 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5167 break;
5168 case Primitive::kPrimFloat:
5169 case Primitive::kPrimDouble:
5170 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5171 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005172 }
5173 }
David Brazdil0debae72015-11-12 18:37:00 +00005174
5175 // If neither branch falls through (case 3), the conditional branch to `true_target`
5176 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5177 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005178 __ B(false_target);
5179 }
5180}
5181
5182void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5183 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005184 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005185 locations->SetInAt(0, Location::RequiresRegister());
5186 }
5187}
5188
5189void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005190 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5191 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5192 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5193 nullptr : codegen_->GetLabelOf(true_successor);
5194 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5195 nullptr : codegen_->GetLabelOf(false_successor);
5196 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005197}
5198
5199void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5200 LocationSummary* locations = new (GetGraph()->GetArena())
5201 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005202 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00005203 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005204 locations->SetInAt(0, Location::RequiresRegister());
5205 }
5206}
5207
5208void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005209 SlowPathCodeMIPS* slow_path =
5210 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005211 GenerateTestAndBranch(deoptimize,
5212 /* condition_input_index */ 0,
5213 slow_path->GetEntryLabel(),
5214 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005215}
5216
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005217// This function returns true if a conditional move can be generated for HSelect.
5218// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5219// branches and regular moves.
5220//
5221// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5222//
5223// While determining feasibility of a conditional move and setting inputs/outputs
5224// are two distinct tasks, this function does both because they share quite a bit
5225// of common logic.
5226static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5227 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5228 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5229 HCondition* condition = cond->AsCondition();
5230
5231 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5232 Primitive::Type dst_type = select->GetType();
5233
5234 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5235 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5236 bool is_true_value_zero_constant =
5237 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5238 bool is_false_value_zero_constant =
5239 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5240
5241 bool can_move_conditionally = false;
5242 bool use_const_for_false_in = false;
5243 bool use_const_for_true_in = false;
5244
5245 if (!cond->IsConstant()) {
5246 switch (cond_type) {
5247 default:
5248 switch (dst_type) {
5249 default:
5250 // Moving int on int condition.
5251 if (is_r6) {
5252 if (is_true_value_zero_constant) {
5253 // seleqz out_reg, false_reg, cond_reg
5254 can_move_conditionally = true;
5255 use_const_for_true_in = true;
5256 } else if (is_false_value_zero_constant) {
5257 // selnez out_reg, true_reg, cond_reg
5258 can_move_conditionally = true;
5259 use_const_for_false_in = true;
5260 } else if (materialized) {
5261 // Not materializing unmaterialized int conditions
5262 // to keep the instruction count low.
5263 // selnez AT, true_reg, cond_reg
5264 // seleqz TMP, false_reg, cond_reg
5265 // or out_reg, AT, TMP
5266 can_move_conditionally = true;
5267 }
5268 } else {
5269 // movn out_reg, true_reg/ZERO, cond_reg
5270 can_move_conditionally = true;
5271 use_const_for_true_in = is_true_value_zero_constant;
5272 }
5273 break;
5274 case Primitive::kPrimLong:
5275 // Moving long on int condition.
5276 if (is_r6) {
5277 if (is_true_value_zero_constant) {
5278 // seleqz out_reg_lo, false_reg_lo, cond_reg
5279 // seleqz out_reg_hi, false_reg_hi, cond_reg
5280 can_move_conditionally = true;
5281 use_const_for_true_in = true;
5282 } else if (is_false_value_zero_constant) {
5283 // selnez out_reg_lo, true_reg_lo, cond_reg
5284 // selnez out_reg_hi, true_reg_hi, cond_reg
5285 can_move_conditionally = true;
5286 use_const_for_false_in = true;
5287 }
5288 // Other long conditional moves would generate 6+ instructions,
5289 // which is too many.
5290 } else {
5291 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5292 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5293 can_move_conditionally = true;
5294 use_const_for_true_in = is_true_value_zero_constant;
5295 }
5296 break;
5297 case Primitive::kPrimFloat:
5298 case Primitive::kPrimDouble:
5299 // Moving float/double on int condition.
5300 if (is_r6) {
5301 if (materialized) {
5302 // Not materializing unmaterialized int conditions
5303 // to keep the instruction count low.
5304 can_move_conditionally = true;
5305 if (is_true_value_zero_constant) {
5306 // sltu TMP, ZERO, cond_reg
5307 // mtc1 TMP, temp_cond_reg
5308 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5309 use_const_for_true_in = true;
5310 } else if (is_false_value_zero_constant) {
5311 // sltu TMP, ZERO, cond_reg
5312 // mtc1 TMP, temp_cond_reg
5313 // selnez.fmt out_reg, true_reg, temp_cond_reg
5314 use_const_for_false_in = true;
5315 } else {
5316 // sltu TMP, ZERO, cond_reg
5317 // mtc1 TMP, temp_cond_reg
5318 // sel.fmt temp_cond_reg, false_reg, true_reg
5319 // mov.fmt out_reg, temp_cond_reg
5320 }
5321 }
5322 } else {
5323 // movn.fmt out_reg, true_reg, cond_reg
5324 can_move_conditionally = true;
5325 }
5326 break;
5327 }
5328 break;
5329 case Primitive::kPrimLong:
5330 // We don't materialize long comparison now
5331 // and use conditional branches instead.
5332 break;
5333 case Primitive::kPrimFloat:
5334 case Primitive::kPrimDouble:
5335 switch (dst_type) {
5336 default:
5337 // Moving int on float/double condition.
5338 if (is_r6) {
5339 if (is_true_value_zero_constant) {
5340 // mfc1 TMP, temp_cond_reg
5341 // seleqz out_reg, false_reg, TMP
5342 can_move_conditionally = true;
5343 use_const_for_true_in = true;
5344 } else if (is_false_value_zero_constant) {
5345 // mfc1 TMP, temp_cond_reg
5346 // selnez out_reg, true_reg, TMP
5347 can_move_conditionally = true;
5348 use_const_for_false_in = true;
5349 } else {
5350 // mfc1 TMP, temp_cond_reg
5351 // selnez AT, true_reg, TMP
5352 // seleqz TMP, false_reg, TMP
5353 // or out_reg, AT, TMP
5354 can_move_conditionally = true;
5355 }
5356 } else {
5357 // movt out_reg, true_reg/ZERO, cc
5358 can_move_conditionally = true;
5359 use_const_for_true_in = is_true_value_zero_constant;
5360 }
5361 break;
5362 case Primitive::kPrimLong:
5363 // Moving long on float/double condition.
5364 if (is_r6) {
5365 if (is_true_value_zero_constant) {
5366 // mfc1 TMP, temp_cond_reg
5367 // seleqz out_reg_lo, false_reg_lo, TMP
5368 // seleqz out_reg_hi, false_reg_hi, TMP
5369 can_move_conditionally = true;
5370 use_const_for_true_in = true;
5371 } else if (is_false_value_zero_constant) {
5372 // mfc1 TMP, temp_cond_reg
5373 // selnez out_reg_lo, true_reg_lo, TMP
5374 // selnez out_reg_hi, true_reg_hi, TMP
5375 can_move_conditionally = true;
5376 use_const_for_false_in = true;
5377 }
5378 // Other long conditional moves would generate 6+ instructions,
5379 // which is too many.
5380 } else {
5381 // movt out_reg_lo, true_reg_lo/ZERO, cc
5382 // movt out_reg_hi, true_reg_hi/ZERO, cc
5383 can_move_conditionally = true;
5384 use_const_for_true_in = is_true_value_zero_constant;
5385 }
5386 break;
5387 case Primitive::kPrimFloat:
5388 case Primitive::kPrimDouble:
5389 // Moving float/double on float/double condition.
5390 if (is_r6) {
5391 can_move_conditionally = true;
5392 if (is_true_value_zero_constant) {
5393 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5394 use_const_for_true_in = true;
5395 } else if (is_false_value_zero_constant) {
5396 // selnez.fmt out_reg, true_reg, temp_cond_reg
5397 use_const_for_false_in = true;
5398 } else {
5399 // sel.fmt temp_cond_reg, false_reg, true_reg
5400 // mov.fmt out_reg, temp_cond_reg
5401 }
5402 } else {
5403 // movt.fmt out_reg, true_reg, cc
5404 can_move_conditionally = true;
5405 }
5406 break;
5407 }
5408 break;
5409 }
5410 }
5411
5412 if (can_move_conditionally) {
5413 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5414 } else {
5415 DCHECK(!use_const_for_false_in);
5416 DCHECK(!use_const_for_true_in);
5417 }
5418
5419 if (locations_to_set != nullptr) {
5420 if (use_const_for_false_in) {
5421 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5422 } else {
5423 locations_to_set->SetInAt(0,
5424 Primitive::IsFloatingPointType(dst_type)
5425 ? Location::RequiresFpuRegister()
5426 : Location::RequiresRegister());
5427 }
5428 if (use_const_for_true_in) {
5429 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5430 } else {
5431 locations_to_set->SetInAt(1,
5432 Primitive::IsFloatingPointType(dst_type)
5433 ? Location::RequiresFpuRegister()
5434 : Location::RequiresRegister());
5435 }
5436 if (materialized) {
5437 locations_to_set->SetInAt(2, Location::RequiresRegister());
5438 }
5439 // On R6 we don't require the output to be the same as the
5440 // first input for conditional moves unlike on R2.
5441 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5442 if (is_out_same_as_first_in) {
5443 locations_to_set->SetOut(Location::SameAsFirstInput());
5444 } else {
5445 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5446 ? Location::RequiresFpuRegister()
5447 : Location::RequiresRegister());
5448 }
5449 }
5450
5451 return can_move_conditionally;
5452}
5453
5454void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5455 LocationSummary* locations = select->GetLocations();
5456 Location dst = locations->Out();
5457 Location src = locations->InAt(1);
5458 Register src_reg = ZERO;
5459 Register src_reg_high = ZERO;
5460 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5461 Register cond_reg = TMP;
5462 int cond_cc = 0;
5463 Primitive::Type cond_type = Primitive::kPrimInt;
5464 bool cond_inverted = false;
5465 Primitive::Type dst_type = select->GetType();
5466
5467 if (IsBooleanValueOrMaterializedCondition(cond)) {
5468 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5469 } else {
5470 HCondition* condition = cond->AsCondition();
5471 LocationSummary* cond_locations = cond->GetLocations();
5472 IfCondition if_cond = condition->GetCondition();
5473 cond_type = condition->InputAt(0)->GetType();
5474 switch (cond_type) {
5475 default:
5476 DCHECK_NE(cond_type, Primitive::kPrimLong);
5477 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5478 break;
5479 case Primitive::kPrimFloat:
5480 case Primitive::kPrimDouble:
5481 cond_inverted = MaterializeFpCompareR2(if_cond,
5482 condition->IsGtBias(),
5483 cond_type,
5484 cond_locations,
5485 cond_cc);
5486 break;
5487 }
5488 }
5489
5490 DCHECK(dst.Equals(locations->InAt(0)));
5491 if (src.IsRegister()) {
5492 src_reg = src.AsRegister<Register>();
5493 } else if (src.IsRegisterPair()) {
5494 src_reg = src.AsRegisterPairLow<Register>();
5495 src_reg_high = src.AsRegisterPairHigh<Register>();
5496 } else if (src.IsConstant()) {
5497 DCHECK(src.GetConstant()->IsZeroBitPattern());
5498 }
5499
5500 switch (cond_type) {
5501 default:
5502 switch (dst_type) {
5503 default:
5504 if (cond_inverted) {
5505 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5506 } else {
5507 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5508 }
5509 break;
5510 case Primitive::kPrimLong:
5511 if (cond_inverted) {
5512 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5513 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5514 } else {
5515 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5516 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5517 }
5518 break;
5519 case Primitive::kPrimFloat:
5520 if (cond_inverted) {
5521 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5522 } else {
5523 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5524 }
5525 break;
5526 case Primitive::kPrimDouble:
5527 if (cond_inverted) {
5528 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5529 } else {
5530 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5531 }
5532 break;
5533 }
5534 break;
5535 case Primitive::kPrimLong:
5536 LOG(FATAL) << "Unreachable";
5537 UNREACHABLE();
5538 case Primitive::kPrimFloat:
5539 case Primitive::kPrimDouble:
5540 switch (dst_type) {
5541 default:
5542 if (cond_inverted) {
5543 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5544 } else {
5545 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5546 }
5547 break;
5548 case Primitive::kPrimLong:
5549 if (cond_inverted) {
5550 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5551 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5552 } else {
5553 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5554 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5555 }
5556 break;
5557 case Primitive::kPrimFloat:
5558 if (cond_inverted) {
5559 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5560 } else {
5561 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5562 }
5563 break;
5564 case Primitive::kPrimDouble:
5565 if (cond_inverted) {
5566 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5567 } else {
5568 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5569 }
5570 break;
5571 }
5572 break;
5573 }
5574}
5575
5576void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5577 LocationSummary* locations = select->GetLocations();
5578 Location dst = locations->Out();
5579 Location false_src = locations->InAt(0);
5580 Location true_src = locations->InAt(1);
5581 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5582 Register cond_reg = TMP;
5583 FRegister fcond_reg = FTMP;
5584 Primitive::Type cond_type = Primitive::kPrimInt;
5585 bool cond_inverted = false;
5586 Primitive::Type dst_type = select->GetType();
5587
5588 if (IsBooleanValueOrMaterializedCondition(cond)) {
5589 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5590 } else {
5591 HCondition* condition = cond->AsCondition();
5592 LocationSummary* cond_locations = cond->GetLocations();
5593 IfCondition if_cond = condition->GetCondition();
5594 cond_type = condition->InputAt(0)->GetType();
5595 switch (cond_type) {
5596 default:
5597 DCHECK_NE(cond_type, Primitive::kPrimLong);
5598 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5599 break;
5600 case Primitive::kPrimFloat:
5601 case Primitive::kPrimDouble:
5602 cond_inverted = MaterializeFpCompareR6(if_cond,
5603 condition->IsGtBias(),
5604 cond_type,
5605 cond_locations,
5606 fcond_reg);
5607 break;
5608 }
5609 }
5610
5611 if (true_src.IsConstant()) {
5612 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5613 }
5614 if (false_src.IsConstant()) {
5615 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5616 }
5617
5618 switch (dst_type) {
5619 default:
5620 if (Primitive::IsFloatingPointType(cond_type)) {
5621 __ Mfc1(cond_reg, fcond_reg);
5622 }
5623 if (true_src.IsConstant()) {
5624 if (cond_inverted) {
5625 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5626 } else {
5627 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5628 }
5629 } else if (false_src.IsConstant()) {
5630 if (cond_inverted) {
5631 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5632 } else {
5633 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5634 }
5635 } else {
5636 DCHECK_NE(cond_reg, AT);
5637 if (cond_inverted) {
5638 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5639 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5640 } else {
5641 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5642 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5643 }
5644 __ Or(dst.AsRegister<Register>(), AT, TMP);
5645 }
5646 break;
5647 case Primitive::kPrimLong: {
5648 if (Primitive::IsFloatingPointType(cond_type)) {
5649 __ Mfc1(cond_reg, fcond_reg);
5650 }
5651 Register dst_lo = dst.AsRegisterPairLow<Register>();
5652 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5653 if (true_src.IsConstant()) {
5654 Register src_lo = false_src.AsRegisterPairLow<Register>();
5655 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5656 if (cond_inverted) {
5657 __ Selnez(dst_lo, src_lo, cond_reg);
5658 __ Selnez(dst_hi, src_hi, cond_reg);
5659 } else {
5660 __ Seleqz(dst_lo, src_lo, cond_reg);
5661 __ Seleqz(dst_hi, src_hi, cond_reg);
5662 }
5663 } else {
5664 DCHECK(false_src.IsConstant());
5665 Register src_lo = true_src.AsRegisterPairLow<Register>();
5666 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5667 if (cond_inverted) {
5668 __ Seleqz(dst_lo, src_lo, cond_reg);
5669 __ Seleqz(dst_hi, src_hi, cond_reg);
5670 } else {
5671 __ Selnez(dst_lo, src_lo, cond_reg);
5672 __ Selnez(dst_hi, src_hi, cond_reg);
5673 }
5674 }
5675 break;
5676 }
5677 case Primitive::kPrimFloat: {
5678 if (!Primitive::IsFloatingPointType(cond_type)) {
5679 // sel*.fmt tests bit 0 of the condition register, account for that.
5680 __ Sltu(TMP, ZERO, cond_reg);
5681 __ Mtc1(TMP, fcond_reg);
5682 }
5683 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5684 if (true_src.IsConstant()) {
5685 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5686 if (cond_inverted) {
5687 __ SelnezS(dst_reg, src_reg, fcond_reg);
5688 } else {
5689 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5690 }
5691 } else if (false_src.IsConstant()) {
5692 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5693 if (cond_inverted) {
5694 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5695 } else {
5696 __ SelnezS(dst_reg, src_reg, fcond_reg);
5697 }
5698 } else {
5699 if (cond_inverted) {
5700 __ SelS(fcond_reg,
5701 true_src.AsFpuRegister<FRegister>(),
5702 false_src.AsFpuRegister<FRegister>());
5703 } else {
5704 __ SelS(fcond_reg,
5705 false_src.AsFpuRegister<FRegister>(),
5706 true_src.AsFpuRegister<FRegister>());
5707 }
5708 __ MovS(dst_reg, fcond_reg);
5709 }
5710 break;
5711 }
5712 case Primitive::kPrimDouble: {
5713 if (!Primitive::IsFloatingPointType(cond_type)) {
5714 // sel*.fmt tests bit 0 of the condition register, account for that.
5715 __ Sltu(TMP, ZERO, cond_reg);
5716 __ Mtc1(TMP, fcond_reg);
5717 }
5718 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5719 if (true_src.IsConstant()) {
5720 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5721 if (cond_inverted) {
5722 __ SelnezD(dst_reg, src_reg, fcond_reg);
5723 } else {
5724 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5725 }
5726 } else if (false_src.IsConstant()) {
5727 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5728 if (cond_inverted) {
5729 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5730 } else {
5731 __ SelnezD(dst_reg, src_reg, fcond_reg);
5732 }
5733 } else {
5734 if (cond_inverted) {
5735 __ SelD(fcond_reg,
5736 true_src.AsFpuRegister<FRegister>(),
5737 false_src.AsFpuRegister<FRegister>());
5738 } else {
5739 __ SelD(fcond_reg,
5740 false_src.AsFpuRegister<FRegister>(),
5741 true_src.AsFpuRegister<FRegister>());
5742 }
5743 __ MovD(dst_reg, fcond_reg);
5744 }
5745 break;
5746 }
5747 }
5748}
5749
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005750void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5751 LocationSummary* locations = new (GetGraph()->GetArena())
5752 LocationSummary(flag, LocationSummary::kNoCall);
5753 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005754}
5755
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005756void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5757 __ LoadFromOffset(kLoadWord,
5758 flag->GetLocations()->Out().AsRegister<Register>(),
5759 SP,
5760 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005761}
5762
David Brazdil74eb1b22015-12-14 11:44:01 +00005763void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5764 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005765 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005766}
5767
5768void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005769 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5770 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5771 if (is_r6) {
5772 GenConditionalMoveR6(select);
5773 } else {
5774 GenConditionalMoveR2(select);
5775 }
5776 } else {
5777 LocationSummary* locations = select->GetLocations();
5778 MipsLabel false_target;
5779 GenerateTestAndBranch(select,
5780 /* condition_input_index */ 2,
5781 /* true_target */ nullptr,
5782 &false_target);
5783 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5784 __ Bind(&false_target);
5785 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005786}
5787
David Srbecky0cf44932015-12-09 14:09:59 +00005788void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5789 new (GetGraph()->GetArena()) LocationSummary(info);
5790}
5791
David Srbeckyd28f4a02016-03-14 17:14:24 +00005792void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5793 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005794}
5795
5796void CodeGeneratorMIPS::GenerateNop() {
5797 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005798}
5799
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005800void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5801 Primitive::Type field_type = field_info.GetFieldType();
5802 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5803 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08005804 bool object_field_get_with_read_barrier =
5805 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08005807 instruction,
5808 generate_volatile
5809 ? LocationSummary::kCallOnMainOnly
5810 : (object_field_get_with_read_barrier
5811 ? LocationSummary::kCallOnSlowPath
5812 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005813
Alexey Frunzec61c0762017-04-10 13:54:23 -07005814 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5815 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5816 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005817 locations->SetInAt(0, Location::RequiresRegister());
5818 if (generate_volatile) {
5819 InvokeRuntimeCallingConvention calling_convention;
5820 // need A0 to hold base + offset
5821 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5822 if (field_type == Primitive::kPrimLong) {
5823 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
5824 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005825 // Use Location::Any() to prevent situations when running out of available fp registers.
5826 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005827 // Need some temp core regs since FP results are returned in core registers
5828 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
5829 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
5830 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
5831 }
5832 } else {
5833 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5834 locations->SetOut(Location::RequiresFpuRegister());
5835 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005836 // The output overlaps in the case of an object field get with
5837 // read barriers enabled: we do not want the move to overwrite the
5838 // object's location, as we need it to emit the read barrier.
5839 locations->SetOut(Location::RequiresRegister(),
5840 object_field_get_with_read_barrier
5841 ? Location::kOutputOverlap
5842 : Location::kNoOutputOverlap);
5843 }
5844 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5845 // We need a temporary register for the read barrier marking slow
5846 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
5847 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005848 }
5849 }
5850}
5851
5852void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
5853 const FieldInfo& field_info,
5854 uint32_t dex_pc) {
5855 Primitive::Type type = field_info.GetFieldType();
5856 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005857 Location obj_loc = locations->InAt(0);
5858 Register obj = obj_loc.AsRegister<Register>();
5859 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005860 LoadOperandType load_type = kLoadUnsignedByte;
5861 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005862 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01005863 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005864
5865 switch (type) {
5866 case Primitive::kPrimBoolean:
5867 load_type = kLoadUnsignedByte;
5868 break;
5869 case Primitive::kPrimByte:
5870 load_type = kLoadSignedByte;
5871 break;
5872 case Primitive::kPrimShort:
5873 load_type = kLoadSignedHalfword;
5874 break;
5875 case Primitive::kPrimChar:
5876 load_type = kLoadUnsignedHalfword;
5877 break;
5878 case Primitive::kPrimInt:
5879 case Primitive::kPrimFloat:
5880 case Primitive::kPrimNot:
5881 load_type = kLoadWord;
5882 break;
5883 case Primitive::kPrimLong:
5884 case Primitive::kPrimDouble:
5885 load_type = kLoadDoubleword;
5886 break;
5887 case Primitive::kPrimVoid:
5888 LOG(FATAL) << "Unreachable type " << type;
5889 UNREACHABLE();
5890 }
5891
5892 if (is_volatile && load_type == kLoadDoubleword) {
5893 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01005894 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005895 // Do implicit Null check
5896 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
5897 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01005898 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005899 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
5900 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005901 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08005902 if (dst_loc.IsFpuRegister()) {
5903 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005904 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08005905 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005906 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005907 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005908 __ StoreToOffset(kStoreWord,
5909 locations->GetTemp(1).AsRegister<Register>(),
5910 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005911 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005912 __ StoreToOffset(kStoreWord,
5913 locations->GetTemp(2).AsRegister<Register>(),
5914 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08005915 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005916 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005917 }
5918 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005919 if (type == Primitive::kPrimNot) {
5920 // /* HeapReference<Object> */ dst = *(obj + offset)
5921 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5922 Location temp_loc = locations->GetTemp(0);
5923 // Note that a potential implicit null check is handled in this
5924 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
5925 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5926 dst_loc,
5927 obj,
5928 offset,
5929 temp_loc,
5930 /* needs_null_check */ true);
5931 if (is_volatile) {
5932 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5933 }
5934 } else {
5935 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
5936 if (is_volatile) {
5937 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5938 }
5939 // If read barriers are enabled, emit read barriers other than
5940 // Baker's using a slow path (and also unpoison the loaded
5941 // reference, if heap poisoning is enabled).
5942 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
5943 }
5944 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005945 Register dst;
5946 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08005947 DCHECK(dst_loc.IsRegisterPair());
5948 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005949 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005950 DCHECK(dst_loc.IsRegister());
5951 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005952 }
Alexey Frunze2923db72016-08-20 01:55:47 -07005953 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005954 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08005955 DCHECK(dst_loc.IsFpuRegister());
5956 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005957 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07005958 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005959 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07005960 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005961 }
5962 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005963 }
5964
Alexey Frunze15958152017-02-09 19:08:30 -08005965 // Memory barriers, in the case of references, are handled in the
5966 // previous switch statement.
5967 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005968 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5969 }
5970}
5971
5972void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5973 Primitive::Type field_type = field_info.GetFieldType();
5974 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5975 bool generate_volatile = field_info.IsVolatile() && is_wide;
5976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005977 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005978
5979 locations->SetInAt(0, Location::RequiresRegister());
5980 if (generate_volatile) {
5981 InvokeRuntimeCallingConvention calling_convention;
5982 // need A0 to hold base + offset
5983 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5984 if (field_type == Primitive::kPrimLong) {
5985 locations->SetInAt(1, Location::RegisterPairLocation(
5986 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5987 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02005988 // Use Location::Any() to prevent situations when running out of available fp registers.
5989 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005990 // Pass FP parameters in core registers.
5991 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5992 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
5993 }
5994 } else {
5995 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005996 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005997 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07005998 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005999 }
6000 }
6001}
6002
6003void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6004 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006005 uint32_t dex_pc,
6006 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006007 Primitive::Type type = field_info.GetFieldType();
6008 LocationSummary* locations = instruction->GetLocations();
6009 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006010 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006011 StoreOperandType store_type = kStoreByte;
6012 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006013 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006014 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006015 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006016
6017 switch (type) {
6018 case Primitive::kPrimBoolean:
6019 case Primitive::kPrimByte:
6020 store_type = kStoreByte;
6021 break;
6022 case Primitive::kPrimShort:
6023 case Primitive::kPrimChar:
6024 store_type = kStoreHalfword;
6025 break;
6026 case Primitive::kPrimInt:
6027 case Primitive::kPrimFloat:
6028 case Primitive::kPrimNot:
6029 store_type = kStoreWord;
6030 break;
6031 case Primitive::kPrimLong:
6032 case Primitive::kPrimDouble:
6033 store_type = kStoreDoubleword;
6034 break;
6035 case Primitive::kPrimVoid:
6036 LOG(FATAL) << "Unreachable type " << type;
6037 UNREACHABLE();
6038 }
6039
6040 if (is_volatile) {
6041 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6042 }
6043
6044 if (is_volatile && store_type == kStoreDoubleword) {
6045 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006046 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006047 // Do implicit Null check.
6048 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6049 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6050 if (type == Primitive::kPrimDouble) {
6051 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006052 if (value_location.IsFpuRegister()) {
6053 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6054 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006055 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006056 value_location.AsFpuRegister<FRegister>());
6057 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006058 __ LoadFromOffset(kLoadWord,
6059 locations->GetTemp(1).AsRegister<Register>(),
6060 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006061 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006062 __ LoadFromOffset(kLoadWord,
6063 locations->GetTemp(2).AsRegister<Register>(),
6064 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006065 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006066 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006067 DCHECK(value_location.IsConstant());
6068 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6069 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006070 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6071 locations->GetTemp(1).AsRegister<Register>(),
6072 value);
6073 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006074 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006075 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006076 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6077 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006078 if (value_location.IsConstant()) {
6079 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6080 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6081 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006082 Register src;
6083 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006084 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006085 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006086 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006087 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006088 if (kPoisonHeapReferences && needs_write_barrier) {
6089 // Note that in the case where `value` is a null reference,
6090 // we do not enter this block, as a null reference does not
6091 // need poisoning.
6092 DCHECK_EQ(type, Primitive::kPrimNot);
6093 __ PoisonHeapReference(TMP, src);
6094 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6095 } else {
6096 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6097 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006098 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006099 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006100 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006101 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006102 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006103 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006104 }
6105 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006106 }
6107
Alexey Frunzec061de12017-02-14 13:27:23 -08006108 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006109 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006110 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006111 }
6112
6113 if (is_volatile) {
6114 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6115 }
6116}
6117
6118void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6119 HandleFieldGet(instruction, instruction->GetFieldInfo());
6120}
6121
6122void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6123 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6124}
6125
6126void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6127 HandleFieldSet(instruction, instruction->GetFieldInfo());
6128}
6129
6130void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006131 HandleFieldSet(instruction,
6132 instruction->GetFieldInfo(),
6133 instruction->GetDexPc(),
6134 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006135}
6136
Alexey Frunze15958152017-02-09 19:08:30 -08006137void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6138 HInstruction* instruction,
6139 Location out,
6140 uint32_t offset,
6141 Location maybe_temp,
6142 ReadBarrierOption read_barrier_option) {
6143 Register out_reg = out.AsRegister<Register>();
6144 if (read_barrier_option == kWithReadBarrier) {
6145 CHECK(kEmitCompilerReadBarrier);
6146 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6147 if (kUseBakerReadBarrier) {
6148 // Load with fast path based Baker's read barrier.
6149 // /* HeapReference<Object> */ out = *(out + offset)
6150 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6151 out,
6152 out_reg,
6153 offset,
6154 maybe_temp,
6155 /* needs_null_check */ false);
6156 } else {
6157 // Load with slow path based read barrier.
6158 // Save the value of `out` into `maybe_temp` before overwriting it
6159 // in the following move operation, as we will need it for the
6160 // read barrier below.
6161 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6162 // /* HeapReference<Object> */ out = *(out + offset)
6163 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6164 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6165 }
6166 } else {
6167 // Plain load with no read barrier.
6168 // /* HeapReference<Object> */ out = *(out + offset)
6169 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6170 __ MaybeUnpoisonHeapReference(out_reg);
6171 }
6172}
6173
6174void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6175 HInstruction* instruction,
6176 Location out,
6177 Location obj,
6178 uint32_t offset,
6179 Location maybe_temp,
6180 ReadBarrierOption read_barrier_option) {
6181 Register out_reg = out.AsRegister<Register>();
6182 Register obj_reg = obj.AsRegister<Register>();
6183 if (read_barrier_option == kWithReadBarrier) {
6184 CHECK(kEmitCompilerReadBarrier);
6185 if (kUseBakerReadBarrier) {
6186 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6187 // Load with fast path based Baker's read barrier.
6188 // /* HeapReference<Object> */ out = *(obj + offset)
6189 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6190 out,
6191 obj_reg,
6192 offset,
6193 maybe_temp,
6194 /* needs_null_check */ false);
6195 } else {
6196 // Load with slow path based read barrier.
6197 // /* HeapReference<Object> */ out = *(obj + offset)
6198 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6199 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6200 }
6201 } else {
6202 // Plain load with no read barrier.
6203 // /* HeapReference<Object> */ out = *(obj + offset)
6204 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6205 __ MaybeUnpoisonHeapReference(out_reg);
6206 }
6207}
6208
6209void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6210 Location root,
6211 Register obj,
6212 uint32_t offset,
6213 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006214 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006215 if (read_barrier_option == kWithReadBarrier) {
6216 DCHECK(kEmitCompilerReadBarrier);
6217 if (kUseBakerReadBarrier) {
6218 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6219 // Baker's read barrier are used:
6220 //
6221 // root = obj.field;
6222 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6223 // if (temp != null) {
6224 // root = temp(root)
6225 // }
6226
6227 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6228 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6229 static_assert(
6230 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6231 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6232 "have different sizes.");
6233 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6234 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6235 "have different sizes.");
6236
6237 // Slow path marking the GC root `root`.
6238 Location temp = Location::RegisterLocation(T9);
6239 SlowPathCodeMIPS* slow_path =
6240 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6241 instruction,
6242 root,
6243 /*entrypoint*/ temp);
6244 codegen_->AddSlowPath(slow_path);
6245
6246 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6247 const int32_t entry_point_offset =
6248 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6249 // Loading the entrypoint does not require a load acquire since it is only changed when
6250 // threads are suspended or running a checkpoint.
6251 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6252 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6253 // checking GetIsGcMarking.
6254 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6255 __ Bind(slow_path->GetExitLabel());
6256 } else {
6257 // GC root loaded through a slow path for read barriers other
6258 // than Baker's.
6259 // /* GcRoot<mirror::Object>* */ root = obj + offset
6260 __ Addiu32(root_reg, obj, offset);
6261 // /* mirror::Object* */ root = root->Read()
6262 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6263 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006264 } else {
6265 // Plain GC root load with no read barrier.
6266 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6267 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6268 // Note that GC roots are not affected by heap poisoning, thus we
6269 // do not have to unpoison `root_reg` here.
6270 }
6271}
6272
Alexey Frunze15958152017-02-09 19:08:30 -08006273void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6274 Location ref,
6275 Register obj,
6276 uint32_t offset,
6277 Location temp,
6278 bool needs_null_check) {
6279 DCHECK(kEmitCompilerReadBarrier);
6280 DCHECK(kUseBakerReadBarrier);
6281
6282 // /* HeapReference<Object> */ ref = *(obj + offset)
6283 Location no_index = Location::NoLocation();
6284 ScaleFactor no_scale_factor = TIMES_1;
6285 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6286 ref,
6287 obj,
6288 offset,
6289 no_index,
6290 no_scale_factor,
6291 temp,
6292 needs_null_check);
6293}
6294
6295void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6296 Location ref,
6297 Register obj,
6298 uint32_t data_offset,
6299 Location index,
6300 Location temp,
6301 bool needs_null_check) {
6302 DCHECK(kEmitCompilerReadBarrier);
6303 DCHECK(kUseBakerReadBarrier);
6304
6305 static_assert(
6306 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6307 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6308 // /* HeapReference<Object> */ ref =
6309 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6310 ScaleFactor scale_factor = TIMES_4;
6311 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6312 ref,
6313 obj,
6314 data_offset,
6315 index,
6316 scale_factor,
6317 temp,
6318 needs_null_check);
6319}
6320
6321void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6322 Location ref,
6323 Register obj,
6324 uint32_t offset,
6325 Location index,
6326 ScaleFactor scale_factor,
6327 Location temp,
6328 bool needs_null_check,
6329 bool always_update_field) {
6330 DCHECK(kEmitCompilerReadBarrier);
6331 DCHECK(kUseBakerReadBarrier);
6332
6333 // In slow path based read barriers, the read barrier call is
6334 // inserted after the original load. However, in fast path based
6335 // Baker's read barriers, we need to perform the load of
6336 // mirror::Object::monitor_ *before* the original reference load.
6337 // This load-load ordering is required by the read barrier.
6338 // The fast path/slow path (for Baker's algorithm) should look like:
6339 //
6340 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6341 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6342 // HeapReference<Object> ref = *src; // Original reference load.
6343 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6344 // if (is_gray) {
6345 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6346 // }
6347 //
6348 // Note: the original implementation in ReadBarrier::Barrier is
6349 // slightly more complex as it performs additional checks that we do
6350 // not do here for performance reasons.
6351
6352 Register ref_reg = ref.AsRegister<Register>();
6353 Register temp_reg = temp.AsRegister<Register>();
6354 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6355
6356 // /* int32_t */ monitor = obj->monitor_
6357 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6358 if (needs_null_check) {
6359 MaybeRecordImplicitNullCheck(instruction);
6360 }
6361 // /* LockWord */ lock_word = LockWord(monitor)
6362 static_assert(sizeof(LockWord) == sizeof(int32_t),
6363 "art::LockWord and int32_t have different sizes.");
6364
6365 __ Sync(0); // Barrier to prevent load-load reordering.
6366
6367 // The actual reference load.
6368 if (index.IsValid()) {
6369 // Load types involving an "index": ArrayGet,
6370 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6371 // intrinsics.
6372 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6373 if (index.IsConstant()) {
6374 size_t computed_offset =
6375 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6376 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6377 } else {
6378 // Handle the special case of the
6379 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6380 // intrinsics, which use a register pair as index ("long
6381 // offset"), of which only the low part contains data.
6382 Register index_reg = index.IsRegisterPair()
6383 ? index.AsRegisterPairLow<Register>()
6384 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006385 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006386 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6387 }
6388 } else {
6389 // /* HeapReference<Object> */ ref = *(obj + offset)
6390 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6391 }
6392
6393 // Object* ref = ref_addr->AsMirrorPtr()
6394 __ MaybeUnpoisonHeapReference(ref_reg);
6395
6396 // Slow path marking the object `ref` when it is gray.
6397 SlowPathCodeMIPS* slow_path;
6398 if (always_update_field) {
6399 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6400 // of the form `obj + field_offset`, where `obj` is a register and
6401 // `field_offset` is a register pair (of which only the lower half
6402 // is used). Thus `offset` and `scale_factor` above are expected
6403 // to be null in this code path.
6404 DCHECK_EQ(offset, 0u);
6405 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6406 slow_path = new (GetGraph()->GetArena())
6407 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6408 ref,
6409 obj,
6410 /* field_offset */ index,
6411 temp_reg);
6412 } else {
6413 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6414 }
6415 AddSlowPath(slow_path);
6416
6417 // if (rb_state == ReadBarrier::GrayState())
6418 // ref = ReadBarrier::Mark(ref);
6419 // Given the numeric representation, it's enough to check the low bit of the
6420 // rb_state. We do that by shifting the bit into the sign bit (31) and
6421 // performing a branch on less than zero.
6422 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6423 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6424 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6425 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6426 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6427 __ Bind(slow_path->GetExitLabel());
6428}
6429
6430void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6431 Location out,
6432 Location ref,
6433 Location obj,
6434 uint32_t offset,
6435 Location index) {
6436 DCHECK(kEmitCompilerReadBarrier);
6437
6438 // Insert a slow path based read barrier *after* the reference load.
6439 //
6440 // If heap poisoning is enabled, the unpoisoning of the loaded
6441 // reference will be carried out by the runtime within the slow
6442 // path.
6443 //
6444 // Note that `ref` currently does not get unpoisoned (when heap
6445 // poisoning is enabled), which is alright as the `ref` argument is
6446 // not used by the artReadBarrierSlow entry point.
6447 //
6448 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6449 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6450 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6451 AddSlowPath(slow_path);
6452
6453 __ B(slow_path->GetEntryLabel());
6454 __ Bind(slow_path->GetExitLabel());
6455}
6456
6457void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6458 Location out,
6459 Location ref,
6460 Location obj,
6461 uint32_t offset,
6462 Location index) {
6463 if (kEmitCompilerReadBarrier) {
6464 // Baker's read barriers shall be handled by the fast path
6465 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6466 DCHECK(!kUseBakerReadBarrier);
6467 // If heap poisoning is enabled, unpoisoning will be taken care of
6468 // by the runtime within the slow path.
6469 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6470 } else if (kPoisonHeapReferences) {
6471 __ UnpoisonHeapReference(out.AsRegister<Register>());
6472 }
6473}
6474
6475void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6476 Location out,
6477 Location root) {
6478 DCHECK(kEmitCompilerReadBarrier);
6479
6480 // Insert a slow path based read barrier *after* the GC root load.
6481 //
6482 // Note that GC roots are not affected by heap poisoning, so we do
6483 // not need to do anything special for this here.
6484 SlowPathCodeMIPS* slow_path =
6485 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6486 AddSlowPath(slow_path);
6487
6488 __ B(slow_path->GetEntryLabel());
6489 __ Bind(slow_path->GetExitLabel());
6490}
6491
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006492void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006493 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6494 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07006495 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006496 switch (type_check_kind) {
6497 case TypeCheckKind::kExactCheck:
6498 case TypeCheckKind::kAbstractClassCheck:
6499 case TypeCheckKind::kClassHierarchyCheck:
6500 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006501 call_kind =
6502 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006503 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006504 break;
6505 case TypeCheckKind::kArrayCheck:
6506 case TypeCheckKind::kUnresolvedCheck:
6507 case TypeCheckKind::kInterfaceCheck:
6508 call_kind = LocationSummary::kCallOnSlowPath;
6509 break;
6510 }
6511
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006513 if (baker_read_barrier_slow_path) {
6514 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6515 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006516 locations->SetInAt(0, Location::RequiresRegister());
6517 locations->SetInAt(1, Location::RequiresRegister());
6518 // The output does overlap inputs.
6519 // Note that TypeCheckSlowPathMIPS uses this register too.
6520 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006521 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006522}
6523
6524void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006525 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006526 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006527 Location obj_loc = locations->InAt(0);
6528 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006529 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006530 Location out_loc = locations->Out();
6531 Register out = out_loc.AsRegister<Register>();
6532 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6533 DCHECK_LE(num_temps, 1u);
6534 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006535 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6536 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6537 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6538 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006539 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006540 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006541
6542 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006543 // Avoid this check if we know `obj` is not null.
6544 if (instruction->MustDoNullCheck()) {
6545 __ Move(out, ZERO);
6546 __ Beqz(obj, &done);
6547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006548
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006549 switch (type_check_kind) {
6550 case TypeCheckKind::kExactCheck: {
6551 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006552 GenerateReferenceLoadTwoRegisters(instruction,
6553 out_loc,
6554 obj_loc,
6555 class_offset,
6556 maybe_temp_loc,
6557 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006558 // Classes must be equal for the instanceof to succeed.
6559 __ Xor(out, out, cls);
6560 __ Sltiu(out, out, 1);
6561 break;
6562 }
6563
6564 case TypeCheckKind::kAbstractClassCheck: {
6565 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006566 GenerateReferenceLoadTwoRegisters(instruction,
6567 out_loc,
6568 obj_loc,
6569 class_offset,
6570 maybe_temp_loc,
6571 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006572 // If the class is abstract, we eagerly fetch the super class of the
6573 // object to avoid doing a comparison we know will fail.
6574 MipsLabel loop;
6575 __ Bind(&loop);
6576 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006577 GenerateReferenceLoadOneRegister(instruction,
6578 out_loc,
6579 super_offset,
6580 maybe_temp_loc,
6581 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006582 // If `out` is null, we use it for the result, and jump to `done`.
6583 __ Beqz(out, &done);
6584 __ Bne(out, cls, &loop);
6585 __ LoadConst32(out, 1);
6586 break;
6587 }
6588
6589 case TypeCheckKind::kClassHierarchyCheck: {
6590 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006591 GenerateReferenceLoadTwoRegisters(instruction,
6592 out_loc,
6593 obj_loc,
6594 class_offset,
6595 maybe_temp_loc,
6596 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006597 // Walk over the class hierarchy to find a match.
6598 MipsLabel loop, success;
6599 __ Bind(&loop);
6600 __ Beq(out, cls, &success);
6601 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006602 GenerateReferenceLoadOneRegister(instruction,
6603 out_loc,
6604 super_offset,
6605 maybe_temp_loc,
6606 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006607 __ Bnez(out, &loop);
6608 // If `out` is null, we use it for the result, and jump to `done`.
6609 __ B(&done);
6610 __ Bind(&success);
6611 __ LoadConst32(out, 1);
6612 break;
6613 }
6614
6615 case TypeCheckKind::kArrayObjectCheck: {
6616 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006617 GenerateReferenceLoadTwoRegisters(instruction,
6618 out_loc,
6619 obj_loc,
6620 class_offset,
6621 maybe_temp_loc,
6622 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006623 // Do an exact check.
6624 MipsLabel success;
6625 __ Beq(out, cls, &success);
6626 // Otherwise, we need to check that the object's class is a non-primitive array.
6627 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006628 GenerateReferenceLoadOneRegister(instruction,
6629 out_loc,
6630 component_offset,
6631 maybe_temp_loc,
6632 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006633 // If `out` is null, we use it for the result, and jump to `done`.
6634 __ Beqz(out, &done);
6635 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6636 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6637 __ Sltiu(out, out, 1);
6638 __ B(&done);
6639 __ Bind(&success);
6640 __ LoadConst32(out, 1);
6641 break;
6642 }
6643
6644 case TypeCheckKind::kArrayCheck: {
6645 // No read barrier since the slow path will retry upon failure.
6646 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006647 GenerateReferenceLoadTwoRegisters(instruction,
6648 out_loc,
6649 obj_loc,
6650 class_offset,
6651 maybe_temp_loc,
6652 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006653 DCHECK(locations->OnlyCallsOnSlowPath());
6654 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6655 /* is_fatal */ false);
6656 codegen_->AddSlowPath(slow_path);
6657 __ Bne(out, cls, slow_path->GetEntryLabel());
6658 __ LoadConst32(out, 1);
6659 break;
6660 }
6661
6662 case TypeCheckKind::kUnresolvedCheck:
6663 case TypeCheckKind::kInterfaceCheck: {
6664 // Note that we indeed only call on slow path, but we always go
6665 // into the slow path for the unresolved and interface check
6666 // cases.
6667 //
6668 // We cannot directly call the InstanceofNonTrivial runtime
6669 // entry point without resorting to a type checking slow path
6670 // here (i.e. by calling InvokeRuntime directly), as it would
6671 // require to assign fixed registers for the inputs of this
6672 // HInstanceOf instruction (following the runtime calling
6673 // convention), which might be cluttered by the potential first
6674 // read barrier emission at the beginning of this method.
6675 //
6676 // TODO: Introduce a new runtime entry point taking the object
6677 // to test (instead of its class) as argument, and let it deal
6678 // with the read barrier issues. This will let us refactor this
6679 // case of the `switch` code as it was previously (with a direct
6680 // call to the runtime not using a type checking slow path).
6681 // This should also be beneficial for the other cases above.
6682 DCHECK(locations->OnlyCallsOnSlowPath());
6683 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6684 /* is_fatal */ false);
6685 codegen_->AddSlowPath(slow_path);
6686 __ B(slow_path->GetEntryLabel());
6687 break;
6688 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006689 }
6690
6691 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006692
6693 if (slow_path != nullptr) {
6694 __ Bind(slow_path->GetExitLabel());
6695 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006696}
6697
6698void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6700 locations->SetOut(Location::ConstantLocation(constant));
6701}
6702
6703void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6704 // Will be generated at use site.
6705}
6706
6707void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6709 locations->SetOut(Location::ConstantLocation(constant));
6710}
6711
6712void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6713 // Will be generated at use site.
6714}
6715
6716void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6717 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6718 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6719}
6720
6721void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6722 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006723 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006725 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006726}
6727
6728void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6729 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6730 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731 Location receiver = invoke->GetLocations()->InAt(0);
6732 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006733 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734
6735 // Set the hidden argument.
6736 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6737 invoke->GetDexMethodIndex());
6738
6739 // temp = object->GetClass();
6740 if (receiver.IsStackSlot()) {
6741 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6742 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6743 } else {
6744 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6745 }
6746 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006747 // Instead of simply (possibly) unpoisoning `temp` here, we should
6748 // emit a read barrier for the previous class reference load.
6749 // However this is not required in practice, as this is an
6750 // intermediate/temporary reference and because the current
6751 // concurrent copying collector keeps the from-space memory
6752 // intact/accessible until the end of the marking phase (the
6753 // concurrent copying collector may not in the future).
6754 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006755 __ LoadFromOffset(kLoadWord, temp, temp,
6756 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6757 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006758 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006759 // temp = temp->GetImtEntryAt(method_offset);
6760 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6761 // T9 = temp->GetEntryPoint();
6762 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6763 // T9();
6764 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006765 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006766 DCHECK(!codegen_->IsLeafMethod());
6767 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6768}
6769
6770void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006771 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6772 if (intrinsic.TryDispatch(invoke)) {
6773 return;
6774 }
6775
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006776 HandleInvoke(invoke);
6777}
6778
6779void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006780 // Explicit clinit checks triggered by static invokes must have been pruned by
6781 // art::PrepareForRegisterAllocation.
6782 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006783
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006784 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6785 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006786
Chris Larsen701566a2015-10-27 15:29:13 -07006787 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6788 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006789 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6790 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6791 }
Chris Larsen701566a2015-10-27 15:29:13 -07006792 return;
6793 }
6794
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006795 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006796
6797 // Add the extra input register if either the dex cache array base register
6798 // or the PC-relative base register for accessing literals is needed.
6799 if (has_extra_input) {
6800 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
6801 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006802}
6803
Orion Hodsonac141392017-01-13 11:53:47 +00006804void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6805 HandleInvoke(invoke);
6806}
6807
6808void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6809 codegen_->GenerateInvokePolymorphicCall(invoke);
6810}
6811
Chris Larsen701566a2015-10-27 15:29:13 -07006812static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006813 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07006814 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
6815 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006816 return true;
6817 }
6818 return false;
6819}
6820
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006821HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07006822 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006823 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006824 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006825 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
6826 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07006827 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006828 bool is_r6 = GetInstructionSetFeatures().IsR6();
6829 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006830 switch (desired_string_load_kind) {
6831 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6832 DCHECK(!GetCompilerOptions().GetCompilePic());
6833 break;
6834 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6835 DCHECK(GetCompilerOptions().GetCompilePic());
6836 break;
6837 case HLoadString::LoadKind::kBootImageAddress:
6838 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006839 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006840 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07006841 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006842 case HLoadString::LoadKind::kJitTableAddress:
6843 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006844 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006845 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006846 case HLoadString::LoadKind::kDexCacheViaMethod:
6847 fallback_load = false;
6848 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006849 }
6850 if (fallback_load) {
6851 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
6852 }
6853 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006854}
6855
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006856HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
6857 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006858 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07006859 // is incompatible with it.
6860 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006861 bool is_r6 = GetInstructionSetFeatures().IsR6();
6862 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006863 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006864 case HLoadClass::LoadKind::kInvalid:
6865 LOG(FATAL) << "UNREACHABLE";
6866 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07006867 case HLoadClass::LoadKind::kReferrersClass:
6868 fallback_load = false;
6869 break;
6870 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6871 DCHECK(!GetCompilerOptions().GetCompilePic());
6872 break;
6873 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6874 DCHECK(GetCompilerOptions().GetCompilePic());
6875 break;
6876 case HLoadClass::LoadKind::kBootImageAddress:
6877 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006878 case HLoadClass::LoadKind::kBssEntry:
6879 DCHECK(!Runtime::Current()->UseJitCompilation());
6880 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006881 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07006882 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08006883 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006884 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07006885 case HLoadClass::LoadKind::kDexCacheViaMethod:
6886 fallback_load = false;
6887 break;
6888 }
6889 if (fallback_load) {
6890 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
6891 }
6892 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006893}
6894
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006895Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6896 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006897 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006898 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6899 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6900 if (!invoke->GetLocations()->Intrinsified()) {
6901 return location.AsRegister<Register>();
6902 }
6903 // For intrinsics we allow any location, so it may be on the stack.
6904 if (!location.IsRegister()) {
6905 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6906 return temp;
6907 }
6908 // For register locations, check if the register was saved. If so, get it from the stack.
6909 // Note: There is a chance that the register was saved but not overwritten, so we could
6910 // save one load. However, since this is just an intrinsic slow path we prefer this
6911 // simple and more robust approach rather that trying to determine if that's the case.
6912 SlowPathCode* slow_path = GetCurrentSlowPath();
6913 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6914 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6915 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6916 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6917 return temp;
6918 }
6919 return location.AsRegister<Register>();
6920}
6921
Vladimir Markodc151b22015-10-15 18:02:30 +01006922HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
6923 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006924 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006925 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006926 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006927 // is incompatible with it.
6928 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006929 bool is_r6 = GetInstructionSetFeatures().IsR6();
6930 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006931 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006932 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006933 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01006934 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006935 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01006936 break;
6937 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006938 if (fallback_load) {
6939 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6940 dispatch_info.method_load_data = 0;
6941 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006942 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006943}
6944
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006945void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
6946 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006947 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006948 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
6949 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006950 bool is_r6 = GetInstructionSetFeatures().IsR6();
6951 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006952 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
6953 : ZERO;
6954
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006955 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006956 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006957 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006958 uint32_t offset =
6959 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006960 __ LoadFromOffset(kLoadWord,
6961 temp.AsRegister<Register>(),
6962 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006963 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006964 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006965 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006966 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006967 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006968 break;
6969 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6970 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6971 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006972 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
6973 if (is_r6) {
6974 uint32_t offset = invoke->GetDexCacheArrayOffset();
6975 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6976 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
6977 bool reordering = __ SetReorder(false);
6978 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
6979 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
6980 __ SetReorder(reordering);
6981 } else {
6982 HMipsDexCacheArraysBase* base =
6983 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
6984 int32_t offset =
6985 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
6986 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6987 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006988 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006989 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006990 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006991 Register reg = temp.AsRegister<Register>();
6992 Register method_reg;
6993 if (current_method.IsRegister()) {
6994 method_reg = current_method.AsRegister<Register>();
6995 } else {
6996 // TODO: use the appropriate DCHECK() here if possible.
6997 // DCHECK(invoke->GetLocations()->Intrinsified());
6998 DCHECK(!current_method.IsValid());
6999 method_reg = reg;
7000 __ Lw(reg, SP, kCurrentMethodStackOffset);
7001 }
7002
7003 // temp = temp->dex_cache_resolved_methods_;
7004 __ LoadFromOffset(kLoadWord,
7005 reg,
7006 method_reg,
7007 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007008 // temp = temp[index_in_cache];
7009 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7010 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007011 __ LoadFromOffset(kLoadWord,
7012 reg,
7013 reg,
7014 CodeGenerator::GetCachePointerOffset(index_in_cache));
7015 break;
7016 }
7017 }
7018
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007019 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007020 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007021 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007022 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007023 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7024 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007025 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007026 T9,
7027 callee_method.AsRegister<Register>(),
7028 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007029 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007030 // T9()
7031 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007032 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007033 break;
7034 }
7035 DCHECK(!IsLeafMethod());
7036}
7037
7038void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007039 // Explicit clinit checks triggered by static invokes must have been pruned by
7040 // art::PrepareForRegisterAllocation.
7041 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007042
7043 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7044 return;
7045 }
7046
7047 LocationSummary* locations = invoke->GetLocations();
7048 codegen_->GenerateStaticOrDirectCall(invoke,
7049 locations->HasTemps()
7050 ? locations->GetTemp(0)
7051 : Location::NoLocation());
7052 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7053}
7054
Chris Larsen3acee732015-11-18 13:31:08 -08007055void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007056 // Use the calling convention instead of the location of the receiver, as
7057 // intrinsics may have put the receiver in a different register. In the intrinsics
7058 // slow path, the arguments have been moved to the right place, so here we are
7059 // guaranteed that the receiver is the first register of the calling convention.
7060 InvokeDexCallingConvention calling_convention;
7061 Register receiver = calling_convention.GetRegisterAt(0);
7062
Chris Larsen3acee732015-11-18 13:31:08 -08007063 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007064 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7065 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7066 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007067 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007068
7069 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007070 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007071 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007072 // Instead of simply (possibly) unpoisoning `temp` here, we should
7073 // emit a read barrier for the previous class reference load.
7074 // However this is not required in practice, as this is an
7075 // intermediate/temporary reference and because the current
7076 // concurrent copying collector keeps the from-space memory
7077 // intact/accessible until the end of the marking phase (the
7078 // concurrent copying collector may not in the future).
7079 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007080 // temp = temp->GetMethodAt(method_offset);
7081 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7082 // T9 = temp->GetEntryPoint();
7083 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7084 // T9();
7085 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007086 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08007087}
7088
7089void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7090 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7091 return;
7092 }
7093
7094 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007095 DCHECK(!codegen_->IsLeafMethod());
7096 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7097}
7098
7099void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007100 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7101 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007102 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007103 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7104 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007105 return;
7106 }
Vladimir Marko41559982017-01-06 14:04:23 +00007107 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007108 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007109 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7110 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007111 ? LocationSummary::kCallOnSlowPath
7112 : LocationSummary::kNoCall;
7113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007114 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7115 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7116 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007117 switch (load_kind) {
7118 // We need an extra register for PC-relative literals on R2.
7119 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007120 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007121 case HLoadClass::LoadKind::kBootImageAddress:
7122 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007123 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007124 break;
7125 }
7126 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007127 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007128 locations->SetInAt(0, Location::RequiresRegister());
7129 break;
7130 default:
7131 break;
7132 }
7133 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007134 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7135 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7136 // Rely on the type resolution or initialization and marking to save everything we need.
7137 // Request a temp to hold the BSS entry location for the slow path on R2
7138 // (no benefit for R6).
7139 if (!isR6) {
7140 locations->AddTemp(Location::RequiresRegister());
7141 }
7142 RegisterSet caller_saves = RegisterSet::Empty();
7143 InvokeRuntimeCallingConvention calling_convention;
7144 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7145 locations->SetCustomSlowPathCallerSaves(caller_saves);
7146 } else {
7147 // For non-Baker read barriers we have a temp-clobbering call.
7148 }
7149 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007150}
7151
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007152// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7153// move.
7154void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007155 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7156 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7157 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007158 return;
7159 }
Vladimir Marko41559982017-01-06 14:04:23 +00007160 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007161
Vladimir Marko41559982017-01-06 14:04:23 +00007162 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007163 Location out_loc = locations->Out();
7164 Register out = out_loc.AsRegister<Register>();
7165 Register base_or_current_method_reg;
7166 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7167 switch (load_kind) {
7168 // We need an extra register for PC-relative literals on R2.
7169 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007170 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007171 case HLoadClass::LoadKind::kBootImageAddress:
7172 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007173 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7174 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007175 case HLoadClass::LoadKind::kReferrersClass:
7176 case HLoadClass::LoadKind::kDexCacheViaMethod:
7177 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7178 break;
7179 default:
7180 base_or_current_method_reg = ZERO;
7181 break;
7182 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007183
Alexey Frunze15958152017-02-09 19:08:30 -08007184 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7185 ? kWithoutReadBarrier
7186 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007187 bool generate_null_check = false;
7188 switch (load_kind) {
7189 case HLoadClass::LoadKind::kReferrersClass: {
7190 DCHECK(!cls->CanCallRuntime());
7191 DCHECK(!cls->MustGenerateClinitCheck());
7192 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7193 GenerateGcRootFieldLoad(cls,
7194 out_loc,
7195 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007196 ArtMethod::DeclaringClassOffset().Int32Value(),
7197 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007198 break;
7199 }
7200 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007201 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007202 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007203 __ LoadLiteral(out,
7204 base_or_current_method_reg,
7205 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
7206 cls->GetTypeIndex()));
7207 break;
7208 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
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 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7212 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007213 bool reordering = __ SetReorder(false);
7214 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7215 __ Addiu(out, out, /* placeholder */ 0x5678);
7216 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007217 break;
7218 }
7219 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007220 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007221 uint32_t address = dchecked_integral_cast<uint32_t>(
7222 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7223 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007224 __ LoadLiteral(out,
7225 base_or_current_method_reg,
7226 codegen_->DeduplicateBootImageAddressLiteral(address));
7227 break;
7228 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007229 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007230 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007231 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007232 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7233 if (isR6 || non_baker_read_barrier) {
7234 bool reordering = __ SetReorder(false);
7235 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7236 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
7237 __ SetReorder(reordering);
7238 } else {
7239 // On R2 save the BSS entry address in a temporary register instead of
7240 // recalculating it in the slow path.
7241 Register temp = locations->GetTemp(0).AsRegister<Register>();
7242 bool reordering = __ SetReorder(false);
7243 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7244 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7245 __ SetReorder(reordering);
7246 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
7247 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007248 generate_null_check = true;
7249 break;
7250 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007251 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007252 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7253 cls->GetTypeIndex(),
7254 cls->GetClass());
7255 bool reordering = __ SetReorder(false);
7256 __ Bind(&info->high_label);
7257 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007258 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007259 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007260 break;
7261 }
Vladimir Marko41559982017-01-06 14:04:23 +00007262 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007263 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007264 LOG(FATAL) << "UNREACHABLE";
7265 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007266 }
7267
7268 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7269 DCHECK(cls->CanCallRuntime());
7270 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7271 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7272 codegen_->AddSlowPath(slow_path);
7273 if (generate_null_check) {
7274 __ Beqz(out, slow_path->GetEntryLabel());
7275 }
7276 if (cls->MustGenerateClinitCheck()) {
7277 GenerateClassInitializationCheck(slow_path, out);
7278 } else {
7279 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007280 }
7281 }
7282}
7283
7284static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007285 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007286}
7287
7288void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7289 LocationSummary* locations =
7290 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7291 locations->SetOut(Location::RequiresRegister());
7292}
7293
7294void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7295 Register out = load->GetLocations()->Out().AsRegister<Register>();
7296 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7297}
7298
7299void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7300 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7301}
7302
7303void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7304 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7305}
7306
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007307void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007308 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007309 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007310 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007311 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007312 switch (load_kind) {
7313 // We need an extra register for PC-relative literals on R2.
7314 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7315 case HLoadString::LoadKind::kBootImageAddress:
7316 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007317 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007318 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007319 break;
7320 }
7321 FALLTHROUGH_INTENDED;
7322 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007323 case HLoadString::LoadKind::kDexCacheViaMethod:
7324 locations->SetInAt(0, Location::RequiresRegister());
7325 break;
7326 default:
7327 break;
7328 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007329 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
7330 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007331 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007332 } else {
7333 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007334 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7335 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7336 // Rely on the pResolveString and marking to save everything we need.
7337 // Request a temp to hold the BSS entry location for the slow path on R2
7338 // (no benefit for R6).
7339 if (!isR6) {
7340 locations->AddTemp(Location::RequiresRegister());
7341 }
7342 RegisterSet caller_saves = RegisterSet::Empty();
7343 InvokeRuntimeCallingConvention calling_convention;
7344 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7345 locations->SetCustomSlowPathCallerSaves(caller_saves);
7346 } else {
7347 // For non-Baker read barriers we have a temp-clobbering call.
7348 }
7349 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007350 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007351}
7352
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007353// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7354// move.
7355void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007356 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007357 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007358 Location out_loc = locations->Out();
7359 Register out = out_loc.AsRegister<Register>();
7360 Register base_or_current_method_reg;
7361 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7362 switch (load_kind) {
7363 // We need an extra register for PC-relative literals on R2.
7364 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7365 case HLoadString::LoadKind::kBootImageAddress:
7366 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007367 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007368 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7369 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007370 default:
7371 base_or_current_method_reg = ZERO;
7372 break;
7373 }
7374
7375 switch (load_kind) {
7376 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007377 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007378 __ LoadLiteral(out,
7379 base_or_current_method_reg,
7380 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7381 load->GetStringIndex()));
7382 return; // No dex cache slow path.
7383 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007384 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007385 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007386 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007387 bool reordering = __ SetReorder(false);
7388 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7389 __ Addiu(out, out, /* placeholder */ 0x5678);
7390 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007391 return; // No dex cache slow path.
7392 }
7393 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007394 uint32_t address = dchecked_integral_cast<uint32_t>(
7395 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7396 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007397 __ LoadLiteral(out,
7398 base_or_current_method_reg,
7399 codegen_->DeduplicateBootImageAddressLiteral(address));
7400 return; // No dex cache slow path.
7401 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007402 case HLoadString::LoadKind::kBssEntry: {
7403 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7404 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007405 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007406 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7407 if (isR6 || non_baker_read_barrier) {
7408 bool reordering = __ SetReorder(false);
7409 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7410 GenerateGcRootFieldLoad(load,
7411 out_loc,
7412 out,
7413 /* placeholder */ 0x5678,
7414 kCompilerReadBarrierOption);
7415 __ SetReorder(reordering);
7416 } else {
7417 // On R2 save the BSS entry address in a temporary register instead of
7418 // recalculating it in the slow path.
7419 Register temp = locations->GetTemp(0).AsRegister<Register>();
7420 bool reordering = __ SetReorder(false);
7421 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7422 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7423 __ SetReorder(reordering);
7424 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7425 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007426 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7427 codegen_->AddSlowPath(slow_path);
7428 __ Beqz(out, slow_path->GetEntryLabel());
7429 __ Bind(slow_path->GetExitLabel());
7430 return;
7431 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007432 case HLoadString::LoadKind::kJitTableAddress: {
7433 CodeGeneratorMIPS::JitPatchInfo* info =
7434 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7435 load->GetStringIndex(),
7436 load->GetString());
7437 bool reordering = __ SetReorder(false);
7438 __ Bind(&info->high_label);
7439 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007440 GenerateGcRootFieldLoad(load,
7441 out_loc,
7442 out,
7443 /* placeholder */ 0x5678,
7444 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007445 __ SetReorder(reordering);
7446 return;
7447 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007448 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007449 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007450 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007451
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007452 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007453 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7454 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007455 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007456 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007457 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7458 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007459}
7460
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007461void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7463 locations->SetOut(Location::ConstantLocation(constant));
7464}
7465
7466void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7467 // Will be generated at use site.
7468}
7469
7470void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7471 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007473 InvokeRuntimeCallingConvention calling_convention;
7474 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7475}
7476
7477void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7478 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007479 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007480 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7481 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007482 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007483 }
7484 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7485}
7486
7487void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7488 LocationSummary* locations =
7489 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7490 switch (mul->GetResultType()) {
7491 case Primitive::kPrimInt:
7492 case Primitive::kPrimLong:
7493 locations->SetInAt(0, Location::RequiresRegister());
7494 locations->SetInAt(1, Location::RequiresRegister());
7495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7496 break;
7497
7498 case Primitive::kPrimFloat:
7499 case Primitive::kPrimDouble:
7500 locations->SetInAt(0, Location::RequiresFpuRegister());
7501 locations->SetInAt(1, Location::RequiresFpuRegister());
7502 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7503 break;
7504
7505 default:
7506 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7507 }
7508}
7509
7510void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7511 Primitive::Type type = instruction->GetType();
7512 LocationSummary* locations = instruction->GetLocations();
7513 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7514
7515 switch (type) {
7516 case Primitive::kPrimInt: {
7517 Register dst = locations->Out().AsRegister<Register>();
7518 Register lhs = locations->InAt(0).AsRegister<Register>();
7519 Register rhs = locations->InAt(1).AsRegister<Register>();
7520
7521 if (isR6) {
7522 __ MulR6(dst, lhs, rhs);
7523 } else {
7524 __ MulR2(dst, lhs, rhs);
7525 }
7526 break;
7527 }
7528 case Primitive::kPrimLong: {
7529 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7530 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7531 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7532 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7533 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7534 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7535
7536 // Extra checks to protect caused by the existance of A1_A2.
7537 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7538 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7539 DCHECK_NE(dst_high, lhs_low);
7540 DCHECK_NE(dst_high, rhs_low);
7541
7542 // A_B * C_D
7543 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7544 // dst_lo: [ low(B*D) ]
7545 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7546
7547 if (isR6) {
7548 __ MulR6(TMP, lhs_high, rhs_low);
7549 __ MulR6(dst_high, lhs_low, rhs_high);
7550 __ Addu(dst_high, dst_high, TMP);
7551 __ MuhuR6(TMP, lhs_low, rhs_low);
7552 __ Addu(dst_high, dst_high, TMP);
7553 __ MulR6(dst_low, lhs_low, rhs_low);
7554 } else {
7555 __ MulR2(TMP, lhs_high, rhs_low);
7556 __ MulR2(dst_high, lhs_low, rhs_high);
7557 __ Addu(dst_high, dst_high, TMP);
7558 __ MultuR2(lhs_low, rhs_low);
7559 __ Mfhi(TMP);
7560 __ Addu(dst_high, dst_high, TMP);
7561 __ Mflo(dst_low);
7562 }
7563 break;
7564 }
7565 case Primitive::kPrimFloat:
7566 case Primitive::kPrimDouble: {
7567 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7568 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7569 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7570 if (type == Primitive::kPrimFloat) {
7571 __ MulS(dst, lhs, rhs);
7572 } else {
7573 __ MulD(dst, lhs, rhs);
7574 }
7575 break;
7576 }
7577 default:
7578 LOG(FATAL) << "Unexpected mul type " << type;
7579 }
7580}
7581
7582void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7583 LocationSummary* locations =
7584 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7585 switch (neg->GetResultType()) {
7586 case Primitive::kPrimInt:
7587 case Primitive::kPrimLong:
7588 locations->SetInAt(0, Location::RequiresRegister());
7589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7590 break;
7591
7592 case Primitive::kPrimFloat:
7593 case Primitive::kPrimDouble:
7594 locations->SetInAt(0, Location::RequiresFpuRegister());
7595 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7596 break;
7597
7598 default:
7599 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7600 }
7601}
7602
7603void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7604 Primitive::Type type = instruction->GetType();
7605 LocationSummary* locations = instruction->GetLocations();
7606
7607 switch (type) {
7608 case Primitive::kPrimInt: {
7609 Register dst = locations->Out().AsRegister<Register>();
7610 Register src = locations->InAt(0).AsRegister<Register>();
7611 __ Subu(dst, ZERO, src);
7612 break;
7613 }
7614 case Primitive::kPrimLong: {
7615 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7616 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7617 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7618 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7619 __ Subu(dst_low, ZERO, src_low);
7620 __ Sltu(TMP, ZERO, dst_low);
7621 __ Subu(dst_high, ZERO, src_high);
7622 __ Subu(dst_high, dst_high, TMP);
7623 break;
7624 }
7625 case Primitive::kPrimFloat:
7626 case Primitive::kPrimDouble: {
7627 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7628 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7629 if (type == Primitive::kPrimFloat) {
7630 __ NegS(dst, src);
7631 } else {
7632 __ NegD(dst, src);
7633 }
7634 break;
7635 }
7636 default:
7637 LOG(FATAL) << "Unexpected neg type " << type;
7638 }
7639}
7640
7641void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7642 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007644 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007645 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007646 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7647 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007648}
7649
7650void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007651 // Note: if heap poisoning is enabled, the entry point takes care
7652 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007653 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7654 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007655}
7656
7657void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7658 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007659 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007660 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007661 if (instruction->IsStringAlloc()) {
7662 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7663 } else {
7664 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007665 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007666 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7667}
7668
7669void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007670 // Note: if heap poisoning is enabled, the entry point takes care
7671 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007672 if (instruction->IsStringAlloc()) {
7673 // String is allocated through StringFactory. Call NewEmptyString entry point.
7674 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007675 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007676 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7677 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7678 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007679 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007680 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7681 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007682 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007683 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007684 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007685}
7686
7687void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7689 locations->SetInAt(0, Location::RequiresRegister());
7690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7691}
7692
7693void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7694 Primitive::Type type = instruction->GetType();
7695 LocationSummary* locations = instruction->GetLocations();
7696
7697 switch (type) {
7698 case Primitive::kPrimInt: {
7699 Register dst = locations->Out().AsRegister<Register>();
7700 Register src = locations->InAt(0).AsRegister<Register>();
7701 __ Nor(dst, src, ZERO);
7702 break;
7703 }
7704
7705 case Primitive::kPrimLong: {
7706 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7707 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7708 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7709 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7710 __ Nor(dst_high, src_high, ZERO);
7711 __ Nor(dst_low, src_low, ZERO);
7712 break;
7713 }
7714
7715 default:
7716 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7717 }
7718}
7719
7720void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7721 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7722 locations->SetInAt(0, Location::RequiresRegister());
7723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7724}
7725
7726void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7727 LocationSummary* locations = instruction->GetLocations();
7728 __ Xori(locations->Out().AsRegister<Register>(),
7729 locations->InAt(0).AsRegister<Register>(),
7730 1);
7731}
7732
7733void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007734 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7735 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007736}
7737
Calin Juravle2ae48182016-03-16 14:05:09 +00007738void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7739 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007740 return;
7741 }
7742 Location obj = instruction->GetLocations()->InAt(0);
7743
7744 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007745 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007746}
7747
Calin Juravle2ae48182016-03-16 14:05:09 +00007748void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007749 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007750 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007751
7752 Location obj = instruction->GetLocations()->InAt(0);
7753
7754 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7755}
7756
7757void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007758 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007759}
7760
7761void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7762 HandleBinaryOp(instruction);
7763}
7764
7765void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7766 HandleBinaryOp(instruction);
7767}
7768
7769void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7770 LOG(FATAL) << "Unreachable";
7771}
7772
7773void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7774 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7775}
7776
7777void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7779 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7780 if (location.IsStackSlot()) {
7781 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7782 } else if (location.IsDoubleStackSlot()) {
7783 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7784 }
7785 locations->SetOut(location);
7786}
7787
7788void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7789 ATTRIBUTE_UNUSED) {
7790 // Nothing to do, the parameter is already at its location.
7791}
7792
7793void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
7794 LocationSummary* locations =
7795 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7796 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
7797}
7798
7799void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
7800 ATTRIBUTE_UNUSED) {
7801 // Nothing to do, the method is already at its location.
7802}
7803
7804void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
7805 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01007806 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007807 locations->SetInAt(i, Location::Any());
7808 }
7809 locations->SetOut(Location::Any());
7810}
7811
7812void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
7813 LOG(FATAL) << "Unreachable";
7814}
7815
7816void LocationsBuilderMIPS::VisitRem(HRem* rem) {
7817 Primitive::Type type = rem->GetResultType();
7818 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007819 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
7821
7822 switch (type) {
7823 case Primitive::kPrimInt:
7824 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08007825 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007826 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7827 break;
7828
7829 case Primitive::kPrimLong: {
7830 InvokeRuntimeCallingConvention calling_convention;
7831 locations->SetInAt(0, Location::RegisterPairLocation(
7832 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7833 locations->SetInAt(1, Location::RegisterPairLocation(
7834 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
7835 locations->SetOut(calling_convention.GetReturnLocation(type));
7836 break;
7837 }
7838
7839 case Primitive::kPrimFloat:
7840 case Primitive::kPrimDouble: {
7841 InvokeRuntimeCallingConvention calling_convention;
7842 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
7843 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
7844 locations->SetOut(calling_convention.GetReturnLocation(type));
7845 break;
7846 }
7847
7848 default:
7849 LOG(FATAL) << "Unexpected rem type " << type;
7850 }
7851}
7852
7853void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
7854 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007855
7856 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08007857 case Primitive::kPrimInt:
7858 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007859 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007860 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007861 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007862 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
7863 break;
7864 }
7865 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007866 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007867 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007868 break;
7869 }
7870 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007871 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007872 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007873 break;
7874 }
7875 default:
7876 LOG(FATAL) << "Unexpected rem type " << type;
7877 }
7878}
7879
7880void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7881 memory_barrier->SetLocations(nullptr);
7882}
7883
7884void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7885 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7886}
7887
7888void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
7889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
7890 Primitive::Type return_type = ret->InputAt(0)->GetType();
7891 locations->SetInAt(0, MipsReturnLocation(return_type));
7892}
7893
7894void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7895 codegen_->GenerateFrameExit();
7896}
7897
7898void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
7899 ret->SetLocations(nullptr);
7900}
7901
7902void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7903 codegen_->GenerateFrameExit();
7904}
7905
Alexey Frunze92d90602015-12-18 18:16:36 -08007906void LocationsBuilderMIPS::VisitRor(HRor* ror) {
7907 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007908}
7909
Alexey Frunze92d90602015-12-18 18:16:36 -08007910void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
7911 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007912}
7913
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007914void LocationsBuilderMIPS::VisitShl(HShl* shl) {
7915 HandleShift(shl);
7916}
7917
7918void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
7919 HandleShift(shl);
7920}
7921
7922void LocationsBuilderMIPS::VisitShr(HShr* shr) {
7923 HandleShift(shr);
7924}
7925
7926void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
7927 HandleShift(shr);
7928}
7929
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007930void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
7931 HandleBinaryOp(instruction);
7932}
7933
7934void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
7935 HandleBinaryOp(instruction);
7936}
7937
7938void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7939 HandleFieldGet(instruction, instruction->GetFieldInfo());
7940}
7941
7942void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7943 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
7944}
7945
7946void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7947 HandleFieldSet(instruction, instruction->GetFieldInfo());
7948}
7949
7950void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01007951 HandleFieldSet(instruction,
7952 instruction->GetFieldInfo(),
7953 instruction->GetDexPc(),
7954 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007955}
7956
7957void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
7958 HUnresolvedInstanceFieldGet* instruction) {
7959 FieldAccessCallingConventionMIPS calling_convention;
7960 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7961 instruction->GetFieldType(),
7962 calling_convention);
7963}
7964
7965void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
7966 HUnresolvedInstanceFieldGet* instruction) {
7967 FieldAccessCallingConventionMIPS calling_convention;
7968 codegen_->GenerateUnresolvedFieldAccess(instruction,
7969 instruction->GetFieldType(),
7970 instruction->GetFieldIndex(),
7971 instruction->GetDexPc(),
7972 calling_convention);
7973}
7974
7975void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
7976 HUnresolvedInstanceFieldSet* instruction) {
7977 FieldAccessCallingConventionMIPS calling_convention;
7978 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7979 instruction->GetFieldType(),
7980 calling_convention);
7981}
7982
7983void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
7984 HUnresolvedInstanceFieldSet* instruction) {
7985 FieldAccessCallingConventionMIPS calling_convention;
7986 codegen_->GenerateUnresolvedFieldAccess(instruction,
7987 instruction->GetFieldType(),
7988 instruction->GetFieldIndex(),
7989 instruction->GetDexPc(),
7990 calling_convention);
7991}
7992
7993void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
7994 HUnresolvedStaticFieldGet* instruction) {
7995 FieldAccessCallingConventionMIPS calling_convention;
7996 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
7997 instruction->GetFieldType(),
7998 calling_convention);
7999}
8000
8001void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8002 HUnresolvedStaticFieldGet* instruction) {
8003 FieldAccessCallingConventionMIPS calling_convention;
8004 codegen_->GenerateUnresolvedFieldAccess(instruction,
8005 instruction->GetFieldType(),
8006 instruction->GetFieldIndex(),
8007 instruction->GetDexPc(),
8008 calling_convention);
8009}
8010
8011void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8012 HUnresolvedStaticFieldSet* instruction) {
8013 FieldAccessCallingConventionMIPS calling_convention;
8014 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8015 instruction->GetFieldType(),
8016 calling_convention);
8017}
8018
8019void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8020 HUnresolvedStaticFieldSet* instruction) {
8021 FieldAccessCallingConventionMIPS calling_convention;
8022 codegen_->GenerateUnresolvedFieldAccess(instruction,
8023 instruction->GetFieldType(),
8024 instruction->GetFieldIndex(),
8025 instruction->GetDexPc(),
8026 calling_convention);
8027}
8028
8029void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008030 LocationSummary* locations =
8031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01008032 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008033}
8034
8035void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8036 HBasicBlock* block = instruction->GetBlock();
8037 if (block->GetLoopInformation() != nullptr) {
8038 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8039 // The back edge will generate the suspend check.
8040 return;
8041 }
8042 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8043 // The goto will generate the suspend check.
8044 return;
8045 }
8046 GenerateSuspendCheck(instruction, nullptr);
8047}
8048
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008049void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8050 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008052 InvokeRuntimeCallingConvention calling_convention;
8053 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8054}
8055
8056void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008057 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008058 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8059}
8060
8061void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8062 Primitive::Type input_type = conversion->GetInputType();
8063 Primitive::Type result_type = conversion->GetResultType();
8064 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008065 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008066
8067 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8068 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8069 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8070 }
8071
8072 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008073 if (!isR6 &&
8074 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8075 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008076 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008077 }
8078
8079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8080
8081 if (call_kind == LocationSummary::kNoCall) {
8082 if (Primitive::IsFloatingPointType(input_type)) {
8083 locations->SetInAt(0, Location::RequiresFpuRegister());
8084 } else {
8085 locations->SetInAt(0, Location::RequiresRegister());
8086 }
8087
8088 if (Primitive::IsFloatingPointType(result_type)) {
8089 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8090 } else {
8091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8092 }
8093 } else {
8094 InvokeRuntimeCallingConvention calling_convention;
8095
8096 if (Primitive::IsFloatingPointType(input_type)) {
8097 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8098 } else {
8099 DCHECK_EQ(input_type, Primitive::kPrimLong);
8100 locations->SetInAt(0, Location::RegisterPairLocation(
8101 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8102 }
8103
8104 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8105 }
8106}
8107
8108void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8109 LocationSummary* locations = conversion->GetLocations();
8110 Primitive::Type result_type = conversion->GetResultType();
8111 Primitive::Type input_type = conversion->GetInputType();
8112 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008113 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008114
8115 DCHECK_NE(input_type, result_type);
8116
8117 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8118 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8119 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8120 Register src = locations->InAt(0).AsRegister<Register>();
8121
Alexey Frunzea871ef12016-06-27 15:20:11 -07008122 if (dst_low != src) {
8123 __ Move(dst_low, src);
8124 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008125 __ Sra(dst_high, src, 31);
8126 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8127 Register dst = locations->Out().AsRegister<Register>();
8128 Register src = (input_type == Primitive::kPrimLong)
8129 ? locations->InAt(0).AsRegisterPairLow<Register>()
8130 : locations->InAt(0).AsRegister<Register>();
8131
8132 switch (result_type) {
8133 case Primitive::kPrimChar:
8134 __ Andi(dst, src, 0xFFFF);
8135 break;
8136 case Primitive::kPrimByte:
8137 if (has_sign_extension) {
8138 __ Seb(dst, src);
8139 } else {
8140 __ Sll(dst, src, 24);
8141 __ Sra(dst, dst, 24);
8142 }
8143 break;
8144 case Primitive::kPrimShort:
8145 if (has_sign_extension) {
8146 __ Seh(dst, src);
8147 } else {
8148 __ Sll(dst, src, 16);
8149 __ Sra(dst, dst, 16);
8150 }
8151 break;
8152 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008153 if (dst != src) {
8154 __ Move(dst, src);
8155 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008156 break;
8157
8158 default:
8159 LOG(FATAL) << "Unexpected type conversion from " << input_type
8160 << " to " << result_type;
8161 }
8162 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008163 if (input_type == Primitive::kPrimLong) {
8164 if (isR6) {
8165 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8166 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8167 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8168 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8169 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8170 __ Mtc1(src_low, FTMP);
8171 __ Mthc1(src_high, FTMP);
8172 if (result_type == Primitive::kPrimFloat) {
8173 __ Cvtsl(dst, FTMP);
8174 } else {
8175 __ Cvtdl(dst, FTMP);
8176 }
8177 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008178 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8179 : kQuickL2d;
8180 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008181 if (result_type == Primitive::kPrimFloat) {
8182 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8183 } else {
8184 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8185 }
8186 }
8187 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008188 Register src = locations->InAt(0).AsRegister<Register>();
8189 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8190 __ Mtc1(src, FTMP);
8191 if (result_type == Primitive::kPrimFloat) {
8192 __ Cvtsw(dst, FTMP);
8193 } else {
8194 __ Cvtdw(dst, FTMP);
8195 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008196 }
8197 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8198 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008199 if (result_type == Primitive::kPrimLong) {
8200 if (isR6) {
8201 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8202 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8203 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8204 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8205 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8206 MipsLabel truncate;
8207 MipsLabel done;
8208
8209 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8210 // value when the input is either a NaN or is outside of the range of the output type
8211 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8212 // the same result.
8213 //
8214 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8215 // value of the output type if the input is outside of the range after the truncation or
8216 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8217 // results. This matches the desired float/double-to-int/long conversion exactly.
8218 //
8219 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
8220 //
8221 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8222 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8223 // even though it must be NAN2008=1 on R6.
8224 //
8225 // The code takes care of the different behaviors by first comparing the input to the
8226 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8227 // If the input is greater than or equal to the minimum, it procedes to the truncate
8228 // instruction, which will handle such an input the same way irrespective of NAN2008.
8229 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8230 // in order to return either zero or the minimum value.
8231 //
8232 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8233 // truncate instruction for MIPS64R6.
8234 if (input_type == Primitive::kPrimFloat) {
8235 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
8236 __ LoadConst32(TMP, min_val);
8237 __ Mtc1(TMP, FTMP);
8238 __ CmpLeS(FTMP, FTMP, src);
8239 } else {
8240 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
8241 __ LoadConst32(TMP, High32Bits(min_val));
8242 __ Mtc1(ZERO, FTMP);
8243 __ Mthc1(TMP, FTMP);
8244 __ CmpLeD(FTMP, FTMP, src);
8245 }
8246
8247 __ Bc1nez(FTMP, &truncate);
8248
8249 if (input_type == Primitive::kPrimFloat) {
8250 __ CmpEqS(FTMP, src, src);
8251 } else {
8252 __ CmpEqD(FTMP, src, src);
8253 }
8254 __ Move(dst_low, ZERO);
8255 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
8256 __ Mfc1(TMP, FTMP);
8257 __ And(dst_high, dst_high, TMP);
8258
8259 __ B(&done);
8260
8261 __ Bind(&truncate);
8262
8263 if (input_type == Primitive::kPrimFloat) {
8264 __ TruncLS(FTMP, src);
8265 } else {
8266 __ TruncLD(FTMP, src);
8267 }
8268 __ Mfc1(dst_low, FTMP);
8269 __ Mfhc1(dst_high, FTMP);
8270
8271 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008272 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008273 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8274 : kQuickD2l;
8275 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008276 if (input_type == Primitive::kPrimFloat) {
8277 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8278 } else {
8279 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8280 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008281 }
8282 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008283 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8284 Register dst = locations->Out().AsRegister<Register>();
8285 MipsLabel truncate;
8286 MipsLabel done;
8287
8288 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
8289 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
8290 // even though it must be NAN2008=1 on R6.
8291 //
8292 // For details see the large comment above for the truncation of float/double to long on R6.
8293 //
8294 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
8295 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008296 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008297 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8298 __ LoadConst32(TMP, min_val);
8299 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008300 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008301 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8302 __ LoadConst32(TMP, High32Bits(min_val));
8303 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07008304 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008305 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008306
8307 if (isR6) {
8308 if (input_type == Primitive::kPrimFloat) {
8309 __ CmpLeS(FTMP, FTMP, src);
8310 } else {
8311 __ CmpLeD(FTMP, FTMP, src);
8312 }
8313 __ Bc1nez(FTMP, &truncate);
8314
8315 if (input_type == Primitive::kPrimFloat) {
8316 __ CmpEqS(FTMP, src, src);
8317 } else {
8318 __ CmpEqD(FTMP, src, src);
8319 }
8320 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8321 __ Mfc1(TMP, FTMP);
8322 __ And(dst, dst, TMP);
8323 } else {
8324 if (input_type == Primitive::kPrimFloat) {
8325 __ ColeS(0, FTMP, src);
8326 } else {
8327 __ ColeD(0, FTMP, src);
8328 }
8329 __ Bc1t(0, &truncate);
8330
8331 if (input_type == Primitive::kPrimFloat) {
8332 __ CeqS(0, src, src);
8333 } else {
8334 __ CeqD(0, src, src);
8335 }
8336 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8337 __ Movf(dst, ZERO, 0);
8338 }
8339
8340 __ B(&done);
8341
8342 __ Bind(&truncate);
8343
8344 if (input_type == Primitive::kPrimFloat) {
8345 __ TruncWS(FTMP, src);
8346 } else {
8347 __ TruncWD(FTMP, src);
8348 }
8349 __ Mfc1(dst, FTMP);
8350
8351 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008352 }
8353 } else if (Primitive::IsFloatingPointType(result_type) &&
8354 Primitive::IsFloatingPointType(input_type)) {
8355 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8356 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8357 if (result_type == Primitive::kPrimFloat) {
8358 __ Cvtsd(dst, src);
8359 } else {
8360 __ Cvtds(dst, src);
8361 }
8362 } else {
8363 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8364 << " to " << result_type;
8365 }
8366}
8367
8368void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8369 HandleShift(ushr);
8370}
8371
8372void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8373 HandleShift(ushr);
8374}
8375
8376void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8377 HandleBinaryOp(instruction);
8378}
8379
8380void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8381 HandleBinaryOp(instruction);
8382}
8383
8384void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8385 // Nothing to do, this should be removed during prepare for register allocator.
8386 LOG(FATAL) << "Unreachable";
8387}
8388
8389void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8390 // Nothing to do, this should be removed during prepare for register allocator.
8391 LOG(FATAL) << "Unreachable";
8392}
8393
8394void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008395 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008396}
8397
8398void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008399 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008400}
8401
8402void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008403 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008404}
8405
8406void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008407 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008408}
8409
8410void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008411 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008412}
8413
8414void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008415 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008416}
8417
8418void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008419 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008420}
8421
8422void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008423 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008424}
8425
8426void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008427 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008428}
8429
8430void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008431 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008432}
8433
8434void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008435 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008436}
8437
8438void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008439 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008440}
8441
8442void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008443 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008444}
8445
8446void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008447 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008448}
8449
8450void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008451 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008452}
8453
8454void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008455 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008456}
8457
8458void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008459 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008460}
8461
8462void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008463 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008464}
8465
8466void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008467 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008468}
8469
8470void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008471 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008472}
8473
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008474void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8475 LocationSummary* locations =
8476 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8477 locations->SetInAt(0, Location::RequiresRegister());
8478}
8479
Alexey Frunze96b66822016-09-10 02:32:44 -07008480void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8481 int32_t lower_bound,
8482 uint32_t num_entries,
8483 HBasicBlock* switch_block,
8484 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008486 Register temp_reg = TMP;
8487 __ Addiu32(temp_reg, value_reg, -lower_bound);
8488 // Jump to default if index is negative
8489 // Note: We don't check the case that index is positive while value < lower_bound, because in
8490 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8491 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8492
Alexey Frunze96b66822016-09-10 02:32:44 -07008493 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008494 // Jump to successors[0] if value == lower_bound.
8495 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8496 int32_t last_index = 0;
8497 for (; num_entries - last_index > 2; last_index += 2) {
8498 __ Addiu(temp_reg, temp_reg, -2);
8499 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8500 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8501 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8502 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8503 }
8504 if (num_entries - last_index == 2) {
8505 // The last missing case_value.
8506 __ Addiu(temp_reg, temp_reg, -1);
8507 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008508 }
8509
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008510 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008511 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008512 __ B(codegen_->GetLabelOf(default_block));
8513 }
8514}
8515
Alexey Frunze96b66822016-09-10 02:32:44 -07008516void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8517 Register constant_area,
8518 int32_t lower_bound,
8519 uint32_t num_entries,
8520 HBasicBlock* switch_block,
8521 HBasicBlock* default_block) {
8522 // Create a jump table.
8523 std::vector<MipsLabel*> labels(num_entries);
8524 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8525 for (uint32_t i = 0; i < num_entries; i++) {
8526 labels[i] = codegen_->GetLabelOf(successors[i]);
8527 }
8528 JumpTable* table = __ CreateJumpTable(std::move(labels));
8529
8530 // Is the value in range?
8531 __ Addiu32(TMP, value_reg, -lower_bound);
8532 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8533 __ Sltiu(AT, TMP, num_entries);
8534 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8535 } else {
8536 __ LoadConst32(AT, num_entries);
8537 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8538 }
8539
8540 // We are in the range of the table.
8541 // Load the target address from the jump table, indexing by the value.
8542 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008543 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008544 __ Lw(TMP, TMP, 0);
8545 // Compute the absolute target address by adding the table start address
8546 // (the table contains offsets to targets relative to its start).
8547 __ Addu(TMP, TMP, AT);
8548 // And jump.
8549 __ Jr(TMP);
8550 __ NopIfNoReordering();
8551}
8552
8553void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8554 int32_t lower_bound = switch_instr->GetStartValue();
8555 uint32_t num_entries = switch_instr->GetNumEntries();
8556 LocationSummary* locations = switch_instr->GetLocations();
8557 Register value_reg = locations->InAt(0).AsRegister<Register>();
8558 HBasicBlock* switch_block = switch_instr->GetBlock();
8559 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8560
8561 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8562 num_entries > kPackedSwitchJumpTableThreshold) {
8563 // R6 uses PC-relative addressing to access the jump table.
8564 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8565 // the jump table and it is implemented by changing HPackedSwitch to
8566 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8567 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8568 GenTableBasedPackedSwitch(value_reg,
8569 ZERO,
8570 lower_bound,
8571 num_entries,
8572 switch_block,
8573 default_block);
8574 } else {
8575 GenPackedSwitchWithCompares(value_reg,
8576 lower_bound,
8577 num_entries,
8578 switch_block,
8579 default_block);
8580 }
8581}
8582
8583void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8584 LocationSummary* locations =
8585 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8586 locations->SetInAt(0, Location::RequiresRegister());
8587 // Constant area pointer (HMipsComputeBaseMethodAddress).
8588 locations->SetInAt(1, Location::RequiresRegister());
8589}
8590
8591void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8592 int32_t lower_bound = switch_instr->GetStartValue();
8593 uint32_t num_entries = switch_instr->GetNumEntries();
8594 LocationSummary* locations = switch_instr->GetLocations();
8595 Register value_reg = locations->InAt(0).AsRegister<Register>();
8596 Register constant_area = locations->InAt(1).AsRegister<Register>();
8597 HBasicBlock* switch_block = switch_instr->GetBlock();
8598 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8599
8600 // This is an R2-only path. HPackedSwitch has been changed to
8601 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8602 // required to address the jump table relative to PC.
8603 GenTableBasedPackedSwitch(value_reg,
8604 constant_area,
8605 lower_bound,
8606 num_entries,
8607 switch_block,
8608 default_block);
8609}
8610
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008611void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8612 HMipsComputeBaseMethodAddress* insn) {
8613 LocationSummary* locations =
8614 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8615 locations->SetOut(Location::RequiresRegister());
8616}
8617
8618void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8619 HMipsComputeBaseMethodAddress* insn) {
8620 LocationSummary* locations = insn->GetLocations();
8621 Register reg = locations->Out().AsRegister<Register>();
8622
8623 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8624
8625 // Generate a dummy PC-relative call to obtain PC.
8626 __ Nal();
8627 // Grab the return address off RA.
8628 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008629 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008630
8631 // Remember this offset (the obtained PC value) for later use with constant area.
8632 __ BindPcRelBaseLabel();
8633}
8634
8635void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8636 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8637 locations->SetOut(Location::RequiresRegister());
8638}
8639
8640void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8641 Register reg = base->GetLocations()->Out().AsRegister<Register>();
8642 CodeGeneratorMIPS::PcRelativePatchInfo* info =
8643 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008644 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8645 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008646 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008647 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
8648 __ Addiu(reg, reg, /* placeholder */ 0x5678);
8649 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008650}
8651
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008652void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8653 // The trampoline uses the same calling convention as dex calling conventions,
8654 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8655 // the method_idx.
8656 HandleInvoke(invoke);
8657}
8658
8659void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8660 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8661}
8662
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008663void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8664 LocationSummary* locations =
8665 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8666 locations->SetInAt(0, Location::RequiresRegister());
8667 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008668}
8669
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008670void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8671 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008672 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008673 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008674 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008675 __ LoadFromOffset(kLoadWord,
8676 locations->Out().AsRegister<Register>(),
8677 locations->InAt(0).AsRegister<Register>(),
8678 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008679 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008680 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008681 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008682 __ LoadFromOffset(kLoadWord,
8683 locations->Out().AsRegister<Register>(),
8684 locations->InAt(0).AsRegister<Register>(),
8685 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008686 __ LoadFromOffset(kLoadWord,
8687 locations->Out().AsRegister<Register>(),
8688 locations->Out().AsRegister<Register>(),
8689 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008690 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008691}
8692
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008693#undef __
8694#undef QUICK_ENTRY_POINT
8695
8696} // namespace mips
8697} // namespace art