blob: 232241c5ad139d79a3b27e1eebed75bcddc6339b [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 Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080021#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070022#include "entrypoints/quick/quick_entrypoints.h"
23#include "entrypoints/quick/quick_entrypoints_enum.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070026#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "offsets.h"
30#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070032#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070033#include "utils/stack_checks.h"
34
35namespace art {
36namespace mips64 {
37
38static constexpr int kCurrentMethodStackOffset = 0;
39static constexpr GpuRegister kMethodRegisterArgument = A0;
40
Alexey Frunze4dda3372015-06-01 18:31:49 -070041Location Mips64ReturnLocation(Primitive::Type return_type) {
42 switch (return_type) {
43 case Primitive::kPrimBoolean:
44 case Primitive::kPrimByte:
45 case Primitive::kPrimChar:
46 case Primitive::kPrimShort:
47 case Primitive::kPrimInt:
48 case Primitive::kPrimNot:
49 case Primitive::kPrimLong:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimFloat:
53 case Primitive::kPrimDouble:
54 return Location::FpuRegisterLocation(F0);
55
56 case Primitive::kPrimVoid:
57 return Location();
58 }
59 UNREACHABLE();
60}
61
62Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
63 return Mips64ReturnLocation(type);
64}
65
66Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
67 return Location::RegisterLocation(kMethodRegisterArgument);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
71 Location next_location;
72 if (type == Primitive::kPrimVoid) {
73 LOG(FATAL) << "Unexpected parameter type " << type;
74 }
75
76 if (Primitive::IsFloatingPointType(type) &&
77 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
78 next_location = Location::FpuRegisterLocation(
79 calling_convention.GetFpuRegisterAt(float_index_++));
80 gp_index_++;
81 } else if (!Primitive::IsFloatingPointType(type) &&
82 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
83 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
84 float_index_++;
85 } else {
86 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
87 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
88 : Location::StackSlot(stack_offset);
89 }
90
91 // Space on the stack is reserved for all arguments.
92 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
93
Alexey Frunze4dda3372015-06-01 18:31:49 -070094 return next_location;
95}
96
97Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
98 return Mips64ReturnLocation(type);
99}
100
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100101// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
102#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700103#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
105class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100110 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
112 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000113 if (instruction_->CanThrowIntoCatchBlock()) {
114 // Live registers will be restored in the catch block if caught.
115 SaveLiveRegisters(codegen, instruction_->GetLocations());
116 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 // We're moving two locations to locations that could overlap, so we need a parallel
118 // move resolver.
119 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100120 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
122 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
125 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100126 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
127 ? kQuickThrowStringBounds
128 : kQuickThrowArrayBounds;
129 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100130 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700131 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
132 }
133
Alexandre Rames8158f282015-08-07 10:26:17 +0100134 bool IsFatal() const OVERRIDE { return true; }
135
Roland Levillain46648892015-06-19 16:07:18 +0100136 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
137
Alexey Frunze4dda3372015-06-01 18:31:49 -0700138 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700139 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
140};
141
142class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
143 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700144 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
145 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700146
147 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
148 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
149 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100150 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700151 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Roland Levillain46648892015-06-19 16:07:18 +0100156 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
157
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700159 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
160};
161
162class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
163 public:
164 LoadClassSlowPathMIPS64(HLoadClass* cls,
165 HInstruction* at,
166 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700167 bool do_clinit,
168 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr)
169 : SlowPathCodeMIPS64(at),
170 cls_(cls),
171 dex_pc_(dex_pc),
172 do_clinit_(do_clinit),
173 bss_info_high_(bss_info_high) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700174 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
175 }
176
177 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000178 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700179 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700180 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700181 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
182 InvokeRuntimeCallingConvention calling_convention;
183 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
184 const bool is_load_class_bss_entry =
185 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700186 __ Bind(GetEntryLabel());
187 SaveLiveRegisters(codegen, locations);
188
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700189 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
190 GpuRegister entry_address = kNoGpuRegister;
191 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
192 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
193 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
194 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
195 // kSaveEverything call.
196 entry_address = temp_is_a0 ? out.AsRegister<GpuRegister>() : temp;
197 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
198 if (temp_is_a0) {
199 __ Move(entry_address, temp);
200 }
201 }
202
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000203 dex::TypeIndex type_index = cls_->GetTypeIndex();
204 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100205 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
206 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000207 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700208 if (do_clinit_) {
209 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
210 } else {
211 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
212 }
213
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700214 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
215 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
216 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
217 DCHECK(bss_info_high_);
218 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
219 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
220 __ Bind(&info_low->label);
221 __ StoreToOffset(kStoreWord,
222 calling_convention.GetRegisterAt(0),
223 entry_address,
224 /* placeholder */ 0x5678);
225 }
226
Alexey Frunze4dda3372015-06-01 18:31:49 -0700227 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700228 if (out.IsValid()) {
229 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000230 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700231 mips64_codegen->MoveLocation(out,
232 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
233 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700234 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700235 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700236
237 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
238 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
239 // For non-Baker read barriers we need to re-calculate the address of
240 // the class entry.
241 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000242 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700243 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
244 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
245 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
246 __ StoreToOffset(kStoreWord, out.AsRegister<GpuRegister>(), TMP, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000247 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700248 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700249 }
250
Roland Levillain46648892015-06-19 16:07:18 +0100251 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
252
Alexey Frunze4dda3372015-06-01 18:31:49 -0700253 private:
254 // The class this slow path will load.
255 HLoadClass* const cls_;
256
Alexey Frunze4dda3372015-06-01 18:31:49 -0700257 // The dex PC of `at_`.
258 const uint32_t dex_pc_;
259
260 // Whether to initialize the class.
261 const bool do_clinit_;
262
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700263 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
264 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
265
Alexey Frunze4dda3372015-06-01 18:31:49 -0700266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
267};
268
269class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
270 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700271 explicit LoadStringSlowPathMIPS64(HLoadString* instruction,
272 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high)
273 : SlowPathCodeMIPS64(instruction), bss_info_high_(bss_info_high) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700274
275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700276 DCHECK(instruction_->IsLoadString());
277 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700278 LocationSummary* locations = instruction_->GetLocations();
279 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700280 HLoadString* load = instruction_->AsLoadString();
281 const dex::StringIndex string_index = load->GetStringIndex();
282 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700283 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700284 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
285 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700286 __ Bind(GetEntryLabel());
287 SaveLiveRegisters(codegen, locations);
288
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700289 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
290 GpuRegister entry_address = kNoGpuRegister;
291 if (baker_or_no_read_barriers) {
292 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
293 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
294 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
295 // kSaveEverything call.
296 entry_address = temp_is_a0 ? out : temp;
297 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
298 if (temp_is_a0) {
299 __ Move(entry_address, temp);
300 }
301 }
302
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000303 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100304 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700305 instruction_,
306 instruction_->GetDexPc(),
307 this);
308 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700309
310 // Store the resolved string to the BSS entry.
311 if (baker_or_no_read_barriers) {
312 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
313 DCHECK(bss_info_high_);
314 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
315 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(),
316 string_index,
317 bss_info_high_);
318 __ Bind(&info_low->label);
319 __ StoreToOffset(kStoreWord,
320 calling_convention.GetRegisterAt(0),
321 entry_address,
322 /* placeholder */ 0x5678);
323 }
324
Alexey Frunze4dda3372015-06-01 18:31:49 -0700325 Primitive::Type type = instruction_->GetType();
326 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700327 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700329 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800330
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700331 // Store the resolved string to the BSS entry.
332 if (!baker_or_no_read_barriers) {
333 // For non-Baker read barriers we need to re-calculate the address of
334 // the string entry.
335 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
336 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
337 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
338 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index, info_high);
339 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
340 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
341 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700342 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700343 }
344
Roland Levillain46648892015-06-19 16:07:18 +0100345 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
346
Alexey Frunze4dda3372015-06-01 18:31:49 -0700347 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700348 // Pointer to the high half PC-relative patch info.
349 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
350
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
352};
353
354class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
355 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000356 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700357
358 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
359 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
360 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000361 if (instruction_->CanThrowIntoCatchBlock()) {
362 // Live registers will be restored in the catch block if caught.
363 SaveLiveRegisters(codegen, instruction_->GetLocations());
364 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100365 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 instruction_,
367 instruction_->GetDexPc(),
368 this);
369 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
370 }
371
Alexandre Rames8158f282015-08-07 10:26:17 +0100372 bool IsFatal() const OVERRIDE { return true; }
373
Roland Levillain46648892015-06-19 16:07:18 +0100374 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
375
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
378};
379
380class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
381 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100382 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200386 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
388 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200389 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100390 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200392 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700394 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700396 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 }
398 }
399
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700400 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 DCHECK(successor_ == nullptr);
402 return &return_label_;
403 }
404
Roland Levillain46648892015-06-19 16:07:18 +0100405 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
406
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700408 // If not null, the block to branch to after the suspend check.
409 HBasicBlock* const successor_;
410
411 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700412 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413
414 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
415};
416
417class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
418 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800419 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
420 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700421
422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800424
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100425 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700426 DCHECK(instruction_->IsCheckCast()
427 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
428 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
429
430 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800431 if (!is_fatal_) {
432 SaveLiveRegisters(codegen, locations);
433 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700434
435 // We're moving two locations to locations that could overlap, so we need a parallel
436 // move resolver.
437 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800438 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700439 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
440 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800441 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700442 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
443 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700444 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100445 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800446 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700447 Primitive::Type ret_type = instruction_->GetType();
448 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
449 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700450 } else {
451 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800452 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
453 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700454 }
455
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800456 if (!is_fatal_) {
457 RestoreLiveRegisters(codegen, locations);
458 __ Bc(GetExitLabel());
459 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700460 }
461
Roland Levillain46648892015-06-19 16:07:18 +0100462 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
463
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800464 bool IsFatal() const OVERRIDE { return is_fatal_; }
465
Alexey Frunze4dda3372015-06-01 18:31:49 -0700466 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800467 const bool is_fatal_;
468
Alexey Frunze4dda3372015-06-01 18:31:49 -0700469 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
470};
471
472class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
473 public:
Aart Bik42249c32016-01-07 15:33:50 -0800474 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000475 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700476
477 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800478 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700479 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100480 LocationSummary* locations = instruction_->GetLocations();
481 SaveLiveRegisters(codegen, locations);
482 InvokeRuntimeCallingConvention calling_convention;
483 __ LoadConst32(calling_convention.GetRegisterAt(0),
484 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100485 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100486 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700487 }
488
Roland Levillain46648892015-06-19 16:07:18 +0100489 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
490
Alexey Frunze4dda3372015-06-01 18:31:49 -0700491 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700492 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
493};
494
Alexey Frunze15958152017-02-09 19:08:30 -0800495class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
496 public:
497 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
498
499 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
500 LocationSummary* locations = instruction_->GetLocations();
501 __ Bind(GetEntryLabel());
502 SaveLiveRegisters(codegen, locations);
503
504 InvokeRuntimeCallingConvention calling_convention;
505 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
506 parallel_move.AddMove(
507 locations->InAt(0),
508 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
509 Primitive::kPrimNot,
510 nullptr);
511 parallel_move.AddMove(
512 locations->InAt(1),
513 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
514 Primitive::kPrimInt,
515 nullptr);
516 parallel_move.AddMove(
517 locations->InAt(2),
518 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
519 Primitive::kPrimNot,
520 nullptr);
521 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
522
523 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
524 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
525 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
526 RestoreLiveRegisters(codegen, locations);
527 __ Bc(GetExitLabel());
528 }
529
530 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
531
532 private:
533 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
534};
535
536// Slow path marking an object reference `ref` during a read
537// barrier. The field `obj.field` in the object `obj` holding this
538// reference does not get updated by this slow path after marking (see
539// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
540//
541// This means that after the execution of this slow path, `ref` will
542// always be up-to-date, but `obj.field` may not; i.e., after the
543// flip, `ref` will be a to-space reference, but `obj.field` will
544// probably still be a from-space reference (unless it gets updated by
545// another thread, or if another thread installed another object
546// reference (different from `ref`) in `obj.field`).
547//
548// If `entrypoint` is a valid location it is assumed to already be
549// holding the entrypoint. The case where the entrypoint is passed in
550// is for the GcRoot read barrier.
551class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
552 public:
553 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
554 Location ref,
555 Location entrypoint = Location::NoLocation())
556 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
557 DCHECK(kEmitCompilerReadBarrier);
558 }
559
560 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
561
562 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
563 LocationSummary* locations = instruction_->GetLocations();
564 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
565 DCHECK(locations->CanCall());
566 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
567 DCHECK(instruction_->IsInstanceFieldGet() ||
568 instruction_->IsStaticFieldGet() ||
569 instruction_->IsArrayGet() ||
570 instruction_->IsArraySet() ||
571 instruction_->IsLoadClass() ||
572 instruction_->IsLoadString() ||
573 instruction_->IsInstanceOf() ||
574 instruction_->IsCheckCast() ||
575 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
576 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
577 << "Unexpected instruction in read barrier marking slow path: "
578 << instruction_->DebugName();
579
580 __ Bind(GetEntryLabel());
581 // No need to save live registers; it's taken care of by the
582 // entrypoint. Also, there is no need to update the stack mask,
583 // as this runtime call will not trigger a garbage collection.
584 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
585 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
586 (S2 <= ref_reg && ref_reg <= S7) ||
587 (ref_reg == S8)) << ref_reg;
588 // "Compact" slow path, saving two moves.
589 //
590 // Instead of using the standard runtime calling convention (input
591 // and output in A0 and V0 respectively):
592 //
593 // A0 <- ref
594 // V0 <- ReadBarrierMark(A0)
595 // ref <- V0
596 //
597 // we just use rX (the register containing `ref`) as input and output
598 // of a dedicated entrypoint:
599 //
600 // rX <- ReadBarrierMarkRegX(rX)
601 //
602 if (entrypoint_.IsValid()) {
603 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
604 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
605 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
606 __ Nop();
607 } else {
608 int32_t entry_point_offset =
609 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
610 // This runtime call does not require a stack map.
611 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
612 instruction_,
613 this);
614 }
615 __ Bc(GetExitLabel());
616 }
617
618 private:
619 // The location (register) of the marked object reference.
620 const Location ref_;
621
622 // The location of the entrypoint if already loaded.
623 const Location entrypoint_;
624
625 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
626};
627
628// Slow path marking an object reference `ref` during a read barrier,
629// and if needed, atomically updating the field `obj.field` in the
630// object `obj` holding this reference after marking (contrary to
631// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
632// `obj.field`).
633//
634// This means that after the execution of this slow path, both `ref`
635// and `obj.field` will be up-to-date; i.e., after the flip, both will
636// hold the same to-space reference (unless another thread installed
637// another object reference (different from `ref`) in `obj.field`).
638class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
639 public:
640 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
641 Location ref,
642 GpuRegister obj,
643 Location field_offset,
644 GpuRegister temp1)
645 : SlowPathCodeMIPS64(instruction),
646 ref_(ref),
647 obj_(obj),
648 field_offset_(field_offset),
649 temp1_(temp1) {
650 DCHECK(kEmitCompilerReadBarrier);
651 }
652
653 const char* GetDescription() const OVERRIDE {
654 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
655 }
656
657 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
658 LocationSummary* locations = instruction_->GetLocations();
659 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
660 DCHECK(locations->CanCall());
661 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
662 // This slow path is only used by the UnsafeCASObject intrinsic.
663 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
664 << "Unexpected instruction in read barrier marking and field updating slow path: "
665 << instruction_->DebugName();
666 DCHECK(instruction_->GetLocations()->Intrinsified());
667 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
668 DCHECK(field_offset_.IsRegister()) << field_offset_;
669
670 __ Bind(GetEntryLabel());
671
672 // Save the old reference.
673 // Note that we cannot use AT or TMP to save the old reference, as those
674 // are used by the code that follows, but we need the old reference after
675 // the call to the ReadBarrierMarkRegX entry point.
676 DCHECK_NE(temp1_, AT);
677 DCHECK_NE(temp1_, TMP);
678 __ Move(temp1_, ref_reg);
679
680 // No need to save live registers; it's taken care of by the
681 // entrypoint. Also, there is no need to update the stack mask,
682 // as this runtime call will not trigger a garbage collection.
683 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
684 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
685 (S2 <= ref_reg && ref_reg <= S7) ||
686 (ref_reg == S8)) << ref_reg;
687 // "Compact" slow path, saving two moves.
688 //
689 // Instead of using the standard runtime calling convention (input
690 // and output in A0 and V0 respectively):
691 //
692 // A0 <- ref
693 // V0 <- ReadBarrierMark(A0)
694 // ref <- V0
695 //
696 // we just use rX (the register containing `ref`) as input and output
697 // of a dedicated entrypoint:
698 //
699 // rX <- ReadBarrierMarkRegX(rX)
700 //
701 int32_t entry_point_offset =
702 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
703 // This runtime call does not require a stack map.
704 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
705 instruction_,
706 this);
707
708 // If the new reference is different from the old reference,
709 // update the field in the holder (`*(obj_ + field_offset_)`).
710 //
711 // Note that this field could also hold a different object, if
712 // another thread had concurrently changed it. In that case, the
713 // the compare-and-set (CAS) loop below would abort, leaving the
714 // field as-is.
715 Mips64Label done;
716 __ Beqc(temp1_, ref_reg, &done);
717
718 // Update the the holder's field atomically. This may fail if
719 // mutator updates before us, but it's OK. This is achieved
720 // using a strong compare-and-set (CAS) operation with relaxed
721 // memory synchronization ordering, where the expected value is
722 // the old reference and the desired value is the new reference.
723
724 // Convenience aliases.
725 GpuRegister base = obj_;
726 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
727 GpuRegister expected = temp1_;
728 GpuRegister value = ref_reg;
729 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
730 GpuRegister tmp = AT; // Value in memory.
731
732 __ Daddu(tmp_ptr, base, offset);
733
734 if (kPoisonHeapReferences) {
735 __ PoisonHeapReference(expected);
736 // Do not poison `value` if it is the same register as
737 // `expected`, which has just been poisoned.
738 if (value != expected) {
739 __ PoisonHeapReference(value);
740 }
741 }
742
743 // do {
744 // tmp = [r_ptr] - expected;
745 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
746
747 Mips64Label loop_head, exit_loop;
748 __ Bind(&loop_head);
749 __ Ll(tmp, tmp_ptr);
750 // The LL instruction sign-extends the 32-bit value, but
751 // 32-bit references must be zero-extended. Zero-extend `tmp`.
752 __ Dext(tmp, tmp, 0, 32);
753 __ Bnec(tmp, expected, &exit_loop);
754 __ Move(tmp, value);
755 __ Sc(tmp, tmp_ptr);
756 __ Beqzc(tmp, &loop_head);
757 __ Bind(&exit_loop);
758
759 if (kPoisonHeapReferences) {
760 __ UnpoisonHeapReference(expected);
761 // Do not unpoison `value` if it is the same register as
762 // `expected`, which has just been unpoisoned.
763 if (value != expected) {
764 __ UnpoisonHeapReference(value);
765 }
766 }
767
768 __ Bind(&done);
769 __ Bc(GetExitLabel());
770 }
771
772 private:
773 // The location (register) of the marked object reference.
774 const Location ref_;
775 // The register containing the object holding the marked object reference field.
776 const GpuRegister obj_;
777 // The location of the offset of the marked reference field within `obj_`.
778 Location field_offset_;
779
780 const GpuRegister temp1_;
781
782 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
783};
784
785// Slow path generating a read barrier for a heap reference.
786class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
787 public:
788 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
789 Location out,
790 Location ref,
791 Location obj,
792 uint32_t offset,
793 Location index)
794 : SlowPathCodeMIPS64(instruction),
795 out_(out),
796 ref_(ref),
797 obj_(obj),
798 offset_(offset),
799 index_(index) {
800 DCHECK(kEmitCompilerReadBarrier);
801 // If `obj` is equal to `out` or `ref`, it means the initial object
802 // has been overwritten by (or after) the heap object reference load
803 // to be instrumented, e.g.:
804 //
805 // __ LoadFromOffset(kLoadWord, out, out, offset);
806 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
807 //
808 // In that case, we have lost the information about the original
809 // object, and the emitted read barrier cannot work properly.
810 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
811 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
812 }
813
814 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
815 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
816 LocationSummary* locations = instruction_->GetLocations();
817 Primitive::Type type = Primitive::kPrimNot;
818 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
819 DCHECK(locations->CanCall());
820 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
821 DCHECK(instruction_->IsInstanceFieldGet() ||
822 instruction_->IsStaticFieldGet() ||
823 instruction_->IsArrayGet() ||
824 instruction_->IsInstanceOf() ||
825 instruction_->IsCheckCast() ||
826 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
827 << "Unexpected instruction in read barrier for heap reference slow path: "
828 << instruction_->DebugName();
829
830 __ Bind(GetEntryLabel());
831 SaveLiveRegisters(codegen, locations);
832
833 // We may have to change the index's value, but as `index_` is a
834 // constant member (like other "inputs" of this slow path),
835 // introduce a copy of it, `index`.
836 Location index = index_;
837 if (index_.IsValid()) {
838 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
839 if (instruction_->IsArrayGet()) {
840 // Compute the actual memory offset and store it in `index`.
841 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
842 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
843 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
844 // We are about to change the value of `index_reg` (see the
845 // calls to art::mips64::Mips64Assembler::Sll and
846 // art::mips64::MipsAssembler::Addiu32 below), but it has
847 // not been saved by the previous call to
848 // art::SlowPathCode::SaveLiveRegisters, as it is a
849 // callee-save register --
850 // art::SlowPathCode::SaveLiveRegisters does not consider
851 // callee-save registers, as it has been designed with the
852 // assumption that callee-save registers are supposed to be
853 // handled by the called function. So, as a callee-save
854 // register, `index_reg` _would_ eventually be saved onto
855 // the stack, but it would be too late: we would have
856 // changed its value earlier. Therefore, we manually save
857 // it here into another freely available register,
858 // `free_reg`, chosen of course among the caller-save
859 // registers (as a callee-save `free_reg` register would
860 // exhibit the same problem).
861 //
862 // Note we could have requested a temporary register from
863 // the register allocator instead; but we prefer not to, as
864 // this is a slow path, and we know we can find a
865 // caller-save register that is available.
866 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
867 __ Move(free_reg, index_reg);
868 index_reg = free_reg;
869 index = Location::RegisterLocation(index_reg);
870 } else {
871 // The initial register stored in `index_` has already been
872 // saved in the call to art::SlowPathCode::SaveLiveRegisters
873 // (as it is not a callee-save register), so we can freely
874 // use it.
875 }
876 // Shifting the index value contained in `index_reg` by the scale
877 // factor (2) cannot overflow in practice, as the runtime is
878 // unable to allocate object arrays with a size larger than
879 // 2^26 - 1 (that is, 2^28 - 4 bytes).
880 __ Sll(index_reg, index_reg, TIMES_4);
881 static_assert(
882 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
883 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
884 __ Addiu32(index_reg, index_reg, offset_);
885 } else {
886 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
887 // intrinsics, `index_` is not shifted by a scale factor of 2
888 // (as in the case of ArrayGet), as it is actually an offset
889 // to an object field within an object.
890 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
891 DCHECK(instruction_->GetLocations()->Intrinsified());
892 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
893 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
894 << instruction_->AsInvoke()->GetIntrinsic();
895 DCHECK_EQ(offset_, 0U);
896 DCHECK(index_.IsRegister());
897 }
898 }
899
900 // We're moving two or three locations to locations that could
901 // overlap, so we need a parallel move resolver.
902 InvokeRuntimeCallingConvention calling_convention;
903 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
904 parallel_move.AddMove(ref_,
905 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
906 Primitive::kPrimNot,
907 nullptr);
908 parallel_move.AddMove(obj_,
909 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
910 Primitive::kPrimNot,
911 nullptr);
912 if (index.IsValid()) {
913 parallel_move.AddMove(index,
914 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
915 Primitive::kPrimInt,
916 nullptr);
917 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
918 } else {
919 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
920 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
921 }
922 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
923 instruction_,
924 instruction_->GetDexPc(),
925 this);
926 CheckEntrypointTypes<
927 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
928 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
929
930 RestoreLiveRegisters(codegen, locations);
931 __ Bc(GetExitLabel());
932 }
933
934 const char* GetDescription() const OVERRIDE {
935 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
936 }
937
938 private:
939 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
940 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
941 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
942 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
943 if (i != ref &&
944 i != obj &&
945 !codegen->IsCoreCalleeSaveRegister(i) &&
946 !codegen->IsBlockedCoreRegister(i)) {
947 return static_cast<GpuRegister>(i);
948 }
949 }
950 // We shall never fail to find a free caller-save register, as
951 // there are more than two core caller-save registers on MIPS64
952 // (meaning it is possible to find one which is different from
953 // `ref` and `obj`).
954 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
955 LOG(FATAL) << "Could not find a free caller-save register";
956 UNREACHABLE();
957 }
958
959 const Location out_;
960 const Location ref_;
961 const Location obj_;
962 const uint32_t offset_;
963 // An additional location containing an index to an array.
964 // Only used for HArrayGet and the UnsafeGetObject &
965 // UnsafeGetObjectVolatile intrinsics.
966 const Location index_;
967
968 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
969};
970
971// Slow path generating a read barrier for a GC root.
972class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
973 public:
974 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
975 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
976 DCHECK(kEmitCompilerReadBarrier);
977 }
978
979 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
980 LocationSummary* locations = instruction_->GetLocations();
981 Primitive::Type type = Primitive::kPrimNot;
982 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
983 DCHECK(locations->CanCall());
984 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
985 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
986 << "Unexpected instruction in read barrier for GC root slow path: "
987 << instruction_->DebugName();
988
989 __ Bind(GetEntryLabel());
990 SaveLiveRegisters(codegen, locations);
991
992 InvokeRuntimeCallingConvention calling_convention;
993 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
994 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
995 root_,
996 Primitive::kPrimNot);
997 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
998 instruction_,
999 instruction_->GetDexPc(),
1000 this);
1001 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1002 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1003
1004 RestoreLiveRegisters(codegen, locations);
1005 __ Bc(GetExitLabel());
1006 }
1007
1008 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
1009
1010 private:
1011 const Location out_;
1012 const Location root_;
1013
1014 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
1015};
1016
Alexey Frunze4dda3372015-06-01 18:31:49 -07001017CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
1018 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001019 const CompilerOptions& compiler_options,
1020 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001021 : CodeGenerator(graph,
1022 kNumberOfGpuRegisters,
1023 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001024 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1026 arraysize(kCoreCalleeSaves)),
1027 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1028 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001029 compiler_options,
1030 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001031 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032 location_builder_(graph, this),
1033 instruction_visitor_(graph, this),
1034 move_resolver_(graph->GetArena(), this),
Goran Jakovljevic19680d32017-05-11 10:38:36 +02001035 assembler_(graph->GetArena(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001036 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001037 uint32_literals_(std::less<uint32_t>(),
1038 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001039 uint64_literals_(std::less<uint64_t>(),
1040 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001041 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001042 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001043 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001044 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001045 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001046 jit_string_patches_(StringReferenceValueComparator(),
1047 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1048 jit_class_patches_(TypeReferenceValueComparator(),
1049 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001050 // Save RA (containing the return address) to mimic Quick.
1051 AddAllocatedRegister(Location::RegisterLocation(RA));
1052}
1053
1054#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001055// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1056#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001057#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -07001058
1059void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001060 // Ensure that we fix up branches.
1061 __ FinalizeCode();
1062
1063 // Adjust native pc offsets in stack maps.
1064 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001065 uint32_t old_position =
1066 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001067 uint32_t new_position = __ GetAdjustedPosition(old_position);
1068 DCHECK_GE(new_position, old_position);
1069 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1070 }
1071
1072 // Adjust pc offsets for the disassembly information.
1073 if (disasm_info_ != nullptr) {
1074 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1075 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1076 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1077 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1078 it.second.start = __ GetAdjustedPosition(it.second.start);
1079 it.second.end = __ GetAdjustedPosition(it.second.end);
1080 }
1081 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1082 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1083 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1084 }
1085 }
1086
Alexey Frunze4dda3372015-06-01 18:31:49 -07001087 CodeGenerator::Finalize(allocator);
1088}
1089
1090Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1091 return codegen_->GetAssembler();
1092}
1093
1094void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001095 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001096 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1097}
1098
1099void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001100 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001101 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1102}
1103
1104void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1105 // Pop reg
1106 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001107 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001108}
1109
1110void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1111 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001112 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001113 __ Sd(GpuRegister(reg), SP, 0);
1114}
1115
1116void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1117 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1118 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1119 // Allocate a scratch register other than TMP, if available.
1120 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1121 // automatically unspilled when the scratch scope object is destroyed).
1122 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1123 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001124 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001125 __ LoadFromOffset(load_type,
1126 GpuRegister(ensure_scratch.GetRegister()),
1127 SP,
1128 index1 + stack_offset);
1129 __ LoadFromOffset(load_type,
1130 TMP,
1131 SP,
1132 index2 + stack_offset);
1133 __ StoreToOffset(store_type,
1134 GpuRegister(ensure_scratch.GetRegister()),
1135 SP,
1136 index2 + stack_offset);
1137 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1138}
1139
1140static dwarf::Reg DWARFReg(GpuRegister reg) {
1141 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1142}
1143
David Srbeckyba702002016-02-01 18:15:29 +00001144static dwarf::Reg DWARFReg(FpuRegister reg) {
1145 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1146}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001147
1148void CodeGeneratorMIPS64::GenerateFrameEntry() {
1149 __ Bind(&frame_entry_label_);
1150
1151 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1152
1153 if (do_overflow_check) {
1154 __ LoadFromOffset(kLoadWord,
1155 ZERO,
1156 SP,
1157 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1158 RecordPcInfo(nullptr, 0);
1159 }
1160
Alexey Frunze4dda3372015-06-01 18:31:49 -07001161 if (HasEmptyFrame()) {
1162 return;
1163 }
1164
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001165 // Make sure the frame size isn't unreasonably large.
1166 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1167 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1168 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001169
1170 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001171
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001172 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001173 __ IncreaseFrameSize(ofs);
1174
1175 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1176 GpuRegister reg = kCoreCalleeSaves[i];
1177 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001178 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001179 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001180 __ cfi().RelOffset(DWARFReg(reg), ofs);
1181 }
1182 }
1183
1184 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1185 FpuRegister reg = kFpuCalleeSaves[i];
1186 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001187 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001188 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001189 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001190 }
1191 }
1192
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001193 // Save the current method if we need it. Note that we do not
1194 // do this in HCurrentMethod, as the instruction might have been removed
1195 // in the SSA graph.
1196 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001197 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001198 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001199
1200 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1201 // Initialize should_deoptimize flag to 0.
1202 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1203 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001204}
1205
1206void CodeGeneratorMIPS64::GenerateFrameExit() {
1207 __ cfi().RememberState();
1208
Alexey Frunze4dda3372015-06-01 18:31:49 -07001209 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001210 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001211
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001212 // For better instruction scheduling restore RA before other registers.
1213 uint32_t ofs = GetFrameSize();
1214 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 GpuRegister reg = kCoreCalleeSaves[i];
1216 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001217 ofs -= kMips64DoublewordSize;
1218 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 __ cfi().Restore(DWARFReg(reg));
1220 }
1221 }
1222
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001223 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1224 FpuRegister reg = kFpuCalleeSaves[i];
1225 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1226 ofs -= kMips64DoublewordSize;
1227 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1228 __ cfi().Restore(DWARFReg(reg));
1229 }
1230 }
1231
1232 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001233 }
1234
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001235 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001236
1237 __ cfi().RestoreState();
1238 __ cfi().DefCFAOffset(GetFrameSize());
1239}
1240
1241void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1242 __ Bind(GetLabelOf(block));
1243}
1244
1245void CodeGeneratorMIPS64::MoveLocation(Location destination,
1246 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001248 if (source.Equals(destination)) {
1249 return;
1250 }
1251
1252 // A valid move can always be inferred from the destination and source
1253 // locations. When moving from and to a register, the argument type can be
1254 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001256 DCHECK_EQ(unspecified_type, false);
1257
1258 if (destination.IsRegister() || destination.IsFpuRegister()) {
1259 if (unspecified_type) {
1260 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1261 if (source.IsStackSlot() ||
1262 (src_cst != nullptr && (src_cst->IsIntConstant()
1263 || src_cst->IsFloatConstant()
1264 || src_cst->IsNullConstant()))) {
1265 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001267 } else {
1268 // If the source is a double stack slot or a 64bit constant, a 64bit
1269 // type is appropriate. Else the source is a register, and since the
1270 // type has not been specified, we chose a 64bit type to force a 64bit
1271 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273 }
1274 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1276 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001277 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1278 // Move to GPR/FPR from stack
1279 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001280 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001281 __ LoadFpuFromOffset(load_type,
1282 destination.AsFpuRegister<FpuRegister>(),
1283 SP,
1284 source.GetStackIndex());
1285 } else {
1286 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1287 __ LoadFromOffset(load_type,
1288 destination.AsRegister<GpuRegister>(),
1289 SP,
1290 source.GetStackIndex());
1291 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001292 } else if (source.IsSIMDStackSlot()) {
1293 __ LoadFpuFromOffset(kLoadQuadword,
1294 destination.AsFpuRegister<FpuRegister>(),
1295 SP,
1296 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 } else if (source.IsConstant()) {
1298 // Move to GPR/FPR from constant
1299 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001300 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001301 gpr = destination.AsRegister<GpuRegister>();
1302 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001303 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001304 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1305 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1306 gpr = ZERO;
1307 } else {
1308 __ LoadConst32(gpr, value);
1309 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001310 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001311 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1312 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1313 gpr = ZERO;
1314 } else {
1315 __ LoadConst64(gpr, value);
1316 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001317 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001318 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001320 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001321 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1322 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001323 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001324 if (destination.IsRegister()) {
1325 // Move to GPR from GPR
1326 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1327 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001328 DCHECK(destination.IsFpuRegister());
1329 if (Primitive::Is64BitType(dst_type)) {
1330 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1331 } else {
1332 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1333 }
1334 }
1335 } else if (source.IsFpuRegister()) {
1336 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001337 if (GetGraph()->HasSIMD()) {
1338 __ MoveV(VectorRegisterFrom(destination),
1339 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001340 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001341 // Move to FPR from FPR
1342 if (dst_type == Primitive::kPrimFloat) {
1343 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1344 } else {
1345 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
1346 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1347 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001348 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001349 } else {
1350 DCHECK(destination.IsRegister());
1351 if (Primitive::Is64BitType(dst_type)) {
1352 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1353 } else {
1354 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001356 }
1357 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001358 } else if (destination.IsSIMDStackSlot()) {
1359 if (source.IsFpuRegister()) {
1360 __ StoreFpuToOffset(kStoreQuadword,
1361 source.AsFpuRegister<FpuRegister>(),
1362 SP,
1363 destination.GetStackIndex());
1364 } else {
1365 DCHECK(source.IsSIMDStackSlot());
1366 __ LoadFpuFromOffset(kLoadQuadword,
1367 FTMP,
1368 SP,
1369 source.GetStackIndex());
1370 __ StoreFpuToOffset(kStoreQuadword,
1371 FTMP,
1372 SP,
1373 destination.GetStackIndex());
1374 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001375 } else { // The destination is not a register. It must be a stack slot.
1376 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1377 if (source.IsRegister() || source.IsFpuRegister()) {
1378 if (unspecified_type) {
1379 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001380 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001381 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001382 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001383 }
1384 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001385 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1386 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001387 // Move to stack from GPR/FPR
1388 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1389 if (source.IsRegister()) {
1390 __ StoreToOffset(store_type,
1391 source.AsRegister<GpuRegister>(),
1392 SP,
1393 destination.GetStackIndex());
1394 } else {
1395 __ StoreFpuToOffset(store_type,
1396 source.AsFpuRegister<FpuRegister>(),
1397 SP,
1398 destination.GetStackIndex());
1399 }
1400 } else if (source.IsConstant()) {
1401 // Move to stack from constant
1402 HConstant* src_cst = source.GetConstant();
1403 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001404 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001405 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001406 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1407 if (value != 0) {
1408 gpr = TMP;
1409 __ LoadConst32(gpr, value);
1410 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001411 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001412 DCHECK(destination.IsDoubleStackSlot());
1413 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1414 if (value != 0) {
1415 gpr = TMP;
1416 __ LoadConst64(gpr, value);
1417 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001418 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001419 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001420 } else {
1421 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1422 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1423 // Move to stack from stack
1424 if (destination.IsStackSlot()) {
1425 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1426 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1427 } else {
1428 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1429 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1430 }
1431 }
1432 }
1433}
1434
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001435void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001436 DCHECK(!loc1.IsConstant());
1437 DCHECK(!loc2.IsConstant());
1438
1439 if (loc1.Equals(loc2)) {
1440 return;
1441 }
1442
1443 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1444 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1445 bool is_fp_reg1 = loc1.IsFpuRegister();
1446 bool is_fp_reg2 = loc2.IsFpuRegister();
1447
1448 if (loc2.IsRegister() && loc1.IsRegister()) {
1449 // Swap 2 GPRs
1450 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1451 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1452 __ Move(TMP, r2);
1453 __ Move(r2, r1);
1454 __ Move(r1, TMP);
1455 } else if (is_fp_reg2 && is_fp_reg1) {
1456 // Swap 2 FPRs
1457 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1458 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001459 if (type == Primitive::kPrimFloat) {
1460 __ MovS(FTMP, r1);
1461 __ MovS(r1, r2);
1462 __ MovS(r2, FTMP);
1463 } else {
1464 DCHECK_EQ(type, Primitive::kPrimDouble);
1465 __ MovD(FTMP, r1);
1466 __ MovD(r1, r2);
1467 __ MovD(r2, FTMP);
1468 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001469 } else if (is_slot1 != is_slot2) {
1470 // Swap GPR/FPR and stack slot
1471 Location reg_loc = is_slot1 ? loc2 : loc1;
1472 Location mem_loc = is_slot1 ? loc1 : loc2;
1473 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1474 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1475 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1476 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1477 if (reg_loc.IsFpuRegister()) {
1478 __ StoreFpuToOffset(store_type,
1479 reg_loc.AsFpuRegister<FpuRegister>(),
1480 SP,
1481 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001482 if (mem_loc.IsStackSlot()) {
1483 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1484 } else {
1485 DCHECK(mem_loc.IsDoubleStackSlot());
1486 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1487 }
1488 } else {
1489 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1490 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1491 }
1492 } else if (is_slot1 && is_slot2) {
1493 move_resolver_.Exchange(loc1.GetStackIndex(),
1494 loc2.GetStackIndex(),
1495 loc1.IsDoubleStackSlot());
1496 } else {
1497 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1498 }
1499}
1500
Calin Juravle175dc732015-08-25 15:42:32 +01001501void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1502 DCHECK(location.IsRegister());
1503 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1504}
1505
Calin Juravlee460d1d2015-09-29 04:52:17 +01001506void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1507 if (location.IsRegister()) {
1508 locations->AddTemp(location);
1509 } else {
1510 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1511 }
1512}
1513
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001514void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1515 GpuRegister value,
1516 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001517 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001518 GpuRegister card = AT;
1519 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001520 if (value_can_be_null) {
1521 __ Beqzc(value, &done);
1522 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001523 __ LoadFromOffset(kLoadDoubleword,
1524 card,
1525 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001526 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001527 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1528 __ Daddu(temp, card, temp);
1529 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001530 if (value_can_be_null) {
1531 __ Bind(&done);
1532 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001533}
1534
Alexey Frunze19f6c692016-11-30 19:19:55 -08001535template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1536inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1537 const ArenaDeque<PcRelativePatchInfo>& infos,
1538 ArenaVector<LinkerPatch>* linker_patches) {
1539 for (const PcRelativePatchInfo& info : infos) {
1540 const DexFile& dex_file = info.target_dex_file;
1541 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001542 DCHECK(info.label.IsBound());
1543 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1544 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1545 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1546 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001547 }
1548}
1549
1550void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1551 DCHECK(linker_patches->empty());
1552 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001553 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001554 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001555 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001556 type_bss_entry_patches_.size() +
1557 pc_relative_string_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001558 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001559 if (GetCompilerOptions().IsBootImage()) {
1560 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Alexey Frunzef63f5692016-12-13 17:43:11 -08001561 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001562 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1563 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001564 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1565 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001566 } else {
1567 DCHECK(pc_relative_method_patches_.empty());
1568 DCHECK(pc_relative_type_patches_.empty());
1569 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1570 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001571 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001572 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1573 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001574 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1575 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001576 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001577}
1578
Vladimir Marko65979462017-05-19 17:25:12 +01001579CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001580 MethodReference target_method,
1581 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001582 return NewPcRelativePatch(*target_method.dex_file,
1583 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001584 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001585 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001586}
1587
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001588CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001589 MethodReference target_method,
1590 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001591 return NewPcRelativePatch(*target_method.dex_file,
1592 target_method.dex_method_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001593 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001594 &method_bss_entry_patches_);
1595}
1596
Alexey Frunzef63f5692016-12-13 17:43:11 -08001597CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001598 const DexFile& dex_file,
1599 dex::TypeIndex type_index,
1600 const PcRelativePatchInfo* info_high) {
1601 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001602}
1603
Vladimir Marko1998cd02017-01-13 13:02:58 +00001604CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001605 const DexFile& dex_file,
1606 dex::TypeIndex type_index,
1607 const PcRelativePatchInfo* info_high) {
1608 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001609}
1610
Vladimir Marko65979462017-05-19 17:25:12 +01001611CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001612 const DexFile& dex_file,
1613 dex::StringIndex string_index,
1614 const PcRelativePatchInfo* info_high) {
1615 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001616}
1617
Alexey Frunze19f6c692016-11-30 19:19:55 -08001618CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001619 const DexFile& dex_file,
1620 uint32_t offset_or_index,
1621 const PcRelativePatchInfo* info_high,
1622 ArenaDeque<PcRelativePatchInfo>* patches) {
1623 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001624 return &patches->back();
1625}
1626
Alexey Frunzef63f5692016-12-13 17:43:11 -08001627Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1628 return map->GetOrCreate(
1629 value,
1630 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1631}
1632
Alexey Frunze19f6c692016-11-30 19:19:55 -08001633Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1634 return uint64_literals_.GetOrCreate(
1635 value,
1636 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1637}
1638
Alexey Frunzef63f5692016-12-13 17:43:11 -08001639Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001640 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001641}
1642
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001643void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1644 GpuRegister out,
1645 PcRelativePatchInfo* info_low) {
1646 DCHECK(!info_high->patch_info_high);
1647 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001648 // Add the high half of a 32-bit offset to PC.
1649 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001650 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001651 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001652 DCHECK_EQ(info_low->patch_info_high, info_high);
1653 __ Bind(&info_low->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001654}
1655
Alexey Frunze627c1a02017-01-30 19:28:14 -08001656Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1657 dex::StringIndex string_index,
1658 Handle<mirror::String> handle) {
1659 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1660 reinterpret_cast64<uint64_t>(handle.GetReference()));
1661 return jit_string_patches_.GetOrCreate(
1662 StringReference(&dex_file, string_index),
1663 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1664}
1665
1666Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1667 dex::TypeIndex type_index,
1668 Handle<mirror::Class> handle) {
1669 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1670 reinterpret_cast64<uint64_t>(handle.GetReference()));
1671 return jit_class_patches_.GetOrCreate(
1672 TypeReference(&dex_file, type_index),
1673 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1674}
1675
1676void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1677 const uint8_t* roots_data,
1678 const Literal* literal,
1679 uint64_t index_in_table) const {
1680 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1681 uintptr_t address =
1682 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1683 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1684}
1685
1686void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1687 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001688 const StringReference& string_reference = entry.first;
1689 Literal* table_entry_literal = entry.second;
1690 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001691 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001692 uint64_t index_in_table = it->second;
1693 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001694 }
1695 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001696 const TypeReference& type_reference = entry.first;
1697 Literal* table_entry_literal = entry.second;
1698 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001699 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001700 uint64_t index_in_table = it->second;
1701 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001702 }
1703}
1704
David Brazdil58282f42016-01-14 12:45:10 +00001705void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1707 blocked_core_registers_[ZERO] = true;
1708 blocked_core_registers_[K0] = true;
1709 blocked_core_registers_[K1] = true;
1710 blocked_core_registers_[GP] = true;
1711 blocked_core_registers_[SP] = true;
1712 blocked_core_registers_[RA] = true;
1713
Lazar Trsicd9672662015-09-03 17:33:01 +02001714 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1715 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001716 blocked_core_registers_[AT] = true;
1717 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001718 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001719 blocked_fpu_registers_[FTMP] = true;
1720
1721 // Reserve suspend and thread registers.
1722 blocked_core_registers_[S0] = true;
1723 blocked_core_registers_[TR] = true;
1724
1725 // Reserve T9 for function calls
1726 blocked_core_registers_[T9] = true;
1727
Goran Jakovljevic782be112016-06-21 12:39:04 +02001728 if (GetGraph()->IsDebuggable()) {
1729 // Stubs do not save callee-save floating point registers. If the graph
1730 // is debuggable, we need to deal with these registers differently. For
1731 // now, just block them.
1732 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1733 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1734 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001735 }
1736}
1737
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1739 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001740 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001741}
1742
1743size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1744 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001745 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001746}
1747
1748size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001749 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1750 FpuRegister(reg_id),
1751 SP,
1752 stack_index);
1753 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001754}
1755
1756size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001757 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1758 FpuRegister(reg_id),
1759 SP,
1760 stack_index);
1761 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762}
1763
1764void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001765 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766}
1767
1768void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001769 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770}
1771
Calin Juravle175dc732015-08-25 15:42:32 +01001772void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001773 HInstruction* instruction,
1774 uint32_t dex_pc,
1775 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001776 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001777 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001778 if (EntrypointRequiresStackMap(entrypoint)) {
1779 RecordPcInfo(instruction, dex_pc, slow_path);
1780 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001781}
1782
Alexey Frunze15958152017-02-09 19:08:30 -08001783void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1784 HInstruction* instruction,
1785 SlowPathCode* slow_path) {
1786 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1787 GenerateInvokeRuntime(entry_point_offset);
1788}
1789
1790void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1791 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1792 __ Jalr(T9);
1793 __ Nop();
1794}
1795
Alexey Frunze4dda3372015-06-01 18:31:49 -07001796void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1797 GpuRegister class_reg) {
1798 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1799 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1800 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001801 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1802 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803 __ Bind(slow_path->GetExitLabel());
1804}
1805
1806void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1807 __ Sync(0); // only stype 0 is supported
1808}
1809
1810void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1811 HBasicBlock* successor) {
1812 SuspendCheckSlowPathMIPS64* slow_path =
1813 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1814 codegen_->AddSlowPath(slow_path);
1815
1816 __ LoadFromOffset(kLoadUnsignedHalfword,
1817 TMP,
1818 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001819 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001820 if (successor == nullptr) {
1821 __ Bnezc(TMP, slow_path->GetEntryLabel());
1822 __ Bind(slow_path->GetReturnLabel());
1823 } else {
1824 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001825 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001826 // slow_path will return to GetLabelOf(successor).
1827 }
1828}
1829
1830InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1831 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001832 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833 assembler_(codegen->GetAssembler()),
1834 codegen_(codegen) {}
1835
1836void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1837 DCHECK_EQ(instruction->InputCount(), 2U);
1838 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1839 Primitive::Type type = instruction->GetResultType();
1840 switch (type) {
1841 case Primitive::kPrimInt:
1842 case Primitive::kPrimLong: {
1843 locations->SetInAt(0, Location::RequiresRegister());
1844 HInstruction* right = instruction->InputAt(1);
1845 bool can_use_imm = false;
1846 if (right->IsConstant()) {
1847 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1848 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1849 can_use_imm = IsUint<16>(imm);
1850 } else if (instruction->IsAdd()) {
1851 can_use_imm = IsInt<16>(imm);
1852 } else {
1853 DCHECK(instruction->IsSub());
1854 can_use_imm = IsInt<16>(-imm);
1855 }
1856 }
1857 if (can_use_imm)
1858 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1859 else
1860 locations->SetInAt(1, Location::RequiresRegister());
1861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1862 }
1863 break;
1864
1865 case Primitive::kPrimFloat:
1866 case Primitive::kPrimDouble:
1867 locations->SetInAt(0, Location::RequiresFpuRegister());
1868 locations->SetInAt(1, Location::RequiresFpuRegister());
1869 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1870 break;
1871
1872 default:
1873 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1874 }
1875}
1876
1877void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1878 Primitive::Type type = instruction->GetType();
1879 LocationSummary* locations = instruction->GetLocations();
1880
1881 switch (type) {
1882 case Primitive::kPrimInt:
1883 case Primitive::kPrimLong: {
1884 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1885 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1886 Location rhs_location = locations->InAt(1);
1887
1888 GpuRegister rhs_reg = ZERO;
1889 int64_t rhs_imm = 0;
1890 bool use_imm = rhs_location.IsConstant();
1891 if (use_imm) {
1892 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1893 } else {
1894 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1895 }
1896
1897 if (instruction->IsAnd()) {
1898 if (use_imm)
1899 __ Andi(dst, lhs, rhs_imm);
1900 else
1901 __ And(dst, lhs, rhs_reg);
1902 } else if (instruction->IsOr()) {
1903 if (use_imm)
1904 __ Ori(dst, lhs, rhs_imm);
1905 else
1906 __ Or(dst, lhs, rhs_reg);
1907 } else if (instruction->IsXor()) {
1908 if (use_imm)
1909 __ Xori(dst, lhs, rhs_imm);
1910 else
1911 __ Xor(dst, lhs, rhs_reg);
1912 } else if (instruction->IsAdd()) {
1913 if (type == Primitive::kPrimInt) {
1914 if (use_imm)
1915 __ Addiu(dst, lhs, rhs_imm);
1916 else
1917 __ Addu(dst, lhs, rhs_reg);
1918 } else {
1919 if (use_imm)
1920 __ Daddiu(dst, lhs, rhs_imm);
1921 else
1922 __ Daddu(dst, lhs, rhs_reg);
1923 }
1924 } else {
1925 DCHECK(instruction->IsSub());
1926 if (type == Primitive::kPrimInt) {
1927 if (use_imm)
1928 __ Addiu(dst, lhs, -rhs_imm);
1929 else
1930 __ Subu(dst, lhs, rhs_reg);
1931 } else {
1932 if (use_imm)
1933 __ Daddiu(dst, lhs, -rhs_imm);
1934 else
1935 __ Dsubu(dst, lhs, rhs_reg);
1936 }
1937 }
1938 break;
1939 }
1940 case Primitive::kPrimFloat:
1941 case Primitive::kPrimDouble: {
1942 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1943 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1944 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1945 if (instruction->IsAdd()) {
1946 if (type == Primitive::kPrimFloat)
1947 __ AddS(dst, lhs, rhs);
1948 else
1949 __ AddD(dst, lhs, rhs);
1950 } else if (instruction->IsSub()) {
1951 if (type == Primitive::kPrimFloat)
1952 __ SubS(dst, lhs, rhs);
1953 else
1954 __ SubD(dst, lhs, rhs);
1955 } else {
1956 LOG(FATAL) << "Unexpected floating-point binary operation";
1957 }
1958 break;
1959 }
1960 default:
1961 LOG(FATAL) << "Unexpected binary operation type " << type;
1962 }
1963}
1964
1965void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001966 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967
1968 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1969 Primitive::Type type = instr->GetResultType();
1970 switch (type) {
1971 case Primitive::kPrimInt:
1972 case Primitive::kPrimLong: {
1973 locations->SetInAt(0, Location::RequiresRegister());
1974 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001976 break;
1977 }
1978 default:
1979 LOG(FATAL) << "Unexpected shift type " << type;
1980 }
1981}
1982
1983void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001984 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001985 LocationSummary* locations = instr->GetLocations();
1986 Primitive::Type type = instr->GetType();
1987
1988 switch (type) {
1989 case Primitive::kPrimInt:
1990 case Primitive::kPrimLong: {
1991 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1992 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1993 Location rhs_location = locations->InAt(1);
1994
1995 GpuRegister rhs_reg = ZERO;
1996 int64_t rhs_imm = 0;
1997 bool use_imm = rhs_location.IsConstant();
1998 if (use_imm) {
1999 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2000 } else {
2001 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2002 }
2003
2004 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002005 uint32_t shift_value = rhs_imm &
2006 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002007
Alexey Frunze92d90602015-12-18 18:16:36 -08002008 if (shift_value == 0) {
2009 if (dst != lhs) {
2010 __ Move(dst, lhs);
2011 }
2012 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002013 if (instr->IsShl()) {
2014 __ Sll(dst, lhs, shift_value);
2015 } else if (instr->IsShr()) {
2016 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002017 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002018 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002019 } else {
2020 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 }
2022 } else {
2023 if (shift_value < 32) {
2024 if (instr->IsShl()) {
2025 __ Dsll(dst, lhs, shift_value);
2026 } else if (instr->IsShr()) {
2027 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002028 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002029 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002030 } else {
2031 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032 }
2033 } else {
2034 shift_value -= 32;
2035 if (instr->IsShl()) {
2036 __ Dsll32(dst, lhs, shift_value);
2037 } else if (instr->IsShr()) {
2038 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002039 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002041 } else {
2042 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043 }
2044 }
2045 }
2046 } else {
2047 if (type == Primitive::kPrimInt) {
2048 if (instr->IsShl()) {
2049 __ Sllv(dst, lhs, rhs_reg);
2050 } else if (instr->IsShr()) {
2051 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002052 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002053 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002054 } else {
2055 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002056 }
2057 } else {
2058 if (instr->IsShl()) {
2059 __ Dsllv(dst, lhs, rhs_reg);
2060 } else if (instr->IsShr()) {
2061 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002062 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002064 } else {
2065 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002066 }
2067 }
2068 }
2069 break;
2070 }
2071 default:
2072 LOG(FATAL) << "Unexpected shift operation type " << type;
2073 }
2074}
2075
2076void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2077 HandleBinaryOp(instruction);
2078}
2079
2080void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2081 HandleBinaryOp(instruction);
2082}
2083
2084void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2085 HandleBinaryOp(instruction);
2086}
2087
2088void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2089 HandleBinaryOp(instruction);
2090}
2091
2092void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002093 Primitive::Type type = instruction->GetType();
2094 bool object_array_get_with_read_barrier =
2095 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002096 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002097 new (GetGraph()->GetArena()) LocationSummary(instruction,
2098 object_array_get_with_read_barrier
2099 ? LocationSummary::kCallOnSlowPath
2100 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002101 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2102 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2103 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002104 locations->SetInAt(0, Location::RequiresRegister());
2105 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002106 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002107 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2108 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002109 // The output overlaps in the case of an object array get with
2110 // read barriers enabled: we do not want the move to overwrite the
2111 // array's location, as we need it to emit the read barrier.
2112 locations->SetOut(Location::RequiresRegister(),
2113 object_array_get_with_read_barrier
2114 ? Location::kOutputOverlap
2115 : Location::kNoOutputOverlap);
2116 }
2117 // We need a temporary register for the read barrier marking slow
2118 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2119 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2120 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121 }
2122}
2123
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002124static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2125 auto null_checker = [codegen, instruction]() {
2126 codegen->MaybeRecordImplicitNullCheck(instruction);
2127 };
2128 return null_checker;
2129}
2130
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2132 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002133 Location obj_loc = locations->InAt(0);
2134 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2135 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002136 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002137 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002138 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002139
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002140 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002141 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2142 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002143 switch (type) {
2144 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002145 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002146 if (index.IsConstant()) {
2147 size_t offset =
2148 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002149 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002150 } else {
2151 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002152 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002153 }
2154 break;
2155 }
2156
2157 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002158 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002159 if (index.IsConstant()) {
2160 size_t offset =
2161 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002162 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002163 } else {
2164 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002165 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 }
2167 break;
2168 }
2169
2170 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002171 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002172 if (index.IsConstant()) {
2173 size_t offset =
2174 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002175 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002176 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002177 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002178 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 }
2180 break;
2181 }
2182
2183 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002184 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002185 if (maybe_compressed_char_at) {
2186 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002187 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002188 __ Dext(TMP, TMP, 0, 1);
2189 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2190 "Expecting 0=compressed, 1=uncompressed");
2191 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002193 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2194 if (maybe_compressed_char_at) {
2195 Mips64Label uncompressed_load, done;
2196 __ Bnezc(TMP, &uncompressed_load);
2197 __ LoadFromOffset(kLoadUnsignedByte,
2198 out,
2199 obj,
2200 data_offset + (const_index << TIMES_1));
2201 __ Bc(&done);
2202 __ Bind(&uncompressed_load);
2203 __ LoadFromOffset(kLoadUnsignedHalfword,
2204 out,
2205 obj,
2206 data_offset + (const_index << TIMES_2));
2207 __ Bind(&done);
2208 } else {
2209 __ LoadFromOffset(kLoadUnsignedHalfword,
2210 out,
2211 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002212 data_offset + (const_index << TIMES_2),
2213 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002214 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002216 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2217 if (maybe_compressed_char_at) {
2218 Mips64Label uncompressed_load, done;
2219 __ Bnezc(TMP, &uncompressed_load);
2220 __ Daddu(TMP, obj, index_reg);
2221 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2222 __ Bc(&done);
2223 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002224 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002225 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2226 __ Bind(&done);
2227 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002228 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002229 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002230 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002231 }
2232 break;
2233 }
2234
Alexey Frunze15958152017-02-09 19:08:30 -08002235 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002236 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002237 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002238 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2239 if (index.IsConstant()) {
2240 size_t offset =
2241 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002242 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002243 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002244 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002245 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002246 }
2247 break;
2248 }
2249
Alexey Frunze15958152017-02-09 19:08:30 -08002250 case Primitive::kPrimNot: {
2251 static_assert(
2252 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2253 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2254 // /* HeapReference<Object> */ out =
2255 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2256 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2257 Location temp = locations->GetTemp(0);
2258 // Note that a potential implicit null check is handled in this
2259 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2260 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2261 out_loc,
2262 obj,
2263 data_offset,
2264 index,
2265 temp,
2266 /* needs_null_check */ true);
2267 } else {
2268 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2269 if (index.IsConstant()) {
2270 size_t offset =
2271 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2272 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2273 // If read barriers are enabled, emit read barriers other than
2274 // Baker's using a slow path (and also unpoison the loaded
2275 // reference, if heap poisoning is enabled).
2276 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2277 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002278 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002279 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2280 // If read barriers are enabled, emit read barriers other than
2281 // Baker's using a slow path (and also unpoison the loaded
2282 // reference, if heap poisoning is enabled).
2283 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2284 out_loc,
2285 out_loc,
2286 obj_loc,
2287 data_offset,
2288 index);
2289 }
2290 }
2291 break;
2292 }
2293
Alexey Frunze4dda3372015-06-01 18:31:49 -07002294 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002295 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002296 if (index.IsConstant()) {
2297 size_t offset =
2298 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002299 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002300 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002301 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002302 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002303 }
2304 break;
2305 }
2306
2307 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002308 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002309 if (index.IsConstant()) {
2310 size_t offset =
2311 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002312 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002313 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002314 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002315 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002316 }
2317 break;
2318 }
2319
2320 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002321 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002322 if (index.IsConstant()) {
2323 size_t offset =
2324 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002325 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002326 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002327 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002328 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002329 }
2330 break;
2331 }
2332
2333 case Primitive::kPrimVoid:
2334 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2335 UNREACHABLE();
2336 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002337}
2338
2339void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2341 locations->SetInAt(0, Location::RequiresRegister());
2342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2343}
2344
2345void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2346 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002347 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002348 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2349 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2350 __ LoadFromOffset(kLoadWord, out, obj, offset);
2351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002352 // Mask out compression flag from String's array length.
2353 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2354 __ Srl(out, out, 1u);
2355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002356}
2357
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002358Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2359 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2360 ? Location::ConstantLocation(instruction->AsConstant())
2361 : Location::RequiresRegister();
2362}
2363
2364Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2365 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2366 // We can store a non-zero float or double constant without first loading it into the FPU,
2367 // but we should only prefer this if the constant has a single use.
2368 if (instruction->IsConstant() &&
2369 (instruction->AsConstant()->IsZeroBitPattern() ||
2370 instruction->GetUses().HasExactlyOneElement())) {
2371 return Location::ConstantLocation(instruction->AsConstant());
2372 // Otherwise fall through and require an FPU register for the constant.
2373 }
2374 return Location::RequiresFpuRegister();
2375}
2376
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002378 Primitive::Type value_type = instruction->GetComponentType();
2379
2380 bool needs_write_barrier =
2381 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2382 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2383
Alexey Frunze4dda3372015-06-01 18:31:49 -07002384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2385 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002386 may_need_runtime_call_for_type_check ?
2387 LocationSummary::kCallOnSlowPath :
2388 LocationSummary::kNoCall);
2389
2390 locations->SetInAt(0, Location::RequiresRegister());
2391 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2392 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2393 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002394 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002395 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2396 }
2397 if (needs_write_barrier) {
2398 // Temporary register for the write barrier.
2399 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400 }
2401}
2402
2403void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2404 LocationSummary* locations = instruction->GetLocations();
2405 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2406 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002407 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002408 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002409 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002410 bool needs_write_barrier =
2411 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002412 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002413 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002414
2415 switch (value_type) {
2416 case Primitive::kPrimBoolean:
2417 case Primitive::kPrimByte: {
2418 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002419 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002420 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002421 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002422 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2423 }
2424 if (value_location.IsConstant()) {
2425 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2426 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2427 } else {
2428 GpuRegister value = value_location.AsRegister<GpuRegister>();
2429 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002430 }
2431 break;
2432 }
2433
2434 case Primitive::kPrimShort:
2435 case Primitive::kPrimChar: {
2436 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002437 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002438 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002439 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002440 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002441 }
2442 if (value_location.IsConstant()) {
2443 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2444 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2445 } else {
2446 GpuRegister value = value_location.AsRegister<GpuRegister>();
2447 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448 }
2449 break;
2450 }
2451
Alexey Frunze15958152017-02-09 19:08:30 -08002452 case Primitive::kPrimInt: {
2453 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2454 if (index.IsConstant()) {
2455 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2456 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002457 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002458 }
2459 if (value_location.IsConstant()) {
2460 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2461 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2462 } else {
2463 GpuRegister value = value_location.AsRegister<GpuRegister>();
2464 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2465 }
2466 break;
2467 }
2468
Alexey Frunze4dda3372015-06-01 18:31:49 -07002469 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002470 if (value_location.IsConstant()) {
2471 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002473 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002474 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002475 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002476 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002477 }
Alexey Frunze15958152017-02-09 19:08:30 -08002478 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2479 DCHECK_EQ(value, 0);
2480 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2481 DCHECK(!needs_write_barrier);
2482 DCHECK(!may_need_runtime_call_for_type_check);
2483 break;
2484 }
2485
2486 DCHECK(needs_write_barrier);
2487 GpuRegister value = value_location.AsRegister<GpuRegister>();
2488 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2489 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2490 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2491 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2492 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2493 Mips64Label done;
2494 SlowPathCodeMIPS64* slow_path = nullptr;
2495
2496 if (may_need_runtime_call_for_type_check) {
2497 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2498 codegen_->AddSlowPath(slow_path);
2499 if (instruction->GetValueCanBeNull()) {
2500 Mips64Label non_zero;
2501 __ Bnezc(value, &non_zero);
2502 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2503 if (index.IsConstant()) {
2504 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002505 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002506 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002507 }
Alexey Frunze15958152017-02-09 19:08:30 -08002508 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2509 __ Bc(&done);
2510 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002511 }
Alexey Frunze15958152017-02-09 19:08:30 -08002512
2513 // Note that when read barriers are enabled, the type checks
2514 // are performed without read barriers. This is fine, even in
2515 // the case where a class object is in the from-space after
2516 // the flip, as a comparison involving such a type would not
2517 // produce a false positive; it may of course produce a false
2518 // negative, in which case we would take the ArraySet slow
2519 // path.
2520
2521 // /* HeapReference<Class> */ temp1 = obj->klass_
2522 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2523 __ MaybeUnpoisonHeapReference(temp1);
2524
2525 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2526 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2527 // /* HeapReference<Class> */ temp2 = value->klass_
2528 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2529 // If heap poisoning is enabled, no need to unpoison `temp1`
2530 // nor `temp2`, as we are comparing two poisoned references.
2531
2532 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2533 Mips64Label do_put;
2534 __ Beqc(temp1, temp2, &do_put);
2535 // If heap poisoning is enabled, the `temp1` reference has
2536 // not been unpoisoned yet; unpoison it now.
2537 __ MaybeUnpoisonHeapReference(temp1);
2538
2539 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2540 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2541 // If heap poisoning is enabled, no need to unpoison
2542 // `temp1`, as we are comparing against null below.
2543 __ Bnezc(temp1, slow_path->GetEntryLabel());
2544 __ Bind(&do_put);
2545 } else {
2546 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2547 }
2548 }
2549
2550 GpuRegister source = value;
2551 if (kPoisonHeapReferences) {
2552 // Note that in the case where `value` is a null reference,
2553 // we do not enter this block, as a null reference does not
2554 // need poisoning.
2555 __ Move(temp1, value);
2556 __ PoisonHeapReference(temp1);
2557 source = temp1;
2558 }
2559
2560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2561 if (index.IsConstant()) {
2562 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002564 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002565 }
2566 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2567
2568 if (!may_need_runtime_call_for_type_check) {
2569 codegen_->MaybeRecordImplicitNullCheck(instruction);
2570 }
2571
2572 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2573
2574 if (done.IsLinked()) {
2575 __ Bind(&done);
2576 }
2577
2578 if (slow_path != nullptr) {
2579 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002580 }
2581 break;
2582 }
2583
2584 case Primitive::kPrimLong: {
2585 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002586 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002587 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002588 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002589 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002590 }
2591 if (value_location.IsConstant()) {
2592 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2593 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2594 } else {
2595 GpuRegister value = value_location.AsRegister<GpuRegister>();
2596 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002597 }
2598 break;
2599 }
2600
2601 case Primitive::kPrimFloat: {
2602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002604 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002605 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002606 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002607 }
2608 if (value_location.IsConstant()) {
2609 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2610 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2611 } else {
2612 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2613 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002614 }
2615 break;
2616 }
2617
2618 case Primitive::kPrimDouble: {
2619 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002620 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002621 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002622 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002623 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002624 }
2625 if (value_location.IsConstant()) {
2626 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2627 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2628 } else {
2629 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2630 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002631 }
2632 break;
2633 }
2634
2635 case Primitive::kPrimVoid:
2636 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2637 UNREACHABLE();
2638 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002639}
2640
2641void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002642 RegisterSet caller_saves = RegisterSet::Empty();
2643 InvokeRuntimeCallingConvention calling_convention;
2644 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2645 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2646 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002649}
2650
2651void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2652 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002653 BoundsCheckSlowPathMIPS64* slow_path =
2654 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002655 codegen_->AddSlowPath(slow_path);
2656
2657 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2658 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2659
2660 // length is limited by the maximum positive signed 32-bit integer.
2661 // Unsigned comparison of length and index checks for index < 0
2662 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002663 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002664}
2665
Alexey Frunze15958152017-02-09 19:08:30 -08002666// Temp is used for read barrier.
2667static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2668 if (kEmitCompilerReadBarrier &&
2669 (kUseBakerReadBarrier ||
2670 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2671 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2672 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2673 return 1;
2674 }
2675 return 0;
2676}
2677
2678// Extra temp is used for read barrier.
2679static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2680 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2681}
2682
Alexey Frunze4dda3372015-06-01 18:31:49 -07002683void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002684 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2685 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2686
2687 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2688 switch (type_check_kind) {
2689 case TypeCheckKind::kExactCheck:
2690 case TypeCheckKind::kAbstractClassCheck:
2691 case TypeCheckKind::kClassHierarchyCheck:
2692 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002693 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002694 ? LocationSummary::kCallOnSlowPath
2695 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2696 break;
2697 case TypeCheckKind::kArrayCheck:
2698 case TypeCheckKind::kUnresolvedCheck:
2699 case TypeCheckKind::kInterfaceCheck:
2700 call_kind = LocationSummary::kCallOnSlowPath;
2701 break;
2702 }
2703
2704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002705 locations->SetInAt(0, Location::RequiresRegister());
2706 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002707 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002708}
2709
2710void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002711 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002712 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002713 Location obj_loc = locations->InAt(0);
2714 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002715 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002716 Location temp_loc = locations->GetTemp(0);
2717 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2718 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2719 DCHECK_LE(num_temps, 2u);
2720 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002721 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2722 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2723 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2724 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2725 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2726 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2727 const uint32_t object_array_data_offset =
2728 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2729 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002730
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002731 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2732 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2733 // read barriers is done for performance and code size reasons.
2734 bool is_type_check_slow_path_fatal = false;
2735 if (!kEmitCompilerReadBarrier) {
2736 is_type_check_slow_path_fatal =
2737 (type_check_kind == TypeCheckKind::kExactCheck ||
2738 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2739 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2740 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2741 !instruction->CanThrowIntoCatchBlock();
2742 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002743 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002744 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2745 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746 codegen_->AddSlowPath(slow_path);
2747
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002748 // Avoid this check if we know `obj` is not null.
2749 if (instruction->MustDoNullCheck()) {
2750 __ Beqzc(obj, &done);
2751 }
2752
2753 switch (type_check_kind) {
2754 case TypeCheckKind::kExactCheck:
2755 case TypeCheckKind::kArrayCheck: {
2756 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002757 GenerateReferenceLoadTwoRegisters(instruction,
2758 temp_loc,
2759 obj_loc,
2760 class_offset,
2761 maybe_temp2_loc,
2762 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002763 // Jump to slow path for throwing the exception or doing a
2764 // more involved array check.
2765 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2766 break;
2767 }
2768
2769 case TypeCheckKind::kAbstractClassCheck: {
2770 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002771 GenerateReferenceLoadTwoRegisters(instruction,
2772 temp_loc,
2773 obj_loc,
2774 class_offset,
2775 maybe_temp2_loc,
2776 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002777 // If the class is abstract, we eagerly fetch the super class of the
2778 // object to avoid doing a comparison we know will fail.
2779 Mips64Label loop;
2780 __ Bind(&loop);
2781 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002782 GenerateReferenceLoadOneRegister(instruction,
2783 temp_loc,
2784 super_offset,
2785 maybe_temp2_loc,
2786 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002787 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2788 // exception.
2789 __ Beqzc(temp, slow_path->GetEntryLabel());
2790 // Otherwise, compare the classes.
2791 __ Bnec(temp, cls, &loop);
2792 break;
2793 }
2794
2795 case TypeCheckKind::kClassHierarchyCheck: {
2796 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002797 GenerateReferenceLoadTwoRegisters(instruction,
2798 temp_loc,
2799 obj_loc,
2800 class_offset,
2801 maybe_temp2_loc,
2802 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002803 // Walk over the class hierarchy to find a match.
2804 Mips64Label loop;
2805 __ Bind(&loop);
2806 __ Beqc(temp, cls, &done);
2807 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002808 GenerateReferenceLoadOneRegister(instruction,
2809 temp_loc,
2810 super_offset,
2811 maybe_temp2_loc,
2812 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002813 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2814 // exception. Otherwise, jump to the beginning of the loop.
2815 __ Bnezc(temp, &loop);
2816 __ Bc(slow_path->GetEntryLabel());
2817 break;
2818 }
2819
2820 case TypeCheckKind::kArrayObjectCheck: {
2821 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002822 GenerateReferenceLoadTwoRegisters(instruction,
2823 temp_loc,
2824 obj_loc,
2825 class_offset,
2826 maybe_temp2_loc,
2827 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002828 // Do an exact check.
2829 __ Beqc(temp, cls, &done);
2830 // Otherwise, we need to check that the object's class is a non-primitive array.
2831 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002832 GenerateReferenceLoadOneRegister(instruction,
2833 temp_loc,
2834 component_offset,
2835 maybe_temp2_loc,
2836 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002837 // If the component type is null, jump to the slow path to throw the exception.
2838 __ Beqzc(temp, slow_path->GetEntryLabel());
2839 // Otherwise, the object is indeed an array, further check that this component
2840 // type is not a primitive type.
2841 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2842 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2843 __ Bnezc(temp, slow_path->GetEntryLabel());
2844 break;
2845 }
2846
2847 case TypeCheckKind::kUnresolvedCheck:
2848 // We always go into the type check slow path for the unresolved check case.
2849 // We cannot directly call the CheckCast runtime entry point
2850 // without resorting to a type checking slow path here (i.e. by
2851 // calling InvokeRuntime directly), as it would require to
2852 // assign fixed registers for the inputs of this HInstanceOf
2853 // instruction (following the runtime calling convention), which
2854 // might be cluttered by the potential first read barrier
2855 // emission at the beginning of this method.
2856 __ Bc(slow_path->GetEntryLabel());
2857 break;
2858
2859 case TypeCheckKind::kInterfaceCheck: {
2860 // Avoid read barriers to improve performance of the fast path. We can not get false
2861 // positives by doing this.
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 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002870 GenerateReferenceLoadTwoRegisters(instruction,
2871 temp_loc,
2872 temp_loc,
2873 iftable_offset,
2874 maybe_temp2_loc,
2875 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002876 // Iftable is never null.
2877 __ Lw(TMP, temp, array_length_offset);
2878 // Loop through the iftable and check if any class matches.
2879 Mips64Label loop;
2880 __ Bind(&loop);
2881 __ Beqzc(TMP, slow_path->GetEntryLabel());
2882 __ Lwu(AT, temp, object_array_data_offset);
2883 __ MaybeUnpoisonHeapReference(AT);
2884 // Go to next interface.
2885 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2886 __ Addiu(TMP, TMP, -2);
2887 // Compare the classes and continue the loop if they do not match.
2888 __ Bnec(AT, cls, &loop);
2889 break;
2890 }
2891 }
2892
2893 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002894 __ Bind(slow_path->GetExitLabel());
2895}
2896
2897void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2898 LocationSummary* locations =
2899 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2900 locations->SetInAt(0, Location::RequiresRegister());
2901 if (check->HasUses()) {
2902 locations->SetOut(Location::SameAsFirstInput());
2903 }
2904}
2905
2906void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2907 // We assume the class is not null.
2908 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2909 check->GetLoadClass(),
2910 check,
2911 check->GetDexPc(),
2912 true);
2913 codegen_->AddSlowPath(slow_path);
2914 GenerateClassInitializationCheck(slow_path,
2915 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2916}
2917
2918void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2919 Primitive::Type in_type = compare->InputAt(0)->GetType();
2920
Alexey Frunze299a9392015-12-08 16:08:02 -08002921 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002922
2923 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002924 case Primitive::kPrimBoolean:
2925 case Primitive::kPrimByte:
2926 case Primitive::kPrimShort:
2927 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002928 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002929 case Primitive::kPrimLong:
2930 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002931 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2933 break;
2934
2935 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002936 case Primitive::kPrimDouble:
2937 locations->SetInAt(0, Location::RequiresFpuRegister());
2938 locations->SetInAt(1, Location::RequiresFpuRegister());
2939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002940 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941
2942 default:
2943 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2944 }
2945}
2946
2947void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2948 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002949 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002950 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2951
2952 // 0 if: left == right
2953 // 1 if: left > right
2954 // -1 if: left < right
2955 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002956 case Primitive::kPrimBoolean:
2957 case Primitive::kPrimByte:
2958 case Primitive::kPrimShort:
2959 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002960 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002962 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002963 Location rhs_location = locations->InAt(1);
2964 bool use_imm = rhs_location.IsConstant();
2965 GpuRegister rhs = ZERO;
2966 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002967 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002968 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2969 if (value != 0) {
2970 rhs = AT;
2971 __ LoadConst64(rhs, value);
2972 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002973 } else {
2974 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2975 if (value != 0) {
2976 rhs = AT;
2977 __ LoadConst32(rhs, value);
2978 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002979 }
2980 } else {
2981 rhs = rhs_location.AsRegister<GpuRegister>();
2982 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002983 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002984 __ Slt(res, rhs, lhs);
2985 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002986 break;
2987 }
2988
Alexey Frunze299a9392015-12-08 16:08:02 -08002989 case Primitive::kPrimFloat: {
2990 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2991 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2992 Mips64Label done;
2993 __ CmpEqS(FTMP, lhs, rhs);
2994 __ LoadConst32(res, 0);
2995 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002996 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002997 __ CmpLtS(FTMP, lhs, rhs);
2998 __ LoadConst32(res, -1);
2999 __ Bc1nez(FTMP, &done);
3000 __ LoadConst32(res, 1);
3001 } else {
3002 __ CmpLtS(FTMP, rhs, lhs);
3003 __ LoadConst32(res, 1);
3004 __ Bc1nez(FTMP, &done);
3005 __ LoadConst32(res, -1);
3006 }
3007 __ Bind(&done);
3008 break;
3009 }
3010
Alexey Frunze4dda3372015-06-01 18:31:49 -07003011 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003012 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3013 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3014 Mips64Label done;
3015 __ CmpEqD(FTMP, lhs, rhs);
3016 __ LoadConst32(res, 0);
3017 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003018 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003019 __ CmpLtD(FTMP, lhs, rhs);
3020 __ LoadConst32(res, -1);
3021 __ Bc1nez(FTMP, &done);
3022 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003023 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003024 __ CmpLtD(FTMP, rhs, lhs);
3025 __ LoadConst32(res, 1);
3026 __ Bc1nez(FTMP, &done);
3027 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003028 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003029 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003030 break;
3031 }
3032
3033 default:
3034 LOG(FATAL) << "Unimplemented compare type " << in_type;
3035 }
3036}
3037
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003038void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003040 switch (instruction->InputAt(0)->GetType()) {
3041 default:
3042 case Primitive::kPrimLong:
3043 locations->SetInAt(0, Location::RequiresRegister());
3044 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3045 break;
3046
3047 case Primitive::kPrimFloat:
3048 case Primitive::kPrimDouble:
3049 locations->SetInAt(0, Location::RequiresFpuRegister());
3050 locations->SetInAt(1, Location::RequiresFpuRegister());
3051 break;
3052 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003053 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003054 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3055 }
3056}
3057
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003058void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003059 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003060 return;
3061 }
3062
Alexey Frunze299a9392015-12-08 16:08:02 -08003063 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003064 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003065 switch (type) {
3066 default:
3067 // Integer case.
3068 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3069 return;
3070 case Primitive::kPrimLong:
3071 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3072 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08003073 case Primitive::kPrimFloat:
3074 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003075 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3076 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003077 }
3078}
3079
Alexey Frunzec857c742015-09-23 15:12:39 -07003080void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3081 DCHECK(instruction->IsDiv() || instruction->IsRem());
3082 Primitive::Type type = instruction->GetResultType();
3083
3084 LocationSummary* locations = instruction->GetLocations();
3085 Location second = locations->InAt(1);
3086 DCHECK(second.IsConstant());
3087
3088 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3089 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3090 int64_t imm = Int64FromConstant(second.GetConstant());
3091 DCHECK(imm == 1 || imm == -1);
3092
3093 if (instruction->IsRem()) {
3094 __ Move(out, ZERO);
3095 } else {
3096 if (imm == -1) {
3097 if (type == Primitive::kPrimInt) {
3098 __ Subu(out, ZERO, dividend);
3099 } else {
3100 DCHECK_EQ(type, Primitive::kPrimLong);
3101 __ Dsubu(out, ZERO, dividend);
3102 }
3103 } else if (out != dividend) {
3104 __ Move(out, dividend);
3105 }
3106 }
3107}
3108
3109void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3110 DCHECK(instruction->IsDiv() || instruction->IsRem());
3111 Primitive::Type type = instruction->GetResultType();
3112
3113 LocationSummary* locations = instruction->GetLocations();
3114 Location second = locations->InAt(1);
3115 DCHECK(second.IsConstant());
3116
3117 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3118 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3119 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003120 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003121 int ctz_imm = CTZ(abs_imm);
3122
3123 if (instruction->IsDiv()) {
3124 if (type == Primitive::kPrimInt) {
3125 if (ctz_imm == 1) {
3126 // Fast path for division by +/-2, which is very common.
3127 __ Srl(TMP, dividend, 31);
3128 } else {
3129 __ Sra(TMP, dividend, 31);
3130 __ Srl(TMP, TMP, 32 - ctz_imm);
3131 }
3132 __ Addu(out, dividend, TMP);
3133 __ Sra(out, out, ctz_imm);
3134 if (imm < 0) {
3135 __ Subu(out, ZERO, out);
3136 }
3137 } else {
3138 DCHECK_EQ(type, Primitive::kPrimLong);
3139 if (ctz_imm == 1) {
3140 // Fast path for division by +/-2, which is very common.
3141 __ Dsrl32(TMP, dividend, 31);
3142 } else {
3143 __ Dsra32(TMP, dividend, 31);
3144 if (ctz_imm > 32) {
3145 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3146 } else {
3147 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3148 }
3149 }
3150 __ Daddu(out, dividend, TMP);
3151 if (ctz_imm < 32) {
3152 __ Dsra(out, out, ctz_imm);
3153 } else {
3154 __ Dsra32(out, out, ctz_imm - 32);
3155 }
3156 if (imm < 0) {
3157 __ Dsubu(out, ZERO, out);
3158 }
3159 }
3160 } else {
3161 if (type == Primitive::kPrimInt) {
3162 if (ctz_imm == 1) {
3163 // Fast path for modulo +/-2, which is very common.
3164 __ Sra(TMP, dividend, 31);
3165 __ Subu(out, dividend, TMP);
3166 __ Andi(out, out, 1);
3167 __ Addu(out, out, TMP);
3168 } else {
3169 __ Sra(TMP, dividend, 31);
3170 __ Srl(TMP, TMP, 32 - ctz_imm);
3171 __ Addu(out, dividend, TMP);
3172 if (IsUint<16>(abs_imm - 1)) {
3173 __ Andi(out, out, abs_imm - 1);
3174 } else {
3175 __ Sll(out, out, 32 - ctz_imm);
3176 __ Srl(out, out, 32 - ctz_imm);
3177 }
3178 __ Subu(out, out, TMP);
3179 }
3180 } else {
3181 DCHECK_EQ(type, Primitive::kPrimLong);
3182 if (ctz_imm == 1) {
3183 // Fast path for modulo +/-2, which is very common.
3184 __ Dsra32(TMP, dividend, 31);
3185 __ Dsubu(out, dividend, TMP);
3186 __ Andi(out, out, 1);
3187 __ Daddu(out, out, TMP);
3188 } else {
3189 __ Dsra32(TMP, dividend, 31);
3190 if (ctz_imm > 32) {
3191 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3192 } else {
3193 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3194 }
3195 __ Daddu(out, dividend, TMP);
3196 if (IsUint<16>(abs_imm - 1)) {
3197 __ Andi(out, out, abs_imm - 1);
3198 } else {
3199 if (ctz_imm > 32) {
3200 __ Dsll(out, out, 64 - ctz_imm);
3201 __ Dsrl(out, out, 64 - ctz_imm);
3202 } else {
3203 __ Dsll32(out, out, 32 - ctz_imm);
3204 __ Dsrl32(out, out, 32 - ctz_imm);
3205 }
3206 }
3207 __ Dsubu(out, out, TMP);
3208 }
3209 }
3210 }
3211}
3212
3213void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3214 DCHECK(instruction->IsDiv() || instruction->IsRem());
3215
3216 LocationSummary* locations = instruction->GetLocations();
3217 Location second = locations->InAt(1);
3218 DCHECK(second.IsConstant());
3219
3220 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3221 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3222 int64_t imm = Int64FromConstant(second.GetConstant());
3223
3224 Primitive::Type type = instruction->GetResultType();
3225 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3226
3227 int64_t magic;
3228 int shift;
3229 CalculateMagicAndShiftForDivRem(imm,
3230 (type == Primitive::kPrimLong),
3231 &magic,
3232 &shift);
3233
3234 if (type == Primitive::kPrimInt) {
3235 __ LoadConst32(TMP, magic);
3236 __ MuhR6(TMP, dividend, TMP);
3237
3238 if (imm > 0 && magic < 0) {
3239 __ Addu(TMP, TMP, dividend);
3240 } else if (imm < 0 && magic > 0) {
3241 __ Subu(TMP, TMP, dividend);
3242 }
3243
3244 if (shift != 0) {
3245 __ Sra(TMP, TMP, shift);
3246 }
3247
3248 if (instruction->IsDiv()) {
3249 __ Sra(out, TMP, 31);
3250 __ Subu(out, TMP, out);
3251 } else {
3252 __ Sra(AT, TMP, 31);
3253 __ Subu(AT, TMP, AT);
3254 __ LoadConst32(TMP, imm);
3255 __ MulR6(TMP, AT, TMP);
3256 __ Subu(out, dividend, TMP);
3257 }
3258 } else {
3259 __ LoadConst64(TMP, magic);
3260 __ Dmuh(TMP, dividend, TMP);
3261
3262 if (imm > 0 && magic < 0) {
3263 __ Daddu(TMP, TMP, dividend);
3264 } else if (imm < 0 && magic > 0) {
3265 __ Dsubu(TMP, TMP, dividend);
3266 }
3267
3268 if (shift >= 32) {
3269 __ Dsra32(TMP, TMP, shift - 32);
3270 } else if (shift > 0) {
3271 __ Dsra(TMP, TMP, shift);
3272 }
3273
3274 if (instruction->IsDiv()) {
3275 __ Dsra32(out, TMP, 31);
3276 __ Dsubu(out, TMP, out);
3277 } else {
3278 __ Dsra32(AT, TMP, 31);
3279 __ Dsubu(AT, TMP, AT);
3280 __ LoadConst64(TMP, imm);
3281 __ Dmul(TMP, AT, TMP);
3282 __ Dsubu(out, dividend, TMP);
3283 }
3284 }
3285}
3286
3287void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3288 DCHECK(instruction->IsDiv() || instruction->IsRem());
3289 Primitive::Type type = instruction->GetResultType();
3290 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3291
3292 LocationSummary* locations = instruction->GetLocations();
3293 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3294 Location second = locations->InAt(1);
3295
3296 if (second.IsConstant()) {
3297 int64_t imm = Int64FromConstant(second.GetConstant());
3298 if (imm == 0) {
3299 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3300 } else if (imm == 1 || imm == -1) {
3301 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003302 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003303 DivRemByPowerOfTwo(instruction);
3304 } else {
3305 DCHECK(imm <= -2 || imm >= 2);
3306 GenerateDivRemWithAnyConstant(instruction);
3307 }
3308 } else {
3309 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3310 GpuRegister divisor = second.AsRegister<GpuRegister>();
3311 if (instruction->IsDiv()) {
3312 if (type == Primitive::kPrimInt)
3313 __ DivR6(out, dividend, divisor);
3314 else
3315 __ Ddiv(out, dividend, divisor);
3316 } else {
3317 if (type == Primitive::kPrimInt)
3318 __ ModR6(out, dividend, divisor);
3319 else
3320 __ Dmod(out, dividend, divisor);
3321 }
3322 }
3323}
3324
Alexey Frunze4dda3372015-06-01 18:31:49 -07003325void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3326 LocationSummary* locations =
3327 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3328 switch (div->GetResultType()) {
3329 case Primitive::kPrimInt:
3330 case Primitive::kPrimLong:
3331 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003332 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3334 break;
3335
3336 case Primitive::kPrimFloat:
3337 case Primitive::kPrimDouble:
3338 locations->SetInAt(0, Location::RequiresFpuRegister());
3339 locations->SetInAt(1, Location::RequiresFpuRegister());
3340 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3341 break;
3342
3343 default:
3344 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3345 }
3346}
3347
3348void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3349 Primitive::Type type = instruction->GetType();
3350 LocationSummary* locations = instruction->GetLocations();
3351
3352 switch (type) {
3353 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003354 case Primitive::kPrimLong:
3355 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003356 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003357 case Primitive::kPrimFloat:
3358 case Primitive::kPrimDouble: {
3359 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3360 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3361 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3362 if (type == Primitive::kPrimFloat)
3363 __ DivS(dst, lhs, rhs);
3364 else
3365 __ DivD(dst, lhs, rhs);
3366 break;
3367 }
3368 default:
3369 LOG(FATAL) << "Unexpected div type " << type;
3370 }
3371}
3372
3373void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003374 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003375 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003376}
3377
3378void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3379 SlowPathCodeMIPS64* slow_path =
3380 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3381 codegen_->AddSlowPath(slow_path);
3382 Location value = instruction->GetLocations()->InAt(0);
3383
3384 Primitive::Type type = instruction->GetType();
3385
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003386 if (!Primitive::IsIntegralType(type)) {
3387 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003388 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003389 }
3390
3391 if (value.IsConstant()) {
3392 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3393 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003394 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003395 } else {
3396 // A division by a non-null constant is valid. We don't need to perform
3397 // any check, so simply fall through.
3398 }
3399 } else {
3400 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3401 }
3402}
3403
3404void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3405 LocationSummary* locations =
3406 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3407 locations->SetOut(Location::ConstantLocation(constant));
3408}
3409
3410void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3411 // Will be generated at use site.
3412}
3413
3414void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3415 exit->SetLocations(nullptr);
3416}
3417
3418void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3419}
3420
3421void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3422 LocationSummary* locations =
3423 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3424 locations->SetOut(Location::ConstantLocation(constant));
3425}
3426
3427void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3428 // Will be generated at use site.
3429}
3430
David Brazdilfc6a86a2015-06-26 10:33:45 +00003431void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003432 DCHECK(!successor->IsExitBlock());
3433 HBasicBlock* block = got->GetBlock();
3434 HInstruction* previous = got->GetPrevious();
3435 HLoopInformation* info = block->GetLoopInformation();
3436
3437 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3438 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3439 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3440 return;
3441 }
3442 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3443 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3444 }
3445 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003446 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003447 }
3448}
3449
David Brazdilfc6a86a2015-06-26 10:33:45 +00003450void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3451 got->SetLocations(nullptr);
3452}
3453
3454void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3455 HandleGoto(got, got->GetSuccessor());
3456}
3457
3458void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3459 try_boundary->SetLocations(nullptr);
3460}
3461
3462void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3463 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3464 if (!successor->IsExitBlock()) {
3465 HandleGoto(try_boundary, successor);
3466 }
3467}
3468
Alexey Frunze299a9392015-12-08 16:08:02 -08003469void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3470 bool is64bit,
3471 LocationSummary* locations) {
3472 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3473 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3474 Location rhs_location = locations->InAt(1);
3475 GpuRegister rhs_reg = ZERO;
3476 int64_t rhs_imm = 0;
3477 bool use_imm = rhs_location.IsConstant();
3478 if (use_imm) {
3479 if (is64bit) {
3480 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3481 } else {
3482 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3483 }
3484 } else {
3485 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3486 }
3487 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3488
3489 switch (cond) {
3490 case kCondEQ:
3491 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003492 if (use_imm && IsInt<16>(-rhs_imm)) {
3493 if (rhs_imm == 0) {
3494 if (cond == kCondEQ) {
3495 __ Sltiu(dst, lhs, 1);
3496 } else {
3497 __ Sltu(dst, ZERO, lhs);
3498 }
3499 } else {
3500 if (is64bit) {
3501 __ Daddiu(dst, lhs, -rhs_imm);
3502 } else {
3503 __ Addiu(dst, lhs, -rhs_imm);
3504 }
3505 if (cond == kCondEQ) {
3506 __ Sltiu(dst, dst, 1);
3507 } else {
3508 __ Sltu(dst, ZERO, dst);
3509 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003510 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003511 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003512 if (use_imm && IsUint<16>(rhs_imm)) {
3513 __ Xori(dst, lhs, rhs_imm);
3514 } else {
3515 if (use_imm) {
3516 rhs_reg = TMP;
3517 __ LoadConst64(rhs_reg, rhs_imm);
3518 }
3519 __ Xor(dst, lhs, rhs_reg);
3520 }
3521 if (cond == kCondEQ) {
3522 __ Sltiu(dst, dst, 1);
3523 } else {
3524 __ Sltu(dst, ZERO, dst);
3525 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003526 }
3527 break;
3528
3529 case kCondLT:
3530 case kCondGE:
3531 if (use_imm && IsInt<16>(rhs_imm)) {
3532 __ Slti(dst, lhs, rhs_imm);
3533 } else {
3534 if (use_imm) {
3535 rhs_reg = TMP;
3536 __ LoadConst64(rhs_reg, rhs_imm);
3537 }
3538 __ Slt(dst, lhs, rhs_reg);
3539 }
3540 if (cond == kCondGE) {
3541 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3542 // only the slt instruction but no sge.
3543 __ Xori(dst, dst, 1);
3544 }
3545 break;
3546
3547 case kCondLE:
3548 case kCondGT:
3549 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3550 // Simulate lhs <= rhs via lhs < rhs + 1.
3551 __ Slti(dst, lhs, rhs_imm_plus_one);
3552 if (cond == kCondGT) {
3553 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3554 // only the slti instruction but no sgti.
3555 __ Xori(dst, dst, 1);
3556 }
3557 } else {
3558 if (use_imm) {
3559 rhs_reg = TMP;
3560 __ LoadConst64(rhs_reg, rhs_imm);
3561 }
3562 __ Slt(dst, rhs_reg, lhs);
3563 if (cond == kCondLE) {
3564 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3565 // only the slt instruction but no sle.
3566 __ Xori(dst, dst, 1);
3567 }
3568 }
3569 break;
3570
3571 case kCondB:
3572 case kCondAE:
3573 if (use_imm && IsInt<16>(rhs_imm)) {
3574 // Sltiu sign-extends its 16-bit immediate operand before
3575 // the comparison and thus lets us compare directly with
3576 // unsigned values in the ranges [0, 0x7fff] and
3577 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3578 __ Sltiu(dst, lhs, rhs_imm);
3579 } else {
3580 if (use_imm) {
3581 rhs_reg = TMP;
3582 __ LoadConst64(rhs_reg, rhs_imm);
3583 }
3584 __ Sltu(dst, lhs, rhs_reg);
3585 }
3586 if (cond == kCondAE) {
3587 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3588 // only the sltu instruction but no sgeu.
3589 __ Xori(dst, dst, 1);
3590 }
3591 break;
3592
3593 case kCondBE:
3594 case kCondA:
3595 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3596 // Simulate lhs <= rhs via lhs < rhs + 1.
3597 // Note that this only works if rhs + 1 does not overflow
3598 // to 0, hence the check above.
3599 // Sltiu sign-extends its 16-bit immediate operand before
3600 // the comparison and thus lets us compare directly with
3601 // unsigned values in the ranges [0, 0x7fff] and
3602 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3603 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3604 if (cond == kCondA) {
3605 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3606 // only the sltiu instruction but no sgtiu.
3607 __ Xori(dst, dst, 1);
3608 }
3609 } else {
3610 if (use_imm) {
3611 rhs_reg = TMP;
3612 __ LoadConst64(rhs_reg, rhs_imm);
3613 }
3614 __ Sltu(dst, rhs_reg, lhs);
3615 if (cond == kCondBE) {
3616 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3617 // only the sltu instruction but no sleu.
3618 __ Xori(dst, dst, 1);
3619 }
3620 }
3621 break;
3622 }
3623}
3624
3625void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3626 bool is64bit,
3627 LocationSummary* locations,
3628 Mips64Label* label) {
3629 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3630 Location rhs_location = locations->InAt(1);
3631 GpuRegister rhs_reg = ZERO;
3632 int64_t rhs_imm = 0;
3633 bool use_imm = rhs_location.IsConstant();
3634 if (use_imm) {
3635 if (is64bit) {
3636 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3637 } else {
3638 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3639 }
3640 } else {
3641 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3642 }
3643
3644 if (use_imm && rhs_imm == 0) {
3645 switch (cond) {
3646 case kCondEQ:
3647 case kCondBE: // <= 0 if zero
3648 __ Beqzc(lhs, label);
3649 break;
3650 case kCondNE:
3651 case kCondA: // > 0 if non-zero
3652 __ Bnezc(lhs, label);
3653 break;
3654 case kCondLT:
3655 __ Bltzc(lhs, label);
3656 break;
3657 case kCondGE:
3658 __ Bgezc(lhs, label);
3659 break;
3660 case kCondLE:
3661 __ Blezc(lhs, label);
3662 break;
3663 case kCondGT:
3664 __ Bgtzc(lhs, label);
3665 break;
3666 case kCondB: // always false
3667 break;
3668 case kCondAE: // always true
3669 __ Bc(label);
3670 break;
3671 }
3672 } else {
3673 if (use_imm) {
3674 rhs_reg = TMP;
3675 __ LoadConst64(rhs_reg, rhs_imm);
3676 }
3677 switch (cond) {
3678 case kCondEQ:
3679 __ Beqc(lhs, rhs_reg, label);
3680 break;
3681 case kCondNE:
3682 __ Bnec(lhs, rhs_reg, label);
3683 break;
3684 case kCondLT:
3685 __ Bltc(lhs, rhs_reg, label);
3686 break;
3687 case kCondGE:
3688 __ Bgec(lhs, rhs_reg, label);
3689 break;
3690 case kCondLE:
3691 __ Bgec(rhs_reg, lhs, label);
3692 break;
3693 case kCondGT:
3694 __ Bltc(rhs_reg, lhs, label);
3695 break;
3696 case kCondB:
3697 __ Bltuc(lhs, rhs_reg, label);
3698 break;
3699 case kCondAE:
3700 __ Bgeuc(lhs, rhs_reg, label);
3701 break;
3702 case kCondBE:
3703 __ Bgeuc(rhs_reg, lhs, label);
3704 break;
3705 case kCondA:
3706 __ Bltuc(rhs_reg, lhs, label);
3707 break;
3708 }
3709 }
3710}
3711
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003712void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3713 bool gt_bias,
3714 Primitive::Type type,
3715 LocationSummary* locations) {
3716 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3717 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3718 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3719 if (type == Primitive::kPrimFloat) {
3720 switch (cond) {
3721 case kCondEQ:
3722 __ CmpEqS(FTMP, lhs, rhs);
3723 __ Mfc1(dst, FTMP);
3724 __ Andi(dst, dst, 1);
3725 break;
3726 case kCondNE:
3727 __ CmpEqS(FTMP, lhs, rhs);
3728 __ Mfc1(dst, FTMP);
3729 __ Addiu(dst, dst, 1);
3730 break;
3731 case kCondLT:
3732 if (gt_bias) {
3733 __ CmpLtS(FTMP, lhs, rhs);
3734 } else {
3735 __ CmpUltS(FTMP, lhs, rhs);
3736 }
3737 __ Mfc1(dst, FTMP);
3738 __ Andi(dst, dst, 1);
3739 break;
3740 case kCondLE:
3741 if (gt_bias) {
3742 __ CmpLeS(FTMP, lhs, rhs);
3743 } else {
3744 __ CmpUleS(FTMP, lhs, rhs);
3745 }
3746 __ Mfc1(dst, FTMP);
3747 __ Andi(dst, dst, 1);
3748 break;
3749 case kCondGT:
3750 if (gt_bias) {
3751 __ CmpUltS(FTMP, rhs, lhs);
3752 } else {
3753 __ CmpLtS(FTMP, rhs, lhs);
3754 }
3755 __ Mfc1(dst, FTMP);
3756 __ Andi(dst, dst, 1);
3757 break;
3758 case kCondGE:
3759 if (gt_bias) {
3760 __ CmpUleS(FTMP, rhs, lhs);
3761 } else {
3762 __ CmpLeS(FTMP, rhs, lhs);
3763 }
3764 __ Mfc1(dst, FTMP);
3765 __ Andi(dst, dst, 1);
3766 break;
3767 default:
3768 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3769 UNREACHABLE();
3770 }
3771 } else {
3772 DCHECK_EQ(type, Primitive::kPrimDouble);
3773 switch (cond) {
3774 case kCondEQ:
3775 __ CmpEqD(FTMP, lhs, rhs);
3776 __ Mfc1(dst, FTMP);
3777 __ Andi(dst, dst, 1);
3778 break;
3779 case kCondNE:
3780 __ CmpEqD(FTMP, lhs, rhs);
3781 __ Mfc1(dst, FTMP);
3782 __ Addiu(dst, dst, 1);
3783 break;
3784 case kCondLT:
3785 if (gt_bias) {
3786 __ CmpLtD(FTMP, lhs, rhs);
3787 } else {
3788 __ CmpUltD(FTMP, lhs, rhs);
3789 }
3790 __ Mfc1(dst, FTMP);
3791 __ Andi(dst, dst, 1);
3792 break;
3793 case kCondLE:
3794 if (gt_bias) {
3795 __ CmpLeD(FTMP, lhs, rhs);
3796 } else {
3797 __ CmpUleD(FTMP, lhs, rhs);
3798 }
3799 __ Mfc1(dst, FTMP);
3800 __ Andi(dst, dst, 1);
3801 break;
3802 case kCondGT:
3803 if (gt_bias) {
3804 __ CmpUltD(FTMP, rhs, lhs);
3805 } else {
3806 __ CmpLtD(FTMP, rhs, lhs);
3807 }
3808 __ Mfc1(dst, FTMP);
3809 __ Andi(dst, dst, 1);
3810 break;
3811 case kCondGE:
3812 if (gt_bias) {
3813 __ CmpUleD(FTMP, rhs, lhs);
3814 } else {
3815 __ CmpLeD(FTMP, rhs, lhs);
3816 }
3817 __ Mfc1(dst, FTMP);
3818 __ Andi(dst, dst, 1);
3819 break;
3820 default:
3821 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3822 UNREACHABLE();
3823 }
3824 }
3825}
3826
Alexey Frunze299a9392015-12-08 16:08:02 -08003827void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3828 bool gt_bias,
3829 Primitive::Type type,
3830 LocationSummary* locations,
3831 Mips64Label* label) {
3832 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3833 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3834 if (type == Primitive::kPrimFloat) {
3835 switch (cond) {
3836 case kCondEQ:
3837 __ CmpEqS(FTMP, lhs, rhs);
3838 __ Bc1nez(FTMP, label);
3839 break;
3840 case kCondNE:
3841 __ CmpEqS(FTMP, lhs, rhs);
3842 __ Bc1eqz(FTMP, label);
3843 break;
3844 case kCondLT:
3845 if (gt_bias) {
3846 __ CmpLtS(FTMP, lhs, rhs);
3847 } else {
3848 __ CmpUltS(FTMP, lhs, rhs);
3849 }
3850 __ Bc1nez(FTMP, label);
3851 break;
3852 case kCondLE:
3853 if (gt_bias) {
3854 __ CmpLeS(FTMP, lhs, rhs);
3855 } else {
3856 __ CmpUleS(FTMP, lhs, rhs);
3857 }
3858 __ Bc1nez(FTMP, label);
3859 break;
3860 case kCondGT:
3861 if (gt_bias) {
3862 __ CmpUltS(FTMP, rhs, lhs);
3863 } else {
3864 __ CmpLtS(FTMP, rhs, lhs);
3865 }
3866 __ Bc1nez(FTMP, label);
3867 break;
3868 case kCondGE:
3869 if (gt_bias) {
3870 __ CmpUleS(FTMP, rhs, lhs);
3871 } else {
3872 __ CmpLeS(FTMP, rhs, lhs);
3873 }
3874 __ Bc1nez(FTMP, label);
3875 break;
3876 default:
3877 LOG(FATAL) << "Unexpected non-floating-point condition";
3878 }
3879 } else {
3880 DCHECK_EQ(type, Primitive::kPrimDouble);
3881 switch (cond) {
3882 case kCondEQ:
3883 __ CmpEqD(FTMP, lhs, rhs);
3884 __ Bc1nez(FTMP, label);
3885 break;
3886 case kCondNE:
3887 __ CmpEqD(FTMP, lhs, rhs);
3888 __ Bc1eqz(FTMP, label);
3889 break;
3890 case kCondLT:
3891 if (gt_bias) {
3892 __ CmpLtD(FTMP, lhs, rhs);
3893 } else {
3894 __ CmpUltD(FTMP, lhs, rhs);
3895 }
3896 __ Bc1nez(FTMP, label);
3897 break;
3898 case kCondLE:
3899 if (gt_bias) {
3900 __ CmpLeD(FTMP, lhs, rhs);
3901 } else {
3902 __ CmpUleD(FTMP, lhs, rhs);
3903 }
3904 __ Bc1nez(FTMP, label);
3905 break;
3906 case kCondGT:
3907 if (gt_bias) {
3908 __ CmpUltD(FTMP, rhs, lhs);
3909 } else {
3910 __ CmpLtD(FTMP, rhs, lhs);
3911 }
3912 __ Bc1nez(FTMP, label);
3913 break;
3914 case kCondGE:
3915 if (gt_bias) {
3916 __ CmpUleD(FTMP, rhs, lhs);
3917 } else {
3918 __ CmpLeD(FTMP, rhs, lhs);
3919 }
3920 __ Bc1nez(FTMP, label);
3921 break;
3922 default:
3923 LOG(FATAL) << "Unexpected non-floating-point condition";
3924 }
3925 }
3926}
3927
Alexey Frunze4dda3372015-06-01 18:31:49 -07003928void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003929 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003930 Mips64Label* true_target,
3931 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003932 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003933
David Brazdil0debae72015-11-12 18:37:00 +00003934 if (true_target == nullptr && false_target == nullptr) {
3935 // Nothing to do. The code always falls through.
3936 return;
3937 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003938 // Constant condition, statically compared against "true" (integer value 1).
3939 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003940 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003941 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003942 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003943 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003944 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003945 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003946 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003947 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003948 }
David Brazdil0debae72015-11-12 18:37:00 +00003949 return;
3950 }
3951
3952 // The following code generates these patterns:
3953 // (1) true_target == nullptr && false_target != nullptr
3954 // - opposite condition true => branch to false_target
3955 // (2) true_target != nullptr && false_target == nullptr
3956 // - condition true => branch to true_target
3957 // (3) true_target != nullptr && false_target != nullptr
3958 // - condition true => branch to true_target
3959 // - branch to false_target
3960 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003961 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003962 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003963 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003964 if (true_target == nullptr) {
3965 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3966 } else {
3967 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3968 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003969 } else {
3970 // The condition instruction has not been materialized, use its inputs as
3971 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003972 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003973 Primitive::Type type = condition->InputAt(0)->GetType();
3974 LocationSummary* locations = cond->GetLocations();
3975 IfCondition if_cond = condition->GetCondition();
3976 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003977
David Brazdil0debae72015-11-12 18:37:00 +00003978 if (true_target == nullptr) {
3979 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003980 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003981 }
3982
Alexey Frunze299a9392015-12-08 16:08:02 -08003983 switch (type) {
3984 default:
3985 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3986 break;
3987 case Primitive::kPrimLong:
3988 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3989 break;
3990 case Primitive::kPrimFloat:
3991 case Primitive::kPrimDouble:
3992 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3993 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003994 }
3995 }
David Brazdil0debae72015-11-12 18:37:00 +00003996
3997 // If neither branch falls through (case 3), the conditional branch to `true_target`
3998 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3999 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004000 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004001 }
4002}
4003
4004void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
4005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004006 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004007 locations->SetInAt(0, Location::RequiresRegister());
4008 }
4009}
4010
4011void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004012 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4013 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004014 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004015 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004016 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004017 nullptr : codegen_->GetLabelOf(false_successor);
4018 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004019}
4020
4021void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
4022 LocationSummary* locations = new (GetGraph()->GetArena())
4023 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004024 InvokeRuntimeCallingConvention calling_convention;
4025 RegisterSet caller_saves = RegisterSet::Empty();
4026 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4027 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004028 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004029 locations->SetInAt(0, Location::RequiresRegister());
4030 }
4031}
4032
4033void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004034 SlowPathCodeMIPS64* slow_path =
4035 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004036 GenerateTestAndBranch(deoptimize,
4037 /* condition_input_index */ 0,
4038 slow_path->GetEntryLabel(),
4039 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004040}
4041
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004042void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4043 LocationSummary* locations = new (GetGraph()->GetArena())
4044 LocationSummary(flag, LocationSummary::kNoCall);
4045 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004046}
4047
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004048void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4049 __ LoadFromOffset(kLoadWord,
4050 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4051 SP,
4052 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004053}
4054
David Brazdil74eb1b22015-12-14 11:44:01 +00004055void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
4056 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
4057 if (Primitive::IsFloatingPointType(select->GetType())) {
4058 locations->SetInAt(0, Location::RequiresFpuRegister());
4059 locations->SetInAt(1, Location::RequiresFpuRegister());
4060 } else {
4061 locations->SetInAt(0, Location::RequiresRegister());
4062 locations->SetInAt(1, Location::RequiresRegister());
4063 }
4064 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
4065 locations->SetInAt(2, Location::RequiresRegister());
4066 }
4067 locations->SetOut(Location::SameAsFirstInput());
4068}
4069
4070void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
4071 LocationSummary* locations = select->GetLocations();
4072 Mips64Label false_target;
4073 GenerateTestAndBranch(select,
4074 /* condition_input_index */ 2,
4075 /* true_target */ nullptr,
4076 &false_target);
4077 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4078 __ Bind(&false_target);
4079}
4080
David Srbecky0cf44932015-12-09 14:09:59 +00004081void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4082 new (GetGraph()->GetArena()) LocationSummary(info);
4083}
4084
David Srbeckyd28f4a02016-03-14 17:14:24 +00004085void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4086 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004087}
4088
4089void CodeGeneratorMIPS64::GenerateNop() {
4090 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004091}
4092
Alexey Frunze4dda3372015-06-01 18:31:49 -07004093void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004094 const FieldInfo& field_info) {
4095 Primitive::Type field_type = field_info.GetFieldType();
4096 bool object_field_get_with_read_barrier =
4097 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
4098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4099 instruction,
4100 object_field_get_with_read_barrier
4101 ? LocationSummary::kCallOnSlowPath
4102 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004103 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4104 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4105 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004106 locations->SetInAt(0, Location::RequiresRegister());
4107 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4108 locations->SetOut(Location::RequiresFpuRegister());
4109 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004110 // The output overlaps in the case of an object field get with
4111 // read barriers enabled: we do not want the move to overwrite the
4112 // object's location, as we need it to emit the read barrier.
4113 locations->SetOut(Location::RequiresRegister(),
4114 object_field_get_with_read_barrier
4115 ? Location::kOutputOverlap
4116 : Location::kNoOutputOverlap);
4117 }
4118 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4119 // We need a temporary register for the read barrier marking slow
4120 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
4121 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004122 }
4123}
4124
4125void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4126 const FieldInfo& field_info) {
4127 Primitive::Type type = field_info.GetFieldType();
4128 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004129 Location obj_loc = locations->InAt(0);
4130 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4131 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004132 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004133 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004134 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004135 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4136
Alexey Frunze4dda3372015-06-01 18:31:49 -07004137 switch (type) {
4138 case Primitive::kPrimBoolean:
4139 load_type = kLoadUnsignedByte;
4140 break;
4141 case Primitive::kPrimByte:
4142 load_type = kLoadSignedByte;
4143 break;
4144 case Primitive::kPrimShort:
4145 load_type = kLoadSignedHalfword;
4146 break;
4147 case Primitive::kPrimChar:
4148 load_type = kLoadUnsignedHalfword;
4149 break;
4150 case Primitive::kPrimInt:
4151 case Primitive::kPrimFloat:
4152 load_type = kLoadWord;
4153 break;
4154 case Primitive::kPrimLong:
4155 case Primitive::kPrimDouble:
4156 load_type = kLoadDoubleword;
4157 break;
4158 case Primitive::kPrimNot:
4159 load_type = kLoadUnsignedWord;
4160 break;
4161 case Primitive::kPrimVoid:
4162 LOG(FATAL) << "Unreachable type " << type;
4163 UNREACHABLE();
4164 }
4165 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004166 DCHECK(dst_loc.IsRegister());
4167 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4168 if (type == Primitive::kPrimNot) {
4169 // /* HeapReference<Object> */ dst = *(obj + offset)
4170 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4171 Location temp_loc = locations->GetTemp(0);
4172 // Note that a potential implicit null check is handled in this
4173 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4174 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4175 dst_loc,
4176 obj,
4177 offset,
4178 temp_loc,
4179 /* needs_null_check */ true);
4180 if (is_volatile) {
4181 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4182 }
4183 } else {
4184 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4185 if (is_volatile) {
4186 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4187 }
4188 // If read barriers are enabled, emit read barriers other than
4189 // Baker's using a slow path (and also unpoison the loaded
4190 // reference, if heap poisoning is enabled).
4191 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4192 }
4193 } else {
4194 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4195 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004196 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004197 DCHECK(dst_loc.IsFpuRegister());
4198 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004199 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004200 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004201
Alexey Frunze15958152017-02-09 19:08:30 -08004202 // Memory barriers, in the case of references, are handled in the
4203 // previous switch statement.
4204 if (is_volatile && (type != Primitive::kPrimNot)) {
4205 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004206 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004207}
4208
4209void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4210 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4211 LocationSummary* locations =
4212 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4213 locations->SetInAt(0, Location::RequiresRegister());
4214 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004215 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004216 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004217 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004218 }
4219}
4220
4221void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004222 const FieldInfo& field_info,
4223 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004224 Primitive::Type type = field_info.GetFieldType();
4225 LocationSummary* locations = instruction->GetLocations();
4226 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004227 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004228 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004229 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004230 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4231 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004232 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4233
Alexey Frunze4dda3372015-06-01 18:31:49 -07004234 switch (type) {
4235 case Primitive::kPrimBoolean:
4236 case Primitive::kPrimByte:
4237 store_type = kStoreByte;
4238 break;
4239 case Primitive::kPrimShort:
4240 case Primitive::kPrimChar:
4241 store_type = kStoreHalfword;
4242 break;
4243 case Primitive::kPrimInt:
4244 case Primitive::kPrimFloat:
4245 case Primitive::kPrimNot:
4246 store_type = kStoreWord;
4247 break;
4248 case Primitive::kPrimLong:
4249 case Primitive::kPrimDouble:
4250 store_type = kStoreDoubleword;
4251 break;
4252 case Primitive::kPrimVoid:
4253 LOG(FATAL) << "Unreachable type " << type;
4254 UNREACHABLE();
4255 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004256
Alexey Frunze15958152017-02-09 19:08:30 -08004257 if (is_volatile) {
4258 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4259 }
4260
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004261 if (value_location.IsConstant()) {
4262 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4263 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4264 } else {
4265 if (!Primitive::IsFloatingPointType(type)) {
4266 DCHECK(value_location.IsRegister());
4267 GpuRegister src = value_location.AsRegister<GpuRegister>();
4268 if (kPoisonHeapReferences && needs_write_barrier) {
4269 // Note that in the case where `value` is a null reference,
4270 // we do not enter this block, as a null reference does not
4271 // need poisoning.
4272 DCHECK_EQ(type, Primitive::kPrimNot);
4273 __ PoisonHeapReference(TMP, src);
4274 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4275 } else {
4276 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4277 }
4278 } else {
4279 DCHECK(value_location.IsFpuRegister());
4280 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4281 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4282 }
4283 }
Alexey Frunze15958152017-02-09 19:08:30 -08004284
Alexey Frunzec061de12017-02-14 13:27:23 -08004285 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004286 DCHECK(value_location.IsRegister());
4287 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004288 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004289 }
Alexey Frunze15958152017-02-09 19:08:30 -08004290
4291 if (is_volatile) {
4292 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4293 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004294}
4295
4296void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4297 HandleFieldGet(instruction, instruction->GetFieldInfo());
4298}
4299
4300void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4301 HandleFieldGet(instruction, instruction->GetFieldInfo());
4302}
4303
4304void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4305 HandleFieldSet(instruction, instruction->GetFieldInfo());
4306}
4307
4308void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004309 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004310}
4311
Alexey Frunze15958152017-02-09 19:08:30 -08004312void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4313 HInstruction* instruction,
4314 Location out,
4315 uint32_t offset,
4316 Location maybe_temp,
4317 ReadBarrierOption read_barrier_option) {
4318 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4319 if (read_barrier_option == kWithReadBarrier) {
4320 CHECK(kEmitCompilerReadBarrier);
4321 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4322 if (kUseBakerReadBarrier) {
4323 // Load with fast path based Baker's read barrier.
4324 // /* HeapReference<Object> */ out = *(out + offset)
4325 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4326 out,
4327 out_reg,
4328 offset,
4329 maybe_temp,
4330 /* needs_null_check */ false);
4331 } else {
4332 // Load with slow path based read barrier.
4333 // Save the value of `out` into `maybe_temp` before overwriting it
4334 // in the following move operation, as we will need it for the
4335 // read barrier below.
4336 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4337 // /* HeapReference<Object> */ out = *(out + offset)
4338 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4339 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4340 }
4341 } else {
4342 // Plain load with no read barrier.
4343 // /* HeapReference<Object> */ out = *(out + offset)
4344 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4345 __ MaybeUnpoisonHeapReference(out_reg);
4346 }
4347}
4348
4349void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4350 HInstruction* instruction,
4351 Location out,
4352 Location obj,
4353 uint32_t offset,
4354 Location maybe_temp,
4355 ReadBarrierOption read_barrier_option) {
4356 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4357 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4358 if (read_barrier_option == kWithReadBarrier) {
4359 CHECK(kEmitCompilerReadBarrier);
4360 if (kUseBakerReadBarrier) {
4361 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4362 // Load with fast path based Baker's read barrier.
4363 // /* HeapReference<Object> */ out = *(obj + offset)
4364 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4365 out,
4366 obj_reg,
4367 offset,
4368 maybe_temp,
4369 /* needs_null_check */ false);
4370 } else {
4371 // Load with slow path based read barrier.
4372 // /* HeapReference<Object> */ out = *(obj + offset)
4373 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4374 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4375 }
4376 } else {
4377 // Plain load with no read barrier.
4378 // /* HeapReference<Object> */ out = *(obj + offset)
4379 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4380 __ MaybeUnpoisonHeapReference(out_reg);
4381 }
4382}
4383
Alexey Frunzef63f5692016-12-13 17:43:11 -08004384void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
Alexey Frunze15958152017-02-09 19:08:30 -08004385 HInstruction* instruction,
Alexey Frunzef63f5692016-12-13 17:43:11 -08004386 Location root,
4387 GpuRegister obj,
Alexey Frunze15958152017-02-09 19:08:30 -08004388 uint32_t offset,
4389 ReadBarrierOption read_barrier_option) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004390 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004391 if (read_barrier_option == kWithReadBarrier) {
4392 DCHECK(kEmitCompilerReadBarrier);
4393 if (kUseBakerReadBarrier) {
4394 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4395 // Baker's read barrier are used:
4396 //
4397 // root = obj.field;
4398 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4399 // if (temp != null) {
4400 // root = temp(root)
4401 // }
4402
4403 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4404 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4405 static_assert(
4406 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4407 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4408 "have different sizes.");
4409 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4410 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4411 "have different sizes.");
4412
4413 // Slow path marking the GC root `root`.
4414 Location temp = Location::RegisterLocation(T9);
4415 SlowPathCodeMIPS64* slow_path =
4416 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4417 instruction,
4418 root,
4419 /*entrypoint*/ temp);
4420 codegen_->AddSlowPath(slow_path);
4421
4422 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4423 const int32_t entry_point_offset =
4424 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4425 // Loading the entrypoint does not require a load acquire since it is only changed when
4426 // threads are suspended or running a checkpoint.
4427 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4428 // The entrypoint is null when the GC is not marking, this prevents one load compared to
4429 // checking GetIsGcMarking.
4430 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4431 __ Bind(slow_path->GetExitLabel());
4432 } else {
4433 // GC root loaded through a slow path for read barriers other
4434 // than Baker's.
4435 // /* GcRoot<mirror::Object>* */ root = obj + offset
4436 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4437 // /* mirror::Object* */ root = root->Read()
4438 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4439 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004440 } else {
4441 // Plain GC root load with no read barrier.
4442 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4443 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4444 // Note that GC roots are not affected by heap poisoning, thus we
4445 // do not have to unpoison `root_reg` here.
4446 }
4447}
4448
Alexey Frunze15958152017-02-09 19:08:30 -08004449void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4450 Location ref,
4451 GpuRegister obj,
4452 uint32_t offset,
4453 Location temp,
4454 bool needs_null_check) {
4455 DCHECK(kEmitCompilerReadBarrier);
4456 DCHECK(kUseBakerReadBarrier);
4457
4458 // /* HeapReference<Object> */ ref = *(obj + offset)
4459 Location no_index = Location::NoLocation();
4460 ScaleFactor no_scale_factor = TIMES_1;
4461 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4462 ref,
4463 obj,
4464 offset,
4465 no_index,
4466 no_scale_factor,
4467 temp,
4468 needs_null_check);
4469}
4470
4471void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4472 Location ref,
4473 GpuRegister obj,
4474 uint32_t data_offset,
4475 Location index,
4476 Location temp,
4477 bool needs_null_check) {
4478 DCHECK(kEmitCompilerReadBarrier);
4479 DCHECK(kUseBakerReadBarrier);
4480
4481 static_assert(
4482 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4483 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4484 // /* HeapReference<Object> */ ref =
4485 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4486 ScaleFactor scale_factor = TIMES_4;
4487 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4488 ref,
4489 obj,
4490 data_offset,
4491 index,
4492 scale_factor,
4493 temp,
4494 needs_null_check);
4495}
4496
4497void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4498 Location ref,
4499 GpuRegister obj,
4500 uint32_t offset,
4501 Location index,
4502 ScaleFactor scale_factor,
4503 Location temp,
4504 bool needs_null_check,
4505 bool always_update_field) {
4506 DCHECK(kEmitCompilerReadBarrier);
4507 DCHECK(kUseBakerReadBarrier);
4508
4509 // In slow path based read barriers, the read barrier call is
4510 // inserted after the original load. However, in fast path based
4511 // Baker's read barriers, we need to perform the load of
4512 // mirror::Object::monitor_ *before* the original reference load.
4513 // This load-load ordering is required by the read barrier.
4514 // The fast path/slow path (for Baker's algorithm) should look like:
4515 //
4516 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4517 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4518 // HeapReference<Object> ref = *src; // Original reference load.
4519 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4520 // if (is_gray) {
4521 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4522 // }
4523 //
4524 // Note: the original implementation in ReadBarrier::Barrier is
4525 // slightly more complex as it performs additional checks that we do
4526 // not do here for performance reasons.
4527
4528 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4529 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4530 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4531
4532 // /* int32_t */ monitor = obj->monitor_
4533 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4534 if (needs_null_check) {
4535 MaybeRecordImplicitNullCheck(instruction);
4536 }
4537 // /* LockWord */ lock_word = LockWord(monitor)
4538 static_assert(sizeof(LockWord) == sizeof(int32_t),
4539 "art::LockWord and int32_t have different sizes.");
4540
4541 __ Sync(0); // Barrier to prevent load-load reordering.
4542
4543 // The actual reference load.
4544 if (index.IsValid()) {
4545 // Load types involving an "index": ArrayGet,
4546 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4547 // intrinsics.
4548 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4549 if (index.IsConstant()) {
4550 size_t computed_offset =
4551 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4552 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4553 } else {
4554 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004555 if (scale_factor == TIMES_1) {
4556 __ Daddu(TMP, index_reg, obj);
4557 } else {
4558 __ Dlsa(TMP, index_reg, obj, scale_factor);
4559 }
Alexey Frunze15958152017-02-09 19:08:30 -08004560 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4561 }
4562 } else {
4563 // /* HeapReference<Object> */ ref = *(obj + offset)
4564 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4565 }
4566
4567 // Object* ref = ref_addr->AsMirrorPtr()
4568 __ MaybeUnpoisonHeapReference(ref_reg);
4569
4570 // Slow path marking the object `ref` when it is gray.
4571 SlowPathCodeMIPS64* slow_path;
4572 if (always_update_field) {
4573 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4574 // of the form `obj + field_offset`, where `obj` is a register and
4575 // `field_offset` is a register. Thus `offset` and `scale_factor`
4576 // above are expected to be null in this code path.
4577 DCHECK_EQ(offset, 0u);
4578 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4579 slow_path = new (GetGraph()->GetArena())
4580 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4581 ref,
4582 obj,
4583 /* field_offset */ index,
4584 temp_reg);
4585 } else {
4586 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4587 }
4588 AddSlowPath(slow_path);
4589
4590 // if (rb_state == ReadBarrier::GrayState())
4591 // ref = ReadBarrier::Mark(ref);
4592 // Given the numeric representation, it's enough to check the low bit of the
4593 // rb_state. We do that by shifting the bit into the sign bit (31) and
4594 // performing a branch on less than zero.
4595 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4596 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4597 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4598 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4599 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4600 __ Bind(slow_path->GetExitLabel());
4601}
4602
4603void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4604 Location out,
4605 Location ref,
4606 Location obj,
4607 uint32_t offset,
4608 Location index) {
4609 DCHECK(kEmitCompilerReadBarrier);
4610
4611 // Insert a slow path based read barrier *after* the reference load.
4612 //
4613 // If heap poisoning is enabled, the unpoisoning of the loaded
4614 // reference will be carried out by the runtime within the slow
4615 // path.
4616 //
4617 // Note that `ref` currently does not get unpoisoned (when heap
4618 // poisoning is enabled), which is alright as the `ref` argument is
4619 // not used by the artReadBarrierSlow entry point.
4620 //
4621 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4622 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4623 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4624 AddSlowPath(slow_path);
4625
4626 __ Bc(slow_path->GetEntryLabel());
4627 __ Bind(slow_path->GetExitLabel());
4628}
4629
4630void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4631 Location out,
4632 Location ref,
4633 Location obj,
4634 uint32_t offset,
4635 Location index) {
4636 if (kEmitCompilerReadBarrier) {
4637 // Baker's read barriers shall be handled by the fast path
4638 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4639 DCHECK(!kUseBakerReadBarrier);
4640 // If heap poisoning is enabled, unpoisoning will be taken care of
4641 // by the runtime within the slow path.
4642 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4643 } else if (kPoisonHeapReferences) {
4644 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4645 }
4646}
4647
4648void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4649 Location out,
4650 Location root) {
4651 DCHECK(kEmitCompilerReadBarrier);
4652
4653 // Insert a slow path based read barrier *after* the GC root load.
4654 //
4655 // Note that GC roots are not affected by heap poisoning, so we do
4656 // not need to do anything special for this here.
4657 SlowPathCodeMIPS64* slow_path =
4658 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4659 AddSlowPath(slow_path);
4660
4661 __ Bc(slow_path->GetEntryLabel());
4662 __ Bind(slow_path->GetExitLabel());
4663}
4664
Alexey Frunze4dda3372015-06-01 18:31:49 -07004665void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004666 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4667 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004668 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004669 switch (type_check_kind) {
4670 case TypeCheckKind::kExactCheck:
4671 case TypeCheckKind::kAbstractClassCheck:
4672 case TypeCheckKind::kClassHierarchyCheck:
4673 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004674 call_kind =
4675 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004676 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004677 break;
4678 case TypeCheckKind::kArrayCheck:
4679 case TypeCheckKind::kUnresolvedCheck:
4680 case TypeCheckKind::kInterfaceCheck:
4681 call_kind = LocationSummary::kCallOnSlowPath;
4682 break;
4683 }
4684
Alexey Frunze4dda3372015-06-01 18:31:49 -07004685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004686 if (baker_read_barrier_slow_path) {
4687 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4688 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004689 locations->SetInAt(0, Location::RequiresRegister());
4690 locations->SetInAt(1, Location::RequiresRegister());
4691 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004692 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004693 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004694 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004695}
4696
4697void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004698 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004699 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004700 Location obj_loc = locations->InAt(0);
4701 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004702 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004703 Location out_loc = locations->Out();
4704 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4705 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4706 DCHECK_LE(num_temps, 1u);
4707 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004708 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4709 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4710 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4711 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004712 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004713 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004714
4715 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004716 // Avoid this check if we know `obj` is not null.
4717 if (instruction->MustDoNullCheck()) {
4718 __ Move(out, ZERO);
4719 __ Beqzc(obj, &done);
4720 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004721
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004722 switch (type_check_kind) {
4723 case TypeCheckKind::kExactCheck: {
4724 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004725 GenerateReferenceLoadTwoRegisters(instruction,
4726 out_loc,
4727 obj_loc,
4728 class_offset,
4729 maybe_temp_loc,
4730 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004731 // Classes must be equal for the instanceof to succeed.
4732 __ Xor(out, out, cls);
4733 __ Sltiu(out, out, 1);
4734 break;
4735 }
4736
4737 case TypeCheckKind::kAbstractClassCheck: {
4738 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004739 GenerateReferenceLoadTwoRegisters(instruction,
4740 out_loc,
4741 obj_loc,
4742 class_offset,
4743 maybe_temp_loc,
4744 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004745 // If the class is abstract, we eagerly fetch the super class of the
4746 // object to avoid doing a comparison we know will fail.
4747 Mips64Label loop;
4748 __ Bind(&loop);
4749 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004750 GenerateReferenceLoadOneRegister(instruction,
4751 out_loc,
4752 super_offset,
4753 maybe_temp_loc,
4754 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004755 // If `out` is null, we use it for the result, and jump to `done`.
4756 __ Beqzc(out, &done);
4757 __ Bnec(out, cls, &loop);
4758 __ LoadConst32(out, 1);
4759 break;
4760 }
4761
4762 case TypeCheckKind::kClassHierarchyCheck: {
4763 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004764 GenerateReferenceLoadTwoRegisters(instruction,
4765 out_loc,
4766 obj_loc,
4767 class_offset,
4768 maybe_temp_loc,
4769 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004770 // Walk over the class hierarchy to find a match.
4771 Mips64Label loop, success;
4772 __ Bind(&loop);
4773 __ Beqc(out, cls, &success);
4774 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004775 GenerateReferenceLoadOneRegister(instruction,
4776 out_loc,
4777 super_offset,
4778 maybe_temp_loc,
4779 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004780 __ Bnezc(out, &loop);
4781 // If `out` is null, we use it for the result, and jump to `done`.
4782 __ Bc(&done);
4783 __ Bind(&success);
4784 __ LoadConst32(out, 1);
4785 break;
4786 }
4787
4788 case TypeCheckKind::kArrayObjectCheck: {
4789 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004790 GenerateReferenceLoadTwoRegisters(instruction,
4791 out_loc,
4792 obj_loc,
4793 class_offset,
4794 maybe_temp_loc,
4795 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004796 // Do an exact check.
4797 Mips64Label success;
4798 __ Beqc(out, cls, &success);
4799 // Otherwise, we need to check that the object's class is a non-primitive array.
4800 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004801 GenerateReferenceLoadOneRegister(instruction,
4802 out_loc,
4803 component_offset,
4804 maybe_temp_loc,
4805 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004806 // If `out` is null, we use it for the result, and jump to `done`.
4807 __ Beqzc(out, &done);
4808 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4809 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4810 __ Sltiu(out, out, 1);
4811 __ Bc(&done);
4812 __ Bind(&success);
4813 __ LoadConst32(out, 1);
4814 break;
4815 }
4816
4817 case TypeCheckKind::kArrayCheck: {
4818 // No read barrier since the slow path will retry upon failure.
4819 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004820 GenerateReferenceLoadTwoRegisters(instruction,
4821 out_loc,
4822 obj_loc,
4823 class_offset,
4824 maybe_temp_loc,
4825 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004826 DCHECK(locations->OnlyCallsOnSlowPath());
4827 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4828 /* is_fatal */ false);
4829 codegen_->AddSlowPath(slow_path);
4830 __ Bnec(out, cls, slow_path->GetEntryLabel());
4831 __ LoadConst32(out, 1);
4832 break;
4833 }
4834
4835 case TypeCheckKind::kUnresolvedCheck:
4836 case TypeCheckKind::kInterfaceCheck: {
4837 // Note that we indeed only call on slow path, but we always go
4838 // into the slow path for the unresolved and interface check
4839 // cases.
4840 //
4841 // We cannot directly call the InstanceofNonTrivial runtime
4842 // entry point without resorting to a type checking slow path
4843 // here (i.e. by calling InvokeRuntime directly), as it would
4844 // require to assign fixed registers for the inputs of this
4845 // HInstanceOf instruction (following the runtime calling
4846 // convention), which might be cluttered by the potential first
4847 // read barrier emission at the beginning of this method.
4848 //
4849 // TODO: Introduce a new runtime entry point taking the object
4850 // to test (instead of its class) as argument, and let it deal
4851 // with the read barrier issues. This will let us refactor this
4852 // case of the `switch` code as it was previously (with a direct
4853 // call to the runtime not using a type checking slow path).
4854 // This should also be beneficial for the other cases above.
4855 DCHECK(locations->OnlyCallsOnSlowPath());
4856 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4857 /* is_fatal */ false);
4858 codegen_->AddSlowPath(slow_path);
4859 __ Bc(slow_path->GetEntryLabel());
4860 break;
4861 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004862 }
4863
4864 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004865
4866 if (slow_path != nullptr) {
4867 __ Bind(slow_path->GetExitLabel());
4868 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004869}
4870
4871void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4873 locations->SetOut(Location::ConstantLocation(constant));
4874}
4875
4876void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4877 // Will be generated at use site.
4878}
4879
4880void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4881 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4882 locations->SetOut(Location::ConstantLocation(constant));
4883}
4884
4885void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4886 // Will be generated at use site.
4887}
4888
Calin Juravle175dc732015-08-25 15:42:32 +01004889void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4890 // The trampoline uses the same calling convention as dex calling conventions,
4891 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4892 // the method_idx.
4893 HandleInvoke(invoke);
4894}
4895
4896void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4897 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4898}
4899
Alexey Frunze4dda3372015-06-01 18:31:49 -07004900void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4901 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4902 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4903}
4904
4905void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4906 HandleInvoke(invoke);
4907 // The register T0 is required to be used for the hidden argument in
4908 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4909 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4910}
4911
4912void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4913 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4914 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004915 Location receiver = invoke->GetLocations()->InAt(0);
4916 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004917 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004918
4919 // Set the hidden argument.
4920 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4921 invoke->GetDexMethodIndex());
4922
4923 // temp = object->GetClass();
4924 if (receiver.IsStackSlot()) {
4925 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4926 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4927 } else {
4928 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4929 }
4930 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004931 // Instead of simply (possibly) unpoisoning `temp` here, we should
4932 // emit a read barrier for the previous class reference load.
4933 // However this is not required in practice, as this is an
4934 // intermediate/temporary reference and because the current
4935 // concurrent copying collector keeps the from-space memory
4936 // intact/accessible until the end of the marking phase (the
4937 // concurrent copying collector may not in the future).
4938 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004939 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4940 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4941 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004942 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004943 // temp = temp->GetImtEntryAt(method_offset);
4944 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4945 // T9 = temp->GetEntryPoint();
4946 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4947 // T9();
4948 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004949 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004950 DCHECK(!codegen_->IsLeafMethod());
4951 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4952}
4953
4954void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004955 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4956 if (intrinsic.TryDispatch(invoke)) {
4957 return;
4958 }
4959
Alexey Frunze4dda3372015-06-01 18:31:49 -07004960 HandleInvoke(invoke);
4961}
4962
4963void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004964 // Explicit clinit checks triggered by static invokes must have been pruned by
4965 // art::PrepareForRegisterAllocation.
4966 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004967
Chris Larsen3039e382015-08-26 07:54:08 -07004968 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4969 if (intrinsic.TryDispatch(invoke)) {
4970 return;
4971 }
4972
Alexey Frunze4dda3372015-06-01 18:31:49 -07004973 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004974}
4975
Orion Hodsonac141392017-01-13 11:53:47 +00004976void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4977 HandleInvoke(invoke);
4978}
4979
4980void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4981 codegen_->GenerateInvokePolymorphicCall(invoke);
4982}
4983
Chris Larsen3039e382015-08-26 07:54:08 -07004984static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004985 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004986 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4987 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004988 return true;
4989 }
4990 return false;
4991}
4992
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004993HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004994 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004995 bool fallback_load = false;
4996 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004997 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Alexey Frunzef63f5692016-12-13 17:43:11 -08004998 case HLoadString::LoadKind::kBssEntry:
4999 DCHECK(!Runtime::Current()->UseJitCompilation());
5000 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005001 case HLoadString::LoadKind::kJitTableAddress:
5002 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005003 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005004 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005005 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005006 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005007 }
5008 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005009 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005010 }
5011 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005012}
5013
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005014HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5015 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005016 bool fallback_load = false;
5017 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005018 case HLoadClass::LoadKind::kInvalid:
5019 LOG(FATAL) << "UNREACHABLE";
5020 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005021 case HLoadClass::LoadKind::kReferrersClass:
5022 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005023 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005024 case HLoadClass::LoadKind::kBssEntry:
5025 DCHECK(!Runtime::Current()->UseJitCompilation());
5026 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005027 case HLoadClass::LoadKind::kJitTableAddress:
5028 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005029 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005030 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005031 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005032 break;
5033 }
5034 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005035 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005036 }
5037 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005038}
5039
Vladimir Markodc151b22015-10-15 18:02:30 +01005040HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5041 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005042 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005043 // On MIPS64 we support all dispatch types.
5044 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005045}
5046
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005047void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5048 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005049 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005050 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005051 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5052 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5053
Alexey Frunze19f6c692016-11-30 19:19:55 -08005054 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005055 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005056 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005057 uint32_t offset =
5058 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005059 __ LoadFromOffset(kLoadDoubleword,
5060 temp.AsRegister<GpuRegister>(),
5061 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005062 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005063 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005064 }
Vladimir Marko58155012015-08-19 12:49:41 +00005065 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005066 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005067 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005068 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5069 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005070 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005071 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005072 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5073 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5074 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005075 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5076 break;
5077 }
Vladimir Marko58155012015-08-19 12:49:41 +00005078 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005079 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5080 kLoadDoubleword,
5081 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005082 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005083 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005084 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005085 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005086 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5087 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5088 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005089 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5090 break;
5091 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005092 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5093 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5094 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005095 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005096 }
5097
Alexey Frunze19f6c692016-11-30 19:19:55 -08005098 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005099 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005100 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005101 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005102 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5103 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5104 __ LoadFromOffset(kLoadDoubleword,
5105 T9,
5106 callee_method.AsRegister<GpuRegister>(),
5107 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005108 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005109 // T9()
5110 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005111 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005112 break;
5113 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005114 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5115
Alexey Frunze4dda3372015-06-01 18:31:49 -07005116 DCHECK(!IsLeafMethod());
5117}
5118
5119void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005120 // Explicit clinit checks triggered by static invokes must have been pruned by
5121 // art::PrepareForRegisterAllocation.
5122 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005123
5124 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5125 return;
5126 }
5127
5128 LocationSummary* locations = invoke->GetLocations();
5129 codegen_->GenerateStaticOrDirectCall(invoke,
5130 locations->HasTemps()
5131 ? locations->GetTemp(0)
5132 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005133}
5134
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005135void CodeGeneratorMIPS64::GenerateVirtualCall(
5136 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005137 // Use the calling convention instead of the location of the receiver, as
5138 // intrinsics may have put the receiver in a different register. In the intrinsics
5139 // slow path, the arguments have been moved to the right place, so here we are
5140 // guaranteed that the receiver is the first register of the calling convention.
5141 InvokeDexCallingConvention calling_convention;
5142 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5143
Alexey Frunze53afca12015-11-05 16:34:23 -08005144 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005145 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5146 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5147 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005148 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005149
5150 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005151 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005152 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005153 // Instead of simply (possibly) unpoisoning `temp` here, we should
5154 // emit a read barrier for the previous class reference load.
5155 // However this is not required in practice, as this is an
5156 // intermediate/temporary reference and because the current
5157 // concurrent copying collector keeps the from-space memory
5158 // intact/accessible until the end of the marking phase (the
5159 // concurrent copying collector may not in the future).
5160 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005161 // temp = temp->GetMethodAt(method_offset);
5162 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5163 // T9 = temp->GetEntryPoint();
5164 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5165 // T9();
5166 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005167 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005168 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005169}
5170
5171void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5172 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5173 return;
5174 }
5175
5176 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005177 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005178}
5179
5180void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005181 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005182 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005183 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005184 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5185 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005186 return;
5187 }
Vladimir Marko41559982017-01-06 14:04:23 +00005188 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005189
Alexey Frunze15958152017-02-09 19:08:30 -08005190 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5191 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005192 ? LocationSummary::kCallOnSlowPath
5193 : LocationSummary::kNoCall;
5194 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005195 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5196 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5197 }
Vladimir Marko41559982017-01-06 14:04:23 +00005198 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005199 locations->SetInAt(0, Location::RequiresRegister());
5200 }
5201 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005202 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5203 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5204 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005205 // Request a temp to hold the BSS entry location for the slow path.
5206 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005207 RegisterSet caller_saves = RegisterSet::Empty();
5208 InvokeRuntimeCallingConvention calling_convention;
5209 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5210 locations->SetCustomSlowPathCallerSaves(caller_saves);
5211 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005212 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005213 }
5214 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005215}
5216
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005217// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5218// move.
5219void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005220 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005221 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005222 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005223 return;
5224 }
Vladimir Marko41559982017-01-06 14:04:23 +00005225 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005226
Vladimir Marko41559982017-01-06 14:04:23 +00005227 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005228 Location out_loc = locations->Out();
5229 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5230 GpuRegister current_method_reg = ZERO;
5231 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005232 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005233 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5234 }
5235
Alexey Frunze15958152017-02-09 19:08:30 -08005236 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5237 ? kWithoutReadBarrier
5238 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005239 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005240 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005241 switch (load_kind) {
5242 case HLoadClass::LoadKind::kReferrersClass:
5243 DCHECK(!cls->CanCallRuntime());
5244 DCHECK(!cls->MustGenerateClinitCheck());
5245 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5246 GenerateGcRootFieldLoad(cls,
5247 out_loc,
5248 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005249 ArtMethod::DeclaringClassOffset().Int32Value(),
5250 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005251 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005252 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005253 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005254 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005255 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08005256 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005257 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5258 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
5259 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005260 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5261 break;
5262 }
5263 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005264 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005265 uint32_t address = dchecked_integral_cast<uint32_t>(
5266 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5267 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005268 __ LoadLiteral(out,
5269 kLoadUnsignedWord,
5270 codegen_->DeduplicateBootImageAddressLiteral(address));
5271 break;
5272 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005273 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005274 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
5275 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5276 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
5277 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
5278 GpuRegister temp = non_baker_read_barrier
5279 ? out
5280 : locations->GetTemp(0).AsRegister<GpuRegister>();
5281 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp, info_low);
5282 GenerateGcRootFieldLoad(cls, out_loc, temp, /* placeholder */ 0x5678, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005283 generate_null_check = true;
5284 break;
5285 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005286 case HLoadClass::LoadKind::kJitTableAddress:
5287 __ LoadLiteral(out,
5288 kLoadUnsignedWord,
5289 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5290 cls->GetTypeIndex(),
5291 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005292 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005293 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005294 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005295 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005296 LOG(FATAL) << "UNREACHABLE";
5297 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005298 }
5299
5300 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5301 DCHECK(cls->CanCallRuntime());
5302 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005303 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005304 codegen_->AddSlowPath(slow_path);
5305 if (generate_null_check) {
5306 __ Beqzc(out, slow_path->GetEntryLabel());
5307 }
5308 if (cls->MustGenerateClinitCheck()) {
5309 GenerateClassInitializationCheck(slow_path, out);
5310 } else {
5311 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005312 }
5313 }
5314}
5315
David Brazdilcb1c0552015-08-04 16:22:25 +01005316static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005317 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005318}
5319
Alexey Frunze4dda3372015-06-01 18:31:49 -07005320void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5321 LocationSummary* locations =
5322 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5323 locations->SetOut(Location::RequiresRegister());
5324}
5325
5326void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5327 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005328 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5329}
5330
5331void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5332 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5333}
5334
5335void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5336 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005337}
5338
Alexey Frunze4dda3372015-06-01 18:31:49 -07005339void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005340 HLoadString::LoadKind load_kind = load->GetLoadKind();
5341 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005343 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005344 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005345 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005346 } else {
5347 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005348 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5349 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5350 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005351 // Request a temp to hold the BSS entry location for the slow path.
5352 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005353 RegisterSet caller_saves = RegisterSet::Empty();
5354 InvokeRuntimeCallingConvention calling_convention;
5355 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5356 locations->SetCustomSlowPathCallerSaves(caller_saves);
5357 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005358 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005359 }
5360 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005361 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005362}
5363
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005364// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5365// move.
5366void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005367 HLoadString::LoadKind load_kind = load->GetLoadKind();
5368 LocationSummary* locations = load->GetLocations();
5369 Location out_loc = locations->Out();
5370 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5371
5372 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005373 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5374 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005375 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005376 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005377 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5378 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
5379 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005380 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5381 return; // No dex cache slow path.
5382 }
5383 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005384 uint32_t address = dchecked_integral_cast<uint32_t>(
5385 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5386 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005387 __ LoadLiteral(out,
5388 kLoadUnsignedWord,
5389 codegen_->DeduplicateBootImageAddressLiteral(address));
5390 return; // No dex cache slow path.
5391 }
5392 case HLoadString::LoadKind::kBssEntry: {
5393 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005394 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005395 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005396 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5397 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
5398 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
5399 GpuRegister temp = non_baker_read_barrier
5400 ? out
5401 : locations->GetTemp(0).AsRegister<GpuRegister>();
5402 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp, info_low);
Alexey Frunze15958152017-02-09 19:08:30 -08005403 GenerateGcRootFieldLoad(load,
5404 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005405 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08005406 /* placeholder */ 0x5678,
5407 kCompilerReadBarrierOption);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005408 SlowPathCodeMIPS64* slow_path =
5409 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005410 codegen_->AddSlowPath(slow_path);
5411 __ Beqzc(out, slow_path->GetEntryLabel());
5412 __ Bind(slow_path->GetExitLabel());
5413 return;
5414 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005415 case HLoadString::LoadKind::kJitTableAddress:
5416 __ LoadLiteral(out,
5417 kLoadUnsignedWord,
5418 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5419 load->GetStringIndex(),
5420 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005421 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005422 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005423 default:
5424 break;
5425 }
5426
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005427 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005428 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005429 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005430 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005431 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5432 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5433 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005434}
5435
Alexey Frunze4dda3372015-06-01 18:31:49 -07005436void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5438 locations->SetOut(Location::ConstantLocation(constant));
5439}
5440
5441void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5442 // Will be generated at use site.
5443}
5444
5445void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5446 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005447 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005448 InvokeRuntimeCallingConvention calling_convention;
5449 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5450}
5451
5452void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005453 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005454 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005455 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005456 if (instruction->IsEnter()) {
5457 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5458 } else {
5459 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5460 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005461}
5462
5463void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5464 LocationSummary* locations =
5465 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5466 switch (mul->GetResultType()) {
5467 case Primitive::kPrimInt:
5468 case Primitive::kPrimLong:
5469 locations->SetInAt(0, Location::RequiresRegister());
5470 locations->SetInAt(1, Location::RequiresRegister());
5471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5472 break;
5473
5474 case Primitive::kPrimFloat:
5475 case Primitive::kPrimDouble:
5476 locations->SetInAt(0, Location::RequiresFpuRegister());
5477 locations->SetInAt(1, Location::RequiresFpuRegister());
5478 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5479 break;
5480
5481 default:
5482 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5483 }
5484}
5485
5486void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5487 Primitive::Type type = instruction->GetType();
5488 LocationSummary* locations = instruction->GetLocations();
5489
5490 switch (type) {
5491 case Primitive::kPrimInt:
5492 case Primitive::kPrimLong: {
5493 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5494 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5495 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5496 if (type == Primitive::kPrimInt)
5497 __ MulR6(dst, lhs, rhs);
5498 else
5499 __ Dmul(dst, lhs, rhs);
5500 break;
5501 }
5502 case Primitive::kPrimFloat:
5503 case Primitive::kPrimDouble: {
5504 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5505 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5506 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5507 if (type == Primitive::kPrimFloat)
5508 __ MulS(dst, lhs, rhs);
5509 else
5510 __ MulD(dst, lhs, rhs);
5511 break;
5512 }
5513 default:
5514 LOG(FATAL) << "Unexpected mul type " << type;
5515 }
5516}
5517
5518void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5519 LocationSummary* locations =
5520 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5521 switch (neg->GetResultType()) {
5522 case Primitive::kPrimInt:
5523 case Primitive::kPrimLong:
5524 locations->SetInAt(0, Location::RequiresRegister());
5525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5526 break;
5527
5528 case Primitive::kPrimFloat:
5529 case Primitive::kPrimDouble:
5530 locations->SetInAt(0, Location::RequiresFpuRegister());
5531 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5532 break;
5533
5534 default:
5535 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5536 }
5537}
5538
5539void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5540 Primitive::Type type = instruction->GetType();
5541 LocationSummary* locations = instruction->GetLocations();
5542
5543 switch (type) {
5544 case Primitive::kPrimInt:
5545 case Primitive::kPrimLong: {
5546 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5547 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5548 if (type == Primitive::kPrimInt)
5549 __ Subu(dst, ZERO, src);
5550 else
5551 __ Dsubu(dst, ZERO, src);
5552 break;
5553 }
5554 case Primitive::kPrimFloat:
5555 case Primitive::kPrimDouble: {
5556 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5557 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5558 if (type == Primitive::kPrimFloat)
5559 __ NegS(dst, src);
5560 else
5561 __ NegD(dst, src);
5562 break;
5563 }
5564 default:
5565 LOG(FATAL) << "Unexpected neg type " << type;
5566 }
5567}
5568
5569void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5570 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005571 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005572 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005573 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005574 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5575 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005576}
5577
5578void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005579 // Note: if heap poisoning is enabled, the entry point takes care
5580 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005581 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5582 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005583}
5584
5585void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5586 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005588 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005589 if (instruction->IsStringAlloc()) {
5590 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5591 } else {
5592 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005593 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005594 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5595}
5596
5597void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005598 // Note: if heap poisoning is enabled, the entry point takes care
5599 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005600 if (instruction->IsStringAlloc()) {
5601 // String is allocated through StringFactory. Call NewEmptyString entry point.
5602 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005603 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005604 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005605 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5606 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5607 __ Jalr(T9);
5608 __ Nop();
5609 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5610 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005611 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005612 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005613 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005614}
5615
5616void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5618 locations->SetInAt(0, Location::RequiresRegister());
5619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5620}
5621
5622void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5623 Primitive::Type type = instruction->GetType();
5624 LocationSummary* locations = instruction->GetLocations();
5625
5626 switch (type) {
5627 case Primitive::kPrimInt:
5628 case Primitive::kPrimLong: {
5629 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5630 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5631 __ Nor(dst, src, ZERO);
5632 break;
5633 }
5634
5635 default:
5636 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5637 }
5638}
5639
5640void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5642 locations->SetInAt(0, Location::RequiresRegister());
5643 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5644}
5645
5646void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5647 LocationSummary* locations = instruction->GetLocations();
5648 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5649 locations->InAt(0).AsRegister<GpuRegister>(),
5650 1);
5651}
5652
5653void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005654 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5655 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005656}
5657
Calin Juravle2ae48182016-03-16 14:05:09 +00005658void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5659 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005660 return;
5661 }
5662 Location obj = instruction->GetLocations()->InAt(0);
5663
5664 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005665 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005666}
5667
Calin Juravle2ae48182016-03-16 14:05:09 +00005668void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005669 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005670 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005671
5672 Location obj = instruction->GetLocations()->InAt(0);
5673
5674 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5675}
5676
5677void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005678 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005679}
5680
5681void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5682 HandleBinaryOp(instruction);
5683}
5684
5685void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5686 HandleBinaryOp(instruction);
5687}
5688
5689void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5690 LOG(FATAL) << "Unreachable";
5691}
5692
5693void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5694 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5695}
5696
5697void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5698 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5699 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5700 if (location.IsStackSlot()) {
5701 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5702 } else if (location.IsDoubleStackSlot()) {
5703 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5704 }
5705 locations->SetOut(location);
5706}
5707
5708void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5709 ATTRIBUTE_UNUSED) {
5710 // Nothing to do, the parameter is already at its location.
5711}
5712
5713void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5714 LocationSummary* locations =
5715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5716 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5717}
5718
5719void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5720 ATTRIBUTE_UNUSED) {
5721 // Nothing to do, the method is already at its location.
5722}
5723
5724void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5725 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005726 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005727 locations->SetInAt(i, Location::Any());
5728 }
5729 locations->SetOut(Location::Any());
5730}
5731
5732void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5733 LOG(FATAL) << "Unreachable";
5734}
5735
5736void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5737 Primitive::Type type = rem->GetResultType();
5738 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005739 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5740 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005741 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5742
5743 switch (type) {
5744 case Primitive::kPrimInt:
5745 case Primitive::kPrimLong:
5746 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005747 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5749 break;
5750
5751 case Primitive::kPrimFloat:
5752 case Primitive::kPrimDouble: {
5753 InvokeRuntimeCallingConvention calling_convention;
5754 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5755 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5756 locations->SetOut(calling_convention.GetReturnLocation(type));
5757 break;
5758 }
5759
5760 default:
5761 LOG(FATAL) << "Unexpected rem type " << type;
5762 }
5763}
5764
5765void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
5766 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005767
5768 switch (type) {
5769 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07005770 case Primitive::kPrimLong:
5771 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005772 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005773
5774 case Primitive::kPrimFloat:
5775 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01005776 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5777 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005778 if (type == Primitive::kPrimFloat) {
5779 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5780 } else {
5781 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5782 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005783 break;
5784 }
5785 default:
5786 LOG(FATAL) << "Unexpected rem type " << type;
5787 }
5788}
5789
Igor Murashkind01745e2017-04-05 16:40:31 -07005790void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5791 constructor_fence->SetLocations(nullptr);
5792}
5793
5794void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
5795 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5796 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5797}
5798
Alexey Frunze4dda3372015-06-01 18:31:49 -07005799void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5800 memory_barrier->SetLocations(nullptr);
5801}
5802
5803void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5804 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5805}
5806
5807void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
5808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5809 Primitive::Type return_type = ret->InputAt(0)->GetType();
5810 locations->SetInAt(0, Mips64ReturnLocation(return_type));
5811}
5812
5813void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5814 codegen_->GenerateFrameExit();
5815}
5816
5817void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
5818 ret->SetLocations(nullptr);
5819}
5820
5821void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5822 codegen_->GenerateFrameExit();
5823}
5824
Alexey Frunze92d90602015-12-18 18:16:36 -08005825void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
5826 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005827}
5828
Alexey Frunze92d90602015-12-18 18:16:36 -08005829void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
5830 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005831}
5832
Alexey Frunze4dda3372015-06-01 18:31:49 -07005833void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
5834 HandleShift(shl);
5835}
5836
5837void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
5838 HandleShift(shl);
5839}
5840
5841void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
5842 HandleShift(shr);
5843}
5844
5845void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
5846 HandleShift(shr);
5847}
5848
Alexey Frunze4dda3372015-06-01 18:31:49 -07005849void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
5850 HandleBinaryOp(instruction);
5851}
5852
5853void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
5854 HandleBinaryOp(instruction);
5855}
5856
5857void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5858 HandleFieldGet(instruction, instruction->GetFieldInfo());
5859}
5860
5861void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5862 HandleFieldGet(instruction, instruction->GetFieldInfo());
5863}
5864
5865void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5866 HandleFieldSet(instruction, instruction->GetFieldInfo());
5867}
5868
5869void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005870 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005871}
5872
Calin Juravlee460d1d2015-09-29 04:52:17 +01005873void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
5874 HUnresolvedInstanceFieldGet* instruction) {
5875 FieldAccessCallingConventionMIPS64 calling_convention;
5876 codegen_->CreateUnresolvedFieldLocationSummary(
5877 instruction, instruction->GetFieldType(), calling_convention);
5878}
5879
5880void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
5881 HUnresolvedInstanceFieldGet* instruction) {
5882 FieldAccessCallingConventionMIPS64 calling_convention;
5883 codegen_->GenerateUnresolvedFieldAccess(instruction,
5884 instruction->GetFieldType(),
5885 instruction->GetFieldIndex(),
5886 instruction->GetDexPc(),
5887 calling_convention);
5888}
5889
5890void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
5891 HUnresolvedInstanceFieldSet* instruction) {
5892 FieldAccessCallingConventionMIPS64 calling_convention;
5893 codegen_->CreateUnresolvedFieldLocationSummary(
5894 instruction, instruction->GetFieldType(), calling_convention);
5895}
5896
5897void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
5898 HUnresolvedInstanceFieldSet* instruction) {
5899 FieldAccessCallingConventionMIPS64 calling_convention;
5900 codegen_->GenerateUnresolvedFieldAccess(instruction,
5901 instruction->GetFieldType(),
5902 instruction->GetFieldIndex(),
5903 instruction->GetDexPc(),
5904 calling_convention);
5905}
5906
5907void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
5908 HUnresolvedStaticFieldGet* instruction) {
5909 FieldAccessCallingConventionMIPS64 calling_convention;
5910 codegen_->CreateUnresolvedFieldLocationSummary(
5911 instruction, instruction->GetFieldType(), calling_convention);
5912}
5913
5914void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
5915 HUnresolvedStaticFieldGet* instruction) {
5916 FieldAccessCallingConventionMIPS64 calling_convention;
5917 codegen_->GenerateUnresolvedFieldAccess(instruction,
5918 instruction->GetFieldType(),
5919 instruction->GetFieldIndex(),
5920 instruction->GetDexPc(),
5921 calling_convention);
5922}
5923
5924void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
5925 HUnresolvedStaticFieldSet* instruction) {
5926 FieldAccessCallingConventionMIPS64 calling_convention;
5927 codegen_->CreateUnresolvedFieldLocationSummary(
5928 instruction, instruction->GetFieldType(), calling_convention);
5929}
5930
5931void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
5932 HUnresolvedStaticFieldSet* instruction) {
5933 FieldAccessCallingConventionMIPS64 calling_convention;
5934 codegen_->GenerateUnresolvedFieldAccess(instruction,
5935 instruction->GetFieldType(),
5936 instruction->GetFieldIndex(),
5937 instruction->GetDexPc(),
5938 calling_convention);
5939}
5940
Alexey Frunze4dda3372015-06-01 18:31:49 -07005941void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005942 LocationSummary* locations =
5943 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02005944 // In suspend check slow path, usually there are no caller-save registers at all.
5945 // If SIMD instructions are present, however, we force spilling all live SIMD
5946 // registers in full width (since the runtime only saves/restores lower part).
5947 locations->SetCustomSlowPathCallerSaves(
5948 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005949}
5950
5951void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
5952 HBasicBlock* block = instruction->GetBlock();
5953 if (block->GetLoopInformation() != nullptr) {
5954 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5955 // The back edge will generate the suspend check.
5956 return;
5957 }
5958 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5959 // The goto will generate the suspend check.
5960 return;
5961 }
5962 GenerateSuspendCheck(instruction, nullptr);
5963}
5964
Alexey Frunze4dda3372015-06-01 18:31:49 -07005965void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
5966 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005968 InvokeRuntimeCallingConvention calling_convention;
5969 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5970}
5971
5972void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005973 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005974 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5975}
5976
5977void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5978 Primitive::Type input_type = conversion->GetInputType();
5979 Primitive::Type result_type = conversion->GetResultType();
5980 DCHECK_NE(input_type, result_type);
5981
5982 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5983 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5984 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5985 }
5986
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
5988
5989 if (Primitive::IsFloatingPointType(input_type)) {
5990 locations->SetInAt(0, Location::RequiresFpuRegister());
5991 } else {
5992 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005993 }
5994
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005995 if (Primitive::IsFloatingPointType(result_type)) {
5996 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005997 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005998 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005999 }
6000}
6001
6002void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6003 LocationSummary* locations = conversion->GetLocations();
6004 Primitive::Type result_type = conversion->GetResultType();
6005 Primitive::Type input_type = conversion->GetInputType();
6006
6007 DCHECK_NE(input_type, result_type);
6008
6009 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6010 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6011 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6012
6013 switch (result_type) {
6014 case Primitive::kPrimChar:
6015 __ Andi(dst, src, 0xFFFF);
6016 break;
6017 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006018 if (input_type == Primitive::kPrimLong) {
6019 // Type conversion from long to types narrower than int is a result of code
6020 // transformations. To avoid unpredictable results for SEB and SEH, we first
6021 // need to sign-extend the low 32-bit value into bits 32 through 63.
6022 __ Sll(dst, src, 0);
6023 __ Seb(dst, dst);
6024 } else {
6025 __ Seb(dst, src);
6026 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006027 break;
6028 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006029 if (input_type == Primitive::kPrimLong) {
6030 // Type conversion from long to types narrower than int is a result of code
6031 // transformations. To avoid unpredictable results for SEB and SEH, we first
6032 // need to sign-extend the low 32-bit value into bits 32 through 63.
6033 __ Sll(dst, src, 0);
6034 __ Seh(dst, dst);
6035 } else {
6036 __ Seh(dst, src);
6037 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006038 break;
6039 case Primitive::kPrimInt:
6040 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006041 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6042 // conversions, except when the input and output registers are the same and we are not
6043 // converting longs to shorter types. In these cases, do nothing.
6044 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
6045 __ Sll(dst, src, 0);
6046 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006047 break;
6048
6049 default:
6050 LOG(FATAL) << "Unexpected type conversion from " << input_type
6051 << " to " << result_type;
6052 }
6053 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006054 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6055 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6056 if (input_type == Primitive::kPrimLong) {
6057 __ Dmtc1(src, FTMP);
6058 if (result_type == Primitive::kPrimFloat) {
6059 __ Cvtsl(dst, FTMP);
6060 } else {
6061 __ Cvtdl(dst, FTMP);
6062 }
6063 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006064 __ Mtc1(src, FTMP);
6065 if (result_type == Primitive::kPrimFloat) {
6066 __ Cvtsw(dst, FTMP);
6067 } else {
6068 __ Cvtdw(dst, FTMP);
6069 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006070 }
6071 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6072 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006073 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6074 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006075
6076 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006077 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006078 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006079 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006080 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006081 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006082 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006083 } else {
6084 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006085 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006086 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006087 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006088 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006089 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006090 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006091 } else if (Primitive::IsFloatingPointType(result_type) &&
6092 Primitive::IsFloatingPointType(input_type)) {
6093 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6094 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6095 if (result_type == Primitive::kPrimFloat) {
6096 __ Cvtsd(dst, src);
6097 } else {
6098 __ Cvtds(dst, src);
6099 }
6100 } else {
6101 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6102 << " to " << result_type;
6103 }
6104}
6105
6106void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6107 HandleShift(ushr);
6108}
6109
6110void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6111 HandleShift(ushr);
6112}
6113
6114void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6115 HandleBinaryOp(instruction);
6116}
6117
6118void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6119 HandleBinaryOp(instruction);
6120}
6121
6122void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6123 // Nothing to do, this should be removed during prepare for register allocator.
6124 LOG(FATAL) << "Unreachable";
6125}
6126
6127void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6128 // Nothing to do, this should be removed during prepare for register allocator.
6129 LOG(FATAL) << "Unreachable";
6130}
6131
6132void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006133 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006134}
6135
6136void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006137 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006138}
6139
6140void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006141 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006142}
6143
6144void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006145 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006146}
6147
6148void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006149 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006150}
6151
6152void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006153 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006154}
6155
6156void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006157 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006158}
6159
6160void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006161 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006162}
6163
6164void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006165 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006166}
6167
6168void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006169 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006170}
6171
6172void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006173 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006174}
6175
6176void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006177 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006178}
6179
Aart Bike9f37602015-10-09 11:15:55 -07006180void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006181 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006182}
6183
6184void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006185 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006186}
6187
6188void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006189 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006190}
6191
6192void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006193 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006194}
6195
6196void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006197 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006198}
6199
6200void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006201 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006202}
6203
6204void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006205 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006206}
6207
6208void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006209 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006210}
6211
Mark Mendellfe57faa2015-09-18 09:26:15 -04006212// Simple implementation of packed switch - generate cascaded compare/jumps.
6213void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6214 LocationSummary* locations =
6215 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6216 locations->SetInAt(0, Location::RequiresRegister());
6217}
6218
Alexey Frunze0960ac52016-12-20 17:24:59 -08006219void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6220 int32_t lower_bound,
6221 uint32_t num_entries,
6222 HBasicBlock* switch_block,
6223 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006224 // Create a set of compare/jumps.
6225 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006226 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006227 // Jump to default if index is negative
6228 // Note: We don't check the case that index is positive while value < lower_bound, because in
6229 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6230 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6231
Alexey Frunze0960ac52016-12-20 17:24:59 -08006232 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006233 // Jump to successors[0] if value == lower_bound.
6234 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6235 int32_t last_index = 0;
6236 for (; num_entries - last_index > 2; last_index += 2) {
6237 __ Addiu(temp_reg, temp_reg, -2);
6238 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6239 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6240 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6241 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6242 }
6243 if (num_entries - last_index == 2) {
6244 // The last missing case_value.
6245 __ Addiu(temp_reg, temp_reg, -1);
6246 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006247 }
6248
6249 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006250 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006251 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006252 }
6253}
6254
Alexey Frunze0960ac52016-12-20 17:24:59 -08006255void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6256 int32_t lower_bound,
6257 uint32_t num_entries,
6258 HBasicBlock* switch_block,
6259 HBasicBlock* default_block) {
6260 // Create a jump table.
6261 std::vector<Mips64Label*> labels(num_entries);
6262 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6263 for (uint32_t i = 0; i < num_entries; i++) {
6264 labels[i] = codegen_->GetLabelOf(successors[i]);
6265 }
6266 JumpTable* table = __ CreateJumpTable(std::move(labels));
6267
6268 // Is the value in range?
6269 __ Addiu32(TMP, value_reg, -lower_bound);
6270 __ LoadConst32(AT, num_entries);
6271 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6272
6273 // We are in the range of the table.
6274 // Load the target address from the jump table, indexing by the value.
6275 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006276 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006277 __ Lw(TMP, TMP, 0);
6278 // Compute the absolute target address by adding the table start address
6279 // (the table contains offsets to targets relative to its start).
6280 __ Daddu(TMP, TMP, AT);
6281 // And jump.
6282 __ Jr(TMP);
6283 __ Nop();
6284}
6285
6286void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6287 int32_t lower_bound = switch_instr->GetStartValue();
6288 uint32_t num_entries = switch_instr->GetNumEntries();
6289 LocationSummary* locations = switch_instr->GetLocations();
6290 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6291 HBasicBlock* switch_block = switch_instr->GetBlock();
6292 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6293
6294 if (num_entries > kPackedSwitchJumpTableThreshold) {
6295 GenTableBasedPackedSwitch(value_reg,
6296 lower_bound,
6297 num_entries,
6298 switch_block,
6299 default_block);
6300 } else {
6301 GenPackedSwitchWithCompares(value_reg,
6302 lower_bound,
6303 num_entries,
6304 switch_block,
6305 default_block);
6306 }
6307}
6308
Chris Larsenc9905a62017-03-13 17:06:18 -07006309void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6310 LocationSummary* locations =
6311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6312 locations->SetInAt(0, Location::RequiresRegister());
6313 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006314}
6315
Chris Larsenc9905a62017-03-13 17:06:18 -07006316void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6317 LocationSummary* locations = instruction->GetLocations();
6318 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6319 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6320 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6321 __ LoadFromOffset(kLoadDoubleword,
6322 locations->Out().AsRegister<GpuRegister>(),
6323 locations->InAt(0).AsRegister<GpuRegister>(),
6324 method_offset);
6325 } else {
6326 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6327 instruction->GetIndex(), kMips64PointerSize));
6328 __ LoadFromOffset(kLoadDoubleword,
6329 locations->Out().AsRegister<GpuRegister>(),
6330 locations->InAt(0).AsRegister<GpuRegister>(),
6331 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6332 __ LoadFromOffset(kLoadDoubleword,
6333 locations->Out().AsRegister<GpuRegister>(),
6334 locations->Out().AsRegister<GpuRegister>(),
6335 method_offset);
6336 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006337}
6338
Alexey Frunze4dda3372015-06-01 18:31:49 -07006339} // namespace mips64
6340} // namespace art