blob: 503026e39910da6143aff70c589bad83e5cbeb91 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800102 Register reg = calling_convention.GetRegisterAt(gp_index);
103 if (reg == A1 || reg == A3) {
104 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200105 gp_index++;
106 }
107 Register low_even = calling_convention.GetRegisterAt(gp_index);
108 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
109 DCHECK_EQ(low_even + 1, high_odd);
110 next_location = Location::RegisterPairLocation(low_even, high_odd);
111 } else {
112 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
113 next_location = Location::DoubleStackSlot(stack_offset);
114 }
115 break;
116 }
117
118 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
119 // will take up the even/odd pair, while floats are stored in even regs only.
120 // On 64 bit FPU, both double and float are stored in even registers only.
121 case Primitive::kPrimFloat:
122 case Primitive::kPrimDouble: {
123 uint32_t float_index = float_index_++;
124 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
125 next_location = Location::FpuRegisterLocation(
126 calling_convention.GetFpuRegisterAt(float_index));
127 } else {
128 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
129 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
130 : Location::StackSlot(stack_offset);
131 }
132 break;
133 }
134
135 case Primitive::kPrimVoid:
136 LOG(FATAL) << "Unexpected parameter type " << type;
137 break;
138 }
139
140 // Space on the stack is reserved for all arguments.
141 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
142
143 return next_location;
144}
145
146Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
147 return MipsReturnLocation(type);
148}
149
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100150// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
151#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700152#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200153
154class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
155 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000156 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 LocationSummary* locations = instruction_->GetLocations();
160 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
161 __ Bind(GetEntryLabel());
162 if (instruction_->CanThrowIntoCatchBlock()) {
163 // Live registers will be restored in the catch block if caught.
164 SaveLiveRegisters(codegen, instruction_->GetLocations());
165 }
166 // We're moving two locations to locations that could overlap, so we need a parallel
167 // move resolver.
168 InvokeRuntimeCallingConvention calling_convention;
169 codegen->EmitParallelMoves(locations->InAt(0),
170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
171 Primitive::kPrimInt,
172 locations->InAt(1),
173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100175 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
176 ? kQuickThrowStringBounds
177 : kQuickThrowArrayBounds;
178 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100179 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
181 }
182
183 bool IsFatal() const OVERRIDE { return true; }
184
185 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
186
187 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
189};
190
191class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
192 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000193 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200194
195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
196 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
197 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100198 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200199 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
200 }
201
202 bool IsFatal() const OVERRIDE { return true; }
203
204 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
205
206 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
208};
209
210class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
211 public:
212 LoadClassSlowPathMIPS(HLoadClass* cls,
213 HInstruction* at,
214 uint32_t dex_pc,
215 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000216 : SlowPathCodeMIPS(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
218 }
219
220 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000221 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700222 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200223 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700224 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
225 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
226 InvokeRuntimeCallingConvention calling_convention;
227 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
228 const bool is_load_class_bss_entry =
229 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230 __ Bind(GetEntryLabel());
231 SaveLiveRegisters(codegen, locations);
232
Alexey Frunzec61c0762017-04-10 13:54:23 -0700233 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
234 Register entry_address = kNoRegister;
235 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
236 Register temp = locations->GetTemp(0).AsRegister<Register>();
237 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
238 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
239 // kSaveEverything call.
240 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
241 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
242 if (temp_is_a0) {
243 __ Move(entry_address, temp);
244 }
245 }
246
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000247 dex::TypeIndex type_index = cls_->GetTypeIndex();
248 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100249 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
250 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000251 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200252 if (do_clinit_) {
253 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
254 } else {
255 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
256 }
257
Alexey Frunzec61c0762017-04-10 13:54:23 -0700258 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
259 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
260 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
261 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
262 }
263
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200264 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200265 if (out.IsValid()) {
266 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000267 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700268 mips_codegen->MoveLocation(out,
269 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
270 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200271 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700273
274 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
275 if (is_load_class_bss_entry && !r2_baker_or_no_read_barriers) {
276 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
277 // the class entry.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000278 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000280 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800281 bool reordering = __ SetReorder(false);
282 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
283 __ StoreToOffset(kStoreWord, out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
284 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000285 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 __ B(GetExitLabel());
287 }
288
289 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
290
291 private:
292 // The class this slow path will load.
293 HLoadClass* const cls_;
294
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200295 // The dex PC of `at_`.
296 const uint32_t dex_pc_;
297
298 // Whether to initialize the class.
299 const bool do_clinit_;
300
301 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
302};
303
304class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
305 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000306 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307
308 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700309 DCHECK(instruction_->IsLoadString());
310 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200311 LocationSummary* locations = instruction_->GetLocations();
312 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700313 HLoadString* load = instruction_->AsLoadString();
314 const dex::StringIndex string_index = load->GetStringIndex();
315 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700317 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
318 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
319 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200320 __ Bind(GetEntryLabel());
321 SaveLiveRegisters(codegen, locations);
322
Alexey Frunzec61c0762017-04-10 13:54:23 -0700323 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
324 Register entry_address = kNoRegister;
325 if (r2_baker_or_no_read_barriers) {
326 Register temp = locations->GetTemp(0).AsRegister<Register>();
327 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
328 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
329 // kSaveEverything call.
330 entry_address = temp_is_a0 ? out : temp;
331 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
332 if (temp_is_a0) {
333 __ Move(entry_address, temp);
334 }
335 }
336
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000337 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100338 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700340
341 // Store the resolved string to the BSS entry.
342 if (r2_baker_or_no_read_barriers) {
343 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
344 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
345 }
346
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 Primitive::Type type = instruction_->GetType();
348 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700349 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000352
Alexey Frunzec61c0762017-04-10 13:54:23 -0700353 // Store the resolved string to the BSS entry.
354 if (!r2_baker_or_no_read_barriers) {
355 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
356 // the string entry.
357 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
358 CodeGeneratorMIPS::PcRelativePatchInfo* info =
359 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
360 bool reordering = __ SetReorder(false);
361 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
362 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
363 __ SetReorder(reordering);
364 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200365 __ B(GetExitLabel());
366 }
367
368 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
369
370 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200371 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
372};
373
374class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
375 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000376 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377
378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
379 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
380 __ Bind(GetEntryLabel());
381 if (instruction_->CanThrowIntoCatchBlock()) {
382 // Live registers will be restored in the catch block if caught.
383 SaveLiveRegisters(codegen, instruction_->GetLocations());
384 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100385 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200386 instruction_,
387 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100388 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
390 }
391
392 bool IsFatal() const OVERRIDE { return true; }
393
394 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
395
396 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
398};
399
400class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
401 public:
402 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
407 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100408 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 if (successor_ == nullptr) {
411 __ B(GetReturnLabel());
412 } else {
413 __ B(mips_codegen->GetLabelOf(successor_));
414 }
415 }
416
417 MipsLabel* GetReturnLabel() {
418 DCHECK(successor_ == nullptr);
419 return &return_label_;
420 }
421
422 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
423
424 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425 // If not null, the block to branch to after the suspend check.
426 HBasicBlock* const successor_;
427
428 // If `successor_` is null, the label to branch to after the suspend check.
429 MipsLabel return_label_;
430
431 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
432};
433
434class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
435 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800436 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
437 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438
439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
440 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441 uint32_t dex_pc = instruction_->GetDexPc();
442 DCHECK(instruction_->IsCheckCast()
443 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
444 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
445
446 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800447 if (!is_fatal_) {
448 SaveLiveRegisters(codegen, locations);
449 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450
451 // We're moving two locations to locations that could overlap, so we need a parallel
452 // move resolver.
453 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800454 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
456 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800457 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200458 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
459 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100461 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800462 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200463 Primitive::Type ret_type = instruction_->GetType();
464 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
465 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200466 } else {
467 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800468 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
469 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200470 }
471
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800472 if (!is_fatal_) {
473 RestoreLiveRegisters(codegen, locations);
474 __ B(GetExitLabel());
475 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200476 }
477
478 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
479
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800480 bool IsFatal() const OVERRIDE { return is_fatal_; }
481
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800483 const bool is_fatal_;
484
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
486};
487
488class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
489 public:
Aart Bik42249c32016-01-07 15:33:50 -0800490 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000491 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200492
493 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800494 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200495 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100496 LocationSummary* locations = instruction_->GetLocations();
497 SaveLiveRegisters(codegen, locations);
498 InvokeRuntimeCallingConvention calling_convention;
499 __ LoadConst32(calling_convention.GetRegisterAt(0),
500 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100501 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100502 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200503 }
504
505 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
506
507 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200508 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
509};
510
Alexey Frunze15958152017-02-09 19:08:30 -0800511class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
512 public:
513 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
516 LocationSummary* locations = instruction_->GetLocations();
517 __ Bind(GetEntryLabel());
518 SaveLiveRegisters(codegen, locations);
519
520 InvokeRuntimeCallingConvention calling_convention;
521 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
522 parallel_move.AddMove(
523 locations->InAt(0),
524 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
525 Primitive::kPrimNot,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(1),
529 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
530 Primitive::kPrimInt,
531 nullptr);
532 parallel_move.AddMove(
533 locations->InAt(2),
534 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
535 Primitive::kPrimNot,
536 nullptr);
537 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
538
539 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
540 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
541 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
542 RestoreLiveRegisters(codegen, locations);
543 __ B(GetExitLabel());
544 }
545
546 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
547
548 private:
549 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
550};
551
552// Slow path marking an object reference `ref` during a read
553// barrier. The field `obj.field` in the object `obj` holding this
554// reference does not get updated by this slow path after marking (see
555// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
556//
557// This means that after the execution of this slow path, `ref` will
558// always be up-to-date, but `obj.field` may not; i.e., after the
559// flip, `ref` will be a to-space reference, but `obj.field` will
560// probably still be a from-space reference (unless it gets updated by
561// another thread, or if another thread installed another object
562// reference (different from `ref`) in `obj.field`).
563//
564// If `entrypoint` is a valid location it is assumed to already be
565// holding the entrypoint. The case where the entrypoint is passed in
566// is for the GcRoot read barrier.
567class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
568 public:
569 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
570 Location ref,
571 Location entrypoint = Location::NoLocation())
572 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
573 DCHECK(kEmitCompilerReadBarrier);
574 }
575
576 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
577
578 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
579 LocationSummary* locations = instruction_->GetLocations();
580 Register ref_reg = ref_.AsRegister<Register>();
581 DCHECK(locations->CanCall());
582 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
583 DCHECK(instruction_->IsInstanceFieldGet() ||
584 instruction_->IsStaticFieldGet() ||
585 instruction_->IsArrayGet() ||
586 instruction_->IsArraySet() ||
587 instruction_->IsLoadClass() ||
588 instruction_->IsLoadString() ||
589 instruction_->IsInstanceOf() ||
590 instruction_->IsCheckCast() ||
591 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
592 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
593 << "Unexpected instruction in read barrier marking slow path: "
594 << instruction_->DebugName();
595
596 __ Bind(GetEntryLabel());
597 // No need to save live registers; it's taken care of by the
598 // entrypoint. Also, there is no need to update the stack mask,
599 // as this runtime call will not trigger a garbage collection.
600 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
601 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
602 (S2 <= ref_reg && ref_reg <= S7) ||
603 (ref_reg == FP)) << ref_reg;
604 // "Compact" slow path, saving two moves.
605 //
606 // Instead of using the standard runtime calling convention (input
607 // and output in A0 and V0 respectively):
608 //
609 // A0 <- ref
610 // V0 <- ReadBarrierMark(A0)
611 // ref <- V0
612 //
613 // we just use rX (the register containing `ref`) as input and output
614 // of a dedicated entrypoint:
615 //
616 // rX <- ReadBarrierMarkRegX(rX)
617 //
618 if (entrypoint_.IsValid()) {
619 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
620 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
621 __ Jalr(entrypoint_.AsRegister<Register>());
622 __ NopIfNoReordering();
623 } else {
624 int32_t entry_point_offset =
625 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
626 // This runtime call does not require a stack map.
627 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
628 instruction_,
629 this,
630 /* direct */ false);
631 }
632 __ B(GetExitLabel());
633 }
634
635 private:
636 // The location (register) of the marked object reference.
637 const Location ref_;
638
639 // The location of the entrypoint if already loaded.
640 const Location entrypoint_;
641
642 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
643};
644
645// Slow path marking an object reference `ref` during a read barrier,
646// and if needed, atomically updating the field `obj.field` in the
647// object `obj` holding this reference after marking (contrary to
648// ReadBarrierMarkSlowPathMIPS above, which never tries to update
649// `obj.field`).
650//
651// This means that after the execution of this slow path, both `ref`
652// and `obj.field` will be up-to-date; i.e., after the flip, both will
653// hold the same to-space reference (unless another thread installed
654// another object reference (different from `ref`) in `obj.field`).
655class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
656 public:
657 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
658 Location ref,
659 Register obj,
660 Location field_offset,
661 Register temp1)
662 : SlowPathCodeMIPS(instruction),
663 ref_(ref),
664 obj_(obj),
665 field_offset_(field_offset),
666 temp1_(temp1) {
667 DCHECK(kEmitCompilerReadBarrier);
668 }
669
670 const char* GetDescription() const OVERRIDE {
671 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
672 }
673
674 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
675 LocationSummary* locations = instruction_->GetLocations();
676 Register ref_reg = ref_.AsRegister<Register>();
677 DCHECK(locations->CanCall());
678 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
679 // This slow path is only used by the UnsafeCASObject intrinsic.
680 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
681 << "Unexpected instruction in read barrier marking and field updating slow path: "
682 << instruction_->DebugName();
683 DCHECK(instruction_->GetLocations()->Intrinsified());
684 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
685 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
686
687 __ Bind(GetEntryLabel());
688
689 // Save the old reference.
690 // Note that we cannot use AT or TMP to save the old reference, as those
691 // are used by the code that follows, but we need the old reference after
692 // the call to the ReadBarrierMarkRegX entry point.
693 DCHECK_NE(temp1_, AT);
694 DCHECK_NE(temp1_, TMP);
695 __ Move(temp1_, ref_reg);
696
697 // No need to save live registers; it's taken care of by the
698 // entrypoint. Also, there is no need to update the stack mask,
699 // as this runtime call will not trigger a garbage collection.
700 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
701 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
702 (S2 <= ref_reg && ref_reg <= S7) ||
703 (ref_reg == FP)) << ref_reg;
704 // "Compact" slow path, saving two moves.
705 //
706 // Instead of using the standard runtime calling convention (input
707 // and output in A0 and V0 respectively):
708 //
709 // A0 <- ref
710 // V0 <- ReadBarrierMark(A0)
711 // ref <- V0
712 //
713 // we just use rX (the register containing `ref`) as input and output
714 // of a dedicated entrypoint:
715 //
716 // rX <- ReadBarrierMarkRegX(rX)
717 //
718 int32_t entry_point_offset =
719 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
720 // This runtime call does not require a stack map.
721 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
722 instruction_,
723 this,
724 /* direct */ false);
725
726 // If the new reference is different from the old reference,
727 // update the field in the holder (`*(obj_ + field_offset_)`).
728 //
729 // Note that this field could also hold a different object, if
730 // another thread had concurrently changed it. In that case, the
731 // the compare-and-set (CAS) loop below would abort, leaving the
732 // field as-is.
733 MipsLabel done;
734 __ Beq(temp1_, ref_reg, &done);
735
736 // Update the the holder's field atomically. This may fail if
737 // mutator updates before us, but it's OK. This is achieved
738 // using a strong compare-and-set (CAS) operation with relaxed
739 // memory synchronization ordering, where the expected value is
740 // the old reference and the desired value is the new reference.
741
742 // Convenience aliases.
743 Register base = obj_;
744 // The UnsafeCASObject intrinsic uses a register pair as field
745 // offset ("long offset"), of which only the low part contains
746 // data.
747 Register offset = field_offset_.AsRegisterPairLow<Register>();
748 Register expected = temp1_;
749 Register value = ref_reg;
750 Register tmp_ptr = TMP; // Pointer to actual memory.
751 Register tmp = AT; // Value in memory.
752
753 __ Addu(tmp_ptr, base, offset);
754
755 if (kPoisonHeapReferences) {
756 __ PoisonHeapReference(expected);
757 // Do not poison `value` if it is the same register as
758 // `expected`, which has just been poisoned.
759 if (value != expected) {
760 __ PoisonHeapReference(value);
761 }
762 }
763
764 // do {
765 // tmp = [r_ptr] - expected;
766 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
767
768 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
769 MipsLabel loop_head, exit_loop;
770 __ Bind(&loop_head);
771 if (is_r6) {
772 __ LlR6(tmp, tmp_ptr);
773 } else {
774 __ LlR2(tmp, tmp_ptr);
775 }
776 __ Bne(tmp, expected, &exit_loop);
777 __ Move(tmp, value);
778 if (is_r6) {
779 __ ScR6(tmp, tmp_ptr);
780 } else {
781 __ ScR2(tmp, tmp_ptr);
782 }
783 __ Beqz(tmp, &loop_head);
784 __ Bind(&exit_loop);
785
786 if (kPoisonHeapReferences) {
787 __ UnpoisonHeapReference(expected);
788 // Do not unpoison `value` if it is the same register as
789 // `expected`, which has just been unpoisoned.
790 if (value != expected) {
791 __ UnpoisonHeapReference(value);
792 }
793 }
794
795 __ Bind(&done);
796 __ B(GetExitLabel());
797 }
798
799 private:
800 // The location (register) of the marked object reference.
801 const Location ref_;
802 // The register containing the object holding the marked object reference field.
803 const Register obj_;
804 // The location of the offset of the marked reference field within `obj_`.
805 Location field_offset_;
806
807 const Register temp1_;
808
809 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
810};
811
812// Slow path generating a read barrier for a heap reference.
813class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
814 public:
815 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
816 Location out,
817 Location ref,
818 Location obj,
819 uint32_t offset,
820 Location index)
821 : SlowPathCodeMIPS(instruction),
822 out_(out),
823 ref_(ref),
824 obj_(obj),
825 offset_(offset),
826 index_(index) {
827 DCHECK(kEmitCompilerReadBarrier);
828 // If `obj` is equal to `out` or `ref`, it means the initial object
829 // has been overwritten by (or after) the heap object reference load
830 // to be instrumented, e.g.:
831 //
832 // __ LoadFromOffset(kLoadWord, out, out, offset);
833 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
834 //
835 // In that case, we have lost the information about the original
836 // object, and the emitted read barrier cannot work properly.
837 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
838 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
839 }
840
841 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
842 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
843 LocationSummary* locations = instruction_->GetLocations();
844 Register reg_out = out_.AsRegister<Register>();
845 DCHECK(locations->CanCall());
846 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
847 DCHECK(instruction_->IsInstanceFieldGet() ||
848 instruction_->IsStaticFieldGet() ||
849 instruction_->IsArrayGet() ||
850 instruction_->IsInstanceOf() ||
851 instruction_->IsCheckCast() ||
852 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
853 << "Unexpected instruction in read barrier for heap reference slow path: "
854 << instruction_->DebugName();
855
856 __ Bind(GetEntryLabel());
857 SaveLiveRegisters(codegen, locations);
858
859 // We may have to change the index's value, but as `index_` is a
860 // constant member (like other "inputs" of this slow path),
861 // introduce a copy of it, `index`.
862 Location index = index_;
863 if (index_.IsValid()) {
864 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
865 if (instruction_->IsArrayGet()) {
866 // Compute the actual memory offset and store it in `index`.
867 Register index_reg = index_.AsRegister<Register>();
868 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
869 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
870 // We are about to change the value of `index_reg` (see the
871 // calls to art::mips::MipsAssembler::Sll and
872 // art::mips::MipsAssembler::Addiu32 below), but it has
873 // not been saved by the previous call to
874 // art::SlowPathCode::SaveLiveRegisters, as it is a
875 // callee-save register --
876 // art::SlowPathCode::SaveLiveRegisters does not consider
877 // callee-save registers, as it has been designed with the
878 // assumption that callee-save registers are supposed to be
879 // handled by the called function. So, as a callee-save
880 // register, `index_reg` _would_ eventually be saved onto
881 // the stack, but it would be too late: we would have
882 // changed its value earlier. Therefore, we manually save
883 // it here into another freely available register,
884 // `free_reg`, chosen of course among the caller-save
885 // registers (as a callee-save `free_reg` register would
886 // exhibit the same problem).
887 //
888 // Note we could have requested a temporary register from
889 // the register allocator instead; but we prefer not to, as
890 // this is a slow path, and we know we can find a
891 // caller-save register that is available.
892 Register free_reg = FindAvailableCallerSaveRegister(codegen);
893 __ Move(free_reg, index_reg);
894 index_reg = free_reg;
895 index = Location::RegisterLocation(index_reg);
896 } else {
897 // The initial register stored in `index_` has already been
898 // saved in the call to art::SlowPathCode::SaveLiveRegisters
899 // (as it is not a callee-save register), so we can freely
900 // use it.
901 }
902 // Shifting the index value contained in `index_reg` by the scale
903 // factor (2) cannot overflow in practice, as the runtime is
904 // unable to allocate object arrays with a size larger than
905 // 2^26 - 1 (that is, 2^28 - 4 bytes).
906 __ Sll(index_reg, index_reg, TIMES_4);
907 static_assert(
908 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
909 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
910 __ Addiu32(index_reg, index_reg, offset_);
911 } else {
912 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
913 // intrinsics, `index_` is not shifted by a scale factor of 2
914 // (as in the case of ArrayGet), as it is actually an offset
915 // to an object field within an object.
916 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
917 DCHECK(instruction_->GetLocations()->Intrinsified());
918 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
919 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
920 << instruction_->AsInvoke()->GetIntrinsic();
921 DCHECK_EQ(offset_, 0U);
922 DCHECK(index_.IsRegisterPair());
923 // UnsafeGet's offset location is a register pair, the low
924 // part contains the correct offset.
925 index = index_.ToLow();
926 }
927 }
928
929 // We're moving two or three locations to locations that could
930 // overlap, so we need a parallel move resolver.
931 InvokeRuntimeCallingConvention calling_convention;
932 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
933 parallel_move.AddMove(ref_,
934 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
935 Primitive::kPrimNot,
936 nullptr);
937 parallel_move.AddMove(obj_,
938 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
939 Primitive::kPrimNot,
940 nullptr);
941 if (index.IsValid()) {
942 parallel_move.AddMove(index,
943 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
944 Primitive::kPrimInt,
945 nullptr);
946 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
947 } else {
948 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
949 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
950 }
951 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
952 instruction_,
953 instruction_->GetDexPc(),
954 this);
955 CheckEntrypointTypes<
956 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
957 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
958
959 RestoreLiveRegisters(codegen, locations);
960 __ B(GetExitLabel());
961 }
962
963 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
964
965 private:
966 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
967 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
968 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
969 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
970 if (i != ref &&
971 i != obj &&
972 !codegen->IsCoreCalleeSaveRegister(i) &&
973 !codegen->IsBlockedCoreRegister(i)) {
974 return static_cast<Register>(i);
975 }
976 }
977 // We shall never fail to find a free caller-save register, as
978 // there are more than two core caller-save registers on MIPS
979 // (meaning it is possible to find one which is different from
980 // `ref` and `obj`).
981 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
982 LOG(FATAL) << "Could not find a free caller-save register";
983 UNREACHABLE();
984 }
985
986 const Location out_;
987 const Location ref_;
988 const Location obj_;
989 const uint32_t offset_;
990 // An additional location containing an index to an array.
991 // Only used for HArrayGet and the UnsafeGetObject &
992 // UnsafeGetObjectVolatile intrinsics.
993 const Location index_;
994
995 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
996};
997
998// Slow path generating a read barrier for a GC root.
999class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1000 public:
1001 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1002 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1003 DCHECK(kEmitCompilerReadBarrier);
1004 }
1005
1006 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1007 LocationSummary* locations = instruction_->GetLocations();
1008 Register reg_out = out_.AsRegister<Register>();
1009 DCHECK(locations->CanCall());
1010 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1011 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1012 << "Unexpected instruction in read barrier for GC root slow path: "
1013 << instruction_->DebugName();
1014
1015 __ Bind(GetEntryLabel());
1016 SaveLiveRegisters(codegen, locations);
1017
1018 InvokeRuntimeCallingConvention calling_convention;
1019 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
1020 mips_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
1021 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1022 instruction_,
1023 instruction_->GetDexPc(),
1024 this);
1025 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1026 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
1027
1028 RestoreLiveRegisters(codegen, locations);
1029 __ B(GetExitLabel());
1030 }
1031
1032 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1033
1034 private:
1035 const Location out_;
1036 const Location root_;
1037
1038 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1039};
1040
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001041CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1042 const MipsInstructionSetFeatures& isa_features,
1043 const CompilerOptions& compiler_options,
1044 OptimizingCompilerStats* stats)
1045 : CodeGenerator(graph,
1046 kNumberOfCoreRegisters,
1047 kNumberOfFRegisters,
1048 kNumberOfRegisterPairs,
1049 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1050 arraysize(kCoreCalleeSaves)),
1051 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1052 arraysize(kFpuCalleeSaves)),
1053 compiler_options,
1054 stats),
1055 block_labels_(nullptr),
1056 location_builder_(graph, this),
1057 instruction_visitor_(graph, this),
1058 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001059 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001060 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001061 uint32_literals_(std::less<uint32_t>(),
1062 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001063 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1064 boot_image_string_patches_(StringReferenceValueComparator(),
1065 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1066 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1067 boot_image_type_patches_(TypeReferenceValueComparator(),
1068 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1069 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001070 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001071 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1072 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001073 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001074 // Save RA (containing the return address) to mimic Quick.
1075 AddAllocatedRegister(Location::RegisterLocation(RA));
1076}
1077
1078#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001079// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1080#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001081#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001082
1083void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1084 // Ensure that we fix up branches.
1085 __ FinalizeCode();
1086
1087 // Adjust native pc offsets in stack maps.
1088 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001089 uint32_t old_position =
1090 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001091 uint32_t new_position = __ GetAdjustedPosition(old_position);
1092 DCHECK_GE(new_position, old_position);
1093 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1094 }
1095
1096 // Adjust pc offsets for the disassembly information.
1097 if (disasm_info_ != nullptr) {
1098 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1099 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1100 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1101 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1102 it.second.start = __ GetAdjustedPosition(it.second.start);
1103 it.second.end = __ GetAdjustedPosition(it.second.end);
1104 }
1105 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1106 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1107 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1108 }
1109 }
1110
1111 CodeGenerator::Finalize(allocator);
1112}
1113
1114MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1115 return codegen_->GetAssembler();
1116}
1117
1118void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1119 DCHECK_LT(index, moves_.size());
1120 MoveOperands* move = moves_[index];
1121 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1122}
1123
1124void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1125 DCHECK_LT(index, moves_.size());
1126 MoveOperands* move = moves_[index];
1127 Primitive::Type type = move->GetType();
1128 Location loc1 = move->GetDestination();
1129 Location loc2 = move->GetSource();
1130
1131 DCHECK(!loc1.IsConstant());
1132 DCHECK(!loc2.IsConstant());
1133
1134 if (loc1.Equals(loc2)) {
1135 return;
1136 }
1137
1138 if (loc1.IsRegister() && loc2.IsRegister()) {
1139 // Swap 2 GPRs.
1140 Register r1 = loc1.AsRegister<Register>();
1141 Register r2 = loc2.AsRegister<Register>();
1142 __ Move(TMP, r2);
1143 __ Move(r2, r1);
1144 __ Move(r1, TMP);
1145 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1146 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1147 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1148 if (type == Primitive::kPrimFloat) {
1149 __ MovS(FTMP, f2);
1150 __ MovS(f2, f1);
1151 __ MovS(f1, FTMP);
1152 } else {
1153 DCHECK_EQ(type, Primitive::kPrimDouble);
1154 __ MovD(FTMP, f2);
1155 __ MovD(f2, f1);
1156 __ MovD(f1, FTMP);
1157 }
1158 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1159 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1160 // Swap FPR and GPR.
1161 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1162 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1163 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001164 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001165 __ Move(TMP, r2);
1166 __ Mfc1(r2, f1);
1167 __ Mtc1(TMP, f1);
1168 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1169 // Swap 2 GPR register pairs.
1170 Register r1 = loc1.AsRegisterPairLow<Register>();
1171 Register r2 = loc2.AsRegisterPairLow<Register>();
1172 __ Move(TMP, r2);
1173 __ Move(r2, r1);
1174 __ Move(r1, TMP);
1175 r1 = loc1.AsRegisterPairHigh<Register>();
1176 r2 = loc2.AsRegisterPairHigh<Register>();
1177 __ Move(TMP, r2);
1178 __ Move(r2, r1);
1179 __ Move(r1, TMP);
1180 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1181 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1182 // Swap FPR and GPR register pair.
1183 DCHECK_EQ(type, Primitive::kPrimDouble);
1184 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1185 : loc2.AsFpuRegister<FRegister>();
1186 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1187 : loc2.AsRegisterPairLow<Register>();
1188 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1189 : loc2.AsRegisterPairHigh<Register>();
1190 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1191 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1192 // unpredictable and the following mfch1 will fail.
1193 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001194 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001195 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001196 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001197 __ Move(r2_l, TMP);
1198 __ Move(r2_h, AT);
1199 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1200 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1201 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1202 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001203 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1204 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001205 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1206 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001207 __ Move(TMP, reg);
1208 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1209 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1210 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1211 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1212 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1213 : loc2.AsRegisterPairLow<Register>();
1214 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1215 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001216 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001217 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1218 : loc2.GetHighStackIndex(kMipsWordSize);
1219 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001220 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001221 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001222 __ Move(TMP, reg_h);
1223 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1224 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001225 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1226 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1227 : loc2.AsFpuRegister<FRegister>();
1228 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1229 if (type == Primitive::kPrimFloat) {
1230 __ MovS(FTMP, reg);
1231 __ LoadSFromOffset(reg, SP, offset);
1232 __ StoreSToOffset(FTMP, SP, offset);
1233 } else {
1234 DCHECK_EQ(type, Primitive::kPrimDouble);
1235 __ MovD(FTMP, reg);
1236 __ LoadDFromOffset(reg, SP, offset);
1237 __ StoreDToOffset(FTMP, SP, offset);
1238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239 } else {
1240 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1241 }
1242}
1243
1244void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1245 __ Pop(static_cast<Register>(reg));
1246}
1247
1248void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1249 __ Push(static_cast<Register>(reg));
1250}
1251
1252void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1253 // Allocate a scratch register other than TMP, if available.
1254 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1255 // automatically unspilled when the scratch scope object is destroyed).
1256 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1257 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1258 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1259 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1260 __ LoadFromOffset(kLoadWord,
1261 Register(ensure_scratch.GetRegister()),
1262 SP,
1263 index1 + stack_offset);
1264 __ LoadFromOffset(kLoadWord,
1265 TMP,
1266 SP,
1267 index2 + stack_offset);
1268 __ StoreToOffset(kStoreWord,
1269 Register(ensure_scratch.GetRegister()),
1270 SP,
1271 index2 + stack_offset);
1272 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1273 }
1274}
1275
Alexey Frunze73296a72016-06-03 22:51:46 -07001276void CodeGeneratorMIPS::ComputeSpillMask() {
1277 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1278 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1279 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1280 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1281 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1282 // within the stack frame.
1283 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1284 core_spill_mask_ |= (1 << ZERO);
1285 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001286}
1287
1288bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001289 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001290 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1291 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1292 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001293 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001294}
1295
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001296static dwarf::Reg DWARFReg(Register reg) {
1297 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1298}
1299
1300// TODO: mapping of floating-point registers to DWARF.
1301
1302void CodeGeneratorMIPS::GenerateFrameEntry() {
1303 __ Bind(&frame_entry_label_);
1304
1305 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1306
1307 if (do_overflow_check) {
1308 __ LoadFromOffset(kLoadWord,
1309 ZERO,
1310 SP,
1311 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1312 RecordPcInfo(nullptr, 0);
1313 }
1314
1315 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001316 CHECK_EQ(fpu_spill_mask_, 0u);
1317 CHECK_EQ(core_spill_mask_, 1u << RA);
1318 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001319 return;
1320 }
1321
1322 // Make sure the frame size isn't unreasonably large.
1323 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1324 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1325 }
1326
1327 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001330 __ IncreaseFrameSize(ofs);
1331
Alexey Frunze73296a72016-06-03 22:51:46 -07001332 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1333 Register reg = static_cast<Register>(MostSignificantBit(mask));
1334 mask ^= 1u << reg;
1335 ofs -= kMipsWordSize;
1336 // The ZERO register is only included for alignment.
1337 if (reg != ZERO) {
1338 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339 __ cfi().RelOffset(DWARFReg(reg), ofs);
1340 }
1341 }
1342
Alexey Frunze73296a72016-06-03 22:51:46 -07001343 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1344 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1345 mask ^= 1u << reg;
1346 ofs -= kMipsDoublewordSize;
1347 __ StoreDToOffset(reg, SP, ofs);
1348 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001349 }
1350
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001351 // Save the current method if we need it. Note that we do not
1352 // do this in HCurrentMethod, as the instruction might have been removed
1353 // in the SSA graph.
1354 if (RequiresCurrentMethod()) {
1355 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1356 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001357
1358 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1359 // Initialize should deoptimize flag to 0.
1360 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1361 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362}
1363
1364void CodeGeneratorMIPS::GenerateFrameExit() {
1365 __ cfi().RememberState();
1366
1367 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001369
Alexey Frunze73296a72016-06-03 22:51:46 -07001370 // For better instruction scheduling restore RA before other registers.
1371 uint32_t ofs = GetFrameSize();
1372 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1373 Register reg = static_cast<Register>(MostSignificantBit(mask));
1374 mask ^= 1u << reg;
1375 ofs -= kMipsWordSize;
1376 // The ZERO register is only included for alignment.
1377 if (reg != ZERO) {
1378 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379 __ cfi().Restore(DWARFReg(reg));
1380 }
1381 }
1382
Alexey Frunze73296a72016-06-03 22:51:46 -07001383 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1384 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1385 mask ^= 1u << reg;
1386 ofs -= kMipsDoublewordSize;
1387 __ LoadDFromOffset(reg, SP, ofs);
1388 // TODO: __ cfi().Restore(DWARFReg(reg));
1389 }
1390
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001391 size_t frame_size = GetFrameSize();
1392 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1393 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1394 bool reordering = __ SetReorder(false);
1395 if (exchange) {
1396 __ Jr(RA);
1397 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1398 } else {
1399 __ DecreaseFrameSize(frame_size);
1400 __ Jr(RA);
1401 __ Nop(); // In delay slot.
1402 }
1403 __ SetReorder(reordering);
1404 } else {
1405 __ Jr(RA);
1406 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001407 }
1408
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001409 __ cfi().RestoreState();
1410 __ cfi().DefCFAOffset(GetFrameSize());
1411}
1412
1413void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1414 __ Bind(GetLabelOf(block));
1415}
1416
1417void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1418 if (src.Equals(dst)) {
1419 return;
1420 }
1421
1422 if (src.IsConstant()) {
1423 MoveConstant(dst, src.GetConstant());
1424 } else {
1425 if (Primitive::Is64BitType(dst_type)) {
1426 Move64(dst, src);
1427 } else {
1428 Move32(dst, src);
1429 }
1430 }
1431}
1432
1433void CodeGeneratorMIPS::Move32(Location destination, Location source) {
1434 if (source.Equals(destination)) {
1435 return;
1436 }
1437
1438 if (destination.IsRegister()) {
1439 if (source.IsRegister()) {
1440 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1441 } else if (source.IsFpuRegister()) {
1442 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1443 } else {
1444 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1445 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
1446 }
1447 } else if (destination.IsFpuRegister()) {
1448 if (source.IsRegister()) {
1449 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1450 } else if (source.IsFpuRegister()) {
1451 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1452 } else {
1453 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1454 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1455 }
1456 } else {
1457 DCHECK(destination.IsStackSlot()) << destination;
1458 if (source.IsRegister()) {
1459 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
1460 } else if (source.IsFpuRegister()) {
1461 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1462 } else {
1463 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1464 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1465 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1466 }
1467 }
1468}
1469
1470void CodeGeneratorMIPS::Move64(Location destination, Location source) {
1471 if (source.Equals(destination)) {
1472 return;
1473 }
1474
1475 if (destination.IsRegisterPair()) {
1476 if (source.IsRegisterPair()) {
1477 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1478 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1479 } else if (source.IsFpuRegister()) {
1480 Register dst_high = destination.AsRegisterPairHigh<Register>();
1481 Register dst_low = destination.AsRegisterPairLow<Register>();
1482 FRegister src = source.AsFpuRegister<FRegister>();
1483 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001484 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001485 } else {
1486 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1487 int32_t off = source.GetStackIndex();
1488 Register r = destination.AsRegisterPairLow<Register>();
1489 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1490 }
1491 } else if (destination.IsFpuRegister()) {
1492 if (source.IsRegisterPair()) {
1493 FRegister dst = destination.AsFpuRegister<FRegister>();
1494 Register src_high = source.AsRegisterPairHigh<Register>();
1495 Register src_low = source.AsRegisterPairLow<Register>();
1496 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001497 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001498 } else if (source.IsFpuRegister()) {
1499 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1500 } else {
1501 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1502 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1503 }
1504 } else {
1505 DCHECK(destination.IsDoubleStackSlot()) << destination;
1506 int32_t off = destination.GetStackIndex();
1507 if (source.IsRegisterPair()) {
1508 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
1509 } else if (source.IsFpuRegister()) {
1510 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
1511 } else {
1512 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1513 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1514 __ StoreToOffset(kStoreWord, TMP, SP, off);
1515 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1516 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
1517 }
1518 }
1519}
1520
1521void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1522 if (c->IsIntConstant() || c->IsNullConstant()) {
1523 // Move 32 bit constant.
1524 int32_t value = GetInt32ValueOf(c);
1525 if (destination.IsRegister()) {
1526 Register dst = destination.AsRegister<Register>();
1527 __ LoadConst32(dst, value);
1528 } else {
1529 DCHECK(destination.IsStackSlot())
1530 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001531 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001532 }
1533 } else if (c->IsLongConstant()) {
1534 // Move 64 bit constant.
1535 int64_t value = GetInt64ValueOf(c);
1536 if (destination.IsRegisterPair()) {
1537 Register r_h = destination.AsRegisterPairHigh<Register>();
1538 Register r_l = destination.AsRegisterPairLow<Register>();
1539 __ LoadConst64(r_h, r_l, value);
1540 } else {
1541 DCHECK(destination.IsDoubleStackSlot())
1542 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001543 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001544 }
1545 } else if (c->IsFloatConstant()) {
1546 // Move 32 bit float constant.
1547 int32_t value = GetInt32ValueOf(c);
1548 if (destination.IsFpuRegister()) {
1549 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1550 } else {
1551 DCHECK(destination.IsStackSlot())
1552 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001553 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001554 }
1555 } else {
1556 // Move 64 bit double constant.
1557 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1558 int64_t value = GetInt64ValueOf(c);
1559 if (destination.IsFpuRegister()) {
1560 FRegister fd = destination.AsFpuRegister<FRegister>();
1561 __ LoadDConst64(fd, value, TMP);
1562 } else {
1563 DCHECK(destination.IsDoubleStackSlot())
1564 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001565 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001566 }
1567 }
1568}
1569
1570void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1571 DCHECK(destination.IsRegister());
1572 Register dst = destination.AsRegister<Register>();
1573 __ LoadConst32(dst, value);
1574}
1575
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1577 if (location.IsRegister()) {
1578 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001579 } else if (location.IsRegisterPair()) {
1580 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1581 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001582 } else {
1583 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1584 }
1585}
1586
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1588inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1589 const ArenaDeque<PcRelativePatchInfo>& infos,
1590 ArenaVector<LinkerPatch>* linker_patches) {
1591 for (const PcRelativePatchInfo& info : infos) {
1592 const DexFile& dex_file = info.target_dex_file;
1593 size_t offset_or_index = info.offset_or_index;
1594 DCHECK(info.high_label.IsBound());
1595 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1596 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1597 // the assembler's base label used for PC-relative addressing.
1598 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1599 ? __ GetLabelLocation(&info.pc_rel_label)
1600 : __ GetPcRelBaseLabelLocation();
1601 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1602 }
1603}
1604
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001605void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1606 DCHECK(linker_patches->empty());
1607 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001608 pc_relative_dex_cache_patches_.size() +
1609 pc_relative_string_patches_.size() +
1610 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001611 type_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001612 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001613 boot_image_type_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001614 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001615 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1616 linker_patches);
1617 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001618 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00001619 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1620 linker_patches);
1621 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001622 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1623 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001624 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1625 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001626 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001627 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1628 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001629 for (const auto& entry : boot_image_string_patches_) {
1630 const StringReference& target_string = entry.first;
1631 Literal* literal = entry.second;
1632 DCHECK(literal->GetLabel()->IsBound());
1633 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1634 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1635 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001636 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001637 }
1638 for (const auto& entry : boot_image_type_patches_) {
1639 const TypeReference& target_type = entry.first;
1640 Literal* literal = entry.second;
1641 DCHECK(literal->GetLabel()->IsBound());
1642 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1643 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1644 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001645 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001646 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001647 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001648}
1649
1650CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001651 const DexFile& dex_file, dex::StringIndex string_index) {
1652 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001653}
1654
1655CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001656 const DexFile& dex_file, dex::TypeIndex type_index) {
1657 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001658}
1659
Vladimir Marko1998cd02017-01-13 13:02:58 +00001660CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1661 const DexFile& dex_file, dex::TypeIndex type_index) {
1662 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1663}
1664
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001665CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1666 const DexFile& dex_file, uint32_t element_offset) {
1667 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1668}
1669
1670CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1671 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1672 patches->emplace_back(dex_file, offset_or_index);
1673 return &patches->back();
1674}
1675
Alexey Frunze06a46c42016-07-19 15:00:40 -07001676Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1677 return map->GetOrCreate(
1678 value,
1679 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1680}
1681
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001682Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1683 MethodToLiteralMap* map) {
1684 return map->GetOrCreate(
1685 target_method,
1686 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1687}
1688
Alexey Frunze06a46c42016-07-19 15:00:40 -07001689Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001690 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001691 return boot_image_string_patches_.GetOrCreate(
1692 StringReference(&dex_file, string_index),
1693 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1694}
1695
1696Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001697 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001698 return boot_image_type_patches_.GetOrCreate(
1699 TypeReference(&dex_file, type_index),
1700 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1701}
1702
1703Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001704 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001705}
1706
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001707void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1708 Register out,
1709 Register base) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001710 if (GetInstructionSetFeatures().IsR6()) {
1711 DCHECK_EQ(base, ZERO);
1712 __ Bind(&info->high_label);
1713 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001714 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001715 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001716 } else {
1717 // If base is ZERO, emit NAL to obtain the actual base.
1718 if (base == ZERO) {
1719 // Generate a dummy PC-relative call to obtain PC.
1720 __ Nal();
1721 }
1722 __ Bind(&info->high_label);
1723 __ Lui(out, /* placeholder */ 0x1234);
1724 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1725 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1726 if (base == ZERO) {
1727 __ Bind(&info->pc_rel_label);
1728 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001729 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001730 __ Addu(out, out, (base == ZERO) ? RA : base);
1731 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001732 // The immediately following instruction will add the sign-extended low half of the 32-bit
1733 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001734}
1735
Alexey Frunze627c1a02017-01-30 19:28:14 -08001736CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1737 const DexFile& dex_file,
1738 dex::StringIndex dex_index,
1739 Handle<mirror::String> handle) {
1740 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1741 reinterpret_cast64<uint64_t>(handle.GetReference()));
1742 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1743 return &jit_string_patches_.back();
1744}
1745
1746CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1747 const DexFile& dex_file,
1748 dex::TypeIndex dex_index,
1749 Handle<mirror::Class> handle) {
1750 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1751 reinterpret_cast64<uint64_t>(handle.GetReference()));
1752 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1753 return &jit_class_patches_.back();
1754}
1755
1756void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1757 const uint8_t* roots_data,
1758 const CodeGeneratorMIPS::JitPatchInfo& info,
1759 uint64_t index_in_table) const {
1760 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1761 uintptr_t address =
1762 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1763 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1764 // lui reg, addr32_high
1765 DCHECK_EQ(code[literal_offset + 0], 0x34);
1766 DCHECK_EQ(code[literal_offset + 1], 0x12);
1767 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1768 DCHECK_EQ(code[literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001769 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001770 DCHECK_EQ(code[literal_offset + 4], 0x78);
1771 DCHECK_EQ(code[literal_offset + 5], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001772 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001773 // lui reg, addr32_high
1774 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1775 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001776 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001777 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1778 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1779}
1780
1781void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1782 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001783 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1784 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001785 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001786 uint64_t index_in_table = it->second;
1787 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001788 }
1789 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001790 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1791 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001792 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001793 uint64_t index_in_table = it->second;
1794 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001795 }
1796}
1797
Goran Jakovljevice114da22016-12-26 14:21:43 +01001798void CodeGeneratorMIPS::MarkGCCard(Register object,
1799 Register value,
1800 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001801 MipsLabel done;
1802 Register card = AT;
1803 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001804 if (value_can_be_null) {
1805 __ Beqz(value, &done);
1806 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001807 __ LoadFromOffset(kLoadWord,
1808 card,
1809 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001810 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001811 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1812 __ Addu(temp, card, temp);
1813 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001814 if (value_can_be_null) {
1815 __ Bind(&done);
1816 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001817}
1818
David Brazdil58282f42016-01-14 12:45:10 +00001819void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001820 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1821 blocked_core_registers_[ZERO] = true;
1822 blocked_core_registers_[K0] = true;
1823 blocked_core_registers_[K1] = true;
1824 blocked_core_registers_[GP] = true;
1825 blocked_core_registers_[SP] = true;
1826 blocked_core_registers_[RA] = true;
1827
1828 // AT and TMP(T8) are used as temporary/scratch registers
1829 // (similar to how AT is used by MIPS assemblers).
1830 blocked_core_registers_[AT] = true;
1831 blocked_core_registers_[TMP] = true;
1832 blocked_fpu_registers_[FTMP] = true;
1833
1834 // Reserve suspend and thread registers.
1835 blocked_core_registers_[S0] = true;
1836 blocked_core_registers_[TR] = true;
1837
1838 // Reserve T9 for function calls
1839 blocked_core_registers_[T9] = true;
1840
1841 // Reserve odd-numbered FPU registers.
1842 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1843 blocked_fpu_registers_[i] = true;
1844 }
1845
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001846 if (GetGraph()->IsDebuggable()) {
1847 // Stubs do not save callee-save floating point registers. If the graph
1848 // is debuggable, we need to deal with these registers differently. For
1849 // now, just block them.
1850 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1851 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1852 }
1853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854}
1855
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1857 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1858 return kMipsWordSize;
1859}
1860
1861size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1862 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1863 return kMipsWordSize;
1864}
1865
1866size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1867 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1868 return kMipsDoublewordSize;
1869}
1870
1871size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1872 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1873 return kMipsDoublewordSize;
1874}
1875
1876void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001877 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878}
1879
1880void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001881 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882}
1883
Serban Constantinescufca16662016-07-14 09:21:59 +01001884constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1885
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001886void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1887 HInstruction* instruction,
1888 uint32_t dex_pc,
1889 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001890 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001891 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1892 IsDirectEntrypoint(entrypoint));
1893 if (EntrypointRequiresStackMap(entrypoint)) {
1894 RecordPcInfo(instruction, dex_pc, slow_path);
1895 }
1896}
1897
1898void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1899 HInstruction* instruction,
1900 SlowPathCode* slow_path,
1901 bool direct) {
1902 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1903 GenerateInvokeRuntime(entry_point_offset, direct);
1904}
1905
1906void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001907 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001908 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001909 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001910 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001911 // Reserve argument space on stack (for $a0-$a3) for
1912 // entrypoints that directly reference native implementations.
1913 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001914 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001916 } else {
1917 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001918 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001919 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920}
1921
1922void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1923 Register class_reg) {
1924 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1925 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1926 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1927 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1928 __ Sync(0);
1929 __ Bind(slow_path->GetExitLabel());
1930}
1931
1932void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1933 __ Sync(0); // Only stype 0 is supported.
1934}
1935
1936void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1937 HBasicBlock* successor) {
1938 SuspendCheckSlowPathMIPS* slow_path =
1939 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1940 codegen_->AddSlowPath(slow_path);
1941
1942 __ LoadFromOffset(kLoadUnsignedHalfword,
1943 TMP,
1944 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001945 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946 if (successor == nullptr) {
1947 __ Bnez(TMP, slow_path->GetEntryLabel());
1948 __ Bind(slow_path->GetReturnLabel());
1949 } else {
1950 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1951 __ B(slow_path->GetEntryLabel());
1952 // slow_path will return to GetLabelOf(successor).
1953 }
1954}
1955
1956InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1957 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001958 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959 assembler_(codegen->GetAssembler()),
1960 codegen_(codegen) {}
1961
1962void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1963 DCHECK_EQ(instruction->InputCount(), 2U);
1964 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1965 Primitive::Type type = instruction->GetResultType();
1966 switch (type) {
1967 case Primitive::kPrimInt: {
1968 locations->SetInAt(0, Location::RequiresRegister());
1969 HInstruction* right = instruction->InputAt(1);
1970 bool can_use_imm = false;
1971 if (right->IsConstant()) {
1972 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1973 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1974 can_use_imm = IsUint<16>(imm);
1975 } else if (instruction->IsAdd()) {
1976 can_use_imm = IsInt<16>(imm);
1977 } else {
1978 DCHECK(instruction->IsSub());
1979 can_use_imm = IsInt<16>(-imm);
1980 }
1981 }
1982 if (can_use_imm)
1983 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1984 else
1985 locations->SetInAt(1, Location::RequiresRegister());
1986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1987 break;
1988 }
1989
1990 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001991 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001992 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001994 break;
1995 }
1996
1997 case Primitive::kPrimFloat:
1998 case Primitive::kPrimDouble:
1999 DCHECK(instruction->IsAdd() || instruction->IsSub());
2000 locations->SetInAt(0, Location::RequiresFpuRegister());
2001 locations->SetInAt(1, Location::RequiresFpuRegister());
2002 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2003 break;
2004
2005 default:
2006 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2007 }
2008}
2009
2010void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2011 Primitive::Type type = instruction->GetType();
2012 LocationSummary* locations = instruction->GetLocations();
2013
2014 switch (type) {
2015 case Primitive::kPrimInt: {
2016 Register dst = locations->Out().AsRegister<Register>();
2017 Register lhs = locations->InAt(0).AsRegister<Register>();
2018 Location rhs_location = locations->InAt(1);
2019
2020 Register rhs_reg = ZERO;
2021 int32_t rhs_imm = 0;
2022 bool use_imm = rhs_location.IsConstant();
2023 if (use_imm) {
2024 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2025 } else {
2026 rhs_reg = rhs_location.AsRegister<Register>();
2027 }
2028
2029 if (instruction->IsAnd()) {
2030 if (use_imm)
2031 __ Andi(dst, lhs, rhs_imm);
2032 else
2033 __ And(dst, lhs, rhs_reg);
2034 } else if (instruction->IsOr()) {
2035 if (use_imm)
2036 __ Ori(dst, lhs, rhs_imm);
2037 else
2038 __ Or(dst, lhs, rhs_reg);
2039 } else if (instruction->IsXor()) {
2040 if (use_imm)
2041 __ Xori(dst, lhs, rhs_imm);
2042 else
2043 __ Xor(dst, lhs, rhs_reg);
2044 } else if (instruction->IsAdd()) {
2045 if (use_imm)
2046 __ Addiu(dst, lhs, rhs_imm);
2047 else
2048 __ Addu(dst, lhs, rhs_reg);
2049 } else {
2050 DCHECK(instruction->IsSub());
2051 if (use_imm)
2052 __ Addiu(dst, lhs, -rhs_imm);
2053 else
2054 __ Subu(dst, lhs, rhs_reg);
2055 }
2056 break;
2057 }
2058
2059 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2061 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2062 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2063 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002064 Location rhs_location = locations->InAt(1);
2065 bool use_imm = rhs_location.IsConstant();
2066 if (!use_imm) {
2067 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2068 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2069 if (instruction->IsAnd()) {
2070 __ And(dst_low, lhs_low, rhs_low);
2071 __ And(dst_high, lhs_high, rhs_high);
2072 } else if (instruction->IsOr()) {
2073 __ Or(dst_low, lhs_low, rhs_low);
2074 __ Or(dst_high, lhs_high, rhs_high);
2075 } else if (instruction->IsXor()) {
2076 __ Xor(dst_low, lhs_low, rhs_low);
2077 __ Xor(dst_high, lhs_high, rhs_high);
2078 } else if (instruction->IsAdd()) {
2079 if (lhs_low == rhs_low) {
2080 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2081 __ Slt(TMP, lhs_low, ZERO);
2082 __ Addu(dst_low, lhs_low, rhs_low);
2083 } else {
2084 __ Addu(dst_low, lhs_low, rhs_low);
2085 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2086 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2087 }
2088 __ Addu(dst_high, lhs_high, rhs_high);
2089 __ Addu(dst_high, dst_high, TMP);
2090 } else {
2091 DCHECK(instruction->IsSub());
2092 __ Sltu(TMP, lhs_low, rhs_low);
2093 __ Subu(dst_low, lhs_low, rhs_low);
2094 __ Subu(dst_high, lhs_high, rhs_high);
2095 __ Subu(dst_high, dst_high, TMP);
2096 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002097 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002098 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2099 if (instruction->IsOr()) {
2100 uint32_t low = Low32Bits(value);
2101 uint32_t high = High32Bits(value);
2102 if (IsUint<16>(low)) {
2103 if (dst_low != lhs_low || low != 0) {
2104 __ Ori(dst_low, lhs_low, low);
2105 }
2106 } else {
2107 __ LoadConst32(TMP, low);
2108 __ Or(dst_low, lhs_low, TMP);
2109 }
2110 if (IsUint<16>(high)) {
2111 if (dst_high != lhs_high || high != 0) {
2112 __ Ori(dst_high, lhs_high, high);
2113 }
2114 } else {
2115 if (high != low) {
2116 __ LoadConst32(TMP, high);
2117 }
2118 __ Or(dst_high, lhs_high, TMP);
2119 }
2120 } else if (instruction->IsXor()) {
2121 uint32_t low = Low32Bits(value);
2122 uint32_t high = High32Bits(value);
2123 if (IsUint<16>(low)) {
2124 if (dst_low != lhs_low || low != 0) {
2125 __ Xori(dst_low, lhs_low, low);
2126 }
2127 } else {
2128 __ LoadConst32(TMP, low);
2129 __ Xor(dst_low, lhs_low, TMP);
2130 }
2131 if (IsUint<16>(high)) {
2132 if (dst_high != lhs_high || high != 0) {
2133 __ Xori(dst_high, lhs_high, high);
2134 }
2135 } else {
2136 if (high != low) {
2137 __ LoadConst32(TMP, high);
2138 }
2139 __ Xor(dst_high, lhs_high, TMP);
2140 }
2141 } else if (instruction->IsAnd()) {
2142 uint32_t low = Low32Bits(value);
2143 uint32_t high = High32Bits(value);
2144 if (IsUint<16>(low)) {
2145 __ Andi(dst_low, lhs_low, low);
2146 } else if (low != 0xFFFFFFFF) {
2147 __ LoadConst32(TMP, low);
2148 __ And(dst_low, lhs_low, TMP);
2149 } else if (dst_low != lhs_low) {
2150 __ Move(dst_low, lhs_low);
2151 }
2152 if (IsUint<16>(high)) {
2153 __ Andi(dst_high, lhs_high, high);
2154 } else if (high != 0xFFFFFFFF) {
2155 if (high != low) {
2156 __ LoadConst32(TMP, high);
2157 }
2158 __ And(dst_high, lhs_high, TMP);
2159 } else if (dst_high != lhs_high) {
2160 __ Move(dst_high, lhs_high);
2161 }
2162 } else {
2163 if (instruction->IsSub()) {
2164 value = -value;
2165 } else {
2166 DCHECK(instruction->IsAdd());
2167 }
2168 int32_t low = Low32Bits(value);
2169 int32_t high = High32Bits(value);
2170 if (IsInt<16>(low)) {
2171 if (dst_low != lhs_low || low != 0) {
2172 __ Addiu(dst_low, lhs_low, low);
2173 }
2174 if (low != 0) {
2175 __ Sltiu(AT, dst_low, low);
2176 }
2177 } else {
2178 __ LoadConst32(TMP, low);
2179 __ Addu(dst_low, lhs_low, TMP);
2180 __ Sltu(AT, dst_low, TMP);
2181 }
2182 if (IsInt<16>(high)) {
2183 if (dst_high != lhs_high || high != 0) {
2184 __ Addiu(dst_high, lhs_high, high);
2185 }
2186 } else {
2187 if (high != low) {
2188 __ LoadConst32(TMP, high);
2189 }
2190 __ Addu(dst_high, lhs_high, TMP);
2191 }
2192 if (low != 0) {
2193 __ Addu(dst_high, dst_high, AT);
2194 }
2195 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002196 }
2197 break;
2198 }
2199
2200 case Primitive::kPrimFloat:
2201 case Primitive::kPrimDouble: {
2202 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2203 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2204 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2205 if (instruction->IsAdd()) {
2206 if (type == Primitive::kPrimFloat) {
2207 __ AddS(dst, lhs, rhs);
2208 } else {
2209 __ AddD(dst, lhs, rhs);
2210 }
2211 } else {
2212 DCHECK(instruction->IsSub());
2213 if (type == Primitive::kPrimFloat) {
2214 __ SubS(dst, lhs, rhs);
2215 } else {
2216 __ SubD(dst, lhs, rhs);
2217 }
2218 }
2219 break;
2220 }
2221
2222 default:
2223 LOG(FATAL) << "Unexpected binary operation type " << type;
2224 }
2225}
2226
2227void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002228 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002229
2230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2231 Primitive::Type type = instr->GetResultType();
2232 switch (type) {
2233 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002234 locations->SetInAt(0, Location::RequiresRegister());
2235 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2237 break;
2238 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002239 locations->SetInAt(0, Location::RequiresRegister());
2240 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2241 locations->SetOut(Location::RequiresRegister());
2242 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243 default:
2244 LOG(FATAL) << "Unexpected shift type " << type;
2245 }
2246}
2247
2248static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2249
2250void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002251 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002252 LocationSummary* locations = instr->GetLocations();
2253 Primitive::Type type = instr->GetType();
2254
2255 Location rhs_location = locations->InAt(1);
2256 bool use_imm = rhs_location.IsConstant();
2257 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2258 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002259 const uint32_t shift_mask =
2260 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002261 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002262 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2263 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264
2265 switch (type) {
2266 case Primitive::kPrimInt: {
2267 Register dst = locations->Out().AsRegister<Register>();
2268 Register lhs = locations->InAt(0).AsRegister<Register>();
2269 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002270 if (shift_value == 0) {
2271 if (dst != lhs) {
2272 __ Move(dst, lhs);
2273 }
2274 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275 __ Sll(dst, lhs, shift_value);
2276 } else if (instr->IsShr()) {
2277 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002278 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002279 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002280 } else {
2281 if (has_ins_rotr) {
2282 __ Rotr(dst, lhs, shift_value);
2283 } else {
2284 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2285 __ Srl(dst, lhs, shift_value);
2286 __ Or(dst, dst, TMP);
2287 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288 }
2289 } else {
2290 if (instr->IsShl()) {
2291 __ Sllv(dst, lhs, rhs_reg);
2292 } else if (instr->IsShr()) {
2293 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002294 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002295 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002296 } else {
2297 if (has_ins_rotr) {
2298 __ Rotrv(dst, lhs, rhs_reg);
2299 } else {
2300 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002301 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2302 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2303 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2304 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2305 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002306 __ Sllv(TMP, lhs, TMP);
2307 __ Srlv(dst, lhs, rhs_reg);
2308 __ Or(dst, dst, TMP);
2309 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310 }
2311 }
2312 break;
2313 }
2314
2315 case Primitive::kPrimLong: {
2316 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2317 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2318 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2319 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2320 if (use_imm) {
2321 if (shift_value == 0) {
2322 codegen_->Move64(locations->Out(), locations->InAt(0));
2323 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002324 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002325 if (instr->IsShl()) {
2326 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2327 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2328 __ Sll(dst_low, lhs_low, shift_value);
2329 } else if (instr->IsShr()) {
2330 __ Srl(dst_low, lhs_low, shift_value);
2331 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2332 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002333 } else if (instr->IsUShr()) {
2334 __ Srl(dst_low, lhs_low, shift_value);
2335 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2336 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002337 } else {
2338 __ Srl(dst_low, lhs_low, shift_value);
2339 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2340 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002341 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002343 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002344 if (instr->IsShl()) {
2345 __ Sll(dst_low, lhs_low, shift_value);
2346 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2347 __ Sll(dst_high, lhs_high, shift_value);
2348 __ Or(dst_high, dst_high, TMP);
2349 } else if (instr->IsShr()) {
2350 __ Sra(dst_high, lhs_high, shift_value);
2351 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2352 __ Srl(dst_low, lhs_low, shift_value);
2353 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002354 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002355 __ Srl(dst_high, lhs_high, shift_value);
2356 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2357 __ Srl(dst_low, lhs_low, shift_value);
2358 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002359 } else {
2360 __ Srl(TMP, lhs_low, shift_value);
2361 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2362 __ Or(dst_low, dst_low, TMP);
2363 __ Srl(TMP, lhs_high, shift_value);
2364 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2365 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002366 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002367 }
2368 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002369 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002371 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 __ Move(dst_low, ZERO);
2373 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002374 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002375 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002376 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002377 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002379 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002380 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002381 // 64-bit rotation by 32 is just a swap.
2382 __ Move(dst_low, lhs_high);
2383 __ Move(dst_high, lhs_low);
2384 } else {
2385 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002386 __ Srl(dst_low, lhs_high, shift_value_high);
2387 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2388 __ Srl(dst_high, lhs_low, shift_value_high);
2389 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002390 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002391 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2392 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002393 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002394 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2395 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002396 __ Or(dst_high, dst_high, TMP);
2397 }
2398 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002399 }
2400 }
2401 } else {
2402 MipsLabel done;
2403 if (instr->IsShl()) {
2404 __ Sllv(dst_low, lhs_low, rhs_reg);
2405 __ Nor(AT, ZERO, rhs_reg);
2406 __ Srl(TMP, lhs_low, 1);
2407 __ Srlv(TMP, TMP, AT);
2408 __ Sllv(dst_high, lhs_high, rhs_reg);
2409 __ Or(dst_high, dst_high, TMP);
2410 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2411 __ Beqz(TMP, &done);
2412 __ Move(dst_high, dst_low);
2413 __ Move(dst_low, ZERO);
2414 } else if (instr->IsShr()) {
2415 __ Srav(dst_high, lhs_high, rhs_reg);
2416 __ Nor(AT, ZERO, rhs_reg);
2417 __ Sll(TMP, lhs_high, 1);
2418 __ Sllv(TMP, TMP, AT);
2419 __ Srlv(dst_low, lhs_low, rhs_reg);
2420 __ Or(dst_low, dst_low, TMP);
2421 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2422 __ Beqz(TMP, &done);
2423 __ Move(dst_low, dst_high);
2424 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002425 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426 __ Srlv(dst_high, lhs_high, rhs_reg);
2427 __ Nor(AT, ZERO, rhs_reg);
2428 __ Sll(TMP, lhs_high, 1);
2429 __ Sllv(TMP, TMP, AT);
2430 __ Srlv(dst_low, lhs_low, rhs_reg);
2431 __ Or(dst_low, dst_low, TMP);
2432 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2433 __ Beqz(TMP, &done);
2434 __ Move(dst_low, dst_high);
2435 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002436 } else {
2437 __ Nor(AT, ZERO, rhs_reg);
2438 __ Srlv(TMP, lhs_low, rhs_reg);
2439 __ Sll(dst_low, lhs_high, 1);
2440 __ Sllv(dst_low, dst_low, AT);
2441 __ Or(dst_low, dst_low, TMP);
2442 __ Srlv(TMP, lhs_high, rhs_reg);
2443 __ Sll(dst_high, lhs_low, 1);
2444 __ Sllv(dst_high, dst_high, AT);
2445 __ Or(dst_high, dst_high, TMP);
2446 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2447 __ Beqz(TMP, &done);
2448 __ Move(TMP, dst_high);
2449 __ Move(dst_high, dst_low);
2450 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002451 }
2452 __ Bind(&done);
2453 }
2454 break;
2455 }
2456
2457 default:
2458 LOG(FATAL) << "Unexpected shift operation type " << type;
2459 }
2460}
2461
2462void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2463 HandleBinaryOp(instruction);
2464}
2465
2466void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2467 HandleBinaryOp(instruction);
2468}
2469
2470void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2471 HandleBinaryOp(instruction);
2472}
2473
2474void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2475 HandleBinaryOp(instruction);
2476}
2477
2478void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002479 Primitive::Type type = instruction->GetType();
2480 bool object_array_get_with_read_barrier =
2481 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002482 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002483 new (GetGraph()->GetArena()) LocationSummary(instruction,
2484 object_array_get_with_read_barrier
2485 ? LocationSummary::kCallOnSlowPath
2486 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002487 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2488 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2489 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002490 locations->SetInAt(0, Location::RequiresRegister());
2491 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002492 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002493 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2494 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002495 // The output overlaps in the case of an object array get with
2496 // read barriers enabled: we do not want the move to overwrite the
2497 // array's location, as we need it to emit the read barrier.
2498 locations->SetOut(Location::RequiresRegister(),
2499 object_array_get_with_read_barrier
2500 ? Location::kOutputOverlap
2501 : Location::kNoOutputOverlap);
2502 }
2503 // We need a temporary register for the read barrier marking slow
2504 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2505 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2506 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 }
2508}
2509
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002510static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2511 auto null_checker = [codegen, instruction]() {
2512 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002513 };
2514 return null_checker;
2515}
2516
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002517void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2518 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002519 Location obj_loc = locations->InAt(0);
2520 Register obj = obj_loc.AsRegister<Register>();
2521 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002522 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002523 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002524 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002525
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002526 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002527 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2528 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002529 switch (type) {
2530 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002531 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002532 if (index.IsConstant()) {
2533 size_t offset =
2534 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002535 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002536 } else {
2537 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002538 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002539 }
2540 break;
2541 }
2542
2543 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002544 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002545 if (index.IsConstant()) {
2546 size_t offset =
2547 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002548 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002549 } else {
2550 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002551 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002552 }
2553 break;
2554 }
2555
2556 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002557 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002558 if (index.IsConstant()) {
2559 size_t offset =
2560 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002561 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002563 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002564 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002565 }
2566 break;
2567 }
2568
2569 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002570 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002571 if (maybe_compressed_char_at) {
2572 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2573 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2574 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2575 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2576 "Expecting 0=compressed, 1=uncompressed");
2577 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002578 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002579 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2580 if (maybe_compressed_char_at) {
2581 MipsLabel uncompressed_load, done;
2582 __ Bnez(TMP, &uncompressed_load);
2583 __ LoadFromOffset(kLoadUnsignedByte,
2584 out,
2585 obj,
2586 data_offset + (const_index << TIMES_1));
2587 __ B(&done);
2588 __ Bind(&uncompressed_load);
2589 __ LoadFromOffset(kLoadUnsignedHalfword,
2590 out,
2591 obj,
2592 data_offset + (const_index << TIMES_2));
2593 __ Bind(&done);
2594 } else {
2595 __ LoadFromOffset(kLoadUnsignedHalfword,
2596 out,
2597 obj,
2598 data_offset + (const_index << TIMES_2),
2599 null_checker);
2600 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002601 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002602 Register index_reg = index.AsRegister<Register>();
2603 if (maybe_compressed_char_at) {
2604 MipsLabel uncompressed_load, done;
2605 __ Bnez(TMP, &uncompressed_load);
2606 __ Addu(TMP, obj, index_reg);
2607 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2608 __ B(&done);
2609 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002610 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002611 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2612 __ Bind(&done);
2613 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002614 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002615 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2616 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 }
2618 break;
2619 }
2620
Alexey Frunze15958152017-02-09 19:08:30 -08002621 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002622 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002623 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 if (index.IsConstant()) {
2625 size_t offset =
2626 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002627 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002629 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002630 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002631 }
2632 break;
2633 }
2634
Alexey Frunze15958152017-02-09 19:08:30 -08002635 case Primitive::kPrimNot: {
2636 static_assert(
2637 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2638 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2639 // /* HeapReference<Object> */ out =
2640 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2641 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2642 Location temp = locations->GetTemp(0);
2643 // Note that a potential implicit null check is handled in this
2644 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2645 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2646 out_loc,
2647 obj,
2648 data_offset,
2649 index,
2650 temp,
2651 /* needs_null_check */ true);
2652 } else {
2653 Register out = out_loc.AsRegister<Register>();
2654 if (index.IsConstant()) {
2655 size_t offset =
2656 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2657 __ LoadFromOffset(kLoadWord, out, obj, 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, out_loc, out_loc, obj_loc, offset);
2662 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002663 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002664 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2665 // If read barriers are enabled, emit read barriers other than
2666 // Baker's using a slow path (and also unpoison the loaded
2667 // reference, if heap poisoning is enabled).
2668 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2669 out_loc,
2670 out_loc,
2671 obj_loc,
2672 data_offset,
2673 index);
2674 }
2675 }
2676 break;
2677 }
2678
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002679 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002680 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 if (index.IsConstant()) {
2682 size_t offset =
2683 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002684 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002685 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002686 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002687 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688 }
2689 break;
2690 }
2691
2692 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002693 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694 if (index.IsConstant()) {
2695 size_t offset =
2696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002697 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002698 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002699 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002700 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701 }
2702 break;
2703 }
2704
2705 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002706 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 if (index.IsConstant()) {
2708 size_t offset =
2709 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002710 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002712 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002713 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002714 }
2715 break;
2716 }
2717
2718 case Primitive::kPrimVoid:
2719 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2720 UNREACHABLE();
2721 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002722}
2723
2724void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2726 locations->SetInAt(0, Location::RequiresRegister());
2727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2728}
2729
2730void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2731 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002732 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002733 Register obj = locations->InAt(0).AsRegister<Register>();
2734 Register out = locations->Out().AsRegister<Register>();
2735 __ LoadFromOffset(kLoadWord, out, obj, offset);
2736 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002737 // Mask out compression flag from String's array length.
2738 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2739 __ Srl(out, out, 1u);
2740 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002741}
2742
Alexey Frunzef58b2482016-09-02 22:14:06 -07002743Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2744 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2745 ? Location::ConstantLocation(instruction->AsConstant())
2746 : Location::RequiresRegister();
2747}
2748
2749Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2750 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2751 // We can store a non-zero float or double constant without first loading it into the FPU,
2752 // but we should only prefer this if the constant has a single use.
2753 if (instruction->IsConstant() &&
2754 (instruction->AsConstant()->IsZeroBitPattern() ||
2755 instruction->GetUses().HasExactlyOneElement())) {
2756 return Location::ConstantLocation(instruction->AsConstant());
2757 // Otherwise fall through and require an FPU register for the constant.
2758 }
2759 return Location::RequiresFpuRegister();
2760}
2761
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002762void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002763 Primitive::Type value_type = instruction->GetComponentType();
2764
2765 bool needs_write_barrier =
2766 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2767 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2768
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002769 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2770 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002771 may_need_runtime_call_for_type_check ?
2772 LocationSummary::kCallOnSlowPath :
2773 LocationSummary::kNoCall);
2774
2775 locations->SetInAt(0, Location::RequiresRegister());
2776 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2777 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2778 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002779 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002780 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2781 }
2782 if (needs_write_barrier) {
2783 // Temporary register for the write barrier.
2784 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002785 }
2786}
2787
2788void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2789 LocationSummary* locations = instruction->GetLocations();
2790 Register obj = locations->InAt(0).AsRegister<Register>();
2791 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002792 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002793 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002794 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002795 bool needs_write_barrier =
2796 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002797 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002798 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002799
2800 switch (value_type) {
2801 case Primitive::kPrimBoolean:
2802 case Primitive::kPrimByte: {
2803 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002804 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002805 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002806 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002807 __ Addu(base_reg, obj, index.AsRegister<Register>());
2808 }
2809 if (value_location.IsConstant()) {
2810 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2811 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2812 } else {
2813 Register value = value_location.AsRegister<Register>();
2814 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 }
2816 break;
2817 }
2818
2819 case Primitive::kPrimShort:
2820 case Primitive::kPrimChar: {
2821 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002823 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002825 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002826 }
2827 if (value_location.IsConstant()) {
2828 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2829 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2830 } else {
2831 Register value = value_location.AsRegister<Register>();
2832 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002833 }
2834 break;
2835 }
2836
Alexey Frunze15958152017-02-09 19:08:30 -08002837 case Primitive::kPrimInt: {
2838 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2839 if (index.IsConstant()) {
2840 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2841 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002842 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002843 }
2844 if (value_location.IsConstant()) {
2845 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2846 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2847 } else {
2848 Register value = value_location.AsRegister<Register>();
2849 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2850 }
2851 break;
2852 }
2853
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002854 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002855 if (value_location.IsConstant()) {
2856 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002857 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002858 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002859 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002861 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002862 }
Alexey Frunze15958152017-02-09 19:08:30 -08002863 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2864 DCHECK_EQ(value, 0);
2865 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2866 DCHECK(!needs_write_barrier);
2867 DCHECK(!may_need_runtime_call_for_type_check);
2868 break;
2869 }
2870
2871 DCHECK(needs_write_barrier);
2872 Register value = value_location.AsRegister<Register>();
2873 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2874 Register temp2 = TMP; // Doesn't need to survive slow path.
2875 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2876 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2877 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2878 MipsLabel done;
2879 SlowPathCodeMIPS* slow_path = nullptr;
2880
2881 if (may_need_runtime_call_for_type_check) {
2882 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2883 codegen_->AddSlowPath(slow_path);
2884 if (instruction->GetValueCanBeNull()) {
2885 MipsLabel non_zero;
2886 __ Bnez(value, &non_zero);
2887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2888 if (index.IsConstant()) {
2889 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002890 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002891 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002892 }
Alexey Frunze15958152017-02-09 19:08:30 -08002893 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2894 __ B(&done);
2895 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002896 }
Alexey Frunze15958152017-02-09 19:08:30 -08002897
2898 // Note that when read barriers are enabled, the type checks
2899 // are performed without read barriers. This is fine, even in
2900 // the case where a class object is in the from-space after
2901 // the flip, as a comparison involving such a type would not
2902 // produce a false positive; it may of course produce a false
2903 // negative, in which case we would take the ArraySet slow
2904 // path.
2905
2906 // /* HeapReference<Class> */ temp1 = obj->klass_
2907 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2908 __ MaybeUnpoisonHeapReference(temp1);
2909
2910 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2911 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2912 // /* HeapReference<Class> */ temp2 = value->klass_
2913 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2914 // If heap poisoning is enabled, no need to unpoison `temp1`
2915 // nor `temp2`, as we are comparing two poisoned references.
2916
2917 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2918 MipsLabel do_put;
2919 __ Beq(temp1, temp2, &do_put);
2920 // If heap poisoning is enabled, the `temp1` reference has
2921 // not been unpoisoned yet; unpoison it now.
2922 __ MaybeUnpoisonHeapReference(temp1);
2923
2924 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2925 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2926 // If heap poisoning is enabled, no need to unpoison
2927 // `temp1`, as we are comparing against null below.
2928 __ Bnez(temp1, slow_path->GetEntryLabel());
2929 __ Bind(&do_put);
2930 } else {
2931 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2932 }
2933 }
2934
2935 Register source = value;
2936 if (kPoisonHeapReferences) {
2937 // Note that in the case where `value` is a null reference,
2938 // we do not enter this block, as a null reference does not
2939 // need poisoning.
2940 __ Move(temp1, value);
2941 __ PoisonHeapReference(temp1);
2942 source = temp1;
2943 }
2944
2945 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2946 if (index.IsConstant()) {
2947 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002948 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002949 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002950 }
2951 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2952
2953 if (!may_need_runtime_call_for_type_check) {
2954 codegen_->MaybeRecordImplicitNullCheck(instruction);
2955 }
2956
2957 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2958
2959 if (done.IsLinked()) {
2960 __ Bind(&done);
2961 }
2962
2963 if (slow_path != nullptr) {
2964 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 }
2966 break;
2967 }
2968
2969 case Primitive::kPrimLong: {
2970 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002971 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002972 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002974 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002975 }
2976 if (value_location.IsConstant()) {
2977 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2978 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2979 } else {
2980 Register value = value_location.AsRegisterPairLow<Register>();
2981 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002982 }
2983 break;
2984 }
2985
2986 case Primitive::kPrimFloat: {
2987 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002988 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002989 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002990 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002991 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002992 }
2993 if (value_location.IsConstant()) {
2994 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2995 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2996 } else {
2997 FRegister value = value_location.AsFpuRegister<FRegister>();
2998 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002999 }
3000 break;
3001 }
3002
3003 case Primitive::kPrimDouble: {
3004 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003005 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003006 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003007 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003008 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003009 }
3010 if (value_location.IsConstant()) {
3011 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3012 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3013 } else {
3014 FRegister value = value_location.AsFpuRegister<FRegister>();
3015 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003016 }
3017 break;
3018 }
3019
3020 case Primitive::kPrimVoid:
3021 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3022 UNREACHABLE();
3023 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003024}
3025
3026void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003027 RegisterSet caller_saves = RegisterSet::Empty();
3028 InvokeRuntimeCallingConvention calling_convention;
3029 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3030 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3031 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003032 locations->SetInAt(0, Location::RequiresRegister());
3033 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003034}
3035
3036void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3037 LocationSummary* locations = instruction->GetLocations();
3038 BoundsCheckSlowPathMIPS* slow_path =
3039 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3040 codegen_->AddSlowPath(slow_path);
3041
3042 Register index = locations->InAt(0).AsRegister<Register>();
3043 Register length = locations->InAt(1).AsRegister<Register>();
3044
3045 // length is limited by the maximum positive signed 32-bit integer.
3046 // Unsigned comparison of length and index checks for index < 0
3047 // and for length <= index simultaneously.
3048 __ Bgeu(index, length, slow_path->GetEntryLabel());
3049}
3050
Alexey Frunze15958152017-02-09 19:08:30 -08003051// Temp is used for read barrier.
3052static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3053 if (kEmitCompilerReadBarrier &&
3054 (kUseBakerReadBarrier ||
3055 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3056 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3057 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3058 return 1;
3059 }
3060 return 0;
3061}
3062
3063// Extra temp is used for read barrier.
3064static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3065 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3066}
3067
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003068void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003069 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3070 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3071
3072 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3073 switch (type_check_kind) {
3074 case TypeCheckKind::kExactCheck:
3075 case TypeCheckKind::kAbstractClassCheck:
3076 case TypeCheckKind::kClassHierarchyCheck:
3077 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003078 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003079 ? LocationSummary::kCallOnSlowPath
3080 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3081 break;
3082 case TypeCheckKind::kArrayCheck:
3083 case TypeCheckKind::kUnresolvedCheck:
3084 case TypeCheckKind::kInterfaceCheck:
3085 call_kind = LocationSummary::kCallOnSlowPath;
3086 break;
3087 }
3088
3089 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003090 locations->SetInAt(0, Location::RequiresRegister());
3091 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003092 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093}
3094
3095void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003096 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003098 Location obj_loc = locations->InAt(0);
3099 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003100 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003101 Location temp_loc = locations->GetTemp(0);
3102 Register temp = temp_loc.AsRegister<Register>();
3103 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3104 DCHECK_LE(num_temps, 2u);
3105 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003106 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3107 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3108 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3109 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3110 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3111 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3112 const uint32_t object_array_data_offset =
3113 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3114 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003115
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003116 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3117 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3118 // read barriers is done for performance and code size reasons.
3119 bool is_type_check_slow_path_fatal = false;
3120 if (!kEmitCompilerReadBarrier) {
3121 is_type_check_slow_path_fatal =
3122 (type_check_kind == TypeCheckKind::kExactCheck ||
3123 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3124 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3125 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3126 !instruction->CanThrowIntoCatchBlock();
3127 }
3128 SlowPathCodeMIPS* slow_path =
3129 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3130 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 codegen_->AddSlowPath(slow_path);
3132
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003133 // Avoid this check if we know `obj` is not null.
3134 if (instruction->MustDoNullCheck()) {
3135 __ Beqz(obj, &done);
3136 }
3137
3138 switch (type_check_kind) {
3139 case TypeCheckKind::kExactCheck:
3140 case TypeCheckKind::kArrayCheck: {
3141 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003142 GenerateReferenceLoadTwoRegisters(instruction,
3143 temp_loc,
3144 obj_loc,
3145 class_offset,
3146 maybe_temp2_loc,
3147 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003148 // Jump to slow path for throwing the exception or doing a
3149 // more involved array check.
3150 __ Bne(temp, cls, slow_path->GetEntryLabel());
3151 break;
3152 }
3153
3154 case TypeCheckKind::kAbstractClassCheck: {
3155 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003156 GenerateReferenceLoadTwoRegisters(instruction,
3157 temp_loc,
3158 obj_loc,
3159 class_offset,
3160 maybe_temp2_loc,
3161 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003162 // If the class is abstract, we eagerly fetch the super class of the
3163 // object to avoid doing a comparison we know will fail.
3164 MipsLabel loop;
3165 __ Bind(&loop);
3166 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003167 GenerateReferenceLoadOneRegister(instruction,
3168 temp_loc,
3169 super_offset,
3170 maybe_temp2_loc,
3171 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003172 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3173 // exception.
3174 __ Beqz(temp, slow_path->GetEntryLabel());
3175 // Otherwise, compare the classes.
3176 __ Bne(temp, cls, &loop);
3177 break;
3178 }
3179
3180 case TypeCheckKind::kClassHierarchyCheck: {
3181 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003182 GenerateReferenceLoadTwoRegisters(instruction,
3183 temp_loc,
3184 obj_loc,
3185 class_offset,
3186 maybe_temp2_loc,
3187 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003188 // Walk over the class hierarchy to find a match.
3189 MipsLabel loop;
3190 __ Bind(&loop);
3191 __ Beq(temp, cls, &done);
3192 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003193 GenerateReferenceLoadOneRegister(instruction,
3194 temp_loc,
3195 super_offset,
3196 maybe_temp2_loc,
3197 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003198 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3199 // exception. Otherwise, jump to the beginning of the loop.
3200 __ Bnez(temp, &loop);
3201 __ B(slow_path->GetEntryLabel());
3202 break;
3203 }
3204
3205 case TypeCheckKind::kArrayObjectCheck: {
3206 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003207 GenerateReferenceLoadTwoRegisters(instruction,
3208 temp_loc,
3209 obj_loc,
3210 class_offset,
3211 maybe_temp2_loc,
3212 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003213 // Do an exact check.
3214 __ Beq(temp, cls, &done);
3215 // Otherwise, we need to check that the object's class is a non-primitive array.
3216 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003217 GenerateReferenceLoadOneRegister(instruction,
3218 temp_loc,
3219 component_offset,
3220 maybe_temp2_loc,
3221 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003222 // If the component type is null, jump to the slow path to throw the exception.
3223 __ Beqz(temp, slow_path->GetEntryLabel());
3224 // Otherwise, the object is indeed an array, further check that this component
3225 // type is not a primitive type.
3226 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3227 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3228 __ Bnez(temp, slow_path->GetEntryLabel());
3229 break;
3230 }
3231
3232 case TypeCheckKind::kUnresolvedCheck:
3233 // We always go into the type check slow path for the unresolved check case.
3234 // We cannot directly call the CheckCast runtime entry point
3235 // without resorting to a type checking slow path here (i.e. by
3236 // calling InvokeRuntime directly), as it would require to
3237 // assign fixed registers for the inputs of this HInstanceOf
3238 // instruction (following the runtime calling convention), which
3239 // might be cluttered by the potential first read barrier
3240 // emission at the beginning of this method.
3241 __ B(slow_path->GetEntryLabel());
3242 break;
3243
3244 case TypeCheckKind::kInterfaceCheck: {
3245 // Avoid read barriers to improve performance of the fast path. We can not get false
3246 // positives by doing this.
3247 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003248 GenerateReferenceLoadTwoRegisters(instruction,
3249 temp_loc,
3250 obj_loc,
3251 class_offset,
3252 maybe_temp2_loc,
3253 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003254 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003255 GenerateReferenceLoadTwoRegisters(instruction,
3256 temp_loc,
3257 temp_loc,
3258 iftable_offset,
3259 maybe_temp2_loc,
3260 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003261 // Iftable is never null.
3262 __ Lw(TMP, temp, array_length_offset);
3263 // Loop through the iftable and check if any class matches.
3264 MipsLabel loop;
3265 __ Bind(&loop);
3266 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3267 __ Beqz(TMP, slow_path->GetEntryLabel());
3268 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3269 __ MaybeUnpoisonHeapReference(AT);
3270 // Go to next interface.
3271 __ Addiu(TMP, TMP, -2);
3272 // Compare the classes and continue the loop if they do not match.
3273 __ Bne(AT, cls, &loop);
3274 break;
3275 }
3276 }
3277
3278 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003279 __ Bind(slow_path->GetExitLabel());
3280}
3281
3282void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3283 LocationSummary* locations =
3284 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3285 locations->SetInAt(0, Location::RequiresRegister());
3286 if (check->HasUses()) {
3287 locations->SetOut(Location::SameAsFirstInput());
3288 }
3289}
3290
3291void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3292 // We assume the class is not null.
3293 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3294 check->GetLoadClass(),
3295 check,
3296 check->GetDexPc(),
3297 true);
3298 codegen_->AddSlowPath(slow_path);
3299 GenerateClassInitializationCheck(slow_path,
3300 check->GetLocations()->InAt(0).AsRegister<Register>());
3301}
3302
3303void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3304 Primitive::Type in_type = compare->InputAt(0)->GetType();
3305
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003306 LocationSummary* locations =
3307 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003308
3309 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003310 case Primitive::kPrimBoolean:
3311 case Primitive::kPrimByte:
3312 case Primitive::kPrimShort:
3313 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003314 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003315 locations->SetInAt(0, Location::RequiresRegister());
3316 locations->SetInAt(1, Location::RequiresRegister());
3317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3318 break;
3319
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003320 case Primitive::kPrimLong:
3321 locations->SetInAt(0, Location::RequiresRegister());
3322 locations->SetInAt(1, Location::RequiresRegister());
3323 // Output overlaps because it is written before doing the low comparison.
3324 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3325 break;
3326
3327 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003328 case Primitive::kPrimDouble:
3329 locations->SetInAt(0, Location::RequiresFpuRegister());
3330 locations->SetInAt(1, Location::RequiresFpuRegister());
3331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003332 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003333
3334 default:
3335 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3336 }
3337}
3338
3339void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3340 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003341 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003342 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003343 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003344
3345 // 0 if: left == right
3346 // 1 if: left > right
3347 // -1 if: left < right
3348 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003349 case Primitive::kPrimBoolean:
3350 case Primitive::kPrimByte:
3351 case Primitive::kPrimShort:
3352 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003353 case Primitive::kPrimInt: {
3354 Register lhs = locations->InAt(0).AsRegister<Register>();
3355 Register rhs = locations->InAt(1).AsRegister<Register>();
3356 __ Slt(TMP, lhs, rhs);
3357 __ Slt(res, rhs, lhs);
3358 __ Subu(res, res, TMP);
3359 break;
3360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003361 case Primitive::kPrimLong: {
3362 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003363 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3364 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3365 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3366 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3367 // TODO: more efficient (direct) comparison with a constant.
3368 __ Slt(TMP, lhs_high, rhs_high);
3369 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3370 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3371 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3372 __ Sltu(TMP, lhs_low, rhs_low);
3373 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3374 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3375 __ Bind(&done);
3376 break;
3377 }
3378
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003379 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003380 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003381 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3382 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3383 MipsLabel done;
3384 if (isR6) {
3385 __ CmpEqS(FTMP, lhs, rhs);
3386 __ LoadConst32(res, 0);
3387 __ Bc1nez(FTMP, &done);
3388 if (gt_bias) {
3389 __ CmpLtS(FTMP, lhs, rhs);
3390 __ LoadConst32(res, -1);
3391 __ Bc1nez(FTMP, &done);
3392 __ LoadConst32(res, 1);
3393 } else {
3394 __ CmpLtS(FTMP, rhs, lhs);
3395 __ LoadConst32(res, 1);
3396 __ Bc1nez(FTMP, &done);
3397 __ LoadConst32(res, -1);
3398 }
3399 } else {
3400 if (gt_bias) {
3401 __ ColtS(0, lhs, rhs);
3402 __ LoadConst32(res, -1);
3403 __ Bc1t(0, &done);
3404 __ CeqS(0, lhs, rhs);
3405 __ LoadConst32(res, 1);
3406 __ Movt(res, ZERO, 0);
3407 } else {
3408 __ ColtS(0, rhs, lhs);
3409 __ LoadConst32(res, 1);
3410 __ Bc1t(0, &done);
3411 __ CeqS(0, lhs, rhs);
3412 __ LoadConst32(res, -1);
3413 __ Movt(res, ZERO, 0);
3414 }
3415 }
3416 __ Bind(&done);
3417 break;
3418 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003419 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003420 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003421 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3422 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3423 MipsLabel done;
3424 if (isR6) {
3425 __ CmpEqD(FTMP, lhs, rhs);
3426 __ LoadConst32(res, 0);
3427 __ Bc1nez(FTMP, &done);
3428 if (gt_bias) {
3429 __ CmpLtD(FTMP, lhs, rhs);
3430 __ LoadConst32(res, -1);
3431 __ Bc1nez(FTMP, &done);
3432 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003433 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003434 __ CmpLtD(FTMP, rhs, lhs);
3435 __ LoadConst32(res, 1);
3436 __ Bc1nez(FTMP, &done);
3437 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438 }
3439 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003440 if (gt_bias) {
3441 __ ColtD(0, lhs, rhs);
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 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003448 __ ColtD(0, rhs, lhs);
3449 __ LoadConst32(res, 1);
3450 __ Bc1t(0, &done);
3451 __ CeqD(0, lhs, rhs);
3452 __ LoadConst32(res, -1);
3453 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003454 }
3455 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003456 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003457 break;
3458 }
3459
3460 default:
3461 LOG(FATAL) << "Unimplemented compare type " << in_type;
3462 }
3463}
3464
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003465void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003467 switch (instruction->InputAt(0)->GetType()) {
3468 default:
3469 case Primitive::kPrimLong:
3470 locations->SetInAt(0, Location::RequiresRegister());
3471 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3472 break;
3473
3474 case Primitive::kPrimFloat:
3475 case Primitive::kPrimDouble:
3476 locations->SetInAt(0, Location::RequiresFpuRegister());
3477 locations->SetInAt(1, Location::RequiresFpuRegister());
3478 break;
3479 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003480 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3482 }
3483}
3484
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003485void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003486 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003487 return;
3488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003489
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003490 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003491 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003492
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003493 switch (type) {
3494 default:
3495 // Integer case.
3496 GenerateIntCompare(instruction->GetCondition(), locations);
3497 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003498
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003499 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003500 GenerateLongCompare(instruction->GetCondition(), locations);
3501 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003502
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003503 case Primitive::kPrimFloat:
3504 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003505 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3506 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003507 }
3508}
3509
Alexey Frunze7e99e052015-11-24 19:28:01 -08003510void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3511 DCHECK(instruction->IsDiv() || instruction->IsRem());
3512 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3513
3514 LocationSummary* locations = instruction->GetLocations();
3515 Location second = locations->InAt(1);
3516 DCHECK(second.IsConstant());
3517
3518 Register out = locations->Out().AsRegister<Register>();
3519 Register dividend = locations->InAt(0).AsRegister<Register>();
3520 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3521 DCHECK(imm == 1 || imm == -1);
3522
3523 if (instruction->IsRem()) {
3524 __ Move(out, ZERO);
3525 } else {
3526 if (imm == -1) {
3527 __ Subu(out, ZERO, dividend);
3528 } else if (out != dividend) {
3529 __ Move(out, dividend);
3530 }
3531 }
3532}
3533
3534void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3535 DCHECK(instruction->IsDiv() || instruction->IsRem());
3536 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3537
3538 LocationSummary* locations = instruction->GetLocations();
3539 Location second = locations->InAt(1);
3540 DCHECK(second.IsConstant());
3541
3542 Register out = locations->Out().AsRegister<Register>();
3543 Register dividend = locations->InAt(0).AsRegister<Register>();
3544 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003545 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003546 int ctz_imm = CTZ(abs_imm);
3547
3548 if (instruction->IsDiv()) {
3549 if (ctz_imm == 1) {
3550 // Fast path for division by +/-2, which is very common.
3551 __ Srl(TMP, dividend, 31);
3552 } else {
3553 __ Sra(TMP, dividend, 31);
3554 __ Srl(TMP, TMP, 32 - ctz_imm);
3555 }
3556 __ Addu(out, dividend, TMP);
3557 __ Sra(out, out, ctz_imm);
3558 if (imm < 0) {
3559 __ Subu(out, ZERO, out);
3560 }
3561 } else {
3562 if (ctz_imm == 1) {
3563 // Fast path for modulo +/-2, which is very common.
3564 __ Sra(TMP, dividend, 31);
3565 __ Subu(out, dividend, TMP);
3566 __ Andi(out, out, 1);
3567 __ Addu(out, out, TMP);
3568 } else {
3569 __ Sra(TMP, dividend, 31);
3570 __ Srl(TMP, TMP, 32 - ctz_imm);
3571 __ Addu(out, dividend, TMP);
3572 if (IsUint<16>(abs_imm - 1)) {
3573 __ Andi(out, out, abs_imm - 1);
3574 } else {
3575 __ Sll(out, out, 32 - ctz_imm);
3576 __ Srl(out, out, 32 - ctz_imm);
3577 }
3578 __ Subu(out, out, TMP);
3579 }
3580 }
3581}
3582
3583void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3584 DCHECK(instruction->IsDiv() || instruction->IsRem());
3585 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3586
3587 LocationSummary* locations = instruction->GetLocations();
3588 Location second = locations->InAt(1);
3589 DCHECK(second.IsConstant());
3590
3591 Register out = locations->Out().AsRegister<Register>();
3592 Register dividend = locations->InAt(0).AsRegister<Register>();
3593 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3594
3595 int64_t magic;
3596 int shift;
3597 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3598
3599 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3600
3601 __ LoadConst32(TMP, magic);
3602 if (isR6) {
3603 __ MuhR6(TMP, dividend, TMP);
3604 } else {
3605 __ MultR2(dividend, TMP);
3606 __ Mfhi(TMP);
3607 }
3608 if (imm > 0 && magic < 0) {
3609 __ Addu(TMP, TMP, dividend);
3610 } else if (imm < 0 && magic > 0) {
3611 __ Subu(TMP, TMP, dividend);
3612 }
3613
3614 if (shift != 0) {
3615 __ Sra(TMP, TMP, shift);
3616 }
3617
3618 if (instruction->IsDiv()) {
3619 __ Sra(out, TMP, 31);
3620 __ Subu(out, TMP, out);
3621 } else {
3622 __ Sra(AT, TMP, 31);
3623 __ Subu(AT, TMP, AT);
3624 __ LoadConst32(TMP, imm);
3625 if (isR6) {
3626 __ MulR6(TMP, AT, TMP);
3627 } else {
3628 __ MulR2(TMP, AT, TMP);
3629 }
3630 __ Subu(out, dividend, TMP);
3631 }
3632}
3633
3634void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3635 DCHECK(instruction->IsDiv() || instruction->IsRem());
3636 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3637
3638 LocationSummary* locations = instruction->GetLocations();
3639 Register out = locations->Out().AsRegister<Register>();
3640 Location second = locations->InAt(1);
3641
3642 if (second.IsConstant()) {
3643 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3644 if (imm == 0) {
3645 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3646 } else if (imm == 1 || imm == -1) {
3647 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003648 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003649 DivRemByPowerOfTwo(instruction);
3650 } else {
3651 DCHECK(imm <= -2 || imm >= 2);
3652 GenerateDivRemWithAnyConstant(instruction);
3653 }
3654 } else {
3655 Register dividend = locations->InAt(0).AsRegister<Register>();
3656 Register divisor = second.AsRegister<Register>();
3657 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3658 if (instruction->IsDiv()) {
3659 if (isR6) {
3660 __ DivR6(out, dividend, divisor);
3661 } else {
3662 __ DivR2(out, dividend, divisor);
3663 }
3664 } else {
3665 if (isR6) {
3666 __ ModR6(out, dividend, divisor);
3667 } else {
3668 __ ModR2(out, dividend, divisor);
3669 }
3670 }
3671 }
3672}
3673
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003674void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3675 Primitive::Type type = div->GetResultType();
3676 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003677 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003678 : LocationSummary::kNoCall;
3679
3680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3681
3682 switch (type) {
3683 case Primitive::kPrimInt:
3684 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003685 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3687 break;
3688
3689 case Primitive::kPrimLong: {
3690 InvokeRuntimeCallingConvention calling_convention;
3691 locations->SetInAt(0, Location::RegisterPairLocation(
3692 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3693 locations->SetInAt(1, Location::RegisterPairLocation(
3694 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3695 locations->SetOut(calling_convention.GetReturnLocation(type));
3696 break;
3697 }
3698
3699 case Primitive::kPrimFloat:
3700 case Primitive::kPrimDouble:
3701 locations->SetInAt(0, Location::RequiresFpuRegister());
3702 locations->SetInAt(1, Location::RequiresFpuRegister());
3703 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3704 break;
3705
3706 default:
3707 LOG(FATAL) << "Unexpected div type " << type;
3708 }
3709}
3710
3711void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3712 Primitive::Type type = instruction->GetType();
3713 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003714
3715 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003716 case Primitive::kPrimInt:
3717 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003718 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003719 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003720 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003721 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3722 break;
3723 }
3724 case Primitive::kPrimFloat:
3725 case Primitive::kPrimDouble: {
3726 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3727 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3728 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3729 if (type == Primitive::kPrimFloat) {
3730 __ DivS(dst, lhs, rhs);
3731 } else {
3732 __ DivD(dst, lhs, rhs);
3733 }
3734 break;
3735 }
3736 default:
3737 LOG(FATAL) << "Unexpected div type " << type;
3738 }
3739}
3740
3741void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003742 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003743 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003744}
3745
3746void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3747 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3748 codegen_->AddSlowPath(slow_path);
3749 Location value = instruction->GetLocations()->InAt(0);
3750 Primitive::Type type = instruction->GetType();
3751
3752 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003753 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003754 case Primitive::kPrimByte:
3755 case Primitive::kPrimChar:
3756 case Primitive::kPrimShort:
3757 case Primitive::kPrimInt: {
3758 if (value.IsConstant()) {
3759 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3760 __ B(slow_path->GetEntryLabel());
3761 } else {
3762 // A division by a non-null constant is valid. We don't need to perform
3763 // any check, so simply fall through.
3764 }
3765 } else {
3766 DCHECK(value.IsRegister()) << value;
3767 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3768 }
3769 break;
3770 }
3771 case Primitive::kPrimLong: {
3772 if (value.IsConstant()) {
3773 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3774 __ B(slow_path->GetEntryLabel());
3775 } else {
3776 // A division by a non-null constant is valid. We don't need to perform
3777 // any check, so simply fall through.
3778 }
3779 } else {
3780 DCHECK(value.IsRegisterPair()) << value;
3781 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3782 __ Beqz(TMP, slow_path->GetEntryLabel());
3783 }
3784 break;
3785 }
3786 default:
3787 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3788 }
3789}
3790
3791void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3792 LocationSummary* locations =
3793 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3794 locations->SetOut(Location::ConstantLocation(constant));
3795}
3796
3797void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3798 // Will be generated at use site.
3799}
3800
3801void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3802 exit->SetLocations(nullptr);
3803}
3804
3805void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3806}
3807
3808void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3809 LocationSummary* locations =
3810 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3811 locations->SetOut(Location::ConstantLocation(constant));
3812}
3813
3814void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3815 // Will be generated at use site.
3816}
3817
3818void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3819 got->SetLocations(nullptr);
3820}
3821
3822void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3823 DCHECK(!successor->IsExitBlock());
3824 HBasicBlock* block = got->GetBlock();
3825 HInstruction* previous = got->GetPrevious();
3826 HLoopInformation* info = block->GetLoopInformation();
3827
3828 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3829 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3830 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3831 return;
3832 }
3833 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3834 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3835 }
3836 if (!codegen_->GoesToNextBlock(block, successor)) {
3837 __ B(codegen_->GetLabelOf(successor));
3838 }
3839}
3840
3841void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3842 HandleGoto(got, got->GetSuccessor());
3843}
3844
3845void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3846 try_boundary->SetLocations(nullptr);
3847}
3848
3849void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3850 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3851 if (!successor->IsExitBlock()) {
3852 HandleGoto(try_boundary, successor);
3853 }
3854}
3855
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003856void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3857 LocationSummary* locations) {
3858 Register dst = locations->Out().AsRegister<Register>();
3859 Register lhs = locations->InAt(0).AsRegister<Register>();
3860 Location rhs_location = locations->InAt(1);
3861 Register rhs_reg = ZERO;
3862 int64_t rhs_imm = 0;
3863 bool use_imm = rhs_location.IsConstant();
3864 if (use_imm) {
3865 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3866 } else {
3867 rhs_reg = rhs_location.AsRegister<Register>();
3868 }
3869
3870 switch (cond) {
3871 case kCondEQ:
3872 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003873 if (use_imm && IsInt<16>(-rhs_imm)) {
3874 if (rhs_imm == 0) {
3875 if (cond == kCondEQ) {
3876 __ Sltiu(dst, lhs, 1);
3877 } else {
3878 __ Sltu(dst, ZERO, lhs);
3879 }
3880 } else {
3881 __ Addiu(dst, lhs, -rhs_imm);
3882 if (cond == kCondEQ) {
3883 __ Sltiu(dst, dst, 1);
3884 } else {
3885 __ Sltu(dst, ZERO, dst);
3886 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003887 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003888 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003889 if (use_imm && IsUint<16>(rhs_imm)) {
3890 __ Xori(dst, lhs, rhs_imm);
3891 } else {
3892 if (use_imm) {
3893 rhs_reg = TMP;
3894 __ LoadConst32(rhs_reg, rhs_imm);
3895 }
3896 __ Xor(dst, lhs, rhs_reg);
3897 }
3898 if (cond == kCondEQ) {
3899 __ Sltiu(dst, dst, 1);
3900 } else {
3901 __ Sltu(dst, ZERO, dst);
3902 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003903 }
3904 break;
3905
3906 case kCondLT:
3907 case kCondGE:
3908 if (use_imm && IsInt<16>(rhs_imm)) {
3909 __ Slti(dst, lhs, rhs_imm);
3910 } else {
3911 if (use_imm) {
3912 rhs_reg = TMP;
3913 __ LoadConst32(rhs_reg, rhs_imm);
3914 }
3915 __ Slt(dst, lhs, rhs_reg);
3916 }
3917 if (cond == kCondGE) {
3918 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3919 // only the slt instruction but no sge.
3920 __ Xori(dst, dst, 1);
3921 }
3922 break;
3923
3924 case kCondLE:
3925 case kCondGT:
3926 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3927 // Simulate lhs <= rhs via lhs < rhs + 1.
3928 __ Slti(dst, lhs, rhs_imm + 1);
3929 if (cond == kCondGT) {
3930 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3931 // only the slti instruction but no sgti.
3932 __ Xori(dst, dst, 1);
3933 }
3934 } else {
3935 if (use_imm) {
3936 rhs_reg = TMP;
3937 __ LoadConst32(rhs_reg, rhs_imm);
3938 }
3939 __ Slt(dst, rhs_reg, lhs);
3940 if (cond == kCondLE) {
3941 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3942 // only the slt instruction but no sle.
3943 __ Xori(dst, dst, 1);
3944 }
3945 }
3946 break;
3947
3948 case kCondB:
3949 case kCondAE:
3950 if (use_imm && IsInt<16>(rhs_imm)) {
3951 // Sltiu sign-extends its 16-bit immediate operand before
3952 // the comparison and thus lets us compare directly with
3953 // unsigned values in the ranges [0, 0x7fff] and
3954 // [0xffff8000, 0xffffffff].
3955 __ Sltiu(dst, lhs, rhs_imm);
3956 } else {
3957 if (use_imm) {
3958 rhs_reg = TMP;
3959 __ LoadConst32(rhs_reg, rhs_imm);
3960 }
3961 __ Sltu(dst, lhs, rhs_reg);
3962 }
3963 if (cond == kCondAE) {
3964 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3965 // only the sltu instruction but no sgeu.
3966 __ Xori(dst, dst, 1);
3967 }
3968 break;
3969
3970 case kCondBE:
3971 case kCondA:
3972 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3973 // Simulate lhs <= rhs via lhs < rhs + 1.
3974 // Note that this only works if rhs + 1 does not overflow
3975 // to 0, hence the check above.
3976 // Sltiu sign-extends its 16-bit immediate operand before
3977 // the comparison and thus lets us compare directly with
3978 // unsigned values in the ranges [0, 0x7fff] and
3979 // [0xffff8000, 0xffffffff].
3980 __ Sltiu(dst, lhs, rhs_imm + 1);
3981 if (cond == kCondA) {
3982 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3983 // only the sltiu instruction but no sgtiu.
3984 __ Xori(dst, dst, 1);
3985 }
3986 } else {
3987 if (use_imm) {
3988 rhs_reg = TMP;
3989 __ LoadConst32(rhs_reg, rhs_imm);
3990 }
3991 __ Sltu(dst, rhs_reg, lhs);
3992 if (cond == kCondBE) {
3993 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3994 // only the sltu instruction but no sleu.
3995 __ Xori(dst, dst, 1);
3996 }
3997 }
3998 break;
3999 }
4000}
4001
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004002bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4003 LocationSummary* input_locations,
4004 Register dst) {
4005 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4006 Location rhs_location = input_locations->InAt(1);
4007 Register rhs_reg = ZERO;
4008 int64_t rhs_imm = 0;
4009 bool use_imm = rhs_location.IsConstant();
4010 if (use_imm) {
4011 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4012 } else {
4013 rhs_reg = rhs_location.AsRegister<Register>();
4014 }
4015
4016 switch (cond) {
4017 case kCondEQ:
4018 case kCondNE:
4019 if (use_imm && IsInt<16>(-rhs_imm)) {
4020 __ Addiu(dst, lhs, -rhs_imm);
4021 } else if (use_imm && IsUint<16>(rhs_imm)) {
4022 __ Xori(dst, lhs, rhs_imm);
4023 } else {
4024 if (use_imm) {
4025 rhs_reg = TMP;
4026 __ LoadConst32(rhs_reg, rhs_imm);
4027 }
4028 __ Xor(dst, lhs, rhs_reg);
4029 }
4030 return (cond == kCondEQ);
4031
4032 case kCondLT:
4033 case kCondGE:
4034 if (use_imm && IsInt<16>(rhs_imm)) {
4035 __ Slti(dst, lhs, rhs_imm);
4036 } else {
4037 if (use_imm) {
4038 rhs_reg = TMP;
4039 __ LoadConst32(rhs_reg, rhs_imm);
4040 }
4041 __ Slt(dst, lhs, rhs_reg);
4042 }
4043 return (cond == kCondGE);
4044
4045 case kCondLE:
4046 case kCondGT:
4047 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4048 // Simulate lhs <= rhs via lhs < rhs + 1.
4049 __ Slti(dst, lhs, rhs_imm + 1);
4050 return (cond == kCondGT);
4051 } else {
4052 if (use_imm) {
4053 rhs_reg = TMP;
4054 __ LoadConst32(rhs_reg, rhs_imm);
4055 }
4056 __ Slt(dst, rhs_reg, lhs);
4057 return (cond == kCondLE);
4058 }
4059
4060 case kCondB:
4061 case kCondAE:
4062 if (use_imm && IsInt<16>(rhs_imm)) {
4063 // Sltiu sign-extends its 16-bit immediate operand before
4064 // the comparison and thus lets us compare directly with
4065 // unsigned values in the ranges [0, 0x7fff] and
4066 // [0xffff8000, 0xffffffff].
4067 __ Sltiu(dst, lhs, rhs_imm);
4068 } else {
4069 if (use_imm) {
4070 rhs_reg = TMP;
4071 __ LoadConst32(rhs_reg, rhs_imm);
4072 }
4073 __ Sltu(dst, lhs, rhs_reg);
4074 }
4075 return (cond == kCondAE);
4076
4077 case kCondBE:
4078 case kCondA:
4079 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4080 // Simulate lhs <= rhs via lhs < rhs + 1.
4081 // Note that this only works if rhs + 1 does not overflow
4082 // to 0, hence the check above.
4083 // Sltiu sign-extends its 16-bit immediate operand before
4084 // the comparison and thus lets us compare directly with
4085 // unsigned values in the ranges [0, 0x7fff] and
4086 // [0xffff8000, 0xffffffff].
4087 __ Sltiu(dst, lhs, rhs_imm + 1);
4088 return (cond == kCondA);
4089 } else {
4090 if (use_imm) {
4091 rhs_reg = TMP;
4092 __ LoadConst32(rhs_reg, rhs_imm);
4093 }
4094 __ Sltu(dst, rhs_reg, lhs);
4095 return (cond == kCondBE);
4096 }
4097 }
4098}
4099
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004100void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4101 LocationSummary* locations,
4102 MipsLabel* label) {
4103 Register lhs = locations->InAt(0).AsRegister<Register>();
4104 Location rhs_location = locations->InAt(1);
4105 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004106 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004107 bool use_imm = rhs_location.IsConstant();
4108 if (use_imm) {
4109 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4110 } else {
4111 rhs_reg = rhs_location.AsRegister<Register>();
4112 }
4113
4114 if (use_imm && rhs_imm == 0) {
4115 switch (cond) {
4116 case kCondEQ:
4117 case kCondBE: // <= 0 if zero
4118 __ Beqz(lhs, label);
4119 break;
4120 case kCondNE:
4121 case kCondA: // > 0 if non-zero
4122 __ Bnez(lhs, label);
4123 break;
4124 case kCondLT:
4125 __ Bltz(lhs, label);
4126 break;
4127 case kCondGE:
4128 __ Bgez(lhs, label);
4129 break;
4130 case kCondLE:
4131 __ Blez(lhs, label);
4132 break;
4133 case kCondGT:
4134 __ Bgtz(lhs, label);
4135 break;
4136 case kCondB: // always false
4137 break;
4138 case kCondAE: // always true
4139 __ B(label);
4140 break;
4141 }
4142 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004143 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4144 if (isR6 || !use_imm) {
4145 if (use_imm) {
4146 rhs_reg = TMP;
4147 __ LoadConst32(rhs_reg, rhs_imm);
4148 }
4149 switch (cond) {
4150 case kCondEQ:
4151 __ Beq(lhs, rhs_reg, label);
4152 break;
4153 case kCondNE:
4154 __ Bne(lhs, rhs_reg, label);
4155 break;
4156 case kCondLT:
4157 __ Blt(lhs, rhs_reg, label);
4158 break;
4159 case kCondGE:
4160 __ Bge(lhs, rhs_reg, label);
4161 break;
4162 case kCondLE:
4163 __ Bge(rhs_reg, lhs, label);
4164 break;
4165 case kCondGT:
4166 __ Blt(rhs_reg, lhs, label);
4167 break;
4168 case kCondB:
4169 __ Bltu(lhs, rhs_reg, label);
4170 break;
4171 case kCondAE:
4172 __ Bgeu(lhs, rhs_reg, label);
4173 break;
4174 case kCondBE:
4175 __ Bgeu(rhs_reg, lhs, label);
4176 break;
4177 case kCondA:
4178 __ Bltu(rhs_reg, lhs, label);
4179 break;
4180 }
4181 } else {
4182 // Special cases for more efficient comparison with constants on R2.
4183 switch (cond) {
4184 case kCondEQ:
4185 __ LoadConst32(TMP, rhs_imm);
4186 __ Beq(lhs, TMP, label);
4187 break;
4188 case kCondNE:
4189 __ LoadConst32(TMP, rhs_imm);
4190 __ Bne(lhs, TMP, label);
4191 break;
4192 case kCondLT:
4193 if (IsInt<16>(rhs_imm)) {
4194 __ Slti(TMP, lhs, rhs_imm);
4195 __ Bnez(TMP, label);
4196 } else {
4197 __ LoadConst32(TMP, rhs_imm);
4198 __ Blt(lhs, TMP, label);
4199 }
4200 break;
4201 case kCondGE:
4202 if (IsInt<16>(rhs_imm)) {
4203 __ Slti(TMP, lhs, rhs_imm);
4204 __ Beqz(TMP, label);
4205 } else {
4206 __ LoadConst32(TMP, rhs_imm);
4207 __ Bge(lhs, TMP, label);
4208 }
4209 break;
4210 case kCondLE:
4211 if (IsInt<16>(rhs_imm + 1)) {
4212 // Simulate lhs <= rhs via lhs < rhs + 1.
4213 __ Slti(TMP, lhs, rhs_imm + 1);
4214 __ Bnez(TMP, label);
4215 } else {
4216 __ LoadConst32(TMP, rhs_imm);
4217 __ Bge(TMP, lhs, label);
4218 }
4219 break;
4220 case kCondGT:
4221 if (IsInt<16>(rhs_imm + 1)) {
4222 // Simulate lhs > rhs via !(lhs < rhs + 1).
4223 __ Slti(TMP, lhs, rhs_imm + 1);
4224 __ Beqz(TMP, label);
4225 } else {
4226 __ LoadConst32(TMP, rhs_imm);
4227 __ Blt(TMP, lhs, label);
4228 }
4229 break;
4230 case kCondB:
4231 if (IsInt<16>(rhs_imm)) {
4232 __ Sltiu(TMP, lhs, rhs_imm);
4233 __ Bnez(TMP, label);
4234 } else {
4235 __ LoadConst32(TMP, rhs_imm);
4236 __ Bltu(lhs, TMP, label);
4237 }
4238 break;
4239 case kCondAE:
4240 if (IsInt<16>(rhs_imm)) {
4241 __ Sltiu(TMP, lhs, rhs_imm);
4242 __ Beqz(TMP, label);
4243 } else {
4244 __ LoadConst32(TMP, rhs_imm);
4245 __ Bgeu(lhs, TMP, label);
4246 }
4247 break;
4248 case kCondBE:
4249 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4250 // Simulate lhs <= rhs via lhs < rhs + 1.
4251 // Note that this only works if rhs + 1 does not overflow
4252 // to 0, hence the check above.
4253 __ Sltiu(TMP, lhs, rhs_imm + 1);
4254 __ Bnez(TMP, label);
4255 } else {
4256 __ LoadConst32(TMP, rhs_imm);
4257 __ Bgeu(TMP, lhs, label);
4258 }
4259 break;
4260 case kCondA:
4261 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4262 // Simulate lhs > rhs via !(lhs < rhs + 1).
4263 // Note that this only works if rhs + 1 does not overflow
4264 // to 0, hence the check above.
4265 __ Sltiu(TMP, lhs, rhs_imm + 1);
4266 __ Beqz(TMP, label);
4267 } else {
4268 __ LoadConst32(TMP, rhs_imm);
4269 __ Bltu(TMP, lhs, label);
4270 }
4271 break;
4272 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004273 }
4274 }
4275}
4276
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004277void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4278 LocationSummary* locations) {
4279 Register dst = locations->Out().AsRegister<Register>();
4280 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4281 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4282 Location rhs_location = locations->InAt(1);
4283 Register rhs_high = ZERO;
4284 Register rhs_low = ZERO;
4285 int64_t imm = 0;
4286 uint32_t imm_high = 0;
4287 uint32_t imm_low = 0;
4288 bool use_imm = rhs_location.IsConstant();
4289 if (use_imm) {
4290 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4291 imm_high = High32Bits(imm);
4292 imm_low = Low32Bits(imm);
4293 } else {
4294 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4295 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4296 }
4297 if (use_imm && imm == 0) {
4298 switch (cond) {
4299 case kCondEQ:
4300 case kCondBE: // <= 0 if zero
4301 __ Or(dst, lhs_high, lhs_low);
4302 __ Sltiu(dst, dst, 1);
4303 break;
4304 case kCondNE:
4305 case kCondA: // > 0 if non-zero
4306 __ Or(dst, lhs_high, lhs_low);
4307 __ Sltu(dst, ZERO, dst);
4308 break;
4309 case kCondLT:
4310 __ Slt(dst, lhs_high, ZERO);
4311 break;
4312 case kCondGE:
4313 __ Slt(dst, lhs_high, ZERO);
4314 __ Xori(dst, dst, 1);
4315 break;
4316 case kCondLE:
4317 __ Or(TMP, lhs_high, lhs_low);
4318 __ Sra(AT, lhs_high, 31);
4319 __ Sltu(dst, AT, TMP);
4320 __ Xori(dst, dst, 1);
4321 break;
4322 case kCondGT:
4323 __ Or(TMP, lhs_high, lhs_low);
4324 __ Sra(AT, lhs_high, 31);
4325 __ Sltu(dst, AT, TMP);
4326 break;
4327 case kCondB: // always false
4328 __ Andi(dst, dst, 0);
4329 break;
4330 case kCondAE: // always true
4331 __ Ori(dst, ZERO, 1);
4332 break;
4333 }
4334 } else if (use_imm) {
4335 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4336 switch (cond) {
4337 case kCondEQ:
4338 __ LoadConst32(TMP, imm_high);
4339 __ Xor(TMP, TMP, lhs_high);
4340 __ LoadConst32(AT, imm_low);
4341 __ Xor(AT, AT, lhs_low);
4342 __ Or(dst, TMP, AT);
4343 __ Sltiu(dst, dst, 1);
4344 break;
4345 case kCondNE:
4346 __ LoadConst32(TMP, imm_high);
4347 __ Xor(TMP, TMP, lhs_high);
4348 __ LoadConst32(AT, imm_low);
4349 __ Xor(AT, AT, lhs_low);
4350 __ Or(dst, TMP, AT);
4351 __ Sltu(dst, ZERO, dst);
4352 break;
4353 case kCondLT:
4354 case kCondGE:
4355 if (dst == lhs_low) {
4356 __ LoadConst32(TMP, imm_low);
4357 __ Sltu(dst, lhs_low, TMP);
4358 }
4359 __ LoadConst32(TMP, imm_high);
4360 __ Slt(AT, lhs_high, TMP);
4361 __ Slt(TMP, TMP, lhs_high);
4362 if (dst != lhs_low) {
4363 __ LoadConst32(dst, imm_low);
4364 __ Sltu(dst, lhs_low, dst);
4365 }
4366 __ Slt(dst, TMP, dst);
4367 __ Or(dst, dst, AT);
4368 if (cond == kCondGE) {
4369 __ Xori(dst, dst, 1);
4370 }
4371 break;
4372 case kCondGT:
4373 case kCondLE:
4374 if (dst == lhs_low) {
4375 __ LoadConst32(TMP, imm_low);
4376 __ Sltu(dst, TMP, lhs_low);
4377 }
4378 __ LoadConst32(TMP, imm_high);
4379 __ Slt(AT, TMP, lhs_high);
4380 __ Slt(TMP, lhs_high, TMP);
4381 if (dst != lhs_low) {
4382 __ LoadConst32(dst, imm_low);
4383 __ Sltu(dst, dst, lhs_low);
4384 }
4385 __ Slt(dst, TMP, dst);
4386 __ Or(dst, dst, AT);
4387 if (cond == kCondLE) {
4388 __ Xori(dst, dst, 1);
4389 }
4390 break;
4391 case kCondB:
4392 case kCondAE:
4393 if (dst == lhs_low) {
4394 __ LoadConst32(TMP, imm_low);
4395 __ Sltu(dst, lhs_low, TMP);
4396 }
4397 __ LoadConst32(TMP, imm_high);
4398 __ Sltu(AT, lhs_high, TMP);
4399 __ Sltu(TMP, TMP, lhs_high);
4400 if (dst != lhs_low) {
4401 __ LoadConst32(dst, imm_low);
4402 __ Sltu(dst, lhs_low, dst);
4403 }
4404 __ Slt(dst, TMP, dst);
4405 __ Or(dst, dst, AT);
4406 if (cond == kCondAE) {
4407 __ Xori(dst, dst, 1);
4408 }
4409 break;
4410 case kCondA:
4411 case kCondBE:
4412 if (dst == lhs_low) {
4413 __ LoadConst32(TMP, imm_low);
4414 __ Sltu(dst, TMP, lhs_low);
4415 }
4416 __ LoadConst32(TMP, imm_high);
4417 __ Sltu(AT, TMP, lhs_high);
4418 __ Sltu(TMP, lhs_high, TMP);
4419 if (dst != lhs_low) {
4420 __ LoadConst32(dst, imm_low);
4421 __ Sltu(dst, dst, lhs_low);
4422 }
4423 __ Slt(dst, TMP, dst);
4424 __ Or(dst, dst, AT);
4425 if (cond == kCondBE) {
4426 __ Xori(dst, dst, 1);
4427 }
4428 break;
4429 }
4430 } else {
4431 switch (cond) {
4432 case kCondEQ:
4433 __ Xor(TMP, lhs_high, rhs_high);
4434 __ Xor(AT, lhs_low, rhs_low);
4435 __ Or(dst, TMP, AT);
4436 __ Sltiu(dst, dst, 1);
4437 break;
4438 case kCondNE:
4439 __ Xor(TMP, lhs_high, rhs_high);
4440 __ Xor(AT, lhs_low, rhs_low);
4441 __ Or(dst, TMP, AT);
4442 __ Sltu(dst, ZERO, dst);
4443 break;
4444 case kCondLT:
4445 case kCondGE:
4446 __ Slt(TMP, rhs_high, lhs_high);
4447 __ Sltu(AT, lhs_low, rhs_low);
4448 __ Slt(TMP, TMP, AT);
4449 __ Slt(AT, lhs_high, rhs_high);
4450 __ Or(dst, AT, TMP);
4451 if (cond == kCondGE) {
4452 __ Xori(dst, dst, 1);
4453 }
4454 break;
4455 case kCondGT:
4456 case kCondLE:
4457 __ Slt(TMP, lhs_high, rhs_high);
4458 __ Sltu(AT, rhs_low, lhs_low);
4459 __ Slt(TMP, TMP, AT);
4460 __ Slt(AT, rhs_high, lhs_high);
4461 __ Or(dst, AT, TMP);
4462 if (cond == kCondLE) {
4463 __ Xori(dst, dst, 1);
4464 }
4465 break;
4466 case kCondB:
4467 case kCondAE:
4468 __ Sltu(TMP, rhs_high, lhs_high);
4469 __ Sltu(AT, lhs_low, rhs_low);
4470 __ Slt(TMP, TMP, AT);
4471 __ Sltu(AT, lhs_high, rhs_high);
4472 __ Or(dst, AT, TMP);
4473 if (cond == kCondAE) {
4474 __ Xori(dst, dst, 1);
4475 }
4476 break;
4477 case kCondA:
4478 case kCondBE:
4479 __ Sltu(TMP, lhs_high, rhs_high);
4480 __ Sltu(AT, rhs_low, lhs_low);
4481 __ Slt(TMP, TMP, AT);
4482 __ Sltu(AT, rhs_high, lhs_high);
4483 __ Or(dst, AT, TMP);
4484 if (cond == kCondBE) {
4485 __ Xori(dst, dst, 1);
4486 }
4487 break;
4488 }
4489 }
4490}
4491
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004492void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4493 LocationSummary* locations,
4494 MipsLabel* label) {
4495 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4496 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4497 Location rhs_location = locations->InAt(1);
4498 Register rhs_high = ZERO;
4499 Register rhs_low = ZERO;
4500 int64_t imm = 0;
4501 uint32_t imm_high = 0;
4502 uint32_t imm_low = 0;
4503 bool use_imm = rhs_location.IsConstant();
4504 if (use_imm) {
4505 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4506 imm_high = High32Bits(imm);
4507 imm_low = Low32Bits(imm);
4508 } else {
4509 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4510 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4511 }
4512
4513 if (use_imm && imm == 0) {
4514 switch (cond) {
4515 case kCondEQ:
4516 case kCondBE: // <= 0 if zero
4517 __ Or(TMP, lhs_high, lhs_low);
4518 __ Beqz(TMP, label);
4519 break;
4520 case kCondNE:
4521 case kCondA: // > 0 if non-zero
4522 __ Or(TMP, lhs_high, lhs_low);
4523 __ Bnez(TMP, label);
4524 break;
4525 case kCondLT:
4526 __ Bltz(lhs_high, label);
4527 break;
4528 case kCondGE:
4529 __ Bgez(lhs_high, label);
4530 break;
4531 case kCondLE:
4532 __ Or(TMP, lhs_high, lhs_low);
4533 __ Sra(AT, lhs_high, 31);
4534 __ Bgeu(AT, TMP, label);
4535 break;
4536 case kCondGT:
4537 __ Or(TMP, lhs_high, lhs_low);
4538 __ Sra(AT, lhs_high, 31);
4539 __ Bltu(AT, TMP, label);
4540 break;
4541 case kCondB: // always false
4542 break;
4543 case kCondAE: // always true
4544 __ B(label);
4545 break;
4546 }
4547 } else if (use_imm) {
4548 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4549 switch (cond) {
4550 case kCondEQ:
4551 __ LoadConst32(TMP, imm_high);
4552 __ Xor(TMP, TMP, lhs_high);
4553 __ LoadConst32(AT, imm_low);
4554 __ Xor(AT, AT, lhs_low);
4555 __ Or(TMP, TMP, AT);
4556 __ Beqz(TMP, label);
4557 break;
4558 case kCondNE:
4559 __ LoadConst32(TMP, imm_high);
4560 __ Xor(TMP, TMP, lhs_high);
4561 __ LoadConst32(AT, imm_low);
4562 __ Xor(AT, AT, lhs_low);
4563 __ Or(TMP, TMP, AT);
4564 __ Bnez(TMP, label);
4565 break;
4566 case kCondLT:
4567 __ LoadConst32(TMP, imm_high);
4568 __ Blt(lhs_high, TMP, label);
4569 __ Slt(TMP, TMP, lhs_high);
4570 __ LoadConst32(AT, imm_low);
4571 __ Sltu(AT, lhs_low, AT);
4572 __ Blt(TMP, AT, label);
4573 break;
4574 case kCondGE:
4575 __ LoadConst32(TMP, imm_high);
4576 __ Blt(TMP, lhs_high, label);
4577 __ Slt(TMP, lhs_high, TMP);
4578 __ LoadConst32(AT, imm_low);
4579 __ Sltu(AT, lhs_low, AT);
4580 __ Or(TMP, TMP, AT);
4581 __ Beqz(TMP, label);
4582 break;
4583 case kCondLE:
4584 __ LoadConst32(TMP, imm_high);
4585 __ Blt(lhs_high, TMP, label);
4586 __ Slt(TMP, TMP, lhs_high);
4587 __ LoadConst32(AT, imm_low);
4588 __ Sltu(AT, AT, lhs_low);
4589 __ Or(TMP, TMP, AT);
4590 __ Beqz(TMP, label);
4591 break;
4592 case kCondGT:
4593 __ LoadConst32(TMP, imm_high);
4594 __ Blt(TMP, lhs_high, label);
4595 __ Slt(TMP, lhs_high, TMP);
4596 __ LoadConst32(AT, imm_low);
4597 __ Sltu(AT, AT, lhs_low);
4598 __ Blt(TMP, AT, label);
4599 break;
4600 case kCondB:
4601 __ LoadConst32(TMP, imm_high);
4602 __ Bltu(lhs_high, TMP, label);
4603 __ Sltu(TMP, TMP, lhs_high);
4604 __ LoadConst32(AT, imm_low);
4605 __ Sltu(AT, lhs_low, AT);
4606 __ Blt(TMP, AT, label);
4607 break;
4608 case kCondAE:
4609 __ LoadConst32(TMP, imm_high);
4610 __ Bltu(TMP, lhs_high, label);
4611 __ Sltu(TMP, lhs_high, TMP);
4612 __ LoadConst32(AT, imm_low);
4613 __ Sltu(AT, lhs_low, AT);
4614 __ Or(TMP, TMP, AT);
4615 __ Beqz(TMP, label);
4616 break;
4617 case kCondBE:
4618 __ LoadConst32(TMP, imm_high);
4619 __ Bltu(lhs_high, TMP, label);
4620 __ Sltu(TMP, TMP, lhs_high);
4621 __ LoadConst32(AT, imm_low);
4622 __ Sltu(AT, AT, lhs_low);
4623 __ Or(TMP, TMP, AT);
4624 __ Beqz(TMP, label);
4625 break;
4626 case kCondA:
4627 __ LoadConst32(TMP, imm_high);
4628 __ Bltu(TMP, lhs_high, label);
4629 __ Sltu(TMP, lhs_high, TMP);
4630 __ LoadConst32(AT, imm_low);
4631 __ Sltu(AT, AT, lhs_low);
4632 __ Blt(TMP, AT, label);
4633 break;
4634 }
4635 } else {
4636 switch (cond) {
4637 case kCondEQ:
4638 __ Xor(TMP, lhs_high, rhs_high);
4639 __ Xor(AT, lhs_low, rhs_low);
4640 __ Or(TMP, TMP, AT);
4641 __ Beqz(TMP, label);
4642 break;
4643 case kCondNE:
4644 __ Xor(TMP, lhs_high, rhs_high);
4645 __ Xor(AT, lhs_low, rhs_low);
4646 __ Or(TMP, TMP, AT);
4647 __ Bnez(TMP, label);
4648 break;
4649 case kCondLT:
4650 __ Blt(lhs_high, rhs_high, label);
4651 __ Slt(TMP, rhs_high, lhs_high);
4652 __ Sltu(AT, lhs_low, rhs_low);
4653 __ Blt(TMP, AT, label);
4654 break;
4655 case kCondGE:
4656 __ Blt(rhs_high, lhs_high, label);
4657 __ Slt(TMP, lhs_high, rhs_high);
4658 __ Sltu(AT, lhs_low, rhs_low);
4659 __ Or(TMP, TMP, AT);
4660 __ Beqz(TMP, label);
4661 break;
4662 case kCondLE:
4663 __ Blt(lhs_high, rhs_high, label);
4664 __ Slt(TMP, rhs_high, lhs_high);
4665 __ Sltu(AT, rhs_low, lhs_low);
4666 __ Or(TMP, TMP, AT);
4667 __ Beqz(TMP, label);
4668 break;
4669 case kCondGT:
4670 __ Blt(rhs_high, lhs_high, label);
4671 __ Slt(TMP, lhs_high, rhs_high);
4672 __ Sltu(AT, rhs_low, lhs_low);
4673 __ Blt(TMP, AT, label);
4674 break;
4675 case kCondB:
4676 __ Bltu(lhs_high, rhs_high, label);
4677 __ Sltu(TMP, rhs_high, lhs_high);
4678 __ Sltu(AT, lhs_low, rhs_low);
4679 __ Blt(TMP, AT, label);
4680 break;
4681 case kCondAE:
4682 __ Bltu(rhs_high, lhs_high, label);
4683 __ Sltu(TMP, lhs_high, rhs_high);
4684 __ Sltu(AT, lhs_low, rhs_low);
4685 __ Or(TMP, TMP, AT);
4686 __ Beqz(TMP, label);
4687 break;
4688 case kCondBE:
4689 __ Bltu(lhs_high, rhs_high, label);
4690 __ Sltu(TMP, rhs_high, lhs_high);
4691 __ Sltu(AT, rhs_low, lhs_low);
4692 __ Or(TMP, TMP, AT);
4693 __ Beqz(TMP, label);
4694 break;
4695 case kCondA:
4696 __ Bltu(rhs_high, lhs_high, label);
4697 __ Sltu(TMP, lhs_high, rhs_high);
4698 __ Sltu(AT, rhs_low, lhs_low);
4699 __ Blt(TMP, AT, label);
4700 break;
4701 }
4702 }
4703}
4704
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004705void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4706 bool gt_bias,
4707 Primitive::Type type,
4708 LocationSummary* locations) {
4709 Register dst = locations->Out().AsRegister<Register>();
4710 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4711 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4712 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4713 if (type == Primitive::kPrimFloat) {
4714 if (isR6) {
4715 switch (cond) {
4716 case kCondEQ:
4717 __ CmpEqS(FTMP, lhs, rhs);
4718 __ Mfc1(dst, FTMP);
4719 __ Andi(dst, dst, 1);
4720 break;
4721 case kCondNE:
4722 __ CmpEqS(FTMP, lhs, rhs);
4723 __ Mfc1(dst, FTMP);
4724 __ Addiu(dst, dst, 1);
4725 break;
4726 case kCondLT:
4727 if (gt_bias) {
4728 __ CmpLtS(FTMP, lhs, rhs);
4729 } else {
4730 __ CmpUltS(FTMP, lhs, rhs);
4731 }
4732 __ Mfc1(dst, FTMP);
4733 __ Andi(dst, dst, 1);
4734 break;
4735 case kCondLE:
4736 if (gt_bias) {
4737 __ CmpLeS(FTMP, lhs, rhs);
4738 } else {
4739 __ CmpUleS(FTMP, lhs, rhs);
4740 }
4741 __ Mfc1(dst, FTMP);
4742 __ Andi(dst, dst, 1);
4743 break;
4744 case kCondGT:
4745 if (gt_bias) {
4746 __ CmpUltS(FTMP, rhs, lhs);
4747 } else {
4748 __ CmpLtS(FTMP, rhs, lhs);
4749 }
4750 __ Mfc1(dst, FTMP);
4751 __ Andi(dst, dst, 1);
4752 break;
4753 case kCondGE:
4754 if (gt_bias) {
4755 __ CmpUleS(FTMP, rhs, lhs);
4756 } else {
4757 __ CmpLeS(FTMP, rhs, lhs);
4758 }
4759 __ Mfc1(dst, FTMP);
4760 __ Andi(dst, dst, 1);
4761 break;
4762 default:
4763 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4764 UNREACHABLE();
4765 }
4766 } else {
4767 switch (cond) {
4768 case kCondEQ:
4769 __ CeqS(0, lhs, rhs);
4770 __ LoadConst32(dst, 1);
4771 __ Movf(dst, ZERO, 0);
4772 break;
4773 case kCondNE:
4774 __ CeqS(0, lhs, rhs);
4775 __ LoadConst32(dst, 1);
4776 __ Movt(dst, ZERO, 0);
4777 break;
4778 case kCondLT:
4779 if (gt_bias) {
4780 __ ColtS(0, lhs, rhs);
4781 } else {
4782 __ CultS(0, lhs, rhs);
4783 }
4784 __ LoadConst32(dst, 1);
4785 __ Movf(dst, ZERO, 0);
4786 break;
4787 case kCondLE:
4788 if (gt_bias) {
4789 __ ColeS(0, lhs, rhs);
4790 } else {
4791 __ CuleS(0, lhs, rhs);
4792 }
4793 __ LoadConst32(dst, 1);
4794 __ Movf(dst, ZERO, 0);
4795 break;
4796 case kCondGT:
4797 if (gt_bias) {
4798 __ CultS(0, rhs, lhs);
4799 } else {
4800 __ ColtS(0, rhs, lhs);
4801 }
4802 __ LoadConst32(dst, 1);
4803 __ Movf(dst, ZERO, 0);
4804 break;
4805 case kCondGE:
4806 if (gt_bias) {
4807 __ CuleS(0, rhs, lhs);
4808 } else {
4809 __ ColeS(0, rhs, lhs);
4810 }
4811 __ LoadConst32(dst, 1);
4812 __ Movf(dst, ZERO, 0);
4813 break;
4814 default:
4815 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4816 UNREACHABLE();
4817 }
4818 }
4819 } else {
4820 DCHECK_EQ(type, Primitive::kPrimDouble);
4821 if (isR6) {
4822 switch (cond) {
4823 case kCondEQ:
4824 __ CmpEqD(FTMP, lhs, rhs);
4825 __ Mfc1(dst, FTMP);
4826 __ Andi(dst, dst, 1);
4827 break;
4828 case kCondNE:
4829 __ CmpEqD(FTMP, lhs, rhs);
4830 __ Mfc1(dst, FTMP);
4831 __ Addiu(dst, dst, 1);
4832 break;
4833 case kCondLT:
4834 if (gt_bias) {
4835 __ CmpLtD(FTMP, lhs, rhs);
4836 } else {
4837 __ CmpUltD(FTMP, lhs, rhs);
4838 }
4839 __ Mfc1(dst, FTMP);
4840 __ Andi(dst, dst, 1);
4841 break;
4842 case kCondLE:
4843 if (gt_bias) {
4844 __ CmpLeD(FTMP, lhs, rhs);
4845 } else {
4846 __ CmpUleD(FTMP, lhs, rhs);
4847 }
4848 __ Mfc1(dst, FTMP);
4849 __ Andi(dst, dst, 1);
4850 break;
4851 case kCondGT:
4852 if (gt_bias) {
4853 __ CmpUltD(FTMP, rhs, lhs);
4854 } else {
4855 __ CmpLtD(FTMP, rhs, lhs);
4856 }
4857 __ Mfc1(dst, FTMP);
4858 __ Andi(dst, dst, 1);
4859 break;
4860 case kCondGE:
4861 if (gt_bias) {
4862 __ CmpUleD(FTMP, rhs, lhs);
4863 } else {
4864 __ CmpLeD(FTMP, rhs, lhs);
4865 }
4866 __ Mfc1(dst, FTMP);
4867 __ Andi(dst, dst, 1);
4868 break;
4869 default:
4870 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4871 UNREACHABLE();
4872 }
4873 } else {
4874 switch (cond) {
4875 case kCondEQ:
4876 __ CeqD(0, lhs, rhs);
4877 __ LoadConst32(dst, 1);
4878 __ Movf(dst, ZERO, 0);
4879 break;
4880 case kCondNE:
4881 __ CeqD(0, lhs, rhs);
4882 __ LoadConst32(dst, 1);
4883 __ Movt(dst, ZERO, 0);
4884 break;
4885 case kCondLT:
4886 if (gt_bias) {
4887 __ ColtD(0, lhs, rhs);
4888 } else {
4889 __ CultD(0, lhs, rhs);
4890 }
4891 __ LoadConst32(dst, 1);
4892 __ Movf(dst, ZERO, 0);
4893 break;
4894 case kCondLE:
4895 if (gt_bias) {
4896 __ ColeD(0, lhs, rhs);
4897 } else {
4898 __ CuleD(0, lhs, rhs);
4899 }
4900 __ LoadConst32(dst, 1);
4901 __ Movf(dst, ZERO, 0);
4902 break;
4903 case kCondGT:
4904 if (gt_bias) {
4905 __ CultD(0, rhs, lhs);
4906 } else {
4907 __ ColtD(0, rhs, lhs);
4908 }
4909 __ LoadConst32(dst, 1);
4910 __ Movf(dst, ZERO, 0);
4911 break;
4912 case kCondGE:
4913 if (gt_bias) {
4914 __ CuleD(0, rhs, lhs);
4915 } else {
4916 __ ColeD(0, rhs, lhs);
4917 }
4918 __ LoadConst32(dst, 1);
4919 __ Movf(dst, ZERO, 0);
4920 break;
4921 default:
4922 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4923 UNREACHABLE();
4924 }
4925 }
4926 }
4927}
4928
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004929bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4930 bool gt_bias,
4931 Primitive::Type type,
4932 LocationSummary* input_locations,
4933 int cc) {
4934 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4935 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4936 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4937 if (type == Primitive::kPrimFloat) {
4938 switch (cond) {
4939 case kCondEQ:
4940 __ CeqS(cc, lhs, rhs);
4941 return false;
4942 case kCondNE:
4943 __ CeqS(cc, lhs, rhs);
4944 return true;
4945 case kCondLT:
4946 if (gt_bias) {
4947 __ ColtS(cc, lhs, rhs);
4948 } else {
4949 __ CultS(cc, lhs, rhs);
4950 }
4951 return false;
4952 case kCondLE:
4953 if (gt_bias) {
4954 __ ColeS(cc, lhs, rhs);
4955 } else {
4956 __ CuleS(cc, lhs, rhs);
4957 }
4958 return false;
4959 case kCondGT:
4960 if (gt_bias) {
4961 __ CultS(cc, rhs, lhs);
4962 } else {
4963 __ ColtS(cc, rhs, lhs);
4964 }
4965 return false;
4966 case kCondGE:
4967 if (gt_bias) {
4968 __ CuleS(cc, rhs, lhs);
4969 } else {
4970 __ ColeS(cc, rhs, lhs);
4971 }
4972 return false;
4973 default:
4974 LOG(FATAL) << "Unexpected non-floating-point condition";
4975 UNREACHABLE();
4976 }
4977 } else {
4978 DCHECK_EQ(type, Primitive::kPrimDouble);
4979 switch (cond) {
4980 case kCondEQ:
4981 __ CeqD(cc, lhs, rhs);
4982 return false;
4983 case kCondNE:
4984 __ CeqD(cc, lhs, rhs);
4985 return true;
4986 case kCondLT:
4987 if (gt_bias) {
4988 __ ColtD(cc, lhs, rhs);
4989 } else {
4990 __ CultD(cc, lhs, rhs);
4991 }
4992 return false;
4993 case kCondLE:
4994 if (gt_bias) {
4995 __ ColeD(cc, lhs, rhs);
4996 } else {
4997 __ CuleD(cc, lhs, rhs);
4998 }
4999 return false;
5000 case kCondGT:
5001 if (gt_bias) {
5002 __ CultD(cc, rhs, lhs);
5003 } else {
5004 __ ColtD(cc, rhs, lhs);
5005 }
5006 return false;
5007 case kCondGE:
5008 if (gt_bias) {
5009 __ CuleD(cc, rhs, lhs);
5010 } else {
5011 __ ColeD(cc, rhs, lhs);
5012 }
5013 return false;
5014 default:
5015 LOG(FATAL) << "Unexpected non-floating-point condition";
5016 UNREACHABLE();
5017 }
5018 }
5019}
5020
5021bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5022 bool gt_bias,
5023 Primitive::Type type,
5024 LocationSummary* input_locations,
5025 FRegister dst) {
5026 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5027 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5028 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
5029 if (type == Primitive::kPrimFloat) {
5030 switch (cond) {
5031 case kCondEQ:
5032 __ CmpEqS(dst, lhs, rhs);
5033 return false;
5034 case kCondNE:
5035 __ CmpEqS(dst, lhs, rhs);
5036 return true;
5037 case kCondLT:
5038 if (gt_bias) {
5039 __ CmpLtS(dst, lhs, rhs);
5040 } else {
5041 __ CmpUltS(dst, lhs, rhs);
5042 }
5043 return false;
5044 case kCondLE:
5045 if (gt_bias) {
5046 __ CmpLeS(dst, lhs, rhs);
5047 } else {
5048 __ CmpUleS(dst, lhs, rhs);
5049 }
5050 return false;
5051 case kCondGT:
5052 if (gt_bias) {
5053 __ CmpUltS(dst, rhs, lhs);
5054 } else {
5055 __ CmpLtS(dst, rhs, lhs);
5056 }
5057 return false;
5058 case kCondGE:
5059 if (gt_bias) {
5060 __ CmpUleS(dst, rhs, lhs);
5061 } else {
5062 __ CmpLeS(dst, rhs, lhs);
5063 }
5064 return false;
5065 default:
5066 LOG(FATAL) << "Unexpected non-floating-point condition";
5067 UNREACHABLE();
5068 }
5069 } else {
5070 DCHECK_EQ(type, Primitive::kPrimDouble);
5071 switch (cond) {
5072 case kCondEQ:
5073 __ CmpEqD(dst, lhs, rhs);
5074 return false;
5075 case kCondNE:
5076 __ CmpEqD(dst, lhs, rhs);
5077 return true;
5078 case kCondLT:
5079 if (gt_bias) {
5080 __ CmpLtD(dst, lhs, rhs);
5081 } else {
5082 __ CmpUltD(dst, lhs, rhs);
5083 }
5084 return false;
5085 case kCondLE:
5086 if (gt_bias) {
5087 __ CmpLeD(dst, lhs, rhs);
5088 } else {
5089 __ CmpUleD(dst, lhs, rhs);
5090 }
5091 return false;
5092 case kCondGT:
5093 if (gt_bias) {
5094 __ CmpUltD(dst, rhs, lhs);
5095 } else {
5096 __ CmpLtD(dst, rhs, lhs);
5097 }
5098 return false;
5099 case kCondGE:
5100 if (gt_bias) {
5101 __ CmpUleD(dst, rhs, lhs);
5102 } else {
5103 __ CmpLeD(dst, rhs, lhs);
5104 }
5105 return false;
5106 default:
5107 LOG(FATAL) << "Unexpected non-floating-point condition";
5108 UNREACHABLE();
5109 }
5110 }
5111}
5112
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005113void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5114 bool gt_bias,
5115 Primitive::Type type,
5116 LocationSummary* locations,
5117 MipsLabel* label) {
5118 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5119 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5120 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5121 if (type == Primitive::kPrimFloat) {
5122 if (isR6) {
5123 switch (cond) {
5124 case kCondEQ:
5125 __ CmpEqS(FTMP, lhs, rhs);
5126 __ Bc1nez(FTMP, label);
5127 break;
5128 case kCondNE:
5129 __ CmpEqS(FTMP, lhs, rhs);
5130 __ Bc1eqz(FTMP, label);
5131 break;
5132 case kCondLT:
5133 if (gt_bias) {
5134 __ CmpLtS(FTMP, lhs, rhs);
5135 } else {
5136 __ CmpUltS(FTMP, lhs, rhs);
5137 }
5138 __ Bc1nez(FTMP, label);
5139 break;
5140 case kCondLE:
5141 if (gt_bias) {
5142 __ CmpLeS(FTMP, lhs, rhs);
5143 } else {
5144 __ CmpUleS(FTMP, lhs, rhs);
5145 }
5146 __ Bc1nez(FTMP, label);
5147 break;
5148 case kCondGT:
5149 if (gt_bias) {
5150 __ CmpUltS(FTMP, rhs, lhs);
5151 } else {
5152 __ CmpLtS(FTMP, rhs, lhs);
5153 }
5154 __ Bc1nez(FTMP, label);
5155 break;
5156 case kCondGE:
5157 if (gt_bias) {
5158 __ CmpUleS(FTMP, rhs, lhs);
5159 } else {
5160 __ CmpLeS(FTMP, rhs, lhs);
5161 }
5162 __ Bc1nez(FTMP, label);
5163 break;
5164 default:
5165 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005166 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005167 }
5168 } else {
5169 switch (cond) {
5170 case kCondEQ:
5171 __ CeqS(0, lhs, rhs);
5172 __ Bc1t(0, label);
5173 break;
5174 case kCondNE:
5175 __ CeqS(0, lhs, rhs);
5176 __ Bc1f(0, label);
5177 break;
5178 case kCondLT:
5179 if (gt_bias) {
5180 __ ColtS(0, lhs, rhs);
5181 } else {
5182 __ CultS(0, lhs, rhs);
5183 }
5184 __ Bc1t(0, label);
5185 break;
5186 case kCondLE:
5187 if (gt_bias) {
5188 __ ColeS(0, lhs, rhs);
5189 } else {
5190 __ CuleS(0, lhs, rhs);
5191 }
5192 __ Bc1t(0, label);
5193 break;
5194 case kCondGT:
5195 if (gt_bias) {
5196 __ CultS(0, rhs, lhs);
5197 } else {
5198 __ ColtS(0, rhs, lhs);
5199 }
5200 __ Bc1t(0, label);
5201 break;
5202 case kCondGE:
5203 if (gt_bias) {
5204 __ CuleS(0, rhs, lhs);
5205 } else {
5206 __ ColeS(0, rhs, lhs);
5207 }
5208 __ Bc1t(0, label);
5209 break;
5210 default:
5211 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005212 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005213 }
5214 }
5215 } else {
5216 DCHECK_EQ(type, Primitive::kPrimDouble);
5217 if (isR6) {
5218 switch (cond) {
5219 case kCondEQ:
5220 __ CmpEqD(FTMP, lhs, rhs);
5221 __ Bc1nez(FTMP, label);
5222 break;
5223 case kCondNE:
5224 __ CmpEqD(FTMP, lhs, rhs);
5225 __ Bc1eqz(FTMP, label);
5226 break;
5227 case kCondLT:
5228 if (gt_bias) {
5229 __ CmpLtD(FTMP, lhs, rhs);
5230 } else {
5231 __ CmpUltD(FTMP, lhs, rhs);
5232 }
5233 __ Bc1nez(FTMP, label);
5234 break;
5235 case kCondLE:
5236 if (gt_bias) {
5237 __ CmpLeD(FTMP, lhs, rhs);
5238 } else {
5239 __ CmpUleD(FTMP, lhs, rhs);
5240 }
5241 __ Bc1nez(FTMP, label);
5242 break;
5243 case kCondGT:
5244 if (gt_bias) {
5245 __ CmpUltD(FTMP, rhs, lhs);
5246 } else {
5247 __ CmpLtD(FTMP, rhs, lhs);
5248 }
5249 __ Bc1nez(FTMP, label);
5250 break;
5251 case kCondGE:
5252 if (gt_bias) {
5253 __ CmpUleD(FTMP, rhs, lhs);
5254 } else {
5255 __ CmpLeD(FTMP, rhs, lhs);
5256 }
5257 __ Bc1nez(FTMP, label);
5258 break;
5259 default:
5260 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005261 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005262 }
5263 } else {
5264 switch (cond) {
5265 case kCondEQ:
5266 __ CeqD(0, lhs, rhs);
5267 __ Bc1t(0, label);
5268 break;
5269 case kCondNE:
5270 __ CeqD(0, lhs, rhs);
5271 __ Bc1f(0, label);
5272 break;
5273 case kCondLT:
5274 if (gt_bias) {
5275 __ ColtD(0, lhs, rhs);
5276 } else {
5277 __ CultD(0, lhs, rhs);
5278 }
5279 __ Bc1t(0, label);
5280 break;
5281 case kCondLE:
5282 if (gt_bias) {
5283 __ ColeD(0, lhs, rhs);
5284 } else {
5285 __ CuleD(0, lhs, rhs);
5286 }
5287 __ Bc1t(0, label);
5288 break;
5289 case kCondGT:
5290 if (gt_bias) {
5291 __ CultD(0, rhs, lhs);
5292 } else {
5293 __ ColtD(0, rhs, lhs);
5294 }
5295 __ Bc1t(0, label);
5296 break;
5297 case kCondGE:
5298 if (gt_bias) {
5299 __ CuleD(0, rhs, lhs);
5300 } else {
5301 __ ColeD(0, rhs, lhs);
5302 }
5303 __ Bc1t(0, label);
5304 break;
5305 default:
5306 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005307 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005308 }
5309 }
5310 }
5311}
5312
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005313void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005314 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005315 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005316 MipsLabel* false_target) {
5317 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005318
David Brazdil0debae72015-11-12 18:37:00 +00005319 if (true_target == nullptr && false_target == nullptr) {
5320 // Nothing to do. The code always falls through.
5321 return;
5322 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005323 // Constant condition, statically compared against "true" (integer value 1).
5324 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005325 if (true_target != nullptr) {
5326 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005327 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005328 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005329 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005330 if (false_target != nullptr) {
5331 __ B(false_target);
5332 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005333 }
David Brazdil0debae72015-11-12 18:37:00 +00005334 return;
5335 }
5336
5337 // The following code generates these patterns:
5338 // (1) true_target == nullptr && false_target != nullptr
5339 // - opposite condition true => branch to false_target
5340 // (2) true_target != nullptr && false_target == nullptr
5341 // - condition true => branch to true_target
5342 // (3) true_target != nullptr && false_target != nullptr
5343 // - condition true => branch to true_target
5344 // - branch to false_target
5345 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005346 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005347 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005348 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005349 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005350 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5351 } else {
5352 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5353 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005354 } else {
5355 // The condition instruction has not been materialized, use its inputs as
5356 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005357 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005358 Primitive::Type type = condition->InputAt(0)->GetType();
5359 LocationSummary* locations = cond->GetLocations();
5360 IfCondition if_cond = condition->GetCondition();
5361 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005362
David Brazdil0debae72015-11-12 18:37:00 +00005363 if (true_target == nullptr) {
5364 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005365 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005366 }
5367
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005368 switch (type) {
5369 default:
5370 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5371 break;
5372 case Primitive::kPrimLong:
5373 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5374 break;
5375 case Primitive::kPrimFloat:
5376 case Primitive::kPrimDouble:
5377 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5378 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005379 }
5380 }
David Brazdil0debae72015-11-12 18:37:00 +00005381
5382 // If neither branch falls through (case 3), the conditional branch to `true_target`
5383 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5384 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005385 __ B(false_target);
5386 }
5387}
5388
5389void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5390 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005391 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005392 locations->SetInAt(0, Location::RequiresRegister());
5393 }
5394}
5395
5396void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005397 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5398 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5399 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5400 nullptr : codegen_->GetLabelOf(true_successor);
5401 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5402 nullptr : codegen_->GetLabelOf(false_successor);
5403 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005404}
5405
5406void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5407 LocationSummary* locations = new (GetGraph()->GetArena())
5408 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005409 InvokeRuntimeCallingConvention calling_convention;
5410 RegisterSet caller_saves = RegisterSet::Empty();
5411 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5412 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005413 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005414 locations->SetInAt(0, Location::RequiresRegister());
5415 }
5416}
5417
5418void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005419 SlowPathCodeMIPS* slow_path =
5420 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005421 GenerateTestAndBranch(deoptimize,
5422 /* condition_input_index */ 0,
5423 slow_path->GetEntryLabel(),
5424 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005425}
5426
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005427// This function returns true if a conditional move can be generated for HSelect.
5428// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5429// branches and regular moves.
5430//
5431// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5432//
5433// While determining feasibility of a conditional move and setting inputs/outputs
5434// are two distinct tasks, this function does both because they share quite a bit
5435// of common logic.
5436static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5437 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5438 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5439 HCondition* condition = cond->AsCondition();
5440
5441 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5442 Primitive::Type dst_type = select->GetType();
5443
5444 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5445 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5446 bool is_true_value_zero_constant =
5447 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5448 bool is_false_value_zero_constant =
5449 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5450
5451 bool can_move_conditionally = false;
5452 bool use_const_for_false_in = false;
5453 bool use_const_for_true_in = false;
5454
5455 if (!cond->IsConstant()) {
5456 switch (cond_type) {
5457 default:
5458 switch (dst_type) {
5459 default:
5460 // Moving int on int condition.
5461 if (is_r6) {
5462 if (is_true_value_zero_constant) {
5463 // seleqz out_reg, false_reg, cond_reg
5464 can_move_conditionally = true;
5465 use_const_for_true_in = true;
5466 } else if (is_false_value_zero_constant) {
5467 // selnez out_reg, true_reg, cond_reg
5468 can_move_conditionally = true;
5469 use_const_for_false_in = true;
5470 } else if (materialized) {
5471 // Not materializing unmaterialized int conditions
5472 // to keep the instruction count low.
5473 // selnez AT, true_reg, cond_reg
5474 // seleqz TMP, false_reg, cond_reg
5475 // or out_reg, AT, TMP
5476 can_move_conditionally = true;
5477 }
5478 } else {
5479 // movn out_reg, true_reg/ZERO, cond_reg
5480 can_move_conditionally = true;
5481 use_const_for_true_in = is_true_value_zero_constant;
5482 }
5483 break;
5484 case Primitive::kPrimLong:
5485 // Moving long on int condition.
5486 if (is_r6) {
5487 if (is_true_value_zero_constant) {
5488 // seleqz out_reg_lo, false_reg_lo, cond_reg
5489 // seleqz out_reg_hi, false_reg_hi, cond_reg
5490 can_move_conditionally = true;
5491 use_const_for_true_in = true;
5492 } else if (is_false_value_zero_constant) {
5493 // selnez out_reg_lo, true_reg_lo, cond_reg
5494 // selnez out_reg_hi, true_reg_hi, cond_reg
5495 can_move_conditionally = true;
5496 use_const_for_false_in = true;
5497 }
5498 // Other long conditional moves would generate 6+ instructions,
5499 // which is too many.
5500 } else {
5501 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5502 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5503 can_move_conditionally = true;
5504 use_const_for_true_in = is_true_value_zero_constant;
5505 }
5506 break;
5507 case Primitive::kPrimFloat:
5508 case Primitive::kPrimDouble:
5509 // Moving float/double on int condition.
5510 if (is_r6) {
5511 if (materialized) {
5512 // Not materializing unmaterialized int conditions
5513 // to keep the instruction count low.
5514 can_move_conditionally = true;
5515 if (is_true_value_zero_constant) {
5516 // sltu TMP, ZERO, cond_reg
5517 // mtc1 TMP, temp_cond_reg
5518 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5519 use_const_for_true_in = true;
5520 } else if (is_false_value_zero_constant) {
5521 // sltu TMP, ZERO, cond_reg
5522 // mtc1 TMP, temp_cond_reg
5523 // selnez.fmt out_reg, true_reg, temp_cond_reg
5524 use_const_for_false_in = true;
5525 } else {
5526 // sltu TMP, ZERO, cond_reg
5527 // mtc1 TMP, temp_cond_reg
5528 // sel.fmt temp_cond_reg, false_reg, true_reg
5529 // mov.fmt out_reg, temp_cond_reg
5530 }
5531 }
5532 } else {
5533 // movn.fmt out_reg, true_reg, cond_reg
5534 can_move_conditionally = true;
5535 }
5536 break;
5537 }
5538 break;
5539 case Primitive::kPrimLong:
5540 // We don't materialize long comparison now
5541 // and use conditional branches instead.
5542 break;
5543 case Primitive::kPrimFloat:
5544 case Primitive::kPrimDouble:
5545 switch (dst_type) {
5546 default:
5547 // Moving int on float/double condition.
5548 if (is_r6) {
5549 if (is_true_value_zero_constant) {
5550 // mfc1 TMP, temp_cond_reg
5551 // seleqz out_reg, false_reg, TMP
5552 can_move_conditionally = true;
5553 use_const_for_true_in = true;
5554 } else if (is_false_value_zero_constant) {
5555 // mfc1 TMP, temp_cond_reg
5556 // selnez out_reg, true_reg, TMP
5557 can_move_conditionally = true;
5558 use_const_for_false_in = true;
5559 } else {
5560 // mfc1 TMP, temp_cond_reg
5561 // selnez AT, true_reg, TMP
5562 // seleqz TMP, false_reg, TMP
5563 // or out_reg, AT, TMP
5564 can_move_conditionally = true;
5565 }
5566 } else {
5567 // movt out_reg, true_reg/ZERO, cc
5568 can_move_conditionally = true;
5569 use_const_for_true_in = is_true_value_zero_constant;
5570 }
5571 break;
5572 case Primitive::kPrimLong:
5573 // Moving long on float/double condition.
5574 if (is_r6) {
5575 if (is_true_value_zero_constant) {
5576 // mfc1 TMP, temp_cond_reg
5577 // seleqz out_reg_lo, false_reg_lo, TMP
5578 // seleqz out_reg_hi, false_reg_hi, TMP
5579 can_move_conditionally = true;
5580 use_const_for_true_in = true;
5581 } else if (is_false_value_zero_constant) {
5582 // mfc1 TMP, temp_cond_reg
5583 // selnez out_reg_lo, true_reg_lo, TMP
5584 // selnez out_reg_hi, true_reg_hi, TMP
5585 can_move_conditionally = true;
5586 use_const_for_false_in = true;
5587 }
5588 // Other long conditional moves would generate 6+ instructions,
5589 // which is too many.
5590 } else {
5591 // movt out_reg_lo, true_reg_lo/ZERO, cc
5592 // movt out_reg_hi, true_reg_hi/ZERO, cc
5593 can_move_conditionally = true;
5594 use_const_for_true_in = is_true_value_zero_constant;
5595 }
5596 break;
5597 case Primitive::kPrimFloat:
5598 case Primitive::kPrimDouble:
5599 // Moving float/double on float/double condition.
5600 if (is_r6) {
5601 can_move_conditionally = true;
5602 if (is_true_value_zero_constant) {
5603 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5604 use_const_for_true_in = true;
5605 } else if (is_false_value_zero_constant) {
5606 // selnez.fmt out_reg, true_reg, temp_cond_reg
5607 use_const_for_false_in = true;
5608 } else {
5609 // sel.fmt temp_cond_reg, false_reg, true_reg
5610 // mov.fmt out_reg, temp_cond_reg
5611 }
5612 } else {
5613 // movt.fmt out_reg, true_reg, cc
5614 can_move_conditionally = true;
5615 }
5616 break;
5617 }
5618 break;
5619 }
5620 }
5621
5622 if (can_move_conditionally) {
5623 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5624 } else {
5625 DCHECK(!use_const_for_false_in);
5626 DCHECK(!use_const_for_true_in);
5627 }
5628
5629 if (locations_to_set != nullptr) {
5630 if (use_const_for_false_in) {
5631 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5632 } else {
5633 locations_to_set->SetInAt(0,
5634 Primitive::IsFloatingPointType(dst_type)
5635 ? Location::RequiresFpuRegister()
5636 : Location::RequiresRegister());
5637 }
5638 if (use_const_for_true_in) {
5639 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5640 } else {
5641 locations_to_set->SetInAt(1,
5642 Primitive::IsFloatingPointType(dst_type)
5643 ? Location::RequiresFpuRegister()
5644 : Location::RequiresRegister());
5645 }
5646 if (materialized) {
5647 locations_to_set->SetInAt(2, Location::RequiresRegister());
5648 }
5649 // On R6 we don't require the output to be the same as the
5650 // first input for conditional moves unlike on R2.
5651 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5652 if (is_out_same_as_first_in) {
5653 locations_to_set->SetOut(Location::SameAsFirstInput());
5654 } else {
5655 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5656 ? Location::RequiresFpuRegister()
5657 : Location::RequiresRegister());
5658 }
5659 }
5660
5661 return can_move_conditionally;
5662}
5663
5664void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5665 LocationSummary* locations = select->GetLocations();
5666 Location dst = locations->Out();
5667 Location src = locations->InAt(1);
5668 Register src_reg = ZERO;
5669 Register src_reg_high = ZERO;
5670 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5671 Register cond_reg = TMP;
5672 int cond_cc = 0;
5673 Primitive::Type cond_type = Primitive::kPrimInt;
5674 bool cond_inverted = false;
5675 Primitive::Type dst_type = select->GetType();
5676
5677 if (IsBooleanValueOrMaterializedCondition(cond)) {
5678 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5679 } else {
5680 HCondition* condition = cond->AsCondition();
5681 LocationSummary* cond_locations = cond->GetLocations();
5682 IfCondition if_cond = condition->GetCondition();
5683 cond_type = condition->InputAt(0)->GetType();
5684 switch (cond_type) {
5685 default:
5686 DCHECK_NE(cond_type, Primitive::kPrimLong);
5687 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5688 break;
5689 case Primitive::kPrimFloat:
5690 case Primitive::kPrimDouble:
5691 cond_inverted = MaterializeFpCompareR2(if_cond,
5692 condition->IsGtBias(),
5693 cond_type,
5694 cond_locations,
5695 cond_cc);
5696 break;
5697 }
5698 }
5699
5700 DCHECK(dst.Equals(locations->InAt(0)));
5701 if (src.IsRegister()) {
5702 src_reg = src.AsRegister<Register>();
5703 } else if (src.IsRegisterPair()) {
5704 src_reg = src.AsRegisterPairLow<Register>();
5705 src_reg_high = src.AsRegisterPairHigh<Register>();
5706 } else if (src.IsConstant()) {
5707 DCHECK(src.GetConstant()->IsZeroBitPattern());
5708 }
5709
5710 switch (cond_type) {
5711 default:
5712 switch (dst_type) {
5713 default:
5714 if (cond_inverted) {
5715 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5716 } else {
5717 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5718 }
5719 break;
5720 case Primitive::kPrimLong:
5721 if (cond_inverted) {
5722 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5723 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5724 } else {
5725 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5726 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5727 }
5728 break;
5729 case Primitive::kPrimFloat:
5730 if (cond_inverted) {
5731 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5732 } else {
5733 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5734 }
5735 break;
5736 case Primitive::kPrimDouble:
5737 if (cond_inverted) {
5738 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5739 } else {
5740 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5741 }
5742 break;
5743 }
5744 break;
5745 case Primitive::kPrimLong:
5746 LOG(FATAL) << "Unreachable";
5747 UNREACHABLE();
5748 case Primitive::kPrimFloat:
5749 case Primitive::kPrimDouble:
5750 switch (dst_type) {
5751 default:
5752 if (cond_inverted) {
5753 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5754 } else {
5755 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5756 }
5757 break;
5758 case Primitive::kPrimLong:
5759 if (cond_inverted) {
5760 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5761 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5762 } else {
5763 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5764 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5765 }
5766 break;
5767 case Primitive::kPrimFloat:
5768 if (cond_inverted) {
5769 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5770 } else {
5771 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5772 }
5773 break;
5774 case Primitive::kPrimDouble:
5775 if (cond_inverted) {
5776 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5777 } else {
5778 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5779 }
5780 break;
5781 }
5782 break;
5783 }
5784}
5785
5786void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5787 LocationSummary* locations = select->GetLocations();
5788 Location dst = locations->Out();
5789 Location false_src = locations->InAt(0);
5790 Location true_src = locations->InAt(1);
5791 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5792 Register cond_reg = TMP;
5793 FRegister fcond_reg = FTMP;
5794 Primitive::Type cond_type = Primitive::kPrimInt;
5795 bool cond_inverted = false;
5796 Primitive::Type dst_type = select->GetType();
5797
5798 if (IsBooleanValueOrMaterializedCondition(cond)) {
5799 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5800 } else {
5801 HCondition* condition = cond->AsCondition();
5802 LocationSummary* cond_locations = cond->GetLocations();
5803 IfCondition if_cond = condition->GetCondition();
5804 cond_type = condition->InputAt(0)->GetType();
5805 switch (cond_type) {
5806 default:
5807 DCHECK_NE(cond_type, Primitive::kPrimLong);
5808 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5809 break;
5810 case Primitive::kPrimFloat:
5811 case Primitive::kPrimDouble:
5812 cond_inverted = MaterializeFpCompareR6(if_cond,
5813 condition->IsGtBias(),
5814 cond_type,
5815 cond_locations,
5816 fcond_reg);
5817 break;
5818 }
5819 }
5820
5821 if (true_src.IsConstant()) {
5822 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5823 }
5824 if (false_src.IsConstant()) {
5825 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5826 }
5827
5828 switch (dst_type) {
5829 default:
5830 if (Primitive::IsFloatingPointType(cond_type)) {
5831 __ Mfc1(cond_reg, fcond_reg);
5832 }
5833 if (true_src.IsConstant()) {
5834 if (cond_inverted) {
5835 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5836 } else {
5837 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5838 }
5839 } else if (false_src.IsConstant()) {
5840 if (cond_inverted) {
5841 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5842 } else {
5843 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5844 }
5845 } else {
5846 DCHECK_NE(cond_reg, AT);
5847 if (cond_inverted) {
5848 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5849 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5850 } else {
5851 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5852 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5853 }
5854 __ Or(dst.AsRegister<Register>(), AT, TMP);
5855 }
5856 break;
5857 case Primitive::kPrimLong: {
5858 if (Primitive::IsFloatingPointType(cond_type)) {
5859 __ Mfc1(cond_reg, fcond_reg);
5860 }
5861 Register dst_lo = dst.AsRegisterPairLow<Register>();
5862 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5863 if (true_src.IsConstant()) {
5864 Register src_lo = false_src.AsRegisterPairLow<Register>();
5865 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5866 if (cond_inverted) {
5867 __ Selnez(dst_lo, src_lo, cond_reg);
5868 __ Selnez(dst_hi, src_hi, cond_reg);
5869 } else {
5870 __ Seleqz(dst_lo, src_lo, cond_reg);
5871 __ Seleqz(dst_hi, src_hi, cond_reg);
5872 }
5873 } else {
5874 DCHECK(false_src.IsConstant());
5875 Register src_lo = true_src.AsRegisterPairLow<Register>();
5876 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5877 if (cond_inverted) {
5878 __ Seleqz(dst_lo, src_lo, cond_reg);
5879 __ Seleqz(dst_hi, src_hi, cond_reg);
5880 } else {
5881 __ Selnez(dst_lo, src_lo, cond_reg);
5882 __ Selnez(dst_hi, src_hi, cond_reg);
5883 }
5884 }
5885 break;
5886 }
5887 case Primitive::kPrimFloat: {
5888 if (!Primitive::IsFloatingPointType(cond_type)) {
5889 // sel*.fmt tests bit 0 of the condition register, account for that.
5890 __ Sltu(TMP, ZERO, cond_reg);
5891 __ Mtc1(TMP, fcond_reg);
5892 }
5893 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5894 if (true_src.IsConstant()) {
5895 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5896 if (cond_inverted) {
5897 __ SelnezS(dst_reg, src_reg, fcond_reg);
5898 } else {
5899 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5900 }
5901 } else if (false_src.IsConstant()) {
5902 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5903 if (cond_inverted) {
5904 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5905 } else {
5906 __ SelnezS(dst_reg, src_reg, fcond_reg);
5907 }
5908 } else {
5909 if (cond_inverted) {
5910 __ SelS(fcond_reg,
5911 true_src.AsFpuRegister<FRegister>(),
5912 false_src.AsFpuRegister<FRegister>());
5913 } else {
5914 __ SelS(fcond_reg,
5915 false_src.AsFpuRegister<FRegister>(),
5916 true_src.AsFpuRegister<FRegister>());
5917 }
5918 __ MovS(dst_reg, fcond_reg);
5919 }
5920 break;
5921 }
5922 case Primitive::kPrimDouble: {
5923 if (!Primitive::IsFloatingPointType(cond_type)) {
5924 // sel*.fmt tests bit 0 of the condition register, account for that.
5925 __ Sltu(TMP, ZERO, cond_reg);
5926 __ Mtc1(TMP, fcond_reg);
5927 }
5928 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5929 if (true_src.IsConstant()) {
5930 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5931 if (cond_inverted) {
5932 __ SelnezD(dst_reg, src_reg, fcond_reg);
5933 } else {
5934 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5935 }
5936 } else if (false_src.IsConstant()) {
5937 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5938 if (cond_inverted) {
5939 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5940 } else {
5941 __ SelnezD(dst_reg, src_reg, fcond_reg);
5942 }
5943 } else {
5944 if (cond_inverted) {
5945 __ SelD(fcond_reg,
5946 true_src.AsFpuRegister<FRegister>(),
5947 false_src.AsFpuRegister<FRegister>());
5948 } else {
5949 __ SelD(fcond_reg,
5950 false_src.AsFpuRegister<FRegister>(),
5951 true_src.AsFpuRegister<FRegister>());
5952 }
5953 __ MovD(dst_reg, fcond_reg);
5954 }
5955 break;
5956 }
5957 }
5958}
5959
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005960void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5961 LocationSummary* locations = new (GetGraph()->GetArena())
5962 LocationSummary(flag, LocationSummary::kNoCall);
5963 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005964}
5965
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005966void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5967 __ LoadFromOffset(kLoadWord,
5968 flag->GetLocations()->Out().AsRegister<Register>(),
5969 SP,
5970 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005971}
5972
David Brazdil74eb1b22015-12-14 11:44:01 +00005973void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005975 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005976}
5977
5978void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005979 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5980 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5981 if (is_r6) {
5982 GenConditionalMoveR6(select);
5983 } else {
5984 GenConditionalMoveR2(select);
5985 }
5986 } else {
5987 LocationSummary* locations = select->GetLocations();
5988 MipsLabel false_target;
5989 GenerateTestAndBranch(select,
5990 /* condition_input_index */ 2,
5991 /* true_target */ nullptr,
5992 &false_target);
5993 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5994 __ Bind(&false_target);
5995 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005996}
5997
David Srbecky0cf44932015-12-09 14:09:59 +00005998void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5999 new (GetGraph()->GetArena()) LocationSummary(info);
6000}
6001
David Srbeckyd28f4a02016-03-14 17:14:24 +00006002void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6003 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006004}
6005
6006void CodeGeneratorMIPS::GenerateNop() {
6007 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006008}
6009
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006010void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
6011 Primitive::Type field_type = field_info.GetFieldType();
6012 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6013 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006014 bool object_field_get_with_read_barrier =
6015 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006017 instruction,
6018 generate_volatile
6019 ? LocationSummary::kCallOnMainOnly
6020 : (object_field_get_with_read_barrier
6021 ? LocationSummary::kCallOnSlowPath
6022 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006023
Alexey Frunzec61c0762017-04-10 13:54:23 -07006024 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6025 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6026 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006027 locations->SetInAt(0, Location::RequiresRegister());
6028 if (generate_volatile) {
6029 InvokeRuntimeCallingConvention calling_convention;
6030 // need A0 to hold base + offset
6031 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6032 if (field_type == Primitive::kPrimLong) {
6033 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
6034 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006035 // Use Location::Any() to prevent situations when running out of available fp registers.
6036 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006037 // Need some temp core regs since FP results are returned in core registers
6038 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
6039 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6040 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6041 }
6042 } else {
6043 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6044 locations->SetOut(Location::RequiresFpuRegister());
6045 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006046 // The output overlaps in the case of an object field get with
6047 // read barriers enabled: we do not want the move to overwrite the
6048 // object's location, as we need it to emit the read barrier.
6049 locations->SetOut(Location::RequiresRegister(),
6050 object_field_get_with_read_barrier
6051 ? Location::kOutputOverlap
6052 : Location::kNoOutputOverlap);
6053 }
6054 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6055 // We need a temporary register for the read barrier marking slow
6056 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
6057 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006058 }
6059 }
6060}
6061
6062void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6063 const FieldInfo& field_info,
6064 uint32_t dex_pc) {
6065 Primitive::Type type = field_info.GetFieldType();
6066 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006067 Location obj_loc = locations->InAt(0);
6068 Register obj = obj_loc.AsRegister<Register>();
6069 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006070 LoadOperandType load_type = kLoadUnsignedByte;
6071 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006072 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006073 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006074
6075 switch (type) {
6076 case Primitive::kPrimBoolean:
6077 load_type = kLoadUnsignedByte;
6078 break;
6079 case Primitive::kPrimByte:
6080 load_type = kLoadSignedByte;
6081 break;
6082 case Primitive::kPrimShort:
6083 load_type = kLoadSignedHalfword;
6084 break;
6085 case Primitive::kPrimChar:
6086 load_type = kLoadUnsignedHalfword;
6087 break;
6088 case Primitive::kPrimInt:
6089 case Primitive::kPrimFloat:
6090 case Primitive::kPrimNot:
6091 load_type = kLoadWord;
6092 break;
6093 case Primitive::kPrimLong:
6094 case Primitive::kPrimDouble:
6095 load_type = kLoadDoubleword;
6096 break;
6097 case Primitive::kPrimVoid:
6098 LOG(FATAL) << "Unreachable type " << type;
6099 UNREACHABLE();
6100 }
6101
6102 if (is_volatile && load_type == kLoadDoubleword) {
6103 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006104 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006105 // Do implicit Null check
6106 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6107 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006108 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006109 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6110 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006111 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006112 if (dst_loc.IsFpuRegister()) {
6113 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006114 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006115 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006116 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006117 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006118 __ StoreToOffset(kStoreWord,
6119 locations->GetTemp(1).AsRegister<Register>(),
6120 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006121 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006122 __ StoreToOffset(kStoreWord,
6123 locations->GetTemp(2).AsRegister<Register>(),
6124 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006125 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006126 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006127 }
6128 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006129 if (type == Primitive::kPrimNot) {
6130 // /* HeapReference<Object> */ dst = *(obj + offset)
6131 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6132 Location temp_loc = locations->GetTemp(0);
6133 // Note that a potential implicit null check is handled in this
6134 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6135 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6136 dst_loc,
6137 obj,
6138 offset,
6139 temp_loc,
6140 /* needs_null_check */ true);
6141 if (is_volatile) {
6142 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6143 }
6144 } else {
6145 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6146 if (is_volatile) {
6147 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6148 }
6149 // If read barriers are enabled, emit read barriers other than
6150 // Baker's using a slow path (and also unpoison the loaded
6151 // reference, if heap poisoning is enabled).
6152 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6153 }
6154 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006155 Register dst;
6156 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006157 DCHECK(dst_loc.IsRegisterPair());
6158 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006159 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006160 DCHECK(dst_loc.IsRegister());
6161 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006162 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006163 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006164 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006165 DCHECK(dst_loc.IsFpuRegister());
6166 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006167 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006168 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006169 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006170 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006171 }
6172 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006173 }
6174
Alexey Frunze15958152017-02-09 19:08:30 -08006175 // Memory barriers, in the case of references, are handled in the
6176 // previous switch statement.
6177 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006178 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6179 }
6180}
6181
6182void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6183 Primitive::Type field_type = field_info.GetFieldType();
6184 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6185 bool generate_volatile = field_info.IsVolatile() && is_wide;
6186 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006187 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006188
6189 locations->SetInAt(0, Location::RequiresRegister());
6190 if (generate_volatile) {
6191 InvokeRuntimeCallingConvention calling_convention;
6192 // need A0 to hold base + offset
6193 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6194 if (field_type == Primitive::kPrimLong) {
6195 locations->SetInAt(1, Location::RegisterPairLocation(
6196 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6197 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006198 // Use Location::Any() to prevent situations when running out of available fp registers.
6199 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006200 // Pass FP parameters in core registers.
6201 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6202 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6203 }
6204 } else {
6205 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006206 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006207 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006208 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006209 }
6210 }
6211}
6212
6213void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6214 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006215 uint32_t dex_pc,
6216 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006217 Primitive::Type type = field_info.GetFieldType();
6218 LocationSummary* locations = instruction->GetLocations();
6219 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006220 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006221 StoreOperandType store_type = kStoreByte;
6222 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006223 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006224 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006225 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006226
6227 switch (type) {
6228 case Primitive::kPrimBoolean:
6229 case Primitive::kPrimByte:
6230 store_type = kStoreByte;
6231 break;
6232 case Primitive::kPrimShort:
6233 case Primitive::kPrimChar:
6234 store_type = kStoreHalfword;
6235 break;
6236 case Primitive::kPrimInt:
6237 case Primitive::kPrimFloat:
6238 case Primitive::kPrimNot:
6239 store_type = kStoreWord;
6240 break;
6241 case Primitive::kPrimLong:
6242 case Primitive::kPrimDouble:
6243 store_type = kStoreDoubleword;
6244 break;
6245 case Primitive::kPrimVoid:
6246 LOG(FATAL) << "Unreachable type " << type;
6247 UNREACHABLE();
6248 }
6249
6250 if (is_volatile) {
6251 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6252 }
6253
6254 if (is_volatile && store_type == kStoreDoubleword) {
6255 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006256 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006257 // Do implicit Null check.
6258 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6259 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6260 if (type == Primitive::kPrimDouble) {
6261 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006262 if (value_location.IsFpuRegister()) {
6263 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6264 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006265 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006266 value_location.AsFpuRegister<FRegister>());
6267 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006268 __ LoadFromOffset(kLoadWord,
6269 locations->GetTemp(1).AsRegister<Register>(),
6270 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006271 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006272 __ LoadFromOffset(kLoadWord,
6273 locations->GetTemp(2).AsRegister<Register>(),
6274 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006275 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006276 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006277 DCHECK(value_location.IsConstant());
6278 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6279 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006280 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6281 locations->GetTemp(1).AsRegister<Register>(),
6282 value);
6283 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006284 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006285 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006286 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6287 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006288 if (value_location.IsConstant()) {
6289 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6290 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6291 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006292 Register src;
6293 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006294 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006296 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006297 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006298 if (kPoisonHeapReferences && needs_write_barrier) {
6299 // Note that in the case where `value` is a null reference,
6300 // we do not enter this block, as a null reference does not
6301 // need poisoning.
6302 DCHECK_EQ(type, Primitive::kPrimNot);
6303 __ PoisonHeapReference(TMP, src);
6304 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6305 } else {
6306 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6307 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006308 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006309 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006310 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006311 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006312 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006313 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006314 }
6315 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006316 }
6317
Alexey Frunzec061de12017-02-14 13:27:23 -08006318 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006319 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006320 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006321 }
6322
6323 if (is_volatile) {
6324 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6325 }
6326}
6327
6328void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6329 HandleFieldGet(instruction, instruction->GetFieldInfo());
6330}
6331
6332void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6333 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6334}
6335
6336void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6337 HandleFieldSet(instruction, instruction->GetFieldInfo());
6338}
6339
6340void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006341 HandleFieldSet(instruction,
6342 instruction->GetFieldInfo(),
6343 instruction->GetDexPc(),
6344 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006345}
6346
Alexey Frunze15958152017-02-09 19:08:30 -08006347void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6348 HInstruction* instruction,
6349 Location out,
6350 uint32_t offset,
6351 Location maybe_temp,
6352 ReadBarrierOption read_barrier_option) {
6353 Register out_reg = out.AsRegister<Register>();
6354 if (read_barrier_option == kWithReadBarrier) {
6355 CHECK(kEmitCompilerReadBarrier);
6356 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6357 if (kUseBakerReadBarrier) {
6358 // Load with fast path based Baker's read barrier.
6359 // /* HeapReference<Object> */ out = *(out + offset)
6360 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6361 out,
6362 out_reg,
6363 offset,
6364 maybe_temp,
6365 /* needs_null_check */ false);
6366 } else {
6367 // Load with slow path based read barrier.
6368 // Save the value of `out` into `maybe_temp` before overwriting it
6369 // in the following move operation, as we will need it for the
6370 // read barrier below.
6371 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6372 // /* HeapReference<Object> */ out = *(out + offset)
6373 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6374 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6375 }
6376 } else {
6377 // Plain load with no read barrier.
6378 // /* HeapReference<Object> */ out = *(out + offset)
6379 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6380 __ MaybeUnpoisonHeapReference(out_reg);
6381 }
6382}
6383
6384void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6385 HInstruction* instruction,
6386 Location out,
6387 Location obj,
6388 uint32_t offset,
6389 Location maybe_temp,
6390 ReadBarrierOption read_barrier_option) {
6391 Register out_reg = out.AsRegister<Register>();
6392 Register obj_reg = obj.AsRegister<Register>();
6393 if (read_barrier_option == kWithReadBarrier) {
6394 CHECK(kEmitCompilerReadBarrier);
6395 if (kUseBakerReadBarrier) {
6396 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6397 // Load with fast path based Baker's read barrier.
6398 // /* HeapReference<Object> */ out = *(obj + offset)
6399 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6400 out,
6401 obj_reg,
6402 offset,
6403 maybe_temp,
6404 /* needs_null_check */ false);
6405 } else {
6406 // Load with slow path based read barrier.
6407 // /* HeapReference<Object> */ out = *(obj + offset)
6408 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6409 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6410 }
6411 } else {
6412 // Plain load with no read barrier.
6413 // /* HeapReference<Object> */ out = *(obj + offset)
6414 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6415 __ MaybeUnpoisonHeapReference(out_reg);
6416 }
6417}
6418
6419void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6420 Location root,
6421 Register obj,
6422 uint32_t offset,
6423 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006424 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006425 if (read_barrier_option == kWithReadBarrier) {
6426 DCHECK(kEmitCompilerReadBarrier);
6427 if (kUseBakerReadBarrier) {
6428 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6429 // Baker's read barrier are used:
6430 //
6431 // root = obj.field;
6432 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6433 // if (temp != null) {
6434 // root = temp(root)
6435 // }
6436
6437 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6438 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6439 static_assert(
6440 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6441 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6442 "have different sizes.");
6443 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6444 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6445 "have different sizes.");
6446
6447 // Slow path marking the GC root `root`.
6448 Location temp = Location::RegisterLocation(T9);
6449 SlowPathCodeMIPS* slow_path =
6450 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6451 instruction,
6452 root,
6453 /*entrypoint*/ temp);
6454 codegen_->AddSlowPath(slow_path);
6455
6456 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6457 const int32_t entry_point_offset =
6458 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6459 // Loading the entrypoint does not require a load acquire since it is only changed when
6460 // threads are suspended or running a checkpoint.
6461 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6462 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6463 // checking GetIsGcMarking.
6464 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6465 __ Bind(slow_path->GetExitLabel());
6466 } else {
6467 // GC root loaded through a slow path for read barriers other
6468 // than Baker's.
6469 // /* GcRoot<mirror::Object>* */ root = obj + offset
6470 __ Addiu32(root_reg, obj, offset);
6471 // /* mirror::Object* */ root = root->Read()
6472 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6473 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006474 } else {
6475 // Plain GC root load with no read barrier.
6476 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6477 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6478 // Note that GC roots are not affected by heap poisoning, thus we
6479 // do not have to unpoison `root_reg` here.
6480 }
6481}
6482
Alexey Frunze15958152017-02-09 19:08:30 -08006483void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6484 Location ref,
6485 Register obj,
6486 uint32_t offset,
6487 Location temp,
6488 bool needs_null_check) {
6489 DCHECK(kEmitCompilerReadBarrier);
6490 DCHECK(kUseBakerReadBarrier);
6491
6492 // /* HeapReference<Object> */ ref = *(obj + offset)
6493 Location no_index = Location::NoLocation();
6494 ScaleFactor no_scale_factor = TIMES_1;
6495 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6496 ref,
6497 obj,
6498 offset,
6499 no_index,
6500 no_scale_factor,
6501 temp,
6502 needs_null_check);
6503}
6504
6505void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6506 Location ref,
6507 Register obj,
6508 uint32_t data_offset,
6509 Location index,
6510 Location temp,
6511 bool needs_null_check) {
6512 DCHECK(kEmitCompilerReadBarrier);
6513 DCHECK(kUseBakerReadBarrier);
6514
6515 static_assert(
6516 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6517 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6518 // /* HeapReference<Object> */ ref =
6519 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6520 ScaleFactor scale_factor = TIMES_4;
6521 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6522 ref,
6523 obj,
6524 data_offset,
6525 index,
6526 scale_factor,
6527 temp,
6528 needs_null_check);
6529}
6530
6531void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6532 Location ref,
6533 Register obj,
6534 uint32_t offset,
6535 Location index,
6536 ScaleFactor scale_factor,
6537 Location temp,
6538 bool needs_null_check,
6539 bool always_update_field) {
6540 DCHECK(kEmitCompilerReadBarrier);
6541 DCHECK(kUseBakerReadBarrier);
6542
6543 // In slow path based read barriers, the read barrier call is
6544 // inserted after the original load. However, in fast path based
6545 // Baker's read barriers, we need to perform the load of
6546 // mirror::Object::monitor_ *before* the original reference load.
6547 // This load-load ordering is required by the read barrier.
6548 // The fast path/slow path (for Baker's algorithm) should look like:
6549 //
6550 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6551 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6552 // HeapReference<Object> ref = *src; // Original reference load.
6553 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6554 // if (is_gray) {
6555 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6556 // }
6557 //
6558 // Note: the original implementation in ReadBarrier::Barrier is
6559 // slightly more complex as it performs additional checks that we do
6560 // not do here for performance reasons.
6561
6562 Register ref_reg = ref.AsRegister<Register>();
6563 Register temp_reg = temp.AsRegister<Register>();
6564 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6565
6566 // /* int32_t */ monitor = obj->monitor_
6567 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6568 if (needs_null_check) {
6569 MaybeRecordImplicitNullCheck(instruction);
6570 }
6571 // /* LockWord */ lock_word = LockWord(monitor)
6572 static_assert(sizeof(LockWord) == sizeof(int32_t),
6573 "art::LockWord and int32_t have different sizes.");
6574
6575 __ Sync(0); // Barrier to prevent load-load reordering.
6576
6577 // The actual reference load.
6578 if (index.IsValid()) {
6579 // Load types involving an "index": ArrayGet,
6580 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6581 // intrinsics.
6582 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6583 if (index.IsConstant()) {
6584 size_t computed_offset =
6585 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6586 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6587 } else {
6588 // Handle the special case of the
6589 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6590 // intrinsics, which use a register pair as index ("long
6591 // offset"), of which only the low part contains data.
6592 Register index_reg = index.IsRegisterPair()
6593 ? index.AsRegisterPairLow<Register>()
6594 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006595 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006596 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6597 }
6598 } else {
6599 // /* HeapReference<Object> */ ref = *(obj + offset)
6600 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6601 }
6602
6603 // Object* ref = ref_addr->AsMirrorPtr()
6604 __ MaybeUnpoisonHeapReference(ref_reg);
6605
6606 // Slow path marking the object `ref` when it is gray.
6607 SlowPathCodeMIPS* slow_path;
6608 if (always_update_field) {
6609 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6610 // of the form `obj + field_offset`, where `obj` is a register and
6611 // `field_offset` is a register pair (of which only the lower half
6612 // is used). Thus `offset` and `scale_factor` above are expected
6613 // to be null in this code path.
6614 DCHECK_EQ(offset, 0u);
6615 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6616 slow_path = new (GetGraph()->GetArena())
6617 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6618 ref,
6619 obj,
6620 /* field_offset */ index,
6621 temp_reg);
6622 } else {
6623 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6624 }
6625 AddSlowPath(slow_path);
6626
6627 // if (rb_state == ReadBarrier::GrayState())
6628 // ref = ReadBarrier::Mark(ref);
6629 // Given the numeric representation, it's enough to check the low bit of the
6630 // rb_state. We do that by shifting the bit into the sign bit (31) and
6631 // performing a branch on less than zero.
6632 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6633 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6634 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6635 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6636 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6637 __ Bind(slow_path->GetExitLabel());
6638}
6639
6640void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6641 Location out,
6642 Location ref,
6643 Location obj,
6644 uint32_t offset,
6645 Location index) {
6646 DCHECK(kEmitCompilerReadBarrier);
6647
6648 // Insert a slow path based read barrier *after* the reference load.
6649 //
6650 // If heap poisoning is enabled, the unpoisoning of the loaded
6651 // reference will be carried out by the runtime within the slow
6652 // path.
6653 //
6654 // Note that `ref` currently does not get unpoisoned (when heap
6655 // poisoning is enabled), which is alright as the `ref` argument is
6656 // not used by the artReadBarrierSlow entry point.
6657 //
6658 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6659 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6660 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6661 AddSlowPath(slow_path);
6662
6663 __ B(slow_path->GetEntryLabel());
6664 __ Bind(slow_path->GetExitLabel());
6665}
6666
6667void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6668 Location out,
6669 Location ref,
6670 Location obj,
6671 uint32_t offset,
6672 Location index) {
6673 if (kEmitCompilerReadBarrier) {
6674 // Baker's read barriers shall be handled by the fast path
6675 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6676 DCHECK(!kUseBakerReadBarrier);
6677 // If heap poisoning is enabled, unpoisoning will be taken care of
6678 // by the runtime within the slow path.
6679 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6680 } else if (kPoisonHeapReferences) {
6681 __ UnpoisonHeapReference(out.AsRegister<Register>());
6682 }
6683}
6684
6685void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6686 Location out,
6687 Location root) {
6688 DCHECK(kEmitCompilerReadBarrier);
6689
6690 // Insert a slow path based read barrier *after* the GC root load.
6691 //
6692 // Note that GC roots are not affected by heap poisoning, so we do
6693 // not need to do anything special for this here.
6694 SlowPathCodeMIPS* slow_path =
6695 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6696 AddSlowPath(slow_path);
6697
6698 __ B(slow_path->GetEntryLabel());
6699 __ Bind(slow_path->GetExitLabel());
6700}
6701
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006702void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006703 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6704 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07006705 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006706 switch (type_check_kind) {
6707 case TypeCheckKind::kExactCheck:
6708 case TypeCheckKind::kAbstractClassCheck:
6709 case TypeCheckKind::kClassHierarchyCheck:
6710 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006711 call_kind =
6712 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006713 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006714 break;
6715 case TypeCheckKind::kArrayCheck:
6716 case TypeCheckKind::kUnresolvedCheck:
6717 case TypeCheckKind::kInterfaceCheck:
6718 call_kind = LocationSummary::kCallOnSlowPath;
6719 break;
6720 }
6721
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006723 if (baker_read_barrier_slow_path) {
6724 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6725 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006726 locations->SetInAt(0, Location::RequiresRegister());
6727 locations->SetInAt(1, Location::RequiresRegister());
6728 // The output does overlap inputs.
6729 // Note that TypeCheckSlowPathMIPS uses this register too.
6730 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006731 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732}
6733
6734void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006735 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006736 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006737 Location obj_loc = locations->InAt(0);
6738 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006740 Location out_loc = locations->Out();
6741 Register out = out_loc.AsRegister<Register>();
6742 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6743 DCHECK_LE(num_temps, 1u);
6744 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006745 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6746 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6747 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6748 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006749 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006750 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006751
6752 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006753 // Avoid this check if we know `obj` is not null.
6754 if (instruction->MustDoNullCheck()) {
6755 __ Move(out, ZERO);
6756 __ Beqz(obj, &done);
6757 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006758
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006759 switch (type_check_kind) {
6760 case TypeCheckKind::kExactCheck: {
6761 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006762 GenerateReferenceLoadTwoRegisters(instruction,
6763 out_loc,
6764 obj_loc,
6765 class_offset,
6766 maybe_temp_loc,
6767 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006768 // Classes must be equal for the instanceof to succeed.
6769 __ Xor(out, out, cls);
6770 __ Sltiu(out, out, 1);
6771 break;
6772 }
6773
6774 case TypeCheckKind::kAbstractClassCheck: {
6775 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006776 GenerateReferenceLoadTwoRegisters(instruction,
6777 out_loc,
6778 obj_loc,
6779 class_offset,
6780 maybe_temp_loc,
6781 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006782 // If the class is abstract, we eagerly fetch the super class of the
6783 // object to avoid doing a comparison we know will fail.
6784 MipsLabel loop;
6785 __ Bind(&loop);
6786 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006787 GenerateReferenceLoadOneRegister(instruction,
6788 out_loc,
6789 super_offset,
6790 maybe_temp_loc,
6791 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006792 // If `out` is null, we use it for the result, and jump to `done`.
6793 __ Beqz(out, &done);
6794 __ Bne(out, cls, &loop);
6795 __ LoadConst32(out, 1);
6796 break;
6797 }
6798
6799 case TypeCheckKind::kClassHierarchyCheck: {
6800 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006801 GenerateReferenceLoadTwoRegisters(instruction,
6802 out_loc,
6803 obj_loc,
6804 class_offset,
6805 maybe_temp_loc,
6806 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006807 // Walk over the class hierarchy to find a match.
6808 MipsLabel loop, success;
6809 __ Bind(&loop);
6810 __ Beq(out, cls, &success);
6811 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006812 GenerateReferenceLoadOneRegister(instruction,
6813 out_loc,
6814 super_offset,
6815 maybe_temp_loc,
6816 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006817 __ Bnez(out, &loop);
6818 // If `out` is null, we use it for the result, and jump to `done`.
6819 __ B(&done);
6820 __ Bind(&success);
6821 __ LoadConst32(out, 1);
6822 break;
6823 }
6824
6825 case TypeCheckKind::kArrayObjectCheck: {
6826 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006827 GenerateReferenceLoadTwoRegisters(instruction,
6828 out_loc,
6829 obj_loc,
6830 class_offset,
6831 maybe_temp_loc,
6832 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006833 // Do an exact check.
6834 MipsLabel success;
6835 __ Beq(out, cls, &success);
6836 // Otherwise, we need to check that the object's class is a non-primitive array.
6837 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006838 GenerateReferenceLoadOneRegister(instruction,
6839 out_loc,
6840 component_offset,
6841 maybe_temp_loc,
6842 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006843 // If `out` is null, we use it for the result, and jump to `done`.
6844 __ Beqz(out, &done);
6845 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6846 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6847 __ Sltiu(out, out, 1);
6848 __ B(&done);
6849 __ Bind(&success);
6850 __ LoadConst32(out, 1);
6851 break;
6852 }
6853
6854 case TypeCheckKind::kArrayCheck: {
6855 // No read barrier since the slow path will retry upon failure.
6856 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006857 GenerateReferenceLoadTwoRegisters(instruction,
6858 out_loc,
6859 obj_loc,
6860 class_offset,
6861 maybe_temp_loc,
6862 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006863 DCHECK(locations->OnlyCallsOnSlowPath());
6864 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6865 /* is_fatal */ false);
6866 codegen_->AddSlowPath(slow_path);
6867 __ Bne(out, cls, slow_path->GetEntryLabel());
6868 __ LoadConst32(out, 1);
6869 break;
6870 }
6871
6872 case TypeCheckKind::kUnresolvedCheck:
6873 case TypeCheckKind::kInterfaceCheck: {
6874 // Note that we indeed only call on slow path, but we always go
6875 // into the slow path for the unresolved and interface check
6876 // cases.
6877 //
6878 // We cannot directly call the InstanceofNonTrivial runtime
6879 // entry point without resorting to a type checking slow path
6880 // here (i.e. by calling InvokeRuntime directly), as it would
6881 // require to assign fixed registers for the inputs of this
6882 // HInstanceOf instruction (following the runtime calling
6883 // convention), which might be cluttered by the potential first
6884 // read barrier emission at the beginning of this method.
6885 //
6886 // TODO: Introduce a new runtime entry point taking the object
6887 // to test (instead of its class) as argument, and let it deal
6888 // with the read barrier issues. This will let us refactor this
6889 // case of the `switch` code as it was previously (with a direct
6890 // call to the runtime not using a type checking slow path).
6891 // This should also be beneficial for the other cases above.
6892 DCHECK(locations->OnlyCallsOnSlowPath());
6893 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6894 /* is_fatal */ false);
6895 codegen_->AddSlowPath(slow_path);
6896 __ B(slow_path->GetEntryLabel());
6897 break;
6898 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006899 }
6900
6901 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006902
6903 if (slow_path != nullptr) {
6904 __ Bind(slow_path->GetExitLabel());
6905 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006906}
6907
6908void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6910 locations->SetOut(Location::ConstantLocation(constant));
6911}
6912
6913void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6914 // Will be generated at use site.
6915}
6916
6917void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6919 locations->SetOut(Location::ConstantLocation(constant));
6920}
6921
6922void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6923 // Will be generated at use site.
6924}
6925
6926void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6927 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6928 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6929}
6930
6931void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6932 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006933 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006934 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006935 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006936}
6937
6938void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6939 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6940 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006941 Location receiver = invoke->GetLocations()->InAt(0);
6942 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006943 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006944
6945 // Set the hidden argument.
6946 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6947 invoke->GetDexMethodIndex());
6948
6949 // temp = object->GetClass();
6950 if (receiver.IsStackSlot()) {
6951 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6952 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6953 } else {
6954 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6955 }
6956 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006957 // Instead of simply (possibly) unpoisoning `temp` here, we should
6958 // emit a read barrier for the previous class reference load.
6959 // However this is not required in practice, as this is an
6960 // intermediate/temporary reference and because the current
6961 // concurrent copying collector keeps the from-space memory
6962 // intact/accessible until the end of the marking phase (the
6963 // concurrent copying collector may not in the future).
6964 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006965 __ LoadFromOffset(kLoadWord, temp, temp,
6966 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6967 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006968 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006969 // temp = temp->GetImtEntryAt(method_offset);
6970 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6971 // T9 = temp->GetEntryPoint();
6972 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6973 // T9();
6974 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006975 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006976 DCHECK(!codegen_->IsLeafMethod());
6977 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6978}
6979
6980void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006981 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6982 if (intrinsic.TryDispatch(invoke)) {
6983 return;
6984 }
6985
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006986 HandleInvoke(invoke);
6987}
6988
6989void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006990 // Explicit clinit checks triggered by static invokes must have been pruned by
6991 // art::PrepareForRegisterAllocation.
6992 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006993
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006994 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6995 bool has_extra_input = invoke->HasPcRelativeDexCache() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006996
Chris Larsen701566a2015-10-27 15:29:13 -07006997 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6998 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006999 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7000 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7001 }
Chris Larsen701566a2015-10-27 15:29:13 -07007002 return;
7003 }
7004
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007005 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007006
7007 // Add the extra input register if either the dex cache array base register
7008 // or the PC-relative base register for accessing literals is needed.
7009 if (has_extra_input) {
7010 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7011 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007012}
7013
Orion Hodsonac141392017-01-13 11:53:47 +00007014void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7015 HandleInvoke(invoke);
7016}
7017
7018void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7019 codegen_->GenerateInvokePolymorphicCall(invoke);
7020}
7021
Chris Larsen701566a2015-10-27 15:29:13 -07007022static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007023 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007024 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7025 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007026 return true;
7027 }
7028 return false;
7029}
7030
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007031HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007032 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007033 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007034 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007035 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
7036 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007037 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007038 bool is_r6 = GetInstructionSetFeatures().IsR6();
7039 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007040 switch (desired_string_load_kind) {
7041 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7042 DCHECK(!GetCompilerOptions().GetCompilePic());
7043 break;
7044 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
7045 DCHECK(GetCompilerOptions().GetCompilePic());
7046 break;
7047 case HLoadString::LoadKind::kBootImageAddress:
7048 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007049 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007050 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007051 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007052 case HLoadString::LoadKind::kJitTableAddress:
7053 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007054 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007055 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007056 case HLoadString::LoadKind::kDexCacheViaMethod:
7057 fallback_load = false;
7058 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007059 }
7060 if (fallback_load) {
7061 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
7062 }
7063 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007064}
7065
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007066HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7067 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007068 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007069 // is incompatible with it.
7070 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007071 bool is_r6 = GetInstructionSetFeatures().IsR6();
7072 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007073 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007074 case HLoadClass::LoadKind::kInvalid:
7075 LOG(FATAL) << "UNREACHABLE";
7076 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007077 case HLoadClass::LoadKind::kReferrersClass:
7078 fallback_load = false;
7079 break;
7080 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
7081 DCHECK(!GetCompilerOptions().GetCompilePic());
7082 break;
7083 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
7084 DCHECK(GetCompilerOptions().GetCompilePic());
7085 break;
7086 case HLoadClass::LoadKind::kBootImageAddress:
7087 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007088 case HLoadClass::LoadKind::kBssEntry:
7089 DCHECK(!Runtime::Current()->UseJitCompilation());
7090 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007091 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007092 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007093 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007094 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007095 case HLoadClass::LoadKind::kDexCacheViaMethod:
7096 fallback_load = false;
7097 break;
7098 }
7099 if (fallback_load) {
7100 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
7101 }
7102 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007103}
7104
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007105Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7106 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007107 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007108 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7109 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7110 if (!invoke->GetLocations()->Intrinsified()) {
7111 return location.AsRegister<Register>();
7112 }
7113 // For intrinsics we allow any location, so it may be on the stack.
7114 if (!location.IsRegister()) {
7115 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7116 return temp;
7117 }
7118 // For register locations, check if the register was saved. If so, get it from the stack.
7119 // Note: There is a chance that the register was saved but not overwritten, so we could
7120 // save one load. However, since this is just an intrinsic slow path we prefer this
7121 // simple and more robust approach rather that trying to determine if that's the case.
7122 SlowPathCode* slow_path = GetCurrentSlowPath();
7123 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7124 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7125 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7126 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7127 return temp;
7128 }
7129 return location.AsRegister<Register>();
7130}
7131
Vladimir Markodc151b22015-10-15 18:02:30 +01007132HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7133 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007134 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007135 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007136 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007137 // is incompatible with it.
7138 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007139 bool is_r6 = GetInstructionSetFeatures().IsR6();
7140 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007141 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007142 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007143 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01007144 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007145 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01007146 break;
7147 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007148 if (fallback_load) {
7149 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7150 dispatch_info.method_load_data = 0;
7151 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007152 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007153}
7154
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007155void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7156 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007157 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007158 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7159 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007160 bool is_r6 = GetInstructionSetFeatures().IsR6();
7161 Register base_reg = (invoke->HasPcRelativeDexCache() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007162 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7163 : ZERO;
7164
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007165 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007166 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007167 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007168 uint32_t offset =
7169 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007170 __ LoadFromOffset(kLoadWord,
7171 temp.AsRegister<Register>(),
7172 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007173 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007174 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007175 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007176 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007177 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007178 break;
7179 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7180 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7181 break;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007182 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
7183 if (is_r6) {
7184 uint32_t offset = invoke->GetDexCacheArrayOffset();
7185 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7186 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
7187 bool reordering = __ SetReorder(false);
7188 EmitPcRelativeAddressPlaceholderHigh(info, TMP, ZERO);
7189 __ Lw(temp.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
7190 __ SetReorder(reordering);
7191 } else {
7192 HMipsDexCacheArraysBase* base =
7193 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
7194 int32_t offset =
7195 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
7196 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7197 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007198 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007199 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007200 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007201 Register reg = temp.AsRegister<Register>();
7202 Register method_reg;
7203 if (current_method.IsRegister()) {
7204 method_reg = current_method.AsRegister<Register>();
7205 } else {
7206 // TODO: use the appropriate DCHECK() here if possible.
7207 // DCHECK(invoke->GetLocations()->Intrinsified());
7208 DCHECK(!current_method.IsValid());
7209 method_reg = reg;
7210 __ Lw(reg, SP, kCurrentMethodStackOffset);
7211 }
7212
7213 // temp = temp->dex_cache_resolved_methods_;
7214 __ LoadFromOffset(kLoadWord,
7215 reg,
7216 method_reg,
7217 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007218 // temp = temp[index_in_cache];
7219 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7220 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007221 __ LoadFromOffset(kLoadWord,
7222 reg,
7223 reg,
7224 CodeGenerator::GetCachePointerOffset(index_in_cache));
7225 break;
7226 }
7227 }
7228
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007229 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007230 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007231 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007232 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007233 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7234 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007235 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007236 T9,
7237 callee_method.AsRegister<Register>(),
7238 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007239 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007240 // T9()
7241 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007242 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007243 break;
7244 }
7245 DCHECK(!IsLeafMethod());
7246}
7247
7248void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007249 // Explicit clinit checks triggered by static invokes must have been pruned by
7250 // art::PrepareForRegisterAllocation.
7251 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007252
7253 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7254 return;
7255 }
7256
7257 LocationSummary* locations = invoke->GetLocations();
7258 codegen_->GenerateStaticOrDirectCall(invoke,
7259 locations->HasTemps()
7260 ? locations->GetTemp(0)
7261 : Location::NoLocation());
7262 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7263}
7264
Chris Larsen3acee732015-11-18 13:31:08 -08007265void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007266 // Use the calling convention instead of the location of the receiver, as
7267 // intrinsics may have put the receiver in a different register. In the intrinsics
7268 // slow path, the arguments have been moved to the right place, so here we are
7269 // guaranteed that the receiver is the first register of the calling convention.
7270 InvokeDexCallingConvention calling_convention;
7271 Register receiver = calling_convention.GetRegisterAt(0);
7272
Chris Larsen3acee732015-11-18 13:31:08 -08007273 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007274 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7275 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7276 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007277 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007278
7279 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007280 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007281 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007282 // Instead of simply (possibly) unpoisoning `temp` here, we should
7283 // emit a read barrier for the previous class reference load.
7284 // However this is not required in practice, as this is an
7285 // intermediate/temporary reference and because the current
7286 // concurrent copying collector keeps the from-space memory
7287 // intact/accessible until the end of the marking phase (the
7288 // concurrent copying collector may not in the future).
7289 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007290 // temp = temp->GetMethodAt(method_offset);
7291 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7292 // T9 = temp->GetEntryPoint();
7293 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7294 // T9();
7295 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007296 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08007297}
7298
7299void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7300 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7301 return;
7302 }
7303
7304 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007305 DCHECK(!codegen_->IsLeafMethod());
7306 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7307}
7308
7309void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007310 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7311 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007312 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007313 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7314 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007315 return;
7316 }
Vladimir Marko41559982017-01-06 14:04:23 +00007317 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007318 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007319 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7320 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007321 ? LocationSummary::kCallOnSlowPath
7322 : LocationSummary::kNoCall;
7323 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007324 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7325 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7326 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007327 switch (load_kind) {
7328 // We need an extra register for PC-relative literals on R2.
7329 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007330 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007331 case HLoadClass::LoadKind::kBootImageAddress:
7332 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007333 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007334 break;
7335 }
7336 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007337 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007338 locations->SetInAt(0, Location::RequiresRegister());
7339 break;
7340 default:
7341 break;
7342 }
7343 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007344 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7345 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7346 // Rely on the type resolution or initialization and marking to save everything we need.
7347 // Request a temp to hold the BSS entry location for the slow path on R2
7348 // (no benefit for R6).
7349 if (!isR6) {
7350 locations->AddTemp(Location::RequiresRegister());
7351 }
7352 RegisterSet caller_saves = RegisterSet::Empty();
7353 InvokeRuntimeCallingConvention calling_convention;
7354 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7355 locations->SetCustomSlowPathCallerSaves(caller_saves);
7356 } else {
7357 // For non-Baker read barriers we have a temp-clobbering call.
7358 }
7359 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007360}
7361
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007362// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7363// move.
7364void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007365 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7366 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7367 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007368 return;
7369 }
Vladimir Marko41559982017-01-06 14:04:23 +00007370 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007371
Vladimir Marko41559982017-01-06 14:04:23 +00007372 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007373 Location out_loc = locations->Out();
7374 Register out = out_loc.AsRegister<Register>();
7375 Register base_or_current_method_reg;
7376 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7377 switch (load_kind) {
7378 // We need an extra register for PC-relative literals on R2.
7379 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007380 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007381 case HLoadClass::LoadKind::kBootImageAddress:
7382 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007383 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7384 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007385 case HLoadClass::LoadKind::kReferrersClass:
7386 case HLoadClass::LoadKind::kDexCacheViaMethod:
7387 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7388 break;
7389 default:
7390 base_or_current_method_reg = ZERO;
7391 break;
7392 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007393
Alexey Frunze15958152017-02-09 19:08:30 -08007394 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7395 ? kWithoutReadBarrier
7396 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007397 bool generate_null_check = false;
7398 switch (load_kind) {
7399 case HLoadClass::LoadKind::kReferrersClass: {
7400 DCHECK(!cls->CanCallRuntime());
7401 DCHECK(!cls->MustGenerateClinitCheck());
7402 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7403 GenerateGcRootFieldLoad(cls,
7404 out_loc,
7405 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007406 ArtMethod::DeclaringClassOffset().Int32Value(),
7407 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007408 break;
7409 }
7410 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007411 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007412 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007413 __ LoadLiteral(out,
7414 base_or_current_method_reg,
7415 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
7416 cls->GetTypeIndex()));
7417 break;
7418 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007419 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007420 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007421 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7422 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007423 bool reordering = __ SetReorder(false);
7424 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7425 __ Addiu(out, out, /* placeholder */ 0x5678);
7426 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007427 break;
7428 }
7429 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007430 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007431 uint32_t address = dchecked_integral_cast<uint32_t>(
7432 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7433 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007434 __ LoadLiteral(out,
7435 base_or_current_method_reg,
7436 codegen_->DeduplicateBootImageAddressLiteral(address));
7437 break;
7438 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007439 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007440 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007441 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007442 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7443 if (isR6 || non_baker_read_barrier) {
7444 bool reordering = __ SetReorder(false);
7445 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7446 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
7447 __ SetReorder(reordering);
7448 } else {
7449 // On R2 save the BSS entry address in a temporary register instead of
7450 // recalculating it in the slow path.
7451 Register temp = locations->GetTemp(0).AsRegister<Register>();
7452 bool reordering = __ SetReorder(false);
7453 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7454 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7455 __ SetReorder(reordering);
7456 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
7457 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007458 generate_null_check = true;
7459 break;
7460 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007461 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007462 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7463 cls->GetTypeIndex(),
7464 cls->GetClass());
7465 bool reordering = __ SetReorder(false);
7466 __ Bind(&info->high_label);
7467 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007468 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007469 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007470 break;
7471 }
Vladimir Marko41559982017-01-06 14:04:23 +00007472 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007473 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007474 LOG(FATAL) << "UNREACHABLE";
7475 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007476 }
7477
7478 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7479 DCHECK(cls->CanCallRuntime());
7480 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7481 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7482 codegen_->AddSlowPath(slow_path);
7483 if (generate_null_check) {
7484 __ Beqz(out, slow_path->GetEntryLabel());
7485 }
7486 if (cls->MustGenerateClinitCheck()) {
7487 GenerateClassInitializationCheck(slow_path, out);
7488 } else {
7489 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007490 }
7491 }
7492}
7493
7494static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007495 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007496}
7497
7498void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7499 LocationSummary* locations =
7500 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7501 locations->SetOut(Location::RequiresRegister());
7502}
7503
7504void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7505 Register out = load->GetLocations()->Out().AsRegister<Register>();
7506 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7507}
7508
7509void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7510 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7511}
7512
7513void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7514 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7515}
7516
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007517void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007518 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007520 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007521 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007522 switch (load_kind) {
7523 // We need an extra register for PC-relative literals on R2.
7524 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7525 case HLoadString::LoadKind::kBootImageAddress:
7526 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007527 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007528 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007529 break;
7530 }
7531 FALLTHROUGH_INTENDED;
7532 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007533 case HLoadString::LoadKind::kDexCacheViaMethod:
7534 locations->SetInAt(0, Location::RequiresRegister());
7535 break;
7536 default:
7537 break;
7538 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007539 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
7540 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007541 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007542 } else {
7543 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007544 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7545 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7546 // Rely on the pResolveString and marking to save everything we need.
7547 // Request a temp to hold the BSS entry location for the slow path on R2
7548 // (no benefit for R6).
7549 if (!isR6) {
7550 locations->AddTemp(Location::RequiresRegister());
7551 }
7552 RegisterSet caller_saves = RegisterSet::Empty();
7553 InvokeRuntimeCallingConvention calling_convention;
7554 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7555 locations->SetCustomSlowPathCallerSaves(caller_saves);
7556 } else {
7557 // For non-Baker read barriers we have a temp-clobbering call.
7558 }
7559 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007560 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007561}
7562
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007563// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7564// move.
7565void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007566 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007567 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007568 Location out_loc = locations->Out();
7569 Register out = out_loc.AsRegister<Register>();
7570 Register base_or_current_method_reg;
7571 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7572 switch (load_kind) {
7573 // We need an extra register for PC-relative literals on R2.
7574 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
7575 case HLoadString::LoadKind::kBootImageAddress:
7576 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007577 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007578 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7579 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007580 default:
7581 base_or_current_method_reg = ZERO;
7582 break;
7583 }
7584
7585 switch (load_kind) {
7586 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007587 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007588 __ LoadLiteral(out,
7589 base_or_current_method_reg,
7590 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7591 load->GetStringIndex()));
7592 return; // No dex cache slow path.
7593 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007594 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007595 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007596 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007597 bool reordering = __ SetReorder(false);
7598 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7599 __ Addiu(out, out, /* placeholder */ 0x5678);
7600 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007601 return; // No dex cache slow path.
7602 }
7603 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007604 uint32_t address = dchecked_integral_cast<uint32_t>(
7605 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7606 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007607 __ LoadLiteral(out,
7608 base_or_current_method_reg,
7609 codegen_->DeduplicateBootImageAddressLiteral(address));
7610 return; // No dex cache slow path.
7611 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007612 case HLoadString::LoadKind::kBssEntry: {
7613 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7614 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007615 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007616 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7617 if (isR6 || non_baker_read_barrier) {
7618 bool reordering = __ SetReorder(false);
7619 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7620 GenerateGcRootFieldLoad(load,
7621 out_loc,
7622 out,
7623 /* placeholder */ 0x5678,
7624 kCompilerReadBarrierOption);
7625 __ SetReorder(reordering);
7626 } else {
7627 // On R2 save the BSS entry address in a temporary register instead of
7628 // recalculating it in the slow path.
7629 Register temp = locations->GetTemp(0).AsRegister<Register>();
7630 bool reordering = __ SetReorder(false);
7631 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7632 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7633 __ SetReorder(reordering);
7634 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7635 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007636 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7637 codegen_->AddSlowPath(slow_path);
7638 __ Beqz(out, slow_path->GetEntryLabel());
7639 __ Bind(slow_path->GetExitLabel());
7640 return;
7641 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007642 case HLoadString::LoadKind::kJitTableAddress: {
7643 CodeGeneratorMIPS::JitPatchInfo* info =
7644 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7645 load->GetStringIndex(),
7646 load->GetString());
7647 bool reordering = __ SetReorder(false);
7648 __ Bind(&info->high_label);
7649 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007650 GenerateGcRootFieldLoad(load,
7651 out_loc,
7652 out,
7653 /* placeholder */ 0x5678,
7654 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007655 __ SetReorder(reordering);
7656 return;
7657 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007658 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007659 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007660 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007661
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007662 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007663 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7664 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007665 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007666 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007667 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7668 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007669}
7670
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007671void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7673 locations->SetOut(Location::ConstantLocation(constant));
7674}
7675
7676void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7677 // Will be generated at use site.
7678}
7679
7680void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7681 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007683 InvokeRuntimeCallingConvention calling_convention;
7684 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7685}
7686
7687void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7688 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007689 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007690 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7691 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007692 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007693 }
7694 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7695}
7696
7697void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7698 LocationSummary* locations =
7699 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7700 switch (mul->GetResultType()) {
7701 case Primitive::kPrimInt:
7702 case Primitive::kPrimLong:
7703 locations->SetInAt(0, Location::RequiresRegister());
7704 locations->SetInAt(1, Location::RequiresRegister());
7705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7706 break;
7707
7708 case Primitive::kPrimFloat:
7709 case Primitive::kPrimDouble:
7710 locations->SetInAt(0, Location::RequiresFpuRegister());
7711 locations->SetInAt(1, Location::RequiresFpuRegister());
7712 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7713 break;
7714
7715 default:
7716 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7717 }
7718}
7719
7720void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7721 Primitive::Type type = instruction->GetType();
7722 LocationSummary* locations = instruction->GetLocations();
7723 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7724
7725 switch (type) {
7726 case Primitive::kPrimInt: {
7727 Register dst = locations->Out().AsRegister<Register>();
7728 Register lhs = locations->InAt(0).AsRegister<Register>();
7729 Register rhs = locations->InAt(1).AsRegister<Register>();
7730
7731 if (isR6) {
7732 __ MulR6(dst, lhs, rhs);
7733 } else {
7734 __ MulR2(dst, lhs, rhs);
7735 }
7736 break;
7737 }
7738 case Primitive::kPrimLong: {
7739 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7740 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7741 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7742 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7743 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7744 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7745
7746 // Extra checks to protect caused by the existance of A1_A2.
7747 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7748 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7749 DCHECK_NE(dst_high, lhs_low);
7750 DCHECK_NE(dst_high, rhs_low);
7751
7752 // A_B * C_D
7753 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7754 // dst_lo: [ low(B*D) ]
7755 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7756
7757 if (isR6) {
7758 __ MulR6(TMP, lhs_high, rhs_low);
7759 __ MulR6(dst_high, lhs_low, rhs_high);
7760 __ Addu(dst_high, dst_high, TMP);
7761 __ MuhuR6(TMP, lhs_low, rhs_low);
7762 __ Addu(dst_high, dst_high, TMP);
7763 __ MulR6(dst_low, lhs_low, rhs_low);
7764 } else {
7765 __ MulR2(TMP, lhs_high, rhs_low);
7766 __ MulR2(dst_high, lhs_low, rhs_high);
7767 __ Addu(dst_high, dst_high, TMP);
7768 __ MultuR2(lhs_low, rhs_low);
7769 __ Mfhi(TMP);
7770 __ Addu(dst_high, dst_high, TMP);
7771 __ Mflo(dst_low);
7772 }
7773 break;
7774 }
7775 case Primitive::kPrimFloat:
7776 case Primitive::kPrimDouble: {
7777 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7778 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7779 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7780 if (type == Primitive::kPrimFloat) {
7781 __ MulS(dst, lhs, rhs);
7782 } else {
7783 __ MulD(dst, lhs, rhs);
7784 }
7785 break;
7786 }
7787 default:
7788 LOG(FATAL) << "Unexpected mul type " << type;
7789 }
7790}
7791
7792void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7793 LocationSummary* locations =
7794 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7795 switch (neg->GetResultType()) {
7796 case Primitive::kPrimInt:
7797 case Primitive::kPrimLong:
7798 locations->SetInAt(0, Location::RequiresRegister());
7799 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7800 break;
7801
7802 case Primitive::kPrimFloat:
7803 case Primitive::kPrimDouble:
7804 locations->SetInAt(0, Location::RequiresFpuRegister());
7805 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7806 break;
7807
7808 default:
7809 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7810 }
7811}
7812
7813void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7814 Primitive::Type type = instruction->GetType();
7815 LocationSummary* locations = instruction->GetLocations();
7816
7817 switch (type) {
7818 case Primitive::kPrimInt: {
7819 Register dst = locations->Out().AsRegister<Register>();
7820 Register src = locations->InAt(0).AsRegister<Register>();
7821 __ Subu(dst, ZERO, src);
7822 break;
7823 }
7824 case Primitive::kPrimLong: {
7825 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7826 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7827 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7828 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7829 __ Subu(dst_low, ZERO, src_low);
7830 __ Sltu(TMP, ZERO, dst_low);
7831 __ Subu(dst_high, ZERO, src_high);
7832 __ Subu(dst_high, dst_high, TMP);
7833 break;
7834 }
7835 case Primitive::kPrimFloat:
7836 case Primitive::kPrimDouble: {
7837 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7838 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7839 if (type == Primitive::kPrimFloat) {
7840 __ NegS(dst, src);
7841 } else {
7842 __ NegD(dst, src);
7843 }
7844 break;
7845 }
7846 default:
7847 LOG(FATAL) << "Unexpected neg type " << type;
7848 }
7849}
7850
7851void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7852 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007853 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007854 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007855 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007856 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7857 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007858}
7859
7860void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007861 // Note: if heap poisoning is enabled, the entry point takes care
7862 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007863 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7864 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007865}
7866
7867void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7868 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007869 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007870 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007871 if (instruction->IsStringAlloc()) {
7872 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7873 } else {
7874 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007875 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007876 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7877}
7878
7879void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007880 // Note: if heap poisoning is enabled, the entry point takes care
7881 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007882 if (instruction->IsStringAlloc()) {
7883 // String is allocated through StringFactory. Call NewEmptyString entry point.
7884 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007885 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007886 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7887 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7888 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007889 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007890 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7891 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007892 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007893 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007894 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007895}
7896
7897void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7899 locations->SetInAt(0, Location::RequiresRegister());
7900 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7901}
7902
7903void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7904 Primitive::Type type = instruction->GetType();
7905 LocationSummary* locations = instruction->GetLocations();
7906
7907 switch (type) {
7908 case Primitive::kPrimInt: {
7909 Register dst = locations->Out().AsRegister<Register>();
7910 Register src = locations->InAt(0).AsRegister<Register>();
7911 __ Nor(dst, src, ZERO);
7912 break;
7913 }
7914
7915 case Primitive::kPrimLong: {
7916 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7917 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7918 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7919 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7920 __ Nor(dst_high, src_high, ZERO);
7921 __ Nor(dst_low, src_low, ZERO);
7922 break;
7923 }
7924
7925 default:
7926 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7927 }
7928}
7929
7930void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7932 locations->SetInAt(0, Location::RequiresRegister());
7933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7934}
7935
7936void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7937 LocationSummary* locations = instruction->GetLocations();
7938 __ Xori(locations->Out().AsRegister<Register>(),
7939 locations->InAt(0).AsRegister<Register>(),
7940 1);
7941}
7942
7943void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007944 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7945 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007946}
7947
Calin Juravle2ae48182016-03-16 14:05:09 +00007948void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7949 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007950 return;
7951 }
7952 Location obj = instruction->GetLocations()->InAt(0);
7953
7954 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007955 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007956}
7957
Calin Juravle2ae48182016-03-16 14:05:09 +00007958void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007959 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007960 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007961
7962 Location obj = instruction->GetLocations()->InAt(0);
7963
7964 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7965}
7966
7967void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007968 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007969}
7970
7971void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7972 HandleBinaryOp(instruction);
7973}
7974
7975void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7976 HandleBinaryOp(instruction);
7977}
7978
7979void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7980 LOG(FATAL) << "Unreachable";
7981}
7982
7983void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7984 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7985}
7986
7987void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7988 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7989 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7990 if (location.IsStackSlot()) {
7991 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7992 } else if (location.IsDoubleStackSlot()) {
7993 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7994 }
7995 locations->SetOut(location);
7996}
7997
7998void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7999 ATTRIBUTE_UNUSED) {
8000 // Nothing to do, the parameter is already at its location.
8001}
8002
8003void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8004 LocationSummary* locations =
8005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8006 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8007}
8008
8009void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8010 ATTRIBUTE_UNUSED) {
8011 // Nothing to do, the method is already at its location.
8012}
8013
8014void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008016 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008017 locations->SetInAt(i, Location::Any());
8018 }
8019 locations->SetOut(Location::Any());
8020}
8021
8022void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8023 LOG(FATAL) << "Unreachable";
8024}
8025
8026void LocationsBuilderMIPS::VisitRem(HRem* rem) {
8027 Primitive::Type type = rem->GetResultType();
8028 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008029 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008030 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8031
8032 switch (type) {
8033 case Primitive::kPrimInt:
8034 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008035 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008036 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8037 break;
8038
8039 case Primitive::kPrimLong: {
8040 InvokeRuntimeCallingConvention calling_convention;
8041 locations->SetInAt(0, Location::RegisterPairLocation(
8042 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8043 locations->SetInAt(1, Location::RegisterPairLocation(
8044 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8045 locations->SetOut(calling_convention.GetReturnLocation(type));
8046 break;
8047 }
8048
8049 case Primitive::kPrimFloat:
8050 case Primitive::kPrimDouble: {
8051 InvokeRuntimeCallingConvention calling_convention;
8052 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8053 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8054 locations->SetOut(calling_convention.GetReturnLocation(type));
8055 break;
8056 }
8057
8058 default:
8059 LOG(FATAL) << "Unexpected rem type " << type;
8060 }
8061}
8062
8063void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
8064 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008065
8066 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08008067 case Primitive::kPrimInt:
8068 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008069 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008070 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008071 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008072 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8073 break;
8074 }
8075 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008076 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008077 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008078 break;
8079 }
8080 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008081 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008082 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008083 break;
8084 }
8085 default:
8086 LOG(FATAL) << "Unexpected rem type " << type;
8087 }
8088}
8089
Igor Murashkind01745e2017-04-05 16:40:31 -07008090void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8091 constructor_fence->SetLocations(nullptr);
8092}
8093
8094void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8095 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8096 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8097}
8098
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008099void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8100 memory_barrier->SetLocations(nullptr);
8101}
8102
8103void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8104 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8105}
8106
8107void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8108 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8109 Primitive::Type return_type = ret->InputAt(0)->GetType();
8110 locations->SetInAt(0, MipsReturnLocation(return_type));
8111}
8112
8113void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8114 codegen_->GenerateFrameExit();
8115}
8116
8117void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8118 ret->SetLocations(nullptr);
8119}
8120
8121void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8122 codegen_->GenerateFrameExit();
8123}
8124
Alexey Frunze92d90602015-12-18 18:16:36 -08008125void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8126 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008127}
8128
Alexey Frunze92d90602015-12-18 18:16:36 -08008129void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8130 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008131}
8132
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008133void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8134 HandleShift(shl);
8135}
8136
8137void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8138 HandleShift(shl);
8139}
8140
8141void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8142 HandleShift(shr);
8143}
8144
8145void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8146 HandleShift(shr);
8147}
8148
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008149void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8150 HandleBinaryOp(instruction);
8151}
8152
8153void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8154 HandleBinaryOp(instruction);
8155}
8156
8157void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8158 HandleFieldGet(instruction, instruction->GetFieldInfo());
8159}
8160
8161void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8162 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8163}
8164
8165void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8166 HandleFieldSet(instruction, instruction->GetFieldInfo());
8167}
8168
8169void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008170 HandleFieldSet(instruction,
8171 instruction->GetFieldInfo(),
8172 instruction->GetDexPc(),
8173 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008174}
8175
8176void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8177 HUnresolvedInstanceFieldGet* instruction) {
8178 FieldAccessCallingConventionMIPS calling_convention;
8179 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8180 instruction->GetFieldType(),
8181 calling_convention);
8182}
8183
8184void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8185 HUnresolvedInstanceFieldGet* instruction) {
8186 FieldAccessCallingConventionMIPS calling_convention;
8187 codegen_->GenerateUnresolvedFieldAccess(instruction,
8188 instruction->GetFieldType(),
8189 instruction->GetFieldIndex(),
8190 instruction->GetDexPc(),
8191 calling_convention);
8192}
8193
8194void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8195 HUnresolvedInstanceFieldSet* instruction) {
8196 FieldAccessCallingConventionMIPS calling_convention;
8197 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8198 instruction->GetFieldType(),
8199 calling_convention);
8200}
8201
8202void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8203 HUnresolvedInstanceFieldSet* instruction) {
8204 FieldAccessCallingConventionMIPS calling_convention;
8205 codegen_->GenerateUnresolvedFieldAccess(instruction,
8206 instruction->GetFieldType(),
8207 instruction->GetFieldIndex(),
8208 instruction->GetDexPc(),
8209 calling_convention);
8210}
8211
8212void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8213 HUnresolvedStaticFieldGet* instruction) {
8214 FieldAccessCallingConventionMIPS calling_convention;
8215 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8216 instruction->GetFieldType(),
8217 calling_convention);
8218}
8219
8220void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8221 HUnresolvedStaticFieldGet* instruction) {
8222 FieldAccessCallingConventionMIPS calling_convention;
8223 codegen_->GenerateUnresolvedFieldAccess(instruction,
8224 instruction->GetFieldType(),
8225 instruction->GetFieldIndex(),
8226 instruction->GetDexPc(),
8227 calling_convention);
8228}
8229
8230void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8231 HUnresolvedStaticFieldSet* instruction) {
8232 FieldAccessCallingConventionMIPS calling_convention;
8233 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8234 instruction->GetFieldType(),
8235 calling_convention);
8236}
8237
8238void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8239 HUnresolvedStaticFieldSet* instruction) {
8240 FieldAccessCallingConventionMIPS calling_convention;
8241 codegen_->GenerateUnresolvedFieldAccess(instruction,
8242 instruction->GetFieldType(),
8243 instruction->GetFieldIndex(),
8244 instruction->GetDexPc(),
8245 calling_convention);
8246}
8247
8248void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008249 LocationSummary* locations =
8250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01008251 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008252}
8253
8254void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8255 HBasicBlock* block = instruction->GetBlock();
8256 if (block->GetLoopInformation() != nullptr) {
8257 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8258 // The back edge will generate the suspend check.
8259 return;
8260 }
8261 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8262 // The goto will generate the suspend check.
8263 return;
8264 }
8265 GenerateSuspendCheck(instruction, nullptr);
8266}
8267
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008268void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8269 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008271 InvokeRuntimeCallingConvention calling_convention;
8272 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8273}
8274
8275void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008276 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008277 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8278}
8279
8280void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8281 Primitive::Type input_type = conversion->GetInputType();
8282 Primitive::Type result_type = conversion->GetResultType();
8283 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008284 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008285
8286 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8287 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8288 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8289 }
8290
8291 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008292 if (!isR6 &&
8293 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8294 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008295 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008296 }
8297
8298 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8299
8300 if (call_kind == LocationSummary::kNoCall) {
8301 if (Primitive::IsFloatingPointType(input_type)) {
8302 locations->SetInAt(0, Location::RequiresFpuRegister());
8303 } else {
8304 locations->SetInAt(0, Location::RequiresRegister());
8305 }
8306
8307 if (Primitive::IsFloatingPointType(result_type)) {
8308 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8309 } else {
8310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8311 }
8312 } else {
8313 InvokeRuntimeCallingConvention calling_convention;
8314
8315 if (Primitive::IsFloatingPointType(input_type)) {
8316 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8317 } else {
8318 DCHECK_EQ(input_type, Primitive::kPrimLong);
8319 locations->SetInAt(0, Location::RegisterPairLocation(
8320 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8321 }
8322
8323 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8324 }
8325}
8326
8327void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8328 LocationSummary* locations = conversion->GetLocations();
8329 Primitive::Type result_type = conversion->GetResultType();
8330 Primitive::Type input_type = conversion->GetInputType();
8331 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008332 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008333
8334 DCHECK_NE(input_type, result_type);
8335
8336 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8337 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8338 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8339 Register src = locations->InAt(0).AsRegister<Register>();
8340
Alexey Frunzea871ef12016-06-27 15:20:11 -07008341 if (dst_low != src) {
8342 __ Move(dst_low, src);
8343 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008344 __ Sra(dst_high, src, 31);
8345 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8346 Register dst = locations->Out().AsRegister<Register>();
8347 Register src = (input_type == Primitive::kPrimLong)
8348 ? locations->InAt(0).AsRegisterPairLow<Register>()
8349 : locations->InAt(0).AsRegister<Register>();
8350
8351 switch (result_type) {
8352 case Primitive::kPrimChar:
8353 __ Andi(dst, src, 0xFFFF);
8354 break;
8355 case Primitive::kPrimByte:
8356 if (has_sign_extension) {
8357 __ Seb(dst, src);
8358 } else {
8359 __ Sll(dst, src, 24);
8360 __ Sra(dst, dst, 24);
8361 }
8362 break;
8363 case Primitive::kPrimShort:
8364 if (has_sign_extension) {
8365 __ Seh(dst, src);
8366 } else {
8367 __ Sll(dst, src, 16);
8368 __ Sra(dst, dst, 16);
8369 }
8370 break;
8371 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008372 if (dst != src) {
8373 __ Move(dst, src);
8374 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008375 break;
8376
8377 default:
8378 LOG(FATAL) << "Unexpected type conversion from " << input_type
8379 << " to " << result_type;
8380 }
8381 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008382 if (input_type == Primitive::kPrimLong) {
8383 if (isR6) {
8384 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8385 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8386 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8387 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8388 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8389 __ Mtc1(src_low, FTMP);
8390 __ Mthc1(src_high, FTMP);
8391 if (result_type == Primitive::kPrimFloat) {
8392 __ Cvtsl(dst, FTMP);
8393 } else {
8394 __ Cvtdl(dst, FTMP);
8395 }
8396 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008397 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8398 : kQuickL2d;
8399 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008400 if (result_type == Primitive::kPrimFloat) {
8401 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8402 } else {
8403 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8404 }
8405 }
8406 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008407 Register src = locations->InAt(0).AsRegister<Register>();
8408 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8409 __ Mtc1(src, FTMP);
8410 if (result_type == Primitive::kPrimFloat) {
8411 __ Cvtsw(dst, FTMP);
8412 } else {
8413 __ Cvtdw(dst, FTMP);
8414 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008415 }
8416 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8417 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008418
8419 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8420 // value of the output type if the input is outside of the range after the truncation or
8421 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8422 // results. This matches the desired float/double-to-int/long conversion exactly.
8423 //
8424 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8425 // value when the input is either a NaN or is outside of the range of the output type
8426 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8427 // the same result.
8428 //
8429 // The code takes care of the different behaviors by first comparing the input to the
8430 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8431 // If the input is greater than or equal to the minimum, it procedes to the truncate
8432 // instruction, which will handle such an input the same way irrespective of NAN2008.
8433 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8434 // in order to return either zero or the minimum value.
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008435 if (result_type == Primitive::kPrimLong) {
8436 if (isR6) {
8437 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8438 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8439 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8440 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8441 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008442
8443 if (input_type == Primitive::kPrimFloat) {
8444 __ TruncLS(FTMP, src);
8445 } else {
8446 __ TruncLD(FTMP, src);
8447 }
8448 __ Mfc1(dst_low, FTMP);
8449 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008450 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008451 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8452 : kQuickD2l;
8453 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008454 if (input_type == Primitive::kPrimFloat) {
8455 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8456 } else {
8457 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008459 }
8460 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008461 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8462 Register dst = locations->Out().AsRegister<Register>();
8463 MipsLabel truncate;
8464 MipsLabel done;
8465
Lena Djokicf4e23a82017-05-09 15:43:45 +02008466 if (!isR6) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008467 if (input_type == Primitive::kPrimFloat) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008468 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8469 __ LoadConst32(TMP, min_val);
8470 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008471 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008472 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8473 __ LoadConst32(TMP, High32Bits(min_val));
8474 __ Mtc1(ZERO, FTMP);
8475 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008476 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008477
8478 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008479 __ ColeS(0, FTMP, src);
8480 } else {
8481 __ ColeD(0, FTMP, src);
8482 }
8483 __ Bc1t(0, &truncate);
8484
8485 if (input_type == Primitive::kPrimFloat) {
8486 __ CeqS(0, src, src);
8487 } else {
8488 __ CeqD(0, src, src);
8489 }
8490 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8491 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008492
8493 __ B(&done);
8494
8495 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008496 }
8497
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008498 if (input_type == Primitive::kPrimFloat) {
8499 __ TruncWS(FTMP, src);
8500 } else {
8501 __ TruncWD(FTMP, src);
8502 }
8503 __ Mfc1(dst, FTMP);
8504
Lena Djokicf4e23a82017-05-09 15:43:45 +02008505 if (!isR6) {
8506 __ Bind(&done);
8507 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008508 }
8509 } else if (Primitive::IsFloatingPointType(result_type) &&
8510 Primitive::IsFloatingPointType(input_type)) {
8511 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8512 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8513 if (result_type == Primitive::kPrimFloat) {
8514 __ Cvtsd(dst, src);
8515 } else {
8516 __ Cvtds(dst, src);
8517 }
8518 } else {
8519 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8520 << " to " << result_type;
8521 }
8522}
8523
8524void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8525 HandleShift(ushr);
8526}
8527
8528void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8529 HandleShift(ushr);
8530}
8531
8532void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8533 HandleBinaryOp(instruction);
8534}
8535
8536void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8537 HandleBinaryOp(instruction);
8538}
8539
8540void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8541 // Nothing to do, this should be removed during prepare for register allocator.
8542 LOG(FATAL) << "Unreachable";
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8546 // Nothing to do, this should be removed during prepare for register allocator.
8547 LOG(FATAL) << "Unreachable";
8548}
8549
8550void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008551 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008552}
8553
8554void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008555 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008556}
8557
8558void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008559 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008560}
8561
8562void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008563 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008564}
8565
8566void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008567 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008568}
8569
8570void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008571 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008572}
8573
8574void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008575 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008576}
8577
8578void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008579 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008580}
8581
8582void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008583 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008584}
8585
8586void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008587 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008588}
8589
8590void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008591 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008592}
8593
8594void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008595 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008596}
8597
8598void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008599 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008600}
8601
8602void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008603 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008604}
8605
8606void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008607 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008608}
8609
8610void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008611 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008612}
8613
8614void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008615 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008616}
8617
8618void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008619 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008620}
8621
8622void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008623 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008624}
8625
8626void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008627 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008628}
8629
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008630void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8631 LocationSummary* locations =
8632 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8633 locations->SetInAt(0, Location::RequiresRegister());
8634}
8635
Alexey Frunze96b66822016-09-10 02:32:44 -07008636void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8637 int32_t lower_bound,
8638 uint32_t num_entries,
8639 HBasicBlock* switch_block,
8640 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008641 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008642 Register temp_reg = TMP;
8643 __ Addiu32(temp_reg, value_reg, -lower_bound);
8644 // Jump to default if index is negative
8645 // Note: We don't check the case that index is positive while value < lower_bound, because in
8646 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8647 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8648
Alexey Frunze96b66822016-09-10 02:32:44 -07008649 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008650 // Jump to successors[0] if value == lower_bound.
8651 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8652 int32_t last_index = 0;
8653 for (; num_entries - last_index > 2; last_index += 2) {
8654 __ Addiu(temp_reg, temp_reg, -2);
8655 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8656 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8657 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8658 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8659 }
8660 if (num_entries - last_index == 2) {
8661 // The last missing case_value.
8662 __ Addiu(temp_reg, temp_reg, -1);
8663 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008664 }
8665
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008666 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008667 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008668 __ B(codegen_->GetLabelOf(default_block));
8669 }
8670}
8671
Alexey Frunze96b66822016-09-10 02:32:44 -07008672void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8673 Register constant_area,
8674 int32_t lower_bound,
8675 uint32_t num_entries,
8676 HBasicBlock* switch_block,
8677 HBasicBlock* default_block) {
8678 // Create a jump table.
8679 std::vector<MipsLabel*> labels(num_entries);
8680 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8681 for (uint32_t i = 0; i < num_entries; i++) {
8682 labels[i] = codegen_->GetLabelOf(successors[i]);
8683 }
8684 JumpTable* table = __ CreateJumpTable(std::move(labels));
8685
8686 // Is the value in range?
8687 __ Addiu32(TMP, value_reg, -lower_bound);
8688 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8689 __ Sltiu(AT, TMP, num_entries);
8690 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8691 } else {
8692 __ LoadConst32(AT, num_entries);
8693 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8694 }
8695
8696 // We are in the range of the table.
8697 // Load the target address from the jump table, indexing by the value.
8698 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008699 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008700 __ Lw(TMP, TMP, 0);
8701 // Compute the absolute target address by adding the table start address
8702 // (the table contains offsets to targets relative to its start).
8703 __ Addu(TMP, TMP, AT);
8704 // And jump.
8705 __ Jr(TMP);
8706 __ NopIfNoReordering();
8707}
8708
8709void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8710 int32_t lower_bound = switch_instr->GetStartValue();
8711 uint32_t num_entries = switch_instr->GetNumEntries();
8712 LocationSummary* locations = switch_instr->GetLocations();
8713 Register value_reg = locations->InAt(0).AsRegister<Register>();
8714 HBasicBlock* switch_block = switch_instr->GetBlock();
8715 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8716
8717 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8718 num_entries > kPackedSwitchJumpTableThreshold) {
8719 // R6 uses PC-relative addressing to access the jump table.
8720 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8721 // the jump table and it is implemented by changing HPackedSwitch to
8722 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8723 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8724 GenTableBasedPackedSwitch(value_reg,
8725 ZERO,
8726 lower_bound,
8727 num_entries,
8728 switch_block,
8729 default_block);
8730 } else {
8731 GenPackedSwitchWithCompares(value_reg,
8732 lower_bound,
8733 num_entries,
8734 switch_block,
8735 default_block);
8736 }
8737}
8738
8739void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8740 LocationSummary* locations =
8741 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8742 locations->SetInAt(0, Location::RequiresRegister());
8743 // Constant area pointer (HMipsComputeBaseMethodAddress).
8744 locations->SetInAt(1, Location::RequiresRegister());
8745}
8746
8747void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8748 int32_t lower_bound = switch_instr->GetStartValue();
8749 uint32_t num_entries = switch_instr->GetNumEntries();
8750 LocationSummary* locations = switch_instr->GetLocations();
8751 Register value_reg = locations->InAt(0).AsRegister<Register>();
8752 Register constant_area = locations->InAt(1).AsRegister<Register>();
8753 HBasicBlock* switch_block = switch_instr->GetBlock();
8754 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8755
8756 // This is an R2-only path. HPackedSwitch has been changed to
8757 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8758 // required to address the jump table relative to PC.
8759 GenTableBasedPackedSwitch(value_reg,
8760 constant_area,
8761 lower_bound,
8762 num_entries,
8763 switch_block,
8764 default_block);
8765}
8766
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008767void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8768 HMipsComputeBaseMethodAddress* insn) {
8769 LocationSummary* locations =
8770 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8771 locations->SetOut(Location::RequiresRegister());
8772}
8773
8774void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8775 HMipsComputeBaseMethodAddress* insn) {
8776 LocationSummary* locations = insn->GetLocations();
8777 Register reg = locations->Out().AsRegister<Register>();
8778
8779 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8780
8781 // Generate a dummy PC-relative call to obtain PC.
8782 __ Nal();
8783 // Grab the return address off RA.
8784 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008785 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008786
8787 // Remember this offset (the obtained PC value) for later use with constant area.
8788 __ BindPcRelBaseLabel();
8789}
8790
8791void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8792 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8793 locations->SetOut(Location::RequiresRegister());
8794}
8795
8796void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
8797 Register reg = base->GetLocations()->Out().AsRegister<Register>();
8798 CodeGeneratorMIPS::PcRelativePatchInfo* info =
8799 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008800 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8801 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008802 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
Alexey Frunze6b892cd2017-01-03 17:11:38 -08008803 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, reg, ZERO);
8804 __ Addiu(reg, reg, /* placeholder */ 0x5678);
8805 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008806}
8807
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008808void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8809 // The trampoline uses the same calling convention as dex calling conventions,
8810 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8811 // the method_idx.
8812 HandleInvoke(invoke);
8813}
8814
8815void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8816 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8817}
8818
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008819void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8820 LocationSummary* locations =
8821 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8822 locations->SetInAt(0, Location::RequiresRegister());
8823 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008824}
8825
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008826void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8827 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008828 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008829 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008830 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008831 __ LoadFromOffset(kLoadWord,
8832 locations->Out().AsRegister<Register>(),
8833 locations->InAt(0).AsRegister<Register>(),
8834 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008835 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008836 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008837 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008838 __ LoadFromOffset(kLoadWord,
8839 locations->Out().AsRegister<Register>(),
8840 locations->InAt(0).AsRegister<Register>(),
8841 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008842 __ LoadFromOffset(kLoadWord,
8843 locations->Out().AsRegister<Register>(),
8844 locations->Out().AsRegister<Register>(),
8845 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008846 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008847}
8848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008849#undef __
8850#undef QUICK_ENTRY_POINT
8851
8852} // namespace mips
8853} // namespace art