blob: 1d5969486384a85daaf2391baf433c2631578ef6 [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 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07004493 Mips64Label skip_call;
4494 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004495 if (label_low != nullptr) {
4496 DCHECK(short_offset);
4497 __ Bind(label_low);
4498 }
4499 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4500 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
4501 // in delay slot.
4502 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004503 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004504 } else {
4505 // Note that we do not actually check the value of `GetIsGcMarking()`
4506 // to decide whether to mark the loaded GC root or not. Instead, we
4507 // load into `temp` (T9) the read barrier mark entry point corresponding
4508 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
4509 // is false, and vice versa.
4510 //
4511 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4512 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4513 // if (temp != null) {
4514 // root = temp(root)
4515 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004516
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004517 if (label_low != nullptr) {
4518 __ Bind(label_low);
4519 }
4520 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4521 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4522 static_assert(
4523 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4524 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4525 "have different sizes.");
4526 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4527 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4528 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08004529
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004530 // Slow path marking the GC root `root`.
4531 Location temp = Location::RegisterLocation(T9);
4532 SlowPathCodeMIPS64* slow_path =
4533 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4534 instruction,
4535 root,
4536 /*entrypoint*/ temp);
4537 codegen_->AddSlowPath(slow_path);
4538
4539 const int32_t entry_point_offset =
4540 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4541 // Loading the entrypoint does not require a load acquire since it is only changed when
4542 // threads are suspended or running a checkpoint.
4543 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4544 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4545 __ Bind(slow_path->GetExitLabel());
4546 }
Alexey Frunze15958152017-02-09 19:08:30 -08004547 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004548 if (label_low != nullptr) {
4549 __ Bind(label_low);
4550 }
Alexey Frunze15958152017-02-09 19:08:30 -08004551 // GC root loaded through a slow path for read barriers other
4552 // than Baker's.
4553 // /* GcRoot<mirror::Object>* */ root = obj + offset
4554 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4555 // /* mirror::Object* */ root = root->Read()
4556 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4557 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004558 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004559 if (label_low != nullptr) {
4560 __ Bind(label_low);
4561 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004562 // Plain GC root load with no read barrier.
4563 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4564 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4565 // Note that GC roots are not affected by heap poisoning, thus we
4566 // do not have to unpoison `root_reg` here.
4567 }
4568}
4569
Alexey Frunze15958152017-02-09 19:08:30 -08004570void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4571 Location ref,
4572 GpuRegister obj,
4573 uint32_t offset,
4574 Location temp,
4575 bool needs_null_check) {
4576 DCHECK(kEmitCompilerReadBarrier);
4577 DCHECK(kUseBakerReadBarrier);
4578
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004579 if (kBakerReadBarrierThunksEnableForFields) {
4580 // Note that we do not actually check the value of `GetIsGcMarking()`
4581 // to decide whether to mark the loaded reference or not. Instead, we
4582 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4583 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4584 // vice versa.
4585 //
4586 // We use thunks for the slow path. That thunk checks the reference
4587 // and jumps to the entrypoint if needed. If the holder is not gray,
4588 // it issues a load-load memory barrier and returns to the original
4589 // reference load.
4590 //
4591 // temp = Thread::Current()->pReadBarrierMarkReg00
4592 // // AKA &art_quick_read_barrier_mark_introspection.
4593 // if (temp != nullptr) {
4594 // temp = &field_array_thunk<holder_reg>
4595 // temp()
4596 // }
4597 // not_gray_return_address:
4598 // // If the offset is too large to fit into the lw instruction, we
4599 // // use an adjusted base register (TMP) here. This register
4600 // // receives bits 16 ... 31 of the offset before the thunk invocation
4601 // // and the thunk benefits from it.
4602 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
4603 // gray_return_address:
4604
4605 DCHECK(temp.IsInvalid());
4606 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4607 const int32_t entry_point_offset =
4608 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4609 // There may have or may have not been a null check if the field offset is smaller than
4610 // the page size.
4611 // There must've been a null check in case it's actually a load from an array.
4612 // We will, however, perform an explicit null check in the thunk as it's easier to
4613 // do it than not.
4614 if (instruction->IsArrayGet()) {
4615 DCHECK(!needs_null_check);
4616 }
4617 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
4618 // Loading the entrypoint does not require a load acquire since it is only changed when
4619 // threads are suspended or running a checkpoint.
4620 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4621 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07004622 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004623 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07004624 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004625 __ Nop(); // In forbidden slot.
4626 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004627 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004628 // /* HeapReference<Object> */ ref = *(obj + offset)
4629 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
4630 } else {
4631 int16_t offset_low = Low16Bits(offset);
4632 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07004633 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004634 __ Daui(TMP, obj, offset_high); // In delay slot.
4635 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004636 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004637 // /* HeapReference<Object> */ ref = *(obj + offset)
4638 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
4639 }
4640 if (needs_null_check) {
4641 MaybeRecordImplicitNullCheck(instruction);
4642 }
4643 __ MaybeUnpoisonHeapReference(ref_reg);
4644 return;
4645 }
4646
Alexey Frunze15958152017-02-09 19:08:30 -08004647 // /* HeapReference<Object> */ ref = *(obj + offset)
4648 Location no_index = Location::NoLocation();
4649 ScaleFactor no_scale_factor = TIMES_1;
4650 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4651 ref,
4652 obj,
4653 offset,
4654 no_index,
4655 no_scale_factor,
4656 temp,
4657 needs_null_check);
4658}
4659
4660void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4661 Location ref,
4662 GpuRegister obj,
4663 uint32_t data_offset,
4664 Location index,
4665 Location temp,
4666 bool needs_null_check) {
4667 DCHECK(kEmitCompilerReadBarrier);
4668 DCHECK(kUseBakerReadBarrier);
4669
4670 static_assert(
4671 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4672 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004673 ScaleFactor scale_factor = TIMES_4;
4674
4675 if (kBakerReadBarrierThunksEnableForArrays) {
4676 // Note that we do not actually check the value of `GetIsGcMarking()`
4677 // to decide whether to mark the loaded reference or not. Instead, we
4678 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4679 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4680 // vice versa.
4681 //
4682 // We use thunks for the slow path. That thunk checks the reference
4683 // and jumps to the entrypoint if needed. If the holder is not gray,
4684 // it issues a load-load memory barrier and returns to the original
4685 // reference load.
4686 //
4687 // temp = Thread::Current()->pReadBarrierMarkReg00
4688 // // AKA &art_quick_read_barrier_mark_introspection.
4689 // if (temp != nullptr) {
4690 // temp = &field_array_thunk<holder_reg>
4691 // temp()
4692 // }
4693 // not_gray_return_address:
4694 // // The element address is pre-calculated in the TMP register before the
4695 // // thunk invocation and the thunk benefits from it.
4696 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
4697 // gray_return_address:
4698
4699 DCHECK(temp.IsInvalid());
4700 DCHECK(index.IsValid());
4701 const int32_t entry_point_offset =
4702 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4703 // We will not do the explicit null check in the thunk as some form of a null check
4704 // must've been done earlier.
4705 DCHECK(!needs_null_check);
4706 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
4707 // Loading the entrypoint does not require a load acquire since it is only changed when
4708 // threads are suspended or running a checkpoint.
4709 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004710 Mips64Label skip_call;
4711 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004712 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4713 GpuRegister index_reg = index.AsRegister<GpuRegister>();
4714 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
4715 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004716 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004717 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
4718 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
4719 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
4720 __ MaybeUnpoisonHeapReference(ref_reg);
4721 return;
4722 }
4723
Alexey Frunze15958152017-02-09 19:08:30 -08004724 // /* HeapReference<Object> */ ref =
4725 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08004726 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4727 ref,
4728 obj,
4729 data_offset,
4730 index,
4731 scale_factor,
4732 temp,
4733 needs_null_check);
4734}
4735
4736void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4737 Location ref,
4738 GpuRegister obj,
4739 uint32_t offset,
4740 Location index,
4741 ScaleFactor scale_factor,
4742 Location temp,
4743 bool needs_null_check,
4744 bool always_update_field) {
4745 DCHECK(kEmitCompilerReadBarrier);
4746 DCHECK(kUseBakerReadBarrier);
4747
4748 // In slow path based read barriers, the read barrier call is
4749 // inserted after the original load. However, in fast path based
4750 // Baker's read barriers, we need to perform the load of
4751 // mirror::Object::monitor_ *before* the original reference load.
4752 // This load-load ordering is required by the read barrier.
4753 // The fast path/slow path (for Baker's algorithm) should look like:
4754 //
4755 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4756 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4757 // HeapReference<Object> ref = *src; // Original reference load.
4758 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4759 // if (is_gray) {
4760 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4761 // }
4762 //
4763 // Note: the original implementation in ReadBarrier::Barrier is
4764 // slightly more complex as it performs additional checks that we do
4765 // not do here for performance reasons.
4766
4767 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4768 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4769 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4770
4771 // /* int32_t */ monitor = obj->monitor_
4772 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4773 if (needs_null_check) {
4774 MaybeRecordImplicitNullCheck(instruction);
4775 }
4776 // /* LockWord */ lock_word = LockWord(monitor)
4777 static_assert(sizeof(LockWord) == sizeof(int32_t),
4778 "art::LockWord and int32_t have different sizes.");
4779
4780 __ Sync(0); // Barrier to prevent load-load reordering.
4781
4782 // The actual reference load.
4783 if (index.IsValid()) {
4784 // Load types involving an "index": ArrayGet,
4785 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4786 // intrinsics.
4787 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4788 if (index.IsConstant()) {
4789 size_t computed_offset =
4790 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4791 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4792 } else {
4793 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004794 if (scale_factor == TIMES_1) {
4795 __ Daddu(TMP, index_reg, obj);
4796 } else {
4797 __ Dlsa(TMP, index_reg, obj, scale_factor);
4798 }
Alexey Frunze15958152017-02-09 19:08:30 -08004799 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4800 }
4801 } else {
4802 // /* HeapReference<Object> */ ref = *(obj + offset)
4803 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4804 }
4805
4806 // Object* ref = ref_addr->AsMirrorPtr()
4807 __ MaybeUnpoisonHeapReference(ref_reg);
4808
4809 // Slow path marking the object `ref` when it is gray.
4810 SlowPathCodeMIPS64* slow_path;
4811 if (always_update_field) {
4812 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4813 // of the form `obj + field_offset`, where `obj` is a register and
4814 // `field_offset` is a register. Thus `offset` and `scale_factor`
4815 // above are expected to be null in this code path.
4816 DCHECK_EQ(offset, 0u);
4817 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4818 slow_path = new (GetGraph()->GetArena())
4819 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4820 ref,
4821 obj,
4822 /* field_offset */ index,
4823 temp_reg);
4824 } else {
4825 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4826 }
4827 AddSlowPath(slow_path);
4828
4829 // if (rb_state == ReadBarrier::GrayState())
4830 // ref = ReadBarrier::Mark(ref);
4831 // Given the numeric representation, it's enough to check the low bit of the
4832 // rb_state. We do that by shifting the bit into the sign bit (31) and
4833 // performing a branch on less than zero.
4834 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4835 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4836 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4837 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4838 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4839 __ Bind(slow_path->GetExitLabel());
4840}
4841
4842void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4843 Location out,
4844 Location ref,
4845 Location obj,
4846 uint32_t offset,
4847 Location index) {
4848 DCHECK(kEmitCompilerReadBarrier);
4849
4850 // Insert a slow path based read barrier *after* the reference load.
4851 //
4852 // If heap poisoning is enabled, the unpoisoning of the loaded
4853 // reference will be carried out by the runtime within the slow
4854 // path.
4855 //
4856 // Note that `ref` currently does not get unpoisoned (when heap
4857 // poisoning is enabled), which is alright as the `ref` argument is
4858 // not used by the artReadBarrierSlow entry point.
4859 //
4860 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4861 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4862 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4863 AddSlowPath(slow_path);
4864
4865 __ Bc(slow_path->GetEntryLabel());
4866 __ Bind(slow_path->GetExitLabel());
4867}
4868
4869void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4870 Location out,
4871 Location ref,
4872 Location obj,
4873 uint32_t offset,
4874 Location index) {
4875 if (kEmitCompilerReadBarrier) {
4876 // Baker's read barriers shall be handled by the fast path
4877 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4878 DCHECK(!kUseBakerReadBarrier);
4879 // If heap poisoning is enabled, unpoisoning will be taken care of
4880 // by the runtime within the slow path.
4881 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4882 } else if (kPoisonHeapReferences) {
4883 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4884 }
4885}
4886
4887void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4888 Location out,
4889 Location root) {
4890 DCHECK(kEmitCompilerReadBarrier);
4891
4892 // Insert a slow path based read barrier *after* the GC root load.
4893 //
4894 // Note that GC roots are not affected by heap poisoning, so we do
4895 // not need to do anything special for this here.
4896 SlowPathCodeMIPS64* slow_path =
4897 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4898 AddSlowPath(slow_path);
4899
4900 __ Bc(slow_path->GetEntryLabel());
4901 __ Bind(slow_path->GetExitLabel());
4902}
4903
Alexey Frunze4dda3372015-06-01 18:31:49 -07004904void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004905 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4906 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004907 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004908 switch (type_check_kind) {
4909 case TypeCheckKind::kExactCheck:
4910 case TypeCheckKind::kAbstractClassCheck:
4911 case TypeCheckKind::kClassHierarchyCheck:
4912 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004913 call_kind =
4914 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004915 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004916 break;
4917 case TypeCheckKind::kArrayCheck:
4918 case TypeCheckKind::kUnresolvedCheck:
4919 case TypeCheckKind::kInterfaceCheck:
4920 call_kind = LocationSummary::kCallOnSlowPath;
4921 break;
4922 }
4923
Alexey Frunze4dda3372015-06-01 18:31:49 -07004924 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004925 if (baker_read_barrier_slow_path) {
4926 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4927 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004928 locations->SetInAt(0, Location::RequiresRegister());
4929 locations->SetInAt(1, Location::RequiresRegister());
4930 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004931 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004932 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004933 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004934}
4935
4936void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004937 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004938 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004939 Location obj_loc = locations->InAt(0);
4940 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004941 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004942 Location out_loc = locations->Out();
4943 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4944 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4945 DCHECK_LE(num_temps, 1u);
4946 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004947 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4948 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4949 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4950 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004951 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004952 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004953
4954 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004955 // Avoid this check if we know `obj` is not null.
4956 if (instruction->MustDoNullCheck()) {
4957 __ Move(out, ZERO);
4958 __ Beqzc(obj, &done);
4959 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004960
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004961 switch (type_check_kind) {
4962 case TypeCheckKind::kExactCheck: {
4963 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004964 GenerateReferenceLoadTwoRegisters(instruction,
4965 out_loc,
4966 obj_loc,
4967 class_offset,
4968 maybe_temp_loc,
4969 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004970 // Classes must be equal for the instanceof to succeed.
4971 __ Xor(out, out, cls);
4972 __ Sltiu(out, out, 1);
4973 break;
4974 }
4975
4976 case TypeCheckKind::kAbstractClassCheck: {
4977 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004978 GenerateReferenceLoadTwoRegisters(instruction,
4979 out_loc,
4980 obj_loc,
4981 class_offset,
4982 maybe_temp_loc,
4983 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004984 // If the class is abstract, we eagerly fetch the super class of the
4985 // object to avoid doing a comparison we know will fail.
4986 Mips64Label loop;
4987 __ Bind(&loop);
4988 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004989 GenerateReferenceLoadOneRegister(instruction,
4990 out_loc,
4991 super_offset,
4992 maybe_temp_loc,
4993 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004994 // If `out` is null, we use it for the result, and jump to `done`.
4995 __ Beqzc(out, &done);
4996 __ Bnec(out, cls, &loop);
4997 __ LoadConst32(out, 1);
4998 break;
4999 }
5000
5001 case TypeCheckKind::kClassHierarchyCheck: {
5002 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005003 GenerateReferenceLoadTwoRegisters(instruction,
5004 out_loc,
5005 obj_loc,
5006 class_offset,
5007 maybe_temp_loc,
5008 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005009 // Walk over the class hierarchy to find a match.
5010 Mips64Label loop, success;
5011 __ Bind(&loop);
5012 __ Beqc(out, cls, &success);
5013 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005014 GenerateReferenceLoadOneRegister(instruction,
5015 out_loc,
5016 super_offset,
5017 maybe_temp_loc,
5018 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005019 __ Bnezc(out, &loop);
5020 // If `out` is null, we use it for the result, and jump to `done`.
5021 __ Bc(&done);
5022 __ Bind(&success);
5023 __ LoadConst32(out, 1);
5024 break;
5025 }
5026
5027 case TypeCheckKind::kArrayObjectCheck: {
5028 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005029 GenerateReferenceLoadTwoRegisters(instruction,
5030 out_loc,
5031 obj_loc,
5032 class_offset,
5033 maybe_temp_loc,
5034 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005035 // Do an exact check.
5036 Mips64Label success;
5037 __ Beqc(out, cls, &success);
5038 // Otherwise, we need to check that the object's class is a non-primitive array.
5039 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005040 GenerateReferenceLoadOneRegister(instruction,
5041 out_loc,
5042 component_offset,
5043 maybe_temp_loc,
5044 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005045 // If `out` is null, we use it for the result, and jump to `done`.
5046 __ Beqzc(out, &done);
5047 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5048 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5049 __ Sltiu(out, out, 1);
5050 __ Bc(&done);
5051 __ Bind(&success);
5052 __ LoadConst32(out, 1);
5053 break;
5054 }
5055
5056 case TypeCheckKind::kArrayCheck: {
5057 // No read barrier since the slow path will retry upon failure.
5058 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005059 GenerateReferenceLoadTwoRegisters(instruction,
5060 out_loc,
5061 obj_loc,
5062 class_offset,
5063 maybe_temp_loc,
5064 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005065 DCHECK(locations->OnlyCallsOnSlowPath());
5066 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5067 /* is_fatal */ false);
5068 codegen_->AddSlowPath(slow_path);
5069 __ Bnec(out, cls, slow_path->GetEntryLabel());
5070 __ LoadConst32(out, 1);
5071 break;
5072 }
5073
5074 case TypeCheckKind::kUnresolvedCheck:
5075 case TypeCheckKind::kInterfaceCheck: {
5076 // Note that we indeed only call on slow path, but we always go
5077 // into the slow path for the unresolved and interface check
5078 // cases.
5079 //
5080 // We cannot directly call the InstanceofNonTrivial runtime
5081 // entry point without resorting to a type checking slow path
5082 // here (i.e. by calling InvokeRuntime directly), as it would
5083 // require to assign fixed registers for the inputs of this
5084 // HInstanceOf instruction (following the runtime calling
5085 // convention), which might be cluttered by the potential first
5086 // read barrier emission at the beginning of this method.
5087 //
5088 // TODO: Introduce a new runtime entry point taking the object
5089 // to test (instead of its class) as argument, and let it deal
5090 // with the read barrier issues. This will let us refactor this
5091 // case of the `switch` code as it was previously (with a direct
5092 // call to the runtime not using a type checking slow path).
5093 // This should also be beneficial for the other cases above.
5094 DCHECK(locations->OnlyCallsOnSlowPath());
5095 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5096 /* is_fatal */ false);
5097 codegen_->AddSlowPath(slow_path);
5098 __ Bc(slow_path->GetEntryLabel());
5099 break;
5100 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005101 }
5102
5103 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005104
5105 if (slow_path != nullptr) {
5106 __ Bind(slow_path->GetExitLabel());
5107 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005108}
5109
5110void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
5111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5112 locations->SetOut(Location::ConstantLocation(constant));
5113}
5114
5115void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5116 // Will be generated at use site.
5117}
5118
5119void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
5120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5121 locations->SetOut(Location::ConstantLocation(constant));
5122}
5123
5124void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5125 // Will be generated at use site.
5126}
5127
Calin Juravle175dc732015-08-25 15:42:32 +01005128void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5129 // The trampoline uses the same calling convention as dex calling conventions,
5130 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5131 // the method_idx.
5132 HandleInvoke(invoke);
5133}
5134
5135void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5136 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5137}
5138
Alexey Frunze4dda3372015-06-01 18:31:49 -07005139void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5140 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5141 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5142}
5143
5144void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5145 HandleInvoke(invoke);
5146 // The register T0 is required to be used for the hidden argument in
5147 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5148 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5149}
5150
5151void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5152 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5153 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005154 Location receiver = invoke->GetLocations()->InAt(0);
5155 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005156 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005157
5158 // Set the hidden argument.
5159 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5160 invoke->GetDexMethodIndex());
5161
5162 // temp = object->GetClass();
5163 if (receiver.IsStackSlot()) {
5164 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5165 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5166 } else {
5167 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5168 }
5169 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005170 // Instead of simply (possibly) unpoisoning `temp` here, we should
5171 // emit a read barrier for the previous class reference load.
5172 // However this is not required in practice, as this is an
5173 // intermediate/temporary reference and because the current
5174 // concurrent copying collector keeps the from-space memory
5175 // intact/accessible until the end of the marking phase (the
5176 // concurrent copying collector may not in the future).
5177 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005178 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5179 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5180 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005181 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005182 // temp = temp->GetImtEntryAt(method_offset);
5183 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5184 // T9 = temp->GetEntryPoint();
5185 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5186 // T9();
5187 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005188 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005189 DCHECK(!codegen_->IsLeafMethod());
5190 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5191}
5192
5193void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005194 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5195 if (intrinsic.TryDispatch(invoke)) {
5196 return;
5197 }
5198
Alexey Frunze4dda3372015-06-01 18:31:49 -07005199 HandleInvoke(invoke);
5200}
5201
5202void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005203 // Explicit clinit checks triggered by static invokes must have been pruned by
5204 // art::PrepareForRegisterAllocation.
5205 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005206
Chris Larsen3039e382015-08-26 07:54:08 -07005207 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5208 if (intrinsic.TryDispatch(invoke)) {
5209 return;
5210 }
5211
Alexey Frunze4dda3372015-06-01 18:31:49 -07005212 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005213}
5214
Orion Hodsonac141392017-01-13 11:53:47 +00005215void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5216 HandleInvoke(invoke);
5217}
5218
5219void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5220 codegen_->GenerateInvokePolymorphicCall(invoke);
5221}
5222
Chris Larsen3039e382015-08-26 07:54:08 -07005223static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005224 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005225 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5226 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005227 return true;
5228 }
5229 return false;
5230}
5231
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005232HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005233 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005234 bool fallback_load = false;
5235 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005236 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005237 case HLoadString::LoadKind::kBssEntry:
5238 DCHECK(!Runtime::Current()->UseJitCompilation());
5239 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005240 case HLoadString::LoadKind::kJitTableAddress:
5241 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005242 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005243 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005244 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005245 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005246 }
5247 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005248 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005249 }
5250 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005251}
5252
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005253HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5254 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005255 bool fallback_load = false;
5256 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005257 case HLoadClass::LoadKind::kInvalid:
5258 LOG(FATAL) << "UNREACHABLE";
5259 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005260 case HLoadClass::LoadKind::kReferrersClass:
5261 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005262 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005263 case HLoadClass::LoadKind::kBssEntry:
5264 DCHECK(!Runtime::Current()->UseJitCompilation());
5265 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005266 case HLoadClass::LoadKind::kJitTableAddress:
5267 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005268 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005269 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005270 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005271 break;
5272 }
5273 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005274 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005275 }
5276 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005277}
5278
Vladimir Markodc151b22015-10-15 18:02:30 +01005279HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5280 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005281 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005282 // On MIPS64 we support all dispatch types.
5283 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005284}
5285
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005286void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5287 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005288 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005289 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005290 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5291 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5292
Alexey Frunze19f6c692016-11-30 19:19:55 -08005293 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005294 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005295 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005296 uint32_t offset =
5297 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005298 __ LoadFromOffset(kLoadDoubleword,
5299 temp.AsRegister<GpuRegister>(),
5300 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005301 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005302 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005303 }
Vladimir Marko58155012015-08-19 12:49:41 +00005304 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005305 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005306 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005307 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5308 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005309 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005310 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005311 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5312 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5313 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005314 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5315 break;
5316 }
Vladimir Marko58155012015-08-19 12:49:41 +00005317 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005318 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5319 kLoadDoubleword,
5320 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005321 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005322 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005323 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005324 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005325 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5326 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5327 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005328 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5329 break;
5330 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005331 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5332 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5333 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005334 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005335 }
5336
Alexey Frunze19f6c692016-11-30 19:19:55 -08005337 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005338 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005339 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005340 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005341 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5342 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5343 __ LoadFromOffset(kLoadDoubleword,
5344 T9,
5345 callee_method.AsRegister<GpuRegister>(),
5346 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005347 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005348 // T9()
5349 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005350 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005351 break;
5352 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005353 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5354
Alexey Frunze4dda3372015-06-01 18:31:49 -07005355 DCHECK(!IsLeafMethod());
5356}
5357
5358void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005359 // Explicit clinit checks triggered by static invokes must have been pruned by
5360 // art::PrepareForRegisterAllocation.
5361 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005362
5363 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5364 return;
5365 }
5366
5367 LocationSummary* locations = invoke->GetLocations();
5368 codegen_->GenerateStaticOrDirectCall(invoke,
5369 locations->HasTemps()
5370 ? locations->GetTemp(0)
5371 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005372}
5373
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005374void CodeGeneratorMIPS64::GenerateVirtualCall(
5375 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005376 // Use the calling convention instead of the location of the receiver, as
5377 // intrinsics may have put the receiver in a different register. In the intrinsics
5378 // slow path, the arguments have been moved to the right place, so here we are
5379 // guaranteed that the receiver is the first register of the calling convention.
5380 InvokeDexCallingConvention calling_convention;
5381 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5382
Alexey Frunze53afca12015-11-05 16:34:23 -08005383 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005384 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5385 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5386 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005387 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005388
5389 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005390 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005391 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005392 // Instead of simply (possibly) unpoisoning `temp` here, we should
5393 // emit a read barrier for the previous class reference load.
5394 // However this is not required in practice, as this is an
5395 // intermediate/temporary reference and because the current
5396 // concurrent copying collector keeps the from-space memory
5397 // intact/accessible until the end of the marking phase (the
5398 // concurrent copying collector may not in the future).
5399 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005400 // temp = temp->GetMethodAt(method_offset);
5401 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5402 // T9 = temp->GetEntryPoint();
5403 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5404 // T9();
5405 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005406 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005407 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005408}
5409
5410void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5411 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5412 return;
5413 }
5414
5415 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005416 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005417}
5418
5419void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005420 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005421 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005422 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005423 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5424 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005425 return;
5426 }
Vladimir Marko41559982017-01-06 14:04:23 +00005427 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005428
Alexey Frunze15958152017-02-09 19:08:30 -08005429 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5430 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005431 ? LocationSummary::kCallOnSlowPath
5432 : LocationSummary::kNoCall;
5433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005434 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5435 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5436 }
Vladimir Marko41559982017-01-06 14:04:23 +00005437 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005438 locations->SetInAt(0, Location::RequiresRegister());
5439 }
5440 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005441 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5442 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5443 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005444 // Request a temp to hold the BSS entry location for the slow path.
5445 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005446 RegisterSet caller_saves = RegisterSet::Empty();
5447 InvokeRuntimeCallingConvention calling_convention;
5448 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5449 locations->SetCustomSlowPathCallerSaves(caller_saves);
5450 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005451 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005452 }
5453 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005454}
5455
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005456// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5457// move.
5458void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005459 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005460 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005461 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005462 return;
5463 }
Vladimir Marko41559982017-01-06 14:04:23 +00005464 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005465
Vladimir Marko41559982017-01-06 14:04:23 +00005466 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005467 Location out_loc = locations->Out();
5468 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5469 GpuRegister current_method_reg = ZERO;
5470 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005471 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005472 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5473 }
5474
Alexey Frunze15958152017-02-09 19:08:30 -08005475 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5476 ? kWithoutReadBarrier
5477 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005478 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005479 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005480 switch (load_kind) {
5481 case HLoadClass::LoadKind::kReferrersClass:
5482 DCHECK(!cls->CanCallRuntime());
5483 DCHECK(!cls->MustGenerateClinitCheck());
5484 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5485 GenerateGcRootFieldLoad(cls,
5486 out_loc,
5487 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005488 ArtMethod::DeclaringClassOffset().Int32Value(),
5489 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005490 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005491 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005492 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005493 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005494 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08005495 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005496 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5497 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
5498 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005499 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5500 break;
5501 }
5502 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005503 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005504 uint32_t address = dchecked_integral_cast<uint32_t>(
5505 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5506 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005507 __ LoadLiteral(out,
5508 kLoadUnsignedWord,
5509 codegen_->DeduplicateBootImageAddressLiteral(address));
5510 break;
5511 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005512 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005513 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
5514 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5515 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
5516 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
5517 GpuRegister temp = non_baker_read_barrier
5518 ? out
5519 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005520 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
5521 GenerateGcRootFieldLoad(cls,
5522 out_loc,
5523 temp,
5524 /* placeholder */ 0x5678,
5525 read_barrier_option,
5526 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005527 generate_null_check = true;
5528 break;
5529 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005530 case HLoadClass::LoadKind::kJitTableAddress:
5531 __ LoadLiteral(out,
5532 kLoadUnsignedWord,
5533 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5534 cls->GetTypeIndex(),
5535 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005536 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005537 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005538 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005539 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005540 LOG(FATAL) << "UNREACHABLE";
5541 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005542 }
5543
5544 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5545 DCHECK(cls->CanCallRuntime());
5546 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005547 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005548 codegen_->AddSlowPath(slow_path);
5549 if (generate_null_check) {
5550 __ Beqzc(out, slow_path->GetEntryLabel());
5551 }
5552 if (cls->MustGenerateClinitCheck()) {
5553 GenerateClassInitializationCheck(slow_path, out);
5554 } else {
5555 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005556 }
5557 }
5558}
5559
David Brazdilcb1c0552015-08-04 16:22:25 +01005560static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005561 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005562}
5563
Alexey Frunze4dda3372015-06-01 18:31:49 -07005564void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5565 LocationSummary* locations =
5566 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5567 locations->SetOut(Location::RequiresRegister());
5568}
5569
5570void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5571 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005572 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5573}
5574
5575void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5576 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5577}
5578
5579void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5580 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005581}
5582
Alexey Frunze4dda3372015-06-01 18:31:49 -07005583void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005584 HLoadString::LoadKind load_kind = load->GetLoadKind();
5585 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005587 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005588 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005589 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005590 } else {
5591 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005592 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5593 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5594 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005595 // Request a temp to hold the BSS entry location for the slow path.
5596 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005597 RegisterSet caller_saves = RegisterSet::Empty();
5598 InvokeRuntimeCallingConvention calling_convention;
5599 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5600 locations->SetCustomSlowPathCallerSaves(caller_saves);
5601 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005602 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005603 }
5604 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005605 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005606}
5607
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005608// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5609// move.
5610void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005611 HLoadString::LoadKind load_kind = load->GetLoadKind();
5612 LocationSummary* locations = load->GetLocations();
5613 Location out_loc = locations->Out();
5614 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5615
5616 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005617 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5618 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005619 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005620 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005621 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5622 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
5623 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005624 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5625 return; // No dex cache slow path.
5626 }
5627 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005628 uint32_t address = dchecked_integral_cast<uint32_t>(
5629 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5630 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005631 __ LoadLiteral(out,
5632 kLoadUnsignedWord,
5633 codegen_->DeduplicateBootImageAddressLiteral(address));
5634 return; // No dex cache slow path.
5635 }
5636 case HLoadString::LoadKind::kBssEntry: {
5637 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005638 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005639 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005640 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5641 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
5642 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
5643 GpuRegister temp = non_baker_read_barrier
5644 ? out
5645 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005646 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08005647 GenerateGcRootFieldLoad(load,
5648 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005649 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08005650 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005651 kCompilerReadBarrierOption,
5652 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005653 SlowPathCodeMIPS64* slow_path =
5654 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005655 codegen_->AddSlowPath(slow_path);
5656 __ Beqzc(out, slow_path->GetEntryLabel());
5657 __ Bind(slow_path->GetExitLabel());
5658 return;
5659 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005660 case HLoadString::LoadKind::kJitTableAddress:
5661 __ LoadLiteral(out,
5662 kLoadUnsignedWord,
5663 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5664 load->GetStringIndex(),
5665 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005666 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005667 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005668 default:
5669 break;
5670 }
5671
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005672 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005673 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005674 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005675 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005676 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5677 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5678 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005679}
5680
Alexey Frunze4dda3372015-06-01 18:31:49 -07005681void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5683 locations->SetOut(Location::ConstantLocation(constant));
5684}
5685
5686void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5687 // Will be generated at use site.
5688}
5689
5690void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5691 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005692 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005693 InvokeRuntimeCallingConvention calling_convention;
5694 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5695}
5696
5697void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005698 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005699 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005700 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005701 if (instruction->IsEnter()) {
5702 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5703 } else {
5704 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5705 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005706}
5707
5708void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5709 LocationSummary* locations =
5710 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5711 switch (mul->GetResultType()) {
5712 case Primitive::kPrimInt:
5713 case Primitive::kPrimLong:
5714 locations->SetInAt(0, Location::RequiresRegister());
5715 locations->SetInAt(1, Location::RequiresRegister());
5716 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5717 break;
5718
5719 case Primitive::kPrimFloat:
5720 case Primitive::kPrimDouble:
5721 locations->SetInAt(0, Location::RequiresFpuRegister());
5722 locations->SetInAt(1, Location::RequiresFpuRegister());
5723 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5724 break;
5725
5726 default:
5727 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5728 }
5729}
5730
5731void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5732 Primitive::Type type = instruction->GetType();
5733 LocationSummary* locations = instruction->GetLocations();
5734
5735 switch (type) {
5736 case Primitive::kPrimInt:
5737 case Primitive::kPrimLong: {
5738 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5739 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5740 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5741 if (type == Primitive::kPrimInt)
5742 __ MulR6(dst, lhs, rhs);
5743 else
5744 __ Dmul(dst, lhs, rhs);
5745 break;
5746 }
5747 case Primitive::kPrimFloat:
5748 case Primitive::kPrimDouble: {
5749 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5750 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5751 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5752 if (type == Primitive::kPrimFloat)
5753 __ MulS(dst, lhs, rhs);
5754 else
5755 __ MulD(dst, lhs, rhs);
5756 break;
5757 }
5758 default:
5759 LOG(FATAL) << "Unexpected mul type " << type;
5760 }
5761}
5762
5763void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5764 LocationSummary* locations =
5765 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5766 switch (neg->GetResultType()) {
5767 case Primitive::kPrimInt:
5768 case Primitive::kPrimLong:
5769 locations->SetInAt(0, Location::RequiresRegister());
5770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5771 break;
5772
5773 case Primitive::kPrimFloat:
5774 case Primitive::kPrimDouble:
5775 locations->SetInAt(0, Location::RequiresFpuRegister());
5776 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5777 break;
5778
5779 default:
5780 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5781 }
5782}
5783
5784void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5785 Primitive::Type type = instruction->GetType();
5786 LocationSummary* locations = instruction->GetLocations();
5787
5788 switch (type) {
5789 case Primitive::kPrimInt:
5790 case Primitive::kPrimLong: {
5791 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5792 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5793 if (type == Primitive::kPrimInt)
5794 __ Subu(dst, ZERO, src);
5795 else
5796 __ Dsubu(dst, ZERO, src);
5797 break;
5798 }
5799 case Primitive::kPrimFloat:
5800 case Primitive::kPrimDouble: {
5801 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5802 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5803 if (type == Primitive::kPrimFloat)
5804 __ NegS(dst, src);
5805 else
5806 __ NegD(dst, src);
5807 break;
5808 }
5809 default:
5810 LOG(FATAL) << "Unexpected neg type " << type;
5811 }
5812}
5813
5814void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5815 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005817 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005818 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005819 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5820 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005821}
5822
5823void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005824 // Note: if heap poisoning is enabled, the entry point takes care
5825 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02005826 QuickEntrypointEnum entrypoint =
5827 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5828 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005829 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02005830 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005831}
5832
5833void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5834 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005835 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005836 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005837 if (instruction->IsStringAlloc()) {
5838 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5839 } else {
5840 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005841 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005842 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5843}
5844
5845void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005846 // Note: if heap poisoning is enabled, the entry point takes care
5847 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005848 if (instruction->IsStringAlloc()) {
5849 // String is allocated through StringFactory. Call NewEmptyString entry point.
5850 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005851 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005852 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005853 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5854 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5855 __ Jalr(T9);
5856 __ Nop();
5857 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5858 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005859 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005860 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005861 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005862}
5863
5864void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5866 locations->SetInAt(0, Location::RequiresRegister());
5867 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5868}
5869
5870void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5871 Primitive::Type type = instruction->GetType();
5872 LocationSummary* locations = instruction->GetLocations();
5873
5874 switch (type) {
5875 case Primitive::kPrimInt:
5876 case Primitive::kPrimLong: {
5877 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5878 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5879 __ Nor(dst, src, ZERO);
5880 break;
5881 }
5882
5883 default:
5884 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5885 }
5886}
5887
5888void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5890 locations->SetInAt(0, Location::RequiresRegister());
5891 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5892}
5893
5894void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5895 LocationSummary* locations = instruction->GetLocations();
5896 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5897 locations->InAt(0).AsRegister<GpuRegister>(),
5898 1);
5899}
5900
5901void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005902 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5903 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005904}
5905
Calin Juravle2ae48182016-03-16 14:05:09 +00005906void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5907 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005908 return;
5909 }
5910 Location obj = instruction->GetLocations()->InAt(0);
5911
5912 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005913 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005914}
5915
Calin Juravle2ae48182016-03-16 14:05:09 +00005916void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005917 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005918 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005919
5920 Location obj = instruction->GetLocations()->InAt(0);
5921
5922 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5923}
5924
5925void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005926 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005927}
5928
5929void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5930 HandleBinaryOp(instruction);
5931}
5932
5933void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5934 HandleBinaryOp(instruction);
5935}
5936
5937void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5938 LOG(FATAL) << "Unreachable";
5939}
5940
5941void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5942 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5943}
5944
5945void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5947 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5948 if (location.IsStackSlot()) {
5949 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5950 } else if (location.IsDoubleStackSlot()) {
5951 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5952 }
5953 locations->SetOut(location);
5954}
5955
5956void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5957 ATTRIBUTE_UNUSED) {
5958 // Nothing to do, the parameter is already at its location.
5959}
5960
5961void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5962 LocationSummary* locations =
5963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5964 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5965}
5966
5967void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5968 ATTRIBUTE_UNUSED) {
5969 // Nothing to do, the method is already at its location.
5970}
5971
5972void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5973 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005974 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005975 locations->SetInAt(i, Location::Any());
5976 }
5977 locations->SetOut(Location::Any());
5978}
5979
5980void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5981 LOG(FATAL) << "Unreachable";
5982}
5983
5984void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5985 Primitive::Type type = rem->GetResultType();
5986 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005987 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5988 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005989 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5990
5991 switch (type) {
5992 case Primitive::kPrimInt:
5993 case Primitive::kPrimLong:
5994 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005995 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5997 break;
5998
5999 case Primitive::kPrimFloat:
6000 case Primitive::kPrimDouble: {
6001 InvokeRuntimeCallingConvention calling_convention;
6002 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6003 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6004 locations->SetOut(calling_convention.GetReturnLocation(type));
6005 break;
6006 }
6007
6008 default:
6009 LOG(FATAL) << "Unexpected rem type " << type;
6010 }
6011}
6012
6013void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
6014 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006015
6016 switch (type) {
6017 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07006018 case Primitive::kPrimLong:
6019 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006020 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006021
6022 case Primitive::kPrimFloat:
6023 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01006024 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
6025 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006026 if (type == Primitive::kPrimFloat) {
6027 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6028 } else {
6029 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6030 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006031 break;
6032 }
6033 default:
6034 LOG(FATAL) << "Unexpected rem type " << type;
6035 }
6036}
6037
Igor Murashkind01745e2017-04-05 16:40:31 -07006038void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6039 constructor_fence->SetLocations(nullptr);
6040}
6041
6042void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6043 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6044 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6045}
6046
Alexey Frunze4dda3372015-06-01 18:31:49 -07006047void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6048 memory_barrier->SetLocations(nullptr);
6049}
6050
6051void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6052 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6053}
6054
6055void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
6056 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6057 Primitive::Type return_type = ret->InputAt(0)->GetType();
6058 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6059}
6060
6061void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6062 codegen_->GenerateFrameExit();
6063}
6064
6065void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6066 ret->SetLocations(nullptr);
6067}
6068
6069void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6070 codegen_->GenerateFrameExit();
6071}
6072
Alexey Frunze92d90602015-12-18 18:16:36 -08006073void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6074 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006075}
6076
Alexey Frunze92d90602015-12-18 18:16:36 -08006077void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6078 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006079}
6080
Alexey Frunze4dda3372015-06-01 18:31:49 -07006081void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6082 HandleShift(shl);
6083}
6084
6085void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6086 HandleShift(shl);
6087}
6088
6089void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6090 HandleShift(shr);
6091}
6092
6093void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6094 HandleShift(shr);
6095}
6096
Alexey Frunze4dda3372015-06-01 18:31:49 -07006097void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6098 HandleBinaryOp(instruction);
6099}
6100
6101void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6102 HandleBinaryOp(instruction);
6103}
6104
6105void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6106 HandleFieldGet(instruction, instruction->GetFieldInfo());
6107}
6108
6109void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6110 HandleFieldGet(instruction, instruction->GetFieldInfo());
6111}
6112
6113void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6114 HandleFieldSet(instruction, instruction->GetFieldInfo());
6115}
6116
6117void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006118 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006119}
6120
Calin Juravlee460d1d2015-09-29 04:52:17 +01006121void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6122 HUnresolvedInstanceFieldGet* instruction) {
6123 FieldAccessCallingConventionMIPS64 calling_convention;
6124 codegen_->CreateUnresolvedFieldLocationSummary(
6125 instruction, instruction->GetFieldType(), calling_convention);
6126}
6127
6128void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6129 HUnresolvedInstanceFieldGet* instruction) {
6130 FieldAccessCallingConventionMIPS64 calling_convention;
6131 codegen_->GenerateUnresolvedFieldAccess(instruction,
6132 instruction->GetFieldType(),
6133 instruction->GetFieldIndex(),
6134 instruction->GetDexPc(),
6135 calling_convention);
6136}
6137
6138void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6139 HUnresolvedInstanceFieldSet* instruction) {
6140 FieldAccessCallingConventionMIPS64 calling_convention;
6141 codegen_->CreateUnresolvedFieldLocationSummary(
6142 instruction, instruction->GetFieldType(), calling_convention);
6143}
6144
6145void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6146 HUnresolvedInstanceFieldSet* instruction) {
6147 FieldAccessCallingConventionMIPS64 calling_convention;
6148 codegen_->GenerateUnresolvedFieldAccess(instruction,
6149 instruction->GetFieldType(),
6150 instruction->GetFieldIndex(),
6151 instruction->GetDexPc(),
6152 calling_convention);
6153}
6154
6155void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6156 HUnresolvedStaticFieldGet* instruction) {
6157 FieldAccessCallingConventionMIPS64 calling_convention;
6158 codegen_->CreateUnresolvedFieldLocationSummary(
6159 instruction, instruction->GetFieldType(), calling_convention);
6160}
6161
6162void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6163 HUnresolvedStaticFieldGet* instruction) {
6164 FieldAccessCallingConventionMIPS64 calling_convention;
6165 codegen_->GenerateUnresolvedFieldAccess(instruction,
6166 instruction->GetFieldType(),
6167 instruction->GetFieldIndex(),
6168 instruction->GetDexPc(),
6169 calling_convention);
6170}
6171
6172void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6173 HUnresolvedStaticFieldSet* instruction) {
6174 FieldAccessCallingConventionMIPS64 calling_convention;
6175 codegen_->CreateUnresolvedFieldLocationSummary(
6176 instruction, instruction->GetFieldType(), calling_convention);
6177}
6178
6179void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6180 HUnresolvedStaticFieldSet* instruction) {
6181 FieldAccessCallingConventionMIPS64 calling_convention;
6182 codegen_->GenerateUnresolvedFieldAccess(instruction,
6183 instruction->GetFieldType(),
6184 instruction->GetFieldIndex(),
6185 instruction->GetDexPc(),
6186 calling_convention);
6187}
6188
Alexey Frunze4dda3372015-06-01 18:31:49 -07006189void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006190 LocationSummary* locations =
6191 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006192 // In suspend check slow path, usually there are no caller-save registers at all.
6193 // If SIMD instructions are present, however, we force spilling all live SIMD
6194 // registers in full width (since the runtime only saves/restores lower part).
6195 locations->SetCustomSlowPathCallerSaves(
6196 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006197}
6198
6199void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6200 HBasicBlock* block = instruction->GetBlock();
6201 if (block->GetLoopInformation() != nullptr) {
6202 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6203 // The back edge will generate the suspend check.
6204 return;
6205 }
6206 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6207 // The goto will generate the suspend check.
6208 return;
6209 }
6210 GenerateSuspendCheck(instruction, nullptr);
6211}
6212
Alexey Frunze4dda3372015-06-01 18:31:49 -07006213void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
6214 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006216 InvokeRuntimeCallingConvention calling_convention;
6217 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6218}
6219
6220void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006221 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006222 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6223}
6224
6225void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6226 Primitive::Type input_type = conversion->GetInputType();
6227 Primitive::Type result_type = conversion->GetResultType();
6228 DCHECK_NE(input_type, result_type);
6229
6230 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6231 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6232 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6233 }
6234
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006235 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
6236
6237 if (Primitive::IsFloatingPointType(input_type)) {
6238 locations->SetInAt(0, Location::RequiresFpuRegister());
6239 } else {
6240 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006241 }
6242
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006243 if (Primitive::IsFloatingPointType(result_type)) {
6244 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006245 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006247 }
6248}
6249
6250void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6251 LocationSummary* locations = conversion->GetLocations();
6252 Primitive::Type result_type = conversion->GetResultType();
6253 Primitive::Type input_type = conversion->GetInputType();
6254
6255 DCHECK_NE(input_type, result_type);
6256
6257 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6258 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6259 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6260
6261 switch (result_type) {
6262 case Primitive::kPrimChar:
6263 __ Andi(dst, src, 0xFFFF);
6264 break;
6265 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006266 if (input_type == Primitive::kPrimLong) {
6267 // Type conversion from long to types narrower than int is a result of code
6268 // transformations. To avoid unpredictable results for SEB and SEH, we first
6269 // need to sign-extend the low 32-bit value into bits 32 through 63.
6270 __ Sll(dst, src, 0);
6271 __ Seb(dst, dst);
6272 } else {
6273 __ Seb(dst, src);
6274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006275 break;
6276 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006277 if (input_type == Primitive::kPrimLong) {
6278 // Type conversion from long to types narrower than int is a result of code
6279 // transformations. To avoid unpredictable results for SEB and SEH, we first
6280 // need to sign-extend the low 32-bit value into bits 32 through 63.
6281 __ Sll(dst, src, 0);
6282 __ Seh(dst, dst);
6283 } else {
6284 __ Seh(dst, src);
6285 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006286 break;
6287 case Primitive::kPrimInt:
6288 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006289 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6290 // conversions, except when the input and output registers are the same and we are not
6291 // converting longs to shorter types. In these cases, do nothing.
6292 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
6293 __ Sll(dst, src, 0);
6294 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006295 break;
6296
6297 default:
6298 LOG(FATAL) << "Unexpected type conversion from " << input_type
6299 << " to " << result_type;
6300 }
6301 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006302 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6303 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6304 if (input_type == Primitive::kPrimLong) {
6305 __ Dmtc1(src, FTMP);
6306 if (result_type == Primitive::kPrimFloat) {
6307 __ Cvtsl(dst, FTMP);
6308 } else {
6309 __ Cvtdl(dst, FTMP);
6310 }
6311 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006312 __ Mtc1(src, FTMP);
6313 if (result_type == Primitive::kPrimFloat) {
6314 __ Cvtsw(dst, FTMP);
6315 } else {
6316 __ Cvtdw(dst, FTMP);
6317 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006318 }
6319 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6320 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006321 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6322 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006323
6324 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006325 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006326 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006327 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006328 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006329 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006330 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006331 } else {
6332 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006333 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006334 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006335 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006336 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006337 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006338 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006339 } else if (Primitive::IsFloatingPointType(result_type) &&
6340 Primitive::IsFloatingPointType(input_type)) {
6341 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6342 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6343 if (result_type == Primitive::kPrimFloat) {
6344 __ Cvtsd(dst, src);
6345 } else {
6346 __ Cvtds(dst, src);
6347 }
6348 } else {
6349 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6350 << " to " << result_type;
6351 }
6352}
6353
6354void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6355 HandleShift(ushr);
6356}
6357
6358void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6359 HandleShift(ushr);
6360}
6361
6362void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6363 HandleBinaryOp(instruction);
6364}
6365
6366void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6367 HandleBinaryOp(instruction);
6368}
6369
6370void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6371 // Nothing to do, this should be removed during prepare for register allocator.
6372 LOG(FATAL) << "Unreachable";
6373}
6374
6375void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6376 // Nothing to do, this should be removed during prepare for register allocator.
6377 LOG(FATAL) << "Unreachable";
6378}
6379
6380void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006381 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006382}
6383
6384void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006385 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006386}
6387
6388void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006389 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006390}
6391
6392void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006393 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006394}
6395
6396void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006397 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006398}
6399
6400void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006401 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006402}
6403
6404void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006405 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006406}
6407
6408void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006409 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006410}
6411
6412void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006413 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006414}
6415
6416void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006417 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006418}
6419
6420void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006421 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006422}
6423
6424void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006425 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006426}
6427
Aart Bike9f37602015-10-09 11:15:55 -07006428void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006429 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006430}
6431
6432void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006433 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006434}
6435
6436void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006437 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006438}
6439
6440void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006441 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006442}
6443
6444void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006445 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006446}
6447
6448void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006449 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006450}
6451
6452void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006453 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006454}
6455
6456void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006457 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006458}
6459
Mark Mendellfe57faa2015-09-18 09:26:15 -04006460// Simple implementation of packed switch - generate cascaded compare/jumps.
6461void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6462 LocationSummary* locations =
6463 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6464 locations->SetInAt(0, Location::RequiresRegister());
6465}
6466
Alexey Frunze0960ac52016-12-20 17:24:59 -08006467void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6468 int32_t lower_bound,
6469 uint32_t num_entries,
6470 HBasicBlock* switch_block,
6471 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006472 // Create a set of compare/jumps.
6473 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006474 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006475 // Jump to default if index is negative
6476 // Note: We don't check the case that index is positive while value < lower_bound, because in
6477 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6478 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6479
Alexey Frunze0960ac52016-12-20 17:24:59 -08006480 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006481 // Jump to successors[0] if value == lower_bound.
6482 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6483 int32_t last_index = 0;
6484 for (; num_entries - last_index > 2; last_index += 2) {
6485 __ Addiu(temp_reg, temp_reg, -2);
6486 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6487 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6488 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6489 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6490 }
6491 if (num_entries - last_index == 2) {
6492 // The last missing case_value.
6493 __ Addiu(temp_reg, temp_reg, -1);
6494 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006495 }
6496
6497 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006498 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006499 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006500 }
6501}
6502
Alexey Frunze0960ac52016-12-20 17:24:59 -08006503void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6504 int32_t lower_bound,
6505 uint32_t num_entries,
6506 HBasicBlock* switch_block,
6507 HBasicBlock* default_block) {
6508 // Create a jump table.
6509 std::vector<Mips64Label*> labels(num_entries);
6510 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6511 for (uint32_t i = 0; i < num_entries; i++) {
6512 labels[i] = codegen_->GetLabelOf(successors[i]);
6513 }
6514 JumpTable* table = __ CreateJumpTable(std::move(labels));
6515
6516 // Is the value in range?
6517 __ Addiu32(TMP, value_reg, -lower_bound);
6518 __ LoadConst32(AT, num_entries);
6519 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6520
6521 // We are in the range of the table.
6522 // Load the target address from the jump table, indexing by the value.
6523 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006524 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006525 __ Lw(TMP, TMP, 0);
6526 // Compute the absolute target address by adding the table start address
6527 // (the table contains offsets to targets relative to its start).
6528 __ Daddu(TMP, TMP, AT);
6529 // And jump.
6530 __ Jr(TMP);
6531 __ Nop();
6532}
6533
6534void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6535 int32_t lower_bound = switch_instr->GetStartValue();
6536 uint32_t num_entries = switch_instr->GetNumEntries();
6537 LocationSummary* locations = switch_instr->GetLocations();
6538 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6539 HBasicBlock* switch_block = switch_instr->GetBlock();
6540 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6541
6542 if (num_entries > kPackedSwitchJumpTableThreshold) {
6543 GenTableBasedPackedSwitch(value_reg,
6544 lower_bound,
6545 num_entries,
6546 switch_block,
6547 default_block);
6548 } else {
6549 GenPackedSwitchWithCompares(value_reg,
6550 lower_bound,
6551 num_entries,
6552 switch_block,
6553 default_block);
6554 }
6555}
6556
Chris Larsenc9905a62017-03-13 17:06:18 -07006557void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6558 LocationSummary* locations =
6559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6560 locations->SetInAt(0, Location::RequiresRegister());
6561 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006562}
6563
Chris Larsenc9905a62017-03-13 17:06:18 -07006564void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6565 LocationSummary* locations = instruction->GetLocations();
6566 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6567 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6568 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6569 __ LoadFromOffset(kLoadDoubleword,
6570 locations->Out().AsRegister<GpuRegister>(),
6571 locations->InAt(0).AsRegister<GpuRegister>(),
6572 method_offset);
6573 } else {
6574 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6575 instruction->GetIndex(), kMips64PointerSize));
6576 __ LoadFromOffset(kLoadDoubleword,
6577 locations->Out().AsRegister<GpuRegister>(),
6578 locations->InAt(0).AsRegister<GpuRegister>(),
6579 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6580 __ LoadFromOffset(kLoadDoubleword,
6581 locations->Out().AsRegister<GpuRegister>(),
6582 locations->Out().AsRegister<GpuRegister>(),
6583 method_offset);
6584 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006585}
6586
Alexey Frunze4dda3372015-06-01 18:31:49 -07006587} // namespace mips64
6588} // namespace art