blob: 71c2bfff1979fe37e974d5a8f7d294ec9638bab7 [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 =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100321 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(),
322 string_index,
323 bss_info_high_);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700324 __ 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 =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100342 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700343 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100344 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700345 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)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001052 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001053 jit_string_patches_(StringReferenceValueComparator(),
1054 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1055 jit_class_patches_(TypeReferenceValueComparator(),
1056 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001057 // Save RA (containing the return address) to mimic Quick.
1058 AddAllocatedRegister(Location::RegisterLocation(RA));
1059}
1060
1061#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1063#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -07001065
1066void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001067 // Ensure that we fix up branches.
1068 __ FinalizeCode();
1069
1070 // Adjust native pc offsets in stack maps.
1071 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001072 uint32_t old_position =
1073 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001074 uint32_t new_position = __ GetAdjustedPosition(old_position);
1075 DCHECK_GE(new_position, old_position);
1076 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1077 }
1078
1079 // Adjust pc offsets for the disassembly information.
1080 if (disasm_info_ != nullptr) {
1081 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1082 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1083 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1084 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1085 it.second.start = __ GetAdjustedPosition(it.second.start);
1086 it.second.end = __ GetAdjustedPosition(it.second.end);
1087 }
1088 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1089 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1090 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1091 }
1092 }
1093
Alexey Frunze4dda3372015-06-01 18:31:49 -07001094 CodeGenerator::Finalize(allocator);
1095}
1096
1097Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1098 return codegen_->GetAssembler();
1099}
1100
1101void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001102 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001103 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1104}
1105
1106void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001107 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001108 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1109}
1110
1111void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1112 // Pop reg
1113 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001114 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001115}
1116
1117void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1118 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001119 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001120 __ Sd(GpuRegister(reg), SP, 0);
1121}
1122
1123void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1124 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1125 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1126 // Allocate a scratch register other than TMP, if available.
1127 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1128 // automatically unspilled when the scratch scope object is destroyed).
1129 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1130 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001131 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132 __ LoadFromOffset(load_type,
1133 GpuRegister(ensure_scratch.GetRegister()),
1134 SP,
1135 index1 + stack_offset);
1136 __ LoadFromOffset(load_type,
1137 TMP,
1138 SP,
1139 index2 + stack_offset);
1140 __ StoreToOffset(store_type,
1141 GpuRegister(ensure_scratch.GetRegister()),
1142 SP,
1143 index2 + stack_offset);
1144 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1145}
1146
1147static dwarf::Reg DWARFReg(GpuRegister reg) {
1148 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1149}
1150
David Srbeckyba702002016-02-01 18:15:29 +00001151static dwarf::Reg DWARFReg(FpuRegister reg) {
1152 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1153}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001154
1155void CodeGeneratorMIPS64::GenerateFrameEntry() {
1156 __ Bind(&frame_entry_label_);
1157
1158 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1159
1160 if (do_overflow_check) {
1161 __ LoadFromOffset(kLoadWord,
1162 ZERO,
1163 SP,
1164 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1165 RecordPcInfo(nullptr, 0);
1166 }
1167
Alexey Frunze4dda3372015-06-01 18:31:49 -07001168 if (HasEmptyFrame()) {
1169 return;
1170 }
1171
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001172 // Make sure the frame size isn't unreasonably large.
1173 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1174 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1175 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001176
1177 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001178
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001179 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001180 __ IncreaseFrameSize(ofs);
1181
1182 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1183 GpuRegister reg = kCoreCalleeSaves[i];
1184 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001185 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001186 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001187 __ cfi().RelOffset(DWARFReg(reg), ofs);
1188 }
1189 }
1190
1191 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1192 FpuRegister reg = kFpuCalleeSaves[i];
1193 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001194 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001195 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001196 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001197 }
1198 }
1199
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001200 // Save the current method if we need it. Note that we do not
1201 // do this in HCurrentMethod, as the instruction might have been removed
1202 // in the SSA graph.
1203 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001204 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001205 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001206
1207 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1208 // Initialize should_deoptimize flag to 0.
1209 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1210 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001211}
1212
1213void CodeGeneratorMIPS64::GenerateFrameExit() {
1214 __ cfi().RememberState();
1215
Alexey Frunze4dda3372015-06-01 18:31:49 -07001216 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001217 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001218
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001219 // For better instruction scheduling restore RA before other registers.
1220 uint32_t ofs = GetFrameSize();
1221 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001222 GpuRegister reg = kCoreCalleeSaves[i];
1223 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001224 ofs -= kMips64DoublewordSize;
1225 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001226 __ cfi().Restore(DWARFReg(reg));
1227 }
1228 }
1229
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001230 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1231 FpuRegister reg = kFpuCalleeSaves[i];
1232 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1233 ofs -= kMips64DoublewordSize;
1234 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1235 __ cfi().Restore(DWARFReg(reg));
1236 }
1237 }
1238
1239 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001240 }
1241
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001242 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001243
1244 __ cfi().RestoreState();
1245 __ cfi().DefCFAOffset(GetFrameSize());
1246}
1247
1248void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1249 __ Bind(GetLabelOf(block));
1250}
1251
1252void CodeGeneratorMIPS64::MoveLocation(Location destination,
1253 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001255 if (source.Equals(destination)) {
1256 return;
1257 }
1258
1259 // A valid move can always be inferred from the destination and source
1260 // locations. When moving from and to a register, the argument type can be
1261 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 DCHECK_EQ(unspecified_type, false);
1264
1265 if (destination.IsRegister() || destination.IsFpuRegister()) {
1266 if (unspecified_type) {
1267 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1268 if (source.IsStackSlot() ||
1269 (src_cst != nullptr && (src_cst->IsIntConstant()
1270 || src_cst->IsFloatConstant()
1271 || src_cst->IsNullConstant()))) {
1272 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001274 } else {
1275 // If the source is a double stack slot or a 64bit constant, a 64bit
1276 // type is appropriate. Else the source is a register, and since the
1277 // type has not been specified, we chose a 64bit type to force a 64bit
1278 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001279 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001280 }
1281 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1283 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001284 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1285 // Move to GPR/FPR from stack
1286 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001288 __ LoadFpuFromOffset(load_type,
1289 destination.AsFpuRegister<FpuRegister>(),
1290 SP,
1291 source.GetStackIndex());
1292 } else {
1293 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1294 __ LoadFromOffset(load_type,
1295 destination.AsRegister<GpuRegister>(),
1296 SP,
1297 source.GetStackIndex());
1298 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001299 } else if (source.IsSIMDStackSlot()) {
1300 __ LoadFpuFromOffset(kLoadQuadword,
1301 destination.AsFpuRegister<FpuRegister>(),
1302 SP,
1303 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001304 } else if (source.IsConstant()) {
1305 // Move to GPR/FPR from constant
1306 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001307 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 gpr = destination.AsRegister<GpuRegister>();
1309 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001310 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001311 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1312 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1313 gpr = ZERO;
1314 } else {
1315 __ LoadConst32(gpr, value);
1316 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001317 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001318 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1319 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1320 gpr = ZERO;
1321 } else {
1322 __ LoadConst64(gpr, value);
1323 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001324 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001325 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001326 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001327 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001328 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1329 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001330 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001331 if (destination.IsRegister()) {
1332 // Move to GPR from GPR
1333 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1334 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001335 DCHECK(destination.IsFpuRegister());
1336 if (Primitive::Is64BitType(dst_type)) {
1337 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1338 } else {
1339 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1340 }
1341 }
1342 } else if (source.IsFpuRegister()) {
1343 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001344 if (GetGraph()->HasSIMD()) {
1345 __ MoveV(VectorRegisterFrom(destination),
1346 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001347 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001348 // Move to FPR from FPR
1349 if (dst_type == Primitive::kPrimFloat) {
1350 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1351 } else {
1352 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
1353 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1354 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001355 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001356 } else {
1357 DCHECK(destination.IsRegister());
1358 if (Primitive::Is64BitType(dst_type)) {
1359 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1360 } else {
1361 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1362 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001363 }
1364 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001365 } else if (destination.IsSIMDStackSlot()) {
1366 if (source.IsFpuRegister()) {
1367 __ StoreFpuToOffset(kStoreQuadword,
1368 source.AsFpuRegister<FpuRegister>(),
1369 SP,
1370 destination.GetStackIndex());
1371 } else {
1372 DCHECK(source.IsSIMDStackSlot());
1373 __ LoadFpuFromOffset(kLoadQuadword,
1374 FTMP,
1375 SP,
1376 source.GetStackIndex());
1377 __ StoreFpuToOffset(kStoreQuadword,
1378 FTMP,
1379 SP,
1380 destination.GetStackIndex());
1381 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001382 } else { // The destination is not a register. It must be a stack slot.
1383 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1384 if (source.IsRegister() || source.IsFpuRegister()) {
1385 if (unspecified_type) {
1386 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001387 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001388 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001389 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001390 }
1391 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001392 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1393 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001394 // Move to stack from GPR/FPR
1395 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1396 if (source.IsRegister()) {
1397 __ StoreToOffset(store_type,
1398 source.AsRegister<GpuRegister>(),
1399 SP,
1400 destination.GetStackIndex());
1401 } else {
1402 __ StoreFpuToOffset(store_type,
1403 source.AsFpuRegister<FpuRegister>(),
1404 SP,
1405 destination.GetStackIndex());
1406 }
1407 } else if (source.IsConstant()) {
1408 // Move to stack from constant
1409 HConstant* src_cst = source.GetConstant();
1410 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001411 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001412 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001413 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1414 if (value != 0) {
1415 gpr = TMP;
1416 __ LoadConst32(gpr, value);
1417 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001418 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001419 DCHECK(destination.IsDoubleStackSlot());
1420 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1421 if (value != 0) {
1422 gpr = TMP;
1423 __ LoadConst64(gpr, value);
1424 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001426 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001427 } else {
1428 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1429 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1430 // Move to stack from stack
1431 if (destination.IsStackSlot()) {
1432 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1433 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1434 } else {
1435 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1436 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1437 }
1438 }
1439 }
1440}
1441
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001442void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001443 DCHECK(!loc1.IsConstant());
1444 DCHECK(!loc2.IsConstant());
1445
1446 if (loc1.Equals(loc2)) {
1447 return;
1448 }
1449
1450 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1451 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1452 bool is_fp_reg1 = loc1.IsFpuRegister();
1453 bool is_fp_reg2 = loc2.IsFpuRegister();
1454
1455 if (loc2.IsRegister() && loc1.IsRegister()) {
1456 // Swap 2 GPRs
1457 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1458 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1459 __ Move(TMP, r2);
1460 __ Move(r2, r1);
1461 __ Move(r1, TMP);
1462 } else if (is_fp_reg2 && is_fp_reg1) {
1463 // Swap 2 FPRs
1464 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1465 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001466 if (type == Primitive::kPrimFloat) {
1467 __ MovS(FTMP, r1);
1468 __ MovS(r1, r2);
1469 __ MovS(r2, FTMP);
1470 } else {
1471 DCHECK_EQ(type, Primitive::kPrimDouble);
1472 __ MovD(FTMP, r1);
1473 __ MovD(r1, r2);
1474 __ MovD(r2, FTMP);
1475 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001476 } else if (is_slot1 != is_slot2) {
1477 // Swap GPR/FPR and stack slot
1478 Location reg_loc = is_slot1 ? loc2 : loc1;
1479 Location mem_loc = is_slot1 ? loc1 : loc2;
1480 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1481 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1482 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1483 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1484 if (reg_loc.IsFpuRegister()) {
1485 __ StoreFpuToOffset(store_type,
1486 reg_loc.AsFpuRegister<FpuRegister>(),
1487 SP,
1488 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001489 if (mem_loc.IsStackSlot()) {
1490 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1491 } else {
1492 DCHECK(mem_loc.IsDoubleStackSlot());
1493 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1494 }
1495 } else {
1496 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1497 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1498 }
1499 } else if (is_slot1 && is_slot2) {
1500 move_resolver_.Exchange(loc1.GetStackIndex(),
1501 loc2.GetStackIndex(),
1502 loc1.IsDoubleStackSlot());
1503 } else {
1504 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1505 }
1506}
1507
Calin Juravle175dc732015-08-25 15:42:32 +01001508void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1509 DCHECK(location.IsRegister());
1510 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1511}
1512
Calin Juravlee460d1d2015-09-29 04:52:17 +01001513void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1514 if (location.IsRegister()) {
1515 locations->AddTemp(location);
1516 } else {
1517 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1518 }
1519}
1520
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001521void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1522 GpuRegister value,
1523 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001524 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001525 GpuRegister card = AT;
1526 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001527 if (value_can_be_null) {
1528 __ Beqzc(value, &done);
1529 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001530 __ LoadFromOffset(kLoadDoubleword,
1531 card,
1532 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001533 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001534 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1535 __ Daddu(temp, card, temp);
1536 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001537 if (value_can_be_null) {
1538 __ Bind(&done);
1539 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001540}
1541
Alexey Frunze19f6c692016-11-30 19:19:55 -08001542template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1543inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1544 const ArenaDeque<PcRelativePatchInfo>& infos,
1545 ArenaVector<LinkerPatch>* linker_patches) {
1546 for (const PcRelativePatchInfo& info : infos) {
1547 const DexFile& dex_file = info.target_dex_file;
1548 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001549 DCHECK(info.label.IsBound());
1550 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1551 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1552 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1553 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001554 }
1555}
1556
1557void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1558 DCHECK(linker_patches->empty());
1559 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001560 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001561 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001562 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001563 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001564 pc_relative_string_patches_.size() +
1565 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001566 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001567 if (GetCompilerOptions().IsBootImage()) {
1568 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Alexey Frunzef63f5692016-12-13 17:43:11 -08001569 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001570 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1571 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001572 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1573 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001574 } else {
1575 DCHECK(pc_relative_method_patches_.empty());
1576 DCHECK(pc_relative_type_patches_.empty());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001577 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(pc_relative_string_patches_,
1578 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001579 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001580 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1581 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001582 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1583 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001584 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
1585 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001586 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001587}
1588
Vladimir Marko65979462017-05-19 17:25:12 +01001589CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001590 MethodReference target_method,
1591 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001592 return NewPcRelativePatch(*target_method.dex_file,
1593 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001594 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001595 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001596}
1597
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001598CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001599 MethodReference target_method,
1600 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001601 return NewPcRelativePatch(*target_method.dex_file,
1602 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001603 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001604 &method_bss_entry_patches_);
1605}
1606
Alexey Frunzef63f5692016-12-13 17:43:11 -08001607CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001608 const DexFile& dex_file,
1609 dex::TypeIndex type_index,
1610 const PcRelativePatchInfo* info_high) {
1611 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001612}
1613
Vladimir Marko1998cd02017-01-13 13:02:58 +00001614CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001615 const DexFile& dex_file,
1616 dex::TypeIndex type_index,
1617 const PcRelativePatchInfo* info_high) {
1618 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001619}
1620
Vladimir Marko65979462017-05-19 17:25:12 +01001621CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001622 const DexFile& dex_file,
1623 dex::StringIndex string_index,
1624 const PcRelativePatchInfo* info_high) {
1625 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001626}
1627
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001628CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1629 const DexFile& dex_file,
1630 dex::StringIndex string_index,
1631 const PcRelativePatchInfo* info_high) {
1632 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1633}
1634
Alexey Frunze19f6c692016-11-30 19:19:55 -08001635CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001636 const DexFile& dex_file,
1637 uint32_t offset_or_index,
1638 const PcRelativePatchInfo* info_high,
1639 ArenaDeque<PcRelativePatchInfo>* patches) {
1640 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001641 return &patches->back();
1642}
1643
Alexey Frunzef63f5692016-12-13 17:43:11 -08001644Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1645 return map->GetOrCreate(
1646 value,
1647 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1648}
1649
Alexey Frunze19f6c692016-11-30 19:19:55 -08001650Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1651 return uint64_literals_.GetOrCreate(
1652 value,
1653 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1654}
1655
Alexey Frunzef63f5692016-12-13 17:43:11 -08001656Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001657 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001658}
1659
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001660void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1661 GpuRegister out,
1662 PcRelativePatchInfo* info_low) {
1663 DCHECK(!info_high->patch_info_high);
1664 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001665 // Add the high half of a 32-bit offset to PC.
1666 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001667 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001668 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001669 if (info_low != nullptr) {
1670 DCHECK_EQ(info_low->patch_info_high, info_high);
1671 __ Bind(&info_low->label);
1672 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001673}
1674
Alexey Frunze627c1a02017-01-30 19:28:14 -08001675Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1676 dex::StringIndex string_index,
1677 Handle<mirror::String> handle) {
1678 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1679 reinterpret_cast64<uint64_t>(handle.GetReference()));
1680 return jit_string_patches_.GetOrCreate(
1681 StringReference(&dex_file, string_index),
1682 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1683}
1684
1685Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1686 dex::TypeIndex type_index,
1687 Handle<mirror::Class> handle) {
1688 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1689 reinterpret_cast64<uint64_t>(handle.GetReference()));
1690 return jit_class_patches_.GetOrCreate(
1691 TypeReference(&dex_file, type_index),
1692 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1693}
1694
1695void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1696 const uint8_t* roots_data,
1697 const Literal* literal,
1698 uint64_t index_in_table) const {
1699 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1700 uintptr_t address =
1701 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1702 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1703}
1704
1705void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1706 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001707 const StringReference& string_reference = entry.first;
1708 Literal* table_entry_literal = entry.second;
1709 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001710 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001711 uint64_t index_in_table = it->second;
1712 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001713 }
1714 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001715 const TypeReference& type_reference = entry.first;
1716 Literal* table_entry_literal = entry.second;
1717 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001718 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001719 uint64_t index_in_table = it->second;
1720 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001721 }
1722}
1723
David Brazdil58282f42016-01-14 12:45:10 +00001724void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001725 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1726 blocked_core_registers_[ZERO] = true;
1727 blocked_core_registers_[K0] = true;
1728 blocked_core_registers_[K1] = true;
1729 blocked_core_registers_[GP] = true;
1730 blocked_core_registers_[SP] = true;
1731 blocked_core_registers_[RA] = true;
1732
Lazar Trsicd9672662015-09-03 17:33:01 +02001733 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1734 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001735 blocked_core_registers_[AT] = true;
1736 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001737 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738 blocked_fpu_registers_[FTMP] = true;
1739
1740 // Reserve suspend and thread registers.
1741 blocked_core_registers_[S0] = true;
1742 blocked_core_registers_[TR] = true;
1743
1744 // Reserve T9 for function calls
1745 blocked_core_registers_[T9] = true;
1746
Goran Jakovljevic782be112016-06-21 12:39:04 +02001747 if (GetGraph()->IsDebuggable()) {
1748 // Stubs do not save callee-save floating point registers. If the graph
1749 // is debuggable, we need to deal with these registers differently. For
1750 // now, just block them.
1751 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1752 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1753 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001754 }
1755}
1756
Alexey Frunze4dda3372015-06-01 18:31:49 -07001757size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1758 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001759 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001760}
1761
1762size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1763 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001764 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001765}
1766
1767size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001768 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1769 FpuRegister(reg_id),
1770 SP,
1771 stack_index);
1772 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001773}
1774
1775size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001776 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1777 FpuRegister(reg_id),
1778 SP,
1779 stack_index);
1780 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001781}
1782
1783void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001784 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001785}
1786
1787void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001788 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001789}
1790
Calin Juravle175dc732015-08-25 15:42:32 +01001791void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001792 HInstruction* instruction,
1793 uint32_t dex_pc,
1794 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001795 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001796 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001797 if (EntrypointRequiresStackMap(entrypoint)) {
1798 RecordPcInfo(instruction, dex_pc, slow_path);
1799 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001800}
1801
Alexey Frunze15958152017-02-09 19:08:30 -08001802void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1803 HInstruction* instruction,
1804 SlowPathCode* slow_path) {
1805 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1806 GenerateInvokeRuntime(entry_point_offset);
1807}
1808
1809void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1810 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1811 __ Jalr(T9);
1812 __ Nop();
1813}
1814
Alexey Frunze4dda3372015-06-01 18:31:49 -07001815void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1816 GpuRegister class_reg) {
1817 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1818 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1819 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001820 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1821 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001822 __ Bind(slow_path->GetExitLabel());
1823}
1824
1825void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1826 __ Sync(0); // only stype 0 is supported
1827}
1828
1829void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1830 HBasicBlock* successor) {
1831 SuspendCheckSlowPathMIPS64* slow_path =
1832 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1833 codegen_->AddSlowPath(slow_path);
1834
1835 __ LoadFromOffset(kLoadUnsignedHalfword,
1836 TMP,
1837 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001838 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001839 if (successor == nullptr) {
1840 __ Bnezc(TMP, slow_path->GetEntryLabel());
1841 __ Bind(slow_path->GetReturnLabel());
1842 } else {
1843 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001844 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001845 // slow_path will return to GetLabelOf(successor).
1846 }
1847}
1848
1849InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1850 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001851 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001852 assembler_(codegen->GetAssembler()),
1853 codegen_(codegen) {}
1854
1855void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1856 DCHECK_EQ(instruction->InputCount(), 2U);
1857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1858 Primitive::Type type = instruction->GetResultType();
1859 switch (type) {
1860 case Primitive::kPrimInt:
1861 case Primitive::kPrimLong: {
1862 locations->SetInAt(0, Location::RequiresRegister());
1863 HInstruction* right = instruction->InputAt(1);
1864 bool can_use_imm = false;
1865 if (right->IsConstant()) {
1866 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1867 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1868 can_use_imm = IsUint<16>(imm);
1869 } else if (instruction->IsAdd()) {
1870 can_use_imm = IsInt<16>(imm);
1871 } else {
1872 DCHECK(instruction->IsSub());
1873 can_use_imm = IsInt<16>(-imm);
1874 }
1875 }
1876 if (can_use_imm)
1877 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1878 else
1879 locations->SetInAt(1, Location::RequiresRegister());
1880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1881 }
1882 break;
1883
1884 case Primitive::kPrimFloat:
1885 case Primitive::kPrimDouble:
1886 locations->SetInAt(0, Location::RequiresFpuRegister());
1887 locations->SetInAt(1, Location::RequiresFpuRegister());
1888 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1889 break;
1890
1891 default:
1892 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1893 }
1894}
1895
1896void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1897 Primitive::Type type = instruction->GetType();
1898 LocationSummary* locations = instruction->GetLocations();
1899
1900 switch (type) {
1901 case Primitive::kPrimInt:
1902 case Primitive::kPrimLong: {
1903 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1904 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1905 Location rhs_location = locations->InAt(1);
1906
1907 GpuRegister rhs_reg = ZERO;
1908 int64_t rhs_imm = 0;
1909 bool use_imm = rhs_location.IsConstant();
1910 if (use_imm) {
1911 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1912 } else {
1913 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1914 }
1915
1916 if (instruction->IsAnd()) {
1917 if (use_imm)
1918 __ Andi(dst, lhs, rhs_imm);
1919 else
1920 __ And(dst, lhs, rhs_reg);
1921 } else if (instruction->IsOr()) {
1922 if (use_imm)
1923 __ Ori(dst, lhs, rhs_imm);
1924 else
1925 __ Or(dst, lhs, rhs_reg);
1926 } else if (instruction->IsXor()) {
1927 if (use_imm)
1928 __ Xori(dst, lhs, rhs_imm);
1929 else
1930 __ Xor(dst, lhs, rhs_reg);
1931 } else if (instruction->IsAdd()) {
1932 if (type == Primitive::kPrimInt) {
1933 if (use_imm)
1934 __ Addiu(dst, lhs, rhs_imm);
1935 else
1936 __ Addu(dst, lhs, rhs_reg);
1937 } else {
1938 if (use_imm)
1939 __ Daddiu(dst, lhs, rhs_imm);
1940 else
1941 __ Daddu(dst, lhs, rhs_reg);
1942 }
1943 } else {
1944 DCHECK(instruction->IsSub());
1945 if (type == Primitive::kPrimInt) {
1946 if (use_imm)
1947 __ Addiu(dst, lhs, -rhs_imm);
1948 else
1949 __ Subu(dst, lhs, rhs_reg);
1950 } else {
1951 if (use_imm)
1952 __ Daddiu(dst, lhs, -rhs_imm);
1953 else
1954 __ Dsubu(dst, lhs, rhs_reg);
1955 }
1956 }
1957 break;
1958 }
1959 case Primitive::kPrimFloat:
1960 case Primitive::kPrimDouble: {
1961 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1962 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1963 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1964 if (instruction->IsAdd()) {
1965 if (type == Primitive::kPrimFloat)
1966 __ AddS(dst, lhs, rhs);
1967 else
1968 __ AddD(dst, lhs, rhs);
1969 } else if (instruction->IsSub()) {
1970 if (type == Primitive::kPrimFloat)
1971 __ SubS(dst, lhs, rhs);
1972 else
1973 __ SubD(dst, lhs, rhs);
1974 } else {
1975 LOG(FATAL) << "Unexpected floating-point binary operation";
1976 }
1977 break;
1978 }
1979 default:
1980 LOG(FATAL) << "Unexpected binary operation type " << type;
1981 }
1982}
1983
1984void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001985 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001986
1987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1988 Primitive::Type type = instr->GetResultType();
1989 switch (type) {
1990 case Primitive::kPrimInt:
1991 case Primitive::kPrimLong: {
1992 locations->SetInAt(0, Location::RequiresRegister());
1993 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001995 break;
1996 }
1997 default:
1998 LOG(FATAL) << "Unexpected shift type " << type;
1999 }
2000}
2001
2002void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002003 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002004 LocationSummary* locations = instr->GetLocations();
2005 Primitive::Type type = instr->GetType();
2006
2007 switch (type) {
2008 case Primitive::kPrimInt:
2009 case Primitive::kPrimLong: {
2010 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2011 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2012 Location rhs_location = locations->InAt(1);
2013
2014 GpuRegister rhs_reg = ZERO;
2015 int64_t rhs_imm = 0;
2016 bool use_imm = rhs_location.IsConstant();
2017 if (use_imm) {
2018 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2019 } else {
2020 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2021 }
2022
2023 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002024 uint32_t shift_value = rhs_imm &
2025 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026
Alexey Frunze92d90602015-12-18 18:16:36 -08002027 if (shift_value == 0) {
2028 if (dst != lhs) {
2029 __ Move(dst, lhs);
2030 }
2031 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032 if (instr->IsShl()) {
2033 __ Sll(dst, lhs, shift_value);
2034 } else if (instr->IsShr()) {
2035 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002036 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002037 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002038 } else {
2039 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 }
2041 } else {
2042 if (shift_value < 32) {
2043 if (instr->IsShl()) {
2044 __ Dsll(dst, lhs, shift_value);
2045 } else if (instr->IsShr()) {
2046 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002047 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002048 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002049 } else {
2050 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002051 }
2052 } else {
2053 shift_value -= 32;
2054 if (instr->IsShl()) {
2055 __ Dsll32(dst, lhs, shift_value);
2056 } else if (instr->IsShr()) {
2057 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002058 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002059 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002060 } else {
2061 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002062 }
2063 }
2064 }
2065 } else {
2066 if (type == Primitive::kPrimInt) {
2067 if (instr->IsShl()) {
2068 __ Sllv(dst, lhs, rhs_reg);
2069 } else if (instr->IsShr()) {
2070 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002071 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002072 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002073 } else {
2074 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002075 }
2076 } else {
2077 if (instr->IsShl()) {
2078 __ Dsllv(dst, lhs, rhs_reg);
2079 } else if (instr->IsShr()) {
2080 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002081 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002082 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002083 } else {
2084 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002085 }
2086 }
2087 }
2088 break;
2089 }
2090 default:
2091 LOG(FATAL) << "Unexpected shift operation type " << type;
2092 }
2093}
2094
2095void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2096 HandleBinaryOp(instruction);
2097}
2098
2099void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2100 HandleBinaryOp(instruction);
2101}
2102
2103void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2104 HandleBinaryOp(instruction);
2105}
2106
2107void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2108 HandleBinaryOp(instruction);
2109}
2110
2111void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002112 Primitive::Type type = instruction->GetType();
2113 bool object_array_get_with_read_barrier =
2114 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002115 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002116 new (GetGraph()->GetArena()) LocationSummary(instruction,
2117 object_array_get_with_read_barrier
2118 ? LocationSummary::kCallOnSlowPath
2119 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002120 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2121 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002123 locations->SetInAt(0, Location::RequiresRegister());
2124 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002125 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002126 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2127 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002128 // The output overlaps in the case of an object array get with
2129 // read barriers enabled: we do not want the move to overwrite the
2130 // array's location, as we need it to emit the read barrier.
2131 locations->SetOut(Location::RequiresRegister(),
2132 object_array_get_with_read_barrier
2133 ? Location::kOutputOverlap
2134 : Location::kNoOutputOverlap);
2135 }
2136 // We need a temporary register for the read barrier marking slow
2137 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2138 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002139 bool temp_needed = instruction->GetIndex()->IsConstant()
2140 ? !kBakerReadBarrierThunksEnableForFields
2141 : !kBakerReadBarrierThunksEnableForArrays;
2142 if (temp_needed) {
2143 locations->AddTemp(Location::RequiresRegister());
2144 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002145 }
2146}
2147
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002148static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2149 auto null_checker = [codegen, instruction]() {
2150 codegen->MaybeRecordImplicitNullCheck(instruction);
2151 };
2152 return null_checker;
2153}
2154
Alexey Frunze4dda3372015-06-01 18:31:49 -07002155void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2156 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002157 Location obj_loc = locations->InAt(0);
2158 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2159 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002160 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002161 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002162 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002163
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002164 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002165 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2166 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002167 switch (type) {
2168 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002169 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002170 if (index.IsConstant()) {
2171 size_t offset =
2172 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002173 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002174 } else {
2175 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002176 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002177 }
2178 break;
2179 }
2180
2181 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002182 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002183 if (index.IsConstant()) {
2184 size_t offset =
2185 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002186 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002187 } else {
2188 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002189 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 }
2191 break;
2192 }
2193
2194 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002195 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002196 if (index.IsConstant()) {
2197 size_t offset =
2198 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002199 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002201 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002202 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203 }
2204 break;
2205 }
2206
2207 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002208 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002209 if (maybe_compressed_char_at) {
2210 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002211 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002212 __ Dext(TMP, TMP, 0, 1);
2213 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2214 "Expecting 0=compressed, 1=uncompressed");
2215 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002216 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002217 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2218 if (maybe_compressed_char_at) {
2219 Mips64Label uncompressed_load, done;
2220 __ Bnezc(TMP, &uncompressed_load);
2221 __ LoadFromOffset(kLoadUnsignedByte,
2222 out,
2223 obj,
2224 data_offset + (const_index << TIMES_1));
2225 __ Bc(&done);
2226 __ Bind(&uncompressed_load);
2227 __ LoadFromOffset(kLoadUnsignedHalfword,
2228 out,
2229 obj,
2230 data_offset + (const_index << TIMES_2));
2231 __ Bind(&done);
2232 } else {
2233 __ LoadFromOffset(kLoadUnsignedHalfword,
2234 out,
2235 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002236 data_offset + (const_index << TIMES_2),
2237 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002238 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002239 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002240 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2241 if (maybe_compressed_char_at) {
2242 Mips64Label uncompressed_load, done;
2243 __ Bnezc(TMP, &uncompressed_load);
2244 __ Daddu(TMP, obj, index_reg);
2245 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2246 __ Bc(&done);
2247 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002248 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002249 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2250 __ Bind(&done);
2251 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002252 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002253 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002254 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002255 }
2256 break;
2257 }
2258
Alexey Frunze15958152017-02-09 19:08:30 -08002259 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002260 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002261 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002262 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2263 if (index.IsConstant()) {
2264 size_t offset =
2265 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002266 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002268 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002269 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002270 }
2271 break;
2272 }
2273
Alexey Frunze15958152017-02-09 19:08:30 -08002274 case Primitive::kPrimNot: {
2275 static_assert(
2276 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2277 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2278 // /* HeapReference<Object> */ out =
2279 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2280 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002281 bool temp_needed = index.IsConstant()
2282 ? !kBakerReadBarrierThunksEnableForFields
2283 : !kBakerReadBarrierThunksEnableForArrays;
2284 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002285 // Note that a potential implicit null check is handled in this
2286 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002287 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2288 if (index.IsConstant()) {
2289 // Array load with a constant index can be treated as a field load.
2290 size_t offset =
2291 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2292 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2293 out_loc,
2294 obj,
2295 offset,
2296 temp,
2297 /* needs_null_check */ false);
2298 } else {
2299 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2300 out_loc,
2301 obj,
2302 data_offset,
2303 index,
2304 temp,
2305 /* needs_null_check */ false);
2306 }
Alexey Frunze15958152017-02-09 19:08:30 -08002307 } else {
2308 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2309 if (index.IsConstant()) {
2310 size_t offset =
2311 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2312 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2313 // If read barriers are enabled, emit read barriers other than
2314 // Baker's using a slow path (and also unpoison the loaded
2315 // reference, if heap poisoning is enabled).
2316 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2317 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002318 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002319 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2320 // If read barriers are enabled, emit read barriers other than
2321 // Baker's using a slow path (and also unpoison the loaded
2322 // reference, if heap poisoning is enabled).
2323 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2324 out_loc,
2325 out_loc,
2326 obj_loc,
2327 data_offset,
2328 index);
2329 }
2330 }
2331 break;
2332 }
2333
Alexey Frunze4dda3372015-06-01 18:31:49 -07002334 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002335 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002336 if (index.IsConstant()) {
2337 size_t offset =
2338 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002339 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002340 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002341 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002342 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 }
2344 break;
2345 }
2346
2347 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002348 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002349 if (index.IsConstant()) {
2350 size_t offset =
2351 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002352 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002353 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002354 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002355 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002356 }
2357 break;
2358 }
2359
2360 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002361 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002362 if (index.IsConstant()) {
2363 size_t offset =
2364 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002365 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002366 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002367 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002368 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002369 }
2370 break;
2371 }
2372
2373 case Primitive::kPrimVoid:
2374 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2375 UNREACHABLE();
2376 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377}
2378
2379void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2381 locations->SetInAt(0, Location::RequiresRegister());
2382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2383}
2384
2385void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2386 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002387 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002388 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2389 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2390 __ LoadFromOffset(kLoadWord, out, obj, offset);
2391 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002392 // Mask out compression flag from String's array length.
2393 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2394 __ Srl(out, out, 1u);
2395 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002396}
2397
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002398Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2399 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2400 ? Location::ConstantLocation(instruction->AsConstant())
2401 : Location::RequiresRegister();
2402}
2403
2404Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2405 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2406 // We can store a non-zero float or double constant without first loading it into the FPU,
2407 // but we should only prefer this if the constant has a single use.
2408 if (instruction->IsConstant() &&
2409 (instruction->AsConstant()->IsZeroBitPattern() ||
2410 instruction->GetUses().HasExactlyOneElement())) {
2411 return Location::ConstantLocation(instruction->AsConstant());
2412 // Otherwise fall through and require an FPU register for the constant.
2413 }
2414 return Location::RequiresFpuRegister();
2415}
2416
Alexey Frunze4dda3372015-06-01 18:31:49 -07002417void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002418 Primitive::Type value_type = instruction->GetComponentType();
2419
2420 bool needs_write_barrier =
2421 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2422 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2423
Alexey Frunze4dda3372015-06-01 18:31:49 -07002424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2425 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002426 may_need_runtime_call_for_type_check ?
2427 LocationSummary::kCallOnSlowPath :
2428 LocationSummary::kNoCall);
2429
2430 locations->SetInAt(0, Location::RequiresRegister());
2431 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2432 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2433 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002434 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002435 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2436 }
2437 if (needs_write_barrier) {
2438 // Temporary register for the write barrier.
2439 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002440 }
2441}
2442
2443void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2444 LocationSummary* locations = instruction->GetLocations();
2445 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2446 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002447 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002449 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002450 bool needs_write_barrier =
2451 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002452 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002453 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002454
2455 switch (value_type) {
2456 case Primitive::kPrimBoolean:
2457 case Primitive::kPrimByte: {
2458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002459 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002460 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002461 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002462 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2463 }
2464 if (value_location.IsConstant()) {
2465 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2466 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2467 } else {
2468 GpuRegister value = value_location.AsRegister<GpuRegister>();
2469 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002470 }
2471 break;
2472 }
2473
2474 case Primitive::kPrimShort:
2475 case Primitive::kPrimChar: {
2476 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002477 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002478 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002479 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002480 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002481 }
2482 if (value_location.IsConstant()) {
2483 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2484 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2485 } else {
2486 GpuRegister value = value_location.AsRegister<GpuRegister>();
2487 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002488 }
2489 break;
2490 }
2491
Alexey Frunze15958152017-02-09 19:08:30 -08002492 case Primitive::kPrimInt: {
2493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2494 if (index.IsConstant()) {
2495 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2496 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002497 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002498 }
2499 if (value_location.IsConstant()) {
2500 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2501 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2502 } else {
2503 GpuRegister value = value_location.AsRegister<GpuRegister>();
2504 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2505 }
2506 break;
2507 }
2508
Alexey Frunze4dda3372015-06-01 18:31:49 -07002509 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002510 if (value_location.IsConstant()) {
2511 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002512 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002513 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002514 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002515 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002516 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002517 }
Alexey Frunze15958152017-02-09 19:08:30 -08002518 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2519 DCHECK_EQ(value, 0);
2520 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2521 DCHECK(!needs_write_barrier);
2522 DCHECK(!may_need_runtime_call_for_type_check);
2523 break;
2524 }
2525
2526 DCHECK(needs_write_barrier);
2527 GpuRegister value = value_location.AsRegister<GpuRegister>();
2528 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2529 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2530 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2531 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2532 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2533 Mips64Label done;
2534 SlowPathCodeMIPS64* slow_path = nullptr;
2535
2536 if (may_need_runtime_call_for_type_check) {
2537 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2538 codegen_->AddSlowPath(slow_path);
2539 if (instruction->GetValueCanBeNull()) {
2540 Mips64Label non_zero;
2541 __ Bnezc(value, &non_zero);
2542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2543 if (index.IsConstant()) {
2544 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002545 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002546 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002547 }
Alexey Frunze15958152017-02-09 19:08:30 -08002548 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2549 __ Bc(&done);
2550 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002551 }
Alexey Frunze15958152017-02-09 19:08:30 -08002552
2553 // Note that when read barriers are enabled, the type checks
2554 // are performed without read barriers. This is fine, even in
2555 // the case where a class object is in the from-space after
2556 // the flip, as a comparison involving such a type would not
2557 // produce a false positive; it may of course produce a false
2558 // negative, in which case we would take the ArraySet slow
2559 // path.
2560
2561 // /* HeapReference<Class> */ temp1 = obj->klass_
2562 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2563 __ MaybeUnpoisonHeapReference(temp1);
2564
2565 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2566 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2567 // /* HeapReference<Class> */ temp2 = value->klass_
2568 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2569 // If heap poisoning is enabled, no need to unpoison `temp1`
2570 // nor `temp2`, as we are comparing two poisoned references.
2571
2572 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2573 Mips64Label do_put;
2574 __ Beqc(temp1, temp2, &do_put);
2575 // If heap poisoning is enabled, the `temp1` reference has
2576 // not been unpoisoned yet; unpoison it now.
2577 __ MaybeUnpoisonHeapReference(temp1);
2578
2579 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2580 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2581 // If heap poisoning is enabled, no need to unpoison
2582 // `temp1`, as we are comparing against null below.
2583 __ Bnezc(temp1, slow_path->GetEntryLabel());
2584 __ Bind(&do_put);
2585 } else {
2586 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2587 }
2588 }
2589
2590 GpuRegister source = value;
2591 if (kPoisonHeapReferences) {
2592 // Note that in the case where `value` is a null reference,
2593 // we do not enter this block, as a null reference does not
2594 // need poisoning.
2595 __ Move(temp1, value);
2596 __ PoisonHeapReference(temp1);
2597 source = temp1;
2598 }
2599
2600 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2601 if (index.IsConstant()) {
2602 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002604 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002605 }
2606 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2607
2608 if (!may_need_runtime_call_for_type_check) {
2609 codegen_->MaybeRecordImplicitNullCheck(instruction);
2610 }
2611
2612 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2613
2614 if (done.IsLinked()) {
2615 __ Bind(&done);
2616 }
2617
2618 if (slow_path != nullptr) {
2619 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002620 }
2621 break;
2622 }
2623
2624 case Primitive::kPrimLong: {
2625 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002626 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002627 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002628 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002629 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002630 }
2631 if (value_location.IsConstant()) {
2632 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2633 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2634 } else {
2635 GpuRegister value = value_location.AsRegister<GpuRegister>();
2636 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002637 }
2638 break;
2639 }
2640
2641 case Primitive::kPrimFloat: {
2642 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002643 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002644 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002645 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002646 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002647 }
2648 if (value_location.IsConstant()) {
2649 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2650 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2651 } else {
2652 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2653 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654 }
2655 break;
2656 }
2657
2658 case Primitive::kPrimDouble: {
2659 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002660 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002661 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002662 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002663 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002664 }
2665 if (value_location.IsConstant()) {
2666 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2667 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2668 } else {
2669 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2670 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002671 }
2672 break;
2673 }
2674
2675 case Primitive::kPrimVoid:
2676 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2677 UNREACHABLE();
2678 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002679}
2680
2681void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002682 RegisterSet caller_saves = RegisterSet::Empty();
2683 InvokeRuntimeCallingConvention calling_convention;
2684 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2685 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2686 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002687 locations->SetInAt(0, Location::RequiresRegister());
2688 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689}
2690
2691void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2692 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002693 BoundsCheckSlowPathMIPS64* slow_path =
2694 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002695 codegen_->AddSlowPath(slow_path);
2696
2697 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2698 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2699
2700 // length is limited by the maximum positive signed 32-bit integer.
2701 // Unsigned comparison of length and index checks for index < 0
2702 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002703 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002704}
2705
Alexey Frunze15958152017-02-09 19:08:30 -08002706// Temp is used for read barrier.
2707static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2708 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002709 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002710 (kUseBakerReadBarrier ||
2711 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2712 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2713 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2714 return 1;
2715 }
2716 return 0;
2717}
2718
2719// Extra temp is used for read barrier.
2720static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2721 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2722}
2723
Alexey Frunze4dda3372015-06-01 18:31:49 -07002724void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002725 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2726 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2727
2728 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2729 switch (type_check_kind) {
2730 case TypeCheckKind::kExactCheck:
2731 case TypeCheckKind::kAbstractClassCheck:
2732 case TypeCheckKind::kClassHierarchyCheck:
2733 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002734 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002735 ? LocationSummary::kCallOnSlowPath
2736 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2737 break;
2738 case TypeCheckKind::kArrayCheck:
2739 case TypeCheckKind::kUnresolvedCheck:
2740 case TypeCheckKind::kInterfaceCheck:
2741 call_kind = LocationSummary::kCallOnSlowPath;
2742 break;
2743 }
2744
2745 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746 locations->SetInAt(0, Location::RequiresRegister());
2747 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002748 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002749}
2750
2751void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002752 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002753 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002754 Location obj_loc = locations->InAt(0);
2755 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002756 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002757 Location temp_loc = locations->GetTemp(0);
2758 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2759 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2760 DCHECK_LE(num_temps, 2u);
2761 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002762 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2763 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2764 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2765 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2766 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2767 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2768 const uint32_t object_array_data_offset =
2769 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2770 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002771
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002772 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2773 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2774 // read barriers is done for performance and code size reasons.
2775 bool is_type_check_slow_path_fatal = false;
2776 if (!kEmitCompilerReadBarrier) {
2777 is_type_check_slow_path_fatal =
2778 (type_check_kind == TypeCheckKind::kExactCheck ||
2779 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2780 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2781 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2782 !instruction->CanThrowIntoCatchBlock();
2783 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002784 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002785 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2786 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002787 codegen_->AddSlowPath(slow_path);
2788
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002789 // Avoid this check if we know `obj` is not null.
2790 if (instruction->MustDoNullCheck()) {
2791 __ Beqzc(obj, &done);
2792 }
2793
2794 switch (type_check_kind) {
2795 case TypeCheckKind::kExactCheck:
2796 case TypeCheckKind::kArrayCheck: {
2797 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002798 GenerateReferenceLoadTwoRegisters(instruction,
2799 temp_loc,
2800 obj_loc,
2801 class_offset,
2802 maybe_temp2_loc,
2803 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002804 // Jump to slow path for throwing the exception or doing a
2805 // more involved array check.
2806 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2807 break;
2808 }
2809
2810 case TypeCheckKind::kAbstractClassCheck: {
2811 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002812 GenerateReferenceLoadTwoRegisters(instruction,
2813 temp_loc,
2814 obj_loc,
2815 class_offset,
2816 maybe_temp2_loc,
2817 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002818 // If the class is abstract, we eagerly fetch the super class of the
2819 // object to avoid doing a comparison we know will fail.
2820 Mips64Label loop;
2821 __ Bind(&loop);
2822 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002823 GenerateReferenceLoadOneRegister(instruction,
2824 temp_loc,
2825 super_offset,
2826 maybe_temp2_loc,
2827 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002828 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2829 // exception.
2830 __ Beqzc(temp, slow_path->GetEntryLabel());
2831 // Otherwise, compare the classes.
2832 __ Bnec(temp, cls, &loop);
2833 break;
2834 }
2835
2836 case TypeCheckKind::kClassHierarchyCheck: {
2837 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002838 GenerateReferenceLoadTwoRegisters(instruction,
2839 temp_loc,
2840 obj_loc,
2841 class_offset,
2842 maybe_temp2_loc,
2843 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002844 // Walk over the class hierarchy to find a match.
2845 Mips64Label loop;
2846 __ Bind(&loop);
2847 __ Beqc(temp, cls, &done);
2848 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002849 GenerateReferenceLoadOneRegister(instruction,
2850 temp_loc,
2851 super_offset,
2852 maybe_temp2_loc,
2853 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002854 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2855 // exception. Otherwise, jump to the beginning of the loop.
2856 __ Bnezc(temp, &loop);
2857 __ Bc(slow_path->GetEntryLabel());
2858 break;
2859 }
2860
2861 case TypeCheckKind::kArrayObjectCheck: {
2862 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002863 GenerateReferenceLoadTwoRegisters(instruction,
2864 temp_loc,
2865 obj_loc,
2866 class_offset,
2867 maybe_temp2_loc,
2868 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002869 // Do an exact check.
2870 __ Beqc(temp, cls, &done);
2871 // Otherwise, we need to check that the object's class is a non-primitive array.
2872 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002873 GenerateReferenceLoadOneRegister(instruction,
2874 temp_loc,
2875 component_offset,
2876 maybe_temp2_loc,
2877 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002878 // If the component type is null, jump to the slow path to throw the exception.
2879 __ Beqzc(temp, slow_path->GetEntryLabel());
2880 // Otherwise, the object is indeed an array, further check that this component
2881 // type is not a primitive type.
2882 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2883 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2884 __ Bnezc(temp, slow_path->GetEntryLabel());
2885 break;
2886 }
2887
2888 case TypeCheckKind::kUnresolvedCheck:
2889 // We always go into the type check slow path for the unresolved check case.
2890 // We cannot directly call the CheckCast runtime entry point
2891 // without resorting to a type checking slow path here (i.e. by
2892 // calling InvokeRuntime directly), as it would require to
2893 // assign fixed registers for the inputs of this HInstanceOf
2894 // instruction (following the runtime calling convention), which
2895 // might be cluttered by the potential first read barrier
2896 // emission at the beginning of this method.
2897 __ Bc(slow_path->GetEntryLabel());
2898 break;
2899
2900 case TypeCheckKind::kInterfaceCheck: {
2901 // Avoid read barriers to improve performance of the fast path. We can not get false
2902 // positives by doing this.
2903 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002904 GenerateReferenceLoadTwoRegisters(instruction,
2905 temp_loc,
2906 obj_loc,
2907 class_offset,
2908 maybe_temp2_loc,
2909 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002910 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002911 GenerateReferenceLoadTwoRegisters(instruction,
2912 temp_loc,
2913 temp_loc,
2914 iftable_offset,
2915 maybe_temp2_loc,
2916 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002917 // Iftable is never null.
2918 __ Lw(TMP, temp, array_length_offset);
2919 // Loop through the iftable and check if any class matches.
2920 Mips64Label loop;
2921 __ Bind(&loop);
2922 __ Beqzc(TMP, slow_path->GetEntryLabel());
2923 __ Lwu(AT, temp, object_array_data_offset);
2924 __ MaybeUnpoisonHeapReference(AT);
2925 // Go to next interface.
2926 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2927 __ Addiu(TMP, TMP, -2);
2928 // Compare the classes and continue the loop if they do not match.
2929 __ Bnec(AT, cls, &loop);
2930 break;
2931 }
2932 }
2933
2934 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002935 __ Bind(slow_path->GetExitLabel());
2936}
2937
2938void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2939 LocationSummary* locations =
2940 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2941 locations->SetInAt(0, Location::RequiresRegister());
2942 if (check->HasUses()) {
2943 locations->SetOut(Location::SameAsFirstInput());
2944 }
2945}
2946
2947void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2948 // We assume the class is not null.
2949 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2950 check->GetLoadClass(),
2951 check,
2952 check->GetDexPc(),
2953 true);
2954 codegen_->AddSlowPath(slow_path);
2955 GenerateClassInitializationCheck(slow_path,
2956 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2957}
2958
2959void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2960 Primitive::Type in_type = compare->InputAt(0)->GetType();
2961
Alexey Frunze299a9392015-12-08 16:08:02 -08002962 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002963
2964 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002965 case Primitive::kPrimBoolean:
2966 case Primitive::kPrimByte:
2967 case Primitive::kPrimShort:
2968 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002969 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970 case Primitive::kPrimLong:
2971 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002972 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2974 break;
2975
2976 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002977 case Primitive::kPrimDouble:
2978 locations->SetInAt(0, Location::RequiresFpuRegister());
2979 locations->SetInAt(1, Location::RequiresFpuRegister());
2980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002981 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002982
2983 default:
2984 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2985 }
2986}
2987
2988void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2989 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002990 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002991 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2992
2993 // 0 if: left == right
2994 // 1 if: left > right
2995 // -1 if: left < right
2996 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002997 case Primitive::kPrimBoolean:
2998 case Primitive::kPrimByte:
2999 case Primitive::kPrimShort:
3000 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003001 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003003 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003004 Location rhs_location = locations->InAt(1);
3005 bool use_imm = rhs_location.IsConstant();
3006 GpuRegister rhs = ZERO;
3007 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003008 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08003009 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3010 if (value != 0) {
3011 rhs = AT;
3012 __ LoadConst64(rhs, value);
3013 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003014 } else {
3015 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3016 if (value != 0) {
3017 rhs = AT;
3018 __ LoadConst32(rhs, value);
3019 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003020 }
3021 } else {
3022 rhs = rhs_location.AsRegister<GpuRegister>();
3023 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003024 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003025 __ Slt(res, rhs, lhs);
3026 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003027 break;
3028 }
3029
Alexey Frunze299a9392015-12-08 16:08:02 -08003030 case Primitive::kPrimFloat: {
3031 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3032 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3033 Mips64Label done;
3034 __ CmpEqS(FTMP, lhs, rhs);
3035 __ LoadConst32(res, 0);
3036 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003037 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003038 __ CmpLtS(FTMP, lhs, rhs);
3039 __ LoadConst32(res, -1);
3040 __ Bc1nez(FTMP, &done);
3041 __ LoadConst32(res, 1);
3042 } else {
3043 __ CmpLtS(FTMP, rhs, lhs);
3044 __ LoadConst32(res, 1);
3045 __ Bc1nez(FTMP, &done);
3046 __ LoadConst32(res, -1);
3047 }
3048 __ Bind(&done);
3049 break;
3050 }
3051
Alexey Frunze4dda3372015-06-01 18:31:49 -07003052 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003053 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3054 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3055 Mips64Label done;
3056 __ CmpEqD(FTMP, lhs, rhs);
3057 __ LoadConst32(res, 0);
3058 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003059 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003060 __ CmpLtD(FTMP, lhs, rhs);
3061 __ LoadConst32(res, -1);
3062 __ Bc1nez(FTMP, &done);
3063 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003064 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003065 __ CmpLtD(FTMP, rhs, lhs);
3066 __ LoadConst32(res, 1);
3067 __ Bc1nez(FTMP, &done);
3068 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003069 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003070 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071 break;
3072 }
3073
3074 default:
3075 LOG(FATAL) << "Unimplemented compare type " << in_type;
3076 }
3077}
3078
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003079void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003081 switch (instruction->InputAt(0)->GetType()) {
3082 default:
3083 case Primitive::kPrimLong:
3084 locations->SetInAt(0, Location::RequiresRegister());
3085 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3086 break;
3087
3088 case Primitive::kPrimFloat:
3089 case Primitive::kPrimDouble:
3090 locations->SetInAt(0, Location::RequiresFpuRegister());
3091 locations->SetInAt(1, Location::RequiresFpuRegister());
3092 break;
3093 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003094 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003095 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3096 }
3097}
3098
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003099void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003100 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003101 return;
3102 }
3103
Alexey Frunze299a9392015-12-08 16:08:02 -08003104 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003105 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003106 switch (type) {
3107 default:
3108 // Integer case.
3109 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3110 return;
3111 case Primitive::kPrimLong:
3112 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3113 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08003114 case Primitive::kPrimFloat:
3115 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003116 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3117 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003118 }
3119}
3120
Alexey Frunzec857c742015-09-23 15:12:39 -07003121void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3122 DCHECK(instruction->IsDiv() || instruction->IsRem());
3123 Primitive::Type type = instruction->GetResultType();
3124
3125 LocationSummary* locations = instruction->GetLocations();
3126 Location second = locations->InAt(1);
3127 DCHECK(second.IsConstant());
3128
3129 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3130 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3131 int64_t imm = Int64FromConstant(second.GetConstant());
3132 DCHECK(imm == 1 || imm == -1);
3133
3134 if (instruction->IsRem()) {
3135 __ Move(out, ZERO);
3136 } else {
3137 if (imm == -1) {
3138 if (type == Primitive::kPrimInt) {
3139 __ Subu(out, ZERO, dividend);
3140 } else {
3141 DCHECK_EQ(type, Primitive::kPrimLong);
3142 __ Dsubu(out, ZERO, dividend);
3143 }
3144 } else if (out != dividend) {
3145 __ Move(out, dividend);
3146 }
3147 }
3148}
3149
3150void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3151 DCHECK(instruction->IsDiv() || instruction->IsRem());
3152 Primitive::Type type = instruction->GetResultType();
3153
3154 LocationSummary* locations = instruction->GetLocations();
3155 Location second = locations->InAt(1);
3156 DCHECK(second.IsConstant());
3157
3158 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3159 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3160 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003161 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003162 int ctz_imm = CTZ(abs_imm);
3163
3164 if (instruction->IsDiv()) {
3165 if (type == Primitive::kPrimInt) {
3166 if (ctz_imm == 1) {
3167 // Fast path for division by +/-2, which is very common.
3168 __ Srl(TMP, dividend, 31);
3169 } else {
3170 __ Sra(TMP, dividend, 31);
3171 __ Srl(TMP, TMP, 32 - ctz_imm);
3172 }
3173 __ Addu(out, dividend, TMP);
3174 __ Sra(out, out, ctz_imm);
3175 if (imm < 0) {
3176 __ Subu(out, ZERO, out);
3177 }
3178 } else {
3179 DCHECK_EQ(type, Primitive::kPrimLong);
3180 if (ctz_imm == 1) {
3181 // Fast path for division by +/-2, which is very common.
3182 __ Dsrl32(TMP, dividend, 31);
3183 } else {
3184 __ Dsra32(TMP, dividend, 31);
3185 if (ctz_imm > 32) {
3186 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3187 } else {
3188 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3189 }
3190 }
3191 __ Daddu(out, dividend, TMP);
3192 if (ctz_imm < 32) {
3193 __ Dsra(out, out, ctz_imm);
3194 } else {
3195 __ Dsra32(out, out, ctz_imm - 32);
3196 }
3197 if (imm < 0) {
3198 __ Dsubu(out, ZERO, out);
3199 }
3200 }
3201 } else {
3202 if (type == Primitive::kPrimInt) {
3203 if (ctz_imm == 1) {
3204 // Fast path for modulo +/-2, which is very common.
3205 __ Sra(TMP, dividend, 31);
3206 __ Subu(out, dividend, TMP);
3207 __ Andi(out, out, 1);
3208 __ Addu(out, out, TMP);
3209 } else {
3210 __ Sra(TMP, dividend, 31);
3211 __ Srl(TMP, TMP, 32 - ctz_imm);
3212 __ Addu(out, dividend, TMP);
3213 if (IsUint<16>(abs_imm - 1)) {
3214 __ Andi(out, out, abs_imm - 1);
3215 } else {
3216 __ Sll(out, out, 32 - ctz_imm);
3217 __ Srl(out, out, 32 - ctz_imm);
3218 }
3219 __ Subu(out, out, TMP);
3220 }
3221 } else {
3222 DCHECK_EQ(type, Primitive::kPrimLong);
3223 if (ctz_imm == 1) {
3224 // Fast path for modulo +/-2, which is very common.
3225 __ Dsra32(TMP, dividend, 31);
3226 __ Dsubu(out, dividend, TMP);
3227 __ Andi(out, out, 1);
3228 __ Daddu(out, out, TMP);
3229 } else {
3230 __ Dsra32(TMP, dividend, 31);
3231 if (ctz_imm > 32) {
3232 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3233 } else {
3234 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3235 }
3236 __ Daddu(out, dividend, TMP);
3237 if (IsUint<16>(abs_imm - 1)) {
3238 __ Andi(out, out, abs_imm - 1);
3239 } else {
3240 if (ctz_imm > 32) {
3241 __ Dsll(out, out, 64 - ctz_imm);
3242 __ Dsrl(out, out, 64 - ctz_imm);
3243 } else {
3244 __ Dsll32(out, out, 32 - ctz_imm);
3245 __ Dsrl32(out, out, 32 - ctz_imm);
3246 }
3247 }
3248 __ Dsubu(out, out, TMP);
3249 }
3250 }
3251 }
3252}
3253
3254void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3255 DCHECK(instruction->IsDiv() || instruction->IsRem());
3256
3257 LocationSummary* locations = instruction->GetLocations();
3258 Location second = locations->InAt(1);
3259 DCHECK(second.IsConstant());
3260
3261 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3262 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3263 int64_t imm = Int64FromConstant(second.GetConstant());
3264
3265 Primitive::Type type = instruction->GetResultType();
3266 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3267
3268 int64_t magic;
3269 int shift;
3270 CalculateMagicAndShiftForDivRem(imm,
3271 (type == Primitive::kPrimLong),
3272 &magic,
3273 &shift);
3274
3275 if (type == Primitive::kPrimInt) {
3276 __ LoadConst32(TMP, magic);
3277 __ MuhR6(TMP, dividend, TMP);
3278
3279 if (imm > 0 && magic < 0) {
3280 __ Addu(TMP, TMP, dividend);
3281 } else if (imm < 0 && magic > 0) {
3282 __ Subu(TMP, TMP, dividend);
3283 }
3284
3285 if (shift != 0) {
3286 __ Sra(TMP, TMP, shift);
3287 }
3288
3289 if (instruction->IsDiv()) {
3290 __ Sra(out, TMP, 31);
3291 __ Subu(out, TMP, out);
3292 } else {
3293 __ Sra(AT, TMP, 31);
3294 __ Subu(AT, TMP, AT);
3295 __ LoadConst32(TMP, imm);
3296 __ MulR6(TMP, AT, TMP);
3297 __ Subu(out, dividend, TMP);
3298 }
3299 } else {
3300 __ LoadConst64(TMP, magic);
3301 __ Dmuh(TMP, dividend, TMP);
3302
3303 if (imm > 0 && magic < 0) {
3304 __ Daddu(TMP, TMP, dividend);
3305 } else if (imm < 0 && magic > 0) {
3306 __ Dsubu(TMP, TMP, dividend);
3307 }
3308
3309 if (shift >= 32) {
3310 __ Dsra32(TMP, TMP, shift - 32);
3311 } else if (shift > 0) {
3312 __ Dsra(TMP, TMP, shift);
3313 }
3314
3315 if (instruction->IsDiv()) {
3316 __ Dsra32(out, TMP, 31);
3317 __ Dsubu(out, TMP, out);
3318 } else {
3319 __ Dsra32(AT, TMP, 31);
3320 __ Dsubu(AT, TMP, AT);
3321 __ LoadConst64(TMP, imm);
3322 __ Dmul(TMP, AT, TMP);
3323 __ Dsubu(out, dividend, TMP);
3324 }
3325 }
3326}
3327
3328void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3329 DCHECK(instruction->IsDiv() || instruction->IsRem());
3330 Primitive::Type type = instruction->GetResultType();
3331 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3332
3333 LocationSummary* locations = instruction->GetLocations();
3334 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3335 Location second = locations->InAt(1);
3336
3337 if (second.IsConstant()) {
3338 int64_t imm = Int64FromConstant(second.GetConstant());
3339 if (imm == 0) {
3340 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3341 } else if (imm == 1 || imm == -1) {
3342 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003343 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003344 DivRemByPowerOfTwo(instruction);
3345 } else {
3346 DCHECK(imm <= -2 || imm >= 2);
3347 GenerateDivRemWithAnyConstant(instruction);
3348 }
3349 } else {
3350 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3351 GpuRegister divisor = second.AsRegister<GpuRegister>();
3352 if (instruction->IsDiv()) {
3353 if (type == Primitive::kPrimInt)
3354 __ DivR6(out, dividend, divisor);
3355 else
3356 __ Ddiv(out, dividend, divisor);
3357 } else {
3358 if (type == Primitive::kPrimInt)
3359 __ ModR6(out, dividend, divisor);
3360 else
3361 __ Dmod(out, dividend, divisor);
3362 }
3363 }
3364}
3365
Alexey Frunze4dda3372015-06-01 18:31:49 -07003366void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3367 LocationSummary* locations =
3368 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3369 switch (div->GetResultType()) {
3370 case Primitive::kPrimInt:
3371 case Primitive::kPrimLong:
3372 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003373 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3375 break;
3376
3377 case Primitive::kPrimFloat:
3378 case Primitive::kPrimDouble:
3379 locations->SetInAt(0, Location::RequiresFpuRegister());
3380 locations->SetInAt(1, Location::RequiresFpuRegister());
3381 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3382 break;
3383
3384 default:
3385 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3386 }
3387}
3388
3389void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3390 Primitive::Type type = instruction->GetType();
3391 LocationSummary* locations = instruction->GetLocations();
3392
3393 switch (type) {
3394 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003395 case Primitive::kPrimLong:
3396 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003397 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003398 case Primitive::kPrimFloat:
3399 case Primitive::kPrimDouble: {
3400 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3401 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3402 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3403 if (type == Primitive::kPrimFloat)
3404 __ DivS(dst, lhs, rhs);
3405 else
3406 __ DivD(dst, lhs, rhs);
3407 break;
3408 }
3409 default:
3410 LOG(FATAL) << "Unexpected div type " << type;
3411 }
3412}
3413
3414void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003415 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003416 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003417}
3418
3419void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3420 SlowPathCodeMIPS64* slow_path =
3421 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3422 codegen_->AddSlowPath(slow_path);
3423 Location value = instruction->GetLocations()->InAt(0);
3424
3425 Primitive::Type type = instruction->GetType();
3426
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003427 if (!Primitive::IsIntegralType(type)) {
3428 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003429 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003430 }
3431
3432 if (value.IsConstant()) {
3433 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3434 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003435 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003436 } else {
3437 // A division by a non-null constant is valid. We don't need to perform
3438 // any check, so simply fall through.
3439 }
3440 } else {
3441 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3442 }
3443}
3444
3445void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3446 LocationSummary* locations =
3447 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3448 locations->SetOut(Location::ConstantLocation(constant));
3449}
3450
3451void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3452 // Will be generated at use site.
3453}
3454
3455void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3456 exit->SetLocations(nullptr);
3457}
3458
3459void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3460}
3461
3462void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3463 LocationSummary* locations =
3464 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3465 locations->SetOut(Location::ConstantLocation(constant));
3466}
3467
3468void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3469 // Will be generated at use site.
3470}
3471
David Brazdilfc6a86a2015-06-26 10:33:45 +00003472void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003473 DCHECK(!successor->IsExitBlock());
3474 HBasicBlock* block = got->GetBlock();
3475 HInstruction* previous = got->GetPrevious();
3476 HLoopInformation* info = block->GetLoopInformation();
3477
3478 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3479 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3480 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3481 return;
3482 }
3483 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3484 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3485 }
3486 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003487 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003488 }
3489}
3490
David Brazdilfc6a86a2015-06-26 10:33:45 +00003491void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3492 got->SetLocations(nullptr);
3493}
3494
3495void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3496 HandleGoto(got, got->GetSuccessor());
3497}
3498
3499void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3500 try_boundary->SetLocations(nullptr);
3501}
3502
3503void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3504 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3505 if (!successor->IsExitBlock()) {
3506 HandleGoto(try_boundary, successor);
3507 }
3508}
3509
Alexey Frunze299a9392015-12-08 16:08:02 -08003510void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3511 bool is64bit,
3512 LocationSummary* locations) {
3513 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3514 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3515 Location rhs_location = locations->InAt(1);
3516 GpuRegister rhs_reg = ZERO;
3517 int64_t rhs_imm = 0;
3518 bool use_imm = rhs_location.IsConstant();
3519 if (use_imm) {
3520 if (is64bit) {
3521 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3522 } else {
3523 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3524 }
3525 } else {
3526 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3527 }
3528 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3529
3530 switch (cond) {
3531 case kCondEQ:
3532 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003533 if (use_imm && IsInt<16>(-rhs_imm)) {
3534 if (rhs_imm == 0) {
3535 if (cond == kCondEQ) {
3536 __ Sltiu(dst, lhs, 1);
3537 } else {
3538 __ Sltu(dst, ZERO, lhs);
3539 }
3540 } else {
3541 if (is64bit) {
3542 __ Daddiu(dst, lhs, -rhs_imm);
3543 } else {
3544 __ Addiu(dst, lhs, -rhs_imm);
3545 }
3546 if (cond == kCondEQ) {
3547 __ Sltiu(dst, dst, 1);
3548 } else {
3549 __ Sltu(dst, ZERO, dst);
3550 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003551 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003552 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003553 if (use_imm && IsUint<16>(rhs_imm)) {
3554 __ Xori(dst, lhs, rhs_imm);
3555 } else {
3556 if (use_imm) {
3557 rhs_reg = TMP;
3558 __ LoadConst64(rhs_reg, rhs_imm);
3559 }
3560 __ Xor(dst, lhs, rhs_reg);
3561 }
3562 if (cond == kCondEQ) {
3563 __ Sltiu(dst, dst, 1);
3564 } else {
3565 __ Sltu(dst, ZERO, dst);
3566 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003567 }
3568 break;
3569
3570 case kCondLT:
3571 case kCondGE:
3572 if (use_imm && IsInt<16>(rhs_imm)) {
3573 __ Slti(dst, lhs, rhs_imm);
3574 } else {
3575 if (use_imm) {
3576 rhs_reg = TMP;
3577 __ LoadConst64(rhs_reg, rhs_imm);
3578 }
3579 __ Slt(dst, lhs, rhs_reg);
3580 }
3581 if (cond == kCondGE) {
3582 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3583 // only the slt instruction but no sge.
3584 __ Xori(dst, dst, 1);
3585 }
3586 break;
3587
3588 case kCondLE:
3589 case kCondGT:
3590 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3591 // Simulate lhs <= rhs via lhs < rhs + 1.
3592 __ Slti(dst, lhs, rhs_imm_plus_one);
3593 if (cond == kCondGT) {
3594 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3595 // only the slti instruction but no sgti.
3596 __ Xori(dst, dst, 1);
3597 }
3598 } else {
3599 if (use_imm) {
3600 rhs_reg = TMP;
3601 __ LoadConst64(rhs_reg, rhs_imm);
3602 }
3603 __ Slt(dst, rhs_reg, lhs);
3604 if (cond == kCondLE) {
3605 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3606 // only the slt instruction but no sle.
3607 __ Xori(dst, dst, 1);
3608 }
3609 }
3610 break;
3611
3612 case kCondB:
3613 case kCondAE:
3614 if (use_imm && IsInt<16>(rhs_imm)) {
3615 // Sltiu sign-extends its 16-bit immediate operand before
3616 // the comparison and thus lets us compare directly with
3617 // unsigned values in the ranges [0, 0x7fff] and
3618 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3619 __ Sltiu(dst, lhs, rhs_imm);
3620 } else {
3621 if (use_imm) {
3622 rhs_reg = TMP;
3623 __ LoadConst64(rhs_reg, rhs_imm);
3624 }
3625 __ Sltu(dst, lhs, rhs_reg);
3626 }
3627 if (cond == kCondAE) {
3628 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3629 // only the sltu instruction but no sgeu.
3630 __ Xori(dst, dst, 1);
3631 }
3632 break;
3633
3634 case kCondBE:
3635 case kCondA:
3636 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3637 // Simulate lhs <= rhs via lhs < rhs + 1.
3638 // Note that this only works if rhs + 1 does not overflow
3639 // to 0, hence the check above.
3640 // Sltiu sign-extends its 16-bit immediate operand before
3641 // the comparison and thus lets us compare directly with
3642 // unsigned values in the ranges [0, 0x7fff] and
3643 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3644 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3645 if (cond == kCondA) {
3646 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3647 // only the sltiu instruction but no sgtiu.
3648 __ Xori(dst, dst, 1);
3649 }
3650 } else {
3651 if (use_imm) {
3652 rhs_reg = TMP;
3653 __ LoadConst64(rhs_reg, rhs_imm);
3654 }
3655 __ Sltu(dst, rhs_reg, lhs);
3656 if (cond == kCondBE) {
3657 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3658 // only the sltu instruction but no sleu.
3659 __ Xori(dst, dst, 1);
3660 }
3661 }
3662 break;
3663 }
3664}
3665
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003666bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3667 bool is64bit,
3668 LocationSummary* input_locations,
3669 GpuRegister dst) {
3670 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3671 Location rhs_location = input_locations->InAt(1);
3672 GpuRegister rhs_reg = ZERO;
3673 int64_t rhs_imm = 0;
3674 bool use_imm = rhs_location.IsConstant();
3675 if (use_imm) {
3676 if (is64bit) {
3677 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3678 } else {
3679 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3680 }
3681 } else {
3682 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3683 }
3684 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3685
3686 switch (cond) {
3687 case kCondEQ:
3688 case kCondNE:
3689 if (use_imm && IsInt<16>(-rhs_imm)) {
3690 if (is64bit) {
3691 __ Daddiu(dst, lhs, -rhs_imm);
3692 } else {
3693 __ Addiu(dst, lhs, -rhs_imm);
3694 }
3695 } else if (use_imm && IsUint<16>(rhs_imm)) {
3696 __ Xori(dst, lhs, rhs_imm);
3697 } else {
3698 if (use_imm) {
3699 rhs_reg = TMP;
3700 __ LoadConst64(rhs_reg, rhs_imm);
3701 }
3702 __ Xor(dst, lhs, rhs_reg);
3703 }
3704 return (cond == kCondEQ);
3705
3706 case kCondLT:
3707 case kCondGE:
3708 if (use_imm && IsInt<16>(rhs_imm)) {
3709 __ Slti(dst, lhs, rhs_imm);
3710 } else {
3711 if (use_imm) {
3712 rhs_reg = TMP;
3713 __ LoadConst64(rhs_reg, rhs_imm);
3714 }
3715 __ Slt(dst, lhs, rhs_reg);
3716 }
3717 return (cond == kCondGE);
3718
3719 case kCondLE:
3720 case kCondGT:
3721 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3722 // Simulate lhs <= rhs via lhs < rhs + 1.
3723 __ Slti(dst, lhs, rhs_imm_plus_one);
3724 return (cond == kCondGT);
3725 } else {
3726 if (use_imm) {
3727 rhs_reg = TMP;
3728 __ LoadConst64(rhs_reg, rhs_imm);
3729 }
3730 __ Slt(dst, rhs_reg, lhs);
3731 return (cond == kCondLE);
3732 }
3733
3734 case kCondB:
3735 case kCondAE:
3736 if (use_imm && IsInt<16>(rhs_imm)) {
3737 // Sltiu sign-extends its 16-bit immediate operand before
3738 // the comparison and thus lets us compare directly with
3739 // unsigned values in the ranges [0, 0x7fff] and
3740 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3741 __ Sltiu(dst, lhs, rhs_imm);
3742 } else {
3743 if (use_imm) {
3744 rhs_reg = TMP;
3745 __ LoadConst64(rhs_reg, rhs_imm);
3746 }
3747 __ Sltu(dst, lhs, rhs_reg);
3748 }
3749 return (cond == kCondAE);
3750
3751 case kCondBE:
3752 case kCondA:
3753 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3754 // Simulate lhs <= rhs via lhs < rhs + 1.
3755 // Note that this only works if rhs + 1 does not overflow
3756 // to 0, hence the check above.
3757 // Sltiu sign-extends its 16-bit immediate operand before
3758 // the comparison and thus lets us compare directly with
3759 // unsigned values in the ranges [0, 0x7fff] and
3760 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3761 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3762 return (cond == kCondA);
3763 } else {
3764 if (use_imm) {
3765 rhs_reg = TMP;
3766 __ LoadConst64(rhs_reg, rhs_imm);
3767 }
3768 __ Sltu(dst, rhs_reg, lhs);
3769 return (cond == kCondBE);
3770 }
3771 }
3772}
3773
Alexey Frunze299a9392015-12-08 16:08:02 -08003774void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3775 bool is64bit,
3776 LocationSummary* locations,
3777 Mips64Label* label) {
3778 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3779 Location rhs_location = locations->InAt(1);
3780 GpuRegister rhs_reg = ZERO;
3781 int64_t rhs_imm = 0;
3782 bool use_imm = rhs_location.IsConstant();
3783 if (use_imm) {
3784 if (is64bit) {
3785 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3786 } else {
3787 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3788 }
3789 } else {
3790 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3791 }
3792
3793 if (use_imm && rhs_imm == 0) {
3794 switch (cond) {
3795 case kCondEQ:
3796 case kCondBE: // <= 0 if zero
3797 __ Beqzc(lhs, label);
3798 break;
3799 case kCondNE:
3800 case kCondA: // > 0 if non-zero
3801 __ Bnezc(lhs, label);
3802 break;
3803 case kCondLT:
3804 __ Bltzc(lhs, label);
3805 break;
3806 case kCondGE:
3807 __ Bgezc(lhs, label);
3808 break;
3809 case kCondLE:
3810 __ Blezc(lhs, label);
3811 break;
3812 case kCondGT:
3813 __ Bgtzc(lhs, label);
3814 break;
3815 case kCondB: // always false
3816 break;
3817 case kCondAE: // always true
3818 __ Bc(label);
3819 break;
3820 }
3821 } else {
3822 if (use_imm) {
3823 rhs_reg = TMP;
3824 __ LoadConst64(rhs_reg, rhs_imm);
3825 }
3826 switch (cond) {
3827 case kCondEQ:
3828 __ Beqc(lhs, rhs_reg, label);
3829 break;
3830 case kCondNE:
3831 __ Bnec(lhs, rhs_reg, label);
3832 break;
3833 case kCondLT:
3834 __ Bltc(lhs, rhs_reg, label);
3835 break;
3836 case kCondGE:
3837 __ Bgec(lhs, rhs_reg, label);
3838 break;
3839 case kCondLE:
3840 __ Bgec(rhs_reg, lhs, label);
3841 break;
3842 case kCondGT:
3843 __ Bltc(rhs_reg, lhs, label);
3844 break;
3845 case kCondB:
3846 __ Bltuc(lhs, rhs_reg, label);
3847 break;
3848 case kCondAE:
3849 __ Bgeuc(lhs, rhs_reg, label);
3850 break;
3851 case kCondBE:
3852 __ Bgeuc(rhs_reg, lhs, label);
3853 break;
3854 case kCondA:
3855 __ Bltuc(rhs_reg, lhs, label);
3856 break;
3857 }
3858 }
3859}
3860
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003861void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3862 bool gt_bias,
3863 Primitive::Type type,
3864 LocationSummary* locations) {
3865 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3866 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3867 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3868 if (type == Primitive::kPrimFloat) {
3869 switch (cond) {
3870 case kCondEQ:
3871 __ CmpEqS(FTMP, lhs, rhs);
3872 __ Mfc1(dst, FTMP);
3873 __ Andi(dst, dst, 1);
3874 break;
3875 case kCondNE:
3876 __ CmpEqS(FTMP, lhs, rhs);
3877 __ Mfc1(dst, FTMP);
3878 __ Addiu(dst, dst, 1);
3879 break;
3880 case kCondLT:
3881 if (gt_bias) {
3882 __ CmpLtS(FTMP, lhs, rhs);
3883 } else {
3884 __ CmpUltS(FTMP, lhs, rhs);
3885 }
3886 __ Mfc1(dst, FTMP);
3887 __ Andi(dst, dst, 1);
3888 break;
3889 case kCondLE:
3890 if (gt_bias) {
3891 __ CmpLeS(FTMP, lhs, rhs);
3892 } else {
3893 __ CmpUleS(FTMP, lhs, rhs);
3894 }
3895 __ Mfc1(dst, FTMP);
3896 __ Andi(dst, dst, 1);
3897 break;
3898 case kCondGT:
3899 if (gt_bias) {
3900 __ CmpUltS(FTMP, rhs, lhs);
3901 } else {
3902 __ CmpLtS(FTMP, rhs, lhs);
3903 }
3904 __ Mfc1(dst, FTMP);
3905 __ Andi(dst, dst, 1);
3906 break;
3907 case kCondGE:
3908 if (gt_bias) {
3909 __ CmpUleS(FTMP, rhs, lhs);
3910 } else {
3911 __ CmpLeS(FTMP, rhs, lhs);
3912 }
3913 __ Mfc1(dst, FTMP);
3914 __ Andi(dst, dst, 1);
3915 break;
3916 default:
3917 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3918 UNREACHABLE();
3919 }
3920 } else {
3921 DCHECK_EQ(type, Primitive::kPrimDouble);
3922 switch (cond) {
3923 case kCondEQ:
3924 __ CmpEqD(FTMP, lhs, rhs);
3925 __ Mfc1(dst, FTMP);
3926 __ Andi(dst, dst, 1);
3927 break;
3928 case kCondNE:
3929 __ CmpEqD(FTMP, lhs, rhs);
3930 __ Mfc1(dst, FTMP);
3931 __ Addiu(dst, dst, 1);
3932 break;
3933 case kCondLT:
3934 if (gt_bias) {
3935 __ CmpLtD(FTMP, lhs, rhs);
3936 } else {
3937 __ CmpUltD(FTMP, lhs, rhs);
3938 }
3939 __ Mfc1(dst, FTMP);
3940 __ Andi(dst, dst, 1);
3941 break;
3942 case kCondLE:
3943 if (gt_bias) {
3944 __ CmpLeD(FTMP, lhs, rhs);
3945 } else {
3946 __ CmpUleD(FTMP, lhs, rhs);
3947 }
3948 __ Mfc1(dst, FTMP);
3949 __ Andi(dst, dst, 1);
3950 break;
3951 case kCondGT:
3952 if (gt_bias) {
3953 __ CmpUltD(FTMP, rhs, lhs);
3954 } else {
3955 __ CmpLtD(FTMP, rhs, lhs);
3956 }
3957 __ Mfc1(dst, FTMP);
3958 __ Andi(dst, dst, 1);
3959 break;
3960 case kCondGE:
3961 if (gt_bias) {
3962 __ CmpUleD(FTMP, rhs, lhs);
3963 } else {
3964 __ CmpLeD(FTMP, rhs, lhs);
3965 }
3966 __ Mfc1(dst, FTMP);
3967 __ Andi(dst, dst, 1);
3968 break;
3969 default:
3970 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3971 UNREACHABLE();
3972 }
3973 }
3974}
3975
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003976bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
3977 bool gt_bias,
3978 Primitive::Type type,
3979 LocationSummary* input_locations,
3980 FpuRegister dst) {
3981 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
3982 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
3983 if (type == Primitive::kPrimFloat) {
3984 switch (cond) {
3985 case kCondEQ:
3986 __ CmpEqS(dst, lhs, rhs);
3987 return false;
3988 case kCondNE:
3989 __ CmpEqS(dst, lhs, rhs);
3990 return true;
3991 case kCondLT:
3992 if (gt_bias) {
3993 __ CmpLtS(dst, lhs, rhs);
3994 } else {
3995 __ CmpUltS(dst, lhs, rhs);
3996 }
3997 return false;
3998 case kCondLE:
3999 if (gt_bias) {
4000 __ CmpLeS(dst, lhs, rhs);
4001 } else {
4002 __ CmpUleS(dst, lhs, rhs);
4003 }
4004 return false;
4005 case kCondGT:
4006 if (gt_bias) {
4007 __ CmpUltS(dst, rhs, lhs);
4008 } else {
4009 __ CmpLtS(dst, rhs, lhs);
4010 }
4011 return false;
4012 case kCondGE:
4013 if (gt_bias) {
4014 __ CmpUleS(dst, rhs, lhs);
4015 } else {
4016 __ CmpLeS(dst, rhs, lhs);
4017 }
4018 return false;
4019 default:
4020 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4021 UNREACHABLE();
4022 }
4023 } else {
4024 DCHECK_EQ(type, Primitive::kPrimDouble);
4025 switch (cond) {
4026 case kCondEQ:
4027 __ CmpEqD(dst, lhs, rhs);
4028 return false;
4029 case kCondNE:
4030 __ CmpEqD(dst, lhs, rhs);
4031 return true;
4032 case kCondLT:
4033 if (gt_bias) {
4034 __ CmpLtD(dst, lhs, rhs);
4035 } else {
4036 __ CmpUltD(dst, lhs, rhs);
4037 }
4038 return false;
4039 case kCondLE:
4040 if (gt_bias) {
4041 __ CmpLeD(dst, lhs, rhs);
4042 } else {
4043 __ CmpUleD(dst, lhs, rhs);
4044 }
4045 return false;
4046 case kCondGT:
4047 if (gt_bias) {
4048 __ CmpUltD(dst, rhs, lhs);
4049 } else {
4050 __ CmpLtD(dst, rhs, lhs);
4051 }
4052 return false;
4053 case kCondGE:
4054 if (gt_bias) {
4055 __ CmpUleD(dst, rhs, lhs);
4056 } else {
4057 __ CmpLeD(dst, rhs, lhs);
4058 }
4059 return false;
4060 default:
4061 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4062 UNREACHABLE();
4063 }
4064 }
4065}
4066
Alexey Frunze299a9392015-12-08 16:08:02 -08004067void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4068 bool gt_bias,
4069 Primitive::Type type,
4070 LocationSummary* locations,
4071 Mips64Label* label) {
4072 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4073 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
4074 if (type == Primitive::kPrimFloat) {
4075 switch (cond) {
4076 case kCondEQ:
4077 __ CmpEqS(FTMP, lhs, rhs);
4078 __ Bc1nez(FTMP, label);
4079 break;
4080 case kCondNE:
4081 __ CmpEqS(FTMP, lhs, rhs);
4082 __ Bc1eqz(FTMP, label);
4083 break;
4084 case kCondLT:
4085 if (gt_bias) {
4086 __ CmpLtS(FTMP, lhs, rhs);
4087 } else {
4088 __ CmpUltS(FTMP, lhs, rhs);
4089 }
4090 __ Bc1nez(FTMP, label);
4091 break;
4092 case kCondLE:
4093 if (gt_bias) {
4094 __ CmpLeS(FTMP, lhs, rhs);
4095 } else {
4096 __ CmpUleS(FTMP, lhs, rhs);
4097 }
4098 __ Bc1nez(FTMP, label);
4099 break;
4100 case kCondGT:
4101 if (gt_bias) {
4102 __ CmpUltS(FTMP, rhs, lhs);
4103 } else {
4104 __ CmpLtS(FTMP, rhs, lhs);
4105 }
4106 __ Bc1nez(FTMP, label);
4107 break;
4108 case kCondGE:
4109 if (gt_bias) {
4110 __ CmpUleS(FTMP, rhs, lhs);
4111 } else {
4112 __ CmpLeS(FTMP, rhs, lhs);
4113 }
4114 __ Bc1nez(FTMP, label);
4115 break;
4116 default:
4117 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004118 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004119 }
4120 } else {
4121 DCHECK_EQ(type, Primitive::kPrimDouble);
4122 switch (cond) {
4123 case kCondEQ:
4124 __ CmpEqD(FTMP, lhs, rhs);
4125 __ Bc1nez(FTMP, label);
4126 break;
4127 case kCondNE:
4128 __ CmpEqD(FTMP, lhs, rhs);
4129 __ Bc1eqz(FTMP, label);
4130 break;
4131 case kCondLT:
4132 if (gt_bias) {
4133 __ CmpLtD(FTMP, lhs, rhs);
4134 } else {
4135 __ CmpUltD(FTMP, lhs, rhs);
4136 }
4137 __ Bc1nez(FTMP, label);
4138 break;
4139 case kCondLE:
4140 if (gt_bias) {
4141 __ CmpLeD(FTMP, lhs, rhs);
4142 } else {
4143 __ CmpUleD(FTMP, lhs, rhs);
4144 }
4145 __ Bc1nez(FTMP, label);
4146 break;
4147 case kCondGT:
4148 if (gt_bias) {
4149 __ CmpUltD(FTMP, rhs, lhs);
4150 } else {
4151 __ CmpLtD(FTMP, rhs, lhs);
4152 }
4153 __ Bc1nez(FTMP, label);
4154 break;
4155 case kCondGE:
4156 if (gt_bias) {
4157 __ CmpUleD(FTMP, rhs, lhs);
4158 } else {
4159 __ CmpLeD(FTMP, rhs, lhs);
4160 }
4161 __ Bc1nez(FTMP, label);
4162 break;
4163 default:
4164 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004165 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004166 }
4167 }
4168}
4169
Alexey Frunze4dda3372015-06-01 18:31:49 -07004170void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004171 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004172 Mips64Label* true_target,
4173 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004174 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004175
David Brazdil0debae72015-11-12 18:37:00 +00004176 if (true_target == nullptr && false_target == nullptr) {
4177 // Nothing to do. The code always falls through.
4178 return;
4179 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004180 // Constant condition, statically compared against "true" (integer value 1).
4181 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004182 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004183 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004184 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004185 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004186 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004187 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004188 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004189 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004190 }
David Brazdil0debae72015-11-12 18:37:00 +00004191 return;
4192 }
4193
4194 // The following code generates these patterns:
4195 // (1) true_target == nullptr && false_target != nullptr
4196 // - opposite condition true => branch to false_target
4197 // (2) true_target != nullptr && false_target == nullptr
4198 // - condition true => branch to true_target
4199 // (3) true_target != nullptr && false_target != nullptr
4200 // - condition true => branch to true_target
4201 // - branch to false_target
4202 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004203 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004204 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004205 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004206 if (true_target == nullptr) {
4207 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4208 } else {
4209 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4210 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004211 } else {
4212 // The condition instruction has not been materialized, use its inputs as
4213 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004214 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004215 Primitive::Type type = condition->InputAt(0)->GetType();
4216 LocationSummary* locations = cond->GetLocations();
4217 IfCondition if_cond = condition->GetCondition();
4218 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004219
David Brazdil0debae72015-11-12 18:37:00 +00004220 if (true_target == nullptr) {
4221 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004222 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004223 }
4224
Alexey Frunze299a9392015-12-08 16:08:02 -08004225 switch (type) {
4226 default:
4227 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4228 break;
4229 case Primitive::kPrimLong:
4230 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4231 break;
4232 case Primitive::kPrimFloat:
4233 case Primitive::kPrimDouble:
4234 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4235 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004236 }
4237 }
David Brazdil0debae72015-11-12 18:37:00 +00004238
4239 // If neither branch falls through (case 3), the conditional branch to `true_target`
4240 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4241 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004242 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004243 }
4244}
4245
4246void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
4247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004248 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004249 locations->SetInAt(0, Location::RequiresRegister());
4250 }
4251}
4252
4253void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004254 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4255 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004256 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004257 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004258 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004259 nullptr : codegen_->GetLabelOf(false_successor);
4260 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004261}
4262
4263void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
4264 LocationSummary* locations = new (GetGraph()->GetArena())
4265 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004266 InvokeRuntimeCallingConvention calling_convention;
4267 RegisterSet caller_saves = RegisterSet::Empty();
4268 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4269 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004270 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004271 locations->SetInAt(0, Location::RequiresRegister());
4272 }
4273}
4274
4275void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004276 SlowPathCodeMIPS64* slow_path =
4277 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004278 GenerateTestAndBranch(deoptimize,
4279 /* condition_input_index */ 0,
4280 slow_path->GetEntryLabel(),
4281 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004282}
4283
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004284// This function returns true if a conditional move can be generated for HSelect.
4285// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4286// branches and regular moves.
4287//
4288// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4289//
4290// While determining feasibility of a conditional move and setting inputs/outputs
4291// are two distinct tasks, this function does both because they share quite a bit
4292// of common logic.
4293static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4294 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4295 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4296 HCondition* condition = cond->AsCondition();
4297
4298 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4299 Primitive::Type dst_type = select->GetType();
4300
4301 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4302 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4303 bool is_true_value_zero_constant =
4304 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4305 bool is_false_value_zero_constant =
4306 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4307
4308 bool can_move_conditionally = false;
4309 bool use_const_for_false_in = false;
4310 bool use_const_for_true_in = false;
4311
4312 if (!cond->IsConstant()) {
4313 if (!Primitive::IsFloatingPointType(cond_type)) {
4314 if (!Primitive::IsFloatingPointType(dst_type)) {
4315 // Moving int/long on int/long condition.
4316 if (is_true_value_zero_constant) {
4317 // seleqz out_reg, false_reg, cond_reg
4318 can_move_conditionally = true;
4319 use_const_for_true_in = true;
4320 } else if (is_false_value_zero_constant) {
4321 // selnez out_reg, true_reg, cond_reg
4322 can_move_conditionally = true;
4323 use_const_for_false_in = true;
4324 } else if (materialized) {
4325 // Not materializing unmaterialized int conditions
4326 // to keep the instruction count low.
4327 // selnez AT, true_reg, cond_reg
4328 // seleqz TMP, false_reg, cond_reg
4329 // or out_reg, AT, TMP
4330 can_move_conditionally = true;
4331 }
4332 } else {
4333 // Moving float/double on int/long condition.
4334 if (materialized) {
4335 // Not materializing unmaterialized int conditions
4336 // to keep the instruction count low.
4337 can_move_conditionally = true;
4338 if (is_true_value_zero_constant) {
4339 // sltu TMP, ZERO, cond_reg
4340 // mtc1 TMP, temp_cond_reg
4341 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4342 use_const_for_true_in = true;
4343 } else if (is_false_value_zero_constant) {
4344 // sltu TMP, ZERO, cond_reg
4345 // mtc1 TMP, temp_cond_reg
4346 // selnez.fmt out_reg, true_reg, temp_cond_reg
4347 use_const_for_false_in = true;
4348 } else {
4349 // sltu TMP, ZERO, cond_reg
4350 // mtc1 TMP, temp_cond_reg
4351 // sel.fmt temp_cond_reg, false_reg, true_reg
4352 // mov.fmt out_reg, temp_cond_reg
4353 }
4354 }
4355 }
4356 } else {
4357 if (!Primitive::IsFloatingPointType(dst_type)) {
4358 // Moving int/long on float/double condition.
4359 can_move_conditionally = true;
4360 if (is_true_value_zero_constant) {
4361 // mfc1 TMP, temp_cond_reg
4362 // seleqz out_reg, false_reg, TMP
4363 use_const_for_true_in = true;
4364 } else if (is_false_value_zero_constant) {
4365 // mfc1 TMP, temp_cond_reg
4366 // selnez out_reg, true_reg, TMP
4367 use_const_for_false_in = true;
4368 } else {
4369 // mfc1 TMP, temp_cond_reg
4370 // selnez AT, true_reg, TMP
4371 // seleqz TMP, false_reg, TMP
4372 // or out_reg, AT, TMP
4373 }
4374 } else {
4375 // Moving float/double on float/double condition.
4376 can_move_conditionally = true;
4377 if (is_true_value_zero_constant) {
4378 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4379 use_const_for_true_in = true;
4380 } else if (is_false_value_zero_constant) {
4381 // selnez.fmt out_reg, true_reg, temp_cond_reg
4382 use_const_for_false_in = true;
4383 } else {
4384 // sel.fmt temp_cond_reg, false_reg, true_reg
4385 // mov.fmt out_reg, temp_cond_reg
4386 }
4387 }
4388 }
4389 }
4390
4391 if (can_move_conditionally) {
4392 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4393 } else {
4394 DCHECK(!use_const_for_false_in);
4395 DCHECK(!use_const_for_true_in);
4396 }
4397
4398 if (locations_to_set != nullptr) {
4399 if (use_const_for_false_in) {
4400 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4401 } else {
4402 locations_to_set->SetInAt(0,
4403 Primitive::IsFloatingPointType(dst_type)
4404 ? Location::RequiresFpuRegister()
4405 : Location::RequiresRegister());
4406 }
4407 if (use_const_for_true_in) {
4408 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4409 } else {
4410 locations_to_set->SetInAt(1,
4411 Primitive::IsFloatingPointType(dst_type)
4412 ? Location::RequiresFpuRegister()
4413 : Location::RequiresRegister());
4414 }
4415 if (materialized) {
4416 locations_to_set->SetInAt(2, Location::RequiresRegister());
4417 }
4418
4419 if (can_move_conditionally) {
4420 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4421 ? Location::RequiresFpuRegister()
4422 : Location::RequiresRegister());
4423 } else {
4424 locations_to_set->SetOut(Location::SameAsFirstInput());
4425 }
4426 }
4427
4428 return can_move_conditionally;
4429}
4430
4431
4432void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4433 LocationSummary* locations = select->GetLocations();
4434 Location dst = locations->Out();
4435 Location false_src = locations->InAt(0);
4436 Location true_src = locations->InAt(1);
4437 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4438 GpuRegister cond_reg = TMP;
4439 FpuRegister fcond_reg = FTMP;
4440 Primitive::Type cond_type = Primitive::kPrimInt;
4441 bool cond_inverted = false;
4442 Primitive::Type dst_type = select->GetType();
4443
4444 if (IsBooleanValueOrMaterializedCondition(cond)) {
4445 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4446 } else {
4447 HCondition* condition = cond->AsCondition();
4448 LocationSummary* cond_locations = cond->GetLocations();
4449 IfCondition if_cond = condition->GetCondition();
4450 cond_type = condition->InputAt(0)->GetType();
4451 switch (cond_type) {
4452 default:
4453 cond_inverted = MaterializeIntLongCompare(if_cond,
4454 /* is64bit */ false,
4455 cond_locations,
4456 cond_reg);
4457 break;
4458 case Primitive::kPrimLong:
4459 cond_inverted = MaterializeIntLongCompare(if_cond,
4460 /* is64bit */ true,
4461 cond_locations,
4462 cond_reg);
4463 break;
4464 case Primitive::kPrimFloat:
4465 case Primitive::kPrimDouble:
4466 cond_inverted = MaterializeFpCompare(if_cond,
4467 condition->IsGtBias(),
4468 cond_type,
4469 cond_locations,
4470 fcond_reg);
4471 break;
4472 }
4473 }
4474
4475 if (true_src.IsConstant()) {
4476 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4477 }
4478 if (false_src.IsConstant()) {
4479 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4480 }
4481
4482 switch (dst_type) {
4483 default:
4484 if (Primitive::IsFloatingPointType(cond_type)) {
4485 __ Mfc1(cond_reg, fcond_reg);
4486 }
4487 if (true_src.IsConstant()) {
4488 if (cond_inverted) {
4489 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4490 } else {
4491 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4492 }
4493 } else if (false_src.IsConstant()) {
4494 if (cond_inverted) {
4495 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4496 } else {
4497 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4498 }
4499 } else {
4500 DCHECK_NE(cond_reg, AT);
4501 if (cond_inverted) {
4502 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4503 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4504 } else {
4505 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4506 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4507 }
4508 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4509 }
4510 break;
4511 case Primitive::kPrimFloat: {
4512 if (!Primitive::IsFloatingPointType(cond_type)) {
4513 // sel*.fmt tests bit 0 of the condition register, account for that.
4514 __ Sltu(TMP, ZERO, cond_reg);
4515 __ Mtc1(TMP, fcond_reg);
4516 }
4517 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4518 if (true_src.IsConstant()) {
4519 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4520 if (cond_inverted) {
4521 __ SelnezS(dst_reg, src_reg, fcond_reg);
4522 } else {
4523 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4524 }
4525 } else if (false_src.IsConstant()) {
4526 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4527 if (cond_inverted) {
4528 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4529 } else {
4530 __ SelnezS(dst_reg, src_reg, fcond_reg);
4531 }
4532 } else {
4533 if (cond_inverted) {
4534 __ SelS(fcond_reg,
4535 true_src.AsFpuRegister<FpuRegister>(),
4536 false_src.AsFpuRegister<FpuRegister>());
4537 } else {
4538 __ SelS(fcond_reg,
4539 false_src.AsFpuRegister<FpuRegister>(),
4540 true_src.AsFpuRegister<FpuRegister>());
4541 }
4542 __ MovS(dst_reg, fcond_reg);
4543 }
4544 break;
4545 }
4546 case Primitive::kPrimDouble: {
4547 if (!Primitive::IsFloatingPointType(cond_type)) {
4548 // sel*.fmt tests bit 0 of the condition register, account for that.
4549 __ Sltu(TMP, ZERO, cond_reg);
4550 __ Mtc1(TMP, fcond_reg);
4551 }
4552 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4553 if (true_src.IsConstant()) {
4554 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4555 if (cond_inverted) {
4556 __ SelnezD(dst_reg, src_reg, fcond_reg);
4557 } else {
4558 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4559 }
4560 } else if (false_src.IsConstant()) {
4561 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4562 if (cond_inverted) {
4563 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4564 } else {
4565 __ SelnezD(dst_reg, src_reg, fcond_reg);
4566 }
4567 } else {
4568 if (cond_inverted) {
4569 __ SelD(fcond_reg,
4570 true_src.AsFpuRegister<FpuRegister>(),
4571 false_src.AsFpuRegister<FpuRegister>());
4572 } else {
4573 __ SelD(fcond_reg,
4574 false_src.AsFpuRegister<FpuRegister>(),
4575 true_src.AsFpuRegister<FpuRegister>());
4576 }
4577 __ MovD(dst_reg, fcond_reg);
4578 }
4579 break;
4580 }
4581 }
4582}
4583
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004584void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4585 LocationSummary* locations = new (GetGraph()->GetArena())
4586 LocationSummary(flag, LocationSummary::kNoCall);
4587 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004588}
4589
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004590void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4591 __ LoadFromOffset(kLoadWord,
4592 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4593 SP,
4594 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004595}
4596
David Brazdil74eb1b22015-12-14 11:44:01 +00004597void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
4598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004599 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004600}
4601
4602void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004603 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4604 GenConditionalMove(select);
4605 } else {
4606 LocationSummary* locations = select->GetLocations();
4607 Mips64Label false_target;
4608 GenerateTestAndBranch(select,
4609 /* condition_input_index */ 2,
4610 /* true_target */ nullptr,
4611 &false_target);
4612 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4613 __ Bind(&false_target);
4614 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004615}
4616
David Srbecky0cf44932015-12-09 14:09:59 +00004617void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4618 new (GetGraph()->GetArena()) LocationSummary(info);
4619}
4620
David Srbeckyd28f4a02016-03-14 17:14:24 +00004621void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4622 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004623}
4624
4625void CodeGeneratorMIPS64::GenerateNop() {
4626 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004627}
4628
Alexey Frunze4dda3372015-06-01 18:31:49 -07004629void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004630 const FieldInfo& field_info) {
4631 Primitive::Type field_type = field_info.GetFieldType();
4632 bool object_field_get_with_read_barrier =
4633 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
4634 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4635 instruction,
4636 object_field_get_with_read_barrier
4637 ? LocationSummary::kCallOnSlowPath
4638 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004639 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4640 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4641 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004642 locations->SetInAt(0, Location::RequiresRegister());
4643 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4644 locations->SetOut(Location::RequiresFpuRegister());
4645 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004646 // The output overlaps in the case of an object field get with
4647 // read barriers enabled: we do not want the move to overwrite the
4648 // object's location, as we need it to emit the read barrier.
4649 locations->SetOut(Location::RequiresRegister(),
4650 object_field_get_with_read_barrier
4651 ? Location::kOutputOverlap
4652 : Location::kNoOutputOverlap);
4653 }
4654 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4655 // We need a temporary register for the read barrier marking slow
4656 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004657 if (!kBakerReadBarrierThunksEnableForFields) {
4658 locations->AddTemp(Location::RequiresRegister());
4659 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004660 }
4661}
4662
4663void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4664 const FieldInfo& field_info) {
4665 Primitive::Type type = field_info.GetFieldType();
4666 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004667 Location obj_loc = locations->InAt(0);
4668 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4669 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004670 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004671 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004672 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004673 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4674
Alexey Frunze4dda3372015-06-01 18:31:49 -07004675 switch (type) {
4676 case Primitive::kPrimBoolean:
4677 load_type = kLoadUnsignedByte;
4678 break;
4679 case Primitive::kPrimByte:
4680 load_type = kLoadSignedByte;
4681 break;
4682 case Primitive::kPrimShort:
4683 load_type = kLoadSignedHalfword;
4684 break;
4685 case Primitive::kPrimChar:
4686 load_type = kLoadUnsignedHalfword;
4687 break;
4688 case Primitive::kPrimInt:
4689 case Primitive::kPrimFloat:
4690 load_type = kLoadWord;
4691 break;
4692 case Primitive::kPrimLong:
4693 case Primitive::kPrimDouble:
4694 load_type = kLoadDoubleword;
4695 break;
4696 case Primitive::kPrimNot:
4697 load_type = kLoadUnsignedWord;
4698 break;
4699 case Primitive::kPrimVoid:
4700 LOG(FATAL) << "Unreachable type " << type;
4701 UNREACHABLE();
4702 }
4703 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004704 DCHECK(dst_loc.IsRegister());
4705 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4706 if (type == Primitive::kPrimNot) {
4707 // /* HeapReference<Object> */ dst = *(obj + offset)
4708 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004709 Location temp_loc =
4710 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004711 // Note that a potential implicit null check is handled in this
4712 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4713 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4714 dst_loc,
4715 obj,
4716 offset,
4717 temp_loc,
4718 /* needs_null_check */ true);
4719 if (is_volatile) {
4720 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4721 }
4722 } else {
4723 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4724 if (is_volatile) {
4725 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4726 }
4727 // If read barriers are enabled, emit read barriers other than
4728 // Baker's using a slow path (and also unpoison the loaded
4729 // reference, if heap poisoning is enabled).
4730 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4731 }
4732 } else {
4733 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4734 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004735 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004736 DCHECK(dst_loc.IsFpuRegister());
4737 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004738 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004739 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004740
Alexey Frunze15958152017-02-09 19:08:30 -08004741 // Memory barriers, in the case of references, are handled in the
4742 // previous switch statement.
4743 if (is_volatile && (type != Primitive::kPrimNot)) {
4744 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004745 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004746}
4747
4748void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4749 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4750 LocationSummary* locations =
4751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4752 locations->SetInAt(0, Location::RequiresRegister());
4753 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004754 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004755 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004756 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004757 }
4758}
4759
4760void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004761 const FieldInfo& field_info,
4762 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004763 Primitive::Type type = field_info.GetFieldType();
4764 LocationSummary* locations = instruction->GetLocations();
4765 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004766 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004767 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004768 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004769 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4770 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004771 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4772
Alexey Frunze4dda3372015-06-01 18:31:49 -07004773 switch (type) {
4774 case Primitive::kPrimBoolean:
4775 case Primitive::kPrimByte:
4776 store_type = kStoreByte;
4777 break;
4778 case Primitive::kPrimShort:
4779 case Primitive::kPrimChar:
4780 store_type = kStoreHalfword;
4781 break;
4782 case Primitive::kPrimInt:
4783 case Primitive::kPrimFloat:
4784 case Primitive::kPrimNot:
4785 store_type = kStoreWord;
4786 break;
4787 case Primitive::kPrimLong:
4788 case Primitive::kPrimDouble:
4789 store_type = kStoreDoubleword;
4790 break;
4791 case Primitive::kPrimVoid:
4792 LOG(FATAL) << "Unreachable type " << type;
4793 UNREACHABLE();
4794 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004795
Alexey Frunze15958152017-02-09 19:08:30 -08004796 if (is_volatile) {
4797 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4798 }
4799
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004800 if (value_location.IsConstant()) {
4801 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4802 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4803 } else {
4804 if (!Primitive::IsFloatingPointType(type)) {
4805 DCHECK(value_location.IsRegister());
4806 GpuRegister src = value_location.AsRegister<GpuRegister>();
4807 if (kPoisonHeapReferences && needs_write_barrier) {
4808 // Note that in the case where `value` is a null reference,
4809 // we do not enter this block, as a null reference does not
4810 // need poisoning.
4811 DCHECK_EQ(type, Primitive::kPrimNot);
4812 __ PoisonHeapReference(TMP, src);
4813 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4814 } else {
4815 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4816 }
4817 } else {
4818 DCHECK(value_location.IsFpuRegister());
4819 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4820 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4821 }
4822 }
Alexey Frunze15958152017-02-09 19:08:30 -08004823
Alexey Frunzec061de12017-02-14 13:27:23 -08004824 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004825 DCHECK(value_location.IsRegister());
4826 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004827 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004828 }
Alexey Frunze15958152017-02-09 19:08:30 -08004829
4830 if (is_volatile) {
4831 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4832 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004833}
4834
4835void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4836 HandleFieldGet(instruction, instruction->GetFieldInfo());
4837}
4838
4839void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4840 HandleFieldGet(instruction, instruction->GetFieldInfo());
4841}
4842
4843void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4844 HandleFieldSet(instruction, instruction->GetFieldInfo());
4845}
4846
4847void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004848 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004849}
4850
Alexey Frunze15958152017-02-09 19:08:30 -08004851void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4852 HInstruction* instruction,
4853 Location out,
4854 uint32_t offset,
4855 Location maybe_temp,
4856 ReadBarrierOption read_barrier_option) {
4857 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4858 if (read_barrier_option == kWithReadBarrier) {
4859 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004860 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4861 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4862 }
Alexey Frunze15958152017-02-09 19:08:30 -08004863 if (kUseBakerReadBarrier) {
4864 // Load with fast path based Baker's read barrier.
4865 // /* HeapReference<Object> */ out = *(out + offset)
4866 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4867 out,
4868 out_reg,
4869 offset,
4870 maybe_temp,
4871 /* needs_null_check */ false);
4872 } else {
4873 // Load with slow path based read barrier.
4874 // Save the value of `out` into `maybe_temp` before overwriting it
4875 // in the following move operation, as we will need it for the
4876 // read barrier below.
4877 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4878 // /* HeapReference<Object> */ out = *(out + offset)
4879 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4880 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4881 }
4882 } else {
4883 // Plain load with no read barrier.
4884 // /* HeapReference<Object> */ out = *(out + offset)
4885 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4886 __ MaybeUnpoisonHeapReference(out_reg);
4887 }
4888}
4889
4890void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4891 HInstruction* instruction,
4892 Location out,
4893 Location obj,
4894 uint32_t offset,
4895 Location maybe_temp,
4896 ReadBarrierOption read_barrier_option) {
4897 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4898 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4899 if (read_barrier_option == kWithReadBarrier) {
4900 CHECK(kEmitCompilerReadBarrier);
4901 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004902 if (!kBakerReadBarrierThunksEnableForFields) {
4903 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4904 }
Alexey Frunze15958152017-02-09 19:08:30 -08004905 // Load with fast path based Baker's read barrier.
4906 // /* HeapReference<Object> */ out = *(obj + offset)
4907 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4908 out,
4909 obj_reg,
4910 offset,
4911 maybe_temp,
4912 /* needs_null_check */ false);
4913 } else {
4914 // Load with slow path based read barrier.
4915 // /* HeapReference<Object> */ out = *(obj + offset)
4916 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4917 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4918 }
4919 } else {
4920 // Plain load with no read barrier.
4921 // /* HeapReference<Object> */ out = *(obj + offset)
4922 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4923 __ MaybeUnpoisonHeapReference(out_reg);
4924 }
4925}
4926
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004927static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4928 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4929 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4930 return reg - V0;
4931 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4932 return 13 + (reg - S2);
4933 } else if (reg == S8) { // One more.
4934 return 19;
4935 }
4936 LOG(FATAL) << "Unexpected register " << reg;
4937 UNREACHABLE();
4938}
4939
4940static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4941 int num = GetBakerMarkThunkNumber(reg) +
4942 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4943 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4944}
4945
4946static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4947 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4948 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4949}
4950
4951void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4952 Location root,
4953 GpuRegister obj,
4954 uint32_t offset,
4955 ReadBarrierOption read_barrier_option,
4956 Mips64Label* label_low) {
4957 if (label_low != nullptr) {
4958 DCHECK_EQ(offset, 0x5678u);
4959 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004960 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004961 if (read_barrier_option == kWithReadBarrier) {
4962 DCHECK(kEmitCompilerReadBarrier);
4963 if (kUseBakerReadBarrier) {
4964 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4965 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004966 if (kBakerReadBarrierThunksEnableForGcRoots) {
4967 // Note that we do not actually check the value of `GetIsGcMarking()`
4968 // to decide whether to mark the loaded GC root or not. Instead, we
4969 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4970 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4971 // vice versa.
4972 //
4973 // We use thunks for the slow path. That thunk checks the reference
4974 // and jumps to the entrypoint if needed.
4975 //
4976 // temp = Thread::Current()->pReadBarrierMarkReg00
4977 // // AKA &art_quick_read_barrier_mark_introspection.
4978 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4979 // if (temp != nullptr) {
4980 // temp = &gc_root_thunk<root_reg>
4981 // root = temp(root)
4982 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004983
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004984 const int32_t entry_point_offset =
4985 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4986 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4987 int16_t offset_low = Low16Bits(offset);
4988 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
4989 // extension in lwu.
4990 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4991 GpuRegister base = short_offset ? obj : TMP;
4992 // Loading the entrypoint does not require a load acquire since it is only changed when
4993 // threads are suspended or running a checkpoint.
4994 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4995 if (!short_offset) {
4996 DCHECK(!label_low);
4997 __ Daui(base, obj, offset_high);
4998 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07004999 Mips64Label skip_call;
5000 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005001 if (label_low != nullptr) {
5002 DCHECK(short_offset);
5003 __ Bind(label_low);
5004 }
5005 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5006 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5007 // in delay slot.
5008 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005009 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005010 } else {
5011 // Note that we do not actually check the value of `GetIsGcMarking()`
5012 // to decide whether to mark the loaded GC root or not. Instead, we
5013 // load into `temp` (T9) the read barrier mark entry point corresponding
5014 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5015 // is false, and vice versa.
5016 //
5017 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5018 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5019 // if (temp != null) {
5020 // root = temp(root)
5021 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005022
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005023 if (label_low != nullptr) {
5024 __ Bind(label_low);
5025 }
5026 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5027 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5028 static_assert(
5029 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5030 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5031 "have different sizes.");
5032 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5033 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5034 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005035
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005036 // Slow path marking the GC root `root`.
5037 Location temp = Location::RegisterLocation(T9);
5038 SlowPathCodeMIPS64* slow_path =
5039 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
5040 instruction,
5041 root,
5042 /*entrypoint*/ temp);
5043 codegen_->AddSlowPath(slow_path);
5044
5045 const int32_t entry_point_offset =
5046 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5047 // Loading the entrypoint does not require a load acquire since it is only changed when
5048 // threads are suspended or running a checkpoint.
5049 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5050 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5051 __ Bind(slow_path->GetExitLabel());
5052 }
Alexey Frunze15958152017-02-09 19:08:30 -08005053 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005054 if (label_low != nullptr) {
5055 __ Bind(label_low);
5056 }
Alexey Frunze15958152017-02-09 19:08:30 -08005057 // GC root loaded through a slow path for read barriers other
5058 // than Baker's.
5059 // /* GcRoot<mirror::Object>* */ root = obj + offset
5060 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5061 // /* mirror::Object* */ root = root->Read()
5062 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5063 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005064 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005065 if (label_low != nullptr) {
5066 __ Bind(label_low);
5067 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005068 // Plain GC root load with no read barrier.
5069 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5070 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5071 // Note that GC roots are not affected by heap poisoning, thus we
5072 // do not have to unpoison `root_reg` here.
5073 }
5074}
5075
Alexey Frunze15958152017-02-09 19:08:30 -08005076void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5077 Location ref,
5078 GpuRegister obj,
5079 uint32_t offset,
5080 Location temp,
5081 bool needs_null_check) {
5082 DCHECK(kEmitCompilerReadBarrier);
5083 DCHECK(kUseBakerReadBarrier);
5084
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005085 if (kBakerReadBarrierThunksEnableForFields) {
5086 // Note that we do not actually check the value of `GetIsGcMarking()`
5087 // to decide whether to mark the loaded reference or not. Instead, we
5088 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5089 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5090 // vice versa.
5091 //
5092 // We use thunks for the slow path. That thunk checks the reference
5093 // and jumps to the entrypoint if needed. If the holder is not gray,
5094 // it issues a load-load memory barrier and returns to the original
5095 // reference load.
5096 //
5097 // temp = Thread::Current()->pReadBarrierMarkReg00
5098 // // AKA &art_quick_read_barrier_mark_introspection.
5099 // if (temp != nullptr) {
5100 // temp = &field_array_thunk<holder_reg>
5101 // temp()
5102 // }
5103 // not_gray_return_address:
5104 // // If the offset is too large to fit into the lw instruction, we
5105 // // use an adjusted base register (TMP) here. This register
5106 // // receives bits 16 ... 31 of the offset before the thunk invocation
5107 // // and the thunk benefits from it.
5108 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5109 // gray_return_address:
5110
5111 DCHECK(temp.IsInvalid());
5112 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5113 const int32_t entry_point_offset =
5114 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5115 // There may have or may have not been a null check if the field offset is smaller than
5116 // the page size.
5117 // There must've been a null check in case it's actually a load from an array.
5118 // We will, however, perform an explicit null check in the thunk as it's easier to
5119 // do it than not.
5120 if (instruction->IsArrayGet()) {
5121 DCHECK(!needs_null_check);
5122 }
5123 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5124 // Loading the entrypoint does not require a load acquire since it is only changed when
5125 // threads are suspended or running a checkpoint.
5126 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5127 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005128 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005129 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005130 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005131 __ Nop(); // In forbidden slot.
5132 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005133 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005134 // /* HeapReference<Object> */ ref = *(obj + offset)
5135 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5136 } else {
5137 int16_t offset_low = Low16Bits(offset);
5138 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005139 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005140 __ Daui(TMP, obj, offset_high); // In delay slot.
5141 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005142 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005143 // /* HeapReference<Object> */ ref = *(obj + offset)
5144 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5145 }
5146 if (needs_null_check) {
5147 MaybeRecordImplicitNullCheck(instruction);
5148 }
5149 __ MaybeUnpoisonHeapReference(ref_reg);
5150 return;
5151 }
5152
Alexey Frunze15958152017-02-09 19:08:30 -08005153 // /* HeapReference<Object> */ ref = *(obj + offset)
5154 Location no_index = Location::NoLocation();
5155 ScaleFactor no_scale_factor = TIMES_1;
5156 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5157 ref,
5158 obj,
5159 offset,
5160 no_index,
5161 no_scale_factor,
5162 temp,
5163 needs_null_check);
5164}
5165
5166void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5167 Location ref,
5168 GpuRegister obj,
5169 uint32_t data_offset,
5170 Location index,
5171 Location temp,
5172 bool needs_null_check) {
5173 DCHECK(kEmitCompilerReadBarrier);
5174 DCHECK(kUseBakerReadBarrier);
5175
5176 static_assert(
5177 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5178 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005179 ScaleFactor scale_factor = TIMES_4;
5180
5181 if (kBakerReadBarrierThunksEnableForArrays) {
5182 // Note that we do not actually check the value of `GetIsGcMarking()`
5183 // to decide whether to mark the loaded reference or not. Instead, we
5184 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5185 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5186 // vice versa.
5187 //
5188 // We use thunks for the slow path. That thunk checks the reference
5189 // and jumps to the entrypoint if needed. If the holder is not gray,
5190 // it issues a load-load memory barrier and returns to the original
5191 // reference load.
5192 //
5193 // temp = Thread::Current()->pReadBarrierMarkReg00
5194 // // AKA &art_quick_read_barrier_mark_introspection.
5195 // if (temp != nullptr) {
5196 // temp = &field_array_thunk<holder_reg>
5197 // temp()
5198 // }
5199 // not_gray_return_address:
5200 // // The element address is pre-calculated in the TMP register before the
5201 // // thunk invocation and the thunk benefits from it.
5202 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5203 // gray_return_address:
5204
5205 DCHECK(temp.IsInvalid());
5206 DCHECK(index.IsValid());
5207 const int32_t entry_point_offset =
5208 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5209 // We will not do the explicit null check in the thunk as some form of a null check
5210 // must've been done earlier.
5211 DCHECK(!needs_null_check);
5212 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5213 // Loading the entrypoint does not require a load acquire since it is only changed when
5214 // threads are suspended or running a checkpoint.
5215 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005216 Mips64Label skip_call;
5217 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005218 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5219 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5220 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5221 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005222 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005223 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5224 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5225 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5226 __ MaybeUnpoisonHeapReference(ref_reg);
5227 return;
5228 }
5229
Alexey Frunze15958152017-02-09 19:08:30 -08005230 // /* HeapReference<Object> */ ref =
5231 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005232 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5233 ref,
5234 obj,
5235 data_offset,
5236 index,
5237 scale_factor,
5238 temp,
5239 needs_null_check);
5240}
5241
5242void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5243 Location ref,
5244 GpuRegister obj,
5245 uint32_t offset,
5246 Location index,
5247 ScaleFactor scale_factor,
5248 Location temp,
5249 bool needs_null_check,
5250 bool always_update_field) {
5251 DCHECK(kEmitCompilerReadBarrier);
5252 DCHECK(kUseBakerReadBarrier);
5253
5254 // In slow path based read barriers, the read barrier call is
5255 // inserted after the original load. However, in fast path based
5256 // Baker's read barriers, we need to perform the load of
5257 // mirror::Object::monitor_ *before* the original reference load.
5258 // This load-load ordering is required by the read barrier.
5259 // The fast path/slow path (for Baker's algorithm) should look like:
5260 //
5261 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5262 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5263 // HeapReference<Object> ref = *src; // Original reference load.
5264 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5265 // if (is_gray) {
5266 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5267 // }
5268 //
5269 // Note: the original implementation in ReadBarrier::Barrier is
5270 // slightly more complex as it performs additional checks that we do
5271 // not do here for performance reasons.
5272
5273 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5274 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5275 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5276
5277 // /* int32_t */ monitor = obj->monitor_
5278 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5279 if (needs_null_check) {
5280 MaybeRecordImplicitNullCheck(instruction);
5281 }
5282 // /* LockWord */ lock_word = LockWord(monitor)
5283 static_assert(sizeof(LockWord) == sizeof(int32_t),
5284 "art::LockWord and int32_t have different sizes.");
5285
5286 __ Sync(0); // Barrier to prevent load-load reordering.
5287
5288 // The actual reference load.
5289 if (index.IsValid()) {
5290 // Load types involving an "index": ArrayGet,
5291 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5292 // intrinsics.
5293 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5294 if (index.IsConstant()) {
5295 size_t computed_offset =
5296 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5297 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5298 } else {
5299 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005300 if (scale_factor == TIMES_1) {
5301 __ Daddu(TMP, index_reg, obj);
5302 } else {
5303 __ Dlsa(TMP, index_reg, obj, scale_factor);
5304 }
Alexey Frunze15958152017-02-09 19:08:30 -08005305 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5306 }
5307 } else {
5308 // /* HeapReference<Object> */ ref = *(obj + offset)
5309 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5310 }
5311
5312 // Object* ref = ref_addr->AsMirrorPtr()
5313 __ MaybeUnpoisonHeapReference(ref_reg);
5314
5315 // Slow path marking the object `ref` when it is gray.
5316 SlowPathCodeMIPS64* slow_path;
5317 if (always_update_field) {
5318 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5319 // of the form `obj + field_offset`, where `obj` is a register and
5320 // `field_offset` is a register. Thus `offset` and `scale_factor`
5321 // above are expected to be null in this code path.
5322 DCHECK_EQ(offset, 0u);
5323 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
5324 slow_path = new (GetGraph()->GetArena())
5325 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5326 ref,
5327 obj,
5328 /* field_offset */ index,
5329 temp_reg);
5330 } else {
5331 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
5332 }
5333 AddSlowPath(slow_path);
5334
5335 // if (rb_state == ReadBarrier::GrayState())
5336 // ref = ReadBarrier::Mark(ref);
5337 // Given the numeric representation, it's enough to check the low bit of the
5338 // rb_state. We do that by shifting the bit into the sign bit (31) and
5339 // performing a branch on less than zero.
5340 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5341 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5342 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5343 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5344 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5345 __ Bind(slow_path->GetExitLabel());
5346}
5347
5348void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5349 Location out,
5350 Location ref,
5351 Location obj,
5352 uint32_t offset,
5353 Location index) {
5354 DCHECK(kEmitCompilerReadBarrier);
5355
5356 // Insert a slow path based read barrier *after* the reference load.
5357 //
5358 // If heap poisoning is enabled, the unpoisoning of the loaded
5359 // reference will be carried out by the runtime within the slow
5360 // path.
5361 //
5362 // Note that `ref` currently does not get unpoisoned (when heap
5363 // poisoning is enabled), which is alright as the `ref` argument is
5364 // not used by the artReadBarrierSlow entry point.
5365 //
5366 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5367 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
5368 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5369 AddSlowPath(slow_path);
5370
5371 __ Bc(slow_path->GetEntryLabel());
5372 __ Bind(slow_path->GetExitLabel());
5373}
5374
5375void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5376 Location out,
5377 Location ref,
5378 Location obj,
5379 uint32_t offset,
5380 Location index) {
5381 if (kEmitCompilerReadBarrier) {
5382 // Baker's read barriers shall be handled by the fast path
5383 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5384 DCHECK(!kUseBakerReadBarrier);
5385 // If heap poisoning is enabled, unpoisoning will be taken care of
5386 // by the runtime within the slow path.
5387 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5388 } else if (kPoisonHeapReferences) {
5389 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5390 }
5391}
5392
5393void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5394 Location out,
5395 Location root) {
5396 DCHECK(kEmitCompilerReadBarrier);
5397
5398 // Insert a slow path based read barrier *after* the GC root load.
5399 //
5400 // Note that GC roots are not affected by heap poisoning, so we do
5401 // not need to do anything special for this here.
5402 SlowPathCodeMIPS64* slow_path =
5403 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
5404 AddSlowPath(slow_path);
5405
5406 __ Bc(slow_path->GetEntryLabel());
5407 __ Bind(slow_path->GetExitLabel());
5408}
5409
Alexey Frunze4dda3372015-06-01 18:31:49 -07005410void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005411 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5412 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005413 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005414 switch (type_check_kind) {
5415 case TypeCheckKind::kExactCheck:
5416 case TypeCheckKind::kAbstractClassCheck:
5417 case TypeCheckKind::kClassHierarchyCheck:
5418 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005419 call_kind =
5420 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005421 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005422 break;
5423 case TypeCheckKind::kArrayCheck:
5424 case TypeCheckKind::kUnresolvedCheck:
5425 case TypeCheckKind::kInterfaceCheck:
5426 call_kind = LocationSummary::kCallOnSlowPath;
5427 break;
5428 }
5429
Alexey Frunze4dda3372015-06-01 18:31:49 -07005430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005431 if (baker_read_barrier_slow_path) {
5432 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5433 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005434 locations->SetInAt(0, Location::RequiresRegister());
5435 locations->SetInAt(1, Location::RequiresRegister());
5436 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005437 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005438 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005439 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005440}
5441
5442void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005443 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005444 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005445 Location obj_loc = locations->InAt(0);
5446 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005447 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005448 Location out_loc = locations->Out();
5449 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5450 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5451 DCHECK_LE(num_temps, 1u);
5452 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005453 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5454 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5455 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5456 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005457 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005458 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005459
5460 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005461 // Avoid this check if we know `obj` is not null.
5462 if (instruction->MustDoNullCheck()) {
5463 __ Move(out, ZERO);
5464 __ Beqzc(obj, &done);
5465 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005466
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005467 switch (type_check_kind) {
5468 case TypeCheckKind::kExactCheck: {
5469 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005470 GenerateReferenceLoadTwoRegisters(instruction,
5471 out_loc,
5472 obj_loc,
5473 class_offset,
5474 maybe_temp_loc,
5475 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005476 // Classes must be equal for the instanceof to succeed.
5477 __ Xor(out, out, cls);
5478 __ Sltiu(out, out, 1);
5479 break;
5480 }
5481
5482 case TypeCheckKind::kAbstractClassCheck: {
5483 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005484 GenerateReferenceLoadTwoRegisters(instruction,
5485 out_loc,
5486 obj_loc,
5487 class_offset,
5488 maybe_temp_loc,
5489 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005490 // If the class is abstract, we eagerly fetch the super class of the
5491 // object to avoid doing a comparison we know will fail.
5492 Mips64Label loop;
5493 __ Bind(&loop);
5494 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005495 GenerateReferenceLoadOneRegister(instruction,
5496 out_loc,
5497 super_offset,
5498 maybe_temp_loc,
5499 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005500 // If `out` is null, we use it for the result, and jump to `done`.
5501 __ Beqzc(out, &done);
5502 __ Bnec(out, cls, &loop);
5503 __ LoadConst32(out, 1);
5504 break;
5505 }
5506
5507 case TypeCheckKind::kClassHierarchyCheck: {
5508 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005509 GenerateReferenceLoadTwoRegisters(instruction,
5510 out_loc,
5511 obj_loc,
5512 class_offset,
5513 maybe_temp_loc,
5514 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005515 // Walk over the class hierarchy to find a match.
5516 Mips64Label loop, success;
5517 __ Bind(&loop);
5518 __ Beqc(out, cls, &success);
5519 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005520 GenerateReferenceLoadOneRegister(instruction,
5521 out_loc,
5522 super_offset,
5523 maybe_temp_loc,
5524 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005525 __ Bnezc(out, &loop);
5526 // If `out` is null, we use it for the result, and jump to `done`.
5527 __ Bc(&done);
5528 __ Bind(&success);
5529 __ LoadConst32(out, 1);
5530 break;
5531 }
5532
5533 case TypeCheckKind::kArrayObjectCheck: {
5534 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005535 GenerateReferenceLoadTwoRegisters(instruction,
5536 out_loc,
5537 obj_loc,
5538 class_offset,
5539 maybe_temp_loc,
5540 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005541 // Do an exact check.
5542 Mips64Label success;
5543 __ Beqc(out, cls, &success);
5544 // Otherwise, we need to check that the object's class is a non-primitive array.
5545 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005546 GenerateReferenceLoadOneRegister(instruction,
5547 out_loc,
5548 component_offset,
5549 maybe_temp_loc,
5550 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005551 // If `out` is null, we use it for the result, and jump to `done`.
5552 __ Beqzc(out, &done);
5553 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5554 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5555 __ Sltiu(out, out, 1);
5556 __ Bc(&done);
5557 __ Bind(&success);
5558 __ LoadConst32(out, 1);
5559 break;
5560 }
5561
5562 case TypeCheckKind::kArrayCheck: {
5563 // No read barrier since the slow path will retry upon failure.
5564 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005565 GenerateReferenceLoadTwoRegisters(instruction,
5566 out_loc,
5567 obj_loc,
5568 class_offset,
5569 maybe_temp_loc,
5570 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005571 DCHECK(locations->OnlyCallsOnSlowPath());
5572 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5573 /* is_fatal */ false);
5574 codegen_->AddSlowPath(slow_path);
5575 __ Bnec(out, cls, slow_path->GetEntryLabel());
5576 __ LoadConst32(out, 1);
5577 break;
5578 }
5579
5580 case TypeCheckKind::kUnresolvedCheck:
5581 case TypeCheckKind::kInterfaceCheck: {
5582 // Note that we indeed only call on slow path, but we always go
5583 // into the slow path for the unresolved and interface check
5584 // cases.
5585 //
5586 // We cannot directly call the InstanceofNonTrivial runtime
5587 // entry point without resorting to a type checking slow path
5588 // here (i.e. by calling InvokeRuntime directly), as it would
5589 // require to assign fixed registers for the inputs of this
5590 // HInstanceOf instruction (following the runtime calling
5591 // convention), which might be cluttered by the potential first
5592 // read barrier emission at the beginning of this method.
5593 //
5594 // TODO: Introduce a new runtime entry point taking the object
5595 // to test (instead of its class) as argument, and let it deal
5596 // with the read barrier issues. This will let us refactor this
5597 // case of the `switch` code as it was previously (with a direct
5598 // call to the runtime not using a type checking slow path).
5599 // This should also be beneficial for the other cases above.
5600 DCHECK(locations->OnlyCallsOnSlowPath());
5601 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5602 /* is_fatal */ false);
5603 codegen_->AddSlowPath(slow_path);
5604 __ Bc(slow_path->GetEntryLabel());
5605 break;
5606 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005607 }
5608
5609 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005610
5611 if (slow_path != nullptr) {
5612 __ Bind(slow_path->GetExitLabel());
5613 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005614}
5615
5616void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
5617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5618 locations->SetOut(Location::ConstantLocation(constant));
5619}
5620
5621void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5622 // Will be generated at use site.
5623}
5624
5625void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
5626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5627 locations->SetOut(Location::ConstantLocation(constant));
5628}
5629
5630void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5631 // Will be generated at use site.
5632}
5633
Calin Juravle175dc732015-08-25 15:42:32 +01005634void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5635 // The trampoline uses the same calling convention as dex calling conventions,
5636 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5637 // the method_idx.
5638 HandleInvoke(invoke);
5639}
5640
5641void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5642 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5643}
5644
Alexey Frunze4dda3372015-06-01 18:31:49 -07005645void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5646 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5647 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5648}
5649
5650void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5651 HandleInvoke(invoke);
5652 // The register T0 is required to be used for the hidden argument in
5653 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5654 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5655}
5656
5657void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5658 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5659 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005660 Location receiver = invoke->GetLocations()->InAt(0);
5661 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005662 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005663
5664 // Set the hidden argument.
5665 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5666 invoke->GetDexMethodIndex());
5667
5668 // temp = object->GetClass();
5669 if (receiver.IsStackSlot()) {
5670 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5671 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5672 } else {
5673 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5674 }
5675 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005676 // Instead of simply (possibly) unpoisoning `temp` here, we should
5677 // emit a read barrier for the previous class reference load.
5678 // However this is not required in practice, as this is an
5679 // intermediate/temporary reference and because the current
5680 // concurrent copying collector keeps the from-space memory
5681 // intact/accessible until the end of the marking phase (the
5682 // concurrent copying collector may not in the future).
5683 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005684 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5685 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5686 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005687 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005688 // temp = temp->GetImtEntryAt(method_offset);
5689 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5690 // T9 = temp->GetEntryPoint();
5691 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5692 // T9();
5693 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005694 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005695 DCHECK(!codegen_->IsLeafMethod());
5696 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5697}
5698
5699void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005700 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5701 if (intrinsic.TryDispatch(invoke)) {
5702 return;
5703 }
5704
Alexey Frunze4dda3372015-06-01 18:31:49 -07005705 HandleInvoke(invoke);
5706}
5707
5708void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005709 // Explicit clinit checks triggered by static invokes must have been pruned by
5710 // art::PrepareForRegisterAllocation.
5711 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005712
Chris Larsen3039e382015-08-26 07:54:08 -07005713 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5714 if (intrinsic.TryDispatch(invoke)) {
5715 return;
5716 }
5717
Alexey Frunze4dda3372015-06-01 18:31:49 -07005718 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005719}
5720
Orion Hodsonac141392017-01-13 11:53:47 +00005721void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5722 HandleInvoke(invoke);
5723}
5724
5725void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5726 codegen_->GenerateInvokePolymorphicCall(invoke);
5727}
5728
Chris Larsen3039e382015-08-26 07:54:08 -07005729static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005730 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005731 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5732 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005733 return true;
5734 }
5735 return false;
5736}
5737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005738HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005739 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005740 bool fallback_load = false;
5741 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005742 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005743 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005744 case HLoadString::LoadKind::kBssEntry:
5745 DCHECK(!Runtime::Current()->UseJitCompilation());
5746 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005747 case HLoadString::LoadKind::kJitTableAddress:
5748 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005749 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005750 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005751 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005752 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005753 }
5754 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005755 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005756 }
5757 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005758}
5759
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005760HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5761 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005762 bool fallback_load = false;
5763 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005764 case HLoadClass::LoadKind::kInvalid:
5765 LOG(FATAL) << "UNREACHABLE";
5766 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005767 case HLoadClass::LoadKind::kReferrersClass:
5768 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005769 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005770 case HLoadClass::LoadKind::kBssEntry:
5771 DCHECK(!Runtime::Current()->UseJitCompilation());
5772 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005773 case HLoadClass::LoadKind::kJitTableAddress:
5774 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005775 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005776 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005777 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005778 break;
5779 }
5780 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005781 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005782 }
5783 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005784}
5785
Vladimir Markodc151b22015-10-15 18:02:30 +01005786HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5787 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005788 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005789 // On MIPS64 we support all dispatch types.
5790 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005791}
5792
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005793void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5794 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005795 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005796 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005797 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5798 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5799
Alexey Frunze19f6c692016-11-30 19:19:55 -08005800 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005801 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005802 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005803 uint32_t offset =
5804 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005805 __ LoadFromOffset(kLoadDoubleword,
5806 temp.AsRegister<GpuRegister>(),
5807 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005808 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005809 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005810 }
Vladimir Marko58155012015-08-19 12:49:41 +00005811 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005812 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005813 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005814 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5815 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005816 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005817 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005818 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5819 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5820 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005821 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5822 break;
5823 }
Vladimir Marko58155012015-08-19 12:49:41 +00005824 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005825 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5826 kLoadDoubleword,
5827 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005828 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005829 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005830 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005831 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005832 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5833 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5834 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005835 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5836 break;
5837 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005838 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5839 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5840 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005841 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005842 }
5843
Alexey Frunze19f6c692016-11-30 19:19:55 -08005844 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005845 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005846 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005847 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005848 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5849 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5850 __ LoadFromOffset(kLoadDoubleword,
5851 T9,
5852 callee_method.AsRegister<GpuRegister>(),
5853 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005854 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005855 // T9()
5856 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005857 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005858 break;
5859 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005860 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5861
Alexey Frunze4dda3372015-06-01 18:31:49 -07005862 DCHECK(!IsLeafMethod());
5863}
5864
5865void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005866 // Explicit clinit checks triggered by static invokes must have been pruned by
5867 // art::PrepareForRegisterAllocation.
5868 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005869
5870 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5871 return;
5872 }
5873
5874 LocationSummary* locations = invoke->GetLocations();
5875 codegen_->GenerateStaticOrDirectCall(invoke,
5876 locations->HasTemps()
5877 ? locations->GetTemp(0)
5878 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005879}
5880
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005881void CodeGeneratorMIPS64::GenerateVirtualCall(
5882 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005883 // Use the calling convention instead of the location of the receiver, as
5884 // intrinsics may have put the receiver in a different register. In the intrinsics
5885 // slow path, the arguments have been moved to the right place, so here we are
5886 // guaranteed that the receiver is the first register of the calling convention.
5887 InvokeDexCallingConvention calling_convention;
5888 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5889
Alexey Frunze53afca12015-11-05 16:34:23 -08005890 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005891 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5892 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5893 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005894 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005895
5896 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005897 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005898 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005899 // Instead of simply (possibly) unpoisoning `temp` here, we should
5900 // emit a read barrier for the previous class reference load.
5901 // However this is not required in practice, as this is an
5902 // intermediate/temporary reference and because the current
5903 // concurrent copying collector keeps the from-space memory
5904 // intact/accessible until the end of the marking phase (the
5905 // concurrent copying collector may not in the future).
5906 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005907 // temp = temp->GetMethodAt(method_offset);
5908 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5909 // T9 = temp->GetEntryPoint();
5910 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5911 // T9();
5912 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005913 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005914 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005915}
5916
5917void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5918 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5919 return;
5920 }
5921
5922 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005923 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005924}
5925
5926void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005927 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005928 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005929 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005930 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5931 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005932 return;
5933 }
Vladimir Marko41559982017-01-06 14:04:23 +00005934 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005935
Alexey Frunze15958152017-02-09 19:08:30 -08005936 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5937 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005938 ? LocationSummary::kCallOnSlowPath
5939 : LocationSummary::kNoCall;
5940 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005941 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5942 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5943 }
Vladimir Marko41559982017-01-06 14:04:23 +00005944 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005945 locations->SetInAt(0, Location::RequiresRegister());
5946 }
5947 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005948 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5949 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5950 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005951 // Request a temp to hold the BSS entry location for the slow path.
5952 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005953 RegisterSet caller_saves = RegisterSet::Empty();
5954 InvokeRuntimeCallingConvention calling_convention;
5955 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5956 locations->SetCustomSlowPathCallerSaves(caller_saves);
5957 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005958 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005959 }
5960 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005961}
5962
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005963// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5964// move.
5965void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005966 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005967 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005968 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005969 return;
5970 }
Vladimir Marko41559982017-01-06 14:04:23 +00005971 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005972
Vladimir Marko41559982017-01-06 14:04:23 +00005973 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005974 Location out_loc = locations->Out();
5975 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5976 GpuRegister current_method_reg = ZERO;
5977 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005978 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005979 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5980 }
5981
Alexey Frunze15958152017-02-09 19:08:30 -08005982 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5983 ? kWithoutReadBarrier
5984 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005985 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005986 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005987 switch (load_kind) {
5988 case HLoadClass::LoadKind::kReferrersClass:
5989 DCHECK(!cls->CanCallRuntime());
5990 DCHECK(!cls->MustGenerateClinitCheck());
5991 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5992 GenerateGcRootFieldLoad(cls,
5993 out_loc,
5994 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005995 ArtMethod::DeclaringClassOffset().Int32Value(),
5996 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005997 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005998 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005999 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006000 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006001 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006002 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006003 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6004 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6005 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006006 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6007 break;
6008 }
6009 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006010 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006011 uint32_t address = dchecked_integral_cast<uint32_t>(
6012 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6013 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006014 __ LoadLiteral(out,
6015 kLoadUnsignedWord,
6016 codegen_->DeduplicateBootImageAddressLiteral(address));
6017 break;
6018 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006019 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006020 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
6021 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6022 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
6023 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6024 GpuRegister temp = non_baker_read_barrier
6025 ? out
6026 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006027 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
6028 GenerateGcRootFieldLoad(cls,
6029 out_loc,
6030 temp,
6031 /* placeholder */ 0x5678,
6032 read_barrier_option,
6033 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006034 generate_null_check = true;
6035 break;
6036 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006037 case HLoadClass::LoadKind::kJitTableAddress:
6038 __ LoadLiteral(out,
6039 kLoadUnsignedWord,
6040 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6041 cls->GetTypeIndex(),
6042 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006043 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006044 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006045 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006046 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006047 LOG(FATAL) << "UNREACHABLE";
6048 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006049 }
6050
6051 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6052 DCHECK(cls->CanCallRuntime());
6053 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006054 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006055 codegen_->AddSlowPath(slow_path);
6056 if (generate_null_check) {
6057 __ Beqzc(out, slow_path->GetEntryLabel());
6058 }
6059 if (cls->MustGenerateClinitCheck()) {
6060 GenerateClassInitializationCheck(slow_path, out);
6061 } else {
6062 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006063 }
6064 }
6065}
6066
David Brazdilcb1c0552015-08-04 16:22:25 +01006067static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006068 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006069}
6070
Alexey Frunze4dda3372015-06-01 18:31:49 -07006071void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6072 LocationSummary* locations =
6073 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6074 locations->SetOut(Location::RequiresRegister());
6075}
6076
6077void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6078 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006079 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6080}
6081
6082void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
6083 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6084}
6085
6086void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6087 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006088}
6089
Alexey Frunze4dda3372015-06-01 18:31:49 -07006090void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006091 HLoadString::LoadKind load_kind = load->GetLoadKind();
6092 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006093 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006094 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006095 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006096 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006097 } else {
6098 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006099 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6100 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6101 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006102 // Request a temp to hold the BSS entry location for the slow path.
6103 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006104 RegisterSet caller_saves = RegisterSet::Empty();
6105 InvokeRuntimeCallingConvention calling_convention;
6106 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6107 locations->SetCustomSlowPathCallerSaves(caller_saves);
6108 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006109 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006110 }
6111 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006112 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006113}
6114
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006115// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6116// move.
6117void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006118 HLoadString::LoadKind load_kind = load->GetLoadKind();
6119 LocationSummary* locations = load->GetLocations();
6120 Location out_loc = locations->Out();
6121 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6122
6123 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006124 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
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 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006131 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006132 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006133 }
6134 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006135 uint32_t address = dchecked_integral_cast<uint32_t>(
6136 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6137 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006138 __ LoadLiteral(out,
6139 kLoadUnsignedWord,
6140 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006141 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006142 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006143 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006144 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006145 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006146 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006147 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6148 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006149 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6150 __ Lwu(out, AT, /* placeholder */ 0x5678);
6151 return;
6152 }
6153 case HLoadString::LoadKind::kBssEntry: {
6154 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6155 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6156 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6157 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6158 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006159 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6160 GpuRegister temp = non_baker_read_barrier
6161 ? out
6162 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006163 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08006164 GenerateGcRootFieldLoad(load,
6165 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006166 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08006167 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006168 kCompilerReadBarrierOption,
6169 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006170 SlowPathCodeMIPS64* slow_path =
6171 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006172 codegen_->AddSlowPath(slow_path);
6173 __ Beqzc(out, slow_path->GetEntryLabel());
6174 __ Bind(slow_path->GetExitLabel());
6175 return;
6176 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006177 case HLoadString::LoadKind::kJitTableAddress:
6178 __ LoadLiteral(out,
6179 kLoadUnsignedWord,
6180 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6181 load->GetStringIndex(),
6182 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006183 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006184 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006185 default:
6186 break;
6187 }
6188
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006189 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006190 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006191 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006192 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006193 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6194 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6195 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006196}
6197
Alexey Frunze4dda3372015-06-01 18:31:49 -07006198void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
6199 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6200 locations->SetOut(Location::ConstantLocation(constant));
6201}
6202
6203void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6204 // Will be generated at use site.
6205}
6206
6207void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
6208 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006209 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006210 InvokeRuntimeCallingConvention calling_convention;
6211 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6212}
6213
6214void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006215 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006216 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006217 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006218 if (instruction->IsEnter()) {
6219 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6220 } else {
6221 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6222 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006223}
6224
6225void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6226 LocationSummary* locations =
6227 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
6228 switch (mul->GetResultType()) {
6229 case Primitive::kPrimInt:
6230 case Primitive::kPrimLong:
6231 locations->SetInAt(0, Location::RequiresRegister());
6232 locations->SetInAt(1, Location::RequiresRegister());
6233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6234 break;
6235
6236 case Primitive::kPrimFloat:
6237 case Primitive::kPrimDouble:
6238 locations->SetInAt(0, Location::RequiresFpuRegister());
6239 locations->SetInAt(1, Location::RequiresFpuRegister());
6240 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6241 break;
6242
6243 default:
6244 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6245 }
6246}
6247
6248void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
6249 Primitive::Type type = instruction->GetType();
6250 LocationSummary* locations = instruction->GetLocations();
6251
6252 switch (type) {
6253 case Primitive::kPrimInt:
6254 case Primitive::kPrimLong: {
6255 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6256 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6257 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
6258 if (type == Primitive::kPrimInt)
6259 __ MulR6(dst, lhs, rhs);
6260 else
6261 __ Dmul(dst, lhs, rhs);
6262 break;
6263 }
6264 case Primitive::kPrimFloat:
6265 case Primitive::kPrimDouble: {
6266 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6267 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6268 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
6269 if (type == Primitive::kPrimFloat)
6270 __ MulS(dst, lhs, rhs);
6271 else
6272 __ MulD(dst, lhs, rhs);
6273 break;
6274 }
6275 default:
6276 LOG(FATAL) << "Unexpected mul type " << type;
6277 }
6278}
6279
6280void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6281 LocationSummary* locations =
6282 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
6283 switch (neg->GetResultType()) {
6284 case Primitive::kPrimInt:
6285 case Primitive::kPrimLong:
6286 locations->SetInAt(0, Location::RequiresRegister());
6287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6288 break;
6289
6290 case Primitive::kPrimFloat:
6291 case Primitive::kPrimDouble:
6292 locations->SetInAt(0, Location::RequiresFpuRegister());
6293 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6294 break;
6295
6296 default:
6297 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6298 }
6299}
6300
6301void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
6302 Primitive::Type type = instruction->GetType();
6303 LocationSummary* locations = instruction->GetLocations();
6304
6305 switch (type) {
6306 case Primitive::kPrimInt:
6307 case Primitive::kPrimLong: {
6308 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6309 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6310 if (type == Primitive::kPrimInt)
6311 __ Subu(dst, ZERO, src);
6312 else
6313 __ Dsubu(dst, ZERO, src);
6314 break;
6315 }
6316 case Primitive::kPrimFloat:
6317 case Primitive::kPrimDouble: {
6318 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6319 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6320 if (type == Primitive::kPrimFloat)
6321 __ NegS(dst, src);
6322 else
6323 __ NegD(dst, src);
6324 break;
6325 }
6326 default:
6327 LOG(FATAL) << "Unexpected neg type " << type;
6328 }
6329}
6330
6331void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
6332 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006334 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006335 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006336 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6337 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006338}
6339
6340void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006341 // Note: if heap poisoning is enabled, the entry point takes care
6342 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006343 QuickEntrypointEnum entrypoint =
6344 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6345 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006346 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006347 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006348}
6349
6350void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
6351 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006352 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006353 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006354 if (instruction->IsStringAlloc()) {
6355 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6356 } else {
6357 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006358 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006359 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6360}
6361
6362void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006363 // Note: if heap poisoning is enabled, the entry point takes care
6364 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006365 if (instruction->IsStringAlloc()) {
6366 // String is allocated through StringFactory. Call NewEmptyString entry point.
6367 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006368 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006369 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006370 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6371 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6372 __ Jalr(T9);
6373 __ Nop();
6374 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6375 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006376 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006377 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006378 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006379}
6380
6381void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
6382 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6383 locations->SetInAt(0, Location::RequiresRegister());
6384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6385}
6386
6387void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
6388 Primitive::Type type = instruction->GetType();
6389 LocationSummary* locations = instruction->GetLocations();
6390
6391 switch (type) {
6392 case Primitive::kPrimInt:
6393 case Primitive::kPrimLong: {
6394 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6395 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6396 __ Nor(dst, src, ZERO);
6397 break;
6398 }
6399
6400 default:
6401 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6402 }
6403}
6404
6405void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6407 locations->SetInAt(0, Location::RequiresRegister());
6408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6409}
6410
6411void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6412 LocationSummary* locations = instruction->GetLocations();
6413 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6414 locations->InAt(0).AsRegister<GpuRegister>(),
6415 1);
6416}
6417
6418void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006419 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6420 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006421}
6422
Calin Juravle2ae48182016-03-16 14:05:09 +00006423void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6424 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006425 return;
6426 }
6427 Location obj = instruction->GetLocations()->InAt(0);
6428
6429 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006430 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006431}
6432
Calin Juravle2ae48182016-03-16 14:05:09 +00006433void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006434 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006435 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006436
6437 Location obj = instruction->GetLocations()->InAt(0);
6438
6439 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6440}
6441
6442void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006443 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006444}
6445
6446void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6447 HandleBinaryOp(instruction);
6448}
6449
6450void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6451 HandleBinaryOp(instruction);
6452}
6453
6454void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6455 LOG(FATAL) << "Unreachable";
6456}
6457
6458void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
6459 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6460}
6461
6462void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
6463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6464 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6465 if (location.IsStackSlot()) {
6466 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6467 } else if (location.IsDoubleStackSlot()) {
6468 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6469 }
6470 locations->SetOut(location);
6471}
6472
6473void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6474 ATTRIBUTE_UNUSED) {
6475 // Nothing to do, the parameter is already at its location.
6476}
6477
6478void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6479 LocationSummary* locations =
6480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6481 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6482}
6483
6484void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6485 ATTRIBUTE_UNUSED) {
6486 // Nothing to do, the method is already at its location.
6487}
6488
6489void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
6490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006491 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006492 locations->SetInAt(i, Location::Any());
6493 }
6494 locations->SetOut(Location::Any());
6495}
6496
6497void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6498 LOG(FATAL) << "Unreachable";
6499}
6500
6501void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
6502 Primitive::Type type = rem->GetResultType();
6503 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006504 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6505 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6507
6508 switch (type) {
6509 case Primitive::kPrimInt:
6510 case Primitive::kPrimLong:
6511 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006512 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006513 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6514 break;
6515
6516 case Primitive::kPrimFloat:
6517 case Primitive::kPrimDouble: {
6518 InvokeRuntimeCallingConvention calling_convention;
6519 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6520 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6521 locations->SetOut(calling_convention.GetReturnLocation(type));
6522 break;
6523 }
6524
6525 default:
6526 LOG(FATAL) << "Unexpected rem type " << type;
6527 }
6528}
6529
6530void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
6531 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006532
6533 switch (type) {
6534 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07006535 case Primitive::kPrimLong:
6536 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006537 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006538
6539 case Primitive::kPrimFloat:
6540 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01006541 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
6542 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006543 if (type == Primitive::kPrimFloat) {
6544 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6545 } else {
6546 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6547 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006548 break;
6549 }
6550 default:
6551 LOG(FATAL) << "Unexpected rem type " << type;
6552 }
6553}
6554
Igor Murashkind01745e2017-04-05 16:40:31 -07006555void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6556 constructor_fence->SetLocations(nullptr);
6557}
6558
6559void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6560 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6561 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6562}
6563
Alexey Frunze4dda3372015-06-01 18:31:49 -07006564void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6565 memory_barrier->SetLocations(nullptr);
6566}
6567
6568void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6569 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6570}
6571
6572void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
6573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6574 Primitive::Type return_type = ret->InputAt(0)->GetType();
6575 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6576}
6577
6578void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6579 codegen_->GenerateFrameExit();
6580}
6581
6582void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6583 ret->SetLocations(nullptr);
6584}
6585
6586void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6587 codegen_->GenerateFrameExit();
6588}
6589
Alexey Frunze92d90602015-12-18 18:16:36 -08006590void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6591 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006592}
6593
Alexey Frunze92d90602015-12-18 18:16:36 -08006594void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6595 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006596}
6597
Alexey Frunze4dda3372015-06-01 18:31:49 -07006598void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6599 HandleShift(shl);
6600}
6601
6602void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6603 HandleShift(shl);
6604}
6605
6606void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6607 HandleShift(shr);
6608}
6609
6610void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6611 HandleShift(shr);
6612}
6613
Alexey Frunze4dda3372015-06-01 18:31:49 -07006614void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6615 HandleBinaryOp(instruction);
6616}
6617
6618void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6619 HandleBinaryOp(instruction);
6620}
6621
6622void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6623 HandleFieldGet(instruction, instruction->GetFieldInfo());
6624}
6625
6626void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6627 HandleFieldGet(instruction, instruction->GetFieldInfo());
6628}
6629
6630void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6631 HandleFieldSet(instruction, instruction->GetFieldInfo());
6632}
6633
6634void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006635 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006636}
6637
Calin Juravlee460d1d2015-09-29 04:52:17 +01006638void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6639 HUnresolvedInstanceFieldGet* instruction) {
6640 FieldAccessCallingConventionMIPS64 calling_convention;
6641 codegen_->CreateUnresolvedFieldLocationSummary(
6642 instruction, instruction->GetFieldType(), calling_convention);
6643}
6644
6645void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6646 HUnresolvedInstanceFieldGet* instruction) {
6647 FieldAccessCallingConventionMIPS64 calling_convention;
6648 codegen_->GenerateUnresolvedFieldAccess(instruction,
6649 instruction->GetFieldType(),
6650 instruction->GetFieldIndex(),
6651 instruction->GetDexPc(),
6652 calling_convention);
6653}
6654
6655void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6656 HUnresolvedInstanceFieldSet* instruction) {
6657 FieldAccessCallingConventionMIPS64 calling_convention;
6658 codegen_->CreateUnresolvedFieldLocationSummary(
6659 instruction, instruction->GetFieldType(), calling_convention);
6660}
6661
6662void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6663 HUnresolvedInstanceFieldSet* instruction) {
6664 FieldAccessCallingConventionMIPS64 calling_convention;
6665 codegen_->GenerateUnresolvedFieldAccess(instruction,
6666 instruction->GetFieldType(),
6667 instruction->GetFieldIndex(),
6668 instruction->GetDexPc(),
6669 calling_convention);
6670}
6671
6672void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6673 HUnresolvedStaticFieldGet* instruction) {
6674 FieldAccessCallingConventionMIPS64 calling_convention;
6675 codegen_->CreateUnresolvedFieldLocationSummary(
6676 instruction, instruction->GetFieldType(), calling_convention);
6677}
6678
6679void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6680 HUnresolvedStaticFieldGet* instruction) {
6681 FieldAccessCallingConventionMIPS64 calling_convention;
6682 codegen_->GenerateUnresolvedFieldAccess(instruction,
6683 instruction->GetFieldType(),
6684 instruction->GetFieldIndex(),
6685 instruction->GetDexPc(),
6686 calling_convention);
6687}
6688
6689void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6690 HUnresolvedStaticFieldSet* instruction) {
6691 FieldAccessCallingConventionMIPS64 calling_convention;
6692 codegen_->CreateUnresolvedFieldLocationSummary(
6693 instruction, instruction->GetFieldType(), calling_convention);
6694}
6695
6696void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6697 HUnresolvedStaticFieldSet* instruction) {
6698 FieldAccessCallingConventionMIPS64 calling_convention;
6699 codegen_->GenerateUnresolvedFieldAccess(instruction,
6700 instruction->GetFieldType(),
6701 instruction->GetFieldIndex(),
6702 instruction->GetDexPc(),
6703 calling_convention);
6704}
6705
Alexey Frunze4dda3372015-06-01 18:31:49 -07006706void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006707 LocationSummary* locations =
6708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006709 // In suspend check slow path, usually there are no caller-save registers at all.
6710 // If SIMD instructions are present, however, we force spilling all live SIMD
6711 // registers in full width (since the runtime only saves/restores lower part).
6712 locations->SetCustomSlowPathCallerSaves(
6713 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006714}
6715
6716void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6717 HBasicBlock* block = instruction->GetBlock();
6718 if (block->GetLoopInformation() != nullptr) {
6719 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6720 // The back edge will generate the suspend check.
6721 return;
6722 }
6723 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6724 // The goto will generate the suspend check.
6725 return;
6726 }
6727 GenerateSuspendCheck(instruction, nullptr);
6728}
6729
Alexey Frunze4dda3372015-06-01 18:31:49 -07006730void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
6731 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006733 InvokeRuntimeCallingConvention calling_convention;
6734 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6735}
6736
6737void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006738 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006739 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6740}
6741
6742void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6743 Primitive::Type input_type = conversion->GetInputType();
6744 Primitive::Type result_type = conversion->GetResultType();
6745 DCHECK_NE(input_type, result_type);
6746
6747 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6748 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6749 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6750 }
6751
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006752 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
6753
6754 if (Primitive::IsFloatingPointType(input_type)) {
6755 locations->SetInAt(0, Location::RequiresFpuRegister());
6756 } else {
6757 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006758 }
6759
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006760 if (Primitive::IsFloatingPointType(result_type)) {
6761 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006762 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006764 }
6765}
6766
6767void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6768 LocationSummary* locations = conversion->GetLocations();
6769 Primitive::Type result_type = conversion->GetResultType();
6770 Primitive::Type input_type = conversion->GetInputType();
6771
6772 DCHECK_NE(input_type, result_type);
6773
6774 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6775 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6776 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6777
6778 switch (result_type) {
6779 case Primitive::kPrimChar:
6780 __ Andi(dst, src, 0xFFFF);
6781 break;
6782 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006783 if (input_type == Primitive::kPrimLong) {
6784 // Type conversion from long to types narrower than int is a result of code
6785 // transformations. To avoid unpredictable results for SEB and SEH, we first
6786 // need to sign-extend the low 32-bit value into bits 32 through 63.
6787 __ Sll(dst, src, 0);
6788 __ Seb(dst, dst);
6789 } else {
6790 __ Seb(dst, src);
6791 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006792 break;
6793 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006794 if (input_type == Primitive::kPrimLong) {
6795 // Type conversion from long to types narrower than int is a result of code
6796 // transformations. To avoid unpredictable results for SEB and SEH, we first
6797 // need to sign-extend the low 32-bit value into bits 32 through 63.
6798 __ Sll(dst, src, 0);
6799 __ Seh(dst, dst);
6800 } else {
6801 __ Seh(dst, src);
6802 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006803 break;
6804 case Primitive::kPrimInt:
6805 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006806 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6807 // conversions, except when the input and output registers are the same and we are not
6808 // converting longs to shorter types. In these cases, do nothing.
6809 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
6810 __ Sll(dst, src, 0);
6811 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006812 break;
6813
6814 default:
6815 LOG(FATAL) << "Unexpected type conversion from " << input_type
6816 << " to " << result_type;
6817 }
6818 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006819 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6820 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6821 if (input_type == Primitive::kPrimLong) {
6822 __ Dmtc1(src, FTMP);
6823 if (result_type == Primitive::kPrimFloat) {
6824 __ Cvtsl(dst, FTMP);
6825 } else {
6826 __ Cvtdl(dst, FTMP);
6827 }
6828 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006829 __ Mtc1(src, FTMP);
6830 if (result_type == Primitive::kPrimFloat) {
6831 __ Cvtsw(dst, FTMP);
6832 } else {
6833 __ Cvtdw(dst, FTMP);
6834 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006835 }
6836 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6837 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006838 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6839 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006840
6841 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006842 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006843 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006844 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006845 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006846 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006847 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006848 } else {
6849 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006850 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006851 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006852 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006853 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006854 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006855 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006856 } else if (Primitive::IsFloatingPointType(result_type) &&
6857 Primitive::IsFloatingPointType(input_type)) {
6858 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6859 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6860 if (result_type == Primitive::kPrimFloat) {
6861 __ Cvtsd(dst, src);
6862 } else {
6863 __ Cvtds(dst, src);
6864 }
6865 } else {
6866 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6867 << " to " << result_type;
6868 }
6869}
6870
6871void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6872 HandleShift(ushr);
6873}
6874
6875void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6876 HandleShift(ushr);
6877}
6878
6879void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6880 HandleBinaryOp(instruction);
6881}
6882
6883void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6884 HandleBinaryOp(instruction);
6885}
6886
6887void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6888 // Nothing to do, this should be removed during prepare for register allocator.
6889 LOG(FATAL) << "Unreachable";
6890}
6891
6892void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6893 // Nothing to do, this should be removed during prepare for register allocator.
6894 LOG(FATAL) << "Unreachable";
6895}
6896
6897void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006898 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006899}
6900
6901void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006902 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006903}
6904
6905void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006906 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006907}
6908
6909void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006910 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006911}
6912
6913void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006914 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006915}
6916
6917void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006918 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006919}
6920
6921void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006922 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006923}
6924
6925void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006926 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006927}
6928
6929void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006930 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006931}
6932
6933void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006934 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006935}
6936
6937void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006938 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006939}
6940
6941void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006942 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006943}
6944
Aart Bike9f37602015-10-09 11:15:55 -07006945void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006946 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006947}
6948
6949void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006950 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006951}
6952
6953void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006954 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006955}
6956
6957void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006958 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006959}
6960
6961void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006962 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006963}
6964
6965void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006966 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006967}
6968
6969void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006970 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006971}
6972
6973void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006974 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006975}
6976
Mark Mendellfe57faa2015-09-18 09:26:15 -04006977// Simple implementation of packed switch - generate cascaded compare/jumps.
6978void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6979 LocationSummary* locations =
6980 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6981 locations->SetInAt(0, Location::RequiresRegister());
6982}
6983
Alexey Frunze0960ac52016-12-20 17:24:59 -08006984void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6985 int32_t lower_bound,
6986 uint32_t num_entries,
6987 HBasicBlock* switch_block,
6988 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006989 // Create a set of compare/jumps.
6990 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006991 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006992 // Jump to default if index is negative
6993 // Note: We don't check the case that index is positive while value < lower_bound, because in
6994 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6995 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6996
Alexey Frunze0960ac52016-12-20 17:24:59 -08006997 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006998 // Jump to successors[0] if value == lower_bound.
6999 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7000 int32_t last_index = 0;
7001 for (; num_entries - last_index > 2; last_index += 2) {
7002 __ Addiu(temp_reg, temp_reg, -2);
7003 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7004 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7005 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7006 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7007 }
7008 if (num_entries - last_index == 2) {
7009 // The last missing case_value.
7010 __ Addiu(temp_reg, temp_reg, -1);
7011 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007012 }
7013
7014 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007015 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007016 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007017 }
7018}
7019
Alexey Frunze0960ac52016-12-20 17:24:59 -08007020void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7021 int32_t lower_bound,
7022 uint32_t num_entries,
7023 HBasicBlock* switch_block,
7024 HBasicBlock* default_block) {
7025 // Create a jump table.
7026 std::vector<Mips64Label*> labels(num_entries);
7027 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7028 for (uint32_t i = 0; i < num_entries; i++) {
7029 labels[i] = codegen_->GetLabelOf(successors[i]);
7030 }
7031 JumpTable* table = __ CreateJumpTable(std::move(labels));
7032
7033 // Is the value in range?
7034 __ Addiu32(TMP, value_reg, -lower_bound);
7035 __ LoadConst32(AT, num_entries);
7036 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7037
7038 // We are in the range of the table.
7039 // Load the target address from the jump table, indexing by the value.
7040 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007041 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007042 __ Lw(TMP, TMP, 0);
7043 // Compute the absolute target address by adding the table start address
7044 // (the table contains offsets to targets relative to its start).
7045 __ Daddu(TMP, TMP, AT);
7046 // And jump.
7047 __ Jr(TMP);
7048 __ Nop();
7049}
7050
7051void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7052 int32_t lower_bound = switch_instr->GetStartValue();
7053 uint32_t num_entries = switch_instr->GetNumEntries();
7054 LocationSummary* locations = switch_instr->GetLocations();
7055 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7056 HBasicBlock* switch_block = switch_instr->GetBlock();
7057 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7058
7059 if (num_entries > kPackedSwitchJumpTableThreshold) {
7060 GenTableBasedPackedSwitch(value_reg,
7061 lower_bound,
7062 num_entries,
7063 switch_block,
7064 default_block);
7065 } else {
7066 GenPackedSwitchWithCompares(value_reg,
7067 lower_bound,
7068 num_entries,
7069 switch_block,
7070 default_block);
7071 }
7072}
7073
Chris Larsenc9905a62017-03-13 17:06:18 -07007074void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7075 LocationSummary* locations =
7076 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7077 locations->SetInAt(0, Location::RequiresRegister());
7078 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007079}
7080
Chris Larsenc9905a62017-03-13 17:06:18 -07007081void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7082 LocationSummary* locations = instruction->GetLocations();
7083 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7084 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7085 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7086 __ LoadFromOffset(kLoadDoubleword,
7087 locations->Out().AsRegister<GpuRegister>(),
7088 locations->InAt(0).AsRegister<GpuRegister>(),
7089 method_offset);
7090 } else {
7091 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7092 instruction->GetIndex(), kMips64PointerSize));
7093 __ LoadFromOffset(kLoadDoubleword,
7094 locations->Out().AsRegister<GpuRegister>(),
7095 locations->InAt(0).AsRegister<GpuRegister>(),
7096 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7097 __ LoadFromOffset(kLoadDoubleword,
7098 locations->Out().AsRegister<GpuRegister>(),
7099 locations->Out().AsRegister<GpuRegister>(),
7100 method_offset);
7101 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007102}
7103
Alexey Frunze4dda3372015-06-01 18:31:49 -07007104} // namespace mips64
7105} // namespace art