blob: 76aed1527bb8b5a4b2ebb1adcd35c578a9a4cbe8 [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
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003655bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3656 bool is64bit,
3657 LocationSummary* input_locations,
3658 GpuRegister dst) {
3659 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3660 Location rhs_location = input_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 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3674
3675 switch (cond) {
3676 case kCondEQ:
3677 case kCondNE:
3678 if (use_imm && IsInt<16>(-rhs_imm)) {
3679 if (is64bit) {
3680 __ Daddiu(dst, lhs, -rhs_imm);
3681 } else {
3682 __ Addiu(dst, lhs, -rhs_imm);
3683 }
3684 } else if (use_imm && IsUint<16>(rhs_imm)) {
3685 __ Xori(dst, lhs, rhs_imm);
3686 } else {
3687 if (use_imm) {
3688 rhs_reg = TMP;
3689 __ LoadConst64(rhs_reg, rhs_imm);
3690 }
3691 __ Xor(dst, lhs, rhs_reg);
3692 }
3693 return (cond == kCondEQ);
3694
3695 case kCondLT:
3696 case kCondGE:
3697 if (use_imm && IsInt<16>(rhs_imm)) {
3698 __ Slti(dst, lhs, rhs_imm);
3699 } else {
3700 if (use_imm) {
3701 rhs_reg = TMP;
3702 __ LoadConst64(rhs_reg, rhs_imm);
3703 }
3704 __ Slt(dst, lhs, rhs_reg);
3705 }
3706 return (cond == kCondGE);
3707
3708 case kCondLE:
3709 case kCondGT:
3710 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3711 // Simulate lhs <= rhs via lhs < rhs + 1.
3712 __ Slti(dst, lhs, rhs_imm_plus_one);
3713 return (cond == kCondGT);
3714 } else {
3715 if (use_imm) {
3716 rhs_reg = TMP;
3717 __ LoadConst64(rhs_reg, rhs_imm);
3718 }
3719 __ Slt(dst, rhs_reg, lhs);
3720 return (cond == kCondLE);
3721 }
3722
3723 case kCondB:
3724 case kCondAE:
3725 if (use_imm && IsInt<16>(rhs_imm)) {
3726 // Sltiu sign-extends its 16-bit immediate operand before
3727 // the comparison and thus lets us compare directly with
3728 // unsigned values in the ranges [0, 0x7fff] and
3729 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3730 __ Sltiu(dst, lhs, rhs_imm);
3731 } else {
3732 if (use_imm) {
3733 rhs_reg = TMP;
3734 __ LoadConst64(rhs_reg, rhs_imm);
3735 }
3736 __ Sltu(dst, lhs, rhs_reg);
3737 }
3738 return (cond == kCondAE);
3739
3740 case kCondBE:
3741 case kCondA:
3742 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3743 // Simulate lhs <= rhs via lhs < rhs + 1.
3744 // Note that this only works if rhs + 1 does not overflow
3745 // to 0, hence the check above.
3746 // Sltiu sign-extends its 16-bit immediate operand before
3747 // the comparison and thus lets us compare directly with
3748 // unsigned values in the ranges [0, 0x7fff] and
3749 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3750 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3751 return (cond == kCondA);
3752 } else {
3753 if (use_imm) {
3754 rhs_reg = TMP;
3755 __ LoadConst64(rhs_reg, rhs_imm);
3756 }
3757 __ Sltu(dst, rhs_reg, lhs);
3758 return (cond == kCondBE);
3759 }
3760 }
3761}
3762
Alexey Frunze299a9392015-12-08 16:08:02 -08003763void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3764 bool is64bit,
3765 LocationSummary* locations,
3766 Mips64Label* label) {
3767 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3768 Location rhs_location = locations->InAt(1);
3769 GpuRegister rhs_reg = ZERO;
3770 int64_t rhs_imm = 0;
3771 bool use_imm = rhs_location.IsConstant();
3772 if (use_imm) {
3773 if (is64bit) {
3774 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3775 } else {
3776 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3777 }
3778 } else {
3779 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3780 }
3781
3782 if (use_imm && rhs_imm == 0) {
3783 switch (cond) {
3784 case kCondEQ:
3785 case kCondBE: // <= 0 if zero
3786 __ Beqzc(lhs, label);
3787 break;
3788 case kCondNE:
3789 case kCondA: // > 0 if non-zero
3790 __ Bnezc(lhs, label);
3791 break;
3792 case kCondLT:
3793 __ Bltzc(lhs, label);
3794 break;
3795 case kCondGE:
3796 __ Bgezc(lhs, label);
3797 break;
3798 case kCondLE:
3799 __ Blezc(lhs, label);
3800 break;
3801 case kCondGT:
3802 __ Bgtzc(lhs, label);
3803 break;
3804 case kCondB: // always false
3805 break;
3806 case kCondAE: // always true
3807 __ Bc(label);
3808 break;
3809 }
3810 } else {
3811 if (use_imm) {
3812 rhs_reg = TMP;
3813 __ LoadConst64(rhs_reg, rhs_imm);
3814 }
3815 switch (cond) {
3816 case kCondEQ:
3817 __ Beqc(lhs, rhs_reg, label);
3818 break;
3819 case kCondNE:
3820 __ Bnec(lhs, rhs_reg, label);
3821 break;
3822 case kCondLT:
3823 __ Bltc(lhs, rhs_reg, label);
3824 break;
3825 case kCondGE:
3826 __ Bgec(lhs, rhs_reg, label);
3827 break;
3828 case kCondLE:
3829 __ Bgec(rhs_reg, lhs, label);
3830 break;
3831 case kCondGT:
3832 __ Bltc(rhs_reg, lhs, label);
3833 break;
3834 case kCondB:
3835 __ Bltuc(lhs, rhs_reg, label);
3836 break;
3837 case kCondAE:
3838 __ Bgeuc(lhs, rhs_reg, label);
3839 break;
3840 case kCondBE:
3841 __ Bgeuc(rhs_reg, lhs, label);
3842 break;
3843 case kCondA:
3844 __ Bltuc(rhs_reg, lhs, label);
3845 break;
3846 }
3847 }
3848}
3849
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003850void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3851 bool gt_bias,
3852 Primitive::Type type,
3853 LocationSummary* locations) {
3854 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3855 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3856 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3857 if (type == Primitive::kPrimFloat) {
3858 switch (cond) {
3859 case kCondEQ:
3860 __ CmpEqS(FTMP, lhs, rhs);
3861 __ Mfc1(dst, FTMP);
3862 __ Andi(dst, dst, 1);
3863 break;
3864 case kCondNE:
3865 __ CmpEqS(FTMP, lhs, rhs);
3866 __ Mfc1(dst, FTMP);
3867 __ Addiu(dst, dst, 1);
3868 break;
3869 case kCondLT:
3870 if (gt_bias) {
3871 __ CmpLtS(FTMP, lhs, rhs);
3872 } else {
3873 __ CmpUltS(FTMP, lhs, rhs);
3874 }
3875 __ Mfc1(dst, FTMP);
3876 __ Andi(dst, dst, 1);
3877 break;
3878 case kCondLE:
3879 if (gt_bias) {
3880 __ CmpLeS(FTMP, lhs, rhs);
3881 } else {
3882 __ CmpUleS(FTMP, lhs, rhs);
3883 }
3884 __ Mfc1(dst, FTMP);
3885 __ Andi(dst, dst, 1);
3886 break;
3887 case kCondGT:
3888 if (gt_bias) {
3889 __ CmpUltS(FTMP, rhs, lhs);
3890 } else {
3891 __ CmpLtS(FTMP, rhs, lhs);
3892 }
3893 __ Mfc1(dst, FTMP);
3894 __ Andi(dst, dst, 1);
3895 break;
3896 case kCondGE:
3897 if (gt_bias) {
3898 __ CmpUleS(FTMP, rhs, lhs);
3899 } else {
3900 __ CmpLeS(FTMP, rhs, lhs);
3901 }
3902 __ Mfc1(dst, FTMP);
3903 __ Andi(dst, dst, 1);
3904 break;
3905 default:
3906 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3907 UNREACHABLE();
3908 }
3909 } else {
3910 DCHECK_EQ(type, Primitive::kPrimDouble);
3911 switch (cond) {
3912 case kCondEQ:
3913 __ CmpEqD(FTMP, lhs, rhs);
3914 __ Mfc1(dst, FTMP);
3915 __ Andi(dst, dst, 1);
3916 break;
3917 case kCondNE:
3918 __ CmpEqD(FTMP, lhs, rhs);
3919 __ Mfc1(dst, FTMP);
3920 __ Addiu(dst, dst, 1);
3921 break;
3922 case kCondLT:
3923 if (gt_bias) {
3924 __ CmpLtD(FTMP, lhs, rhs);
3925 } else {
3926 __ CmpUltD(FTMP, lhs, rhs);
3927 }
3928 __ Mfc1(dst, FTMP);
3929 __ Andi(dst, dst, 1);
3930 break;
3931 case kCondLE:
3932 if (gt_bias) {
3933 __ CmpLeD(FTMP, lhs, rhs);
3934 } else {
3935 __ CmpUleD(FTMP, lhs, rhs);
3936 }
3937 __ Mfc1(dst, FTMP);
3938 __ Andi(dst, dst, 1);
3939 break;
3940 case kCondGT:
3941 if (gt_bias) {
3942 __ CmpUltD(FTMP, rhs, lhs);
3943 } else {
3944 __ CmpLtD(FTMP, rhs, lhs);
3945 }
3946 __ Mfc1(dst, FTMP);
3947 __ Andi(dst, dst, 1);
3948 break;
3949 case kCondGE:
3950 if (gt_bias) {
3951 __ CmpUleD(FTMP, rhs, lhs);
3952 } else {
3953 __ CmpLeD(FTMP, rhs, lhs);
3954 }
3955 __ Mfc1(dst, FTMP);
3956 __ Andi(dst, dst, 1);
3957 break;
3958 default:
3959 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3960 UNREACHABLE();
3961 }
3962 }
3963}
3964
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003965bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
3966 bool gt_bias,
3967 Primitive::Type type,
3968 LocationSummary* input_locations,
3969 FpuRegister dst) {
3970 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
3971 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
3972 if (type == Primitive::kPrimFloat) {
3973 switch (cond) {
3974 case kCondEQ:
3975 __ CmpEqS(dst, lhs, rhs);
3976 return false;
3977 case kCondNE:
3978 __ CmpEqS(dst, lhs, rhs);
3979 return true;
3980 case kCondLT:
3981 if (gt_bias) {
3982 __ CmpLtS(dst, lhs, rhs);
3983 } else {
3984 __ CmpUltS(dst, lhs, rhs);
3985 }
3986 return false;
3987 case kCondLE:
3988 if (gt_bias) {
3989 __ CmpLeS(dst, lhs, rhs);
3990 } else {
3991 __ CmpUleS(dst, lhs, rhs);
3992 }
3993 return false;
3994 case kCondGT:
3995 if (gt_bias) {
3996 __ CmpUltS(dst, rhs, lhs);
3997 } else {
3998 __ CmpLtS(dst, rhs, lhs);
3999 }
4000 return false;
4001 case kCondGE:
4002 if (gt_bias) {
4003 __ CmpUleS(dst, rhs, lhs);
4004 } else {
4005 __ CmpLeS(dst, rhs, lhs);
4006 }
4007 return false;
4008 default:
4009 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4010 UNREACHABLE();
4011 }
4012 } else {
4013 DCHECK_EQ(type, Primitive::kPrimDouble);
4014 switch (cond) {
4015 case kCondEQ:
4016 __ CmpEqD(dst, lhs, rhs);
4017 return false;
4018 case kCondNE:
4019 __ CmpEqD(dst, lhs, rhs);
4020 return true;
4021 case kCondLT:
4022 if (gt_bias) {
4023 __ CmpLtD(dst, lhs, rhs);
4024 } else {
4025 __ CmpUltD(dst, lhs, rhs);
4026 }
4027 return false;
4028 case kCondLE:
4029 if (gt_bias) {
4030 __ CmpLeD(dst, lhs, rhs);
4031 } else {
4032 __ CmpUleD(dst, lhs, rhs);
4033 }
4034 return false;
4035 case kCondGT:
4036 if (gt_bias) {
4037 __ CmpUltD(dst, rhs, lhs);
4038 } else {
4039 __ CmpLtD(dst, rhs, lhs);
4040 }
4041 return false;
4042 case kCondGE:
4043 if (gt_bias) {
4044 __ CmpUleD(dst, rhs, lhs);
4045 } else {
4046 __ CmpLeD(dst, rhs, lhs);
4047 }
4048 return false;
4049 default:
4050 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4051 UNREACHABLE();
4052 }
4053 }
4054}
4055
Alexey Frunze299a9392015-12-08 16:08:02 -08004056void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4057 bool gt_bias,
4058 Primitive::Type type,
4059 LocationSummary* locations,
4060 Mips64Label* label) {
4061 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4062 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
4063 if (type == Primitive::kPrimFloat) {
4064 switch (cond) {
4065 case kCondEQ:
4066 __ CmpEqS(FTMP, lhs, rhs);
4067 __ Bc1nez(FTMP, label);
4068 break;
4069 case kCondNE:
4070 __ CmpEqS(FTMP, lhs, rhs);
4071 __ Bc1eqz(FTMP, label);
4072 break;
4073 case kCondLT:
4074 if (gt_bias) {
4075 __ CmpLtS(FTMP, lhs, rhs);
4076 } else {
4077 __ CmpUltS(FTMP, lhs, rhs);
4078 }
4079 __ Bc1nez(FTMP, label);
4080 break;
4081 case kCondLE:
4082 if (gt_bias) {
4083 __ CmpLeS(FTMP, lhs, rhs);
4084 } else {
4085 __ CmpUleS(FTMP, lhs, rhs);
4086 }
4087 __ Bc1nez(FTMP, label);
4088 break;
4089 case kCondGT:
4090 if (gt_bias) {
4091 __ CmpUltS(FTMP, rhs, lhs);
4092 } else {
4093 __ CmpLtS(FTMP, rhs, lhs);
4094 }
4095 __ Bc1nez(FTMP, label);
4096 break;
4097 case kCondGE:
4098 if (gt_bias) {
4099 __ CmpUleS(FTMP, rhs, lhs);
4100 } else {
4101 __ CmpLeS(FTMP, rhs, lhs);
4102 }
4103 __ Bc1nez(FTMP, label);
4104 break;
4105 default:
4106 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004107 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004108 }
4109 } else {
4110 DCHECK_EQ(type, Primitive::kPrimDouble);
4111 switch (cond) {
4112 case kCondEQ:
4113 __ CmpEqD(FTMP, lhs, rhs);
4114 __ Bc1nez(FTMP, label);
4115 break;
4116 case kCondNE:
4117 __ CmpEqD(FTMP, lhs, rhs);
4118 __ Bc1eqz(FTMP, label);
4119 break;
4120 case kCondLT:
4121 if (gt_bias) {
4122 __ CmpLtD(FTMP, lhs, rhs);
4123 } else {
4124 __ CmpUltD(FTMP, lhs, rhs);
4125 }
4126 __ Bc1nez(FTMP, label);
4127 break;
4128 case kCondLE:
4129 if (gt_bias) {
4130 __ CmpLeD(FTMP, lhs, rhs);
4131 } else {
4132 __ CmpUleD(FTMP, lhs, rhs);
4133 }
4134 __ Bc1nez(FTMP, label);
4135 break;
4136 case kCondGT:
4137 if (gt_bias) {
4138 __ CmpUltD(FTMP, rhs, lhs);
4139 } else {
4140 __ CmpLtD(FTMP, rhs, lhs);
4141 }
4142 __ Bc1nez(FTMP, label);
4143 break;
4144 case kCondGE:
4145 if (gt_bias) {
4146 __ CmpUleD(FTMP, rhs, lhs);
4147 } else {
4148 __ CmpLeD(FTMP, rhs, lhs);
4149 }
4150 __ Bc1nez(FTMP, label);
4151 break;
4152 default:
4153 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004154 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004155 }
4156 }
4157}
4158
Alexey Frunze4dda3372015-06-01 18:31:49 -07004159void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004160 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004161 Mips64Label* true_target,
4162 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004163 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004164
David Brazdil0debae72015-11-12 18:37:00 +00004165 if (true_target == nullptr && false_target == nullptr) {
4166 // Nothing to do. The code always falls through.
4167 return;
4168 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004169 // Constant condition, statically compared against "true" (integer value 1).
4170 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004171 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004172 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004173 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004174 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004175 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004176 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004177 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004178 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004179 }
David Brazdil0debae72015-11-12 18:37:00 +00004180 return;
4181 }
4182
4183 // The following code generates these patterns:
4184 // (1) true_target == nullptr && false_target != nullptr
4185 // - opposite condition true => branch to false_target
4186 // (2) true_target != nullptr && false_target == nullptr
4187 // - condition true => branch to true_target
4188 // (3) true_target != nullptr && false_target != nullptr
4189 // - condition true => branch to true_target
4190 // - branch to false_target
4191 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004192 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004193 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004194 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004195 if (true_target == nullptr) {
4196 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4197 } else {
4198 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4199 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004200 } else {
4201 // The condition instruction has not been materialized, use its inputs as
4202 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004203 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004204 Primitive::Type type = condition->InputAt(0)->GetType();
4205 LocationSummary* locations = cond->GetLocations();
4206 IfCondition if_cond = condition->GetCondition();
4207 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004208
David Brazdil0debae72015-11-12 18:37:00 +00004209 if (true_target == nullptr) {
4210 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004211 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004212 }
4213
Alexey Frunze299a9392015-12-08 16:08:02 -08004214 switch (type) {
4215 default:
4216 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4217 break;
4218 case Primitive::kPrimLong:
4219 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4220 break;
4221 case Primitive::kPrimFloat:
4222 case Primitive::kPrimDouble:
4223 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4224 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004225 }
4226 }
David Brazdil0debae72015-11-12 18:37:00 +00004227
4228 // If neither branch falls through (case 3), the conditional branch to `true_target`
4229 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4230 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004231 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004232 }
4233}
4234
4235void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
4236 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004237 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004238 locations->SetInAt(0, Location::RequiresRegister());
4239 }
4240}
4241
4242void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004243 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4244 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004245 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004246 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004247 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004248 nullptr : codegen_->GetLabelOf(false_successor);
4249 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004250}
4251
4252void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
4253 LocationSummary* locations = new (GetGraph()->GetArena())
4254 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004255 InvokeRuntimeCallingConvention calling_convention;
4256 RegisterSet caller_saves = RegisterSet::Empty();
4257 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4258 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004259 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004260 locations->SetInAt(0, Location::RequiresRegister());
4261 }
4262}
4263
4264void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004265 SlowPathCodeMIPS64* slow_path =
4266 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004267 GenerateTestAndBranch(deoptimize,
4268 /* condition_input_index */ 0,
4269 slow_path->GetEntryLabel(),
4270 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004271}
4272
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004273// This function returns true if a conditional move can be generated for HSelect.
4274// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4275// branches and regular moves.
4276//
4277// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4278//
4279// While determining feasibility of a conditional move and setting inputs/outputs
4280// are two distinct tasks, this function does both because they share quite a bit
4281// of common logic.
4282static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4283 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4284 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4285 HCondition* condition = cond->AsCondition();
4286
4287 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4288 Primitive::Type dst_type = select->GetType();
4289
4290 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4291 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4292 bool is_true_value_zero_constant =
4293 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4294 bool is_false_value_zero_constant =
4295 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4296
4297 bool can_move_conditionally = false;
4298 bool use_const_for_false_in = false;
4299 bool use_const_for_true_in = false;
4300
4301 if (!cond->IsConstant()) {
4302 if (!Primitive::IsFloatingPointType(cond_type)) {
4303 if (!Primitive::IsFloatingPointType(dst_type)) {
4304 // Moving int/long on int/long condition.
4305 if (is_true_value_zero_constant) {
4306 // seleqz out_reg, false_reg, cond_reg
4307 can_move_conditionally = true;
4308 use_const_for_true_in = true;
4309 } else if (is_false_value_zero_constant) {
4310 // selnez out_reg, true_reg, cond_reg
4311 can_move_conditionally = true;
4312 use_const_for_false_in = true;
4313 } else if (materialized) {
4314 // Not materializing unmaterialized int conditions
4315 // to keep the instruction count low.
4316 // selnez AT, true_reg, cond_reg
4317 // seleqz TMP, false_reg, cond_reg
4318 // or out_reg, AT, TMP
4319 can_move_conditionally = true;
4320 }
4321 } else {
4322 // Moving float/double on int/long condition.
4323 if (materialized) {
4324 // Not materializing unmaterialized int conditions
4325 // to keep the instruction count low.
4326 can_move_conditionally = true;
4327 if (is_true_value_zero_constant) {
4328 // sltu TMP, ZERO, cond_reg
4329 // mtc1 TMP, temp_cond_reg
4330 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4331 use_const_for_true_in = true;
4332 } else if (is_false_value_zero_constant) {
4333 // sltu TMP, ZERO, cond_reg
4334 // mtc1 TMP, temp_cond_reg
4335 // selnez.fmt out_reg, true_reg, temp_cond_reg
4336 use_const_for_false_in = true;
4337 } else {
4338 // sltu TMP, ZERO, cond_reg
4339 // mtc1 TMP, temp_cond_reg
4340 // sel.fmt temp_cond_reg, false_reg, true_reg
4341 // mov.fmt out_reg, temp_cond_reg
4342 }
4343 }
4344 }
4345 } else {
4346 if (!Primitive::IsFloatingPointType(dst_type)) {
4347 // Moving int/long on float/double condition.
4348 can_move_conditionally = true;
4349 if (is_true_value_zero_constant) {
4350 // mfc1 TMP, temp_cond_reg
4351 // seleqz out_reg, false_reg, TMP
4352 use_const_for_true_in = true;
4353 } else if (is_false_value_zero_constant) {
4354 // mfc1 TMP, temp_cond_reg
4355 // selnez out_reg, true_reg, TMP
4356 use_const_for_false_in = true;
4357 } else {
4358 // mfc1 TMP, temp_cond_reg
4359 // selnez AT, true_reg, TMP
4360 // seleqz TMP, false_reg, TMP
4361 // or out_reg, AT, TMP
4362 }
4363 } else {
4364 // Moving float/double on float/double condition.
4365 can_move_conditionally = true;
4366 if (is_true_value_zero_constant) {
4367 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4368 use_const_for_true_in = true;
4369 } else if (is_false_value_zero_constant) {
4370 // selnez.fmt out_reg, true_reg, temp_cond_reg
4371 use_const_for_false_in = true;
4372 } else {
4373 // sel.fmt temp_cond_reg, false_reg, true_reg
4374 // mov.fmt out_reg, temp_cond_reg
4375 }
4376 }
4377 }
4378 }
4379
4380 if (can_move_conditionally) {
4381 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4382 } else {
4383 DCHECK(!use_const_for_false_in);
4384 DCHECK(!use_const_for_true_in);
4385 }
4386
4387 if (locations_to_set != nullptr) {
4388 if (use_const_for_false_in) {
4389 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4390 } else {
4391 locations_to_set->SetInAt(0,
4392 Primitive::IsFloatingPointType(dst_type)
4393 ? Location::RequiresFpuRegister()
4394 : Location::RequiresRegister());
4395 }
4396 if (use_const_for_true_in) {
4397 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4398 } else {
4399 locations_to_set->SetInAt(1,
4400 Primitive::IsFloatingPointType(dst_type)
4401 ? Location::RequiresFpuRegister()
4402 : Location::RequiresRegister());
4403 }
4404 if (materialized) {
4405 locations_to_set->SetInAt(2, Location::RequiresRegister());
4406 }
4407
4408 if (can_move_conditionally) {
4409 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4410 ? Location::RequiresFpuRegister()
4411 : Location::RequiresRegister());
4412 } else {
4413 locations_to_set->SetOut(Location::SameAsFirstInput());
4414 }
4415 }
4416
4417 return can_move_conditionally;
4418}
4419
4420
4421void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4422 LocationSummary* locations = select->GetLocations();
4423 Location dst = locations->Out();
4424 Location false_src = locations->InAt(0);
4425 Location true_src = locations->InAt(1);
4426 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4427 GpuRegister cond_reg = TMP;
4428 FpuRegister fcond_reg = FTMP;
4429 Primitive::Type cond_type = Primitive::kPrimInt;
4430 bool cond_inverted = false;
4431 Primitive::Type dst_type = select->GetType();
4432
4433 if (IsBooleanValueOrMaterializedCondition(cond)) {
4434 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4435 } else {
4436 HCondition* condition = cond->AsCondition();
4437 LocationSummary* cond_locations = cond->GetLocations();
4438 IfCondition if_cond = condition->GetCondition();
4439 cond_type = condition->InputAt(0)->GetType();
4440 switch (cond_type) {
4441 default:
4442 cond_inverted = MaterializeIntLongCompare(if_cond,
4443 /* is64bit */ false,
4444 cond_locations,
4445 cond_reg);
4446 break;
4447 case Primitive::kPrimLong:
4448 cond_inverted = MaterializeIntLongCompare(if_cond,
4449 /* is64bit */ true,
4450 cond_locations,
4451 cond_reg);
4452 break;
4453 case Primitive::kPrimFloat:
4454 case Primitive::kPrimDouble:
4455 cond_inverted = MaterializeFpCompare(if_cond,
4456 condition->IsGtBias(),
4457 cond_type,
4458 cond_locations,
4459 fcond_reg);
4460 break;
4461 }
4462 }
4463
4464 if (true_src.IsConstant()) {
4465 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4466 }
4467 if (false_src.IsConstant()) {
4468 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4469 }
4470
4471 switch (dst_type) {
4472 default:
4473 if (Primitive::IsFloatingPointType(cond_type)) {
4474 __ Mfc1(cond_reg, fcond_reg);
4475 }
4476 if (true_src.IsConstant()) {
4477 if (cond_inverted) {
4478 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4479 } else {
4480 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4481 }
4482 } else if (false_src.IsConstant()) {
4483 if (cond_inverted) {
4484 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4485 } else {
4486 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4487 }
4488 } else {
4489 DCHECK_NE(cond_reg, AT);
4490 if (cond_inverted) {
4491 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4492 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4493 } else {
4494 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4495 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4496 }
4497 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4498 }
4499 break;
4500 case Primitive::kPrimFloat: {
4501 if (!Primitive::IsFloatingPointType(cond_type)) {
4502 // sel*.fmt tests bit 0 of the condition register, account for that.
4503 __ Sltu(TMP, ZERO, cond_reg);
4504 __ Mtc1(TMP, fcond_reg);
4505 }
4506 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4507 if (true_src.IsConstant()) {
4508 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4509 if (cond_inverted) {
4510 __ SelnezS(dst_reg, src_reg, fcond_reg);
4511 } else {
4512 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4513 }
4514 } else if (false_src.IsConstant()) {
4515 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4516 if (cond_inverted) {
4517 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4518 } else {
4519 __ SelnezS(dst_reg, src_reg, fcond_reg);
4520 }
4521 } else {
4522 if (cond_inverted) {
4523 __ SelS(fcond_reg,
4524 true_src.AsFpuRegister<FpuRegister>(),
4525 false_src.AsFpuRegister<FpuRegister>());
4526 } else {
4527 __ SelS(fcond_reg,
4528 false_src.AsFpuRegister<FpuRegister>(),
4529 true_src.AsFpuRegister<FpuRegister>());
4530 }
4531 __ MovS(dst_reg, fcond_reg);
4532 }
4533 break;
4534 }
4535 case Primitive::kPrimDouble: {
4536 if (!Primitive::IsFloatingPointType(cond_type)) {
4537 // sel*.fmt tests bit 0 of the condition register, account for that.
4538 __ Sltu(TMP, ZERO, cond_reg);
4539 __ Mtc1(TMP, fcond_reg);
4540 }
4541 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4542 if (true_src.IsConstant()) {
4543 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4544 if (cond_inverted) {
4545 __ SelnezD(dst_reg, src_reg, fcond_reg);
4546 } else {
4547 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4548 }
4549 } else if (false_src.IsConstant()) {
4550 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4551 if (cond_inverted) {
4552 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4553 } else {
4554 __ SelnezD(dst_reg, src_reg, fcond_reg);
4555 }
4556 } else {
4557 if (cond_inverted) {
4558 __ SelD(fcond_reg,
4559 true_src.AsFpuRegister<FpuRegister>(),
4560 false_src.AsFpuRegister<FpuRegister>());
4561 } else {
4562 __ SelD(fcond_reg,
4563 false_src.AsFpuRegister<FpuRegister>(),
4564 true_src.AsFpuRegister<FpuRegister>());
4565 }
4566 __ MovD(dst_reg, fcond_reg);
4567 }
4568 break;
4569 }
4570 }
4571}
4572
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004573void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4574 LocationSummary* locations = new (GetGraph()->GetArena())
4575 LocationSummary(flag, LocationSummary::kNoCall);
4576 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004577}
4578
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004579void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4580 __ LoadFromOffset(kLoadWord,
4581 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4582 SP,
4583 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004584}
4585
David Brazdil74eb1b22015-12-14 11:44:01 +00004586void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
4587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004588 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004589}
4590
4591void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004592 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4593 GenConditionalMove(select);
4594 } else {
4595 LocationSummary* locations = select->GetLocations();
4596 Mips64Label false_target;
4597 GenerateTestAndBranch(select,
4598 /* condition_input_index */ 2,
4599 /* true_target */ nullptr,
4600 &false_target);
4601 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4602 __ Bind(&false_target);
4603 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004604}
4605
David Srbecky0cf44932015-12-09 14:09:59 +00004606void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4607 new (GetGraph()->GetArena()) LocationSummary(info);
4608}
4609
David Srbeckyd28f4a02016-03-14 17:14:24 +00004610void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4611 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004612}
4613
4614void CodeGeneratorMIPS64::GenerateNop() {
4615 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004616}
4617
Alexey Frunze4dda3372015-06-01 18:31:49 -07004618void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004619 const FieldInfo& field_info) {
4620 Primitive::Type field_type = field_info.GetFieldType();
4621 bool object_field_get_with_read_barrier =
4622 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
4623 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4624 instruction,
4625 object_field_get_with_read_barrier
4626 ? LocationSummary::kCallOnSlowPath
4627 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004628 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4629 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4630 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004631 locations->SetInAt(0, Location::RequiresRegister());
4632 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4633 locations->SetOut(Location::RequiresFpuRegister());
4634 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004635 // The output overlaps in the case of an object field get with
4636 // read barriers enabled: we do not want the move to overwrite the
4637 // object's location, as we need it to emit the read barrier.
4638 locations->SetOut(Location::RequiresRegister(),
4639 object_field_get_with_read_barrier
4640 ? Location::kOutputOverlap
4641 : Location::kNoOutputOverlap);
4642 }
4643 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4644 // We need a temporary register for the read barrier marking slow
4645 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004646 if (!kBakerReadBarrierThunksEnableForFields) {
4647 locations->AddTemp(Location::RequiresRegister());
4648 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004649 }
4650}
4651
4652void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4653 const FieldInfo& field_info) {
4654 Primitive::Type type = field_info.GetFieldType();
4655 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004656 Location obj_loc = locations->InAt(0);
4657 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4658 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004659 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004660 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004661 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004662 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4663
Alexey Frunze4dda3372015-06-01 18:31:49 -07004664 switch (type) {
4665 case Primitive::kPrimBoolean:
4666 load_type = kLoadUnsignedByte;
4667 break;
4668 case Primitive::kPrimByte:
4669 load_type = kLoadSignedByte;
4670 break;
4671 case Primitive::kPrimShort:
4672 load_type = kLoadSignedHalfword;
4673 break;
4674 case Primitive::kPrimChar:
4675 load_type = kLoadUnsignedHalfword;
4676 break;
4677 case Primitive::kPrimInt:
4678 case Primitive::kPrimFloat:
4679 load_type = kLoadWord;
4680 break;
4681 case Primitive::kPrimLong:
4682 case Primitive::kPrimDouble:
4683 load_type = kLoadDoubleword;
4684 break;
4685 case Primitive::kPrimNot:
4686 load_type = kLoadUnsignedWord;
4687 break;
4688 case Primitive::kPrimVoid:
4689 LOG(FATAL) << "Unreachable type " << type;
4690 UNREACHABLE();
4691 }
4692 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004693 DCHECK(dst_loc.IsRegister());
4694 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4695 if (type == Primitive::kPrimNot) {
4696 // /* HeapReference<Object> */ dst = *(obj + offset)
4697 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004698 Location temp_loc =
4699 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004700 // Note that a potential implicit null check is handled in this
4701 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4702 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4703 dst_loc,
4704 obj,
4705 offset,
4706 temp_loc,
4707 /* needs_null_check */ true);
4708 if (is_volatile) {
4709 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4710 }
4711 } else {
4712 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4713 if (is_volatile) {
4714 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4715 }
4716 // If read barriers are enabled, emit read barriers other than
4717 // Baker's using a slow path (and also unpoison the loaded
4718 // reference, if heap poisoning is enabled).
4719 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4720 }
4721 } else {
4722 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4723 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004724 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004725 DCHECK(dst_loc.IsFpuRegister());
4726 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004727 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004728 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004729
Alexey Frunze15958152017-02-09 19:08:30 -08004730 // Memory barriers, in the case of references, are handled in the
4731 // previous switch statement.
4732 if (is_volatile && (type != Primitive::kPrimNot)) {
4733 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004734 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004735}
4736
4737void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4738 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4739 LocationSummary* locations =
4740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4741 locations->SetInAt(0, Location::RequiresRegister());
4742 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004743 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004744 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004745 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004746 }
4747}
4748
4749void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004750 const FieldInfo& field_info,
4751 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004752 Primitive::Type type = field_info.GetFieldType();
4753 LocationSummary* locations = instruction->GetLocations();
4754 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004755 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004756 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004757 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004758 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4759 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004760 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4761
Alexey Frunze4dda3372015-06-01 18:31:49 -07004762 switch (type) {
4763 case Primitive::kPrimBoolean:
4764 case Primitive::kPrimByte:
4765 store_type = kStoreByte;
4766 break;
4767 case Primitive::kPrimShort:
4768 case Primitive::kPrimChar:
4769 store_type = kStoreHalfword;
4770 break;
4771 case Primitive::kPrimInt:
4772 case Primitive::kPrimFloat:
4773 case Primitive::kPrimNot:
4774 store_type = kStoreWord;
4775 break;
4776 case Primitive::kPrimLong:
4777 case Primitive::kPrimDouble:
4778 store_type = kStoreDoubleword;
4779 break;
4780 case Primitive::kPrimVoid:
4781 LOG(FATAL) << "Unreachable type " << type;
4782 UNREACHABLE();
4783 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004784
Alexey Frunze15958152017-02-09 19:08:30 -08004785 if (is_volatile) {
4786 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4787 }
4788
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004789 if (value_location.IsConstant()) {
4790 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4791 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4792 } else {
4793 if (!Primitive::IsFloatingPointType(type)) {
4794 DCHECK(value_location.IsRegister());
4795 GpuRegister src = value_location.AsRegister<GpuRegister>();
4796 if (kPoisonHeapReferences && needs_write_barrier) {
4797 // Note that in the case where `value` is a null reference,
4798 // we do not enter this block, as a null reference does not
4799 // need poisoning.
4800 DCHECK_EQ(type, Primitive::kPrimNot);
4801 __ PoisonHeapReference(TMP, src);
4802 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4803 } else {
4804 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4805 }
4806 } else {
4807 DCHECK(value_location.IsFpuRegister());
4808 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4809 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4810 }
4811 }
Alexey Frunze15958152017-02-09 19:08:30 -08004812
Alexey Frunzec061de12017-02-14 13:27:23 -08004813 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004814 DCHECK(value_location.IsRegister());
4815 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004816 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004817 }
Alexey Frunze15958152017-02-09 19:08:30 -08004818
4819 if (is_volatile) {
4820 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4821 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004822}
4823
4824void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4825 HandleFieldGet(instruction, instruction->GetFieldInfo());
4826}
4827
4828void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4829 HandleFieldGet(instruction, instruction->GetFieldInfo());
4830}
4831
4832void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4833 HandleFieldSet(instruction, instruction->GetFieldInfo());
4834}
4835
4836void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004837 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838}
4839
Alexey Frunze15958152017-02-09 19:08:30 -08004840void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4841 HInstruction* instruction,
4842 Location out,
4843 uint32_t offset,
4844 Location maybe_temp,
4845 ReadBarrierOption read_barrier_option) {
4846 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4847 if (read_barrier_option == kWithReadBarrier) {
4848 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004849 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4850 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4851 }
Alexey Frunze15958152017-02-09 19:08:30 -08004852 if (kUseBakerReadBarrier) {
4853 // Load with fast path based Baker's read barrier.
4854 // /* HeapReference<Object> */ out = *(out + offset)
4855 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4856 out,
4857 out_reg,
4858 offset,
4859 maybe_temp,
4860 /* needs_null_check */ false);
4861 } else {
4862 // Load with slow path based read barrier.
4863 // Save the value of `out` into `maybe_temp` before overwriting it
4864 // in the following move operation, as we will need it for the
4865 // read barrier below.
4866 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4867 // /* HeapReference<Object> */ out = *(out + offset)
4868 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4869 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4870 }
4871 } else {
4872 // Plain load with no read barrier.
4873 // /* HeapReference<Object> */ out = *(out + offset)
4874 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4875 __ MaybeUnpoisonHeapReference(out_reg);
4876 }
4877}
4878
4879void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4880 HInstruction* instruction,
4881 Location out,
4882 Location obj,
4883 uint32_t offset,
4884 Location maybe_temp,
4885 ReadBarrierOption read_barrier_option) {
4886 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4887 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4888 if (read_barrier_option == kWithReadBarrier) {
4889 CHECK(kEmitCompilerReadBarrier);
4890 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004891 if (!kBakerReadBarrierThunksEnableForFields) {
4892 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4893 }
Alexey Frunze15958152017-02-09 19:08:30 -08004894 // Load with fast path based Baker's read barrier.
4895 // /* HeapReference<Object> */ out = *(obj + offset)
4896 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4897 out,
4898 obj_reg,
4899 offset,
4900 maybe_temp,
4901 /* needs_null_check */ false);
4902 } else {
4903 // Load with slow path based read barrier.
4904 // /* HeapReference<Object> */ out = *(obj + offset)
4905 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4906 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4907 }
4908 } else {
4909 // Plain load with no read barrier.
4910 // /* HeapReference<Object> */ out = *(obj + offset)
4911 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4912 __ MaybeUnpoisonHeapReference(out_reg);
4913 }
4914}
4915
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004916static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4917 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4918 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4919 return reg - V0;
4920 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4921 return 13 + (reg - S2);
4922 } else if (reg == S8) { // One more.
4923 return 19;
4924 }
4925 LOG(FATAL) << "Unexpected register " << reg;
4926 UNREACHABLE();
4927}
4928
4929static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4930 int num = GetBakerMarkThunkNumber(reg) +
4931 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4932 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4933}
4934
4935static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4936 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4937 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4938}
4939
4940void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4941 Location root,
4942 GpuRegister obj,
4943 uint32_t offset,
4944 ReadBarrierOption read_barrier_option,
4945 Mips64Label* label_low) {
4946 if (label_low != nullptr) {
4947 DCHECK_EQ(offset, 0x5678u);
4948 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004949 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004950 if (read_barrier_option == kWithReadBarrier) {
4951 DCHECK(kEmitCompilerReadBarrier);
4952 if (kUseBakerReadBarrier) {
4953 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4954 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004955 if (kBakerReadBarrierThunksEnableForGcRoots) {
4956 // Note that we do not actually check the value of `GetIsGcMarking()`
4957 // to decide whether to mark the loaded GC root or not. Instead, we
4958 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4959 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4960 // vice versa.
4961 //
4962 // We use thunks for the slow path. That thunk checks the reference
4963 // and jumps to the entrypoint if needed.
4964 //
4965 // temp = Thread::Current()->pReadBarrierMarkReg00
4966 // // AKA &art_quick_read_barrier_mark_introspection.
4967 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4968 // if (temp != nullptr) {
4969 // temp = &gc_root_thunk<root_reg>
4970 // root = temp(root)
4971 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004972
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004973 const int32_t entry_point_offset =
4974 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4975 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4976 int16_t offset_low = Low16Bits(offset);
4977 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
4978 // extension in lwu.
4979 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4980 GpuRegister base = short_offset ? obj : TMP;
4981 // Loading the entrypoint does not require a load acquire since it is only changed when
4982 // threads are suspended or running a checkpoint.
4983 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4984 if (!short_offset) {
4985 DCHECK(!label_low);
4986 __ Daui(base, obj, offset_high);
4987 }
4988 __ Beqz(T9, 2); // Skip jialc.
4989 if (label_low != nullptr) {
4990 DCHECK(short_offset);
4991 __ Bind(label_low);
4992 }
4993 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4994 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
4995 // in delay slot.
4996 __ Jialc(T9, thunk_disp);
4997 } else {
4998 // Note that we do not actually check the value of `GetIsGcMarking()`
4999 // to decide whether to mark the loaded GC root or not. Instead, we
5000 // load into `temp` (T9) the read barrier mark entry point corresponding
5001 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5002 // is false, and vice versa.
5003 //
5004 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5005 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5006 // if (temp != null) {
5007 // root = temp(root)
5008 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005009
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005010 if (label_low != nullptr) {
5011 __ Bind(label_low);
5012 }
5013 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5014 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5015 static_assert(
5016 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5017 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5018 "have different sizes.");
5019 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5020 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5021 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005022
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005023 // Slow path marking the GC root `root`.
5024 Location temp = Location::RegisterLocation(T9);
5025 SlowPathCodeMIPS64* slow_path =
5026 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
5027 instruction,
5028 root,
5029 /*entrypoint*/ temp);
5030 codegen_->AddSlowPath(slow_path);
5031
5032 const int32_t entry_point_offset =
5033 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5034 // Loading the entrypoint does not require a load acquire since it is only changed when
5035 // threads are suspended or running a checkpoint.
5036 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5037 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5038 __ Bind(slow_path->GetExitLabel());
5039 }
Alexey Frunze15958152017-02-09 19:08:30 -08005040 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005041 if (label_low != nullptr) {
5042 __ Bind(label_low);
5043 }
Alexey Frunze15958152017-02-09 19:08:30 -08005044 // GC root loaded through a slow path for read barriers other
5045 // than Baker's.
5046 // /* GcRoot<mirror::Object>* */ root = obj + offset
5047 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5048 // /* mirror::Object* */ root = root->Read()
5049 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5050 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005051 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005052 if (label_low != nullptr) {
5053 __ Bind(label_low);
5054 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005055 // Plain GC root load with no read barrier.
5056 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5057 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5058 // Note that GC roots are not affected by heap poisoning, thus we
5059 // do not have to unpoison `root_reg` here.
5060 }
5061}
5062
Alexey Frunze15958152017-02-09 19:08:30 -08005063void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5064 Location ref,
5065 GpuRegister obj,
5066 uint32_t offset,
5067 Location temp,
5068 bool needs_null_check) {
5069 DCHECK(kEmitCompilerReadBarrier);
5070 DCHECK(kUseBakerReadBarrier);
5071
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005072 if (kBakerReadBarrierThunksEnableForFields) {
5073 // Note that we do not actually check the value of `GetIsGcMarking()`
5074 // to decide whether to mark the loaded reference or not. Instead, we
5075 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5076 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5077 // vice versa.
5078 //
5079 // We use thunks for the slow path. That thunk checks the reference
5080 // and jumps to the entrypoint if needed. If the holder is not gray,
5081 // it issues a load-load memory barrier and returns to the original
5082 // reference load.
5083 //
5084 // temp = Thread::Current()->pReadBarrierMarkReg00
5085 // // AKA &art_quick_read_barrier_mark_introspection.
5086 // if (temp != nullptr) {
5087 // temp = &field_array_thunk<holder_reg>
5088 // temp()
5089 // }
5090 // not_gray_return_address:
5091 // // If the offset is too large to fit into the lw instruction, we
5092 // // use an adjusted base register (TMP) here. This register
5093 // // receives bits 16 ... 31 of the offset before the thunk invocation
5094 // // and the thunk benefits from it.
5095 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5096 // gray_return_address:
5097
5098 DCHECK(temp.IsInvalid());
5099 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5100 const int32_t entry_point_offset =
5101 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5102 // There may have or may have not been a null check if the field offset is smaller than
5103 // the page size.
5104 // There must've been a null check in case it's actually a load from an array.
5105 // We will, however, perform an explicit null check in the thunk as it's easier to
5106 // do it than not.
5107 if (instruction->IsArrayGet()) {
5108 DCHECK(!needs_null_check);
5109 }
5110 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5111 // Loading the entrypoint does not require a load acquire since it is only changed when
5112 // threads are suspended or running a checkpoint.
5113 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5114 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5115 if (short_offset) {
5116 __ Beqzc(T9, 2); // Skip jialc.
5117 __ Nop(); // In forbidden slot.
5118 __ Jialc(T9, thunk_disp);
5119 // /* HeapReference<Object> */ ref = *(obj + offset)
5120 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5121 } else {
5122 int16_t offset_low = Low16Bits(offset);
5123 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
5124 __ Beqz(T9, 2); // Skip jialc.
5125 __ Daui(TMP, obj, offset_high); // In delay slot.
5126 __ Jialc(T9, thunk_disp);
5127 // /* HeapReference<Object> */ ref = *(obj + offset)
5128 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5129 }
5130 if (needs_null_check) {
5131 MaybeRecordImplicitNullCheck(instruction);
5132 }
5133 __ MaybeUnpoisonHeapReference(ref_reg);
5134 return;
5135 }
5136
Alexey Frunze15958152017-02-09 19:08:30 -08005137 // /* HeapReference<Object> */ ref = *(obj + offset)
5138 Location no_index = Location::NoLocation();
5139 ScaleFactor no_scale_factor = TIMES_1;
5140 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5141 ref,
5142 obj,
5143 offset,
5144 no_index,
5145 no_scale_factor,
5146 temp,
5147 needs_null_check);
5148}
5149
5150void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5151 Location ref,
5152 GpuRegister obj,
5153 uint32_t data_offset,
5154 Location index,
5155 Location temp,
5156 bool needs_null_check) {
5157 DCHECK(kEmitCompilerReadBarrier);
5158 DCHECK(kUseBakerReadBarrier);
5159
5160 static_assert(
5161 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5162 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005163 ScaleFactor scale_factor = TIMES_4;
5164
5165 if (kBakerReadBarrierThunksEnableForArrays) {
5166 // Note that we do not actually check the value of `GetIsGcMarking()`
5167 // to decide whether to mark the loaded reference or not. Instead, we
5168 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5169 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5170 // vice versa.
5171 //
5172 // We use thunks for the slow path. That thunk checks the reference
5173 // and jumps to the entrypoint if needed. If the holder is not gray,
5174 // it issues a load-load memory barrier and returns to the original
5175 // reference load.
5176 //
5177 // temp = Thread::Current()->pReadBarrierMarkReg00
5178 // // AKA &art_quick_read_barrier_mark_introspection.
5179 // if (temp != nullptr) {
5180 // temp = &field_array_thunk<holder_reg>
5181 // temp()
5182 // }
5183 // not_gray_return_address:
5184 // // The element address is pre-calculated in the TMP register before the
5185 // // thunk invocation and the thunk benefits from it.
5186 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5187 // gray_return_address:
5188
5189 DCHECK(temp.IsInvalid());
5190 DCHECK(index.IsValid());
5191 const int32_t entry_point_offset =
5192 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5193 // We will not do the explicit null check in the thunk as some form of a null check
5194 // must've been done earlier.
5195 DCHECK(!needs_null_check);
5196 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5197 // Loading the entrypoint does not require a load acquire since it is only changed when
5198 // threads are suspended or running a checkpoint.
5199 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5200 __ Beqz(T9, 2); // Skip jialc.
5201 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5202 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5203 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5204 __ Jialc(T9, thunk_disp);
5205 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5206 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5207 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5208 __ MaybeUnpoisonHeapReference(ref_reg);
5209 return;
5210 }
5211
Alexey Frunze15958152017-02-09 19:08:30 -08005212 // /* HeapReference<Object> */ ref =
5213 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005214 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5215 ref,
5216 obj,
5217 data_offset,
5218 index,
5219 scale_factor,
5220 temp,
5221 needs_null_check);
5222}
5223
5224void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5225 Location ref,
5226 GpuRegister obj,
5227 uint32_t offset,
5228 Location index,
5229 ScaleFactor scale_factor,
5230 Location temp,
5231 bool needs_null_check,
5232 bool always_update_field) {
5233 DCHECK(kEmitCompilerReadBarrier);
5234 DCHECK(kUseBakerReadBarrier);
5235
5236 // In slow path based read barriers, the read barrier call is
5237 // inserted after the original load. However, in fast path based
5238 // Baker's read barriers, we need to perform the load of
5239 // mirror::Object::monitor_ *before* the original reference load.
5240 // This load-load ordering is required by the read barrier.
5241 // The fast path/slow path (for Baker's algorithm) should look like:
5242 //
5243 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5244 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5245 // HeapReference<Object> ref = *src; // Original reference load.
5246 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5247 // if (is_gray) {
5248 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5249 // }
5250 //
5251 // Note: the original implementation in ReadBarrier::Barrier is
5252 // slightly more complex as it performs additional checks that we do
5253 // not do here for performance reasons.
5254
5255 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5256 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5257 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5258
5259 // /* int32_t */ monitor = obj->monitor_
5260 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5261 if (needs_null_check) {
5262 MaybeRecordImplicitNullCheck(instruction);
5263 }
5264 // /* LockWord */ lock_word = LockWord(monitor)
5265 static_assert(sizeof(LockWord) == sizeof(int32_t),
5266 "art::LockWord and int32_t have different sizes.");
5267
5268 __ Sync(0); // Barrier to prevent load-load reordering.
5269
5270 // The actual reference load.
5271 if (index.IsValid()) {
5272 // Load types involving an "index": ArrayGet,
5273 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5274 // intrinsics.
5275 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5276 if (index.IsConstant()) {
5277 size_t computed_offset =
5278 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5279 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5280 } else {
5281 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005282 if (scale_factor == TIMES_1) {
5283 __ Daddu(TMP, index_reg, obj);
5284 } else {
5285 __ Dlsa(TMP, index_reg, obj, scale_factor);
5286 }
Alexey Frunze15958152017-02-09 19:08:30 -08005287 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5288 }
5289 } else {
5290 // /* HeapReference<Object> */ ref = *(obj + offset)
5291 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5292 }
5293
5294 // Object* ref = ref_addr->AsMirrorPtr()
5295 __ MaybeUnpoisonHeapReference(ref_reg);
5296
5297 // Slow path marking the object `ref` when it is gray.
5298 SlowPathCodeMIPS64* slow_path;
5299 if (always_update_field) {
5300 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5301 // of the form `obj + field_offset`, where `obj` is a register and
5302 // `field_offset` is a register. Thus `offset` and `scale_factor`
5303 // above are expected to be null in this code path.
5304 DCHECK_EQ(offset, 0u);
5305 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
5306 slow_path = new (GetGraph()->GetArena())
5307 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5308 ref,
5309 obj,
5310 /* field_offset */ index,
5311 temp_reg);
5312 } else {
5313 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
5314 }
5315 AddSlowPath(slow_path);
5316
5317 // if (rb_state == ReadBarrier::GrayState())
5318 // ref = ReadBarrier::Mark(ref);
5319 // Given the numeric representation, it's enough to check the low bit of the
5320 // rb_state. We do that by shifting the bit into the sign bit (31) and
5321 // performing a branch on less than zero.
5322 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5323 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5324 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5325 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5326 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5327 __ Bind(slow_path->GetExitLabel());
5328}
5329
5330void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5331 Location out,
5332 Location ref,
5333 Location obj,
5334 uint32_t offset,
5335 Location index) {
5336 DCHECK(kEmitCompilerReadBarrier);
5337
5338 // Insert a slow path based read barrier *after* the reference load.
5339 //
5340 // If heap poisoning is enabled, the unpoisoning of the loaded
5341 // reference will be carried out by the runtime within the slow
5342 // path.
5343 //
5344 // Note that `ref` currently does not get unpoisoned (when heap
5345 // poisoning is enabled), which is alright as the `ref` argument is
5346 // not used by the artReadBarrierSlow entry point.
5347 //
5348 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5349 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
5350 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5351 AddSlowPath(slow_path);
5352
5353 __ Bc(slow_path->GetEntryLabel());
5354 __ Bind(slow_path->GetExitLabel());
5355}
5356
5357void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5358 Location out,
5359 Location ref,
5360 Location obj,
5361 uint32_t offset,
5362 Location index) {
5363 if (kEmitCompilerReadBarrier) {
5364 // Baker's read barriers shall be handled by the fast path
5365 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5366 DCHECK(!kUseBakerReadBarrier);
5367 // If heap poisoning is enabled, unpoisoning will be taken care of
5368 // by the runtime within the slow path.
5369 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5370 } else if (kPoisonHeapReferences) {
5371 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5372 }
5373}
5374
5375void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5376 Location out,
5377 Location root) {
5378 DCHECK(kEmitCompilerReadBarrier);
5379
5380 // Insert a slow path based read barrier *after* the GC root load.
5381 //
5382 // Note that GC roots are not affected by heap poisoning, so we do
5383 // not need to do anything special for this here.
5384 SlowPathCodeMIPS64* slow_path =
5385 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
5386 AddSlowPath(slow_path);
5387
5388 __ Bc(slow_path->GetEntryLabel());
5389 __ Bind(slow_path->GetExitLabel());
5390}
5391
Alexey Frunze4dda3372015-06-01 18:31:49 -07005392void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005393 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5394 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005395 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005396 switch (type_check_kind) {
5397 case TypeCheckKind::kExactCheck:
5398 case TypeCheckKind::kAbstractClassCheck:
5399 case TypeCheckKind::kClassHierarchyCheck:
5400 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005401 call_kind =
5402 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005403 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005404 break;
5405 case TypeCheckKind::kArrayCheck:
5406 case TypeCheckKind::kUnresolvedCheck:
5407 case TypeCheckKind::kInterfaceCheck:
5408 call_kind = LocationSummary::kCallOnSlowPath;
5409 break;
5410 }
5411
Alexey Frunze4dda3372015-06-01 18:31:49 -07005412 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005413 if (baker_read_barrier_slow_path) {
5414 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5415 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005416 locations->SetInAt(0, Location::RequiresRegister());
5417 locations->SetInAt(1, Location::RequiresRegister());
5418 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005419 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005420 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005421 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005422}
5423
5424void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005425 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005426 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005427 Location obj_loc = locations->InAt(0);
5428 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005429 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005430 Location out_loc = locations->Out();
5431 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5432 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5433 DCHECK_LE(num_temps, 1u);
5434 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005435 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5436 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5437 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5438 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005439 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005440 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005441
5442 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005443 // Avoid this check if we know `obj` is not null.
5444 if (instruction->MustDoNullCheck()) {
5445 __ Move(out, ZERO);
5446 __ Beqzc(obj, &done);
5447 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005448
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005449 switch (type_check_kind) {
5450 case TypeCheckKind::kExactCheck: {
5451 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005452 GenerateReferenceLoadTwoRegisters(instruction,
5453 out_loc,
5454 obj_loc,
5455 class_offset,
5456 maybe_temp_loc,
5457 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005458 // Classes must be equal for the instanceof to succeed.
5459 __ Xor(out, out, cls);
5460 __ Sltiu(out, out, 1);
5461 break;
5462 }
5463
5464 case TypeCheckKind::kAbstractClassCheck: {
5465 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005466 GenerateReferenceLoadTwoRegisters(instruction,
5467 out_loc,
5468 obj_loc,
5469 class_offset,
5470 maybe_temp_loc,
5471 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005472 // If the class is abstract, we eagerly fetch the super class of the
5473 // object to avoid doing a comparison we know will fail.
5474 Mips64Label loop;
5475 __ Bind(&loop);
5476 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005477 GenerateReferenceLoadOneRegister(instruction,
5478 out_loc,
5479 super_offset,
5480 maybe_temp_loc,
5481 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005482 // If `out` is null, we use it for the result, and jump to `done`.
5483 __ Beqzc(out, &done);
5484 __ Bnec(out, cls, &loop);
5485 __ LoadConst32(out, 1);
5486 break;
5487 }
5488
5489 case TypeCheckKind::kClassHierarchyCheck: {
5490 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005491 GenerateReferenceLoadTwoRegisters(instruction,
5492 out_loc,
5493 obj_loc,
5494 class_offset,
5495 maybe_temp_loc,
5496 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005497 // Walk over the class hierarchy to find a match.
5498 Mips64Label loop, success;
5499 __ Bind(&loop);
5500 __ Beqc(out, cls, &success);
5501 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005502 GenerateReferenceLoadOneRegister(instruction,
5503 out_loc,
5504 super_offset,
5505 maybe_temp_loc,
5506 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005507 __ Bnezc(out, &loop);
5508 // If `out` is null, we use it for the result, and jump to `done`.
5509 __ Bc(&done);
5510 __ Bind(&success);
5511 __ LoadConst32(out, 1);
5512 break;
5513 }
5514
5515 case TypeCheckKind::kArrayObjectCheck: {
5516 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005517 GenerateReferenceLoadTwoRegisters(instruction,
5518 out_loc,
5519 obj_loc,
5520 class_offset,
5521 maybe_temp_loc,
5522 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005523 // Do an exact check.
5524 Mips64Label success;
5525 __ Beqc(out, cls, &success);
5526 // Otherwise, we need to check that the object's class is a non-primitive array.
5527 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005528 GenerateReferenceLoadOneRegister(instruction,
5529 out_loc,
5530 component_offset,
5531 maybe_temp_loc,
5532 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005533 // If `out` is null, we use it for the result, and jump to `done`.
5534 __ Beqzc(out, &done);
5535 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5536 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5537 __ Sltiu(out, out, 1);
5538 __ Bc(&done);
5539 __ Bind(&success);
5540 __ LoadConst32(out, 1);
5541 break;
5542 }
5543
5544 case TypeCheckKind::kArrayCheck: {
5545 // No read barrier since the slow path will retry upon failure.
5546 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005547 GenerateReferenceLoadTwoRegisters(instruction,
5548 out_loc,
5549 obj_loc,
5550 class_offset,
5551 maybe_temp_loc,
5552 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005553 DCHECK(locations->OnlyCallsOnSlowPath());
5554 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5555 /* is_fatal */ false);
5556 codegen_->AddSlowPath(slow_path);
5557 __ Bnec(out, cls, slow_path->GetEntryLabel());
5558 __ LoadConst32(out, 1);
5559 break;
5560 }
5561
5562 case TypeCheckKind::kUnresolvedCheck:
5563 case TypeCheckKind::kInterfaceCheck: {
5564 // Note that we indeed only call on slow path, but we always go
5565 // into the slow path for the unresolved and interface check
5566 // cases.
5567 //
5568 // We cannot directly call the InstanceofNonTrivial runtime
5569 // entry point without resorting to a type checking slow path
5570 // here (i.e. by calling InvokeRuntime directly), as it would
5571 // require to assign fixed registers for the inputs of this
5572 // HInstanceOf instruction (following the runtime calling
5573 // convention), which might be cluttered by the potential first
5574 // read barrier emission at the beginning of this method.
5575 //
5576 // TODO: Introduce a new runtime entry point taking the object
5577 // to test (instead of its class) as argument, and let it deal
5578 // with the read barrier issues. This will let us refactor this
5579 // case of the `switch` code as it was previously (with a direct
5580 // call to the runtime not using a type checking slow path).
5581 // This should also be beneficial for the other cases above.
5582 DCHECK(locations->OnlyCallsOnSlowPath());
5583 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5584 /* is_fatal */ false);
5585 codegen_->AddSlowPath(slow_path);
5586 __ Bc(slow_path->GetEntryLabel());
5587 break;
5588 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005589 }
5590
5591 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005592
5593 if (slow_path != nullptr) {
5594 __ Bind(slow_path->GetExitLabel());
5595 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005596}
5597
5598void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
5599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5600 locations->SetOut(Location::ConstantLocation(constant));
5601}
5602
5603void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5604 // Will be generated at use site.
5605}
5606
5607void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
5608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5609 locations->SetOut(Location::ConstantLocation(constant));
5610}
5611
5612void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5613 // Will be generated at use site.
5614}
5615
Calin Juravle175dc732015-08-25 15:42:32 +01005616void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5617 // The trampoline uses the same calling convention as dex calling conventions,
5618 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5619 // the method_idx.
5620 HandleInvoke(invoke);
5621}
5622
5623void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5624 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5625}
5626
Alexey Frunze4dda3372015-06-01 18:31:49 -07005627void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5628 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5629 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5630}
5631
5632void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5633 HandleInvoke(invoke);
5634 // The register T0 is required to be used for the hidden argument in
5635 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5636 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5637}
5638
5639void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5640 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5641 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005642 Location receiver = invoke->GetLocations()->InAt(0);
5643 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005644 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005645
5646 // Set the hidden argument.
5647 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5648 invoke->GetDexMethodIndex());
5649
5650 // temp = object->GetClass();
5651 if (receiver.IsStackSlot()) {
5652 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5653 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5654 } else {
5655 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5656 }
5657 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005658 // Instead of simply (possibly) unpoisoning `temp` here, we should
5659 // emit a read barrier for the previous class reference load.
5660 // However this is not required in practice, as this is an
5661 // intermediate/temporary reference and because the current
5662 // concurrent copying collector keeps the from-space memory
5663 // intact/accessible until the end of the marking phase (the
5664 // concurrent copying collector may not in the future).
5665 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005666 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5667 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5668 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005669 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005670 // temp = temp->GetImtEntryAt(method_offset);
5671 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5672 // T9 = temp->GetEntryPoint();
5673 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5674 // T9();
5675 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005676 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005677 DCHECK(!codegen_->IsLeafMethod());
5678 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5679}
5680
5681void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005682 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5683 if (intrinsic.TryDispatch(invoke)) {
5684 return;
5685 }
5686
Alexey Frunze4dda3372015-06-01 18:31:49 -07005687 HandleInvoke(invoke);
5688}
5689
5690void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005691 // Explicit clinit checks triggered by static invokes must have been pruned by
5692 // art::PrepareForRegisterAllocation.
5693 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005694
Chris Larsen3039e382015-08-26 07:54:08 -07005695 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5696 if (intrinsic.TryDispatch(invoke)) {
5697 return;
5698 }
5699
Alexey Frunze4dda3372015-06-01 18:31:49 -07005700 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005701}
5702
Orion Hodsonac141392017-01-13 11:53:47 +00005703void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5704 HandleInvoke(invoke);
5705}
5706
5707void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5708 codegen_->GenerateInvokePolymorphicCall(invoke);
5709}
5710
Chris Larsen3039e382015-08-26 07:54:08 -07005711static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005712 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005713 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5714 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005715 return true;
5716 }
5717 return false;
5718}
5719
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005720HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005721 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005722 bool fallback_load = false;
5723 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005724 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005725 case HLoadString::LoadKind::kBssEntry:
5726 DCHECK(!Runtime::Current()->UseJitCompilation());
5727 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005728 case HLoadString::LoadKind::kJitTableAddress:
5729 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005730 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005731 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005732 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005733 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005734 }
5735 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005736 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005737 }
5738 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005739}
5740
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005741HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5742 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005743 bool fallback_load = false;
5744 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005745 case HLoadClass::LoadKind::kInvalid:
5746 LOG(FATAL) << "UNREACHABLE";
5747 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005748 case HLoadClass::LoadKind::kReferrersClass:
5749 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005750 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005751 case HLoadClass::LoadKind::kBssEntry:
5752 DCHECK(!Runtime::Current()->UseJitCompilation());
5753 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005754 case HLoadClass::LoadKind::kJitTableAddress:
5755 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005756 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005757 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005758 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005759 break;
5760 }
5761 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005762 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005763 }
5764 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005765}
5766
Vladimir Markodc151b22015-10-15 18:02:30 +01005767HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5768 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005769 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005770 // On MIPS64 we support all dispatch types.
5771 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005772}
5773
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005774void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5775 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005776 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005777 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005778 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5779 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5780
Alexey Frunze19f6c692016-11-30 19:19:55 -08005781 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005782 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005783 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005784 uint32_t offset =
5785 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005786 __ LoadFromOffset(kLoadDoubleword,
5787 temp.AsRegister<GpuRegister>(),
5788 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005789 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005790 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005791 }
Vladimir Marko58155012015-08-19 12:49:41 +00005792 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005793 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005794 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005795 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5796 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005797 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005798 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005799 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5800 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5801 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005802 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5803 break;
5804 }
Vladimir Marko58155012015-08-19 12:49:41 +00005805 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005806 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5807 kLoadDoubleword,
5808 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005809 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005810 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005811 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005812 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005813 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5814 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5815 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005816 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5817 break;
5818 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005819 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5820 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5821 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005822 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005823 }
5824
Alexey Frunze19f6c692016-11-30 19:19:55 -08005825 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005826 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005827 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005828 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005829 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5830 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5831 __ LoadFromOffset(kLoadDoubleword,
5832 T9,
5833 callee_method.AsRegister<GpuRegister>(),
5834 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005835 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005836 // T9()
5837 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005838 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005839 break;
5840 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005841 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5842
Alexey Frunze4dda3372015-06-01 18:31:49 -07005843 DCHECK(!IsLeafMethod());
5844}
5845
5846void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005847 // Explicit clinit checks triggered by static invokes must have been pruned by
5848 // art::PrepareForRegisterAllocation.
5849 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005850
5851 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5852 return;
5853 }
5854
5855 LocationSummary* locations = invoke->GetLocations();
5856 codegen_->GenerateStaticOrDirectCall(invoke,
5857 locations->HasTemps()
5858 ? locations->GetTemp(0)
5859 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005860}
5861
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005862void CodeGeneratorMIPS64::GenerateVirtualCall(
5863 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005864 // Use the calling convention instead of the location of the receiver, as
5865 // intrinsics may have put the receiver in a different register. In the intrinsics
5866 // slow path, the arguments have been moved to the right place, so here we are
5867 // guaranteed that the receiver is the first register of the calling convention.
5868 InvokeDexCallingConvention calling_convention;
5869 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5870
Alexey Frunze53afca12015-11-05 16:34:23 -08005871 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005872 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5873 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5874 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005875 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005876
5877 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005878 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005879 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005880 // Instead of simply (possibly) unpoisoning `temp` here, we should
5881 // emit a read barrier for the previous class reference load.
5882 // However this is not required in practice, as this is an
5883 // intermediate/temporary reference and because the current
5884 // concurrent copying collector keeps the from-space memory
5885 // intact/accessible until the end of the marking phase (the
5886 // concurrent copying collector may not in the future).
5887 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005888 // temp = temp->GetMethodAt(method_offset);
5889 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5890 // T9 = temp->GetEntryPoint();
5891 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5892 // T9();
5893 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005894 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005895 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005896}
5897
5898void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5899 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5900 return;
5901 }
5902
5903 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005904 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005905}
5906
5907void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005908 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005909 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005910 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005911 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5912 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005913 return;
5914 }
Vladimir Marko41559982017-01-06 14:04:23 +00005915 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005916
Alexey Frunze15958152017-02-09 19:08:30 -08005917 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5918 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005919 ? LocationSummary::kCallOnSlowPath
5920 : LocationSummary::kNoCall;
5921 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005922 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5923 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5924 }
Vladimir Marko41559982017-01-06 14:04:23 +00005925 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005926 locations->SetInAt(0, Location::RequiresRegister());
5927 }
5928 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005929 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5930 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5931 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005932 // Request a temp to hold the BSS entry location for the slow path.
5933 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005934 RegisterSet caller_saves = RegisterSet::Empty();
5935 InvokeRuntimeCallingConvention calling_convention;
5936 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5937 locations->SetCustomSlowPathCallerSaves(caller_saves);
5938 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005939 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005940 }
5941 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005942}
5943
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005944// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5945// move.
5946void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005947 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005948 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005949 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005950 return;
5951 }
Vladimir Marko41559982017-01-06 14:04:23 +00005952 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005953
Vladimir Marko41559982017-01-06 14:04:23 +00005954 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005955 Location out_loc = locations->Out();
5956 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5957 GpuRegister current_method_reg = ZERO;
5958 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005959 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005960 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5961 }
5962
Alexey Frunze15958152017-02-09 19:08:30 -08005963 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5964 ? kWithoutReadBarrier
5965 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005966 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005967 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005968 switch (load_kind) {
5969 case HLoadClass::LoadKind::kReferrersClass:
5970 DCHECK(!cls->CanCallRuntime());
5971 DCHECK(!cls->MustGenerateClinitCheck());
5972 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5973 GenerateGcRootFieldLoad(cls,
5974 out_loc,
5975 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005976 ArtMethod::DeclaringClassOffset().Int32Value(),
5977 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005978 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005979 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005980 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005981 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005982 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08005983 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005984 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5985 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
5986 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005987 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5988 break;
5989 }
5990 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005991 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005992 uint32_t address = dchecked_integral_cast<uint32_t>(
5993 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5994 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005995 __ LoadLiteral(out,
5996 kLoadUnsignedWord,
5997 codegen_->DeduplicateBootImageAddressLiteral(address));
5998 break;
5999 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006000 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006001 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
6002 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6003 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
6004 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6005 GpuRegister temp = non_baker_read_barrier
6006 ? out
6007 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006008 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
6009 GenerateGcRootFieldLoad(cls,
6010 out_loc,
6011 temp,
6012 /* placeholder */ 0x5678,
6013 read_barrier_option,
6014 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006015 generate_null_check = true;
6016 break;
6017 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006018 case HLoadClass::LoadKind::kJitTableAddress:
6019 __ LoadLiteral(out,
6020 kLoadUnsignedWord,
6021 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6022 cls->GetTypeIndex(),
6023 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006024 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006025 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006026 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006027 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006028 LOG(FATAL) << "UNREACHABLE";
6029 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006030 }
6031
6032 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6033 DCHECK(cls->CanCallRuntime());
6034 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006035 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006036 codegen_->AddSlowPath(slow_path);
6037 if (generate_null_check) {
6038 __ Beqzc(out, slow_path->GetEntryLabel());
6039 }
6040 if (cls->MustGenerateClinitCheck()) {
6041 GenerateClassInitializationCheck(slow_path, out);
6042 } else {
6043 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006044 }
6045 }
6046}
6047
David Brazdilcb1c0552015-08-04 16:22:25 +01006048static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006049 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006050}
6051
Alexey Frunze4dda3372015-06-01 18:31:49 -07006052void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6053 LocationSummary* locations =
6054 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6055 locations->SetOut(Location::RequiresRegister());
6056}
6057
6058void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6059 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006060 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6061}
6062
6063void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
6064 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6065}
6066
6067void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6068 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006069}
6070
Alexey Frunze4dda3372015-06-01 18:31:49 -07006071void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006072 HLoadString::LoadKind load_kind = load->GetLoadKind();
6073 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006074 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006075 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006076 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006077 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006078 } else {
6079 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006080 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6081 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6082 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006083 // Request a temp to hold the BSS entry location for the slow path.
6084 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006085 RegisterSet caller_saves = RegisterSet::Empty();
6086 InvokeRuntimeCallingConvention calling_convention;
6087 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6088 locations->SetCustomSlowPathCallerSaves(caller_saves);
6089 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006090 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006091 }
6092 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006093 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006094}
6095
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006096// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6097// move.
6098void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006099 HLoadString::LoadKind load_kind = load->GetLoadKind();
6100 LocationSummary* locations = load->GetLocations();
6101 Location out_loc = locations->Out();
6102 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6103
6104 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006105 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6106 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006107 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006108 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006109 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6110 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6111 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006112 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6113 return; // No dex cache slow path.
6114 }
6115 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006116 uint32_t address = dchecked_integral_cast<uint32_t>(
6117 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6118 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006119 __ LoadLiteral(out,
6120 kLoadUnsignedWord,
6121 codegen_->DeduplicateBootImageAddressLiteral(address));
6122 return; // No dex cache slow path.
6123 }
6124 case HLoadString::LoadKind::kBssEntry: {
6125 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006126 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006127 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006128 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6129 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6130 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6131 GpuRegister temp = non_baker_read_barrier
6132 ? out
6133 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006134 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08006135 GenerateGcRootFieldLoad(load,
6136 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006137 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08006138 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006139 kCompilerReadBarrierOption,
6140 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006141 SlowPathCodeMIPS64* slow_path =
6142 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006143 codegen_->AddSlowPath(slow_path);
6144 __ Beqzc(out, slow_path->GetEntryLabel());
6145 __ Bind(slow_path->GetExitLabel());
6146 return;
6147 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006148 case HLoadString::LoadKind::kJitTableAddress:
6149 __ LoadLiteral(out,
6150 kLoadUnsignedWord,
6151 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6152 load->GetStringIndex(),
6153 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006154 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006155 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006156 default:
6157 break;
6158 }
6159
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006160 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006161 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006162 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006163 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006164 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6165 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6166 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006167}
6168
Alexey Frunze4dda3372015-06-01 18:31:49 -07006169void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
6170 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6171 locations->SetOut(Location::ConstantLocation(constant));
6172}
6173
6174void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6175 // Will be generated at use site.
6176}
6177
6178void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
6179 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006180 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006181 InvokeRuntimeCallingConvention calling_convention;
6182 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6183}
6184
6185void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006186 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006187 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006188 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006189 if (instruction->IsEnter()) {
6190 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6191 } else {
6192 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6193 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006194}
6195
6196void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6197 LocationSummary* locations =
6198 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
6199 switch (mul->GetResultType()) {
6200 case Primitive::kPrimInt:
6201 case Primitive::kPrimLong:
6202 locations->SetInAt(0, Location::RequiresRegister());
6203 locations->SetInAt(1, Location::RequiresRegister());
6204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6205 break;
6206
6207 case Primitive::kPrimFloat:
6208 case Primitive::kPrimDouble:
6209 locations->SetInAt(0, Location::RequiresFpuRegister());
6210 locations->SetInAt(1, Location::RequiresFpuRegister());
6211 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6212 break;
6213
6214 default:
6215 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6216 }
6217}
6218
6219void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
6220 Primitive::Type type = instruction->GetType();
6221 LocationSummary* locations = instruction->GetLocations();
6222
6223 switch (type) {
6224 case Primitive::kPrimInt:
6225 case Primitive::kPrimLong: {
6226 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6227 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6228 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
6229 if (type == Primitive::kPrimInt)
6230 __ MulR6(dst, lhs, rhs);
6231 else
6232 __ Dmul(dst, lhs, rhs);
6233 break;
6234 }
6235 case Primitive::kPrimFloat:
6236 case Primitive::kPrimDouble: {
6237 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6238 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6239 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
6240 if (type == Primitive::kPrimFloat)
6241 __ MulS(dst, lhs, rhs);
6242 else
6243 __ MulD(dst, lhs, rhs);
6244 break;
6245 }
6246 default:
6247 LOG(FATAL) << "Unexpected mul type " << type;
6248 }
6249}
6250
6251void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6252 LocationSummary* locations =
6253 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
6254 switch (neg->GetResultType()) {
6255 case Primitive::kPrimInt:
6256 case Primitive::kPrimLong:
6257 locations->SetInAt(0, Location::RequiresRegister());
6258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6259 break;
6260
6261 case Primitive::kPrimFloat:
6262 case Primitive::kPrimDouble:
6263 locations->SetInAt(0, Location::RequiresFpuRegister());
6264 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6265 break;
6266
6267 default:
6268 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6269 }
6270}
6271
6272void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
6273 Primitive::Type type = instruction->GetType();
6274 LocationSummary* locations = instruction->GetLocations();
6275
6276 switch (type) {
6277 case Primitive::kPrimInt:
6278 case Primitive::kPrimLong: {
6279 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6280 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6281 if (type == Primitive::kPrimInt)
6282 __ Subu(dst, ZERO, src);
6283 else
6284 __ Dsubu(dst, ZERO, src);
6285 break;
6286 }
6287 case Primitive::kPrimFloat:
6288 case Primitive::kPrimDouble: {
6289 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6290 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6291 if (type == Primitive::kPrimFloat)
6292 __ NegS(dst, src);
6293 else
6294 __ NegD(dst, src);
6295 break;
6296 }
6297 default:
6298 LOG(FATAL) << "Unexpected neg type " << type;
6299 }
6300}
6301
6302void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
6303 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006305 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006306 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006307 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6308 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006309}
6310
6311void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006312 // Note: if heap poisoning is enabled, the entry point takes care
6313 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006314 QuickEntrypointEnum entrypoint =
6315 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6316 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006317 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006318 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006319}
6320
6321void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
6322 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006323 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006324 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006325 if (instruction->IsStringAlloc()) {
6326 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6327 } else {
6328 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006329 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006330 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6331}
6332
6333void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006334 // Note: if heap poisoning is enabled, the entry point takes care
6335 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006336 if (instruction->IsStringAlloc()) {
6337 // String is allocated through StringFactory. Call NewEmptyString entry point.
6338 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006339 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006340 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006341 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6342 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6343 __ Jalr(T9);
6344 __ Nop();
6345 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6346 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006347 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006348 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006349 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006350}
6351
6352void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
6353 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6354 locations->SetInAt(0, Location::RequiresRegister());
6355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6356}
6357
6358void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
6359 Primitive::Type type = instruction->GetType();
6360 LocationSummary* locations = instruction->GetLocations();
6361
6362 switch (type) {
6363 case Primitive::kPrimInt:
6364 case Primitive::kPrimLong: {
6365 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6366 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6367 __ Nor(dst, src, ZERO);
6368 break;
6369 }
6370
6371 default:
6372 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6373 }
6374}
6375
6376void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6378 locations->SetInAt(0, Location::RequiresRegister());
6379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6380}
6381
6382void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6383 LocationSummary* locations = instruction->GetLocations();
6384 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6385 locations->InAt(0).AsRegister<GpuRegister>(),
6386 1);
6387}
6388
6389void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006390 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6391 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006392}
6393
Calin Juravle2ae48182016-03-16 14:05:09 +00006394void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6395 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006396 return;
6397 }
6398 Location obj = instruction->GetLocations()->InAt(0);
6399
6400 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006401 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006402}
6403
Calin Juravle2ae48182016-03-16 14:05:09 +00006404void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006405 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006406 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006407
6408 Location obj = instruction->GetLocations()->InAt(0);
6409
6410 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6411}
6412
6413void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006414 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006415}
6416
6417void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6418 HandleBinaryOp(instruction);
6419}
6420
6421void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6422 HandleBinaryOp(instruction);
6423}
6424
6425void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6426 LOG(FATAL) << "Unreachable";
6427}
6428
6429void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
6430 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6431}
6432
6433void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
6434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6435 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6436 if (location.IsStackSlot()) {
6437 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6438 } else if (location.IsDoubleStackSlot()) {
6439 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6440 }
6441 locations->SetOut(location);
6442}
6443
6444void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6445 ATTRIBUTE_UNUSED) {
6446 // Nothing to do, the parameter is already at its location.
6447}
6448
6449void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6450 LocationSummary* locations =
6451 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6452 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6453}
6454
6455void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6456 ATTRIBUTE_UNUSED) {
6457 // Nothing to do, the method is already at its location.
6458}
6459
6460void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
6461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006462 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006463 locations->SetInAt(i, Location::Any());
6464 }
6465 locations->SetOut(Location::Any());
6466}
6467
6468void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6469 LOG(FATAL) << "Unreachable";
6470}
6471
6472void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
6473 Primitive::Type type = rem->GetResultType();
6474 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006475 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6476 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6478
6479 switch (type) {
6480 case Primitive::kPrimInt:
6481 case Primitive::kPrimLong:
6482 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006483 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6485 break;
6486
6487 case Primitive::kPrimFloat:
6488 case Primitive::kPrimDouble: {
6489 InvokeRuntimeCallingConvention calling_convention;
6490 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6491 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6492 locations->SetOut(calling_convention.GetReturnLocation(type));
6493 break;
6494 }
6495
6496 default:
6497 LOG(FATAL) << "Unexpected rem type " << type;
6498 }
6499}
6500
6501void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
6502 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006503
6504 switch (type) {
6505 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07006506 case Primitive::kPrimLong:
6507 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006508 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006509
6510 case Primitive::kPrimFloat:
6511 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01006512 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
6513 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006514 if (type == Primitive::kPrimFloat) {
6515 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6516 } else {
6517 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6518 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006519 break;
6520 }
6521 default:
6522 LOG(FATAL) << "Unexpected rem type " << type;
6523 }
6524}
6525
Igor Murashkind01745e2017-04-05 16:40:31 -07006526void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6527 constructor_fence->SetLocations(nullptr);
6528}
6529
6530void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6531 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6532 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6533}
6534
Alexey Frunze4dda3372015-06-01 18:31:49 -07006535void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6536 memory_barrier->SetLocations(nullptr);
6537}
6538
6539void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6540 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6541}
6542
6543void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
6544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6545 Primitive::Type return_type = ret->InputAt(0)->GetType();
6546 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6547}
6548
6549void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6550 codegen_->GenerateFrameExit();
6551}
6552
6553void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6554 ret->SetLocations(nullptr);
6555}
6556
6557void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6558 codegen_->GenerateFrameExit();
6559}
6560
Alexey Frunze92d90602015-12-18 18:16:36 -08006561void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6562 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006563}
6564
Alexey Frunze92d90602015-12-18 18:16:36 -08006565void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6566 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006567}
6568
Alexey Frunze4dda3372015-06-01 18:31:49 -07006569void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6570 HandleShift(shl);
6571}
6572
6573void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6574 HandleShift(shl);
6575}
6576
6577void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6578 HandleShift(shr);
6579}
6580
6581void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6582 HandleShift(shr);
6583}
6584
Alexey Frunze4dda3372015-06-01 18:31:49 -07006585void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6586 HandleBinaryOp(instruction);
6587}
6588
6589void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6590 HandleBinaryOp(instruction);
6591}
6592
6593void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6594 HandleFieldGet(instruction, instruction->GetFieldInfo());
6595}
6596
6597void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6598 HandleFieldGet(instruction, instruction->GetFieldInfo());
6599}
6600
6601void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6602 HandleFieldSet(instruction, instruction->GetFieldInfo());
6603}
6604
6605void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006606 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006607}
6608
Calin Juravlee460d1d2015-09-29 04:52:17 +01006609void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6610 HUnresolvedInstanceFieldGet* instruction) {
6611 FieldAccessCallingConventionMIPS64 calling_convention;
6612 codegen_->CreateUnresolvedFieldLocationSummary(
6613 instruction, instruction->GetFieldType(), calling_convention);
6614}
6615
6616void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6617 HUnresolvedInstanceFieldGet* instruction) {
6618 FieldAccessCallingConventionMIPS64 calling_convention;
6619 codegen_->GenerateUnresolvedFieldAccess(instruction,
6620 instruction->GetFieldType(),
6621 instruction->GetFieldIndex(),
6622 instruction->GetDexPc(),
6623 calling_convention);
6624}
6625
6626void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6627 HUnresolvedInstanceFieldSet* instruction) {
6628 FieldAccessCallingConventionMIPS64 calling_convention;
6629 codegen_->CreateUnresolvedFieldLocationSummary(
6630 instruction, instruction->GetFieldType(), calling_convention);
6631}
6632
6633void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6634 HUnresolvedInstanceFieldSet* instruction) {
6635 FieldAccessCallingConventionMIPS64 calling_convention;
6636 codegen_->GenerateUnresolvedFieldAccess(instruction,
6637 instruction->GetFieldType(),
6638 instruction->GetFieldIndex(),
6639 instruction->GetDexPc(),
6640 calling_convention);
6641}
6642
6643void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6644 HUnresolvedStaticFieldGet* instruction) {
6645 FieldAccessCallingConventionMIPS64 calling_convention;
6646 codegen_->CreateUnresolvedFieldLocationSummary(
6647 instruction, instruction->GetFieldType(), calling_convention);
6648}
6649
6650void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6651 HUnresolvedStaticFieldGet* instruction) {
6652 FieldAccessCallingConventionMIPS64 calling_convention;
6653 codegen_->GenerateUnresolvedFieldAccess(instruction,
6654 instruction->GetFieldType(),
6655 instruction->GetFieldIndex(),
6656 instruction->GetDexPc(),
6657 calling_convention);
6658}
6659
6660void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6661 HUnresolvedStaticFieldSet* instruction) {
6662 FieldAccessCallingConventionMIPS64 calling_convention;
6663 codegen_->CreateUnresolvedFieldLocationSummary(
6664 instruction, instruction->GetFieldType(), calling_convention);
6665}
6666
6667void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6668 HUnresolvedStaticFieldSet* instruction) {
6669 FieldAccessCallingConventionMIPS64 calling_convention;
6670 codegen_->GenerateUnresolvedFieldAccess(instruction,
6671 instruction->GetFieldType(),
6672 instruction->GetFieldIndex(),
6673 instruction->GetDexPc(),
6674 calling_convention);
6675}
6676
Alexey Frunze4dda3372015-06-01 18:31:49 -07006677void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006678 LocationSummary* locations =
6679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006680 // In suspend check slow path, usually there are no caller-save registers at all.
6681 // If SIMD instructions are present, however, we force spilling all live SIMD
6682 // registers in full width (since the runtime only saves/restores lower part).
6683 locations->SetCustomSlowPathCallerSaves(
6684 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006685}
6686
6687void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6688 HBasicBlock* block = instruction->GetBlock();
6689 if (block->GetLoopInformation() != nullptr) {
6690 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6691 // The back edge will generate the suspend check.
6692 return;
6693 }
6694 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6695 // The goto will generate the suspend check.
6696 return;
6697 }
6698 GenerateSuspendCheck(instruction, nullptr);
6699}
6700
Alexey Frunze4dda3372015-06-01 18:31:49 -07006701void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
6702 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006703 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006704 InvokeRuntimeCallingConvention calling_convention;
6705 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6706}
6707
6708void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006709 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006710 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6711}
6712
6713void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6714 Primitive::Type input_type = conversion->GetInputType();
6715 Primitive::Type result_type = conversion->GetResultType();
6716 DCHECK_NE(input_type, result_type);
6717
6718 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6719 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6720 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6721 }
6722
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
6724
6725 if (Primitive::IsFloatingPointType(input_type)) {
6726 locations->SetInAt(0, Location::RequiresFpuRegister());
6727 } else {
6728 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006729 }
6730
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006731 if (Primitive::IsFloatingPointType(result_type)) {
6732 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006733 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006734 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006735 }
6736}
6737
6738void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6739 LocationSummary* locations = conversion->GetLocations();
6740 Primitive::Type result_type = conversion->GetResultType();
6741 Primitive::Type input_type = conversion->GetInputType();
6742
6743 DCHECK_NE(input_type, result_type);
6744
6745 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6746 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6747 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6748
6749 switch (result_type) {
6750 case Primitive::kPrimChar:
6751 __ Andi(dst, src, 0xFFFF);
6752 break;
6753 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006754 if (input_type == Primitive::kPrimLong) {
6755 // Type conversion from long to types narrower than int is a result of code
6756 // transformations. To avoid unpredictable results for SEB and SEH, we first
6757 // need to sign-extend the low 32-bit value into bits 32 through 63.
6758 __ Sll(dst, src, 0);
6759 __ Seb(dst, dst);
6760 } else {
6761 __ Seb(dst, src);
6762 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006763 break;
6764 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006765 if (input_type == Primitive::kPrimLong) {
6766 // Type conversion from long to types narrower than int is a result of code
6767 // transformations. To avoid unpredictable results for SEB and SEH, we first
6768 // need to sign-extend the low 32-bit value into bits 32 through 63.
6769 __ Sll(dst, src, 0);
6770 __ Seh(dst, dst);
6771 } else {
6772 __ Seh(dst, src);
6773 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006774 break;
6775 case Primitive::kPrimInt:
6776 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006777 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6778 // conversions, except when the input and output registers are the same and we are not
6779 // converting longs to shorter types. In these cases, do nothing.
6780 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
6781 __ Sll(dst, src, 0);
6782 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006783 break;
6784
6785 default:
6786 LOG(FATAL) << "Unexpected type conversion from " << input_type
6787 << " to " << result_type;
6788 }
6789 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006790 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6791 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6792 if (input_type == Primitive::kPrimLong) {
6793 __ Dmtc1(src, FTMP);
6794 if (result_type == Primitive::kPrimFloat) {
6795 __ Cvtsl(dst, FTMP);
6796 } else {
6797 __ Cvtdl(dst, FTMP);
6798 }
6799 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006800 __ Mtc1(src, FTMP);
6801 if (result_type == Primitive::kPrimFloat) {
6802 __ Cvtsw(dst, FTMP);
6803 } else {
6804 __ Cvtdw(dst, FTMP);
6805 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006806 }
6807 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6808 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006809 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6810 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006811
6812 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006813 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006814 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006815 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006816 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006817 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006818 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006819 } else {
6820 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006821 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006822 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006823 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006824 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006825 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006826 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006827 } else if (Primitive::IsFloatingPointType(result_type) &&
6828 Primitive::IsFloatingPointType(input_type)) {
6829 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6830 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6831 if (result_type == Primitive::kPrimFloat) {
6832 __ Cvtsd(dst, src);
6833 } else {
6834 __ Cvtds(dst, src);
6835 }
6836 } else {
6837 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6838 << " to " << result_type;
6839 }
6840}
6841
6842void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6843 HandleShift(ushr);
6844}
6845
6846void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6847 HandleShift(ushr);
6848}
6849
6850void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6851 HandleBinaryOp(instruction);
6852}
6853
6854void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6855 HandleBinaryOp(instruction);
6856}
6857
6858void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6859 // Nothing to do, this should be removed during prepare for register allocator.
6860 LOG(FATAL) << "Unreachable";
6861}
6862
6863void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6864 // Nothing to do, this should be removed during prepare for register allocator.
6865 LOG(FATAL) << "Unreachable";
6866}
6867
6868void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006869 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006870}
6871
6872void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006873 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006874}
6875
6876void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006877 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006878}
6879
6880void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006881 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006882}
6883
6884void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006885 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006886}
6887
6888void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006889 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006890}
6891
6892void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006893 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006894}
6895
6896void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006897 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006898}
6899
6900void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006901 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006902}
6903
6904void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006905 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006906}
6907
6908void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006909 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006910}
6911
6912void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006913 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006914}
6915
Aart Bike9f37602015-10-09 11:15:55 -07006916void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006917 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006918}
6919
6920void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006921 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006922}
6923
6924void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006925 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006926}
6927
6928void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006929 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006930}
6931
6932void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006933 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006934}
6935
6936void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006937 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006938}
6939
6940void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006941 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006942}
6943
6944void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006945 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006946}
6947
Mark Mendellfe57faa2015-09-18 09:26:15 -04006948// Simple implementation of packed switch - generate cascaded compare/jumps.
6949void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6950 LocationSummary* locations =
6951 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6952 locations->SetInAt(0, Location::RequiresRegister());
6953}
6954
Alexey Frunze0960ac52016-12-20 17:24:59 -08006955void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6956 int32_t lower_bound,
6957 uint32_t num_entries,
6958 HBasicBlock* switch_block,
6959 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006960 // Create a set of compare/jumps.
6961 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006962 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006963 // Jump to default if index is negative
6964 // Note: We don't check the case that index is positive while value < lower_bound, because in
6965 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6966 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6967
Alexey Frunze0960ac52016-12-20 17:24:59 -08006968 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006969 // Jump to successors[0] if value == lower_bound.
6970 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6971 int32_t last_index = 0;
6972 for (; num_entries - last_index > 2; last_index += 2) {
6973 __ Addiu(temp_reg, temp_reg, -2);
6974 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6975 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6976 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6977 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6978 }
6979 if (num_entries - last_index == 2) {
6980 // The last missing case_value.
6981 __ Addiu(temp_reg, temp_reg, -1);
6982 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006983 }
6984
6985 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006986 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006987 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006988 }
6989}
6990
Alexey Frunze0960ac52016-12-20 17:24:59 -08006991void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6992 int32_t lower_bound,
6993 uint32_t num_entries,
6994 HBasicBlock* switch_block,
6995 HBasicBlock* default_block) {
6996 // Create a jump table.
6997 std::vector<Mips64Label*> labels(num_entries);
6998 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6999 for (uint32_t i = 0; i < num_entries; i++) {
7000 labels[i] = codegen_->GetLabelOf(successors[i]);
7001 }
7002 JumpTable* table = __ CreateJumpTable(std::move(labels));
7003
7004 // Is the value in range?
7005 __ Addiu32(TMP, value_reg, -lower_bound);
7006 __ LoadConst32(AT, num_entries);
7007 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7008
7009 // We are in the range of the table.
7010 // Load the target address from the jump table, indexing by the value.
7011 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007012 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007013 __ Lw(TMP, TMP, 0);
7014 // Compute the absolute target address by adding the table start address
7015 // (the table contains offsets to targets relative to its start).
7016 __ Daddu(TMP, TMP, AT);
7017 // And jump.
7018 __ Jr(TMP);
7019 __ Nop();
7020}
7021
7022void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7023 int32_t lower_bound = switch_instr->GetStartValue();
7024 uint32_t num_entries = switch_instr->GetNumEntries();
7025 LocationSummary* locations = switch_instr->GetLocations();
7026 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7027 HBasicBlock* switch_block = switch_instr->GetBlock();
7028 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7029
7030 if (num_entries > kPackedSwitchJumpTableThreshold) {
7031 GenTableBasedPackedSwitch(value_reg,
7032 lower_bound,
7033 num_entries,
7034 switch_block,
7035 default_block);
7036 } else {
7037 GenPackedSwitchWithCompares(value_reg,
7038 lower_bound,
7039 num_entries,
7040 switch_block,
7041 default_block);
7042 }
7043}
7044
Chris Larsenc9905a62017-03-13 17:06:18 -07007045void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7046 LocationSummary* locations =
7047 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7048 locations->SetInAt(0, Location::RequiresRegister());
7049 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007050}
7051
Chris Larsenc9905a62017-03-13 17:06:18 -07007052void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7053 LocationSummary* locations = instruction->GetLocations();
7054 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7055 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7056 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7057 __ LoadFromOffset(kLoadDoubleword,
7058 locations->Out().AsRegister<GpuRegister>(),
7059 locations->InAt(0).AsRegister<GpuRegister>(),
7060 method_offset);
7061 } else {
7062 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7063 instruction->GetIndex(), kMips64PointerSize));
7064 __ LoadFromOffset(kLoadDoubleword,
7065 locations->Out().AsRegister<GpuRegister>(),
7066 locations->InAt(0).AsRegister<GpuRegister>(),
7067 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7068 __ LoadFromOffset(kLoadDoubleword,
7069 locations->Out().AsRegister<GpuRegister>(),
7070 locations->Out().AsRegister<GpuRegister>(),
7071 method_offset);
7072 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007073}
7074
Alexey Frunze4dda3372015-06-01 18:31:49 -07007075} // namespace mips64
7076} // namespace art