blob: 3e79f474b6099b7b26b9731c635645ca71fb00a4 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
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_mips64.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips64/asm_support_mips64.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070020#include "art_method.h"
21#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080022#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070023#include "entrypoints/quick/quick_entrypoints.h"
24#include "entrypoints/quick/quick_entrypoints_enum.h"
25#include "gc/accounting/card_table.h"
26#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070027#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070028#include "mirror/array-inl.h"
29#include "mirror/class-inl.h"
30#include "offsets.h"
31#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070033#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070034#include "utils/stack_checks.h"
35
36namespace art {
37namespace mips64 {
38
39static constexpr int kCurrentMethodStackOffset = 0;
40static constexpr GpuRegister kMethodRegisterArgument = A0;
41
Alexey Frunze4147fcc2017-06-17 19:57:27 -070042// Flags controlling the use of thunks for Baker read barriers.
43constexpr bool kBakerReadBarrierThunksEnableForFields = true;
44constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
45constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
46
Alexey Frunze4dda3372015-06-01 18:31:49 -070047Location Mips64ReturnLocation(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 case Primitive::kPrimLong:
56 return Location::RegisterLocation(V0);
57
58 case Primitive::kPrimFloat:
59 case Primitive::kPrimDouble:
60 return Location::FpuRegisterLocation(F0);
61
62 case Primitive::kPrimVoid:
63 return Location();
64 }
65 UNREACHABLE();
66}
67
68Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
69 return Mips64ReturnLocation(type);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
73 return Location::RegisterLocation(kMethodRegisterArgument);
74}
75
76Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
77 Location next_location;
78 if (type == Primitive::kPrimVoid) {
79 LOG(FATAL) << "Unexpected parameter type " << type;
80 }
81
82 if (Primitive::IsFloatingPointType(type) &&
83 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
84 next_location = Location::FpuRegisterLocation(
85 calling_convention.GetFpuRegisterAt(float_index_++));
86 gp_index_++;
87 } else if (!Primitive::IsFloatingPointType(type) &&
88 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
89 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
90 float_index_++;
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
94 : Location::StackSlot(stack_offset);
95 }
96
97 // Space on the stack is reserved for all arguments.
98 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
99
Alexey Frunze4dda3372015-06-01 18:31:49 -0700100 return next_location;
101}
102
103Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
104 return Mips64ReturnLocation(type);
105}
106
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100107// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
108#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700110
111class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
112 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000113 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100116 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
118 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000119 if (instruction_->CanThrowIntoCatchBlock()) {
120 // Live registers will be restored in the catch block if caught.
121 SaveLiveRegisters(codegen, instruction_->GetLocations());
122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700123 // We're moving two locations to locations that could overlap, so we need a parallel
124 // move resolver.
125 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
128 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
131 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100132 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
133 ? kQuickThrowStringBounds
134 : kQuickThrowArrayBounds;
135 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100136 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700137 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
138 }
139
Alexandre Rames8158f282015-08-07 10:26:17 +0100140 bool IsFatal() const OVERRIDE { return true; }
141
Roland Levillain46648892015-06-19 16:07:18 +0100142 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
143
Alexey Frunze4dda3372015-06-01 18:31:49 -0700144 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
146};
147
148class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
149 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700150 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
151 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700152
153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
154 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
155 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100156 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
158 }
159
Alexandre Rames8158f282015-08-07 10:26:17 +0100160 bool IsFatal() const OVERRIDE { return true; }
161
Roland Levillain46648892015-06-19 16:07:18 +0100162 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
163
Alexey Frunze4dda3372015-06-01 18:31:49 -0700164 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700165 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
166};
167
168class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
169 public:
170 LoadClassSlowPathMIPS64(HLoadClass* cls,
171 HInstruction* at,
172 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700173 bool do_clinit,
174 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr)
175 : SlowPathCodeMIPS64(at),
176 cls_(cls),
177 dex_pc_(dex_pc),
178 do_clinit_(do_clinit),
179 bss_info_high_(bss_info_high) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700180 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
181 }
182
183 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000184 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700185 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700186 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700187 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
188 InvokeRuntimeCallingConvention calling_convention;
189 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
190 const bool is_load_class_bss_entry =
191 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 __ Bind(GetEntryLabel());
193 SaveLiveRegisters(codegen, locations);
194
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700195 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
196 GpuRegister entry_address = kNoGpuRegister;
197 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
198 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
199 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
200 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
201 // kSaveEverything call.
202 entry_address = temp_is_a0 ? out.AsRegister<GpuRegister>() : temp;
203 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
204 if (temp_is_a0) {
205 __ Move(entry_address, temp);
206 }
207 }
208
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000209 dex::TypeIndex type_index = cls_->GetTypeIndex();
210 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100211 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
212 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000213 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 if (do_clinit_) {
215 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
216 } else {
217 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
218 }
219
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700220 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
221 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
222 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
223 DCHECK(bss_info_high_);
224 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
225 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
226 __ Bind(&info_low->label);
227 __ StoreToOffset(kStoreWord,
228 calling_convention.GetRegisterAt(0),
229 entry_address,
230 /* placeholder */ 0x5678);
231 }
232
Alexey Frunze4dda3372015-06-01 18:31:49 -0700233 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700234 if (out.IsValid()) {
235 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000236 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700237 mips64_codegen->MoveLocation(out,
238 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
239 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700240 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700241 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700242
243 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
244 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
245 // For non-Baker read barriers we need to re-calculate the address of
246 // the class entry.
247 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000248 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700249 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
250 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
251 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
252 __ StoreToOffset(kStoreWord, out.AsRegister<GpuRegister>(), TMP, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000253 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700254 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700255 }
256
Roland Levillain46648892015-06-19 16:07:18 +0100257 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
258
Alexey Frunze4dda3372015-06-01 18:31:49 -0700259 private:
260 // The class this slow path will load.
261 HLoadClass* const cls_;
262
Alexey Frunze4dda3372015-06-01 18:31:49 -0700263 // The dex PC of `at_`.
264 const uint32_t dex_pc_;
265
266 // Whether to initialize the class.
267 const bool do_clinit_;
268
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700269 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
270 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
271
Alexey Frunze4dda3372015-06-01 18:31:49 -0700272 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
273};
274
275class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
276 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700277 explicit LoadStringSlowPathMIPS64(HLoadString* instruction,
278 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high)
279 : SlowPathCodeMIPS64(instruction), bss_info_high_(bss_info_high) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700280
281 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700282 DCHECK(instruction_->IsLoadString());
283 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700284 LocationSummary* locations = instruction_->GetLocations();
285 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700286 HLoadString* load = instruction_->AsLoadString();
287 const dex::StringIndex string_index = load->GetStringIndex();
288 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700289 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700290 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
291 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700295 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
296 GpuRegister entry_address = kNoGpuRegister;
297 if (baker_or_no_read_barriers) {
298 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
299 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
300 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
301 // kSaveEverything call.
302 entry_address = temp_is_a0 ? out : temp;
303 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
304 if (temp_is_a0) {
305 __ Move(entry_address, temp);
306 }
307 }
308
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000309 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100310 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 instruction_,
312 instruction_->GetDexPc(),
313 this);
314 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700315
316 // Store the resolved string to the BSS entry.
317 if (baker_or_no_read_barriers) {
318 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
319 DCHECK(bss_info_high_);
320 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
321 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(),
322 string_index,
323 bss_info_high_);
324 __ Bind(&info_low->label);
325 __ StoreToOffset(kStoreWord,
326 calling_convention.GetRegisterAt(0),
327 entry_address,
328 /* placeholder */ 0x5678);
329 }
330
Alexey Frunze4dda3372015-06-01 18:31:49 -0700331 Primitive::Type type = instruction_->GetType();
332 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700333 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700334 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800336
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700337 // Store the resolved string to the BSS entry.
338 if (!baker_or_no_read_barriers) {
339 // For non-Baker read barriers we need to re-calculate the address of
340 // the string entry.
341 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
342 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
343 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
344 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index, info_high);
345 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
346 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
347 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700348 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700349 }
350
Roland Levillain46648892015-06-19 16:07:18 +0100351 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
352
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700354 // Pointer to the high half PC-relative patch info.
355 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
356
Alexey Frunze4dda3372015-06-01 18:31:49 -0700357 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
358};
359
360class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
361 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000362 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363
364 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
365 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
366 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000367 if (instruction_->CanThrowIntoCatchBlock()) {
368 // Live registers will be restored in the catch block if caught.
369 SaveLiveRegisters(codegen, instruction_->GetLocations());
370 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100371 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 instruction_,
373 instruction_->GetDexPc(),
374 this);
375 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
376 }
377
Alexandre Rames8158f282015-08-07 10:26:17 +0100378 bool IsFatal() const OVERRIDE { return true; }
379
Roland Levillain46648892015-06-19 16:07:18 +0100380 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
381
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700383 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
384};
385
386class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
387 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100388 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000389 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390
391 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200392 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
394 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200395 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100396 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200398 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700400 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700402 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403 }
404 }
405
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700406 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 DCHECK(successor_ == nullptr);
408 return &return_label_;
409 }
410
Roland Levillain46648892015-06-19 16:07:18 +0100411 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
412
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414 // If not null, the block to branch to after the suspend check.
415 HBasicBlock* const successor_;
416
417 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700418 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700419
420 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
421};
422
423class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
424 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800425 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
426 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
429 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800430
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100431 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700432 DCHECK(instruction_->IsCheckCast()
433 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
434 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
435
436 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800437 if (!is_fatal_) {
438 SaveLiveRegisters(codegen, locations);
439 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700440
441 // We're moving two locations to locations that could overlap, so we need a parallel
442 // move resolver.
443 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800444 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700445 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
446 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800447 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700448 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
449 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700450 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100451 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800452 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700453 Primitive::Type ret_type = instruction_->GetType();
454 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
455 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700456 } else {
457 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800458 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
459 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700460 }
461
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800462 if (!is_fatal_) {
463 RestoreLiveRegisters(codegen, locations);
464 __ Bc(GetExitLabel());
465 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700466 }
467
Roland Levillain46648892015-06-19 16:07:18 +0100468 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
469
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800470 bool IsFatal() const OVERRIDE { return is_fatal_; }
471
Alexey Frunze4dda3372015-06-01 18:31:49 -0700472 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800473 const bool is_fatal_;
474
Alexey Frunze4dda3372015-06-01 18:31:49 -0700475 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
476};
477
478class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
479 public:
Aart Bik42249c32016-01-07 15:33:50 -0800480 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000481 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700482
483 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800484 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700485 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100486 LocationSummary* locations = instruction_->GetLocations();
487 SaveLiveRegisters(codegen, locations);
488 InvokeRuntimeCallingConvention calling_convention;
489 __ LoadConst32(calling_convention.GetRegisterAt(0),
490 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100491 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100492 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700493 }
494
Roland Levillain46648892015-06-19 16:07:18 +0100495 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
496
Alexey Frunze4dda3372015-06-01 18:31:49 -0700497 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700498 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
499};
500
Alexey Frunze15958152017-02-09 19:08:30 -0800501class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
502 public:
503 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
504
505 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
506 LocationSummary* locations = instruction_->GetLocations();
507 __ Bind(GetEntryLabel());
508 SaveLiveRegisters(codegen, locations);
509
510 InvokeRuntimeCallingConvention calling_convention;
511 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
512 parallel_move.AddMove(
513 locations->InAt(0),
514 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 nullptr);
517 parallel_move.AddMove(
518 locations->InAt(1),
519 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
520 Primitive::kPrimInt,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(2),
524 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
525 Primitive::kPrimNot,
526 nullptr);
527 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
528
529 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
530 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
531 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
532 RestoreLiveRegisters(codegen, locations);
533 __ Bc(GetExitLabel());
534 }
535
536 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
537
538 private:
539 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
540};
541
542// Slow path marking an object reference `ref` during a read
543// barrier. The field `obj.field` in the object `obj` holding this
544// reference does not get updated by this slow path after marking (see
545// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
546//
547// This means that after the execution of this slow path, `ref` will
548// always be up-to-date, but `obj.field` may not; i.e., after the
549// flip, `ref` will be a to-space reference, but `obj.field` will
550// probably still be a from-space reference (unless it gets updated by
551// another thread, or if another thread installed another object
552// reference (different from `ref`) in `obj.field`).
553//
554// If `entrypoint` is a valid location it is assumed to already be
555// holding the entrypoint. The case where the entrypoint is passed in
556// is for the GcRoot read barrier.
557class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
558 public:
559 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
560 Location ref,
561 Location entrypoint = Location::NoLocation())
562 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
563 DCHECK(kEmitCompilerReadBarrier);
564 }
565
566 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
567
568 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
569 LocationSummary* locations = instruction_->GetLocations();
570 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
571 DCHECK(locations->CanCall());
572 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
573 DCHECK(instruction_->IsInstanceFieldGet() ||
574 instruction_->IsStaticFieldGet() ||
575 instruction_->IsArrayGet() ||
576 instruction_->IsArraySet() ||
577 instruction_->IsLoadClass() ||
578 instruction_->IsLoadString() ||
579 instruction_->IsInstanceOf() ||
580 instruction_->IsCheckCast() ||
581 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
582 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
583 << "Unexpected instruction in read barrier marking slow path: "
584 << instruction_->DebugName();
585
586 __ Bind(GetEntryLabel());
587 // No need to save live registers; it's taken care of by the
588 // entrypoint. Also, there is no need to update the stack mask,
589 // as this runtime call will not trigger a garbage collection.
590 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
591 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
592 (S2 <= ref_reg && ref_reg <= S7) ||
593 (ref_reg == S8)) << ref_reg;
594 // "Compact" slow path, saving two moves.
595 //
596 // Instead of using the standard runtime calling convention (input
597 // and output in A0 and V0 respectively):
598 //
599 // A0 <- ref
600 // V0 <- ReadBarrierMark(A0)
601 // ref <- V0
602 //
603 // we just use rX (the register containing `ref`) as input and output
604 // of a dedicated entrypoint:
605 //
606 // rX <- ReadBarrierMarkRegX(rX)
607 //
608 if (entrypoint_.IsValid()) {
609 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
610 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
611 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
612 __ Nop();
613 } else {
614 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100615 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800616 // This runtime call does not require a stack map.
617 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
618 instruction_,
619 this);
620 }
621 __ Bc(GetExitLabel());
622 }
623
624 private:
625 // The location (register) of the marked object reference.
626 const Location ref_;
627
628 // The location of the entrypoint if already loaded.
629 const Location entrypoint_;
630
631 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
632};
633
634// Slow path marking an object reference `ref` during a read barrier,
635// and if needed, atomically updating the field `obj.field` in the
636// object `obj` holding this reference after marking (contrary to
637// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
638// `obj.field`).
639//
640// This means that after the execution of this slow path, both `ref`
641// and `obj.field` will be up-to-date; i.e., after the flip, both will
642// hold the same to-space reference (unless another thread installed
643// another object reference (different from `ref`) in `obj.field`).
644class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
645 public:
646 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
647 Location ref,
648 GpuRegister obj,
649 Location field_offset,
650 GpuRegister temp1)
651 : SlowPathCodeMIPS64(instruction),
652 ref_(ref),
653 obj_(obj),
654 field_offset_(field_offset),
655 temp1_(temp1) {
656 DCHECK(kEmitCompilerReadBarrier);
657 }
658
659 const char* GetDescription() const OVERRIDE {
660 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
661 }
662
663 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
664 LocationSummary* locations = instruction_->GetLocations();
665 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
666 DCHECK(locations->CanCall());
667 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
668 // This slow path is only used by the UnsafeCASObject intrinsic.
669 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
670 << "Unexpected instruction in read barrier marking and field updating slow path: "
671 << instruction_->DebugName();
672 DCHECK(instruction_->GetLocations()->Intrinsified());
673 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
674 DCHECK(field_offset_.IsRegister()) << field_offset_;
675
676 __ Bind(GetEntryLabel());
677
678 // Save the old reference.
679 // Note that we cannot use AT or TMP to save the old reference, as those
680 // are used by the code that follows, but we need the old reference after
681 // the call to the ReadBarrierMarkRegX entry point.
682 DCHECK_NE(temp1_, AT);
683 DCHECK_NE(temp1_, TMP);
684 __ Move(temp1_, ref_reg);
685
686 // No need to save live registers; it's taken care of by the
687 // entrypoint. Also, there is no need to update the stack mask,
688 // as this runtime call will not trigger a garbage collection.
689 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
690 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
691 (S2 <= ref_reg && ref_reg <= S7) ||
692 (ref_reg == S8)) << ref_reg;
693 // "Compact" slow path, saving two moves.
694 //
695 // Instead of using the standard runtime calling convention (input
696 // and output in A0 and V0 respectively):
697 //
698 // A0 <- ref
699 // V0 <- ReadBarrierMark(A0)
700 // ref <- V0
701 //
702 // we just use rX (the register containing `ref`) as input and output
703 // of a dedicated entrypoint:
704 //
705 // rX <- ReadBarrierMarkRegX(rX)
706 //
707 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100708 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800709 // This runtime call does not require a stack map.
710 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
711 instruction_,
712 this);
713
714 // If the new reference is different from the old reference,
715 // update the field in the holder (`*(obj_ + field_offset_)`).
716 //
717 // Note that this field could also hold a different object, if
718 // another thread had concurrently changed it. In that case, the
719 // the compare-and-set (CAS) loop below would abort, leaving the
720 // field as-is.
721 Mips64Label done;
722 __ Beqc(temp1_, ref_reg, &done);
723
724 // Update the the holder's field atomically. This may fail if
725 // mutator updates before us, but it's OK. This is achieved
726 // using a strong compare-and-set (CAS) operation with relaxed
727 // memory synchronization ordering, where the expected value is
728 // the old reference and the desired value is the new reference.
729
730 // Convenience aliases.
731 GpuRegister base = obj_;
732 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
733 GpuRegister expected = temp1_;
734 GpuRegister value = ref_reg;
735 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
736 GpuRegister tmp = AT; // Value in memory.
737
738 __ Daddu(tmp_ptr, base, offset);
739
740 if (kPoisonHeapReferences) {
741 __ PoisonHeapReference(expected);
742 // Do not poison `value` if it is the same register as
743 // `expected`, which has just been poisoned.
744 if (value != expected) {
745 __ PoisonHeapReference(value);
746 }
747 }
748
749 // do {
750 // tmp = [r_ptr] - expected;
751 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
752
753 Mips64Label loop_head, exit_loop;
754 __ Bind(&loop_head);
755 __ Ll(tmp, tmp_ptr);
756 // The LL instruction sign-extends the 32-bit value, but
757 // 32-bit references must be zero-extended. Zero-extend `tmp`.
758 __ Dext(tmp, tmp, 0, 32);
759 __ Bnec(tmp, expected, &exit_loop);
760 __ Move(tmp, value);
761 __ Sc(tmp, tmp_ptr);
762 __ Beqzc(tmp, &loop_head);
763 __ Bind(&exit_loop);
764
765 if (kPoisonHeapReferences) {
766 __ UnpoisonHeapReference(expected);
767 // Do not unpoison `value` if it is the same register as
768 // `expected`, which has just been unpoisoned.
769 if (value != expected) {
770 __ UnpoisonHeapReference(value);
771 }
772 }
773
774 __ Bind(&done);
775 __ Bc(GetExitLabel());
776 }
777
778 private:
779 // The location (register) of the marked object reference.
780 const Location ref_;
781 // The register containing the object holding the marked object reference field.
782 const GpuRegister obj_;
783 // The location of the offset of the marked reference field within `obj_`.
784 Location field_offset_;
785
786 const GpuRegister temp1_;
787
788 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
789};
790
791// Slow path generating a read barrier for a heap reference.
792class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
793 public:
794 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
795 Location out,
796 Location ref,
797 Location obj,
798 uint32_t offset,
799 Location index)
800 : SlowPathCodeMIPS64(instruction),
801 out_(out),
802 ref_(ref),
803 obj_(obj),
804 offset_(offset),
805 index_(index) {
806 DCHECK(kEmitCompilerReadBarrier);
807 // If `obj` is equal to `out` or `ref`, it means the initial object
808 // has been overwritten by (or after) the heap object reference load
809 // to be instrumented, e.g.:
810 //
811 // __ LoadFromOffset(kLoadWord, out, out, offset);
812 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
813 //
814 // In that case, we have lost the information about the original
815 // object, and the emitted read barrier cannot work properly.
816 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
817 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
818 }
819
820 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
821 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
822 LocationSummary* locations = instruction_->GetLocations();
823 Primitive::Type type = Primitive::kPrimNot;
824 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
825 DCHECK(locations->CanCall());
826 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
827 DCHECK(instruction_->IsInstanceFieldGet() ||
828 instruction_->IsStaticFieldGet() ||
829 instruction_->IsArrayGet() ||
830 instruction_->IsInstanceOf() ||
831 instruction_->IsCheckCast() ||
832 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
833 << "Unexpected instruction in read barrier for heap reference slow path: "
834 << instruction_->DebugName();
835
836 __ Bind(GetEntryLabel());
837 SaveLiveRegisters(codegen, locations);
838
839 // We may have to change the index's value, but as `index_` is a
840 // constant member (like other "inputs" of this slow path),
841 // introduce a copy of it, `index`.
842 Location index = index_;
843 if (index_.IsValid()) {
844 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
845 if (instruction_->IsArrayGet()) {
846 // Compute the actual memory offset and store it in `index`.
847 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
848 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
849 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
850 // We are about to change the value of `index_reg` (see the
851 // calls to art::mips64::Mips64Assembler::Sll and
852 // art::mips64::MipsAssembler::Addiu32 below), but it has
853 // not been saved by the previous call to
854 // art::SlowPathCode::SaveLiveRegisters, as it is a
855 // callee-save register --
856 // art::SlowPathCode::SaveLiveRegisters does not consider
857 // callee-save registers, as it has been designed with the
858 // assumption that callee-save registers are supposed to be
859 // handled by the called function. So, as a callee-save
860 // register, `index_reg` _would_ eventually be saved onto
861 // the stack, but it would be too late: we would have
862 // changed its value earlier. Therefore, we manually save
863 // it here into another freely available register,
864 // `free_reg`, chosen of course among the caller-save
865 // registers (as a callee-save `free_reg` register would
866 // exhibit the same problem).
867 //
868 // Note we could have requested a temporary register from
869 // the register allocator instead; but we prefer not to, as
870 // this is a slow path, and we know we can find a
871 // caller-save register that is available.
872 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
873 __ Move(free_reg, index_reg);
874 index_reg = free_reg;
875 index = Location::RegisterLocation(index_reg);
876 } else {
877 // The initial register stored in `index_` has already been
878 // saved in the call to art::SlowPathCode::SaveLiveRegisters
879 // (as it is not a callee-save register), so we can freely
880 // use it.
881 }
882 // Shifting the index value contained in `index_reg` by the scale
883 // factor (2) cannot overflow in practice, as the runtime is
884 // unable to allocate object arrays with a size larger than
885 // 2^26 - 1 (that is, 2^28 - 4 bytes).
886 __ Sll(index_reg, index_reg, TIMES_4);
887 static_assert(
888 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
889 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
890 __ Addiu32(index_reg, index_reg, offset_);
891 } else {
892 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
893 // intrinsics, `index_` is not shifted by a scale factor of 2
894 // (as in the case of ArrayGet), as it is actually an offset
895 // to an object field within an object.
896 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
897 DCHECK(instruction_->GetLocations()->Intrinsified());
898 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
899 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
900 << instruction_->AsInvoke()->GetIntrinsic();
901 DCHECK_EQ(offset_, 0U);
902 DCHECK(index_.IsRegister());
903 }
904 }
905
906 // We're moving two or three locations to locations that could
907 // overlap, so we need a parallel move resolver.
908 InvokeRuntimeCallingConvention calling_convention;
909 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
910 parallel_move.AddMove(ref_,
911 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
912 Primitive::kPrimNot,
913 nullptr);
914 parallel_move.AddMove(obj_,
915 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
916 Primitive::kPrimNot,
917 nullptr);
918 if (index.IsValid()) {
919 parallel_move.AddMove(index,
920 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
921 Primitive::kPrimInt,
922 nullptr);
923 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
924 } else {
925 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
926 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
927 }
928 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
929 instruction_,
930 instruction_->GetDexPc(),
931 this);
932 CheckEntrypointTypes<
933 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
934 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
935
936 RestoreLiveRegisters(codegen, locations);
937 __ Bc(GetExitLabel());
938 }
939
940 const char* GetDescription() const OVERRIDE {
941 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
942 }
943
944 private:
945 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
946 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
947 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
948 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
949 if (i != ref &&
950 i != obj &&
951 !codegen->IsCoreCalleeSaveRegister(i) &&
952 !codegen->IsBlockedCoreRegister(i)) {
953 return static_cast<GpuRegister>(i);
954 }
955 }
956 // We shall never fail to find a free caller-save register, as
957 // there are more than two core caller-save registers on MIPS64
958 // (meaning it is possible to find one which is different from
959 // `ref` and `obj`).
960 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
961 LOG(FATAL) << "Could not find a free caller-save register";
962 UNREACHABLE();
963 }
964
965 const Location out_;
966 const Location ref_;
967 const Location obj_;
968 const uint32_t offset_;
969 // An additional location containing an index to an array.
970 // Only used for HArrayGet and the UnsafeGetObject &
971 // UnsafeGetObjectVolatile intrinsics.
972 const Location index_;
973
974 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
975};
976
977// Slow path generating a read barrier for a GC root.
978class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
979 public:
980 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
981 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
982 DCHECK(kEmitCompilerReadBarrier);
983 }
984
985 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
986 LocationSummary* locations = instruction_->GetLocations();
987 Primitive::Type type = Primitive::kPrimNot;
988 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
989 DCHECK(locations->CanCall());
990 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
991 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
992 << "Unexpected instruction in read barrier for GC root slow path: "
993 << instruction_->DebugName();
994
995 __ Bind(GetEntryLabel());
996 SaveLiveRegisters(codegen, locations);
997
998 InvokeRuntimeCallingConvention calling_convention;
999 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
1000 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1001 root_,
1002 Primitive::kPrimNot);
1003 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1004 instruction_,
1005 instruction_->GetDexPc(),
1006 this);
1007 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1008 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1009
1010 RestoreLiveRegisters(codegen, locations);
1011 __ Bc(GetExitLabel());
1012 }
1013
1014 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
1015
1016 private:
1017 const Location out_;
1018 const Location root_;
1019
1020 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
1021};
1022
Alexey Frunze4dda3372015-06-01 18:31:49 -07001023CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
1024 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001025 const CompilerOptions& compiler_options,
1026 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001027 : CodeGenerator(graph,
1028 kNumberOfGpuRegisters,
1029 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001030 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001031 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1032 arraysize(kCoreCalleeSaves)),
1033 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1034 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001035 compiler_options,
1036 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001037 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001038 location_builder_(graph, this),
1039 instruction_visitor_(graph, this),
1040 move_resolver_(graph->GetArena(), this),
Goran Jakovljevic19680d32017-05-11 10:38:36 +02001041 assembler_(graph->GetArena(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001042 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001043 uint32_literals_(std::less<uint32_t>(),
1044 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001045 uint64_literals_(std::less<uint64_t>(),
1046 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001047 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001048 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001049 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001050 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001051 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001052 jit_string_patches_(StringReferenceValueComparator(),
1053 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1054 jit_class_patches_(TypeReferenceValueComparator(),
1055 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001056 // Save RA (containing the return address) to mimic Quick.
1057 AddAllocatedRegister(Location::RegisterLocation(RA));
1058}
1059
1060#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001061// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1062#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001063#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -07001064
1065void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001066 // Ensure that we fix up branches.
1067 __ FinalizeCode();
1068
1069 // Adjust native pc offsets in stack maps.
1070 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001071 uint32_t old_position =
1072 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001073 uint32_t new_position = __ GetAdjustedPosition(old_position);
1074 DCHECK_GE(new_position, old_position);
1075 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1076 }
1077
1078 // Adjust pc offsets for the disassembly information.
1079 if (disasm_info_ != nullptr) {
1080 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1081 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1082 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1083 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1084 it.second.start = __ GetAdjustedPosition(it.second.start);
1085 it.second.end = __ GetAdjustedPosition(it.second.end);
1086 }
1087 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1088 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1089 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1090 }
1091 }
1092
Alexey Frunze4dda3372015-06-01 18:31:49 -07001093 CodeGenerator::Finalize(allocator);
1094}
1095
1096Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1097 return codegen_->GetAssembler();
1098}
1099
1100void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001101 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001102 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1103}
1104
1105void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001106 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001107 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1108}
1109
1110void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1111 // Pop reg
1112 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001113 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001114}
1115
1116void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1117 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001118 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001119 __ Sd(GpuRegister(reg), SP, 0);
1120}
1121
1122void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1123 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1124 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1125 // Allocate a scratch register other than TMP, if available.
1126 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1127 // automatically unspilled when the scratch scope object is destroyed).
1128 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1129 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001130 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001131 __ LoadFromOffset(load_type,
1132 GpuRegister(ensure_scratch.GetRegister()),
1133 SP,
1134 index1 + stack_offset);
1135 __ LoadFromOffset(load_type,
1136 TMP,
1137 SP,
1138 index2 + stack_offset);
1139 __ StoreToOffset(store_type,
1140 GpuRegister(ensure_scratch.GetRegister()),
1141 SP,
1142 index2 + stack_offset);
1143 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1144}
1145
1146static dwarf::Reg DWARFReg(GpuRegister reg) {
1147 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1148}
1149
David Srbeckyba702002016-02-01 18:15:29 +00001150static dwarf::Reg DWARFReg(FpuRegister reg) {
1151 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1152}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153
1154void CodeGeneratorMIPS64::GenerateFrameEntry() {
1155 __ Bind(&frame_entry_label_);
1156
1157 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1158
1159 if (do_overflow_check) {
1160 __ LoadFromOffset(kLoadWord,
1161 ZERO,
1162 SP,
1163 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1164 RecordPcInfo(nullptr, 0);
1165 }
1166
Alexey Frunze4dda3372015-06-01 18:31:49 -07001167 if (HasEmptyFrame()) {
1168 return;
1169 }
1170
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001171 // Make sure the frame size isn't unreasonably large.
1172 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1173 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1174 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001175
1176 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001177
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001178 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001179 __ IncreaseFrameSize(ofs);
1180
1181 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1182 GpuRegister reg = kCoreCalleeSaves[i];
1183 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001184 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001185 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186 __ cfi().RelOffset(DWARFReg(reg), ofs);
1187 }
1188 }
1189
1190 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1191 FpuRegister reg = kFpuCalleeSaves[i];
1192 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001193 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001194 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001195 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001196 }
1197 }
1198
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001199 // Save the current method if we need it. Note that we do not
1200 // do this in HCurrentMethod, as the instruction might have been removed
1201 // in the SSA graph.
1202 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001203 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001204 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001205
1206 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1207 // Initialize should_deoptimize flag to 0.
1208 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1209 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001210}
1211
1212void CodeGeneratorMIPS64::GenerateFrameExit() {
1213 __ cfi().RememberState();
1214
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001216 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001217
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001218 // For better instruction scheduling restore RA before other registers.
1219 uint32_t ofs = GetFrameSize();
1220 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001221 GpuRegister reg = kCoreCalleeSaves[i];
1222 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001223 ofs -= kMips64DoublewordSize;
1224 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001225 __ cfi().Restore(DWARFReg(reg));
1226 }
1227 }
1228
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001229 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1230 FpuRegister reg = kFpuCalleeSaves[i];
1231 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1232 ofs -= kMips64DoublewordSize;
1233 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1234 __ cfi().Restore(DWARFReg(reg));
1235 }
1236 }
1237
1238 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 }
1240
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001241 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001242
1243 __ cfi().RestoreState();
1244 __ cfi().DefCFAOffset(GetFrameSize());
1245}
1246
1247void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1248 __ Bind(GetLabelOf(block));
1249}
1250
1251void CodeGeneratorMIPS64::MoveLocation(Location destination,
1252 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001253 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001254 if (source.Equals(destination)) {
1255 return;
1256 }
1257
1258 // A valid move can always be inferred from the destination and source
1259 // locations. When moving from and to a register, the argument type can be
1260 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001262 DCHECK_EQ(unspecified_type, false);
1263
1264 if (destination.IsRegister() || destination.IsFpuRegister()) {
1265 if (unspecified_type) {
1266 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1267 if (source.IsStackSlot() ||
1268 (src_cst != nullptr && (src_cst->IsIntConstant()
1269 || src_cst->IsFloatConstant()
1270 || src_cst->IsNullConstant()))) {
1271 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273 } else {
1274 // If the source is a double stack slot or a 64bit constant, a 64bit
1275 // type is appropriate. Else the source is a register, and since the
1276 // type has not been specified, we chose a 64bit type to force a 64bit
1277 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001279 }
1280 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1282 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001283 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1284 // Move to GPR/FPR from stack
1285 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001287 __ LoadFpuFromOffset(load_type,
1288 destination.AsFpuRegister<FpuRegister>(),
1289 SP,
1290 source.GetStackIndex());
1291 } else {
1292 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1293 __ LoadFromOffset(load_type,
1294 destination.AsRegister<GpuRegister>(),
1295 SP,
1296 source.GetStackIndex());
1297 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001298 } else if (source.IsSIMDStackSlot()) {
1299 __ LoadFpuFromOffset(kLoadQuadword,
1300 destination.AsFpuRegister<FpuRegister>(),
1301 SP,
1302 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001303 } else if (source.IsConstant()) {
1304 // Move to GPR/FPR from constant
1305 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001306 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001307 gpr = destination.AsRegister<GpuRegister>();
1308 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001309 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001310 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1311 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1312 gpr = ZERO;
1313 } else {
1314 __ LoadConst32(gpr, value);
1315 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001316 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001317 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1318 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1319 gpr = ZERO;
1320 } else {
1321 __ LoadConst64(gpr, value);
1322 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001323 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001324 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001325 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001326 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001327 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1328 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001329 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 if (destination.IsRegister()) {
1331 // Move to GPR from GPR
1332 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1333 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001334 DCHECK(destination.IsFpuRegister());
1335 if (Primitive::Is64BitType(dst_type)) {
1336 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1337 } else {
1338 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1339 }
1340 }
1341 } else if (source.IsFpuRegister()) {
1342 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001343 if (GetGraph()->HasSIMD()) {
1344 __ MoveV(VectorRegisterFrom(destination),
1345 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001346 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001347 // Move to FPR from FPR
1348 if (dst_type == Primitive::kPrimFloat) {
1349 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1350 } else {
1351 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
1352 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1353 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001354 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001355 } else {
1356 DCHECK(destination.IsRegister());
1357 if (Primitive::Is64BitType(dst_type)) {
1358 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1359 } else {
1360 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1361 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001362 }
1363 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001364 } else if (destination.IsSIMDStackSlot()) {
1365 if (source.IsFpuRegister()) {
1366 __ StoreFpuToOffset(kStoreQuadword,
1367 source.AsFpuRegister<FpuRegister>(),
1368 SP,
1369 destination.GetStackIndex());
1370 } else {
1371 DCHECK(source.IsSIMDStackSlot());
1372 __ LoadFpuFromOffset(kLoadQuadword,
1373 FTMP,
1374 SP,
1375 source.GetStackIndex());
1376 __ StoreFpuToOffset(kStoreQuadword,
1377 FTMP,
1378 SP,
1379 destination.GetStackIndex());
1380 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001381 } else { // The destination is not a register. It must be a stack slot.
1382 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1383 if (source.IsRegister() || source.IsFpuRegister()) {
1384 if (unspecified_type) {
1385 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001386 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001387 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001388 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001389 }
1390 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001391 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1392 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001393 // Move to stack from GPR/FPR
1394 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1395 if (source.IsRegister()) {
1396 __ StoreToOffset(store_type,
1397 source.AsRegister<GpuRegister>(),
1398 SP,
1399 destination.GetStackIndex());
1400 } else {
1401 __ StoreFpuToOffset(store_type,
1402 source.AsFpuRegister<FpuRegister>(),
1403 SP,
1404 destination.GetStackIndex());
1405 }
1406 } else if (source.IsConstant()) {
1407 // Move to stack from constant
1408 HConstant* src_cst = source.GetConstant();
1409 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001410 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001411 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001412 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1413 if (value != 0) {
1414 gpr = TMP;
1415 __ LoadConst32(gpr, value);
1416 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001417 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001418 DCHECK(destination.IsDoubleStackSlot());
1419 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1420 if (value != 0) {
1421 gpr = TMP;
1422 __ LoadConst64(gpr, value);
1423 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001424 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001425 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001426 } else {
1427 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1428 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1429 // Move to stack from stack
1430 if (destination.IsStackSlot()) {
1431 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1432 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1433 } else {
1434 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1435 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1436 }
1437 }
1438 }
1439}
1440
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001441void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001442 DCHECK(!loc1.IsConstant());
1443 DCHECK(!loc2.IsConstant());
1444
1445 if (loc1.Equals(loc2)) {
1446 return;
1447 }
1448
1449 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1450 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1451 bool is_fp_reg1 = loc1.IsFpuRegister();
1452 bool is_fp_reg2 = loc2.IsFpuRegister();
1453
1454 if (loc2.IsRegister() && loc1.IsRegister()) {
1455 // Swap 2 GPRs
1456 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1457 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1458 __ Move(TMP, r2);
1459 __ Move(r2, r1);
1460 __ Move(r1, TMP);
1461 } else if (is_fp_reg2 && is_fp_reg1) {
1462 // Swap 2 FPRs
1463 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1464 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001465 if (type == Primitive::kPrimFloat) {
1466 __ MovS(FTMP, r1);
1467 __ MovS(r1, r2);
1468 __ MovS(r2, FTMP);
1469 } else {
1470 DCHECK_EQ(type, Primitive::kPrimDouble);
1471 __ MovD(FTMP, r1);
1472 __ MovD(r1, r2);
1473 __ MovD(r2, FTMP);
1474 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001475 } else if (is_slot1 != is_slot2) {
1476 // Swap GPR/FPR and stack slot
1477 Location reg_loc = is_slot1 ? loc2 : loc1;
1478 Location mem_loc = is_slot1 ? loc1 : loc2;
1479 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1480 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1481 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1482 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1483 if (reg_loc.IsFpuRegister()) {
1484 __ StoreFpuToOffset(store_type,
1485 reg_loc.AsFpuRegister<FpuRegister>(),
1486 SP,
1487 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001488 if (mem_loc.IsStackSlot()) {
1489 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1490 } else {
1491 DCHECK(mem_loc.IsDoubleStackSlot());
1492 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1493 }
1494 } else {
1495 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1496 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1497 }
1498 } else if (is_slot1 && is_slot2) {
1499 move_resolver_.Exchange(loc1.GetStackIndex(),
1500 loc2.GetStackIndex(),
1501 loc1.IsDoubleStackSlot());
1502 } else {
1503 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1504 }
1505}
1506
Calin Juravle175dc732015-08-25 15:42:32 +01001507void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1508 DCHECK(location.IsRegister());
1509 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1510}
1511
Calin Juravlee460d1d2015-09-29 04:52:17 +01001512void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1513 if (location.IsRegister()) {
1514 locations->AddTemp(location);
1515 } else {
1516 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1517 }
1518}
1519
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001520void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1521 GpuRegister value,
1522 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001523 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001524 GpuRegister card = AT;
1525 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001526 if (value_can_be_null) {
1527 __ Beqzc(value, &done);
1528 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001529 __ LoadFromOffset(kLoadDoubleword,
1530 card,
1531 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001532 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001533 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1534 __ Daddu(temp, card, temp);
1535 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001536 if (value_can_be_null) {
1537 __ Bind(&done);
1538 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001539}
1540
Alexey Frunze19f6c692016-11-30 19:19:55 -08001541template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1542inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1543 const ArenaDeque<PcRelativePatchInfo>& infos,
1544 ArenaVector<LinkerPatch>* linker_patches) {
1545 for (const PcRelativePatchInfo& info : infos) {
1546 const DexFile& dex_file = info.target_dex_file;
1547 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001548 DCHECK(info.label.IsBound());
1549 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1550 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1551 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1552 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001553 }
1554}
1555
1556void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1557 DCHECK(linker_patches->empty());
1558 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001559 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001560 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001561 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001562 type_bss_entry_patches_.size() +
1563 pc_relative_string_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001564 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001565 if (GetCompilerOptions().IsBootImage()) {
1566 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Alexey Frunzef63f5692016-12-13 17:43:11 -08001567 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001568 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1569 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001570 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1571 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001572 } else {
1573 DCHECK(pc_relative_method_patches_.empty());
1574 DCHECK(pc_relative_type_patches_.empty());
1575 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1576 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001577 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001578 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1579 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001580 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1581 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001582 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001583}
1584
Vladimir Marko65979462017-05-19 17:25:12 +01001585CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001586 MethodReference target_method,
1587 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001588 return NewPcRelativePatch(*target_method.dex_file,
1589 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001590 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001591 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001592}
1593
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001594CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001595 MethodReference target_method,
1596 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001597 return NewPcRelativePatch(*target_method.dex_file,
1598 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001599 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001600 &method_bss_entry_patches_);
1601}
1602
Alexey Frunzef63f5692016-12-13 17:43:11 -08001603CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001604 const DexFile& dex_file,
1605 dex::TypeIndex type_index,
1606 const PcRelativePatchInfo* info_high) {
1607 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001608}
1609
Vladimir Marko1998cd02017-01-13 13:02:58 +00001610CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001611 const DexFile& dex_file,
1612 dex::TypeIndex type_index,
1613 const PcRelativePatchInfo* info_high) {
1614 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001615}
1616
Vladimir Marko65979462017-05-19 17:25:12 +01001617CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001618 const DexFile& dex_file,
1619 dex::StringIndex string_index,
1620 const PcRelativePatchInfo* info_high) {
1621 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001622}
1623
Alexey Frunze19f6c692016-11-30 19:19:55 -08001624CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001625 const DexFile& dex_file,
1626 uint32_t offset_or_index,
1627 const PcRelativePatchInfo* info_high,
1628 ArenaDeque<PcRelativePatchInfo>* patches) {
1629 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001630 return &patches->back();
1631}
1632
Alexey Frunzef63f5692016-12-13 17:43:11 -08001633Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1634 return map->GetOrCreate(
1635 value,
1636 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1637}
1638
Alexey Frunze19f6c692016-11-30 19:19:55 -08001639Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1640 return uint64_literals_.GetOrCreate(
1641 value,
1642 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1643}
1644
Alexey Frunzef63f5692016-12-13 17:43:11 -08001645Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001646 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001647}
1648
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001649void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1650 GpuRegister out,
1651 PcRelativePatchInfo* info_low) {
1652 DCHECK(!info_high->patch_info_high);
1653 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001654 // Add the high half of a 32-bit offset to PC.
1655 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001656 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001657 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001658 if (info_low != nullptr) {
1659 DCHECK_EQ(info_low->patch_info_high, info_high);
1660 __ Bind(&info_low->label);
1661 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001662}
1663
Alexey Frunze627c1a02017-01-30 19:28:14 -08001664Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1665 dex::StringIndex string_index,
1666 Handle<mirror::String> handle) {
1667 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1668 reinterpret_cast64<uint64_t>(handle.GetReference()));
1669 return jit_string_patches_.GetOrCreate(
1670 StringReference(&dex_file, string_index),
1671 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1672}
1673
1674Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1675 dex::TypeIndex type_index,
1676 Handle<mirror::Class> handle) {
1677 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1678 reinterpret_cast64<uint64_t>(handle.GetReference()));
1679 return jit_class_patches_.GetOrCreate(
1680 TypeReference(&dex_file, type_index),
1681 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1682}
1683
1684void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1685 const uint8_t* roots_data,
1686 const Literal* literal,
1687 uint64_t index_in_table) const {
1688 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1689 uintptr_t address =
1690 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1691 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1692}
1693
1694void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1695 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001696 const StringReference& string_reference = entry.first;
1697 Literal* table_entry_literal = entry.second;
1698 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001699 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001700 uint64_t index_in_table = it->second;
1701 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001702 }
1703 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001704 const TypeReference& type_reference = entry.first;
1705 Literal* table_entry_literal = entry.second;
1706 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001707 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001708 uint64_t index_in_table = it->second;
1709 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001710 }
1711}
1712
David Brazdil58282f42016-01-14 12:45:10 +00001713void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001714 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1715 blocked_core_registers_[ZERO] = true;
1716 blocked_core_registers_[K0] = true;
1717 blocked_core_registers_[K1] = true;
1718 blocked_core_registers_[GP] = true;
1719 blocked_core_registers_[SP] = true;
1720 blocked_core_registers_[RA] = true;
1721
Lazar Trsicd9672662015-09-03 17:33:01 +02001722 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1723 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001724 blocked_core_registers_[AT] = true;
1725 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001726 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001727 blocked_fpu_registers_[FTMP] = true;
1728
1729 // Reserve suspend and thread registers.
1730 blocked_core_registers_[S0] = true;
1731 blocked_core_registers_[TR] = true;
1732
1733 // Reserve T9 for function calls
1734 blocked_core_registers_[T9] = true;
1735
Goran Jakovljevic782be112016-06-21 12:39:04 +02001736 if (GetGraph()->IsDebuggable()) {
1737 // Stubs do not save callee-save floating point registers. If the graph
1738 // is debuggable, we need to deal with these registers differently. For
1739 // now, just block them.
1740 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1741 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1742 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001743 }
1744}
1745
Alexey Frunze4dda3372015-06-01 18:31:49 -07001746size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1747 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001748 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001749}
1750
1751size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1752 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001753 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001754}
1755
1756size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001757 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1758 FpuRegister(reg_id),
1759 SP,
1760 stack_index);
1761 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762}
1763
1764size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001765 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1766 FpuRegister(reg_id),
1767 SP,
1768 stack_index);
1769 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770}
1771
1772void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001773 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001774}
1775
1776void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001777 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001778}
1779
Calin Juravle175dc732015-08-25 15:42:32 +01001780void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001781 HInstruction* instruction,
1782 uint32_t dex_pc,
1783 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001784 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001785 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001786 if (EntrypointRequiresStackMap(entrypoint)) {
1787 RecordPcInfo(instruction, dex_pc, slow_path);
1788 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001789}
1790
Alexey Frunze15958152017-02-09 19:08:30 -08001791void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1792 HInstruction* instruction,
1793 SlowPathCode* slow_path) {
1794 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1795 GenerateInvokeRuntime(entry_point_offset);
1796}
1797
1798void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1799 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1800 __ Jalr(T9);
1801 __ Nop();
1802}
1803
Alexey Frunze4dda3372015-06-01 18:31:49 -07001804void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1805 GpuRegister class_reg) {
1806 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1807 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1808 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001809 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1810 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811 __ Bind(slow_path->GetExitLabel());
1812}
1813
1814void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1815 __ Sync(0); // only stype 0 is supported
1816}
1817
1818void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1819 HBasicBlock* successor) {
1820 SuspendCheckSlowPathMIPS64* slow_path =
1821 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1822 codegen_->AddSlowPath(slow_path);
1823
1824 __ LoadFromOffset(kLoadUnsignedHalfword,
1825 TMP,
1826 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001827 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001828 if (successor == nullptr) {
1829 __ Bnezc(TMP, slow_path->GetEntryLabel());
1830 __ Bind(slow_path->GetReturnLabel());
1831 } else {
1832 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001833 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001834 // slow_path will return to GetLabelOf(successor).
1835 }
1836}
1837
1838InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1839 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001840 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001841 assembler_(codegen->GetAssembler()),
1842 codegen_(codegen) {}
1843
1844void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1845 DCHECK_EQ(instruction->InputCount(), 2U);
1846 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1847 Primitive::Type type = instruction->GetResultType();
1848 switch (type) {
1849 case Primitive::kPrimInt:
1850 case Primitive::kPrimLong: {
1851 locations->SetInAt(0, Location::RequiresRegister());
1852 HInstruction* right = instruction->InputAt(1);
1853 bool can_use_imm = false;
1854 if (right->IsConstant()) {
1855 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1856 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1857 can_use_imm = IsUint<16>(imm);
1858 } else if (instruction->IsAdd()) {
1859 can_use_imm = IsInt<16>(imm);
1860 } else {
1861 DCHECK(instruction->IsSub());
1862 can_use_imm = IsInt<16>(-imm);
1863 }
1864 }
1865 if (can_use_imm)
1866 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1867 else
1868 locations->SetInAt(1, Location::RequiresRegister());
1869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1870 }
1871 break;
1872
1873 case Primitive::kPrimFloat:
1874 case Primitive::kPrimDouble:
1875 locations->SetInAt(0, Location::RequiresFpuRegister());
1876 locations->SetInAt(1, Location::RequiresFpuRegister());
1877 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1878 break;
1879
1880 default:
1881 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1882 }
1883}
1884
1885void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1886 Primitive::Type type = instruction->GetType();
1887 LocationSummary* locations = instruction->GetLocations();
1888
1889 switch (type) {
1890 case Primitive::kPrimInt:
1891 case Primitive::kPrimLong: {
1892 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1893 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1894 Location rhs_location = locations->InAt(1);
1895
1896 GpuRegister rhs_reg = ZERO;
1897 int64_t rhs_imm = 0;
1898 bool use_imm = rhs_location.IsConstant();
1899 if (use_imm) {
1900 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1901 } else {
1902 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1903 }
1904
1905 if (instruction->IsAnd()) {
1906 if (use_imm)
1907 __ Andi(dst, lhs, rhs_imm);
1908 else
1909 __ And(dst, lhs, rhs_reg);
1910 } else if (instruction->IsOr()) {
1911 if (use_imm)
1912 __ Ori(dst, lhs, rhs_imm);
1913 else
1914 __ Or(dst, lhs, rhs_reg);
1915 } else if (instruction->IsXor()) {
1916 if (use_imm)
1917 __ Xori(dst, lhs, rhs_imm);
1918 else
1919 __ Xor(dst, lhs, rhs_reg);
1920 } else if (instruction->IsAdd()) {
1921 if (type == Primitive::kPrimInt) {
1922 if (use_imm)
1923 __ Addiu(dst, lhs, rhs_imm);
1924 else
1925 __ Addu(dst, lhs, rhs_reg);
1926 } else {
1927 if (use_imm)
1928 __ Daddiu(dst, lhs, rhs_imm);
1929 else
1930 __ Daddu(dst, lhs, rhs_reg);
1931 }
1932 } else {
1933 DCHECK(instruction->IsSub());
1934 if (type == Primitive::kPrimInt) {
1935 if (use_imm)
1936 __ Addiu(dst, lhs, -rhs_imm);
1937 else
1938 __ Subu(dst, lhs, rhs_reg);
1939 } else {
1940 if (use_imm)
1941 __ Daddiu(dst, lhs, -rhs_imm);
1942 else
1943 __ Dsubu(dst, lhs, rhs_reg);
1944 }
1945 }
1946 break;
1947 }
1948 case Primitive::kPrimFloat:
1949 case Primitive::kPrimDouble: {
1950 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1951 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1952 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1953 if (instruction->IsAdd()) {
1954 if (type == Primitive::kPrimFloat)
1955 __ AddS(dst, lhs, rhs);
1956 else
1957 __ AddD(dst, lhs, rhs);
1958 } else if (instruction->IsSub()) {
1959 if (type == Primitive::kPrimFloat)
1960 __ SubS(dst, lhs, rhs);
1961 else
1962 __ SubD(dst, lhs, rhs);
1963 } else {
1964 LOG(FATAL) << "Unexpected floating-point binary operation";
1965 }
1966 break;
1967 }
1968 default:
1969 LOG(FATAL) << "Unexpected binary operation type " << type;
1970 }
1971}
1972
1973void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001974 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001975
1976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1977 Primitive::Type type = instr->GetResultType();
1978 switch (type) {
1979 case Primitive::kPrimInt:
1980 case Primitive::kPrimLong: {
1981 locations->SetInAt(0, Location::RequiresRegister());
1982 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001984 break;
1985 }
1986 default:
1987 LOG(FATAL) << "Unexpected shift type " << type;
1988 }
1989}
1990
1991void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001992 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001993 LocationSummary* locations = instr->GetLocations();
1994 Primitive::Type type = instr->GetType();
1995
1996 switch (type) {
1997 case Primitive::kPrimInt:
1998 case Primitive::kPrimLong: {
1999 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2000 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2001 Location rhs_location = locations->InAt(1);
2002
2003 GpuRegister rhs_reg = ZERO;
2004 int64_t rhs_imm = 0;
2005 bool use_imm = rhs_location.IsConstant();
2006 if (use_imm) {
2007 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2008 } else {
2009 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2010 }
2011
2012 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002013 uint32_t shift_value = rhs_imm &
2014 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002015
Alexey Frunze92d90602015-12-18 18:16:36 -08002016 if (shift_value == 0) {
2017 if (dst != lhs) {
2018 __ Move(dst, lhs);
2019 }
2020 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 if (instr->IsShl()) {
2022 __ Sll(dst, lhs, shift_value);
2023 } else if (instr->IsShr()) {
2024 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002025 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002027 } else {
2028 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002029 }
2030 } else {
2031 if (shift_value < 32) {
2032 if (instr->IsShl()) {
2033 __ Dsll(dst, lhs, shift_value);
2034 } else if (instr->IsShr()) {
2035 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002036 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002037 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002038 } else {
2039 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 }
2041 } else {
2042 shift_value -= 32;
2043 if (instr->IsShl()) {
2044 __ Dsll32(dst, lhs, shift_value);
2045 } else if (instr->IsShr()) {
2046 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002047 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002048 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002049 } else {
2050 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002051 }
2052 }
2053 }
2054 } else {
2055 if (type == Primitive::kPrimInt) {
2056 if (instr->IsShl()) {
2057 __ Sllv(dst, lhs, rhs_reg);
2058 } else if (instr->IsShr()) {
2059 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002060 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002062 } else {
2063 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002064 }
2065 } else {
2066 if (instr->IsShl()) {
2067 __ Dsllv(dst, lhs, rhs_reg);
2068 } else if (instr->IsShr()) {
2069 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002070 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002071 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002072 } else {
2073 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 }
2075 }
2076 }
2077 break;
2078 }
2079 default:
2080 LOG(FATAL) << "Unexpected shift operation type " << type;
2081 }
2082}
2083
2084void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2085 HandleBinaryOp(instruction);
2086}
2087
2088void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2089 HandleBinaryOp(instruction);
2090}
2091
2092void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2093 HandleBinaryOp(instruction);
2094}
2095
2096void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2097 HandleBinaryOp(instruction);
2098}
2099
2100void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002101 Primitive::Type type = instruction->GetType();
2102 bool object_array_get_with_read_barrier =
2103 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002104 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002105 new (GetGraph()->GetArena()) LocationSummary(instruction,
2106 object_array_get_with_read_barrier
2107 ? LocationSummary::kCallOnSlowPath
2108 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002109 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2110 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2111 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002112 locations->SetInAt(0, Location::RequiresRegister());
2113 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002114 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002115 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2116 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002117 // The output overlaps in the case of an object array get with
2118 // read barriers enabled: we do not want the move to overwrite the
2119 // array's location, as we need it to emit the read barrier.
2120 locations->SetOut(Location::RequiresRegister(),
2121 object_array_get_with_read_barrier
2122 ? Location::kOutputOverlap
2123 : Location::kNoOutputOverlap);
2124 }
2125 // We need a temporary register for the read barrier marking slow
2126 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2127 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002128 bool temp_needed = instruction->GetIndex()->IsConstant()
2129 ? !kBakerReadBarrierThunksEnableForFields
2130 : !kBakerReadBarrierThunksEnableForArrays;
2131 if (temp_needed) {
2132 locations->AddTemp(Location::RequiresRegister());
2133 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002134 }
2135}
2136
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002137static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2138 auto null_checker = [codegen, instruction]() {
2139 codegen->MaybeRecordImplicitNullCheck(instruction);
2140 };
2141 return null_checker;
2142}
2143
Alexey Frunze4dda3372015-06-01 18:31:49 -07002144void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2145 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002146 Location obj_loc = locations->InAt(0);
2147 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2148 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002149 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002150 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002151 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002152
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002153 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002154 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2155 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002156 switch (type) {
2157 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002158 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002159 if (index.IsConstant()) {
2160 size_t offset =
2161 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002162 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002163 } else {
2164 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002165 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 }
2167 break;
2168 }
2169
2170 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002171 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002172 if (index.IsConstant()) {
2173 size_t offset =
2174 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002175 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002176 } else {
2177 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002178 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 }
2180 break;
2181 }
2182
2183 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002184 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002185 if (index.IsConstant()) {
2186 size_t offset =
2187 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002188 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002189 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002190 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002191 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 }
2193 break;
2194 }
2195
2196 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002197 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002198 if (maybe_compressed_char_at) {
2199 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002200 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002201 __ Dext(TMP, TMP, 0, 1);
2202 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2203 "Expecting 0=compressed, 1=uncompressed");
2204 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002205 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002206 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2207 if (maybe_compressed_char_at) {
2208 Mips64Label uncompressed_load, done;
2209 __ Bnezc(TMP, &uncompressed_load);
2210 __ LoadFromOffset(kLoadUnsignedByte,
2211 out,
2212 obj,
2213 data_offset + (const_index << TIMES_1));
2214 __ Bc(&done);
2215 __ Bind(&uncompressed_load);
2216 __ LoadFromOffset(kLoadUnsignedHalfword,
2217 out,
2218 obj,
2219 data_offset + (const_index << TIMES_2));
2220 __ Bind(&done);
2221 } else {
2222 __ LoadFromOffset(kLoadUnsignedHalfword,
2223 out,
2224 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002225 data_offset + (const_index << TIMES_2),
2226 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002227 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002228 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002229 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2230 if (maybe_compressed_char_at) {
2231 Mips64Label uncompressed_load, done;
2232 __ Bnezc(TMP, &uncompressed_load);
2233 __ Daddu(TMP, obj, index_reg);
2234 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2235 __ Bc(&done);
2236 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002237 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002238 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2239 __ Bind(&done);
2240 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002241 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002242 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002244 }
2245 break;
2246 }
2247
Alexey Frunze15958152017-02-09 19:08:30 -08002248 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002249 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002250 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002251 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2252 if (index.IsConstant()) {
2253 size_t offset =
2254 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002255 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002256 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002257 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002258 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002259 }
2260 break;
2261 }
2262
Alexey Frunze15958152017-02-09 19:08:30 -08002263 case Primitive::kPrimNot: {
2264 static_assert(
2265 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2266 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2267 // /* HeapReference<Object> */ out =
2268 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2269 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002270 bool temp_needed = index.IsConstant()
2271 ? !kBakerReadBarrierThunksEnableForFields
2272 : !kBakerReadBarrierThunksEnableForArrays;
2273 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002274 // Note that a potential implicit null check is handled in this
2275 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002276 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2277 if (index.IsConstant()) {
2278 // Array load with a constant index can be treated as a field load.
2279 size_t offset =
2280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2281 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2282 out_loc,
2283 obj,
2284 offset,
2285 temp,
2286 /* needs_null_check */ false);
2287 } else {
2288 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2289 out_loc,
2290 obj,
2291 data_offset,
2292 index,
2293 temp,
2294 /* needs_null_check */ false);
2295 }
Alexey Frunze15958152017-02-09 19:08:30 -08002296 } else {
2297 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2298 if (index.IsConstant()) {
2299 size_t offset =
2300 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2301 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2302 // If read barriers are enabled, emit read barriers other than
2303 // Baker's using a slow path (and also unpoison the loaded
2304 // reference, if heap poisoning is enabled).
2305 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2306 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002307 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002308 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2309 // If read barriers are enabled, emit read barriers other than
2310 // Baker's using a slow path (and also unpoison the loaded
2311 // reference, if heap poisoning is enabled).
2312 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2313 out_loc,
2314 out_loc,
2315 obj_loc,
2316 data_offset,
2317 index);
2318 }
2319 }
2320 break;
2321 }
2322
Alexey Frunze4dda3372015-06-01 18:31:49 -07002323 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002324 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002325 if (index.IsConstant()) {
2326 size_t offset =
2327 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002328 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002329 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002330 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002331 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002332 }
2333 break;
2334 }
2335
2336 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002337 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002338 if (index.IsConstant()) {
2339 size_t offset =
2340 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002341 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002342 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002343 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002344 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002345 }
2346 break;
2347 }
2348
2349 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002350 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002351 if (index.IsConstant()) {
2352 size_t offset =
2353 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002354 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002355 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002356 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002357 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002358 }
2359 break;
2360 }
2361
2362 case Primitive::kPrimVoid:
2363 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2364 UNREACHABLE();
2365 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002366}
2367
2368void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2369 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2370 locations->SetInAt(0, Location::RequiresRegister());
2371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2372}
2373
2374void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2375 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002376 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2378 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2379 __ LoadFromOffset(kLoadWord, out, obj, offset);
2380 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002381 // Mask out compression flag from String's array length.
2382 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2383 __ Srl(out, out, 1u);
2384 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002385}
2386
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002387Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2388 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2389 ? Location::ConstantLocation(instruction->AsConstant())
2390 : Location::RequiresRegister();
2391}
2392
2393Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2394 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2395 // We can store a non-zero float or double constant without first loading it into the FPU,
2396 // but we should only prefer this if the constant has a single use.
2397 if (instruction->IsConstant() &&
2398 (instruction->AsConstant()->IsZeroBitPattern() ||
2399 instruction->GetUses().HasExactlyOneElement())) {
2400 return Location::ConstantLocation(instruction->AsConstant());
2401 // Otherwise fall through and require an FPU register for the constant.
2402 }
2403 return Location::RequiresFpuRegister();
2404}
2405
Alexey Frunze4dda3372015-06-01 18:31:49 -07002406void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002407 Primitive::Type value_type = instruction->GetComponentType();
2408
2409 bool needs_write_barrier =
2410 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2411 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2412
Alexey Frunze4dda3372015-06-01 18:31:49 -07002413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2414 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002415 may_need_runtime_call_for_type_check ?
2416 LocationSummary::kCallOnSlowPath :
2417 LocationSummary::kNoCall);
2418
2419 locations->SetInAt(0, Location::RequiresRegister());
2420 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2421 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2422 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002423 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002424 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2425 }
2426 if (needs_write_barrier) {
2427 // Temporary register for the write barrier.
2428 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002429 }
2430}
2431
2432void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2433 LocationSummary* locations = instruction->GetLocations();
2434 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2435 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002436 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002437 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002438 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002439 bool needs_write_barrier =
2440 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002441 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002442 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002443
2444 switch (value_type) {
2445 case Primitive::kPrimBoolean:
2446 case Primitive::kPrimByte: {
2447 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002449 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002450 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002451 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2452 }
2453 if (value_location.IsConstant()) {
2454 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2455 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2456 } else {
2457 GpuRegister value = value_location.AsRegister<GpuRegister>();
2458 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002459 }
2460 break;
2461 }
2462
2463 case Primitive::kPrimShort:
2464 case Primitive::kPrimChar: {
2465 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002466 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002467 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002469 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002470 }
2471 if (value_location.IsConstant()) {
2472 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2473 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2474 } else {
2475 GpuRegister value = value_location.AsRegister<GpuRegister>();
2476 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002477 }
2478 break;
2479 }
2480
Alexey Frunze15958152017-02-09 19:08:30 -08002481 case Primitive::kPrimInt: {
2482 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2483 if (index.IsConstant()) {
2484 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2485 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002486 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002487 }
2488 if (value_location.IsConstant()) {
2489 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2490 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2491 } else {
2492 GpuRegister value = value_location.AsRegister<GpuRegister>();
2493 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2494 }
2495 break;
2496 }
2497
Alexey Frunze4dda3372015-06-01 18:31:49 -07002498 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002499 if (value_location.IsConstant()) {
2500 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002501 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002502 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002503 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002504 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002505 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002506 }
Alexey Frunze15958152017-02-09 19:08:30 -08002507 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2508 DCHECK_EQ(value, 0);
2509 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2510 DCHECK(!needs_write_barrier);
2511 DCHECK(!may_need_runtime_call_for_type_check);
2512 break;
2513 }
2514
2515 DCHECK(needs_write_barrier);
2516 GpuRegister value = value_location.AsRegister<GpuRegister>();
2517 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2518 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2519 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2520 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2521 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2522 Mips64Label done;
2523 SlowPathCodeMIPS64* slow_path = nullptr;
2524
2525 if (may_need_runtime_call_for_type_check) {
2526 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2527 codegen_->AddSlowPath(slow_path);
2528 if (instruction->GetValueCanBeNull()) {
2529 Mips64Label non_zero;
2530 __ Bnezc(value, &non_zero);
2531 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2532 if (index.IsConstant()) {
2533 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002534 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002535 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002536 }
Alexey Frunze15958152017-02-09 19:08:30 -08002537 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2538 __ Bc(&done);
2539 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002540 }
Alexey Frunze15958152017-02-09 19:08:30 -08002541
2542 // Note that when read barriers are enabled, the type checks
2543 // are performed without read barriers. This is fine, even in
2544 // the case where a class object is in the from-space after
2545 // the flip, as a comparison involving such a type would not
2546 // produce a false positive; it may of course produce a false
2547 // negative, in which case we would take the ArraySet slow
2548 // path.
2549
2550 // /* HeapReference<Class> */ temp1 = obj->klass_
2551 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2552 __ MaybeUnpoisonHeapReference(temp1);
2553
2554 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2555 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2556 // /* HeapReference<Class> */ temp2 = value->klass_
2557 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2558 // If heap poisoning is enabled, no need to unpoison `temp1`
2559 // nor `temp2`, as we are comparing two poisoned references.
2560
2561 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2562 Mips64Label do_put;
2563 __ Beqc(temp1, temp2, &do_put);
2564 // If heap poisoning is enabled, the `temp1` reference has
2565 // not been unpoisoned yet; unpoison it now.
2566 __ MaybeUnpoisonHeapReference(temp1);
2567
2568 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2569 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2570 // If heap poisoning is enabled, no need to unpoison
2571 // `temp1`, as we are comparing against null below.
2572 __ Bnezc(temp1, slow_path->GetEntryLabel());
2573 __ Bind(&do_put);
2574 } else {
2575 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2576 }
2577 }
2578
2579 GpuRegister source = value;
2580 if (kPoisonHeapReferences) {
2581 // Note that in the case where `value` is a null reference,
2582 // we do not enter this block, as a null reference does not
2583 // need poisoning.
2584 __ Move(temp1, value);
2585 __ PoisonHeapReference(temp1);
2586 source = temp1;
2587 }
2588
2589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2590 if (index.IsConstant()) {
2591 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002592 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002593 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002594 }
2595 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2596
2597 if (!may_need_runtime_call_for_type_check) {
2598 codegen_->MaybeRecordImplicitNullCheck(instruction);
2599 }
2600
2601 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2602
2603 if (done.IsLinked()) {
2604 __ Bind(&done);
2605 }
2606
2607 if (slow_path != nullptr) {
2608 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002609 }
2610 break;
2611 }
2612
2613 case Primitive::kPrimLong: {
2614 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002615 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002616 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002617 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002618 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002619 }
2620 if (value_location.IsConstant()) {
2621 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2622 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2623 } else {
2624 GpuRegister value = value_location.AsRegister<GpuRegister>();
2625 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002626 }
2627 break;
2628 }
2629
2630 case Primitive::kPrimFloat: {
2631 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002632 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002633 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002634 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002635 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002636 }
2637 if (value_location.IsConstant()) {
2638 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2639 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2640 } else {
2641 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2642 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002643 }
2644 break;
2645 }
2646
2647 case Primitive::kPrimDouble: {
2648 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002649 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002650 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002651 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002652 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002653 }
2654 if (value_location.IsConstant()) {
2655 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2656 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2657 } else {
2658 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2659 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002660 }
2661 break;
2662 }
2663
2664 case Primitive::kPrimVoid:
2665 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2666 UNREACHABLE();
2667 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002668}
2669
2670void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002671 RegisterSet caller_saves = RegisterSet::Empty();
2672 InvokeRuntimeCallingConvention calling_convention;
2673 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2674 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2675 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002676 locations->SetInAt(0, Location::RequiresRegister());
2677 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002678}
2679
2680void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2681 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002682 BoundsCheckSlowPathMIPS64* slow_path =
2683 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002684 codegen_->AddSlowPath(slow_path);
2685
2686 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2687 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2688
2689 // length is limited by the maximum positive signed 32-bit integer.
2690 // Unsigned comparison of length and index checks for index < 0
2691 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002692 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002693}
2694
Alexey Frunze15958152017-02-09 19:08:30 -08002695// Temp is used for read barrier.
2696static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2697 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002698 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002699 (kUseBakerReadBarrier ||
2700 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2701 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2702 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2703 return 1;
2704 }
2705 return 0;
2706}
2707
2708// Extra temp is used for read barrier.
2709static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2710 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2711}
2712
Alexey Frunze4dda3372015-06-01 18:31:49 -07002713void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002714 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2715 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2716
2717 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2718 switch (type_check_kind) {
2719 case TypeCheckKind::kExactCheck:
2720 case TypeCheckKind::kAbstractClassCheck:
2721 case TypeCheckKind::kClassHierarchyCheck:
2722 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002723 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002724 ? LocationSummary::kCallOnSlowPath
2725 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2726 break;
2727 case TypeCheckKind::kArrayCheck:
2728 case TypeCheckKind::kUnresolvedCheck:
2729 case TypeCheckKind::kInterfaceCheck:
2730 call_kind = LocationSummary::kCallOnSlowPath;
2731 break;
2732 }
2733
2734 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002735 locations->SetInAt(0, Location::RequiresRegister());
2736 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002737 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002738}
2739
2740void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002741 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002742 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002743 Location obj_loc = locations->InAt(0);
2744 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002745 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002746 Location temp_loc = locations->GetTemp(0);
2747 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2748 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2749 DCHECK_LE(num_temps, 2u);
2750 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002751 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2752 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2753 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2754 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2755 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2756 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2757 const uint32_t object_array_data_offset =
2758 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2759 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002760
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002761 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2762 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2763 // read barriers is done for performance and code size reasons.
2764 bool is_type_check_slow_path_fatal = false;
2765 if (!kEmitCompilerReadBarrier) {
2766 is_type_check_slow_path_fatal =
2767 (type_check_kind == TypeCheckKind::kExactCheck ||
2768 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2769 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2770 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2771 !instruction->CanThrowIntoCatchBlock();
2772 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002773 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002774 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2775 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002776 codegen_->AddSlowPath(slow_path);
2777
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002778 // Avoid this check if we know `obj` is not null.
2779 if (instruction->MustDoNullCheck()) {
2780 __ Beqzc(obj, &done);
2781 }
2782
2783 switch (type_check_kind) {
2784 case TypeCheckKind::kExactCheck:
2785 case TypeCheckKind::kArrayCheck: {
2786 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002787 GenerateReferenceLoadTwoRegisters(instruction,
2788 temp_loc,
2789 obj_loc,
2790 class_offset,
2791 maybe_temp2_loc,
2792 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002793 // Jump to slow path for throwing the exception or doing a
2794 // more involved array check.
2795 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2796 break;
2797 }
2798
2799 case TypeCheckKind::kAbstractClassCheck: {
2800 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002801 GenerateReferenceLoadTwoRegisters(instruction,
2802 temp_loc,
2803 obj_loc,
2804 class_offset,
2805 maybe_temp2_loc,
2806 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002807 // If the class is abstract, we eagerly fetch the super class of the
2808 // object to avoid doing a comparison we know will fail.
2809 Mips64Label loop;
2810 __ Bind(&loop);
2811 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002812 GenerateReferenceLoadOneRegister(instruction,
2813 temp_loc,
2814 super_offset,
2815 maybe_temp2_loc,
2816 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002817 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2818 // exception.
2819 __ Beqzc(temp, slow_path->GetEntryLabel());
2820 // Otherwise, compare the classes.
2821 __ Bnec(temp, cls, &loop);
2822 break;
2823 }
2824
2825 case TypeCheckKind::kClassHierarchyCheck: {
2826 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002827 GenerateReferenceLoadTwoRegisters(instruction,
2828 temp_loc,
2829 obj_loc,
2830 class_offset,
2831 maybe_temp2_loc,
2832 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002833 // Walk over the class hierarchy to find a match.
2834 Mips64Label loop;
2835 __ Bind(&loop);
2836 __ Beqc(temp, cls, &done);
2837 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002838 GenerateReferenceLoadOneRegister(instruction,
2839 temp_loc,
2840 super_offset,
2841 maybe_temp2_loc,
2842 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002843 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2844 // exception. Otherwise, jump to the beginning of the loop.
2845 __ Bnezc(temp, &loop);
2846 __ Bc(slow_path->GetEntryLabel());
2847 break;
2848 }
2849
2850 case TypeCheckKind::kArrayObjectCheck: {
2851 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002852 GenerateReferenceLoadTwoRegisters(instruction,
2853 temp_loc,
2854 obj_loc,
2855 class_offset,
2856 maybe_temp2_loc,
2857 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002858 // Do an exact check.
2859 __ Beqc(temp, cls, &done);
2860 // Otherwise, we need to check that the object's class is a non-primitive array.
2861 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002862 GenerateReferenceLoadOneRegister(instruction,
2863 temp_loc,
2864 component_offset,
2865 maybe_temp2_loc,
2866 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002867 // If the component type is null, jump to the slow path to throw the exception.
2868 __ Beqzc(temp, slow_path->GetEntryLabel());
2869 // Otherwise, the object is indeed an array, further check that this component
2870 // type is not a primitive type.
2871 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2872 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2873 __ Bnezc(temp, slow_path->GetEntryLabel());
2874 break;
2875 }
2876
2877 case TypeCheckKind::kUnresolvedCheck:
2878 // We always go into the type check slow path for the unresolved check case.
2879 // We cannot directly call the CheckCast runtime entry point
2880 // without resorting to a type checking slow path here (i.e. by
2881 // calling InvokeRuntime directly), as it would require to
2882 // assign fixed registers for the inputs of this HInstanceOf
2883 // instruction (following the runtime calling convention), which
2884 // might be cluttered by the potential first read barrier
2885 // emission at the beginning of this method.
2886 __ Bc(slow_path->GetEntryLabel());
2887 break;
2888
2889 case TypeCheckKind::kInterfaceCheck: {
2890 // Avoid read barriers to improve performance of the fast path. We can not get false
2891 // positives by doing this.
2892 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002893 GenerateReferenceLoadTwoRegisters(instruction,
2894 temp_loc,
2895 obj_loc,
2896 class_offset,
2897 maybe_temp2_loc,
2898 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002899 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002900 GenerateReferenceLoadTwoRegisters(instruction,
2901 temp_loc,
2902 temp_loc,
2903 iftable_offset,
2904 maybe_temp2_loc,
2905 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002906 // Iftable is never null.
2907 __ Lw(TMP, temp, array_length_offset);
2908 // Loop through the iftable and check if any class matches.
2909 Mips64Label loop;
2910 __ Bind(&loop);
2911 __ Beqzc(TMP, slow_path->GetEntryLabel());
2912 __ Lwu(AT, temp, object_array_data_offset);
2913 __ MaybeUnpoisonHeapReference(AT);
2914 // Go to next interface.
2915 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2916 __ Addiu(TMP, TMP, -2);
2917 // Compare the classes and continue the loop if they do not match.
2918 __ Bnec(AT, cls, &loop);
2919 break;
2920 }
2921 }
2922
2923 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002924 __ Bind(slow_path->GetExitLabel());
2925}
2926
2927void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2928 LocationSummary* locations =
2929 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2930 locations->SetInAt(0, Location::RequiresRegister());
2931 if (check->HasUses()) {
2932 locations->SetOut(Location::SameAsFirstInput());
2933 }
2934}
2935
2936void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2937 // We assume the class is not null.
2938 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2939 check->GetLoadClass(),
2940 check,
2941 check->GetDexPc(),
2942 true);
2943 codegen_->AddSlowPath(slow_path);
2944 GenerateClassInitializationCheck(slow_path,
2945 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2946}
2947
2948void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2949 Primitive::Type in_type = compare->InputAt(0)->GetType();
2950
Alexey Frunze299a9392015-12-08 16:08:02 -08002951 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002952
2953 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002954 case Primitive::kPrimBoolean:
2955 case Primitive::kPrimByte:
2956 case Primitive::kPrimShort:
2957 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002958 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002959 case Primitive::kPrimLong:
2960 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002961 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2963 break;
2964
2965 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002966 case Primitive::kPrimDouble:
2967 locations->SetInAt(0, Location::RequiresFpuRegister());
2968 locations->SetInAt(1, Location::RequiresFpuRegister());
2969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002971
2972 default:
2973 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2974 }
2975}
2976
2977void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2978 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002979 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002980 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2981
2982 // 0 if: left == right
2983 // 1 if: left > right
2984 // -1 if: left < right
2985 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002986 case Primitive::kPrimBoolean:
2987 case Primitive::kPrimByte:
2988 case Primitive::kPrimShort:
2989 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002990 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002991 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002992 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002993 Location rhs_location = locations->InAt(1);
2994 bool use_imm = rhs_location.IsConstant();
2995 GpuRegister rhs = ZERO;
2996 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002997 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002998 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2999 if (value != 0) {
3000 rhs = AT;
3001 __ LoadConst64(rhs, value);
3002 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003003 } else {
3004 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3005 if (value != 0) {
3006 rhs = AT;
3007 __ LoadConst32(rhs, value);
3008 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003009 }
3010 } else {
3011 rhs = rhs_location.AsRegister<GpuRegister>();
3012 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003013 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003014 __ Slt(res, rhs, lhs);
3015 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003016 break;
3017 }
3018
Alexey Frunze299a9392015-12-08 16:08:02 -08003019 case Primitive::kPrimFloat: {
3020 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3021 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3022 Mips64Label done;
3023 __ CmpEqS(FTMP, lhs, rhs);
3024 __ LoadConst32(res, 0);
3025 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003026 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003027 __ CmpLtS(FTMP, lhs, rhs);
3028 __ LoadConst32(res, -1);
3029 __ Bc1nez(FTMP, &done);
3030 __ LoadConst32(res, 1);
3031 } else {
3032 __ CmpLtS(FTMP, rhs, lhs);
3033 __ LoadConst32(res, 1);
3034 __ Bc1nez(FTMP, &done);
3035 __ LoadConst32(res, -1);
3036 }
3037 __ Bind(&done);
3038 break;
3039 }
3040
Alexey Frunze4dda3372015-06-01 18:31:49 -07003041 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003042 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3043 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3044 Mips64Label done;
3045 __ CmpEqD(FTMP, lhs, rhs);
3046 __ LoadConst32(res, 0);
3047 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003048 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003049 __ CmpLtD(FTMP, lhs, rhs);
3050 __ LoadConst32(res, -1);
3051 __ Bc1nez(FTMP, &done);
3052 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003053 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003054 __ CmpLtD(FTMP, rhs, lhs);
3055 __ LoadConst32(res, 1);
3056 __ Bc1nez(FTMP, &done);
3057 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003058 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003059 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003060 break;
3061 }
3062
3063 default:
3064 LOG(FATAL) << "Unimplemented compare type " << in_type;
3065 }
3066}
3067
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003068void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003069 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003070 switch (instruction->InputAt(0)->GetType()) {
3071 default:
3072 case Primitive::kPrimLong:
3073 locations->SetInAt(0, Location::RequiresRegister());
3074 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3075 break;
3076
3077 case Primitive::kPrimFloat:
3078 case Primitive::kPrimDouble:
3079 locations->SetInAt(0, Location::RequiresFpuRegister());
3080 locations->SetInAt(1, Location::RequiresFpuRegister());
3081 break;
3082 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003083 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3085 }
3086}
3087
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003088void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003089 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003090 return;
3091 }
3092
Alexey Frunze299a9392015-12-08 16:08:02 -08003093 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003094 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003095 switch (type) {
3096 default:
3097 // Integer case.
3098 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3099 return;
3100 case Primitive::kPrimLong:
3101 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3102 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08003103 case Primitive::kPrimFloat:
3104 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003105 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3106 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003107 }
3108}
3109
Alexey Frunzec857c742015-09-23 15:12:39 -07003110void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3111 DCHECK(instruction->IsDiv() || instruction->IsRem());
3112 Primitive::Type type = instruction->GetResultType();
3113
3114 LocationSummary* locations = instruction->GetLocations();
3115 Location second = locations->InAt(1);
3116 DCHECK(second.IsConstant());
3117
3118 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3119 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3120 int64_t imm = Int64FromConstant(second.GetConstant());
3121 DCHECK(imm == 1 || imm == -1);
3122
3123 if (instruction->IsRem()) {
3124 __ Move(out, ZERO);
3125 } else {
3126 if (imm == -1) {
3127 if (type == Primitive::kPrimInt) {
3128 __ Subu(out, ZERO, dividend);
3129 } else {
3130 DCHECK_EQ(type, Primitive::kPrimLong);
3131 __ Dsubu(out, ZERO, dividend);
3132 }
3133 } else if (out != dividend) {
3134 __ Move(out, dividend);
3135 }
3136 }
3137}
3138
3139void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3140 DCHECK(instruction->IsDiv() || instruction->IsRem());
3141 Primitive::Type type = instruction->GetResultType();
3142
3143 LocationSummary* locations = instruction->GetLocations();
3144 Location second = locations->InAt(1);
3145 DCHECK(second.IsConstant());
3146
3147 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3148 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3149 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003150 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003151 int ctz_imm = CTZ(abs_imm);
3152
3153 if (instruction->IsDiv()) {
3154 if (type == Primitive::kPrimInt) {
3155 if (ctz_imm == 1) {
3156 // Fast path for division by +/-2, which is very common.
3157 __ Srl(TMP, dividend, 31);
3158 } else {
3159 __ Sra(TMP, dividend, 31);
3160 __ Srl(TMP, TMP, 32 - ctz_imm);
3161 }
3162 __ Addu(out, dividend, TMP);
3163 __ Sra(out, out, ctz_imm);
3164 if (imm < 0) {
3165 __ Subu(out, ZERO, out);
3166 }
3167 } else {
3168 DCHECK_EQ(type, Primitive::kPrimLong);
3169 if (ctz_imm == 1) {
3170 // Fast path for division by +/-2, which is very common.
3171 __ Dsrl32(TMP, dividend, 31);
3172 } else {
3173 __ Dsra32(TMP, dividend, 31);
3174 if (ctz_imm > 32) {
3175 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3176 } else {
3177 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3178 }
3179 }
3180 __ Daddu(out, dividend, TMP);
3181 if (ctz_imm < 32) {
3182 __ Dsra(out, out, ctz_imm);
3183 } else {
3184 __ Dsra32(out, out, ctz_imm - 32);
3185 }
3186 if (imm < 0) {
3187 __ Dsubu(out, ZERO, out);
3188 }
3189 }
3190 } else {
3191 if (type == Primitive::kPrimInt) {
3192 if (ctz_imm == 1) {
3193 // Fast path for modulo +/-2, which is very common.
3194 __ Sra(TMP, dividend, 31);
3195 __ Subu(out, dividend, TMP);
3196 __ Andi(out, out, 1);
3197 __ Addu(out, out, TMP);
3198 } else {
3199 __ Sra(TMP, dividend, 31);
3200 __ Srl(TMP, TMP, 32 - ctz_imm);
3201 __ Addu(out, dividend, TMP);
3202 if (IsUint<16>(abs_imm - 1)) {
3203 __ Andi(out, out, abs_imm - 1);
3204 } else {
3205 __ Sll(out, out, 32 - ctz_imm);
3206 __ Srl(out, out, 32 - ctz_imm);
3207 }
3208 __ Subu(out, out, TMP);
3209 }
3210 } else {
3211 DCHECK_EQ(type, Primitive::kPrimLong);
3212 if (ctz_imm == 1) {
3213 // Fast path for modulo +/-2, which is very common.
3214 __ Dsra32(TMP, dividend, 31);
3215 __ Dsubu(out, dividend, TMP);
3216 __ Andi(out, out, 1);
3217 __ Daddu(out, out, TMP);
3218 } else {
3219 __ Dsra32(TMP, dividend, 31);
3220 if (ctz_imm > 32) {
3221 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3222 } else {
3223 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3224 }
3225 __ Daddu(out, dividend, TMP);
3226 if (IsUint<16>(abs_imm - 1)) {
3227 __ Andi(out, out, abs_imm - 1);
3228 } else {
3229 if (ctz_imm > 32) {
3230 __ Dsll(out, out, 64 - ctz_imm);
3231 __ Dsrl(out, out, 64 - ctz_imm);
3232 } else {
3233 __ Dsll32(out, out, 32 - ctz_imm);
3234 __ Dsrl32(out, out, 32 - ctz_imm);
3235 }
3236 }
3237 __ Dsubu(out, out, TMP);
3238 }
3239 }
3240 }
3241}
3242
3243void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3244 DCHECK(instruction->IsDiv() || instruction->IsRem());
3245
3246 LocationSummary* locations = instruction->GetLocations();
3247 Location second = locations->InAt(1);
3248 DCHECK(second.IsConstant());
3249
3250 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3251 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3252 int64_t imm = Int64FromConstant(second.GetConstant());
3253
3254 Primitive::Type type = instruction->GetResultType();
3255 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3256
3257 int64_t magic;
3258 int shift;
3259 CalculateMagicAndShiftForDivRem(imm,
3260 (type == Primitive::kPrimLong),
3261 &magic,
3262 &shift);
3263
3264 if (type == Primitive::kPrimInt) {
3265 __ LoadConst32(TMP, magic);
3266 __ MuhR6(TMP, dividend, TMP);
3267
3268 if (imm > 0 && magic < 0) {
3269 __ Addu(TMP, TMP, dividend);
3270 } else if (imm < 0 && magic > 0) {
3271 __ Subu(TMP, TMP, dividend);
3272 }
3273
3274 if (shift != 0) {
3275 __ Sra(TMP, TMP, shift);
3276 }
3277
3278 if (instruction->IsDiv()) {
3279 __ Sra(out, TMP, 31);
3280 __ Subu(out, TMP, out);
3281 } else {
3282 __ Sra(AT, TMP, 31);
3283 __ Subu(AT, TMP, AT);
3284 __ LoadConst32(TMP, imm);
3285 __ MulR6(TMP, AT, TMP);
3286 __ Subu(out, dividend, TMP);
3287 }
3288 } else {
3289 __ LoadConst64(TMP, magic);
3290 __ Dmuh(TMP, dividend, TMP);
3291
3292 if (imm > 0 && magic < 0) {
3293 __ Daddu(TMP, TMP, dividend);
3294 } else if (imm < 0 && magic > 0) {
3295 __ Dsubu(TMP, TMP, dividend);
3296 }
3297
3298 if (shift >= 32) {
3299 __ Dsra32(TMP, TMP, shift - 32);
3300 } else if (shift > 0) {
3301 __ Dsra(TMP, TMP, shift);
3302 }
3303
3304 if (instruction->IsDiv()) {
3305 __ Dsra32(out, TMP, 31);
3306 __ Dsubu(out, TMP, out);
3307 } else {
3308 __ Dsra32(AT, TMP, 31);
3309 __ Dsubu(AT, TMP, AT);
3310 __ LoadConst64(TMP, imm);
3311 __ Dmul(TMP, AT, TMP);
3312 __ Dsubu(out, dividend, TMP);
3313 }
3314 }
3315}
3316
3317void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3318 DCHECK(instruction->IsDiv() || instruction->IsRem());
3319 Primitive::Type type = instruction->GetResultType();
3320 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3321
3322 LocationSummary* locations = instruction->GetLocations();
3323 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3324 Location second = locations->InAt(1);
3325
3326 if (second.IsConstant()) {
3327 int64_t imm = Int64FromConstant(second.GetConstant());
3328 if (imm == 0) {
3329 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3330 } else if (imm == 1 || imm == -1) {
3331 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003332 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003333 DivRemByPowerOfTwo(instruction);
3334 } else {
3335 DCHECK(imm <= -2 || imm >= 2);
3336 GenerateDivRemWithAnyConstant(instruction);
3337 }
3338 } else {
3339 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3340 GpuRegister divisor = second.AsRegister<GpuRegister>();
3341 if (instruction->IsDiv()) {
3342 if (type == Primitive::kPrimInt)
3343 __ DivR6(out, dividend, divisor);
3344 else
3345 __ Ddiv(out, dividend, divisor);
3346 } else {
3347 if (type == Primitive::kPrimInt)
3348 __ ModR6(out, dividend, divisor);
3349 else
3350 __ Dmod(out, dividend, divisor);
3351 }
3352 }
3353}
3354
Alexey Frunze4dda3372015-06-01 18:31:49 -07003355void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3356 LocationSummary* locations =
3357 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3358 switch (div->GetResultType()) {
3359 case Primitive::kPrimInt:
3360 case Primitive::kPrimLong:
3361 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003362 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3364 break;
3365
3366 case Primitive::kPrimFloat:
3367 case Primitive::kPrimDouble:
3368 locations->SetInAt(0, Location::RequiresFpuRegister());
3369 locations->SetInAt(1, Location::RequiresFpuRegister());
3370 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3371 break;
3372
3373 default:
3374 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3375 }
3376}
3377
3378void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3379 Primitive::Type type = instruction->GetType();
3380 LocationSummary* locations = instruction->GetLocations();
3381
3382 switch (type) {
3383 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003384 case Primitive::kPrimLong:
3385 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003386 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003387 case Primitive::kPrimFloat:
3388 case Primitive::kPrimDouble: {
3389 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3390 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3391 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3392 if (type == Primitive::kPrimFloat)
3393 __ DivS(dst, lhs, rhs);
3394 else
3395 __ DivD(dst, lhs, rhs);
3396 break;
3397 }
3398 default:
3399 LOG(FATAL) << "Unexpected div type " << type;
3400 }
3401}
3402
3403void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003404 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003405 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406}
3407
3408void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3409 SlowPathCodeMIPS64* slow_path =
3410 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3411 codegen_->AddSlowPath(slow_path);
3412 Location value = instruction->GetLocations()->InAt(0);
3413
3414 Primitive::Type type = instruction->GetType();
3415
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003416 if (!Primitive::IsIntegralType(type)) {
3417 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003418 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003419 }
3420
3421 if (value.IsConstant()) {
3422 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3423 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003424 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003425 } else {
3426 // A division by a non-null constant is valid. We don't need to perform
3427 // any check, so simply fall through.
3428 }
3429 } else {
3430 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3431 }
3432}
3433
3434void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3435 LocationSummary* locations =
3436 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3437 locations->SetOut(Location::ConstantLocation(constant));
3438}
3439
3440void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3441 // Will be generated at use site.
3442}
3443
3444void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3445 exit->SetLocations(nullptr);
3446}
3447
3448void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3449}
3450
3451void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3452 LocationSummary* locations =
3453 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3454 locations->SetOut(Location::ConstantLocation(constant));
3455}
3456
3457void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3458 // Will be generated at use site.
3459}
3460
David Brazdilfc6a86a2015-06-26 10:33:45 +00003461void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003462 DCHECK(!successor->IsExitBlock());
3463 HBasicBlock* block = got->GetBlock();
3464 HInstruction* previous = got->GetPrevious();
3465 HLoopInformation* info = block->GetLoopInformation();
3466
3467 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3468 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3469 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3470 return;
3471 }
3472 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3473 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3474 }
3475 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003476 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003477 }
3478}
3479
David Brazdilfc6a86a2015-06-26 10:33:45 +00003480void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3481 got->SetLocations(nullptr);
3482}
3483
3484void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3485 HandleGoto(got, got->GetSuccessor());
3486}
3487
3488void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3489 try_boundary->SetLocations(nullptr);
3490}
3491
3492void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3493 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3494 if (!successor->IsExitBlock()) {
3495 HandleGoto(try_boundary, successor);
3496 }
3497}
3498
Alexey Frunze299a9392015-12-08 16:08:02 -08003499void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3500 bool is64bit,
3501 LocationSummary* locations) {
3502 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3503 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3504 Location rhs_location = locations->InAt(1);
3505 GpuRegister rhs_reg = ZERO;
3506 int64_t rhs_imm = 0;
3507 bool use_imm = rhs_location.IsConstant();
3508 if (use_imm) {
3509 if (is64bit) {
3510 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3511 } else {
3512 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3513 }
3514 } else {
3515 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3516 }
3517 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3518
3519 switch (cond) {
3520 case kCondEQ:
3521 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003522 if (use_imm && IsInt<16>(-rhs_imm)) {
3523 if (rhs_imm == 0) {
3524 if (cond == kCondEQ) {
3525 __ Sltiu(dst, lhs, 1);
3526 } else {
3527 __ Sltu(dst, ZERO, lhs);
3528 }
3529 } else {
3530 if (is64bit) {
3531 __ Daddiu(dst, lhs, -rhs_imm);
3532 } else {
3533 __ Addiu(dst, lhs, -rhs_imm);
3534 }
3535 if (cond == kCondEQ) {
3536 __ Sltiu(dst, dst, 1);
3537 } else {
3538 __ Sltu(dst, ZERO, dst);
3539 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003540 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003541 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003542 if (use_imm && IsUint<16>(rhs_imm)) {
3543 __ Xori(dst, lhs, rhs_imm);
3544 } else {
3545 if (use_imm) {
3546 rhs_reg = TMP;
3547 __ LoadConst64(rhs_reg, rhs_imm);
3548 }
3549 __ Xor(dst, lhs, rhs_reg);
3550 }
3551 if (cond == kCondEQ) {
3552 __ Sltiu(dst, dst, 1);
3553 } else {
3554 __ Sltu(dst, ZERO, dst);
3555 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003556 }
3557 break;
3558
3559 case kCondLT:
3560 case kCondGE:
3561 if (use_imm && IsInt<16>(rhs_imm)) {
3562 __ Slti(dst, lhs, rhs_imm);
3563 } else {
3564 if (use_imm) {
3565 rhs_reg = TMP;
3566 __ LoadConst64(rhs_reg, rhs_imm);
3567 }
3568 __ Slt(dst, lhs, rhs_reg);
3569 }
3570 if (cond == kCondGE) {
3571 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3572 // only the slt instruction but no sge.
3573 __ Xori(dst, dst, 1);
3574 }
3575 break;
3576
3577 case kCondLE:
3578 case kCondGT:
3579 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3580 // Simulate lhs <= rhs via lhs < rhs + 1.
3581 __ Slti(dst, lhs, rhs_imm_plus_one);
3582 if (cond == kCondGT) {
3583 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3584 // only the slti instruction but no sgti.
3585 __ Xori(dst, dst, 1);
3586 }
3587 } else {
3588 if (use_imm) {
3589 rhs_reg = TMP;
3590 __ LoadConst64(rhs_reg, rhs_imm);
3591 }
3592 __ Slt(dst, rhs_reg, lhs);
3593 if (cond == kCondLE) {
3594 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3595 // only the slt instruction but no sle.
3596 __ Xori(dst, dst, 1);
3597 }
3598 }
3599 break;
3600
3601 case kCondB:
3602 case kCondAE:
3603 if (use_imm && IsInt<16>(rhs_imm)) {
3604 // Sltiu sign-extends its 16-bit immediate operand before
3605 // the comparison and thus lets us compare directly with
3606 // unsigned values in the ranges [0, 0x7fff] and
3607 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3608 __ Sltiu(dst, lhs, rhs_imm);
3609 } else {
3610 if (use_imm) {
3611 rhs_reg = TMP;
3612 __ LoadConst64(rhs_reg, rhs_imm);
3613 }
3614 __ Sltu(dst, lhs, rhs_reg);
3615 }
3616 if (cond == kCondAE) {
3617 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3618 // only the sltu instruction but no sgeu.
3619 __ Xori(dst, dst, 1);
3620 }
3621 break;
3622
3623 case kCondBE:
3624 case kCondA:
3625 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3626 // Simulate lhs <= rhs via lhs < rhs + 1.
3627 // Note that this only works if rhs + 1 does not overflow
3628 // to 0, hence the check above.
3629 // Sltiu sign-extends its 16-bit immediate operand before
3630 // the comparison and thus lets us compare directly with
3631 // unsigned values in the ranges [0, 0x7fff] and
3632 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3633 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3634 if (cond == kCondA) {
3635 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3636 // only the sltiu instruction but no sgtiu.
3637 __ Xori(dst, dst, 1);
3638 }
3639 } else {
3640 if (use_imm) {
3641 rhs_reg = TMP;
3642 __ LoadConst64(rhs_reg, rhs_imm);
3643 }
3644 __ Sltu(dst, rhs_reg, lhs);
3645 if (cond == kCondBE) {
3646 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3647 // only the sltu instruction but no sleu.
3648 __ Xori(dst, dst, 1);
3649 }
3650 }
3651 break;
3652 }
3653}
3654
3655void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3656 bool is64bit,
3657 LocationSummary* locations,
3658 Mips64Label* label) {
3659 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3660 Location rhs_location = locations->InAt(1);
3661 GpuRegister rhs_reg = ZERO;
3662 int64_t rhs_imm = 0;
3663 bool use_imm = rhs_location.IsConstant();
3664 if (use_imm) {
3665 if (is64bit) {
3666 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3667 } else {
3668 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3669 }
3670 } else {
3671 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3672 }
3673
3674 if (use_imm && rhs_imm == 0) {
3675 switch (cond) {
3676 case kCondEQ:
3677 case kCondBE: // <= 0 if zero
3678 __ Beqzc(lhs, label);
3679 break;
3680 case kCondNE:
3681 case kCondA: // > 0 if non-zero
3682 __ Bnezc(lhs, label);
3683 break;
3684 case kCondLT:
3685 __ Bltzc(lhs, label);
3686 break;
3687 case kCondGE:
3688 __ Bgezc(lhs, label);
3689 break;
3690 case kCondLE:
3691 __ Blezc(lhs, label);
3692 break;
3693 case kCondGT:
3694 __ Bgtzc(lhs, label);
3695 break;
3696 case kCondB: // always false
3697 break;
3698 case kCondAE: // always true
3699 __ Bc(label);
3700 break;
3701 }
3702 } else {
3703 if (use_imm) {
3704 rhs_reg = TMP;
3705 __ LoadConst64(rhs_reg, rhs_imm);
3706 }
3707 switch (cond) {
3708 case kCondEQ:
3709 __ Beqc(lhs, rhs_reg, label);
3710 break;
3711 case kCondNE:
3712 __ Bnec(lhs, rhs_reg, label);
3713 break;
3714 case kCondLT:
3715 __ Bltc(lhs, rhs_reg, label);
3716 break;
3717 case kCondGE:
3718 __ Bgec(lhs, rhs_reg, label);
3719 break;
3720 case kCondLE:
3721 __ Bgec(rhs_reg, lhs, label);
3722 break;
3723 case kCondGT:
3724 __ Bltc(rhs_reg, lhs, label);
3725 break;
3726 case kCondB:
3727 __ Bltuc(lhs, rhs_reg, label);
3728 break;
3729 case kCondAE:
3730 __ Bgeuc(lhs, rhs_reg, label);
3731 break;
3732 case kCondBE:
3733 __ Bgeuc(rhs_reg, lhs, label);
3734 break;
3735 case kCondA:
3736 __ Bltuc(rhs_reg, lhs, label);
3737 break;
3738 }
3739 }
3740}
3741
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003742void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3743 bool gt_bias,
3744 Primitive::Type type,
3745 LocationSummary* locations) {
3746 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3747 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3748 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3749 if (type == Primitive::kPrimFloat) {
3750 switch (cond) {
3751 case kCondEQ:
3752 __ CmpEqS(FTMP, lhs, rhs);
3753 __ Mfc1(dst, FTMP);
3754 __ Andi(dst, dst, 1);
3755 break;
3756 case kCondNE:
3757 __ CmpEqS(FTMP, lhs, rhs);
3758 __ Mfc1(dst, FTMP);
3759 __ Addiu(dst, dst, 1);
3760 break;
3761 case kCondLT:
3762 if (gt_bias) {
3763 __ CmpLtS(FTMP, lhs, rhs);
3764 } else {
3765 __ CmpUltS(FTMP, lhs, rhs);
3766 }
3767 __ Mfc1(dst, FTMP);
3768 __ Andi(dst, dst, 1);
3769 break;
3770 case kCondLE:
3771 if (gt_bias) {
3772 __ CmpLeS(FTMP, lhs, rhs);
3773 } else {
3774 __ CmpUleS(FTMP, lhs, rhs);
3775 }
3776 __ Mfc1(dst, FTMP);
3777 __ Andi(dst, dst, 1);
3778 break;
3779 case kCondGT:
3780 if (gt_bias) {
3781 __ CmpUltS(FTMP, rhs, lhs);
3782 } else {
3783 __ CmpLtS(FTMP, rhs, lhs);
3784 }
3785 __ Mfc1(dst, FTMP);
3786 __ Andi(dst, dst, 1);
3787 break;
3788 case kCondGE:
3789 if (gt_bias) {
3790 __ CmpUleS(FTMP, rhs, lhs);
3791 } else {
3792 __ CmpLeS(FTMP, rhs, lhs);
3793 }
3794 __ Mfc1(dst, FTMP);
3795 __ Andi(dst, dst, 1);
3796 break;
3797 default:
3798 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3799 UNREACHABLE();
3800 }
3801 } else {
3802 DCHECK_EQ(type, Primitive::kPrimDouble);
3803 switch (cond) {
3804 case kCondEQ:
3805 __ CmpEqD(FTMP, lhs, rhs);
3806 __ Mfc1(dst, FTMP);
3807 __ Andi(dst, dst, 1);
3808 break;
3809 case kCondNE:
3810 __ CmpEqD(FTMP, lhs, rhs);
3811 __ Mfc1(dst, FTMP);
3812 __ Addiu(dst, dst, 1);
3813 break;
3814 case kCondLT:
3815 if (gt_bias) {
3816 __ CmpLtD(FTMP, lhs, rhs);
3817 } else {
3818 __ CmpUltD(FTMP, lhs, rhs);
3819 }
3820 __ Mfc1(dst, FTMP);
3821 __ Andi(dst, dst, 1);
3822 break;
3823 case kCondLE:
3824 if (gt_bias) {
3825 __ CmpLeD(FTMP, lhs, rhs);
3826 } else {
3827 __ CmpUleD(FTMP, lhs, rhs);
3828 }
3829 __ Mfc1(dst, FTMP);
3830 __ Andi(dst, dst, 1);
3831 break;
3832 case kCondGT:
3833 if (gt_bias) {
3834 __ CmpUltD(FTMP, rhs, lhs);
3835 } else {
3836 __ CmpLtD(FTMP, rhs, lhs);
3837 }
3838 __ Mfc1(dst, FTMP);
3839 __ Andi(dst, dst, 1);
3840 break;
3841 case kCondGE:
3842 if (gt_bias) {
3843 __ CmpUleD(FTMP, rhs, lhs);
3844 } else {
3845 __ CmpLeD(FTMP, rhs, lhs);
3846 }
3847 __ Mfc1(dst, FTMP);
3848 __ Andi(dst, dst, 1);
3849 break;
3850 default:
3851 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3852 UNREACHABLE();
3853 }
3854 }
3855}
3856
Alexey Frunze299a9392015-12-08 16:08:02 -08003857void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3858 bool gt_bias,
3859 Primitive::Type type,
3860 LocationSummary* locations,
3861 Mips64Label* label) {
3862 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3863 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3864 if (type == Primitive::kPrimFloat) {
3865 switch (cond) {
3866 case kCondEQ:
3867 __ CmpEqS(FTMP, lhs, rhs);
3868 __ Bc1nez(FTMP, label);
3869 break;
3870 case kCondNE:
3871 __ CmpEqS(FTMP, lhs, rhs);
3872 __ Bc1eqz(FTMP, label);
3873 break;
3874 case kCondLT:
3875 if (gt_bias) {
3876 __ CmpLtS(FTMP, lhs, rhs);
3877 } else {
3878 __ CmpUltS(FTMP, lhs, rhs);
3879 }
3880 __ Bc1nez(FTMP, label);
3881 break;
3882 case kCondLE:
3883 if (gt_bias) {
3884 __ CmpLeS(FTMP, lhs, rhs);
3885 } else {
3886 __ CmpUleS(FTMP, lhs, rhs);
3887 }
3888 __ Bc1nez(FTMP, label);
3889 break;
3890 case kCondGT:
3891 if (gt_bias) {
3892 __ CmpUltS(FTMP, rhs, lhs);
3893 } else {
3894 __ CmpLtS(FTMP, rhs, lhs);
3895 }
3896 __ Bc1nez(FTMP, label);
3897 break;
3898 case kCondGE:
3899 if (gt_bias) {
3900 __ CmpUleS(FTMP, rhs, lhs);
3901 } else {
3902 __ CmpLeS(FTMP, rhs, lhs);
3903 }
3904 __ Bc1nez(FTMP, label);
3905 break;
3906 default:
3907 LOG(FATAL) << "Unexpected non-floating-point condition";
3908 }
3909 } else {
3910 DCHECK_EQ(type, Primitive::kPrimDouble);
3911 switch (cond) {
3912 case kCondEQ:
3913 __ CmpEqD(FTMP, lhs, rhs);
3914 __ Bc1nez(FTMP, label);
3915 break;
3916 case kCondNE:
3917 __ CmpEqD(FTMP, lhs, rhs);
3918 __ Bc1eqz(FTMP, label);
3919 break;
3920 case kCondLT:
3921 if (gt_bias) {
3922 __ CmpLtD(FTMP, lhs, rhs);
3923 } else {
3924 __ CmpUltD(FTMP, lhs, rhs);
3925 }
3926 __ Bc1nez(FTMP, label);
3927 break;
3928 case kCondLE:
3929 if (gt_bias) {
3930 __ CmpLeD(FTMP, lhs, rhs);
3931 } else {
3932 __ CmpUleD(FTMP, lhs, rhs);
3933 }
3934 __ Bc1nez(FTMP, label);
3935 break;
3936 case kCondGT:
3937 if (gt_bias) {
3938 __ CmpUltD(FTMP, rhs, lhs);
3939 } else {
3940 __ CmpLtD(FTMP, rhs, lhs);
3941 }
3942 __ Bc1nez(FTMP, label);
3943 break;
3944 case kCondGE:
3945 if (gt_bias) {
3946 __ CmpUleD(FTMP, rhs, lhs);
3947 } else {
3948 __ CmpLeD(FTMP, rhs, lhs);
3949 }
3950 __ Bc1nez(FTMP, label);
3951 break;
3952 default:
3953 LOG(FATAL) << "Unexpected non-floating-point condition";
3954 }
3955 }
3956}
3957
Alexey Frunze4dda3372015-06-01 18:31:49 -07003958void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003959 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003960 Mips64Label* true_target,
3961 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003962 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003963
David Brazdil0debae72015-11-12 18:37:00 +00003964 if (true_target == nullptr && false_target == nullptr) {
3965 // Nothing to do. The code always falls through.
3966 return;
3967 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003968 // Constant condition, statically compared against "true" (integer value 1).
3969 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003970 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003971 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003972 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003973 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003974 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003975 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003976 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003977 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003978 }
David Brazdil0debae72015-11-12 18:37:00 +00003979 return;
3980 }
3981
3982 // The following code generates these patterns:
3983 // (1) true_target == nullptr && false_target != nullptr
3984 // - opposite condition true => branch to false_target
3985 // (2) true_target != nullptr && false_target == nullptr
3986 // - condition true => branch to true_target
3987 // (3) true_target != nullptr && false_target != nullptr
3988 // - condition true => branch to true_target
3989 // - branch to false_target
3990 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003991 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003992 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003993 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003994 if (true_target == nullptr) {
3995 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3996 } else {
3997 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3998 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003999 } else {
4000 // The condition instruction has not been materialized, use its inputs as
4001 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004002 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004003 Primitive::Type type = condition->InputAt(0)->GetType();
4004 LocationSummary* locations = cond->GetLocations();
4005 IfCondition if_cond = condition->GetCondition();
4006 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004007
David Brazdil0debae72015-11-12 18:37:00 +00004008 if (true_target == nullptr) {
4009 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004010 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004011 }
4012
Alexey Frunze299a9392015-12-08 16:08:02 -08004013 switch (type) {
4014 default:
4015 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4016 break;
4017 case Primitive::kPrimLong:
4018 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4019 break;
4020 case Primitive::kPrimFloat:
4021 case Primitive::kPrimDouble:
4022 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4023 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004024 }
4025 }
David Brazdil0debae72015-11-12 18:37:00 +00004026
4027 // If neither branch falls through (case 3), the conditional branch to `true_target`
4028 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4029 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004030 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004031 }
4032}
4033
4034void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
4035 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004036 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004037 locations->SetInAt(0, Location::RequiresRegister());
4038 }
4039}
4040
4041void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004042 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4043 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004044 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004045 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004046 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004047 nullptr : codegen_->GetLabelOf(false_successor);
4048 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004049}
4050
4051void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
4052 LocationSummary* locations = new (GetGraph()->GetArena())
4053 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004054 InvokeRuntimeCallingConvention calling_convention;
4055 RegisterSet caller_saves = RegisterSet::Empty();
4056 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4057 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004058 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004059 locations->SetInAt(0, Location::RequiresRegister());
4060 }
4061}
4062
4063void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004064 SlowPathCodeMIPS64* slow_path =
4065 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004066 GenerateTestAndBranch(deoptimize,
4067 /* condition_input_index */ 0,
4068 slow_path->GetEntryLabel(),
4069 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004070}
4071
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004072void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4073 LocationSummary* locations = new (GetGraph()->GetArena())
4074 LocationSummary(flag, LocationSummary::kNoCall);
4075 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004076}
4077
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004078void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4079 __ LoadFromOffset(kLoadWord,
4080 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4081 SP,
4082 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004083}
4084
David Brazdil74eb1b22015-12-14 11:44:01 +00004085void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
4086 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
4087 if (Primitive::IsFloatingPointType(select->GetType())) {
4088 locations->SetInAt(0, Location::RequiresFpuRegister());
4089 locations->SetInAt(1, Location::RequiresFpuRegister());
4090 } else {
4091 locations->SetInAt(0, Location::RequiresRegister());
4092 locations->SetInAt(1, Location::RequiresRegister());
4093 }
4094 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
4095 locations->SetInAt(2, Location::RequiresRegister());
4096 }
4097 locations->SetOut(Location::SameAsFirstInput());
4098}
4099
4100void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
4101 LocationSummary* locations = select->GetLocations();
4102 Mips64Label false_target;
4103 GenerateTestAndBranch(select,
4104 /* condition_input_index */ 2,
4105 /* true_target */ nullptr,
4106 &false_target);
4107 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4108 __ Bind(&false_target);
4109}
4110
David Srbecky0cf44932015-12-09 14:09:59 +00004111void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4112 new (GetGraph()->GetArena()) LocationSummary(info);
4113}
4114
David Srbeckyd28f4a02016-03-14 17:14:24 +00004115void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4116 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004117}
4118
4119void CodeGeneratorMIPS64::GenerateNop() {
4120 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004121}
4122
Alexey Frunze4dda3372015-06-01 18:31:49 -07004123void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004124 const FieldInfo& field_info) {
4125 Primitive::Type field_type = field_info.GetFieldType();
4126 bool object_field_get_with_read_barrier =
4127 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
4128 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4129 instruction,
4130 object_field_get_with_read_barrier
4131 ? LocationSummary::kCallOnSlowPath
4132 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004133 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4134 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4135 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004136 locations->SetInAt(0, Location::RequiresRegister());
4137 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4138 locations->SetOut(Location::RequiresFpuRegister());
4139 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004140 // The output overlaps in the case of an object field get with
4141 // read barriers enabled: we do not want the move to overwrite the
4142 // object's location, as we need it to emit the read barrier.
4143 locations->SetOut(Location::RequiresRegister(),
4144 object_field_get_with_read_barrier
4145 ? Location::kOutputOverlap
4146 : Location::kNoOutputOverlap);
4147 }
4148 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4149 // We need a temporary register for the read barrier marking slow
4150 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004151 if (!kBakerReadBarrierThunksEnableForFields) {
4152 locations->AddTemp(Location::RequiresRegister());
4153 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004154 }
4155}
4156
4157void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4158 const FieldInfo& field_info) {
4159 Primitive::Type type = field_info.GetFieldType();
4160 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004161 Location obj_loc = locations->InAt(0);
4162 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4163 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004164 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004165 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004166 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004167 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4168
Alexey Frunze4dda3372015-06-01 18:31:49 -07004169 switch (type) {
4170 case Primitive::kPrimBoolean:
4171 load_type = kLoadUnsignedByte;
4172 break;
4173 case Primitive::kPrimByte:
4174 load_type = kLoadSignedByte;
4175 break;
4176 case Primitive::kPrimShort:
4177 load_type = kLoadSignedHalfword;
4178 break;
4179 case Primitive::kPrimChar:
4180 load_type = kLoadUnsignedHalfword;
4181 break;
4182 case Primitive::kPrimInt:
4183 case Primitive::kPrimFloat:
4184 load_type = kLoadWord;
4185 break;
4186 case Primitive::kPrimLong:
4187 case Primitive::kPrimDouble:
4188 load_type = kLoadDoubleword;
4189 break;
4190 case Primitive::kPrimNot:
4191 load_type = kLoadUnsignedWord;
4192 break;
4193 case Primitive::kPrimVoid:
4194 LOG(FATAL) << "Unreachable type " << type;
4195 UNREACHABLE();
4196 }
4197 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004198 DCHECK(dst_loc.IsRegister());
4199 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4200 if (type == Primitive::kPrimNot) {
4201 // /* HeapReference<Object> */ dst = *(obj + offset)
4202 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004203 Location temp_loc =
4204 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004205 // Note that a potential implicit null check is handled in this
4206 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4207 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4208 dst_loc,
4209 obj,
4210 offset,
4211 temp_loc,
4212 /* needs_null_check */ true);
4213 if (is_volatile) {
4214 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4215 }
4216 } else {
4217 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4218 if (is_volatile) {
4219 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4220 }
4221 // If read barriers are enabled, emit read barriers other than
4222 // Baker's using a slow path (and also unpoison the loaded
4223 // reference, if heap poisoning is enabled).
4224 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4225 }
4226 } else {
4227 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4228 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004229 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004230 DCHECK(dst_loc.IsFpuRegister());
4231 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004232 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004233 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004234
Alexey Frunze15958152017-02-09 19:08:30 -08004235 // Memory barriers, in the case of references, are handled in the
4236 // previous switch statement.
4237 if (is_volatile && (type != Primitive::kPrimNot)) {
4238 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004239 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004240}
4241
4242void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4243 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4244 LocationSummary* locations =
4245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4246 locations->SetInAt(0, Location::RequiresRegister());
4247 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004248 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004249 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004250 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004251 }
4252}
4253
4254void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004255 const FieldInfo& field_info,
4256 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004257 Primitive::Type type = field_info.GetFieldType();
4258 LocationSummary* locations = instruction->GetLocations();
4259 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004260 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004261 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004262 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004263 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4264 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004265 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4266
Alexey Frunze4dda3372015-06-01 18:31:49 -07004267 switch (type) {
4268 case Primitive::kPrimBoolean:
4269 case Primitive::kPrimByte:
4270 store_type = kStoreByte;
4271 break;
4272 case Primitive::kPrimShort:
4273 case Primitive::kPrimChar:
4274 store_type = kStoreHalfword;
4275 break;
4276 case Primitive::kPrimInt:
4277 case Primitive::kPrimFloat:
4278 case Primitive::kPrimNot:
4279 store_type = kStoreWord;
4280 break;
4281 case Primitive::kPrimLong:
4282 case Primitive::kPrimDouble:
4283 store_type = kStoreDoubleword;
4284 break;
4285 case Primitive::kPrimVoid:
4286 LOG(FATAL) << "Unreachable type " << type;
4287 UNREACHABLE();
4288 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004289
Alexey Frunze15958152017-02-09 19:08:30 -08004290 if (is_volatile) {
4291 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4292 }
4293
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004294 if (value_location.IsConstant()) {
4295 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4296 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4297 } else {
4298 if (!Primitive::IsFloatingPointType(type)) {
4299 DCHECK(value_location.IsRegister());
4300 GpuRegister src = value_location.AsRegister<GpuRegister>();
4301 if (kPoisonHeapReferences && needs_write_barrier) {
4302 // Note that in the case where `value` is a null reference,
4303 // we do not enter this block, as a null reference does not
4304 // need poisoning.
4305 DCHECK_EQ(type, Primitive::kPrimNot);
4306 __ PoisonHeapReference(TMP, src);
4307 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4308 } else {
4309 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4310 }
4311 } else {
4312 DCHECK(value_location.IsFpuRegister());
4313 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4314 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4315 }
4316 }
Alexey Frunze15958152017-02-09 19:08:30 -08004317
Alexey Frunzec061de12017-02-14 13:27:23 -08004318 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004319 DCHECK(value_location.IsRegister());
4320 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004321 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004322 }
Alexey Frunze15958152017-02-09 19:08:30 -08004323
4324 if (is_volatile) {
4325 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4326 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004327}
4328
4329void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4330 HandleFieldGet(instruction, instruction->GetFieldInfo());
4331}
4332
4333void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4334 HandleFieldGet(instruction, instruction->GetFieldInfo());
4335}
4336
4337void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4338 HandleFieldSet(instruction, instruction->GetFieldInfo());
4339}
4340
4341void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004342 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004343}
4344
Alexey Frunze15958152017-02-09 19:08:30 -08004345void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4346 HInstruction* instruction,
4347 Location out,
4348 uint32_t offset,
4349 Location maybe_temp,
4350 ReadBarrierOption read_barrier_option) {
4351 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4352 if (read_barrier_option == kWithReadBarrier) {
4353 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004354 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4355 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4356 }
Alexey Frunze15958152017-02-09 19:08:30 -08004357 if (kUseBakerReadBarrier) {
4358 // Load with fast path based Baker's read barrier.
4359 // /* HeapReference<Object> */ out = *(out + offset)
4360 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4361 out,
4362 out_reg,
4363 offset,
4364 maybe_temp,
4365 /* needs_null_check */ false);
4366 } else {
4367 // Load with slow path based read barrier.
4368 // Save the value of `out` into `maybe_temp` before overwriting it
4369 // in the following move operation, as we will need it for the
4370 // read barrier below.
4371 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4372 // /* HeapReference<Object> */ out = *(out + offset)
4373 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4374 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4375 }
4376 } else {
4377 // Plain load with no read barrier.
4378 // /* HeapReference<Object> */ out = *(out + offset)
4379 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4380 __ MaybeUnpoisonHeapReference(out_reg);
4381 }
4382}
4383
4384void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4385 HInstruction* instruction,
4386 Location out,
4387 Location obj,
4388 uint32_t offset,
4389 Location maybe_temp,
4390 ReadBarrierOption read_barrier_option) {
4391 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4392 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4393 if (read_barrier_option == kWithReadBarrier) {
4394 CHECK(kEmitCompilerReadBarrier);
4395 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004396 if (!kBakerReadBarrierThunksEnableForFields) {
4397 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4398 }
Alexey Frunze15958152017-02-09 19:08:30 -08004399 // Load with fast path based Baker's read barrier.
4400 // /* HeapReference<Object> */ out = *(obj + offset)
4401 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4402 out,
4403 obj_reg,
4404 offset,
4405 maybe_temp,
4406 /* needs_null_check */ false);
4407 } else {
4408 // Load with slow path based read barrier.
4409 // /* HeapReference<Object> */ out = *(obj + offset)
4410 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4411 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4412 }
4413 } else {
4414 // Plain load with no read barrier.
4415 // /* HeapReference<Object> */ out = *(obj + offset)
4416 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4417 __ MaybeUnpoisonHeapReference(out_reg);
4418 }
4419}
4420
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004421static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4422 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4423 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4424 return reg - V0;
4425 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4426 return 13 + (reg - S2);
4427 } else if (reg == S8) { // One more.
4428 return 19;
4429 }
4430 LOG(FATAL) << "Unexpected register " << reg;
4431 UNREACHABLE();
4432}
4433
4434static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4435 int num = GetBakerMarkThunkNumber(reg) +
4436 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4437 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4438}
4439
4440static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4441 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4442 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4443}
4444
4445void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4446 Location root,
4447 GpuRegister obj,
4448 uint32_t offset,
4449 ReadBarrierOption read_barrier_option,
4450 Mips64Label* label_low) {
4451 if (label_low != nullptr) {
4452 DCHECK_EQ(offset, 0x5678u);
4453 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004454 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004455 if (read_barrier_option == kWithReadBarrier) {
4456 DCHECK(kEmitCompilerReadBarrier);
4457 if (kUseBakerReadBarrier) {
4458 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4459 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004460 if (kBakerReadBarrierThunksEnableForGcRoots) {
4461 // Note that we do not actually check the value of `GetIsGcMarking()`
4462 // to decide whether to mark the loaded GC root or not. Instead, we
4463 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4464 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4465 // vice versa.
4466 //
4467 // We use thunks for the slow path. That thunk checks the reference
4468 // and jumps to the entrypoint if needed.
4469 //
4470 // temp = Thread::Current()->pReadBarrierMarkReg00
4471 // // AKA &art_quick_read_barrier_mark_introspection.
4472 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4473 // if (temp != nullptr) {
4474 // temp = &gc_root_thunk<root_reg>
4475 // root = temp(root)
4476 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004477
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004478 const int32_t entry_point_offset =
4479 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4480 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4481 int16_t offset_low = Low16Bits(offset);
4482 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
4483 // extension in lwu.
4484 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4485 GpuRegister base = short_offset ? obj : TMP;
4486 // Loading the entrypoint does not require a load acquire since it is only changed when
4487 // threads are suspended or running a checkpoint.
4488 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4489 if (!short_offset) {
4490 DCHECK(!label_low);
4491 __ Daui(base, obj, offset_high);
4492 }
4493 __ Beqz(T9, 2); // Skip jialc.
4494 if (label_low != nullptr) {
4495 DCHECK(short_offset);
4496 __ Bind(label_low);
4497 }
4498 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4499 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
4500 // in delay slot.
4501 __ Jialc(T9, thunk_disp);
4502 } else {
4503 // Note that we do not actually check the value of `GetIsGcMarking()`
4504 // to decide whether to mark the loaded GC root or not. Instead, we
4505 // load into `temp` (T9) the read barrier mark entry point corresponding
4506 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
4507 // is false, and vice versa.
4508 //
4509 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4510 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4511 // if (temp != null) {
4512 // root = temp(root)
4513 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004514
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004515 if (label_low != nullptr) {
4516 __ Bind(label_low);
4517 }
4518 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4519 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4520 static_assert(
4521 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4522 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4523 "have different sizes.");
4524 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4525 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4526 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08004527
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004528 // Slow path marking the GC root `root`.
4529 Location temp = Location::RegisterLocation(T9);
4530 SlowPathCodeMIPS64* slow_path =
4531 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4532 instruction,
4533 root,
4534 /*entrypoint*/ temp);
4535 codegen_->AddSlowPath(slow_path);
4536
4537 const int32_t entry_point_offset =
4538 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4539 // Loading the entrypoint does not require a load acquire since it is only changed when
4540 // threads are suspended or running a checkpoint.
4541 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4542 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4543 __ Bind(slow_path->GetExitLabel());
4544 }
Alexey Frunze15958152017-02-09 19:08:30 -08004545 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004546 if (label_low != nullptr) {
4547 __ Bind(label_low);
4548 }
Alexey Frunze15958152017-02-09 19:08:30 -08004549 // GC root loaded through a slow path for read barriers other
4550 // than Baker's.
4551 // /* GcRoot<mirror::Object>* */ root = obj + offset
4552 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4553 // /* mirror::Object* */ root = root->Read()
4554 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4555 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004556 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004557 if (label_low != nullptr) {
4558 __ Bind(label_low);
4559 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004560 // Plain GC root load with no read barrier.
4561 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4562 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4563 // Note that GC roots are not affected by heap poisoning, thus we
4564 // do not have to unpoison `root_reg` here.
4565 }
4566}
4567
Alexey Frunze15958152017-02-09 19:08:30 -08004568void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4569 Location ref,
4570 GpuRegister obj,
4571 uint32_t offset,
4572 Location temp,
4573 bool needs_null_check) {
4574 DCHECK(kEmitCompilerReadBarrier);
4575 DCHECK(kUseBakerReadBarrier);
4576
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004577 if (kBakerReadBarrierThunksEnableForFields) {
4578 // Note that we do not actually check the value of `GetIsGcMarking()`
4579 // to decide whether to mark the loaded reference or not. Instead, we
4580 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4581 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4582 // vice versa.
4583 //
4584 // We use thunks for the slow path. That thunk checks the reference
4585 // and jumps to the entrypoint if needed. If the holder is not gray,
4586 // it issues a load-load memory barrier and returns to the original
4587 // reference load.
4588 //
4589 // temp = Thread::Current()->pReadBarrierMarkReg00
4590 // // AKA &art_quick_read_barrier_mark_introspection.
4591 // if (temp != nullptr) {
4592 // temp = &field_array_thunk<holder_reg>
4593 // temp()
4594 // }
4595 // not_gray_return_address:
4596 // // If the offset is too large to fit into the lw instruction, we
4597 // // use an adjusted base register (TMP) here. This register
4598 // // receives bits 16 ... 31 of the offset before the thunk invocation
4599 // // and the thunk benefits from it.
4600 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
4601 // gray_return_address:
4602
4603 DCHECK(temp.IsInvalid());
4604 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4605 const int32_t entry_point_offset =
4606 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4607 // There may have or may have not been a null check if the field offset is smaller than
4608 // the page size.
4609 // There must've been a null check in case it's actually a load from an array.
4610 // We will, however, perform an explicit null check in the thunk as it's easier to
4611 // do it than not.
4612 if (instruction->IsArrayGet()) {
4613 DCHECK(!needs_null_check);
4614 }
4615 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
4616 // Loading the entrypoint does not require a load acquire since it is only changed when
4617 // threads are suspended or running a checkpoint.
4618 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4619 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4620 if (short_offset) {
4621 __ Beqzc(T9, 2); // Skip jialc.
4622 __ Nop(); // In forbidden slot.
4623 __ Jialc(T9, thunk_disp);
4624 // /* HeapReference<Object> */ ref = *(obj + offset)
4625 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
4626 } else {
4627 int16_t offset_low = Low16Bits(offset);
4628 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
4629 __ Beqz(T9, 2); // Skip jialc.
4630 __ Daui(TMP, obj, offset_high); // In delay slot.
4631 __ Jialc(T9, thunk_disp);
4632 // /* HeapReference<Object> */ ref = *(obj + offset)
4633 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
4634 }
4635 if (needs_null_check) {
4636 MaybeRecordImplicitNullCheck(instruction);
4637 }
4638 __ MaybeUnpoisonHeapReference(ref_reg);
4639 return;
4640 }
4641
Alexey Frunze15958152017-02-09 19:08:30 -08004642 // /* HeapReference<Object> */ ref = *(obj + offset)
4643 Location no_index = Location::NoLocation();
4644 ScaleFactor no_scale_factor = TIMES_1;
4645 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4646 ref,
4647 obj,
4648 offset,
4649 no_index,
4650 no_scale_factor,
4651 temp,
4652 needs_null_check);
4653}
4654
4655void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4656 Location ref,
4657 GpuRegister obj,
4658 uint32_t data_offset,
4659 Location index,
4660 Location temp,
4661 bool needs_null_check) {
4662 DCHECK(kEmitCompilerReadBarrier);
4663 DCHECK(kUseBakerReadBarrier);
4664
4665 static_assert(
4666 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4667 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004668 ScaleFactor scale_factor = TIMES_4;
4669
4670 if (kBakerReadBarrierThunksEnableForArrays) {
4671 // Note that we do not actually check the value of `GetIsGcMarking()`
4672 // to decide whether to mark the loaded reference or not. Instead, we
4673 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4674 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4675 // vice versa.
4676 //
4677 // We use thunks for the slow path. That thunk checks the reference
4678 // and jumps to the entrypoint if needed. If the holder is not gray,
4679 // it issues a load-load memory barrier and returns to the original
4680 // reference load.
4681 //
4682 // temp = Thread::Current()->pReadBarrierMarkReg00
4683 // // AKA &art_quick_read_barrier_mark_introspection.
4684 // if (temp != nullptr) {
4685 // temp = &field_array_thunk<holder_reg>
4686 // temp()
4687 // }
4688 // not_gray_return_address:
4689 // // The element address is pre-calculated in the TMP register before the
4690 // // thunk invocation and the thunk benefits from it.
4691 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
4692 // gray_return_address:
4693
4694 DCHECK(temp.IsInvalid());
4695 DCHECK(index.IsValid());
4696 const int32_t entry_point_offset =
4697 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4698 // We will not do the explicit null check in the thunk as some form of a null check
4699 // must've been done earlier.
4700 DCHECK(!needs_null_check);
4701 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
4702 // Loading the entrypoint does not require a load acquire since it is only changed when
4703 // threads are suspended or running a checkpoint.
4704 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4705 __ Beqz(T9, 2); // Skip jialc.
4706 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4707 GpuRegister index_reg = index.AsRegister<GpuRegister>();
4708 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
4709 __ Jialc(T9, thunk_disp);
4710 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
4711 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
4712 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
4713 __ MaybeUnpoisonHeapReference(ref_reg);
4714 return;
4715 }
4716
Alexey Frunze15958152017-02-09 19:08:30 -08004717 // /* HeapReference<Object> */ ref =
4718 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08004719 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4720 ref,
4721 obj,
4722 data_offset,
4723 index,
4724 scale_factor,
4725 temp,
4726 needs_null_check);
4727}
4728
4729void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4730 Location ref,
4731 GpuRegister obj,
4732 uint32_t offset,
4733 Location index,
4734 ScaleFactor scale_factor,
4735 Location temp,
4736 bool needs_null_check,
4737 bool always_update_field) {
4738 DCHECK(kEmitCompilerReadBarrier);
4739 DCHECK(kUseBakerReadBarrier);
4740
4741 // In slow path based read barriers, the read barrier call is
4742 // inserted after the original load. However, in fast path based
4743 // Baker's read barriers, we need to perform the load of
4744 // mirror::Object::monitor_ *before* the original reference load.
4745 // This load-load ordering is required by the read barrier.
4746 // The fast path/slow path (for Baker's algorithm) should look like:
4747 //
4748 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4749 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4750 // HeapReference<Object> ref = *src; // Original reference load.
4751 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4752 // if (is_gray) {
4753 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4754 // }
4755 //
4756 // Note: the original implementation in ReadBarrier::Barrier is
4757 // slightly more complex as it performs additional checks that we do
4758 // not do here for performance reasons.
4759
4760 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4761 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4762 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4763
4764 // /* int32_t */ monitor = obj->monitor_
4765 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4766 if (needs_null_check) {
4767 MaybeRecordImplicitNullCheck(instruction);
4768 }
4769 // /* LockWord */ lock_word = LockWord(monitor)
4770 static_assert(sizeof(LockWord) == sizeof(int32_t),
4771 "art::LockWord and int32_t have different sizes.");
4772
4773 __ Sync(0); // Barrier to prevent load-load reordering.
4774
4775 // The actual reference load.
4776 if (index.IsValid()) {
4777 // Load types involving an "index": ArrayGet,
4778 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4779 // intrinsics.
4780 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4781 if (index.IsConstant()) {
4782 size_t computed_offset =
4783 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4784 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4785 } else {
4786 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004787 if (scale_factor == TIMES_1) {
4788 __ Daddu(TMP, index_reg, obj);
4789 } else {
4790 __ Dlsa(TMP, index_reg, obj, scale_factor);
4791 }
Alexey Frunze15958152017-02-09 19:08:30 -08004792 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4793 }
4794 } else {
4795 // /* HeapReference<Object> */ ref = *(obj + offset)
4796 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4797 }
4798
4799 // Object* ref = ref_addr->AsMirrorPtr()
4800 __ MaybeUnpoisonHeapReference(ref_reg);
4801
4802 // Slow path marking the object `ref` when it is gray.
4803 SlowPathCodeMIPS64* slow_path;
4804 if (always_update_field) {
4805 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4806 // of the form `obj + field_offset`, where `obj` is a register and
4807 // `field_offset` is a register. Thus `offset` and `scale_factor`
4808 // above are expected to be null in this code path.
4809 DCHECK_EQ(offset, 0u);
4810 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4811 slow_path = new (GetGraph()->GetArena())
4812 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4813 ref,
4814 obj,
4815 /* field_offset */ index,
4816 temp_reg);
4817 } else {
4818 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4819 }
4820 AddSlowPath(slow_path);
4821
4822 // if (rb_state == ReadBarrier::GrayState())
4823 // ref = ReadBarrier::Mark(ref);
4824 // Given the numeric representation, it's enough to check the low bit of the
4825 // rb_state. We do that by shifting the bit into the sign bit (31) and
4826 // performing a branch on less than zero.
4827 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4828 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4829 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4830 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4831 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4832 __ Bind(slow_path->GetExitLabel());
4833}
4834
4835void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4836 Location out,
4837 Location ref,
4838 Location obj,
4839 uint32_t offset,
4840 Location index) {
4841 DCHECK(kEmitCompilerReadBarrier);
4842
4843 // Insert a slow path based read barrier *after* the reference load.
4844 //
4845 // If heap poisoning is enabled, the unpoisoning of the loaded
4846 // reference will be carried out by the runtime within the slow
4847 // path.
4848 //
4849 // Note that `ref` currently does not get unpoisoned (when heap
4850 // poisoning is enabled), which is alright as the `ref` argument is
4851 // not used by the artReadBarrierSlow entry point.
4852 //
4853 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4854 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4855 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4856 AddSlowPath(slow_path);
4857
4858 __ Bc(slow_path->GetEntryLabel());
4859 __ Bind(slow_path->GetExitLabel());
4860}
4861
4862void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4863 Location out,
4864 Location ref,
4865 Location obj,
4866 uint32_t offset,
4867 Location index) {
4868 if (kEmitCompilerReadBarrier) {
4869 // Baker's read barriers shall be handled by the fast path
4870 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4871 DCHECK(!kUseBakerReadBarrier);
4872 // If heap poisoning is enabled, unpoisoning will be taken care of
4873 // by the runtime within the slow path.
4874 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4875 } else if (kPoisonHeapReferences) {
4876 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4877 }
4878}
4879
4880void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4881 Location out,
4882 Location root) {
4883 DCHECK(kEmitCompilerReadBarrier);
4884
4885 // Insert a slow path based read barrier *after* the GC root load.
4886 //
4887 // Note that GC roots are not affected by heap poisoning, so we do
4888 // not need to do anything special for this here.
4889 SlowPathCodeMIPS64* slow_path =
4890 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4891 AddSlowPath(slow_path);
4892
4893 __ Bc(slow_path->GetEntryLabel());
4894 __ Bind(slow_path->GetExitLabel());
4895}
4896
Alexey Frunze4dda3372015-06-01 18:31:49 -07004897void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004898 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4899 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004900 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004901 switch (type_check_kind) {
4902 case TypeCheckKind::kExactCheck:
4903 case TypeCheckKind::kAbstractClassCheck:
4904 case TypeCheckKind::kClassHierarchyCheck:
4905 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004906 call_kind =
4907 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004908 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004909 break;
4910 case TypeCheckKind::kArrayCheck:
4911 case TypeCheckKind::kUnresolvedCheck:
4912 case TypeCheckKind::kInterfaceCheck:
4913 call_kind = LocationSummary::kCallOnSlowPath;
4914 break;
4915 }
4916
Alexey Frunze4dda3372015-06-01 18:31:49 -07004917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004918 if (baker_read_barrier_slow_path) {
4919 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4920 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004921 locations->SetInAt(0, Location::RequiresRegister());
4922 locations->SetInAt(1, Location::RequiresRegister());
4923 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004924 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004925 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004926 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004927}
4928
4929void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004930 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004931 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004932 Location obj_loc = locations->InAt(0);
4933 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004934 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004935 Location out_loc = locations->Out();
4936 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4937 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4938 DCHECK_LE(num_temps, 1u);
4939 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004940 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4941 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4942 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4943 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004944 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004945 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004946
4947 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004948 // Avoid this check if we know `obj` is not null.
4949 if (instruction->MustDoNullCheck()) {
4950 __ Move(out, ZERO);
4951 __ Beqzc(obj, &done);
4952 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004953
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004954 switch (type_check_kind) {
4955 case TypeCheckKind::kExactCheck: {
4956 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004957 GenerateReferenceLoadTwoRegisters(instruction,
4958 out_loc,
4959 obj_loc,
4960 class_offset,
4961 maybe_temp_loc,
4962 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004963 // Classes must be equal for the instanceof to succeed.
4964 __ Xor(out, out, cls);
4965 __ Sltiu(out, out, 1);
4966 break;
4967 }
4968
4969 case TypeCheckKind::kAbstractClassCheck: {
4970 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004971 GenerateReferenceLoadTwoRegisters(instruction,
4972 out_loc,
4973 obj_loc,
4974 class_offset,
4975 maybe_temp_loc,
4976 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004977 // If the class is abstract, we eagerly fetch the super class of the
4978 // object to avoid doing a comparison we know will fail.
4979 Mips64Label loop;
4980 __ Bind(&loop);
4981 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004982 GenerateReferenceLoadOneRegister(instruction,
4983 out_loc,
4984 super_offset,
4985 maybe_temp_loc,
4986 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004987 // If `out` is null, we use it for the result, and jump to `done`.
4988 __ Beqzc(out, &done);
4989 __ Bnec(out, cls, &loop);
4990 __ LoadConst32(out, 1);
4991 break;
4992 }
4993
4994 case TypeCheckKind::kClassHierarchyCheck: {
4995 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004996 GenerateReferenceLoadTwoRegisters(instruction,
4997 out_loc,
4998 obj_loc,
4999 class_offset,
5000 maybe_temp_loc,
5001 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005002 // Walk over the class hierarchy to find a match.
5003 Mips64Label loop, success;
5004 __ Bind(&loop);
5005 __ Beqc(out, cls, &success);
5006 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005007 GenerateReferenceLoadOneRegister(instruction,
5008 out_loc,
5009 super_offset,
5010 maybe_temp_loc,
5011 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005012 __ Bnezc(out, &loop);
5013 // If `out` is null, we use it for the result, and jump to `done`.
5014 __ Bc(&done);
5015 __ Bind(&success);
5016 __ LoadConst32(out, 1);
5017 break;
5018 }
5019
5020 case TypeCheckKind::kArrayObjectCheck: {
5021 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005022 GenerateReferenceLoadTwoRegisters(instruction,
5023 out_loc,
5024 obj_loc,
5025 class_offset,
5026 maybe_temp_loc,
5027 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005028 // Do an exact check.
5029 Mips64Label success;
5030 __ Beqc(out, cls, &success);
5031 // Otherwise, we need to check that the object's class is a non-primitive array.
5032 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005033 GenerateReferenceLoadOneRegister(instruction,
5034 out_loc,
5035 component_offset,
5036 maybe_temp_loc,
5037 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005038 // If `out` is null, we use it for the result, and jump to `done`.
5039 __ Beqzc(out, &done);
5040 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5041 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5042 __ Sltiu(out, out, 1);
5043 __ Bc(&done);
5044 __ Bind(&success);
5045 __ LoadConst32(out, 1);
5046 break;
5047 }
5048
5049 case TypeCheckKind::kArrayCheck: {
5050 // No read barrier since the slow path will retry upon failure.
5051 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005052 GenerateReferenceLoadTwoRegisters(instruction,
5053 out_loc,
5054 obj_loc,
5055 class_offset,
5056 maybe_temp_loc,
5057 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005058 DCHECK(locations->OnlyCallsOnSlowPath());
5059 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5060 /* is_fatal */ false);
5061 codegen_->AddSlowPath(slow_path);
5062 __ Bnec(out, cls, slow_path->GetEntryLabel());
5063 __ LoadConst32(out, 1);
5064 break;
5065 }
5066
5067 case TypeCheckKind::kUnresolvedCheck:
5068 case TypeCheckKind::kInterfaceCheck: {
5069 // Note that we indeed only call on slow path, but we always go
5070 // into the slow path for the unresolved and interface check
5071 // cases.
5072 //
5073 // We cannot directly call the InstanceofNonTrivial runtime
5074 // entry point without resorting to a type checking slow path
5075 // here (i.e. by calling InvokeRuntime directly), as it would
5076 // require to assign fixed registers for the inputs of this
5077 // HInstanceOf instruction (following the runtime calling
5078 // convention), which might be cluttered by the potential first
5079 // read barrier emission at the beginning of this method.
5080 //
5081 // TODO: Introduce a new runtime entry point taking the object
5082 // to test (instead of its class) as argument, and let it deal
5083 // with the read barrier issues. This will let us refactor this
5084 // case of the `switch` code as it was previously (with a direct
5085 // call to the runtime not using a type checking slow path).
5086 // This should also be beneficial for the other cases above.
5087 DCHECK(locations->OnlyCallsOnSlowPath());
5088 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5089 /* is_fatal */ false);
5090 codegen_->AddSlowPath(slow_path);
5091 __ Bc(slow_path->GetEntryLabel());
5092 break;
5093 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005094 }
5095
5096 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005097
5098 if (slow_path != nullptr) {
5099 __ Bind(slow_path->GetExitLabel());
5100 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005101}
5102
5103void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
5104 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5105 locations->SetOut(Location::ConstantLocation(constant));
5106}
5107
5108void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5109 // Will be generated at use site.
5110}
5111
5112void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
5113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5114 locations->SetOut(Location::ConstantLocation(constant));
5115}
5116
5117void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5118 // Will be generated at use site.
5119}
5120
Calin Juravle175dc732015-08-25 15:42:32 +01005121void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5122 // The trampoline uses the same calling convention as dex calling conventions,
5123 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5124 // the method_idx.
5125 HandleInvoke(invoke);
5126}
5127
5128void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5129 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5130}
5131
Alexey Frunze4dda3372015-06-01 18:31:49 -07005132void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5133 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5134 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5135}
5136
5137void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5138 HandleInvoke(invoke);
5139 // The register T0 is required to be used for the hidden argument in
5140 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5141 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5142}
5143
5144void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5145 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5146 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005147 Location receiver = invoke->GetLocations()->InAt(0);
5148 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005149 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005150
5151 // Set the hidden argument.
5152 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5153 invoke->GetDexMethodIndex());
5154
5155 // temp = object->GetClass();
5156 if (receiver.IsStackSlot()) {
5157 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5158 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5159 } else {
5160 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5161 }
5162 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005163 // Instead of simply (possibly) unpoisoning `temp` here, we should
5164 // emit a read barrier for the previous class reference load.
5165 // However this is not required in practice, as this is an
5166 // intermediate/temporary reference and because the current
5167 // concurrent copying collector keeps the from-space memory
5168 // intact/accessible until the end of the marking phase (the
5169 // concurrent copying collector may not in the future).
5170 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005171 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5172 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5173 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005174 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005175 // temp = temp->GetImtEntryAt(method_offset);
5176 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5177 // T9 = temp->GetEntryPoint();
5178 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5179 // T9();
5180 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005181 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005182 DCHECK(!codegen_->IsLeafMethod());
5183 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5184}
5185
5186void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005187 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5188 if (intrinsic.TryDispatch(invoke)) {
5189 return;
5190 }
5191
Alexey Frunze4dda3372015-06-01 18:31:49 -07005192 HandleInvoke(invoke);
5193}
5194
5195void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005196 // Explicit clinit checks triggered by static invokes must have been pruned by
5197 // art::PrepareForRegisterAllocation.
5198 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005199
Chris Larsen3039e382015-08-26 07:54:08 -07005200 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5201 if (intrinsic.TryDispatch(invoke)) {
5202 return;
5203 }
5204
Alexey Frunze4dda3372015-06-01 18:31:49 -07005205 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005206}
5207
Orion Hodsonac141392017-01-13 11:53:47 +00005208void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5209 HandleInvoke(invoke);
5210}
5211
5212void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5213 codegen_->GenerateInvokePolymorphicCall(invoke);
5214}
5215
Chris Larsen3039e382015-08-26 07:54:08 -07005216static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005217 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005218 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5219 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005220 return true;
5221 }
5222 return false;
5223}
5224
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005225HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005226 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005227 bool fallback_load = false;
5228 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005229 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005230 case HLoadString::LoadKind::kBssEntry:
5231 DCHECK(!Runtime::Current()->UseJitCompilation());
5232 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005233 case HLoadString::LoadKind::kJitTableAddress:
5234 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005235 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005236 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005237 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005238 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005239 }
5240 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005241 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005242 }
5243 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005244}
5245
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005246HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5247 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005248 bool fallback_load = false;
5249 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005250 case HLoadClass::LoadKind::kInvalid:
5251 LOG(FATAL) << "UNREACHABLE";
5252 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005253 case HLoadClass::LoadKind::kReferrersClass:
5254 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005255 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005256 case HLoadClass::LoadKind::kBssEntry:
5257 DCHECK(!Runtime::Current()->UseJitCompilation());
5258 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005259 case HLoadClass::LoadKind::kJitTableAddress:
5260 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005261 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005262 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005263 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005264 break;
5265 }
5266 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005267 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005268 }
5269 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005270}
5271
Vladimir Markodc151b22015-10-15 18:02:30 +01005272HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5273 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005274 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005275 // On MIPS64 we support all dispatch types.
5276 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005277}
5278
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005279void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5280 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005281 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005282 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005283 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5284 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5285
Alexey Frunze19f6c692016-11-30 19:19:55 -08005286 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005287 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005288 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005289 uint32_t offset =
5290 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005291 __ LoadFromOffset(kLoadDoubleword,
5292 temp.AsRegister<GpuRegister>(),
5293 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005294 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005295 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005296 }
Vladimir Marko58155012015-08-19 12:49:41 +00005297 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005298 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005299 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005300 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5301 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005302 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005303 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005304 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5305 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5306 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005307 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5308 break;
5309 }
Vladimir Marko58155012015-08-19 12:49:41 +00005310 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005311 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5312 kLoadDoubleword,
5313 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005314 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005315 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005316 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005317 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005318 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5319 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5320 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005321 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5322 break;
5323 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005324 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5325 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5326 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005327 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005328 }
5329
Alexey Frunze19f6c692016-11-30 19:19:55 -08005330 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005331 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005332 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005333 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005334 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5335 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5336 __ LoadFromOffset(kLoadDoubleword,
5337 T9,
5338 callee_method.AsRegister<GpuRegister>(),
5339 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005340 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005341 // T9()
5342 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005343 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005344 break;
5345 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005346 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5347
Alexey Frunze4dda3372015-06-01 18:31:49 -07005348 DCHECK(!IsLeafMethod());
5349}
5350
5351void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005352 // Explicit clinit checks triggered by static invokes must have been pruned by
5353 // art::PrepareForRegisterAllocation.
5354 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005355
5356 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5357 return;
5358 }
5359
5360 LocationSummary* locations = invoke->GetLocations();
5361 codegen_->GenerateStaticOrDirectCall(invoke,
5362 locations->HasTemps()
5363 ? locations->GetTemp(0)
5364 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005365}
5366
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005367void CodeGeneratorMIPS64::GenerateVirtualCall(
5368 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005369 // Use the calling convention instead of the location of the receiver, as
5370 // intrinsics may have put the receiver in a different register. In the intrinsics
5371 // slow path, the arguments have been moved to the right place, so here we are
5372 // guaranteed that the receiver is the first register of the calling convention.
5373 InvokeDexCallingConvention calling_convention;
5374 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5375
Alexey Frunze53afca12015-11-05 16:34:23 -08005376 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005377 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5378 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5379 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005380 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005381
5382 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005383 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005384 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005385 // Instead of simply (possibly) unpoisoning `temp` here, we should
5386 // emit a read barrier for the previous class reference load.
5387 // However this is not required in practice, as this is an
5388 // intermediate/temporary reference and because the current
5389 // concurrent copying collector keeps the from-space memory
5390 // intact/accessible until the end of the marking phase (the
5391 // concurrent copying collector may not in the future).
5392 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005393 // temp = temp->GetMethodAt(method_offset);
5394 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5395 // T9 = temp->GetEntryPoint();
5396 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5397 // T9();
5398 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005399 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005400 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005401}
5402
5403void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5404 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5405 return;
5406 }
5407
5408 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005409 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005410}
5411
5412void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005413 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005414 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005415 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005416 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5417 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005418 return;
5419 }
Vladimir Marko41559982017-01-06 14:04:23 +00005420 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005421
Alexey Frunze15958152017-02-09 19:08:30 -08005422 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5423 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005424 ? LocationSummary::kCallOnSlowPath
5425 : LocationSummary::kNoCall;
5426 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005427 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5428 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5429 }
Vladimir Marko41559982017-01-06 14:04:23 +00005430 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005431 locations->SetInAt(0, Location::RequiresRegister());
5432 }
5433 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005434 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5435 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5436 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005437 // Request a temp to hold the BSS entry location for the slow path.
5438 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005439 RegisterSet caller_saves = RegisterSet::Empty();
5440 InvokeRuntimeCallingConvention calling_convention;
5441 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5442 locations->SetCustomSlowPathCallerSaves(caller_saves);
5443 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005444 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005445 }
5446 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005447}
5448
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005449// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5450// move.
5451void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005452 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005453 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005454 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005455 return;
5456 }
Vladimir Marko41559982017-01-06 14:04:23 +00005457 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005458
Vladimir Marko41559982017-01-06 14:04:23 +00005459 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005460 Location out_loc = locations->Out();
5461 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5462 GpuRegister current_method_reg = ZERO;
5463 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005464 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005465 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5466 }
5467
Alexey Frunze15958152017-02-09 19:08:30 -08005468 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5469 ? kWithoutReadBarrier
5470 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005471 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005472 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005473 switch (load_kind) {
5474 case HLoadClass::LoadKind::kReferrersClass:
5475 DCHECK(!cls->CanCallRuntime());
5476 DCHECK(!cls->MustGenerateClinitCheck());
5477 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5478 GenerateGcRootFieldLoad(cls,
5479 out_loc,
5480 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005481 ArtMethod::DeclaringClassOffset().Int32Value(),
5482 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005483 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005484 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005485 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005486 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005487 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08005488 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005489 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5490 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
5491 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005492 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5493 break;
5494 }
5495 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005496 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005497 uint32_t address = dchecked_integral_cast<uint32_t>(
5498 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5499 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005500 __ LoadLiteral(out,
5501 kLoadUnsignedWord,
5502 codegen_->DeduplicateBootImageAddressLiteral(address));
5503 break;
5504 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005505 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005506 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
5507 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5508 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
5509 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
5510 GpuRegister temp = non_baker_read_barrier
5511 ? out
5512 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005513 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
5514 GenerateGcRootFieldLoad(cls,
5515 out_loc,
5516 temp,
5517 /* placeholder */ 0x5678,
5518 read_barrier_option,
5519 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005520 generate_null_check = true;
5521 break;
5522 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005523 case HLoadClass::LoadKind::kJitTableAddress:
5524 __ LoadLiteral(out,
5525 kLoadUnsignedWord,
5526 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5527 cls->GetTypeIndex(),
5528 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005529 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005530 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005531 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005532 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005533 LOG(FATAL) << "UNREACHABLE";
5534 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005535 }
5536
5537 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5538 DCHECK(cls->CanCallRuntime());
5539 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005540 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005541 codegen_->AddSlowPath(slow_path);
5542 if (generate_null_check) {
5543 __ Beqzc(out, slow_path->GetEntryLabel());
5544 }
5545 if (cls->MustGenerateClinitCheck()) {
5546 GenerateClassInitializationCheck(slow_path, out);
5547 } else {
5548 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005549 }
5550 }
5551}
5552
David Brazdilcb1c0552015-08-04 16:22:25 +01005553static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005554 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005555}
5556
Alexey Frunze4dda3372015-06-01 18:31:49 -07005557void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5558 LocationSummary* locations =
5559 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5560 locations->SetOut(Location::RequiresRegister());
5561}
5562
5563void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5564 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005565 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5566}
5567
5568void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5569 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5570}
5571
5572void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5573 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005574}
5575
Alexey Frunze4dda3372015-06-01 18:31:49 -07005576void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005577 HLoadString::LoadKind load_kind = load->GetLoadKind();
5578 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005580 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005581 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005582 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005583 } else {
5584 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005585 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5586 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5587 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005588 // Request a temp to hold the BSS entry location for the slow path.
5589 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005590 RegisterSet caller_saves = RegisterSet::Empty();
5591 InvokeRuntimeCallingConvention calling_convention;
5592 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5593 locations->SetCustomSlowPathCallerSaves(caller_saves);
5594 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005595 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005596 }
5597 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005598 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005599}
5600
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005601// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5602// move.
5603void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005604 HLoadString::LoadKind load_kind = load->GetLoadKind();
5605 LocationSummary* locations = load->GetLocations();
5606 Location out_loc = locations->Out();
5607 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5608
5609 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005610 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5611 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005612 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005613 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005614 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5615 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
5616 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005617 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5618 return; // No dex cache slow path.
5619 }
5620 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005621 uint32_t address = dchecked_integral_cast<uint32_t>(
5622 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5623 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005624 __ LoadLiteral(out,
5625 kLoadUnsignedWord,
5626 codegen_->DeduplicateBootImageAddressLiteral(address));
5627 return; // No dex cache slow path.
5628 }
5629 case HLoadString::LoadKind::kBssEntry: {
5630 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005631 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005632 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005633 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5634 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
5635 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
5636 GpuRegister temp = non_baker_read_barrier
5637 ? out
5638 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005639 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08005640 GenerateGcRootFieldLoad(load,
5641 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005642 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08005643 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005644 kCompilerReadBarrierOption,
5645 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005646 SlowPathCodeMIPS64* slow_path =
5647 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005648 codegen_->AddSlowPath(slow_path);
5649 __ Beqzc(out, slow_path->GetEntryLabel());
5650 __ Bind(slow_path->GetExitLabel());
5651 return;
5652 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005653 case HLoadString::LoadKind::kJitTableAddress:
5654 __ LoadLiteral(out,
5655 kLoadUnsignedWord,
5656 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5657 load->GetStringIndex(),
5658 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005659 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005660 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005661 default:
5662 break;
5663 }
5664
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005665 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005666 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005667 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005668 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005669 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5670 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5671 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005672}
5673
Alexey Frunze4dda3372015-06-01 18:31:49 -07005674void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5675 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5676 locations->SetOut(Location::ConstantLocation(constant));
5677}
5678
5679void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5680 // Will be generated at use site.
5681}
5682
5683void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5684 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005685 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005686 InvokeRuntimeCallingConvention calling_convention;
5687 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5688}
5689
5690void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005691 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005692 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005693 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005694 if (instruction->IsEnter()) {
5695 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5696 } else {
5697 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5698 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005699}
5700
5701void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5702 LocationSummary* locations =
5703 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5704 switch (mul->GetResultType()) {
5705 case Primitive::kPrimInt:
5706 case Primitive::kPrimLong:
5707 locations->SetInAt(0, Location::RequiresRegister());
5708 locations->SetInAt(1, Location::RequiresRegister());
5709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5710 break;
5711
5712 case Primitive::kPrimFloat:
5713 case Primitive::kPrimDouble:
5714 locations->SetInAt(0, Location::RequiresFpuRegister());
5715 locations->SetInAt(1, Location::RequiresFpuRegister());
5716 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5717 break;
5718
5719 default:
5720 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5721 }
5722}
5723
5724void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5725 Primitive::Type type = instruction->GetType();
5726 LocationSummary* locations = instruction->GetLocations();
5727
5728 switch (type) {
5729 case Primitive::kPrimInt:
5730 case Primitive::kPrimLong: {
5731 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5732 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5733 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5734 if (type == Primitive::kPrimInt)
5735 __ MulR6(dst, lhs, rhs);
5736 else
5737 __ Dmul(dst, lhs, rhs);
5738 break;
5739 }
5740 case Primitive::kPrimFloat:
5741 case Primitive::kPrimDouble: {
5742 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5743 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5744 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5745 if (type == Primitive::kPrimFloat)
5746 __ MulS(dst, lhs, rhs);
5747 else
5748 __ MulD(dst, lhs, rhs);
5749 break;
5750 }
5751 default:
5752 LOG(FATAL) << "Unexpected mul type " << type;
5753 }
5754}
5755
5756void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5757 LocationSummary* locations =
5758 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5759 switch (neg->GetResultType()) {
5760 case Primitive::kPrimInt:
5761 case Primitive::kPrimLong:
5762 locations->SetInAt(0, Location::RequiresRegister());
5763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5764 break;
5765
5766 case Primitive::kPrimFloat:
5767 case Primitive::kPrimDouble:
5768 locations->SetInAt(0, Location::RequiresFpuRegister());
5769 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5770 break;
5771
5772 default:
5773 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5774 }
5775}
5776
5777void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5778 Primitive::Type type = instruction->GetType();
5779 LocationSummary* locations = instruction->GetLocations();
5780
5781 switch (type) {
5782 case Primitive::kPrimInt:
5783 case Primitive::kPrimLong: {
5784 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5785 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5786 if (type == Primitive::kPrimInt)
5787 __ Subu(dst, ZERO, src);
5788 else
5789 __ Dsubu(dst, ZERO, src);
5790 break;
5791 }
5792 case Primitive::kPrimFloat:
5793 case Primitive::kPrimDouble: {
5794 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5795 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5796 if (type == Primitive::kPrimFloat)
5797 __ NegS(dst, src);
5798 else
5799 __ NegD(dst, src);
5800 break;
5801 }
5802 default:
5803 LOG(FATAL) << "Unexpected neg type " << type;
5804 }
5805}
5806
5807void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5808 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005810 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005811 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005812 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5813 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005814}
5815
5816void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005817 // Note: if heap poisoning is enabled, the entry point takes care
5818 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02005819 QuickEntrypointEnum entrypoint =
5820 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5821 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005822 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02005823 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005824}
5825
5826void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5827 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005828 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005829 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005830 if (instruction->IsStringAlloc()) {
5831 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5832 } else {
5833 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005834 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005835 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5836}
5837
5838void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005839 // Note: if heap poisoning is enabled, the entry point takes care
5840 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005841 if (instruction->IsStringAlloc()) {
5842 // String is allocated through StringFactory. Call NewEmptyString entry point.
5843 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005844 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005845 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005846 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5847 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5848 __ Jalr(T9);
5849 __ Nop();
5850 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5851 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005852 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005853 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005854 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005855}
5856
5857void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5859 locations->SetInAt(0, Location::RequiresRegister());
5860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5861}
5862
5863void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5864 Primitive::Type type = instruction->GetType();
5865 LocationSummary* locations = instruction->GetLocations();
5866
5867 switch (type) {
5868 case Primitive::kPrimInt:
5869 case Primitive::kPrimLong: {
5870 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5871 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5872 __ Nor(dst, src, ZERO);
5873 break;
5874 }
5875
5876 default:
5877 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5878 }
5879}
5880
5881void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5883 locations->SetInAt(0, Location::RequiresRegister());
5884 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5885}
5886
5887void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5888 LocationSummary* locations = instruction->GetLocations();
5889 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5890 locations->InAt(0).AsRegister<GpuRegister>(),
5891 1);
5892}
5893
5894void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005895 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5896 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005897}
5898
Calin Juravle2ae48182016-03-16 14:05:09 +00005899void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5900 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005901 return;
5902 }
5903 Location obj = instruction->GetLocations()->InAt(0);
5904
5905 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005906 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005907}
5908
Calin Juravle2ae48182016-03-16 14:05:09 +00005909void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005910 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005911 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005912
5913 Location obj = instruction->GetLocations()->InAt(0);
5914
5915 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5916}
5917
5918void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005919 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005920}
5921
5922void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5923 HandleBinaryOp(instruction);
5924}
5925
5926void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5927 HandleBinaryOp(instruction);
5928}
5929
5930void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5931 LOG(FATAL) << "Unreachable";
5932}
5933
5934void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5935 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5936}
5937
5938void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5939 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5940 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5941 if (location.IsStackSlot()) {
5942 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5943 } else if (location.IsDoubleStackSlot()) {
5944 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5945 }
5946 locations->SetOut(location);
5947}
5948
5949void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5950 ATTRIBUTE_UNUSED) {
5951 // Nothing to do, the parameter is already at its location.
5952}
5953
5954void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5955 LocationSummary* locations =
5956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5957 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5958}
5959
5960void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5961 ATTRIBUTE_UNUSED) {
5962 // Nothing to do, the method is already at its location.
5963}
5964
5965void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005967 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005968 locations->SetInAt(i, Location::Any());
5969 }
5970 locations->SetOut(Location::Any());
5971}
5972
5973void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5974 LOG(FATAL) << "Unreachable";
5975}
5976
5977void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5978 Primitive::Type type = rem->GetResultType();
5979 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005980 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5981 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5983
5984 switch (type) {
5985 case Primitive::kPrimInt:
5986 case Primitive::kPrimLong:
5987 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005988 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5990 break;
5991
5992 case Primitive::kPrimFloat:
5993 case Primitive::kPrimDouble: {
5994 InvokeRuntimeCallingConvention calling_convention;
5995 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5996 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5997 locations->SetOut(calling_convention.GetReturnLocation(type));
5998 break;
5999 }
6000
6001 default:
6002 LOG(FATAL) << "Unexpected rem type " << type;
6003 }
6004}
6005
6006void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
6007 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006008
6009 switch (type) {
6010 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07006011 case Primitive::kPrimLong:
6012 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006013 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006014
6015 case Primitive::kPrimFloat:
6016 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01006017 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
6018 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006019 if (type == Primitive::kPrimFloat) {
6020 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6021 } else {
6022 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6023 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006024 break;
6025 }
6026 default:
6027 LOG(FATAL) << "Unexpected rem type " << type;
6028 }
6029}
6030
Igor Murashkind01745e2017-04-05 16:40:31 -07006031void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6032 constructor_fence->SetLocations(nullptr);
6033}
6034
6035void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6036 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6037 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6038}
6039
Alexey Frunze4dda3372015-06-01 18:31:49 -07006040void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6041 memory_barrier->SetLocations(nullptr);
6042}
6043
6044void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6045 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6046}
6047
6048void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
6049 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6050 Primitive::Type return_type = ret->InputAt(0)->GetType();
6051 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6052}
6053
6054void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6055 codegen_->GenerateFrameExit();
6056}
6057
6058void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6059 ret->SetLocations(nullptr);
6060}
6061
6062void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6063 codegen_->GenerateFrameExit();
6064}
6065
Alexey Frunze92d90602015-12-18 18:16:36 -08006066void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6067 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006068}
6069
Alexey Frunze92d90602015-12-18 18:16:36 -08006070void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6071 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006072}
6073
Alexey Frunze4dda3372015-06-01 18:31:49 -07006074void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6075 HandleShift(shl);
6076}
6077
6078void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6079 HandleShift(shl);
6080}
6081
6082void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6083 HandleShift(shr);
6084}
6085
6086void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6087 HandleShift(shr);
6088}
6089
Alexey Frunze4dda3372015-06-01 18:31:49 -07006090void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6091 HandleBinaryOp(instruction);
6092}
6093
6094void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6095 HandleBinaryOp(instruction);
6096}
6097
6098void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6099 HandleFieldGet(instruction, instruction->GetFieldInfo());
6100}
6101
6102void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6103 HandleFieldGet(instruction, instruction->GetFieldInfo());
6104}
6105
6106void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6107 HandleFieldSet(instruction, instruction->GetFieldInfo());
6108}
6109
6110void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006111 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006112}
6113
Calin Juravlee460d1d2015-09-29 04:52:17 +01006114void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6115 HUnresolvedInstanceFieldGet* instruction) {
6116 FieldAccessCallingConventionMIPS64 calling_convention;
6117 codegen_->CreateUnresolvedFieldLocationSummary(
6118 instruction, instruction->GetFieldType(), calling_convention);
6119}
6120
6121void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6122 HUnresolvedInstanceFieldGet* instruction) {
6123 FieldAccessCallingConventionMIPS64 calling_convention;
6124 codegen_->GenerateUnresolvedFieldAccess(instruction,
6125 instruction->GetFieldType(),
6126 instruction->GetFieldIndex(),
6127 instruction->GetDexPc(),
6128 calling_convention);
6129}
6130
6131void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6132 HUnresolvedInstanceFieldSet* instruction) {
6133 FieldAccessCallingConventionMIPS64 calling_convention;
6134 codegen_->CreateUnresolvedFieldLocationSummary(
6135 instruction, instruction->GetFieldType(), calling_convention);
6136}
6137
6138void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6139 HUnresolvedInstanceFieldSet* instruction) {
6140 FieldAccessCallingConventionMIPS64 calling_convention;
6141 codegen_->GenerateUnresolvedFieldAccess(instruction,
6142 instruction->GetFieldType(),
6143 instruction->GetFieldIndex(),
6144 instruction->GetDexPc(),
6145 calling_convention);
6146}
6147
6148void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6149 HUnresolvedStaticFieldGet* instruction) {
6150 FieldAccessCallingConventionMIPS64 calling_convention;
6151 codegen_->CreateUnresolvedFieldLocationSummary(
6152 instruction, instruction->GetFieldType(), calling_convention);
6153}
6154
6155void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6156 HUnresolvedStaticFieldGet* instruction) {
6157 FieldAccessCallingConventionMIPS64 calling_convention;
6158 codegen_->GenerateUnresolvedFieldAccess(instruction,
6159 instruction->GetFieldType(),
6160 instruction->GetFieldIndex(),
6161 instruction->GetDexPc(),
6162 calling_convention);
6163}
6164
6165void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6166 HUnresolvedStaticFieldSet* instruction) {
6167 FieldAccessCallingConventionMIPS64 calling_convention;
6168 codegen_->CreateUnresolvedFieldLocationSummary(
6169 instruction, instruction->GetFieldType(), calling_convention);
6170}
6171
6172void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6173 HUnresolvedStaticFieldSet* instruction) {
6174 FieldAccessCallingConventionMIPS64 calling_convention;
6175 codegen_->GenerateUnresolvedFieldAccess(instruction,
6176 instruction->GetFieldType(),
6177 instruction->GetFieldIndex(),
6178 instruction->GetDexPc(),
6179 calling_convention);
6180}
6181
Alexey Frunze4dda3372015-06-01 18:31:49 -07006182void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006183 LocationSummary* locations =
6184 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006185 // In suspend check slow path, usually there are no caller-save registers at all.
6186 // If SIMD instructions are present, however, we force spilling all live SIMD
6187 // registers in full width (since the runtime only saves/restores lower part).
6188 locations->SetCustomSlowPathCallerSaves(
6189 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006190}
6191
6192void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6193 HBasicBlock* block = instruction->GetBlock();
6194 if (block->GetLoopInformation() != nullptr) {
6195 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6196 // The back edge will generate the suspend check.
6197 return;
6198 }
6199 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6200 // The goto will generate the suspend check.
6201 return;
6202 }
6203 GenerateSuspendCheck(instruction, nullptr);
6204}
6205
Alexey Frunze4dda3372015-06-01 18:31:49 -07006206void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
6207 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006208 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006209 InvokeRuntimeCallingConvention calling_convention;
6210 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6211}
6212
6213void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006214 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006215 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6216}
6217
6218void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6219 Primitive::Type input_type = conversion->GetInputType();
6220 Primitive::Type result_type = conversion->GetResultType();
6221 DCHECK_NE(input_type, result_type);
6222
6223 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6224 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6225 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6226 }
6227
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
6229
6230 if (Primitive::IsFloatingPointType(input_type)) {
6231 locations->SetInAt(0, Location::RequiresFpuRegister());
6232 } else {
6233 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006234 }
6235
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006236 if (Primitive::IsFloatingPointType(result_type)) {
6237 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006238 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006240 }
6241}
6242
6243void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6244 LocationSummary* locations = conversion->GetLocations();
6245 Primitive::Type result_type = conversion->GetResultType();
6246 Primitive::Type input_type = conversion->GetInputType();
6247
6248 DCHECK_NE(input_type, result_type);
6249
6250 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6251 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6252 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6253
6254 switch (result_type) {
6255 case Primitive::kPrimChar:
6256 __ Andi(dst, src, 0xFFFF);
6257 break;
6258 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006259 if (input_type == Primitive::kPrimLong) {
6260 // Type conversion from long to types narrower than int is a result of code
6261 // transformations. To avoid unpredictable results for SEB and SEH, we first
6262 // need to sign-extend the low 32-bit value into bits 32 through 63.
6263 __ Sll(dst, src, 0);
6264 __ Seb(dst, dst);
6265 } else {
6266 __ Seb(dst, src);
6267 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006268 break;
6269 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006270 if (input_type == Primitive::kPrimLong) {
6271 // Type conversion from long to types narrower than int is a result of code
6272 // transformations. To avoid unpredictable results for SEB and SEH, we first
6273 // need to sign-extend the low 32-bit value into bits 32 through 63.
6274 __ Sll(dst, src, 0);
6275 __ Seh(dst, dst);
6276 } else {
6277 __ Seh(dst, src);
6278 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006279 break;
6280 case Primitive::kPrimInt:
6281 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006282 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6283 // conversions, except when the input and output registers are the same and we are not
6284 // converting longs to shorter types. In these cases, do nothing.
6285 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
6286 __ Sll(dst, src, 0);
6287 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006288 break;
6289
6290 default:
6291 LOG(FATAL) << "Unexpected type conversion from " << input_type
6292 << " to " << result_type;
6293 }
6294 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006295 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6296 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6297 if (input_type == Primitive::kPrimLong) {
6298 __ Dmtc1(src, FTMP);
6299 if (result_type == Primitive::kPrimFloat) {
6300 __ Cvtsl(dst, FTMP);
6301 } else {
6302 __ Cvtdl(dst, FTMP);
6303 }
6304 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006305 __ Mtc1(src, FTMP);
6306 if (result_type == Primitive::kPrimFloat) {
6307 __ Cvtsw(dst, FTMP);
6308 } else {
6309 __ Cvtdw(dst, FTMP);
6310 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006311 }
6312 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6313 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006314 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6315 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006316
6317 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006318 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006319 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006320 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006321 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006322 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006323 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006324 } else {
6325 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006326 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006327 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006328 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006329 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006330 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006331 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006332 } else if (Primitive::IsFloatingPointType(result_type) &&
6333 Primitive::IsFloatingPointType(input_type)) {
6334 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6335 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6336 if (result_type == Primitive::kPrimFloat) {
6337 __ Cvtsd(dst, src);
6338 } else {
6339 __ Cvtds(dst, src);
6340 }
6341 } else {
6342 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6343 << " to " << result_type;
6344 }
6345}
6346
6347void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6348 HandleShift(ushr);
6349}
6350
6351void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6352 HandleShift(ushr);
6353}
6354
6355void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6356 HandleBinaryOp(instruction);
6357}
6358
6359void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6360 HandleBinaryOp(instruction);
6361}
6362
6363void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6364 // Nothing to do, this should be removed during prepare for register allocator.
6365 LOG(FATAL) << "Unreachable";
6366}
6367
6368void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6369 // Nothing to do, this should be removed during prepare for register allocator.
6370 LOG(FATAL) << "Unreachable";
6371}
6372
6373void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006374 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006375}
6376
6377void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006378 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006379}
6380
6381void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006382 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006383}
6384
6385void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006386 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006387}
6388
6389void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006390 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006391}
6392
6393void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006394 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006395}
6396
6397void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006398 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006399}
6400
6401void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006402 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006403}
6404
6405void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006406 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006407}
6408
6409void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006410 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006411}
6412
6413void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006414 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006415}
6416
6417void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006418 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006419}
6420
Aart Bike9f37602015-10-09 11:15:55 -07006421void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006422 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006423}
6424
6425void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006426 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006427}
6428
6429void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006430 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006431}
6432
6433void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006434 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006435}
6436
6437void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006438 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006439}
6440
6441void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006442 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006443}
6444
6445void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006446 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006447}
6448
6449void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006450 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006451}
6452
Mark Mendellfe57faa2015-09-18 09:26:15 -04006453// Simple implementation of packed switch - generate cascaded compare/jumps.
6454void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6455 LocationSummary* locations =
6456 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6457 locations->SetInAt(0, Location::RequiresRegister());
6458}
6459
Alexey Frunze0960ac52016-12-20 17:24:59 -08006460void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6461 int32_t lower_bound,
6462 uint32_t num_entries,
6463 HBasicBlock* switch_block,
6464 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006465 // Create a set of compare/jumps.
6466 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006467 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006468 // Jump to default if index is negative
6469 // Note: We don't check the case that index is positive while value < lower_bound, because in
6470 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6471 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6472
Alexey Frunze0960ac52016-12-20 17:24:59 -08006473 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006474 // Jump to successors[0] if value == lower_bound.
6475 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6476 int32_t last_index = 0;
6477 for (; num_entries - last_index > 2; last_index += 2) {
6478 __ Addiu(temp_reg, temp_reg, -2);
6479 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6480 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6481 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6482 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6483 }
6484 if (num_entries - last_index == 2) {
6485 // The last missing case_value.
6486 __ Addiu(temp_reg, temp_reg, -1);
6487 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006488 }
6489
6490 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006491 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006492 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006493 }
6494}
6495
Alexey Frunze0960ac52016-12-20 17:24:59 -08006496void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6497 int32_t lower_bound,
6498 uint32_t num_entries,
6499 HBasicBlock* switch_block,
6500 HBasicBlock* default_block) {
6501 // Create a jump table.
6502 std::vector<Mips64Label*> labels(num_entries);
6503 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6504 for (uint32_t i = 0; i < num_entries; i++) {
6505 labels[i] = codegen_->GetLabelOf(successors[i]);
6506 }
6507 JumpTable* table = __ CreateJumpTable(std::move(labels));
6508
6509 // Is the value in range?
6510 __ Addiu32(TMP, value_reg, -lower_bound);
6511 __ LoadConst32(AT, num_entries);
6512 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6513
6514 // We are in the range of the table.
6515 // Load the target address from the jump table, indexing by the value.
6516 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006517 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006518 __ Lw(TMP, TMP, 0);
6519 // Compute the absolute target address by adding the table start address
6520 // (the table contains offsets to targets relative to its start).
6521 __ Daddu(TMP, TMP, AT);
6522 // And jump.
6523 __ Jr(TMP);
6524 __ Nop();
6525}
6526
6527void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6528 int32_t lower_bound = switch_instr->GetStartValue();
6529 uint32_t num_entries = switch_instr->GetNumEntries();
6530 LocationSummary* locations = switch_instr->GetLocations();
6531 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6532 HBasicBlock* switch_block = switch_instr->GetBlock();
6533 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6534
6535 if (num_entries > kPackedSwitchJumpTableThreshold) {
6536 GenTableBasedPackedSwitch(value_reg,
6537 lower_bound,
6538 num_entries,
6539 switch_block,
6540 default_block);
6541 } else {
6542 GenPackedSwitchWithCompares(value_reg,
6543 lower_bound,
6544 num_entries,
6545 switch_block,
6546 default_block);
6547 }
6548}
6549
Chris Larsenc9905a62017-03-13 17:06:18 -07006550void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6551 LocationSummary* locations =
6552 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6553 locations->SetInAt(0, Location::RequiresRegister());
6554 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006555}
6556
Chris Larsenc9905a62017-03-13 17:06:18 -07006557void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6558 LocationSummary* locations = instruction->GetLocations();
6559 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6560 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6561 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6562 __ LoadFromOffset(kLoadDoubleword,
6563 locations->Out().AsRegister<GpuRegister>(),
6564 locations->InAt(0).AsRegister<GpuRegister>(),
6565 method_offset);
6566 } else {
6567 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6568 instruction->GetIndex(), kMips64PointerSize));
6569 __ LoadFromOffset(kLoadDoubleword,
6570 locations->Out().AsRegister<GpuRegister>(),
6571 locations->InAt(0).AsRegister<GpuRegister>(),
6572 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6573 __ LoadFromOffset(kLoadDoubleword,
6574 locations->Out().AsRegister<GpuRegister>(),
6575 locations->Out().AsRegister<GpuRegister>(),
6576 method_offset);
6577 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006578}
6579
Alexey Frunze4dda3372015-06-01 18:31:49 -07006580} // namespace mips64
6581} // namespace art