blob: 11120cf07a4c73c017b5cdf20e9c547fb5cac0b8 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips64/asm_support_mips64.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070020#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070022#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080023#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070028#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070029#include "intrinsics_mips64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "mirror/array-inl.h"
32#include "mirror/class-inl.h"
33#include "offsets.h"
34#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070035#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070036#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070037#include "utils/stack_checks.h"
38
39namespace art {
40namespace mips64 {
41
42static constexpr int kCurrentMethodStackOffset = 0;
43static constexpr GpuRegister kMethodRegisterArgument = A0;
44
Alexey Frunze4147fcc2017-06-17 19:57:27 -070045// Flags controlling the use of thunks for Baker read barriers.
46constexpr bool kBakerReadBarrierThunksEnableForFields = true;
47constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
48constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
49
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010050Location Mips64ReturnLocation(DataType::Type return_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070051 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010052 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010053 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010054 case DataType::Type::kInt8:
55 case DataType::Type::kUint16:
56 case DataType::Type::kInt16:
57 case DataType::Type::kInt32:
58 case DataType::Type::kReference:
59 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070060 return Location::RegisterLocation(V0);
61
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 case DataType::Type::kFloat32:
63 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070064 return Location::FpuRegisterLocation(F0);
65
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070067 return Location();
68 }
69 UNREACHABLE();
70}
71
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010072Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070073 return Mips64ReturnLocation(type);
74}
75
76Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
77 return Location::RegisterLocation(kMethodRegisterArgument);
78}
79
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010080Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070081 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010082 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070083 LOG(FATAL) << "Unexpected parameter type " << type;
84 }
85
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010086 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070087 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
88 next_location = Location::FpuRegisterLocation(
89 calling_convention.GetFpuRegisterAt(float_index_++));
90 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010091 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070092 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
93 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
94 float_index_++;
95 } else {
96 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010097 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
98 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -070099 }
100
101 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100102 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700103
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104 return next_location;
105}
106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100107Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108 return Mips64ReturnLocation(type);
109}
110
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100111// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
112#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700113#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
116 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000117 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700118
119 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100120 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
122 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000123 if (instruction_->CanThrowIntoCatchBlock()) {
124 // Live registers will be restored in the catch block if caught.
125 SaveLiveRegisters(codegen, instruction_->GetLocations());
126 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 // We're moving two locations to locations that could overlap, so we need a parallel
128 // move resolver.
129 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100130 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700131 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100132 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100133 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700134 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100135 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100136 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
137 ? kQuickThrowStringBounds
138 : kQuickThrowArrayBounds;
139 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100140 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700141 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
142 }
143
Alexandre Rames8158f282015-08-07 10:26:17 +0100144 bool IsFatal() const OVERRIDE { return true; }
145
Roland Levillain46648892015-06-19 16:07:18 +0100146 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
147
Alexey Frunze4dda3372015-06-01 18:31:49 -0700148 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
150};
151
152class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
153 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700154 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
155 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
159 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100160 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700161 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
162 }
163
Alexandre Rames8158f282015-08-07 10:26:17 +0100164 bool IsFatal() const OVERRIDE { return true; }
165
Roland Levillain46648892015-06-19 16:07:18 +0100166 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
167
Alexey Frunze4dda3372015-06-01 18:31:49 -0700168 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
170};
171
172class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
173 public:
174 LoadClassSlowPathMIPS64(HLoadClass* cls,
175 HInstruction* at,
176 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700177 bool do_clinit,
178 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr)
179 : SlowPathCodeMIPS64(at),
180 cls_(cls),
181 dex_pc_(dex_pc),
182 do_clinit_(do_clinit),
183 bss_info_high_(bss_info_high) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700184 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
185 }
186
187 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000188 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700189 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700190 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700191 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
192 InvokeRuntimeCallingConvention calling_convention;
193 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
194 const bool is_load_class_bss_entry =
195 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700196 __ Bind(GetEntryLabel());
197 SaveLiveRegisters(codegen, locations);
198
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700199 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
200 GpuRegister entry_address = kNoGpuRegister;
201 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
202 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
203 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
204 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
205 // kSaveEverything call.
206 entry_address = temp_is_a0 ? out.AsRegister<GpuRegister>() : temp;
207 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
208 if (temp_is_a0) {
209 __ Move(entry_address, temp);
210 }
211 }
212
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000213 dex::TypeIndex type_index = cls_->GetTypeIndex();
214 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100215 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
216 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000217 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700218 if (do_clinit_) {
219 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
220 } else {
221 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
222 }
223
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700224 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
225 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
226 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
227 DCHECK(bss_info_high_);
228 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
229 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
230 __ Bind(&info_low->label);
231 __ StoreToOffset(kStoreWord,
232 calling_convention.GetRegisterAt(0),
233 entry_address,
234 /* placeholder */ 0x5678);
235 }
236
Alexey Frunze4dda3372015-06-01 18:31:49 -0700237 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700238 if (out.IsValid()) {
239 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100240 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700241 mips64_codegen->MoveLocation(out,
242 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
243 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700244 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700246
247 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
248 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
249 // For non-Baker read barriers we need to re-calculate the address of
250 // the class entry.
251 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000252 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700253 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
254 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
255 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
256 __ StoreToOffset(kStoreWord, out.AsRegister<GpuRegister>(), TMP, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000257 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700258 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700259 }
260
Roland Levillain46648892015-06-19 16:07:18 +0100261 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
262
Alexey Frunze4dda3372015-06-01 18:31:49 -0700263 private:
264 // The class this slow path will load.
265 HLoadClass* const cls_;
266
Alexey Frunze4dda3372015-06-01 18:31:49 -0700267 // The dex PC of `at_`.
268 const uint32_t dex_pc_;
269
270 // Whether to initialize the class.
271 const bool do_clinit_;
272
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700273 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
274 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
275
Alexey Frunze4dda3372015-06-01 18:31:49 -0700276 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
277};
278
279class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
280 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700281 explicit LoadStringSlowPathMIPS64(HLoadString* instruction,
282 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high)
283 : SlowPathCodeMIPS64(instruction), bss_info_high_(bss_info_high) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700284
285 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700286 DCHECK(instruction_->IsLoadString());
287 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700290 HLoadString* load = instruction_->AsLoadString();
291 const dex::StringIndex string_index = load->GetStringIndex();
292 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700294 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
295 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700296 __ Bind(GetEntryLabel());
297 SaveLiveRegisters(codegen, locations);
298
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700299 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
300 GpuRegister entry_address = kNoGpuRegister;
301 if (baker_or_no_read_barriers) {
302 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
303 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
304 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
305 // kSaveEverything call.
306 entry_address = temp_is_a0 ? out : temp;
307 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
308 if (temp_is_a0) {
309 __ Move(entry_address, temp);
310 }
311 }
312
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000313 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100314 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 instruction_,
316 instruction_->GetDexPc(),
317 this);
318 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700319
320 // Store the resolved string to the BSS entry.
321 if (baker_or_no_read_barriers) {
322 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
323 DCHECK(bss_info_high_);
324 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100325 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(),
326 string_index,
327 bss_info_high_);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700328 __ Bind(&info_low->label);
329 __ StoreToOffset(kStoreWord,
330 calling_convention.GetRegisterAt(0),
331 entry_address,
332 /* placeholder */ 0x5678);
333 }
334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100335 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700336 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700337 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700338 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700339 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800340
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700341 // Store the resolved string to the BSS entry.
342 if (!baker_or_no_read_barriers) {
343 // For non-Baker read barriers we need to re-calculate the address of
344 // the string entry.
345 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100346 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700347 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100348 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700349 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
350 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
351 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700352 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353 }
354
Roland Levillain46648892015-06-19 16:07:18 +0100355 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
356
Alexey Frunze4dda3372015-06-01 18:31:49 -0700357 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700358 // Pointer to the high half PC-relative patch info.
359 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
360
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
362};
363
364class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
365 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000366 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367
368 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
369 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
370 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000371 if (instruction_->CanThrowIntoCatchBlock()) {
372 // Live registers will be restored in the catch block if caught.
373 SaveLiveRegisters(codegen, instruction_->GetLocations());
374 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100375 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 instruction_,
377 instruction_->GetDexPc(),
378 this);
379 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
380 }
381
Alexandre Rames8158f282015-08-07 10:26:17 +0100382 bool IsFatal() const OVERRIDE { return true; }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
385
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
388};
389
390class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
391 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100392 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700394
395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200396 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
398 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200399 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100400 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200402 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700404 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700405 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700406 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 }
408 }
409
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700410 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 DCHECK(successor_ == nullptr);
412 return &return_label_;
413 }
414
Roland Levillain46648892015-06-19 16:07:18 +0100415 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
416
Alexey Frunze4dda3372015-06-01 18:31:49 -0700417 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700418 // If not null, the block to branch to after the suspend check.
419 HBasicBlock* const successor_;
420
421 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700422 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700423
424 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
425};
426
427class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
428 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800429 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
430 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700431
432 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
433 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800434
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100435 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700436 DCHECK(instruction_->IsCheckCast()
437 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
438 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
439
440 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800441 if (!is_fatal_) {
442 SaveLiveRegisters(codegen, locations);
443 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700444
445 // We're moving two locations to locations that could overlap, so we need a parallel
446 // move resolver.
447 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800448 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700449 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100450 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800451 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700452 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100453 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700454 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100455 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800456 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100457 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700458 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
459 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700460 } else {
461 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800462 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
463 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700464 }
465
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800466 if (!is_fatal_) {
467 RestoreLiveRegisters(codegen, locations);
468 __ Bc(GetExitLabel());
469 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700470 }
471
Roland Levillain46648892015-06-19 16:07:18 +0100472 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
473
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800474 bool IsFatal() const OVERRIDE { return is_fatal_; }
475
Alexey Frunze4dda3372015-06-01 18:31:49 -0700476 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800477 const bool is_fatal_;
478
Alexey Frunze4dda3372015-06-01 18:31:49 -0700479 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
480};
481
482class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
483 public:
Aart Bik42249c32016-01-07 15:33:50 -0800484 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000485 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700486
487 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800488 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700489 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100490 LocationSummary* locations = instruction_->GetLocations();
491 SaveLiveRegisters(codegen, locations);
492 InvokeRuntimeCallingConvention calling_convention;
493 __ LoadConst32(calling_convention.GetRegisterAt(0),
494 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100495 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100496 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700497 }
498
Roland Levillain46648892015-06-19 16:07:18 +0100499 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
500
Alexey Frunze4dda3372015-06-01 18:31:49 -0700501 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700502 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
503};
504
Alexey Frunze15958152017-02-09 19:08:30 -0800505class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
506 public:
507 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
508
509 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
510 LocationSummary* locations = instruction_->GetLocations();
511 __ Bind(GetEntryLabel());
512 SaveLiveRegisters(codegen, locations);
513
514 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100515 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800516 parallel_move.AddMove(
517 locations->InAt(0),
518 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100519 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800520 nullptr);
521 parallel_move.AddMove(
522 locations->InAt(1),
523 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100524 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800525 nullptr);
526 parallel_move.AddMove(
527 locations->InAt(2),
528 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800530 nullptr);
531 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
532
533 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
534 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
535 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
536 RestoreLiveRegisters(codegen, locations);
537 __ Bc(GetExitLabel());
538 }
539
540 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
541
542 private:
543 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
544};
545
546// Slow path marking an object reference `ref` during a read
547// barrier. The field `obj.field` in the object `obj` holding this
548// reference does not get updated by this slow path after marking (see
549// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
550//
551// This means that after the execution of this slow path, `ref` will
552// always be up-to-date, but `obj.field` may not; i.e., after the
553// flip, `ref` will be a to-space reference, but `obj.field` will
554// probably still be a from-space reference (unless it gets updated by
555// another thread, or if another thread installed another object
556// reference (different from `ref`) in `obj.field`).
557//
558// If `entrypoint` is a valid location it is assumed to already be
559// holding the entrypoint. The case where the entrypoint is passed in
560// is for the GcRoot read barrier.
561class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
562 public:
563 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
564 Location ref,
565 Location entrypoint = Location::NoLocation())
566 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
567 DCHECK(kEmitCompilerReadBarrier);
568 }
569
570 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
571
572 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
573 LocationSummary* locations = instruction_->GetLocations();
574 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
575 DCHECK(locations->CanCall());
576 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
577 DCHECK(instruction_->IsInstanceFieldGet() ||
578 instruction_->IsStaticFieldGet() ||
579 instruction_->IsArrayGet() ||
580 instruction_->IsArraySet() ||
581 instruction_->IsLoadClass() ||
582 instruction_->IsLoadString() ||
583 instruction_->IsInstanceOf() ||
584 instruction_->IsCheckCast() ||
585 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
586 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
587 << "Unexpected instruction in read barrier marking slow path: "
588 << instruction_->DebugName();
589
590 __ Bind(GetEntryLabel());
591 // No need to save live registers; it's taken care of by the
592 // entrypoint. Also, there is no need to update the stack mask,
593 // as this runtime call will not trigger a garbage collection.
594 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
595 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
596 (S2 <= ref_reg && ref_reg <= S7) ||
597 (ref_reg == S8)) << ref_reg;
598 // "Compact" slow path, saving two moves.
599 //
600 // Instead of using the standard runtime calling convention (input
601 // and output in A0 and V0 respectively):
602 //
603 // A0 <- ref
604 // V0 <- ReadBarrierMark(A0)
605 // ref <- V0
606 //
607 // we just use rX (the register containing `ref`) as input and output
608 // of a dedicated entrypoint:
609 //
610 // rX <- ReadBarrierMarkRegX(rX)
611 //
612 if (entrypoint_.IsValid()) {
613 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
614 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
615 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
616 __ Nop();
617 } else {
618 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100619 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800620 // This runtime call does not require a stack map.
621 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
622 instruction_,
623 this);
624 }
625 __ Bc(GetExitLabel());
626 }
627
628 private:
629 // The location (register) of the marked object reference.
630 const Location ref_;
631
632 // The location of the entrypoint if already loaded.
633 const Location entrypoint_;
634
635 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
636};
637
638// Slow path marking an object reference `ref` during a read barrier,
639// and if needed, atomically updating the field `obj.field` in the
640// object `obj` holding this reference after marking (contrary to
641// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
642// `obj.field`).
643//
644// This means that after the execution of this slow path, both `ref`
645// and `obj.field` will be up-to-date; i.e., after the flip, both will
646// hold the same to-space reference (unless another thread installed
647// another object reference (different from `ref`) in `obj.field`).
648class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
649 public:
650 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
651 Location ref,
652 GpuRegister obj,
653 Location field_offset,
654 GpuRegister temp1)
655 : SlowPathCodeMIPS64(instruction),
656 ref_(ref),
657 obj_(obj),
658 field_offset_(field_offset),
659 temp1_(temp1) {
660 DCHECK(kEmitCompilerReadBarrier);
661 }
662
663 const char* GetDescription() const OVERRIDE {
664 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
665 }
666
667 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
668 LocationSummary* locations = instruction_->GetLocations();
669 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
670 DCHECK(locations->CanCall());
671 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
672 // This slow path is only used by the UnsafeCASObject intrinsic.
673 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
674 << "Unexpected instruction in read barrier marking and field updating slow path: "
675 << instruction_->DebugName();
676 DCHECK(instruction_->GetLocations()->Intrinsified());
677 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
678 DCHECK(field_offset_.IsRegister()) << field_offset_;
679
680 __ Bind(GetEntryLabel());
681
682 // Save the old reference.
683 // Note that we cannot use AT or TMP to save the old reference, as those
684 // are used by the code that follows, but we need the old reference after
685 // the call to the ReadBarrierMarkRegX entry point.
686 DCHECK_NE(temp1_, AT);
687 DCHECK_NE(temp1_, TMP);
688 __ Move(temp1_, ref_reg);
689
690 // No need to save live registers; it's taken care of by the
691 // entrypoint. Also, there is no need to update the stack mask,
692 // as this runtime call will not trigger a garbage collection.
693 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
694 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
695 (S2 <= ref_reg && ref_reg <= S7) ||
696 (ref_reg == S8)) << ref_reg;
697 // "Compact" slow path, saving two moves.
698 //
699 // Instead of using the standard runtime calling convention (input
700 // and output in A0 and V0 respectively):
701 //
702 // A0 <- ref
703 // V0 <- ReadBarrierMark(A0)
704 // ref <- V0
705 //
706 // we just use rX (the register containing `ref`) as input and output
707 // of a dedicated entrypoint:
708 //
709 // rX <- ReadBarrierMarkRegX(rX)
710 //
711 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100712 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800713 // This runtime call does not require a stack map.
714 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
715 instruction_,
716 this);
717
718 // If the new reference is different from the old reference,
719 // update the field in the holder (`*(obj_ + field_offset_)`).
720 //
721 // Note that this field could also hold a different object, if
722 // another thread had concurrently changed it. In that case, the
723 // the compare-and-set (CAS) loop below would abort, leaving the
724 // field as-is.
725 Mips64Label done;
726 __ Beqc(temp1_, ref_reg, &done);
727
728 // Update the the holder's field atomically. This may fail if
729 // mutator updates before us, but it's OK. This is achieved
730 // using a strong compare-and-set (CAS) operation with relaxed
731 // memory synchronization ordering, where the expected value is
732 // the old reference and the desired value is the new reference.
733
734 // Convenience aliases.
735 GpuRegister base = obj_;
736 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
737 GpuRegister expected = temp1_;
738 GpuRegister value = ref_reg;
739 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
740 GpuRegister tmp = AT; // Value in memory.
741
742 __ Daddu(tmp_ptr, base, offset);
743
744 if (kPoisonHeapReferences) {
745 __ PoisonHeapReference(expected);
746 // Do not poison `value` if it is the same register as
747 // `expected`, which has just been poisoned.
748 if (value != expected) {
749 __ PoisonHeapReference(value);
750 }
751 }
752
753 // do {
754 // tmp = [r_ptr] - expected;
755 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
756
757 Mips64Label loop_head, exit_loop;
758 __ Bind(&loop_head);
759 __ Ll(tmp, tmp_ptr);
760 // The LL instruction sign-extends the 32-bit value, but
761 // 32-bit references must be zero-extended. Zero-extend `tmp`.
762 __ Dext(tmp, tmp, 0, 32);
763 __ Bnec(tmp, expected, &exit_loop);
764 __ Move(tmp, value);
765 __ Sc(tmp, tmp_ptr);
766 __ Beqzc(tmp, &loop_head);
767 __ Bind(&exit_loop);
768
769 if (kPoisonHeapReferences) {
770 __ UnpoisonHeapReference(expected);
771 // Do not unpoison `value` if it is the same register as
772 // `expected`, which has just been unpoisoned.
773 if (value != expected) {
774 __ UnpoisonHeapReference(value);
775 }
776 }
777
778 __ Bind(&done);
779 __ Bc(GetExitLabel());
780 }
781
782 private:
783 // The location (register) of the marked object reference.
784 const Location ref_;
785 // The register containing the object holding the marked object reference field.
786 const GpuRegister obj_;
787 // The location of the offset of the marked reference field within `obj_`.
788 Location field_offset_;
789
790 const GpuRegister temp1_;
791
792 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
793};
794
795// Slow path generating a read barrier for a heap reference.
796class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
797 public:
798 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
799 Location out,
800 Location ref,
801 Location obj,
802 uint32_t offset,
803 Location index)
804 : SlowPathCodeMIPS64(instruction),
805 out_(out),
806 ref_(ref),
807 obj_(obj),
808 offset_(offset),
809 index_(index) {
810 DCHECK(kEmitCompilerReadBarrier);
811 // If `obj` is equal to `out` or `ref`, it means the initial object
812 // has been overwritten by (or after) the heap object reference load
813 // to be instrumented, e.g.:
814 //
815 // __ LoadFromOffset(kLoadWord, out, out, offset);
816 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
817 //
818 // In that case, we have lost the information about the original
819 // object, and the emitted read barrier cannot work properly.
820 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
821 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
822 }
823
824 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
825 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
826 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100827 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800828 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
829 DCHECK(locations->CanCall());
830 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
831 DCHECK(instruction_->IsInstanceFieldGet() ||
832 instruction_->IsStaticFieldGet() ||
833 instruction_->IsArrayGet() ||
834 instruction_->IsInstanceOf() ||
835 instruction_->IsCheckCast() ||
836 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
837 << "Unexpected instruction in read barrier for heap reference slow path: "
838 << instruction_->DebugName();
839
840 __ Bind(GetEntryLabel());
841 SaveLiveRegisters(codegen, locations);
842
843 // We may have to change the index's value, but as `index_` is a
844 // constant member (like other "inputs" of this slow path),
845 // introduce a copy of it, `index`.
846 Location index = index_;
847 if (index_.IsValid()) {
848 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
849 if (instruction_->IsArrayGet()) {
850 // Compute the actual memory offset and store it in `index`.
851 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
852 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
853 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
854 // We are about to change the value of `index_reg` (see the
855 // calls to art::mips64::Mips64Assembler::Sll and
856 // art::mips64::MipsAssembler::Addiu32 below), but it has
857 // not been saved by the previous call to
858 // art::SlowPathCode::SaveLiveRegisters, as it is a
859 // callee-save register --
860 // art::SlowPathCode::SaveLiveRegisters does not consider
861 // callee-save registers, as it has been designed with the
862 // assumption that callee-save registers are supposed to be
863 // handled by the called function. So, as a callee-save
864 // register, `index_reg` _would_ eventually be saved onto
865 // the stack, but it would be too late: we would have
866 // changed its value earlier. Therefore, we manually save
867 // it here into another freely available register,
868 // `free_reg`, chosen of course among the caller-save
869 // registers (as a callee-save `free_reg` register would
870 // exhibit the same problem).
871 //
872 // Note we could have requested a temporary register from
873 // the register allocator instead; but we prefer not to, as
874 // this is a slow path, and we know we can find a
875 // caller-save register that is available.
876 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
877 __ Move(free_reg, index_reg);
878 index_reg = free_reg;
879 index = Location::RegisterLocation(index_reg);
880 } else {
881 // The initial register stored in `index_` has already been
882 // saved in the call to art::SlowPathCode::SaveLiveRegisters
883 // (as it is not a callee-save register), so we can freely
884 // use it.
885 }
886 // Shifting the index value contained in `index_reg` by the scale
887 // factor (2) cannot overflow in practice, as the runtime is
888 // unable to allocate object arrays with a size larger than
889 // 2^26 - 1 (that is, 2^28 - 4 bytes).
890 __ Sll(index_reg, index_reg, TIMES_4);
891 static_assert(
892 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
893 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
894 __ Addiu32(index_reg, index_reg, offset_);
895 } else {
896 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
897 // intrinsics, `index_` is not shifted by a scale factor of 2
898 // (as in the case of ArrayGet), as it is actually an offset
899 // to an object field within an object.
900 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
901 DCHECK(instruction_->GetLocations()->Intrinsified());
902 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
903 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
904 << instruction_->AsInvoke()->GetIntrinsic();
905 DCHECK_EQ(offset_, 0U);
906 DCHECK(index_.IsRegister());
907 }
908 }
909
910 // We're moving two or three locations to locations that could
911 // overlap, so we need a parallel move resolver.
912 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100913 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800914 parallel_move.AddMove(ref_,
915 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100916 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800917 nullptr);
918 parallel_move.AddMove(obj_,
919 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100920 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800921 nullptr);
922 if (index.IsValid()) {
923 parallel_move.AddMove(index,
924 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100925 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800926 nullptr);
927 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
928 } else {
929 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
930 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
931 }
932 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
933 instruction_,
934 instruction_->GetDexPc(),
935 this);
936 CheckEntrypointTypes<
937 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
938 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
939
940 RestoreLiveRegisters(codegen, locations);
941 __ Bc(GetExitLabel());
942 }
943
944 const char* GetDescription() const OVERRIDE {
945 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
946 }
947
948 private:
949 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
950 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
951 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
952 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
953 if (i != ref &&
954 i != obj &&
955 !codegen->IsCoreCalleeSaveRegister(i) &&
956 !codegen->IsBlockedCoreRegister(i)) {
957 return static_cast<GpuRegister>(i);
958 }
959 }
960 // We shall never fail to find a free caller-save register, as
961 // there are more than two core caller-save registers on MIPS64
962 // (meaning it is possible to find one which is different from
963 // `ref` and `obj`).
964 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
965 LOG(FATAL) << "Could not find a free caller-save register";
966 UNREACHABLE();
967 }
968
969 const Location out_;
970 const Location ref_;
971 const Location obj_;
972 const uint32_t offset_;
973 // An additional location containing an index to an array.
974 // Only used for HArrayGet and the UnsafeGetObject &
975 // UnsafeGetObjectVolatile intrinsics.
976 const Location index_;
977
978 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
979};
980
981// Slow path generating a read barrier for a GC root.
982class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
983 public:
984 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
985 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
986 DCHECK(kEmitCompilerReadBarrier);
987 }
988
989 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
990 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100991 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800992 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
993 DCHECK(locations->CanCall());
994 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
995 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
996 << "Unexpected instruction in read barrier for GC root slow path: "
997 << instruction_->DebugName();
998
999 __ Bind(GetEntryLabel());
1000 SaveLiveRegisters(codegen, locations);
1001
1002 InvokeRuntimeCallingConvention calling_convention;
1003 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
1004 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1005 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001006 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001007 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1008 instruction_,
1009 instruction_->GetDexPc(),
1010 this);
1011 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1012 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1013
1014 RestoreLiveRegisters(codegen, locations);
1015 __ Bc(GetExitLabel());
1016 }
1017
1018 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
1019
1020 private:
1021 const Location out_;
1022 const Location root_;
1023
1024 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
1025};
1026
Alexey Frunze4dda3372015-06-01 18:31:49 -07001027CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
1028 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001029 const CompilerOptions& compiler_options,
1030 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001031 : CodeGenerator(graph,
1032 kNumberOfGpuRegisters,
1033 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001034 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001035 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1036 arraysize(kCoreCalleeSaves)),
1037 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1038 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001039 compiler_options,
1040 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001041 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001042 location_builder_(graph, this),
1043 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 move_resolver_(graph->GetAllocator(), this),
1045 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001046 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001047 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001048 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001049 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001050 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1051 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1052 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1053 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1054 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1055 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1056 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001057 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001058 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001059 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001060 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001061 // Save RA (containing the return address) to mimic Quick.
1062 AddAllocatedRegister(Location::RegisterLocation(RA));
1063}
1064
1065#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001066// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1067#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001068#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -07001069
1070void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001071 // Ensure that we fix up branches.
1072 __ FinalizeCode();
1073
1074 // Adjust native pc offsets in stack maps.
1075 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001076 uint32_t old_position =
1077 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001078 uint32_t new_position = __ GetAdjustedPosition(old_position);
1079 DCHECK_GE(new_position, old_position);
1080 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1081 }
1082
1083 // Adjust pc offsets for the disassembly information.
1084 if (disasm_info_ != nullptr) {
1085 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1086 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1087 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1088 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1089 it.second.start = __ GetAdjustedPosition(it.second.start);
1090 it.second.end = __ GetAdjustedPosition(it.second.end);
1091 }
1092 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1093 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1094 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1095 }
1096 }
1097
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 CodeGenerator::Finalize(allocator);
1099}
1100
1101Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1102 return codegen_->GetAssembler();
1103}
1104
1105void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001106 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001107 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1108}
1109
1110void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001111 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001112 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1113}
1114
1115void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1116 // Pop reg
1117 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001118 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001119}
1120
1121void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1122 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001123 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001124 __ Sd(GpuRegister(reg), SP, 0);
1125}
1126
1127void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1128 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1129 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1130 // Allocate a scratch register other than TMP, if available.
1131 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1132 // automatically unspilled when the scratch scope object is destroyed).
1133 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1134 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001135 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001136 __ LoadFromOffset(load_type,
1137 GpuRegister(ensure_scratch.GetRegister()),
1138 SP,
1139 index1 + stack_offset);
1140 __ LoadFromOffset(load_type,
1141 TMP,
1142 SP,
1143 index2 + stack_offset);
1144 __ StoreToOffset(store_type,
1145 GpuRegister(ensure_scratch.GetRegister()),
1146 SP,
1147 index2 + stack_offset);
1148 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1149}
1150
1151static dwarf::Reg DWARFReg(GpuRegister reg) {
1152 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1153}
1154
David Srbeckyba702002016-02-01 18:15:29 +00001155static dwarf::Reg DWARFReg(FpuRegister reg) {
1156 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1157}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001158
1159void CodeGeneratorMIPS64::GenerateFrameEntry() {
1160 __ Bind(&frame_entry_label_);
1161
1162 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1163
1164 if (do_overflow_check) {
1165 __ LoadFromOffset(kLoadWord,
1166 ZERO,
1167 SP,
1168 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1169 RecordPcInfo(nullptr, 0);
1170 }
1171
Alexey Frunze4dda3372015-06-01 18:31:49 -07001172 if (HasEmptyFrame()) {
1173 return;
1174 }
1175
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001176 // Make sure the frame size isn't unreasonably large.
1177 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1178 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1179 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001180
1181 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001183 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001184 __ IncreaseFrameSize(ofs);
1185
1186 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1187 GpuRegister reg = kCoreCalleeSaves[i];
1188 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001189 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001190 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001191 __ cfi().RelOffset(DWARFReg(reg), ofs);
1192 }
1193 }
1194
1195 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1196 FpuRegister reg = kFpuCalleeSaves[i];
1197 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001198 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001199 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001200 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001201 }
1202 }
1203
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001204 // Save the current method if we need it. Note that we do not
1205 // do this in HCurrentMethod, as the instruction might have been removed
1206 // in the SSA graph.
1207 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001208 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001209 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001210
1211 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1212 // Initialize should_deoptimize flag to 0.
1213 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1214 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215}
1216
1217void CodeGeneratorMIPS64::GenerateFrameExit() {
1218 __ cfi().RememberState();
1219
Alexey Frunze4dda3372015-06-01 18:31:49 -07001220 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001221 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001222
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001223 // For better instruction scheduling restore RA before other registers.
1224 uint32_t ofs = GetFrameSize();
1225 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001226 GpuRegister reg = kCoreCalleeSaves[i];
1227 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001228 ofs -= kMips64DoublewordSize;
1229 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001230 __ cfi().Restore(DWARFReg(reg));
1231 }
1232 }
1233
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001234 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1235 FpuRegister reg = kFpuCalleeSaves[i];
1236 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1237 ofs -= kMips64DoublewordSize;
1238 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1239 __ cfi().Restore(DWARFReg(reg));
1240 }
1241 }
1242
1243 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001244 }
1245
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001246 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001247
1248 __ cfi().RestoreState();
1249 __ cfi().DefCFAOffset(GetFrameSize());
1250}
1251
1252void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1253 __ Bind(GetLabelOf(block));
1254}
1255
1256void CodeGeneratorMIPS64::MoveLocation(Location destination,
1257 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001258 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001259 if (source.Equals(destination)) {
1260 return;
1261 }
1262
1263 // A valid move can always be inferred from the destination and source
1264 // locations. When moving from and to a register, the argument type can be
1265 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001266 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001267 DCHECK_EQ(unspecified_type, false);
1268
1269 if (destination.IsRegister() || destination.IsFpuRegister()) {
1270 if (unspecified_type) {
1271 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1272 if (source.IsStackSlot() ||
1273 (src_cst != nullptr && (src_cst->IsIntConstant()
1274 || src_cst->IsFloatConstant()
1275 || src_cst->IsNullConstant()))) {
1276 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001277 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001278 } else {
1279 // If the source is a double stack slot or a 64bit constant, a 64bit
1280 // type is appropriate. Else the source is a register, and since the
1281 // type has not been specified, we chose a 64bit type to force a 64bit
1282 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001284 }
1285 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001286 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1287 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001288 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1289 // Move to GPR/FPR from stack
1290 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001291 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001292 __ LoadFpuFromOffset(load_type,
1293 destination.AsFpuRegister<FpuRegister>(),
1294 SP,
1295 source.GetStackIndex());
1296 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001297 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001298 __ LoadFromOffset(load_type,
1299 destination.AsRegister<GpuRegister>(),
1300 SP,
1301 source.GetStackIndex());
1302 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001303 } else if (source.IsSIMDStackSlot()) {
1304 __ LoadFpuFromOffset(kLoadQuadword,
1305 destination.AsFpuRegister<FpuRegister>(),
1306 SP,
1307 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 } else if (source.IsConstant()) {
1309 // Move to GPR/FPR from constant
1310 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001311 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001312 gpr = destination.AsRegister<GpuRegister>();
1313 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001315 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001316 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001317 gpr = ZERO;
1318 } else {
1319 __ LoadConst32(gpr, value);
1320 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001321 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001322 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001324 gpr = ZERO;
1325 } else {
1326 __ LoadConst64(gpr, value);
1327 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001328 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001329 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001331 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001332 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1333 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001334 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001335 if (destination.IsRegister()) {
1336 // Move to GPR from GPR
1337 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1338 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001339 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001340 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001341 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1342 } else {
1343 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1344 }
1345 }
1346 } else if (source.IsFpuRegister()) {
1347 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001348 if (GetGraph()->HasSIMD()) {
1349 __ MoveV(VectorRegisterFrom(destination),
1350 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001351 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001352 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001353 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001354 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1355 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001356 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001357 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1358 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001359 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001360 } else {
1361 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001362 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001363 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1364 } else {
1365 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1366 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001367 }
1368 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001369 } else if (destination.IsSIMDStackSlot()) {
1370 if (source.IsFpuRegister()) {
1371 __ StoreFpuToOffset(kStoreQuadword,
1372 source.AsFpuRegister<FpuRegister>(),
1373 SP,
1374 destination.GetStackIndex());
1375 } else {
1376 DCHECK(source.IsSIMDStackSlot());
1377 __ LoadFpuFromOffset(kLoadQuadword,
1378 FTMP,
1379 SP,
1380 source.GetStackIndex());
1381 __ StoreFpuToOffset(kStoreQuadword,
1382 FTMP,
1383 SP,
1384 destination.GetStackIndex());
1385 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001386 } else { // The destination is not a register. It must be a stack slot.
1387 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1388 if (source.IsRegister() || source.IsFpuRegister()) {
1389 if (unspecified_type) {
1390 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001391 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001392 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001393 dst_type =
1394 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001395 }
1396 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001397 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1398 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001399 // Move to stack from GPR/FPR
1400 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1401 if (source.IsRegister()) {
1402 __ StoreToOffset(store_type,
1403 source.AsRegister<GpuRegister>(),
1404 SP,
1405 destination.GetStackIndex());
1406 } else {
1407 __ StoreFpuToOffset(store_type,
1408 source.AsFpuRegister<FpuRegister>(),
1409 SP,
1410 destination.GetStackIndex());
1411 }
1412 } else if (source.IsConstant()) {
1413 // Move to stack from constant
1414 HConstant* src_cst = source.GetConstant();
1415 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001416 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001417 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001418 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1419 if (value != 0) {
1420 gpr = TMP;
1421 __ LoadConst32(gpr, value);
1422 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001423 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001424 DCHECK(destination.IsDoubleStackSlot());
1425 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1426 if (value != 0) {
1427 gpr = TMP;
1428 __ LoadConst64(gpr, value);
1429 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001430 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001431 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001432 } else {
1433 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1434 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1435 // Move to stack from stack
1436 if (destination.IsStackSlot()) {
1437 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1438 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1439 } else {
1440 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1441 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1442 }
1443 }
1444 }
1445}
1446
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001448 DCHECK(!loc1.IsConstant());
1449 DCHECK(!loc2.IsConstant());
1450
1451 if (loc1.Equals(loc2)) {
1452 return;
1453 }
1454
1455 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1456 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1457 bool is_fp_reg1 = loc1.IsFpuRegister();
1458 bool is_fp_reg2 = loc2.IsFpuRegister();
1459
1460 if (loc2.IsRegister() && loc1.IsRegister()) {
1461 // Swap 2 GPRs
1462 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1463 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1464 __ Move(TMP, r2);
1465 __ Move(r2, r1);
1466 __ Move(r1, TMP);
1467 } else if (is_fp_reg2 && is_fp_reg1) {
1468 // Swap 2 FPRs
1469 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1470 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001471 if (type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001472 __ MovS(FTMP, r1);
1473 __ MovS(r1, r2);
1474 __ MovS(r2, FTMP);
1475 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001476 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001477 __ MovD(FTMP, r1);
1478 __ MovD(r1, r2);
1479 __ MovD(r2, FTMP);
1480 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001481 } else if (is_slot1 != is_slot2) {
1482 // Swap GPR/FPR and stack slot
1483 Location reg_loc = is_slot1 ? loc2 : loc1;
1484 Location mem_loc = is_slot1 ? loc1 : loc2;
1485 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1486 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001487 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001488 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1489 if (reg_loc.IsFpuRegister()) {
1490 __ StoreFpuToOffset(store_type,
1491 reg_loc.AsFpuRegister<FpuRegister>(),
1492 SP,
1493 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001494 if (mem_loc.IsStackSlot()) {
1495 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1496 } else {
1497 DCHECK(mem_loc.IsDoubleStackSlot());
1498 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1499 }
1500 } else {
1501 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1502 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1503 }
1504 } else if (is_slot1 && is_slot2) {
1505 move_resolver_.Exchange(loc1.GetStackIndex(),
1506 loc2.GetStackIndex(),
1507 loc1.IsDoubleStackSlot());
1508 } else {
1509 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1510 }
1511}
1512
Calin Juravle175dc732015-08-25 15:42:32 +01001513void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1514 DCHECK(location.IsRegister());
1515 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1516}
1517
Calin Juravlee460d1d2015-09-29 04:52:17 +01001518void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1519 if (location.IsRegister()) {
1520 locations->AddTemp(location);
1521 } else {
1522 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1523 }
1524}
1525
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001526void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1527 GpuRegister value,
1528 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001529 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001530 GpuRegister card = AT;
1531 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001532 if (value_can_be_null) {
1533 __ Beqzc(value, &done);
1534 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001535 __ LoadFromOffset(kLoadDoubleword,
1536 card,
1537 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001538 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001539 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1540 __ Daddu(temp, card, temp);
1541 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001542 if (value_can_be_null) {
1543 __ Bind(&done);
1544 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001545}
1546
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001547template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001548inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1549 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001550 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001551 for (const PcRelativePatchInfo& info : infos) {
1552 const DexFile& dex_file = info.target_dex_file;
1553 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001554 DCHECK(info.label.IsBound());
1555 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1556 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1557 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1558 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001559 }
1560}
1561
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001562void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001563 DCHECK(linker_patches->empty());
1564 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001565 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001566 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001567 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001568 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001569 pc_relative_string_patches_.size() +
1570 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001571 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001572 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001573 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1574 pc_relative_method_patches_, linker_patches);
1575 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1576 pc_relative_type_patches_, linker_patches);
1577 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1578 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001579 } else {
1580 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001581 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1582 pc_relative_type_patches_, linker_patches);
1583 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1584 pc_relative_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001585 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001586 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1587 method_bss_entry_patches_, linker_patches);
1588 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1589 type_bss_entry_patches_, linker_patches);
1590 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1591 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001592 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001593}
1594
Vladimir Marko65979462017-05-19 17:25:12 +01001595CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001596 MethodReference target_method,
1597 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001598 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001599 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001600 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001601 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001602}
1603
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001604CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001605 MethodReference target_method,
1606 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001607 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001608 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001609 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001610 &method_bss_entry_patches_);
1611}
1612
Alexey Frunzef63f5692016-12-13 17:43:11 -08001613CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001614 const DexFile& dex_file,
1615 dex::TypeIndex type_index,
1616 const PcRelativePatchInfo* info_high) {
1617 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001618}
1619
Vladimir Marko1998cd02017-01-13 13:02:58 +00001620CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001621 const DexFile& dex_file,
1622 dex::TypeIndex type_index,
1623 const PcRelativePatchInfo* info_high) {
1624 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001625}
1626
Vladimir Marko65979462017-05-19 17:25:12 +01001627CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001628 const DexFile& dex_file,
1629 dex::StringIndex string_index,
1630 const PcRelativePatchInfo* info_high) {
1631 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001632}
1633
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001634CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1635 const DexFile& dex_file,
1636 dex::StringIndex string_index,
1637 const PcRelativePatchInfo* info_high) {
1638 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1639}
1640
Alexey Frunze19f6c692016-11-30 19:19:55 -08001641CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001642 const DexFile& dex_file,
1643 uint32_t offset_or_index,
1644 const PcRelativePatchInfo* info_high,
1645 ArenaDeque<PcRelativePatchInfo>* patches) {
1646 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001647 return &patches->back();
1648}
1649
Alexey Frunzef63f5692016-12-13 17:43:11 -08001650Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1651 return map->GetOrCreate(
1652 value,
1653 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1654}
1655
Alexey Frunze19f6c692016-11-30 19:19:55 -08001656Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1657 return uint64_literals_.GetOrCreate(
1658 value,
1659 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1660}
1661
Alexey Frunzef63f5692016-12-13 17:43:11 -08001662Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001663 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001664}
1665
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001666void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1667 GpuRegister out,
1668 PcRelativePatchInfo* info_low) {
1669 DCHECK(!info_high->patch_info_high);
1670 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001671 // Add the high half of a 32-bit offset to PC.
1672 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001673 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001674 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001675 if (info_low != nullptr) {
1676 DCHECK_EQ(info_low->patch_info_high, info_high);
1677 __ Bind(&info_low->label);
1678 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001679}
1680
Alexey Frunze627c1a02017-01-30 19:28:14 -08001681Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1682 dex::StringIndex string_index,
1683 Handle<mirror::String> handle) {
1684 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1685 reinterpret_cast64<uint64_t>(handle.GetReference()));
1686 return jit_string_patches_.GetOrCreate(
1687 StringReference(&dex_file, string_index),
1688 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1689}
1690
1691Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1692 dex::TypeIndex type_index,
1693 Handle<mirror::Class> handle) {
1694 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1695 reinterpret_cast64<uint64_t>(handle.GetReference()));
1696 return jit_class_patches_.GetOrCreate(
1697 TypeReference(&dex_file, type_index),
1698 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1699}
1700
1701void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1702 const uint8_t* roots_data,
1703 const Literal* literal,
1704 uint64_t index_in_table) const {
1705 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1706 uintptr_t address =
1707 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1708 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1709}
1710
1711void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1712 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001713 const StringReference& string_reference = entry.first;
1714 Literal* table_entry_literal = entry.second;
1715 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001716 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001717 uint64_t index_in_table = it->second;
1718 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001719 }
1720 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001721 const TypeReference& type_reference = entry.first;
1722 Literal* table_entry_literal = entry.second;
1723 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001724 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001725 uint64_t index_in_table = it->second;
1726 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001727 }
1728}
1729
David Brazdil58282f42016-01-14 12:45:10 +00001730void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1732 blocked_core_registers_[ZERO] = true;
1733 blocked_core_registers_[K0] = true;
1734 blocked_core_registers_[K1] = true;
1735 blocked_core_registers_[GP] = true;
1736 blocked_core_registers_[SP] = true;
1737 blocked_core_registers_[RA] = true;
1738
Lazar Trsicd9672662015-09-03 17:33:01 +02001739 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1740 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001741 blocked_core_registers_[AT] = true;
1742 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001743 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001744 blocked_fpu_registers_[FTMP] = true;
1745
1746 // Reserve suspend and thread registers.
1747 blocked_core_registers_[S0] = true;
1748 blocked_core_registers_[TR] = true;
1749
1750 // Reserve T9 for function calls
1751 blocked_core_registers_[T9] = true;
1752
Goran Jakovljevic782be112016-06-21 12:39:04 +02001753 if (GetGraph()->IsDebuggable()) {
1754 // Stubs do not save callee-save floating point registers. If the graph
1755 // is debuggable, we need to deal with these registers differently. For
1756 // now, just block them.
1757 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1758 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1759 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001760 }
1761}
1762
Alexey Frunze4dda3372015-06-01 18:31:49 -07001763size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1764 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001765 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766}
1767
1768size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1769 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001770 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001771}
1772
1773size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001774 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1775 FpuRegister(reg_id),
1776 SP,
1777 stack_index);
1778 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779}
1780
1781size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001782 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1783 FpuRegister(reg_id),
1784 SP,
1785 stack_index);
1786 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787}
1788
1789void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001790 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001791}
1792
1793void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001794 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001795}
1796
Calin Juravle175dc732015-08-25 15:42:32 +01001797void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001798 HInstruction* instruction,
1799 uint32_t dex_pc,
1800 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001801 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001802 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001803 if (EntrypointRequiresStackMap(entrypoint)) {
1804 RecordPcInfo(instruction, dex_pc, slow_path);
1805 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806}
1807
Alexey Frunze15958152017-02-09 19:08:30 -08001808void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1809 HInstruction* instruction,
1810 SlowPathCode* slow_path) {
1811 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1812 GenerateInvokeRuntime(entry_point_offset);
1813}
1814
1815void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1816 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1817 __ Jalr(T9);
1818 __ Nop();
1819}
1820
Alexey Frunze4dda3372015-06-01 18:31:49 -07001821void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1822 GpuRegister class_reg) {
1823 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1824 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1825 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001826 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1827 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001828 __ Bind(slow_path->GetExitLabel());
1829}
1830
1831void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1832 __ Sync(0); // only stype 0 is supported
1833}
1834
1835void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1836 HBasicBlock* successor) {
1837 SuspendCheckSlowPathMIPS64* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001838 new (GetGraph()->GetAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001839 codegen_->AddSlowPath(slow_path);
1840
1841 __ LoadFromOffset(kLoadUnsignedHalfword,
1842 TMP,
1843 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001844 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001845 if (successor == nullptr) {
1846 __ Bnezc(TMP, slow_path->GetEntryLabel());
1847 __ Bind(slow_path->GetReturnLabel());
1848 } else {
1849 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001850 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001851 // slow_path will return to GetLabelOf(successor).
1852 }
1853}
1854
1855InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1856 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001857 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001858 assembler_(codegen->GetAssembler()),
1859 codegen_(codegen) {}
1860
1861void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1862 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001863 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001865 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001866 case DataType::Type::kInt32:
1867 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001868 locations->SetInAt(0, Location::RequiresRegister());
1869 HInstruction* right = instruction->InputAt(1);
1870 bool can_use_imm = false;
1871 if (right->IsConstant()) {
1872 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1873 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1874 can_use_imm = IsUint<16>(imm);
1875 } else if (instruction->IsAdd()) {
1876 can_use_imm = IsInt<16>(imm);
1877 } else {
1878 DCHECK(instruction->IsSub());
1879 can_use_imm = IsInt<16>(-imm);
1880 }
1881 }
1882 if (can_use_imm)
1883 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1884 else
1885 locations->SetInAt(1, Location::RequiresRegister());
1886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1887 }
1888 break;
1889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001890 case DataType::Type::kFloat32:
1891 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001892 locations->SetInAt(0, Location::RequiresFpuRegister());
1893 locations->SetInAt(1, Location::RequiresFpuRegister());
1894 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1895 break;
1896
1897 default:
1898 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1899 }
1900}
1901
1902void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001903 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001904 LocationSummary* locations = instruction->GetLocations();
1905
1906 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001907 case DataType::Type::kInt32:
1908 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001909 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1910 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1911 Location rhs_location = locations->InAt(1);
1912
1913 GpuRegister rhs_reg = ZERO;
1914 int64_t rhs_imm = 0;
1915 bool use_imm = rhs_location.IsConstant();
1916 if (use_imm) {
1917 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1918 } else {
1919 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1920 }
1921
1922 if (instruction->IsAnd()) {
1923 if (use_imm)
1924 __ Andi(dst, lhs, rhs_imm);
1925 else
1926 __ And(dst, lhs, rhs_reg);
1927 } else if (instruction->IsOr()) {
1928 if (use_imm)
1929 __ Ori(dst, lhs, rhs_imm);
1930 else
1931 __ Or(dst, lhs, rhs_reg);
1932 } else if (instruction->IsXor()) {
1933 if (use_imm)
1934 __ Xori(dst, lhs, rhs_imm);
1935 else
1936 __ Xor(dst, lhs, rhs_reg);
1937 } else if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001939 if (use_imm)
1940 __ Addiu(dst, lhs, rhs_imm);
1941 else
1942 __ Addu(dst, lhs, rhs_reg);
1943 } else {
1944 if (use_imm)
1945 __ Daddiu(dst, lhs, rhs_imm);
1946 else
1947 __ Daddu(dst, lhs, rhs_reg);
1948 }
1949 } else {
1950 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001952 if (use_imm)
1953 __ Addiu(dst, lhs, -rhs_imm);
1954 else
1955 __ Subu(dst, lhs, rhs_reg);
1956 } else {
1957 if (use_imm)
1958 __ Daddiu(dst, lhs, -rhs_imm);
1959 else
1960 __ Dsubu(dst, lhs, rhs_reg);
1961 }
1962 }
1963 break;
1964 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001965 case DataType::Type::kFloat32:
1966 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1968 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1969 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1970 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001971 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001972 __ AddS(dst, lhs, rhs);
1973 else
1974 __ AddD(dst, lhs, rhs);
1975 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001977 __ SubS(dst, lhs, rhs);
1978 else
1979 __ SubD(dst, lhs, rhs);
1980 } else {
1981 LOG(FATAL) << "Unexpected floating-point binary operation";
1982 }
1983 break;
1984 }
1985 default:
1986 LOG(FATAL) << "Unexpected binary operation type " << type;
1987 }
1988}
1989
1990void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001991 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001992
Vladimir Markoca6fff82017-10-03 14:49:14 +01001993 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001994 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001995 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996 case DataType::Type::kInt32:
1997 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001998 locations->SetInAt(0, Location::RequiresRegister());
1999 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002000 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002001 break;
2002 }
2003 default:
2004 LOG(FATAL) << "Unexpected shift type " << type;
2005 }
2006}
2007
2008void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002009 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002010 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002011 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002012
2013 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 case DataType::Type::kInt32:
2015 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002016 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2017 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2018 Location rhs_location = locations->InAt(1);
2019
2020 GpuRegister rhs_reg = ZERO;
2021 int64_t rhs_imm = 0;
2022 bool use_imm = rhs_location.IsConstant();
2023 if (use_imm) {
2024 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2025 } else {
2026 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2027 }
2028
2029 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002030 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002031 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032
Alexey Frunze92d90602015-12-18 18:16:36 -08002033 if (shift_value == 0) {
2034 if (dst != lhs) {
2035 __ Move(dst, lhs);
2036 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002037 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002038 if (instr->IsShl()) {
2039 __ Sll(dst, lhs, shift_value);
2040 } else if (instr->IsShr()) {
2041 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002042 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002044 } else {
2045 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002046 }
2047 } else {
2048 if (shift_value < 32) {
2049 if (instr->IsShl()) {
2050 __ Dsll(dst, lhs, shift_value);
2051 } else if (instr->IsShr()) {
2052 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002053 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002054 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002055 } else {
2056 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002057 }
2058 } else {
2059 shift_value -= 32;
2060 if (instr->IsShl()) {
2061 __ Dsll32(dst, lhs, shift_value);
2062 } else if (instr->IsShr()) {
2063 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002064 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002065 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002066 } else {
2067 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002068 }
2069 }
2070 }
2071 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002072 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002073 if (instr->IsShl()) {
2074 __ Sllv(dst, lhs, rhs_reg);
2075 } else if (instr->IsShr()) {
2076 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002077 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002078 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002079 } else {
2080 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002081 }
2082 } else {
2083 if (instr->IsShl()) {
2084 __ Dsllv(dst, lhs, rhs_reg);
2085 } else if (instr->IsShr()) {
2086 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002087 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002088 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002089 } else {
2090 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002091 }
2092 }
2093 }
2094 break;
2095 }
2096 default:
2097 LOG(FATAL) << "Unexpected shift operation type " << type;
2098 }
2099}
2100
2101void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2102 HandleBinaryOp(instruction);
2103}
2104
2105void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2106 HandleBinaryOp(instruction);
2107}
2108
2109void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2110 HandleBinaryOp(instruction);
2111}
2112
2113void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2114 HandleBinaryOp(instruction);
2115}
2116
2117void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002119 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002122 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2123 object_array_get_with_read_barrier
2124 ? LocationSummary::kCallOnSlowPath
2125 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002126 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2127 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2128 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002129 locations->SetInAt(0, Location::RequiresRegister());
2130 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002132 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2133 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002134 // The output overlaps in the case of an object array get with
2135 // read barriers enabled: we do not want the move to overwrite the
2136 // array's location, as we need it to emit the read barrier.
2137 locations->SetOut(Location::RequiresRegister(),
2138 object_array_get_with_read_barrier
2139 ? Location::kOutputOverlap
2140 : Location::kNoOutputOverlap);
2141 }
2142 // We need a temporary register for the read barrier marking slow
2143 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2144 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002145 bool temp_needed = instruction->GetIndex()->IsConstant()
2146 ? !kBakerReadBarrierThunksEnableForFields
2147 : !kBakerReadBarrierThunksEnableForArrays;
2148 if (temp_needed) {
2149 locations->AddTemp(Location::RequiresRegister());
2150 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002151 }
2152}
2153
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002154static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2155 auto null_checker = [codegen, instruction]() {
2156 codegen->MaybeRecordImplicitNullCheck(instruction);
2157 };
2158 return null_checker;
2159}
2160
Alexey Frunze4dda3372015-06-01 18:31:49 -07002161void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2162 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002163 Location obj_loc = locations->InAt(0);
2164 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2165 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002167 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002168 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002170 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002171 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2172 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002173 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002174 case DataType::Type::kBool:
2175 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002176 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002177 if (index.IsConstant()) {
2178 size_t offset =
2179 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002180 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002181 } else {
2182 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002183 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002184 }
2185 break;
2186 }
2187
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002188 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002189 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 if (index.IsConstant()) {
2191 size_t offset =
2192 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002193 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002194 } else {
2195 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002196 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002197 }
2198 break;
2199 }
2200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002202 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002203 if (maybe_compressed_char_at) {
2204 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002205 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002206 __ Dext(TMP, TMP, 0, 1);
2207 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2208 "Expecting 0=compressed, 1=uncompressed");
2209 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002210 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002211 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2212 if (maybe_compressed_char_at) {
2213 Mips64Label uncompressed_load, done;
2214 __ Bnezc(TMP, &uncompressed_load);
2215 __ LoadFromOffset(kLoadUnsignedByte,
2216 out,
2217 obj,
2218 data_offset + (const_index << TIMES_1));
2219 __ Bc(&done);
2220 __ Bind(&uncompressed_load);
2221 __ LoadFromOffset(kLoadUnsignedHalfword,
2222 out,
2223 obj,
2224 data_offset + (const_index << TIMES_2));
2225 __ Bind(&done);
2226 } else {
2227 __ LoadFromOffset(kLoadUnsignedHalfword,
2228 out,
2229 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002230 data_offset + (const_index << TIMES_2),
2231 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002232 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002233 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002234 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2235 if (maybe_compressed_char_at) {
2236 Mips64Label uncompressed_load, done;
2237 __ Bnezc(TMP, &uncompressed_load);
2238 __ Daddu(TMP, obj, index_reg);
2239 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2240 __ Bc(&done);
2241 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002242 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002243 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2244 __ Bind(&done);
2245 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002246 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002247 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002248 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002249 }
2250 break;
2251 }
2252
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002253 case DataType::Type::kInt16: {
2254 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2255 if (index.IsConstant()) {
2256 size_t offset =
2257 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2258 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2259 } else {
2260 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2261 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2262 }
2263 break;
2264 }
2265
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002268 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002269 LoadOperandType load_type =
2270 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002271 if (index.IsConstant()) {
2272 size_t offset =
2273 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002274 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002275 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002276 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002277 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002278 }
2279 break;
2280 }
2281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002283 static_assert(
2284 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2285 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2286 // /* HeapReference<Object> */ out =
2287 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2288 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002289 bool temp_needed = index.IsConstant()
2290 ? !kBakerReadBarrierThunksEnableForFields
2291 : !kBakerReadBarrierThunksEnableForArrays;
2292 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002293 // Note that a potential implicit null check is handled in this
2294 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002295 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2296 if (index.IsConstant()) {
2297 // Array load with a constant index can be treated as a field load.
2298 size_t offset =
2299 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2300 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2301 out_loc,
2302 obj,
2303 offset,
2304 temp,
2305 /* needs_null_check */ false);
2306 } else {
2307 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2308 out_loc,
2309 obj,
2310 data_offset,
2311 index,
2312 temp,
2313 /* needs_null_check */ false);
2314 }
Alexey Frunze15958152017-02-09 19:08:30 -08002315 } else {
2316 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2317 if (index.IsConstant()) {
2318 size_t offset =
2319 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2320 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2321 // If read barriers are enabled, emit read barriers other than
2322 // Baker's using a slow path (and also unpoison the loaded
2323 // reference, if heap poisoning is enabled).
2324 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2325 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002326 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002327 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2328 // If read barriers are enabled, emit read barriers other than
2329 // Baker's using a slow path (and also unpoison the loaded
2330 // reference, if heap poisoning is enabled).
2331 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2332 out_loc,
2333 out_loc,
2334 obj_loc,
2335 data_offset,
2336 index);
2337 }
2338 }
2339 break;
2340 }
2341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002343 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002344 if (index.IsConstant()) {
2345 size_t offset =
2346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002347 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002348 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002349 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002350 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002351 }
2352 break;
2353 }
2354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002356 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002357 if (index.IsConstant()) {
2358 size_t offset =
2359 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002360 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002361 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002362 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002363 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002364 }
2365 break;
2366 }
2367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002369 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002370 if (index.IsConstant()) {
2371 size_t offset =
2372 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002373 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002374 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002375 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002376 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377 }
2378 break;
2379 }
2380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002382 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2383 UNREACHABLE();
2384 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002385}
2386
2387void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002388 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002389 locations->SetInAt(0, Location::RequiresRegister());
2390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2391}
2392
2393void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2394 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002395 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002396 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2397 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2398 __ LoadFromOffset(kLoadWord, out, obj, offset);
2399 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002400 // Mask out compression flag from String's array length.
2401 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2402 __ Srl(out, out, 1u);
2403 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002404}
2405
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002406Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2407 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2408 ? Location::ConstantLocation(instruction->AsConstant())
2409 : Location::RequiresRegister();
2410}
2411
2412Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2413 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2414 // We can store a non-zero float or double constant without first loading it into the FPU,
2415 // but we should only prefer this if the constant has a single use.
2416 if (instruction->IsConstant() &&
2417 (instruction->AsConstant()->IsZeroBitPattern() ||
2418 instruction->GetUses().HasExactlyOneElement())) {
2419 return Location::ConstantLocation(instruction->AsConstant());
2420 // Otherwise fall through and require an FPU register for the constant.
2421 }
2422 return Location::RequiresFpuRegister();
2423}
2424
Alexey Frunze4dda3372015-06-01 18:31:49 -07002425void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002427
2428 bool needs_write_barrier =
2429 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2430 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2431
Vladimir Markoca6fff82017-10-03 14:49:14 +01002432 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002433 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002434 may_need_runtime_call_for_type_check ?
2435 LocationSummary::kCallOnSlowPath :
2436 LocationSummary::kNoCall);
2437
2438 locations->SetInAt(0, Location::RequiresRegister());
2439 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002441 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002442 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002443 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2444 }
2445 if (needs_write_barrier) {
2446 // Temporary register for the write barrier.
2447 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448 }
2449}
2450
2451void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2452 LocationSummary* locations = instruction->GetLocations();
2453 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2454 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002455 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002457 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002458 bool needs_write_barrier =
2459 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002460 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002461 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002462
2463 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002464 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002465 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002467 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002469 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002470 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002471 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2472 }
2473 if (value_location.IsConstant()) {
2474 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2475 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2476 } else {
2477 GpuRegister value = value_location.AsRegister<GpuRegister>();
2478 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002479 }
2480 break;
2481 }
2482
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002483 case DataType::Type::kUint16:
2484 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002486 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002487 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002488 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002489 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002490 }
2491 if (value_location.IsConstant()) {
2492 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2493 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2494 } else {
2495 GpuRegister value = value_location.AsRegister<GpuRegister>();
2496 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002497 }
2498 break;
2499 }
2500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002502 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2503 if (index.IsConstant()) {
2504 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2505 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002506 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002507 }
2508 if (value_location.IsConstant()) {
2509 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2510 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2511 } else {
2512 GpuRegister value = value_location.AsRegister<GpuRegister>();
2513 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2514 }
2515 break;
2516 }
2517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002519 if (value_location.IsConstant()) {
2520 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002522 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002523 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002524 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002525 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002526 }
Alexey Frunze15958152017-02-09 19:08:30 -08002527 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2528 DCHECK_EQ(value, 0);
2529 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2530 DCHECK(!needs_write_barrier);
2531 DCHECK(!may_need_runtime_call_for_type_check);
2532 break;
2533 }
2534
2535 DCHECK(needs_write_barrier);
2536 GpuRegister value = value_location.AsRegister<GpuRegister>();
2537 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2538 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2539 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2540 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2541 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2542 Mips64Label done;
2543 SlowPathCodeMIPS64* slow_path = nullptr;
2544
2545 if (may_need_runtime_call_for_type_check) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002546 slow_path = new (GetGraph()->GetAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002547 codegen_->AddSlowPath(slow_path);
2548 if (instruction->GetValueCanBeNull()) {
2549 Mips64Label non_zero;
2550 __ Bnezc(value, &non_zero);
2551 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2552 if (index.IsConstant()) {
2553 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002554 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002555 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002556 }
Alexey Frunze15958152017-02-09 19:08:30 -08002557 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2558 __ Bc(&done);
2559 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560 }
Alexey Frunze15958152017-02-09 19:08:30 -08002561
2562 // Note that when read barriers are enabled, the type checks
2563 // are performed without read barriers. This is fine, even in
2564 // the case where a class object is in the from-space after
2565 // the flip, as a comparison involving such a type would not
2566 // produce a false positive; it may of course produce a false
2567 // negative, in which case we would take the ArraySet slow
2568 // path.
2569
2570 // /* HeapReference<Class> */ temp1 = obj->klass_
2571 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2572 __ MaybeUnpoisonHeapReference(temp1);
2573
2574 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2575 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2576 // /* HeapReference<Class> */ temp2 = value->klass_
2577 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2578 // If heap poisoning is enabled, no need to unpoison `temp1`
2579 // nor `temp2`, as we are comparing two poisoned references.
2580
2581 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2582 Mips64Label do_put;
2583 __ Beqc(temp1, temp2, &do_put);
2584 // If heap poisoning is enabled, the `temp1` reference has
2585 // not been unpoisoned yet; unpoison it now.
2586 __ MaybeUnpoisonHeapReference(temp1);
2587
2588 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2589 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2590 // If heap poisoning is enabled, no need to unpoison
2591 // `temp1`, as we are comparing against null below.
2592 __ Bnezc(temp1, slow_path->GetEntryLabel());
2593 __ Bind(&do_put);
2594 } else {
2595 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2596 }
2597 }
2598
2599 GpuRegister source = value;
2600 if (kPoisonHeapReferences) {
2601 // Note that in the case where `value` is a null reference,
2602 // we do not enter this block, as a null reference does not
2603 // need poisoning.
2604 __ Move(temp1, value);
2605 __ PoisonHeapReference(temp1);
2606 source = temp1;
2607 }
2608
2609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2610 if (index.IsConstant()) {
2611 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002612 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002613 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002614 }
2615 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2616
2617 if (!may_need_runtime_call_for_type_check) {
2618 codegen_->MaybeRecordImplicitNullCheck(instruction);
2619 }
2620
2621 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2622
2623 if (done.IsLinked()) {
2624 __ Bind(&done);
2625 }
2626
2627 if (slow_path != nullptr) {
2628 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002629 }
2630 break;
2631 }
2632
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002633 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002635 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002636 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002637 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002638 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002639 }
2640 if (value_location.IsConstant()) {
2641 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2642 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2643 } else {
2644 GpuRegister value = value_location.AsRegister<GpuRegister>();
2645 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002646 }
2647 break;
2648 }
2649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002651 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002652 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002653 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002655 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002656 }
2657 if (value_location.IsConstant()) {
2658 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2659 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2660 } else {
2661 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2662 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002663 }
2664 break;
2665 }
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002668 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002669 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002670 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002671 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002672 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002673 }
2674 if (value_location.IsConstant()) {
2675 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2676 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2677 } else {
2678 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2679 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002680 }
2681 break;
2682 }
2683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002685 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2686 UNREACHABLE();
2687 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002688}
2689
2690void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002691 RegisterSet caller_saves = RegisterSet::Empty();
2692 InvokeRuntimeCallingConvention calling_convention;
2693 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2694 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2695 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002698}
2699
2700void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2701 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002702 BoundsCheckSlowPathMIPS64* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002703 new (GetGraph()->GetAllocator()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002704 codegen_->AddSlowPath(slow_path);
2705
2706 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2707 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2708
2709 // length is limited by the maximum positive signed 32-bit integer.
2710 // Unsigned comparison of length and index checks for index < 0
2711 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002712 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002713}
2714
Alexey Frunze15958152017-02-09 19:08:30 -08002715// Temp is used for read barrier.
2716static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2717 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002718 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002719 (kUseBakerReadBarrier ||
2720 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2721 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2722 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2723 return 1;
2724 }
2725 return 0;
2726}
2727
2728// Extra temp is used for read barrier.
2729static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2730 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2731}
2732
Alexey Frunze4dda3372015-06-01 18:31:49 -07002733void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002734 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2735 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2736
2737 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2738 switch (type_check_kind) {
2739 case TypeCheckKind::kExactCheck:
2740 case TypeCheckKind::kAbstractClassCheck:
2741 case TypeCheckKind::kClassHierarchyCheck:
2742 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002743 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002744 ? LocationSummary::kCallOnSlowPath
2745 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2746 break;
2747 case TypeCheckKind::kArrayCheck:
2748 case TypeCheckKind::kUnresolvedCheck:
2749 case TypeCheckKind::kInterfaceCheck:
2750 call_kind = LocationSummary::kCallOnSlowPath;
2751 break;
2752 }
2753
Vladimir Markoca6fff82017-10-03 14:49:14 +01002754 LocationSummary* locations =
2755 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002756 locations->SetInAt(0, Location::RequiresRegister());
2757 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002758 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002759}
2760
2761void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002762 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002763 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002764 Location obj_loc = locations->InAt(0);
2765 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002766 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002767 Location temp_loc = locations->GetTemp(0);
2768 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2769 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2770 DCHECK_LE(num_temps, 2u);
2771 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002772 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2773 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2774 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2775 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2776 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2777 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2778 const uint32_t object_array_data_offset =
2779 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2780 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002781
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002782 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2783 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2784 // read barriers is done for performance and code size reasons.
2785 bool is_type_check_slow_path_fatal = false;
2786 if (!kEmitCompilerReadBarrier) {
2787 is_type_check_slow_path_fatal =
2788 (type_check_kind == TypeCheckKind::kExactCheck ||
2789 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2790 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2791 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2792 !instruction->CanThrowIntoCatchBlock();
2793 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002794 SlowPathCodeMIPS64* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002795 new (GetGraph()->GetAllocator()) TypeCheckSlowPathMIPS64(instruction,
2796 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002797 codegen_->AddSlowPath(slow_path);
2798
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002799 // Avoid this check if we know `obj` is not null.
2800 if (instruction->MustDoNullCheck()) {
2801 __ Beqzc(obj, &done);
2802 }
2803
2804 switch (type_check_kind) {
2805 case TypeCheckKind::kExactCheck:
2806 case TypeCheckKind::kArrayCheck: {
2807 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002808 GenerateReferenceLoadTwoRegisters(instruction,
2809 temp_loc,
2810 obj_loc,
2811 class_offset,
2812 maybe_temp2_loc,
2813 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002814 // Jump to slow path for throwing the exception or doing a
2815 // more involved array check.
2816 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2817 break;
2818 }
2819
2820 case TypeCheckKind::kAbstractClassCheck: {
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 // If the class is abstract, we eagerly fetch the super class of the
2829 // object to avoid doing a comparison we know will fail.
2830 Mips64Label loop;
2831 __ Bind(&loop);
2832 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002833 GenerateReferenceLoadOneRegister(instruction,
2834 temp_loc,
2835 super_offset,
2836 maybe_temp2_loc,
2837 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002838 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2839 // exception.
2840 __ Beqzc(temp, slow_path->GetEntryLabel());
2841 // Otherwise, compare the classes.
2842 __ Bnec(temp, cls, &loop);
2843 break;
2844 }
2845
2846 case TypeCheckKind::kClassHierarchyCheck: {
2847 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002848 GenerateReferenceLoadTwoRegisters(instruction,
2849 temp_loc,
2850 obj_loc,
2851 class_offset,
2852 maybe_temp2_loc,
2853 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002854 // Walk over the class hierarchy to find a match.
2855 Mips64Label loop;
2856 __ Bind(&loop);
2857 __ Beqc(temp, cls, &done);
2858 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002859 GenerateReferenceLoadOneRegister(instruction,
2860 temp_loc,
2861 super_offset,
2862 maybe_temp2_loc,
2863 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002864 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2865 // exception. Otherwise, jump to the beginning of the loop.
2866 __ Bnezc(temp, &loop);
2867 __ Bc(slow_path->GetEntryLabel());
2868 break;
2869 }
2870
2871 case TypeCheckKind::kArrayObjectCheck: {
2872 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002873 GenerateReferenceLoadTwoRegisters(instruction,
2874 temp_loc,
2875 obj_loc,
2876 class_offset,
2877 maybe_temp2_loc,
2878 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002879 // Do an exact check.
2880 __ Beqc(temp, cls, &done);
2881 // Otherwise, we need to check that the object's class is a non-primitive array.
2882 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002883 GenerateReferenceLoadOneRegister(instruction,
2884 temp_loc,
2885 component_offset,
2886 maybe_temp2_loc,
2887 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002888 // If the component type is null, jump to the slow path to throw the exception.
2889 __ Beqzc(temp, slow_path->GetEntryLabel());
2890 // Otherwise, the object is indeed an array, further check that this component
2891 // type is not a primitive type.
2892 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2893 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2894 __ Bnezc(temp, slow_path->GetEntryLabel());
2895 break;
2896 }
2897
2898 case TypeCheckKind::kUnresolvedCheck:
2899 // We always go into the type check slow path for the unresolved check case.
2900 // We cannot directly call the CheckCast runtime entry point
2901 // without resorting to a type checking slow path here (i.e. by
2902 // calling InvokeRuntime directly), as it would require to
2903 // assign fixed registers for the inputs of this HInstanceOf
2904 // instruction (following the runtime calling convention), which
2905 // might be cluttered by the potential first read barrier
2906 // emission at the beginning of this method.
2907 __ Bc(slow_path->GetEntryLabel());
2908 break;
2909
2910 case TypeCheckKind::kInterfaceCheck: {
2911 // Avoid read barriers to improve performance of the fast path. We can not get false
2912 // positives by doing this.
2913 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002914 GenerateReferenceLoadTwoRegisters(instruction,
2915 temp_loc,
2916 obj_loc,
2917 class_offset,
2918 maybe_temp2_loc,
2919 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002920 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002921 GenerateReferenceLoadTwoRegisters(instruction,
2922 temp_loc,
2923 temp_loc,
2924 iftable_offset,
2925 maybe_temp2_loc,
2926 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002927 // Iftable is never null.
2928 __ Lw(TMP, temp, array_length_offset);
2929 // Loop through the iftable and check if any class matches.
2930 Mips64Label loop;
2931 __ Bind(&loop);
2932 __ Beqzc(TMP, slow_path->GetEntryLabel());
2933 __ Lwu(AT, temp, object_array_data_offset);
2934 __ MaybeUnpoisonHeapReference(AT);
2935 // Go to next interface.
2936 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2937 __ Addiu(TMP, TMP, -2);
2938 // Compare the classes and continue the loop if they do not match.
2939 __ Bnec(AT, cls, &loop);
2940 break;
2941 }
2942 }
2943
2944 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002945 __ Bind(slow_path->GetExitLabel());
2946}
2947
2948void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2949 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002950 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002951 locations->SetInAt(0, Location::RequiresRegister());
2952 if (check->HasUses()) {
2953 locations->SetOut(Location::SameAsFirstInput());
2954 }
2955}
2956
2957void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2958 // We assume the class is not null.
Vladimir Markoca6fff82017-10-03 14:49:14 +01002959 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 check->GetLoadClass(),
2961 check,
2962 check->GetDexPc(),
2963 true);
2964 codegen_->AddSlowPath(slow_path);
2965 GenerateClassInitializationCheck(slow_path,
2966 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2967}
2968
2969void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002971
Vladimir Markoca6fff82017-10-03 14:49:14 +01002972 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002973
2974 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002975 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002976 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002978 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002979 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002980 case DataType::Type::kInt32:
2981 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002982 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002983 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2985 break;
2986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002987 case DataType::Type::kFloat32:
2988 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08002989 locations->SetInAt(0, Location::RequiresFpuRegister());
2990 locations->SetInAt(1, Location::RequiresFpuRegister());
2991 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002992 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002993
2994 default:
2995 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2996 }
2997}
2998
2999void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3000 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003001 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003003
3004 // 0 if: left == right
3005 // 1 if: left > right
3006 // -1 if: left < right
3007 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003008 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003009 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003011 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003012 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003013 case DataType::Type::kInt32:
3014 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003015 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003016 Location rhs_location = locations->InAt(1);
3017 bool use_imm = rhs_location.IsConstant();
3018 GpuRegister rhs = ZERO;
3019 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003021 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3022 if (value != 0) {
3023 rhs = AT;
3024 __ LoadConst64(rhs, value);
3025 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003026 } else {
3027 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3028 if (value != 0) {
3029 rhs = AT;
3030 __ LoadConst32(rhs, value);
3031 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003032 }
3033 } else {
3034 rhs = rhs_location.AsRegister<GpuRegister>();
3035 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003036 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003037 __ Slt(res, rhs, lhs);
3038 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039 break;
3040 }
3041
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003042 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003043 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3044 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3045 Mips64Label done;
3046 __ CmpEqS(FTMP, lhs, rhs);
3047 __ LoadConst32(res, 0);
3048 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003049 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003050 __ CmpLtS(FTMP, lhs, rhs);
3051 __ LoadConst32(res, -1);
3052 __ Bc1nez(FTMP, &done);
3053 __ LoadConst32(res, 1);
3054 } else {
3055 __ CmpLtS(FTMP, rhs, lhs);
3056 __ LoadConst32(res, 1);
3057 __ Bc1nez(FTMP, &done);
3058 __ LoadConst32(res, -1);
3059 }
3060 __ Bind(&done);
3061 break;
3062 }
3063
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003064 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003065 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3066 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3067 Mips64Label done;
3068 __ CmpEqD(FTMP, lhs, rhs);
3069 __ LoadConst32(res, 0);
3070 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003071 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003072 __ CmpLtD(FTMP, lhs, rhs);
3073 __ LoadConst32(res, -1);
3074 __ Bc1nez(FTMP, &done);
3075 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003076 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003077 __ CmpLtD(FTMP, rhs, lhs);
3078 __ LoadConst32(res, 1);
3079 __ Bc1nez(FTMP, &done);
3080 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003081 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003082 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003083 break;
3084 }
3085
3086 default:
3087 LOG(FATAL) << "Unimplemented compare type " << in_type;
3088 }
3089}
3090
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003091void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003092 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003093 switch (instruction->InputAt(0)->GetType()) {
3094 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003095 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003096 locations->SetInAt(0, Location::RequiresRegister());
3097 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3098 break;
3099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003100 case DataType::Type::kFloat32:
3101 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003102 locations->SetInAt(0, Location::RequiresFpuRegister());
3103 locations->SetInAt(1, Location::RequiresFpuRegister());
3104 break;
3105 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003106 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003107 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3108 }
3109}
3110
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003111void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003112 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003113 return;
3114 }
3115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003117 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003118 switch (type) {
3119 default:
3120 // Integer case.
3121 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3122 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003124 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3125 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 case DataType::Type::kFloat32:
3127 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003128 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3129 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003130 }
3131}
3132
Alexey Frunzec857c742015-09-23 15:12:39 -07003133void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3134 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003136
3137 LocationSummary* locations = instruction->GetLocations();
3138 Location second = locations->InAt(1);
3139 DCHECK(second.IsConstant());
3140
3141 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3142 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3143 int64_t imm = Int64FromConstant(second.GetConstant());
3144 DCHECK(imm == 1 || imm == -1);
3145
3146 if (instruction->IsRem()) {
3147 __ Move(out, ZERO);
3148 } else {
3149 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003151 __ Subu(out, ZERO, dividend);
3152 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003154 __ Dsubu(out, ZERO, dividend);
3155 }
3156 } else if (out != dividend) {
3157 __ Move(out, dividend);
3158 }
3159 }
3160}
3161
3162void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3163 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003164 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003165
3166 LocationSummary* locations = instruction->GetLocations();
3167 Location second = locations->InAt(1);
3168 DCHECK(second.IsConstant());
3169
3170 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3171 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3172 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003173 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003174 int ctz_imm = CTZ(abs_imm);
3175
3176 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003177 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003178 if (ctz_imm == 1) {
3179 // Fast path for division by +/-2, which is very common.
3180 __ Srl(TMP, dividend, 31);
3181 } else {
3182 __ Sra(TMP, dividend, 31);
3183 __ Srl(TMP, TMP, 32 - ctz_imm);
3184 }
3185 __ Addu(out, dividend, TMP);
3186 __ Sra(out, out, ctz_imm);
3187 if (imm < 0) {
3188 __ Subu(out, ZERO, out);
3189 }
3190 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003192 if (ctz_imm == 1) {
3193 // Fast path for division by +/-2, which is very common.
3194 __ Dsrl32(TMP, dividend, 31);
3195 } else {
3196 __ Dsra32(TMP, dividend, 31);
3197 if (ctz_imm > 32) {
3198 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3199 } else {
3200 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3201 }
3202 }
3203 __ Daddu(out, dividend, TMP);
3204 if (ctz_imm < 32) {
3205 __ Dsra(out, out, ctz_imm);
3206 } else {
3207 __ Dsra32(out, out, ctz_imm - 32);
3208 }
3209 if (imm < 0) {
3210 __ Dsubu(out, ZERO, out);
3211 }
3212 }
3213 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003215 if (ctz_imm == 1) {
3216 // Fast path for modulo +/-2, which is very common.
3217 __ Sra(TMP, dividend, 31);
3218 __ Subu(out, dividend, TMP);
3219 __ Andi(out, out, 1);
3220 __ Addu(out, out, TMP);
3221 } else {
3222 __ Sra(TMP, dividend, 31);
3223 __ Srl(TMP, TMP, 32 - ctz_imm);
3224 __ Addu(out, dividend, TMP);
3225 if (IsUint<16>(abs_imm - 1)) {
3226 __ Andi(out, out, abs_imm - 1);
3227 } else {
3228 __ Sll(out, out, 32 - ctz_imm);
3229 __ Srl(out, out, 32 - ctz_imm);
3230 }
3231 __ Subu(out, out, TMP);
3232 }
3233 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003234 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003235 if (ctz_imm == 1) {
3236 // Fast path for modulo +/-2, which is very common.
3237 __ Dsra32(TMP, dividend, 31);
3238 __ Dsubu(out, dividend, TMP);
3239 __ Andi(out, out, 1);
3240 __ Daddu(out, out, TMP);
3241 } else {
3242 __ Dsra32(TMP, dividend, 31);
3243 if (ctz_imm > 32) {
3244 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3245 } else {
3246 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3247 }
3248 __ Daddu(out, dividend, TMP);
3249 if (IsUint<16>(abs_imm - 1)) {
3250 __ Andi(out, out, abs_imm - 1);
3251 } else {
3252 if (ctz_imm > 32) {
3253 __ Dsll(out, out, 64 - ctz_imm);
3254 __ Dsrl(out, out, 64 - ctz_imm);
3255 } else {
3256 __ Dsll32(out, out, 32 - ctz_imm);
3257 __ Dsrl32(out, out, 32 - ctz_imm);
3258 }
3259 }
3260 __ Dsubu(out, out, TMP);
3261 }
3262 }
3263 }
3264}
3265
3266void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3267 DCHECK(instruction->IsDiv() || instruction->IsRem());
3268
3269 LocationSummary* locations = instruction->GetLocations();
3270 Location second = locations->InAt(1);
3271 DCHECK(second.IsConstant());
3272
3273 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3274 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3275 int64_t imm = Int64FromConstant(second.GetConstant());
3276
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003277 DataType::Type type = instruction->GetResultType();
3278 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003279
3280 int64_t magic;
3281 int shift;
3282 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003283 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003284 &magic,
3285 &shift);
3286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003288 __ LoadConst32(TMP, magic);
3289 __ MuhR6(TMP, dividend, TMP);
3290
3291 if (imm > 0 && magic < 0) {
3292 __ Addu(TMP, TMP, dividend);
3293 } else if (imm < 0 && magic > 0) {
3294 __ Subu(TMP, TMP, dividend);
3295 }
3296
3297 if (shift != 0) {
3298 __ Sra(TMP, TMP, shift);
3299 }
3300
3301 if (instruction->IsDiv()) {
3302 __ Sra(out, TMP, 31);
3303 __ Subu(out, TMP, out);
3304 } else {
3305 __ Sra(AT, TMP, 31);
3306 __ Subu(AT, TMP, AT);
3307 __ LoadConst32(TMP, imm);
3308 __ MulR6(TMP, AT, TMP);
3309 __ Subu(out, dividend, TMP);
3310 }
3311 } else {
3312 __ LoadConst64(TMP, magic);
3313 __ Dmuh(TMP, dividend, TMP);
3314
3315 if (imm > 0 && magic < 0) {
3316 __ Daddu(TMP, TMP, dividend);
3317 } else if (imm < 0 && magic > 0) {
3318 __ Dsubu(TMP, TMP, dividend);
3319 }
3320
3321 if (shift >= 32) {
3322 __ Dsra32(TMP, TMP, shift - 32);
3323 } else if (shift > 0) {
3324 __ Dsra(TMP, TMP, shift);
3325 }
3326
3327 if (instruction->IsDiv()) {
3328 __ Dsra32(out, TMP, 31);
3329 __ Dsubu(out, TMP, out);
3330 } else {
3331 __ Dsra32(AT, TMP, 31);
3332 __ Dsubu(AT, TMP, AT);
3333 __ LoadConst64(TMP, imm);
3334 __ Dmul(TMP, AT, TMP);
3335 __ Dsubu(out, dividend, TMP);
3336 }
3337 }
3338}
3339
3340void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3341 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 DataType::Type type = instruction->GetResultType();
3343 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003344
3345 LocationSummary* locations = instruction->GetLocations();
3346 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3347 Location second = locations->InAt(1);
3348
3349 if (second.IsConstant()) {
3350 int64_t imm = Int64FromConstant(second.GetConstant());
3351 if (imm == 0) {
3352 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3353 } else if (imm == 1 || imm == -1) {
3354 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003355 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003356 DivRemByPowerOfTwo(instruction);
3357 } else {
3358 DCHECK(imm <= -2 || imm >= 2);
3359 GenerateDivRemWithAnyConstant(instruction);
3360 }
3361 } else {
3362 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3363 GpuRegister divisor = second.AsRegister<GpuRegister>();
3364 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003365 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003366 __ DivR6(out, dividend, divisor);
3367 else
3368 __ Ddiv(out, dividend, divisor);
3369 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003370 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003371 __ ModR6(out, dividend, divisor);
3372 else
3373 __ Dmod(out, dividend, divisor);
3374 }
3375 }
3376}
3377
Alexey Frunze4dda3372015-06-01 18:31:49 -07003378void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3379 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003380 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003381 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003382 case DataType::Type::kInt32:
3383 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003384 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003385 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3387 break;
3388
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003389 case DataType::Type::kFloat32:
3390 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003391 locations->SetInAt(0, Location::RequiresFpuRegister());
3392 locations->SetInAt(1, Location::RequiresFpuRegister());
3393 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3394 break;
3395
3396 default:
3397 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3398 }
3399}
3400
3401void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003403 LocationSummary* locations = instruction->GetLocations();
3404
3405 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003406 case DataType::Type::kInt32:
3407 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003408 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003409 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003410 case DataType::Type::kFloat32:
3411 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003412 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3413 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3414 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003415 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003416 __ DivS(dst, lhs, rhs);
3417 else
3418 __ DivD(dst, lhs, rhs);
3419 break;
3420 }
3421 default:
3422 LOG(FATAL) << "Unexpected div type " << type;
3423 }
3424}
3425
3426void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003427 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003428 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003429}
3430
3431void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3432 SlowPathCodeMIPS64* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003433 new (GetGraph()->GetAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003434 codegen_->AddSlowPath(slow_path);
3435 Location value = instruction->GetLocations()->InAt(0);
3436
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003437 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003439 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003440 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003441 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003442 }
3443
3444 if (value.IsConstant()) {
3445 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3446 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003447 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003448 } else {
3449 // A division by a non-null constant is valid. We don't need to perform
3450 // any check, so simply fall through.
3451 }
3452 } else {
3453 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3454 }
3455}
3456
3457void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3458 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003459 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003460 locations->SetOut(Location::ConstantLocation(constant));
3461}
3462
3463void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3464 // Will be generated at use site.
3465}
3466
3467void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3468 exit->SetLocations(nullptr);
3469}
3470
3471void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3472}
3473
3474void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3475 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003476 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003477 locations->SetOut(Location::ConstantLocation(constant));
3478}
3479
3480void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3481 // Will be generated at use site.
3482}
3483
David Brazdilfc6a86a2015-06-26 10:33:45 +00003484void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003485 DCHECK(!successor->IsExitBlock());
3486 HBasicBlock* block = got->GetBlock();
3487 HInstruction* previous = got->GetPrevious();
3488 HLoopInformation* info = block->GetLoopInformation();
3489
3490 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003491 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3492 return;
3493 }
3494 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3495 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3496 }
3497 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003498 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003499 }
3500}
3501
David Brazdilfc6a86a2015-06-26 10:33:45 +00003502void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3503 got->SetLocations(nullptr);
3504}
3505
3506void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3507 HandleGoto(got, got->GetSuccessor());
3508}
3509
3510void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3511 try_boundary->SetLocations(nullptr);
3512}
3513
3514void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3515 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3516 if (!successor->IsExitBlock()) {
3517 HandleGoto(try_boundary, successor);
3518 }
3519}
3520
Alexey Frunze299a9392015-12-08 16:08:02 -08003521void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3522 bool is64bit,
3523 LocationSummary* locations) {
3524 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3525 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3526 Location rhs_location = locations->InAt(1);
3527 GpuRegister rhs_reg = ZERO;
3528 int64_t rhs_imm = 0;
3529 bool use_imm = rhs_location.IsConstant();
3530 if (use_imm) {
3531 if (is64bit) {
3532 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3533 } else {
3534 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3535 }
3536 } else {
3537 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3538 }
3539 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3540
3541 switch (cond) {
3542 case kCondEQ:
3543 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003544 if (use_imm && IsInt<16>(-rhs_imm)) {
3545 if (rhs_imm == 0) {
3546 if (cond == kCondEQ) {
3547 __ Sltiu(dst, lhs, 1);
3548 } else {
3549 __ Sltu(dst, ZERO, lhs);
3550 }
3551 } else {
3552 if (is64bit) {
3553 __ Daddiu(dst, lhs, -rhs_imm);
3554 } else {
3555 __ Addiu(dst, lhs, -rhs_imm);
3556 }
3557 if (cond == kCondEQ) {
3558 __ Sltiu(dst, dst, 1);
3559 } else {
3560 __ Sltu(dst, ZERO, dst);
3561 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003562 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003563 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003564 if (use_imm && IsUint<16>(rhs_imm)) {
3565 __ Xori(dst, lhs, rhs_imm);
3566 } else {
3567 if (use_imm) {
3568 rhs_reg = TMP;
3569 __ LoadConst64(rhs_reg, rhs_imm);
3570 }
3571 __ Xor(dst, lhs, rhs_reg);
3572 }
3573 if (cond == kCondEQ) {
3574 __ Sltiu(dst, dst, 1);
3575 } else {
3576 __ Sltu(dst, ZERO, dst);
3577 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003578 }
3579 break;
3580
3581 case kCondLT:
3582 case kCondGE:
3583 if (use_imm && IsInt<16>(rhs_imm)) {
3584 __ Slti(dst, lhs, rhs_imm);
3585 } else {
3586 if (use_imm) {
3587 rhs_reg = TMP;
3588 __ LoadConst64(rhs_reg, rhs_imm);
3589 }
3590 __ Slt(dst, lhs, rhs_reg);
3591 }
3592 if (cond == kCondGE) {
3593 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3594 // only the slt instruction but no sge.
3595 __ Xori(dst, dst, 1);
3596 }
3597 break;
3598
3599 case kCondLE:
3600 case kCondGT:
3601 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3602 // Simulate lhs <= rhs via lhs < rhs + 1.
3603 __ Slti(dst, lhs, rhs_imm_plus_one);
3604 if (cond == kCondGT) {
3605 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3606 // only the slti instruction but no sgti.
3607 __ Xori(dst, dst, 1);
3608 }
3609 } else {
3610 if (use_imm) {
3611 rhs_reg = TMP;
3612 __ LoadConst64(rhs_reg, rhs_imm);
3613 }
3614 __ Slt(dst, rhs_reg, lhs);
3615 if (cond == kCondLE) {
3616 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3617 // only the slt instruction but no sle.
3618 __ Xori(dst, dst, 1);
3619 }
3620 }
3621 break;
3622
3623 case kCondB:
3624 case kCondAE:
3625 if (use_imm && IsInt<16>(rhs_imm)) {
3626 // Sltiu sign-extends its 16-bit immediate operand before
3627 // the comparison and thus lets us compare directly with
3628 // unsigned values in the ranges [0, 0x7fff] and
3629 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3630 __ Sltiu(dst, lhs, rhs_imm);
3631 } else {
3632 if (use_imm) {
3633 rhs_reg = TMP;
3634 __ LoadConst64(rhs_reg, rhs_imm);
3635 }
3636 __ Sltu(dst, lhs, rhs_reg);
3637 }
3638 if (cond == kCondAE) {
3639 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3640 // only the sltu instruction but no sgeu.
3641 __ Xori(dst, dst, 1);
3642 }
3643 break;
3644
3645 case kCondBE:
3646 case kCondA:
3647 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3648 // Simulate lhs <= rhs via lhs < rhs + 1.
3649 // Note that this only works if rhs + 1 does not overflow
3650 // to 0, hence the check above.
3651 // Sltiu sign-extends its 16-bit immediate operand before
3652 // the comparison and thus lets us compare directly with
3653 // unsigned values in the ranges [0, 0x7fff] and
3654 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3655 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3656 if (cond == kCondA) {
3657 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3658 // only the sltiu instruction but no sgtiu.
3659 __ Xori(dst, dst, 1);
3660 }
3661 } else {
3662 if (use_imm) {
3663 rhs_reg = TMP;
3664 __ LoadConst64(rhs_reg, rhs_imm);
3665 }
3666 __ Sltu(dst, rhs_reg, lhs);
3667 if (cond == kCondBE) {
3668 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3669 // only the sltu instruction but no sleu.
3670 __ Xori(dst, dst, 1);
3671 }
3672 }
3673 break;
3674 }
3675}
3676
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003677bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3678 bool is64bit,
3679 LocationSummary* input_locations,
3680 GpuRegister dst) {
3681 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3682 Location rhs_location = input_locations->InAt(1);
3683 GpuRegister rhs_reg = ZERO;
3684 int64_t rhs_imm = 0;
3685 bool use_imm = rhs_location.IsConstant();
3686 if (use_imm) {
3687 if (is64bit) {
3688 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3689 } else {
3690 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3691 }
3692 } else {
3693 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3694 }
3695 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3696
3697 switch (cond) {
3698 case kCondEQ:
3699 case kCondNE:
3700 if (use_imm && IsInt<16>(-rhs_imm)) {
3701 if (is64bit) {
3702 __ Daddiu(dst, lhs, -rhs_imm);
3703 } else {
3704 __ Addiu(dst, lhs, -rhs_imm);
3705 }
3706 } else if (use_imm && IsUint<16>(rhs_imm)) {
3707 __ Xori(dst, lhs, rhs_imm);
3708 } else {
3709 if (use_imm) {
3710 rhs_reg = TMP;
3711 __ LoadConst64(rhs_reg, rhs_imm);
3712 }
3713 __ Xor(dst, lhs, rhs_reg);
3714 }
3715 return (cond == kCondEQ);
3716
3717 case kCondLT:
3718 case kCondGE:
3719 if (use_imm && IsInt<16>(rhs_imm)) {
3720 __ Slti(dst, lhs, rhs_imm);
3721 } else {
3722 if (use_imm) {
3723 rhs_reg = TMP;
3724 __ LoadConst64(rhs_reg, rhs_imm);
3725 }
3726 __ Slt(dst, lhs, rhs_reg);
3727 }
3728 return (cond == kCondGE);
3729
3730 case kCondLE:
3731 case kCondGT:
3732 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3733 // Simulate lhs <= rhs via lhs < rhs + 1.
3734 __ Slti(dst, lhs, rhs_imm_plus_one);
3735 return (cond == kCondGT);
3736 } else {
3737 if (use_imm) {
3738 rhs_reg = TMP;
3739 __ LoadConst64(rhs_reg, rhs_imm);
3740 }
3741 __ Slt(dst, rhs_reg, lhs);
3742 return (cond == kCondLE);
3743 }
3744
3745 case kCondB:
3746 case kCondAE:
3747 if (use_imm && IsInt<16>(rhs_imm)) {
3748 // Sltiu sign-extends its 16-bit immediate operand before
3749 // the comparison and thus lets us compare directly with
3750 // unsigned values in the ranges [0, 0x7fff] and
3751 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3752 __ Sltiu(dst, lhs, rhs_imm);
3753 } else {
3754 if (use_imm) {
3755 rhs_reg = TMP;
3756 __ LoadConst64(rhs_reg, rhs_imm);
3757 }
3758 __ Sltu(dst, lhs, rhs_reg);
3759 }
3760 return (cond == kCondAE);
3761
3762 case kCondBE:
3763 case kCondA:
3764 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3765 // Simulate lhs <= rhs via lhs < rhs + 1.
3766 // Note that this only works if rhs + 1 does not overflow
3767 // to 0, hence the check above.
3768 // Sltiu sign-extends its 16-bit immediate operand before
3769 // the comparison and thus lets us compare directly with
3770 // unsigned values in the ranges [0, 0x7fff] and
3771 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3772 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3773 return (cond == kCondA);
3774 } else {
3775 if (use_imm) {
3776 rhs_reg = TMP;
3777 __ LoadConst64(rhs_reg, rhs_imm);
3778 }
3779 __ Sltu(dst, rhs_reg, lhs);
3780 return (cond == kCondBE);
3781 }
3782 }
3783}
3784
Alexey Frunze299a9392015-12-08 16:08:02 -08003785void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3786 bool is64bit,
3787 LocationSummary* locations,
3788 Mips64Label* label) {
3789 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3790 Location rhs_location = locations->InAt(1);
3791 GpuRegister rhs_reg = ZERO;
3792 int64_t rhs_imm = 0;
3793 bool use_imm = rhs_location.IsConstant();
3794 if (use_imm) {
3795 if (is64bit) {
3796 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3797 } else {
3798 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3799 }
3800 } else {
3801 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3802 }
3803
3804 if (use_imm && rhs_imm == 0) {
3805 switch (cond) {
3806 case kCondEQ:
3807 case kCondBE: // <= 0 if zero
3808 __ Beqzc(lhs, label);
3809 break;
3810 case kCondNE:
3811 case kCondA: // > 0 if non-zero
3812 __ Bnezc(lhs, label);
3813 break;
3814 case kCondLT:
3815 __ Bltzc(lhs, label);
3816 break;
3817 case kCondGE:
3818 __ Bgezc(lhs, label);
3819 break;
3820 case kCondLE:
3821 __ Blezc(lhs, label);
3822 break;
3823 case kCondGT:
3824 __ Bgtzc(lhs, label);
3825 break;
3826 case kCondB: // always false
3827 break;
3828 case kCondAE: // always true
3829 __ Bc(label);
3830 break;
3831 }
3832 } else {
3833 if (use_imm) {
3834 rhs_reg = TMP;
3835 __ LoadConst64(rhs_reg, rhs_imm);
3836 }
3837 switch (cond) {
3838 case kCondEQ:
3839 __ Beqc(lhs, rhs_reg, label);
3840 break;
3841 case kCondNE:
3842 __ Bnec(lhs, rhs_reg, label);
3843 break;
3844 case kCondLT:
3845 __ Bltc(lhs, rhs_reg, label);
3846 break;
3847 case kCondGE:
3848 __ Bgec(lhs, rhs_reg, label);
3849 break;
3850 case kCondLE:
3851 __ Bgec(rhs_reg, lhs, label);
3852 break;
3853 case kCondGT:
3854 __ Bltc(rhs_reg, lhs, label);
3855 break;
3856 case kCondB:
3857 __ Bltuc(lhs, rhs_reg, label);
3858 break;
3859 case kCondAE:
3860 __ Bgeuc(lhs, rhs_reg, label);
3861 break;
3862 case kCondBE:
3863 __ Bgeuc(rhs_reg, lhs, label);
3864 break;
3865 case kCondA:
3866 __ Bltuc(rhs_reg, lhs, label);
3867 break;
3868 }
3869 }
3870}
3871
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003872void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3873 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003875 LocationSummary* locations) {
3876 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3877 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3878 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003879 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003880 switch (cond) {
3881 case kCondEQ:
3882 __ CmpEqS(FTMP, lhs, rhs);
3883 __ Mfc1(dst, FTMP);
3884 __ Andi(dst, dst, 1);
3885 break;
3886 case kCondNE:
3887 __ CmpEqS(FTMP, lhs, rhs);
3888 __ Mfc1(dst, FTMP);
3889 __ Addiu(dst, dst, 1);
3890 break;
3891 case kCondLT:
3892 if (gt_bias) {
3893 __ CmpLtS(FTMP, lhs, rhs);
3894 } else {
3895 __ CmpUltS(FTMP, lhs, rhs);
3896 }
3897 __ Mfc1(dst, FTMP);
3898 __ Andi(dst, dst, 1);
3899 break;
3900 case kCondLE:
3901 if (gt_bias) {
3902 __ CmpLeS(FTMP, lhs, rhs);
3903 } else {
3904 __ CmpUleS(FTMP, lhs, rhs);
3905 }
3906 __ Mfc1(dst, FTMP);
3907 __ Andi(dst, dst, 1);
3908 break;
3909 case kCondGT:
3910 if (gt_bias) {
3911 __ CmpUltS(FTMP, rhs, lhs);
3912 } else {
3913 __ CmpLtS(FTMP, rhs, lhs);
3914 }
3915 __ Mfc1(dst, FTMP);
3916 __ Andi(dst, dst, 1);
3917 break;
3918 case kCondGE:
3919 if (gt_bias) {
3920 __ CmpUleS(FTMP, rhs, lhs);
3921 } else {
3922 __ CmpLeS(FTMP, rhs, lhs);
3923 }
3924 __ Mfc1(dst, FTMP);
3925 __ Andi(dst, dst, 1);
3926 break;
3927 default:
3928 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3929 UNREACHABLE();
3930 }
3931 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003932 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003933 switch (cond) {
3934 case kCondEQ:
3935 __ CmpEqD(FTMP, lhs, rhs);
3936 __ Mfc1(dst, FTMP);
3937 __ Andi(dst, dst, 1);
3938 break;
3939 case kCondNE:
3940 __ CmpEqD(FTMP, lhs, rhs);
3941 __ Mfc1(dst, FTMP);
3942 __ Addiu(dst, dst, 1);
3943 break;
3944 case kCondLT:
3945 if (gt_bias) {
3946 __ CmpLtD(FTMP, lhs, rhs);
3947 } else {
3948 __ CmpUltD(FTMP, lhs, rhs);
3949 }
3950 __ Mfc1(dst, FTMP);
3951 __ Andi(dst, dst, 1);
3952 break;
3953 case kCondLE:
3954 if (gt_bias) {
3955 __ CmpLeD(FTMP, lhs, rhs);
3956 } else {
3957 __ CmpUleD(FTMP, lhs, rhs);
3958 }
3959 __ Mfc1(dst, FTMP);
3960 __ Andi(dst, dst, 1);
3961 break;
3962 case kCondGT:
3963 if (gt_bias) {
3964 __ CmpUltD(FTMP, rhs, lhs);
3965 } else {
3966 __ CmpLtD(FTMP, rhs, lhs);
3967 }
3968 __ Mfc1(dst, FTMP);
3969 __ Andi(dst, dst, 1);
3970 break;
3971 case kCondGE:
3972 if (gt_bias) {
3973 __ CmpUleD(FTMP, rhs, lhs);
3974 } else {
3975 __ CmpLeD(FTMP, rhs, lhs);
3976 }
3977 __ Mfc1(dst, FTMP);
3978 __ Andi(dst, dst, 1);
3979 break;
3980 default:
3981 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3982 UNREACHABLE();
3983 }
3984 }
3985}
3986
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003987bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
3988 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003989 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003990 LocationSummary* input_locations,
3991 FpuRegister dst) {
3992 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
3993 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003994 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003995 switch (cond) {
3996 case kCondEQ:
3997 __ CmpEqS(dst, lhs, rhs);
3998 return false;
3999 case kCondNE:
4000 __ CmpEqS(dst, lhs, rhs);
4001 return true;
4002 case kCondLT:
4003 if (gt_bias) {
4004 __ CmpLtS(dst, lhs, rhs);
4005 } else {
4006 __ CmpUltS(dst, lhs, rhs);
4007 }
4008 return false;
4009 case kCondLE:
4010 if (gt_bias) {
4011 __ CmpLeS(dst, lhs, rhs);
4012 } else {
4013 __ CmpUleS(dst, lhs, rhs);
4014 }
4015 return false;
4016 case kCondGT:
4017 if (gt_bias) {
4018 __ CmpUltS(dst, rhs, lhs);
4019 } else {
4020 __ CmpLtS(dst, rhs, lhs);
4021 }
4022 return false;
4023 case kCondGE:
4024 if (gt_bias) {
4025 __ CmpUleS(dst, rhs, lhs);
4026 } else {
4027 __ CmpLeS(dst, rhs, lhs);
4028 }
4029 return false;
4030 default:
4031 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4032 UNREACHABLE();
4033 }
4034 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004035 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004036 switch (cond) {
4037 case kCondEQ:
4038 __ CmpEqD(dst, lhs, rhs);
4039 return false;
4040 case kCondNE:
4041 __ CmpEqD(dst, lhs, rhs);
4042 return true;
4043 case kCondLT:
4044 if (gt_bias) {
4045 __ CmpLtD(dst, lhs, rhs);
4046 } else {
4047 __ CmpUltD(dst, lhs, rhs);
4048 }
4049 return false;
4050 case kCondLE:
4051 if (gt_bias) {
4052 __ CmpLeD(dst, lhs, rhs);
4053 } else {
4054 __ CmpUleD(dst, lhs, rhs);
4055 }
4056 return false;
4057 case kCondGT:
4058 if (gt_bias) {
4059 __ CmpUltD(dst, rhs, lhs);
4060 } else {
4061 __ CmpLtD(dst, rhs, lhs);
4062 }
4063 return false;
4064 case kCondGE:
4065 if (gt_bias) {
4066 __ CmpUleD(dst, rhs, lhs);
4067 } else {
4068 __ CmpLeD(dst, rhs, lhs);
4069 }
4070 return false;
4071 default:
4072 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4073 UNREACHABLE();
4074 }
4075 }
4076}
4077
Alexey Frunze299a9392015-12-08 16:08:02 -08004078void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4079 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004080 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004081 LocationSummary* locations,
4082 Mips64Label* label) {
4083 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4084 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004086 switch (cond) {
4087 case kCondEQ:
4088 __ CmpEqS(FTMP, lhs, rhs);
4089 __ Bc1nez(FTMP, label);
4090 break;
4091 case kCondNE:
4092 __ CmpEqS(FTMP, lhs, rhs);
4093 __ Bc1eqz(FTMP, label);
4094 break;
4095 case kCondLT:
4096 if (gt_bias) {
4097 __ CmpLtS(FTMP, lhs, rhs);
4098 } else {
4099 __ CmpUltS(FTMP, lhs, rhs);
4100 }
4101 __ Bc1nez(FTMP, label);
4102 break;
4103 case kCondLE:
4104 if (gt_bias) {
4105 __ CmpLeS(FTMP, lhs, rhs);
4106 } else {
4107 __ CmpUleS(FTMP, lhs, rhs);
4108 }
4109 __ Bc1nez(FTMP, label);
4110 break;
4111 case kCondGT:
4112 if (gt_bias) {
4113 __ CmpUltS(FTMP, rhs, lhs);
4114 } else {
4115 __ CmpLtS(FTMP, rhs, lhs);
4116 }
4117 __ Bc1nez(FTMP, label);
4118 break;
4119 case kCondGE:
4120 if (gt_bias) {
4121 __ CmpUleS(FTMP, rhs, lhs);
4122 } else {
4123 __ CmpLeS(FTMP, rhs, lhs);
4124 }
4125 __ Bc1nez(FTMP, label);
4126 break;
4127 default:
4128 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004129 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004130 }
4131 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004132 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004133 switch (cond) {
4134 case kCondEQ:
4135 __ CmpEqD(FTMP, lhs, rhs);
4136 __ Bc1nez(FTMP, label);
4137 break;
4138 case kCondNE:
4139 __ CmpEqD(FTMP, lhs, rhs);
4140 __ Bc1eqz(FTMP, label);
4141 break;
4142 case kCondLT:
4143 if (gt_bias) {
4144 __ CmpLtD(FTMP, lhs, rhs);
4145 } else {
4146 __ CmpUltD(FTMP, lhs, rhs);
4147 }
4148 __ Bc1nez(FTMP, label);
4149 break;
4150 case kCondLE:
4151 if (gt_bias) {
4152 __ CmpLeD(FTMP, lhs, rhs);
4153 } else {
4154 __ CmpUleD(FTMP, lhs, rhs);
4155 }
4156 __ Bc1nez(FTMP, label);
4157 break;
4158 case kCondGT:
4159 if (gt_bias) {
4160 __ CmpUltD(FTMP, rhs, lhs);
4161 } else {
4162 __ CmpLtD(FTMP, rhs, lhs);
4163 }
4164 __ Bc1nez(FTMP, label);
4165 break;
4166 case kCondGE:
4167 if (gt_bias) {
4168 __ CmpUleD(FTMP, rhs, lhs);
4169 } else {
4170 __ CmpLeD(FTMP, rhs, lhs);
4171 }
4172 __ Bc1nez(FTMP, label);
4173 break;
4174 default:
4175 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004176 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004177 }
4178 }
4179}
4180
Alexey Frunze4dda3372015-06-01 18:31:49 -07004181void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004182 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004183 Mips64Label* true_target,
4184 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004185 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186
David Brazdil0debae72015-11-12 18:37:00 +00004187 if (true_target == nullptr && false_target == nullptr) {
4188 // Nothing to do. The code always falls through.
4189 return;
4190 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004191 // Constant condition, statically compared against "true" (integer value 1).
4192 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004193 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004194 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004195 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004196 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004197 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004198 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004199 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004200 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004201 }
David Brazdil0debae72015-11-12 18:37:00 +00004202 return;
4203 }
4204
4205 // The following code generates these patterns:
4206 // (1) true_target == nullptr && false_target != nullptr
4207 // - opposite condition true => branch to false_target
4208 // (2) true_target != nullptr && false_target == nullptr
4209 // - condition true => branch to true_target
4210 // (3) true_target != nullptr && false_target != nullptr
4211 // - condition true => branch to true_target
4212 // - branch to false_target
4213 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004214 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004215 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004216 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004217 if (true_target == nullptr) {
4218 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4219 } else {
4220 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4221 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004222 } else {
4223 // The condition instruction has not been materialized, use its inputs as
4224 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004225 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004226 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004227 LocationSummary* locations = cond->GetLocations();
4228 IfCondition if_cond = condition->GetCondition();
4229 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004230
David Brazdil0debae72015-11-12 18:37:00 +00004231 if (true_target == nullptr) {
4232 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004233 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004234 }
4235
Alexey Frunze299a9392015-12-08 16:08:02 -08004236 switch (type) {
4237 default:
4238 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4239 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004241 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4242 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004243 case DataType::Type::kFloat32:
4244 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004245 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4246 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004247 }
4248 }
David Brazdil0debae72015-11-12 18:37:00 +00004249
4250 // If neither branch falls through (case 3), the conditional branch to `true_target`
4251 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4252 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004253 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004254 }
4255}
4256
4257void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004258 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004259 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004260 locations->SetInAt(0, Location::RequiresRegister());
4261 }
4262}
4263
4264void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004265 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4266 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004267 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004268 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004269 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004270 nullptr : codegen_->GetLabelOf(false_successor);
4271 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004272}
4273
4274void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004275 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004276 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004277 InvokeRuntimeCallingConvention calling_convention;
4278 RegisterSet caller_saves = RegisterSet::Empty();
4279 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4280 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004281 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004282 locations->SetInAt(0, Location::RequiresRegister());
4283 }
4284}
4285
4286void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004287 SlowPathCodeMIPS64* slow_path =
4288 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004289 GenerateTestAndBranch(deoptimize,
4290 /* condition_input_index */ 0,
4291 slow_path->GetEntryLabel(),
4292 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004293}
4294
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004295// This function returns true if a conditional move can be generated for HSelect.
4296// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4297// branches and regular moves.
4298//
4299// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4300//
4301// While determining feasibility of a conditional move and setting inputs/outputs
4302// are two distinct tasks, this function does both because they share quite a bit
4303// of common logic.
4304static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4305 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4306 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4307 HCondition* condition = cond->AsCondition();
4308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004309 DataType::Type cond_type =
4310 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4311 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004312
4313 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4314 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4315 bool is_true_value_zero_constant =
4316 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4317 bool is_false_value_zero_constant =
4318 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4319
4320 bool can_move_conditionally = false;
4321 bool use_const_for_false_in = false;
4322 bool use_const_for_true_in = false;
4323
4324 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004325 if (!DataType::IsFloatingPointType(cond_type)) {
4326 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004327 // Moving int/long on int/long condition.
4328 if (is_true_value_zero_constant) {
4329 // seleqz out_reg, false_reg, cond_reg
4330 can_move_conditionally = true;
4331 use_const_for_true_in = true;
4332 } else if (is_false_value_zero_constant) {
4333 // selnez out_reg, true_reg, cond_reg
4334 can_move_conditionally = true;
4335 use_const_for_false_in = true;
4336 } else if (materialized) {
4337 // Not materializing unmaterialized int conditions
4338 // to keep the instruction count low.
4339 // selnez AT, true_reg, cond_reg
4340 // seleqz TMP, false_reg, cond_reg
4341 // or out_reg, AT, TMP
4342 can_move_conditionally = true;
4343 }
4344 } else {
4345 // Moving float/double on int/long condition.
4346 if (materialized) {
4347 // Not materializing unmaterialized int conditions
4348 // to keep the instruction count low.
4349 can_move_conditionally = true;
4350 if (is_true_value_zero_constant) {
4351 // sltu TMP, ZERO, cond_reg
4352 // mtc1 TMP, temp_cond_reg
4353 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4354 use_const_for_true_in = true;
4355 } else if (is_false_value_zero_constant) {
4356 // sltu TMP, ZERO, cond_reg
4357 // mtc1 TMP, temp_cond_reg
4358 // selnez.fmt out_reg, true_reg, temp_cond_reg
4359 use_const_for_false_in = true;
4360 } else {
4361 // sltu TMP, ZERO, cond_reg
4362 // mtc1 TMP, temp_cond_reg
4363 // sel.fmt temp_cond_reg, false_reg, true_reg
4364 // mov.fmt out_reg, temp_cond_reg
4365 }
4366 }
4367 }
4368 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004369 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004370 // Moving int/long on float/double condition.
4371 can_move_conditionally = true;
4372 if (is_true_value_zero_constant) {
4373 // mfc1 TMP, temp_cond_reg
4374 // seleqz out_reg, false_reg, TMP
4375 use_const_for_true_in = true;
4376 } else if (is_false_value_zero_constant) {
4377 // mfc1 TMP, temp_cond_reg
4378 // selnez out_reg, true_reg, TMP
4379 use_const_for_false_in = true;
4380 } else {
4381 // mfc1 TMP, temp_cond_reg
4382 // selnez AT, true_reg, TMP
4383 // seleqz TMP, false_reg, TMP
4384 // or out_reg, AT, TMP
4385 }
4386 } else {
4387 // Moving float/double on float/double condition.
4388 can_move_conditionally = true;
4389 if (is_true_value_zero_constant) {
4390 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4391 use_const_for_true_in = true;
4392 } else if (is_false_value_zero_constant) {
4393 // selnez.fmt out_reg, true_reg, temp_cond_reg
4394 use_const_for_false_in = true;
4395 } else {
4396 // sel.fmt temp_cond_reg, false_reg, true_reg
4397 // mov.fmt out_reg, temp_cond_reg
4398 }
4399 }
4400 }
4401 }
4402
4403 if (can_move_conditionally) {
4404 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4405 } else {
4406 DCHECK(!use_const_for_false_in);
4407 DCHECK(!use_const_for_true_in);
4408 }
4409
4410 if (locations_to_set != nullptr) {
4411 if (use_const_for_false_in) {
4412 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4413 } else {
4414 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004415 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004416 ? Location::RequiresFpuRegister()
4417 : Location::RequiresRegister());
4418 }
4419 if (use_const_for_true_in) {
4420 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4421 } else {
4422 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004423 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004424 ? Location::RequiresFpuRegister()
4425 : Location::RequiresRegister());
4426 }
4427 if (materialized) {
4428 locations_to_set->SetInAt(2, Location::RequiresRegister());
4429 }
4430
4431 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004432 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004433 ? Location::RequiresFpuRegister()
4434 : Location::RequiresRegister());
4435 } else {
4436 locations_to_set->SetOut(Location::SameAsFirstInput());
4437 }
4438 }
4439
4440 return can_move_conditionally;
4441}
4442
4443
4444void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4445 LocationSummary* locations = select->GetLocations();
4446 Location dst = locations->Out();
4447 Location false_src = locations->InAt(0);
4448 Location true_src = locations->InAt(1);
4449 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4450 GpuRegister cond_reg = TMP;
4451 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004452 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004453 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004454 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004455
4456 if (IsBooleanValueOrMaterializedCondition(cond)) {
4457 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4458 } else {
4459 HCondition* condition = cond->AsCondition();
4460 LocationSummary* cond_locations = cond->GetLocations();
4461 IfCondition if_cond = condition->GetCondition();
4462 cond_type = condition->InputAt(0)->GetType();
4463 switch (cond_type) {
4464 default:
4465 cond_inverted = MaterializeIntLongCompare(if_cond,
4466 /* is64bit */ false,
4467 cond_locations,
4468 cond_reg);
4469 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004470 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004471 cond_inverted = MaterializeIntLongCompare(if_cond,
4472 /* is64bit */ true,
4473 cond_locations,
4474 cond_reg);
4475 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004476 case DataType::Type::kFloat32:
4477 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004478 cond_inverted = MaterializeFpCompare(if_cond,
4479 condition->IsGtBias(),
4480 cond_type,
4481 cond_locations,
4482 fcond_reg);
4483 break;
4484 }
4485 }
4486
4487 if (true_src.IsConstant()) {
4488 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4489 }
4490 if (false_src.IsConstant()) {
4491 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4492 }
4493
4494 switch (dst_type) {
4495 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004496 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004497 __ Mfc1(cond_reg, fcond_reg);
4498 }
4499 if (true_src.IsConstant()) {
4500 if (cond_inverted) {
4501 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4502 } else {
4503 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4504 }
4505 } else if (false_src.IsConstant()) {
4506 if (cond_inverted) {
4507 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4508 } else {
4509 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4510 }
4511 } else {
4512 DCHECK_NE(cond_reg, AT);
4513 if (cond_inverted) {
4514 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4515 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4516 } else {
4517 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4518 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4519 }
4520 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4521 }
4522 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004523 case DataType::Type::kFloat32: {
4524 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004525 // sel*.fmt tests bit 0 of the condition register, account for that.
4526 __ Sltu(TMP, ZERO, cond_reg);
4527 __ Mtc1(TMP, fcond_reg);
4528 }
4529 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4530 if (true_src.IsConstant()) {
4531 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4532 if (cond_inverted) {
4533 __ SelnezS(dst_reg, src_reg, fcond_reg);
4534 } else {
4535 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4536 }
4537 } else if (false_src.IsConstant()) {
4538 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4539 if (cond_inverted) {
4540 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4541 } else {
4542 __ SelnezS(dst_reg, src_reg, fcond_reg);
4543 }
4544 } else {
4545 if (cond_inverted) {
4546 __ SelS(fcond_reg,
4547 true_src.AsFpuRegister<FpuRegister>(),
4548 false_src.AsFpuRegister<FpuRegister>());
4549 } else {
4550 __ SelS(fcond_reg,
4551 false_src.AsFpuRegister<FpuRegister>(),
4552 true_src.AsFpuRegister<FpuRegister>());
4553 }
4554 __ MovS(dst_reg, fcond_reg);
4555 }
4556 break;
4557 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004558 case DataType::Type::kFloat64: {
4559 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004560 // sel*.fmt tests bit 0 of the condition register, account for that.
4561 __ Sltu(TMP, ZERO, cond_reg);
4562 __ Mtc1(TMP, fcond_reg);
4563 }
4564 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4565 if (true_src.IsConstant()) {
4566 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4567 if (cond_inverted) {
4568 __ SelnezD(dst_reg, src_reg, fcond_reg);
4569 } else {
4570 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4571 }
4572 } else if (false_src.IsConstant()) {
4573 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4574 if (cond_inverted) {
4575 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4576 } else {
4577 __ SelnezD(dst_reg, src_reg, fcond_reg);
4578 }
4579 } else {
4580 if (cond_inverted) {
4581 __ SelD(fcond_reg,
4582 true_src.AsFpuRegister<FpuRegister>(),
4583 false_src.AsFpuRegister<FpuRegister>());
4584 } else {
4585 __ SelD(fcond_reg,
4586 false_src.AsFpuRegister<FpuRegister>(),
4587 true_src.AsFpuRegister<FpuRegister>());
4588 }
4589 __ MovD(dst_reg, fcond_reg);
4590 }
4591 break;
4592 }
4593 }
4594}
4595
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004596void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004597 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004598 LocationSummary(flag, LocationSummary::kNoCall);
4599 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004600}
4601
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004602void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4603 __ LoadFromOffset(kLoadWord,
4604 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4605 SP,
4606 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004607}
4608
David Brazdil74eb1b22015-12-14 11:44:01 +00004609void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004610 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004611 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004612}
4613
4614void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004615 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4616 GenConditionalMove(select);
4617 } else {
4618 LocationSummary* locations = select->GetLocations();
4619 Mips64Label false_target;
4620 GenerateTestAndBranch(select,
4621 /* condition_input_index */ 2,
4622 /* true_target */ nullptr,
4623 &false_target);
4624 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4625 __ Bind(&false_target);
4626 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004627}
4628
David Srbecky0cf44932015-12-09 14:09:59 +00004629void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004630 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004631}
4632
David Srbeckyd28f4a02016-03-14 17:14:24 +00004633void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4634 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004635}
4636
4637void CodeGeneratorMIPS64::GenerateNop() {
4638 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004639}
4640
Alexey Frunze4dda3372015-06-01 18:31:49 -07004641void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004642 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004644 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004646 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004647 instruction,
4648 object_field_get_with_read_barrier
4649 ? LocationSummary::kCallOnSlowPath
4650 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004651 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4652 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4653 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004654 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004655 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004656 locations->SetOut(Location::RequiresFpuRegister());
4657 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004658 // The output overlaps in the case of an object field get with
4659 // read barriers enabled: we do not want the move to overwrite the
4660 // object's location, as we need it to emit the read barrier.
4661 locations->SetOut(Location::RequiresRegister(),
4662 object_field_get_with_read_barrier
4663 ? Location::kOutputOverlap
4664 : Location::kNoOutputOverlap);
4665 }
4666 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4667 // We need a temporary register for the read barrier marking slow
4668 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004669 if (!kBakerReadBarrierThunksEnableForFields) {
4670 locations->AddTemp(Location::RequiresRegister());
4671 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004672 }
4673}
4674
4675void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4676 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004678 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004679 Location obj_loc = locations->InAt(0);
4680 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4681 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004682 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004683 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004684 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004685 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4686
Alexey Frunze4dda3372015-06-01 18:31:49 -07004687 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004688 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004689 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004690 load_type = kLoadUnsignedByte;
4691 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004693 load_type = kLoadSignedByte;
4694 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004695 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004696 load_type = kLoadUnsignedHalfword;
4697 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004698 case DataType::Type::kInt16:
4699 load_type = kLoadSignedHalfword;
4700 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004701 case DataType::Type::kInt32:
4702 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004703 load_type = kLoadWord;
4704 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004705 case DataType::Type::kInt64:
4706 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004707 load_type = kLoadDoubleword;
4708 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004709 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004710 load_type = kLoadUnsignedWord;
4711 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004712 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004713 LOG(FATAL) << "Unreachable type " << type;
4714 UNREACHABLE();
4715 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004716 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004717 DCHECK(dst_loc.IsRegister());
4718 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004720 // /* HeapReference<Object> */ dst = *(obj + offset)
4721 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004722 Location temp_loc =
4723 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004724 // Note that a potential implicit null check is handled in this
4725 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4726 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4727 dst_loc,
4728 obj,
4729 offset,
4730 temp_loc,
4731 /* needs_null_check */ true);
4732 if (is_volatile) {
4733 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4734 }
4735 } else {
4736 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4737 if (is_volatile) {
4738 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4739 }
4740 // If read barriers are enabled, emit read barriers other than
4741 // Baker's using a slow path (and also unpoison the loaded
4742 // reference, if heap poisoning is enabled).
4743 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4744 }
4745 } else {
4746 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4747 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004748 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004749 DCHECK(dst_loc.IsFpuRegister());
4750 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004751 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004752 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004753
Alexey Frunze15958152017-02-09 19:08:30 -08004754 // Memory barriers, in the case of references, are handled in the
4755 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004756 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004757 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004758 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004759}
4760
4761void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4762 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4763 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004764 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004765 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004766 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004767 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004768 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004769 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004770 }
4771}
4772
4773void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004774 const FieldInfo& field_info,
4775 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004776 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004777 LocationSummary* locations = instruction->GetLocations();
4778 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004779 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004780 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004781 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004782 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4783 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004784 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4785
Alexey Frunze4dda3372015-06-01 18:31:49 -07004786 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004787 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004788 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004790 store_type = kStoreByte;
4791 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004793 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004794 store_type = kStoreHalfword;
4795 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 case DataType::Type::kInt32:
4797 case DataType::Type::kFloat32:
4798 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004799 store_type = kStoreWord;
4800 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004801 case DataType::Type::kInt64:
4802 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004803 store_type = kStoreDoubleword;
4804 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004806 LOG(FATAL) << "Unreachable type " << type;
4807 UNREACHABLE();
4808 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004809
Alexey Frunze15958152017-02-09 19:08:30 -08004810 if (is_volatile) {
4811 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4812 }
4813
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004814 if (value_location.IsConstant()) {
4815 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4816 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4817 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004818 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004819 DCHECK(value_location.IsRegister());
4820 GpuRegister src = value_location.AsRegister<GpuRegister>();
4821 if (kPoisonHeapReferences && needs_write_barrier) {
4822 // Note that in the case where `value` is a null reference,
4823 // we do not enter this block, as a null reference does not
4824 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004825 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004826 __ PoisonHeapReference(TMP, src);
4827 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4828 } else {
4829 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4830 }
4831 } else {
4832 DCHECK(value_location.IsFpuRegister());
4833 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4834 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4835 }
4836 }
Alexey Frunze15958152017-02-09 19:08:30 -08004837
Alexey Frunzec061de12017-02-14 13:27:23 -08004838 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004839 DCHECK(value_location.IsRegister());
4840 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004841 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004842 }
Alexey Frunze15958152017-02-09 19:08:30 -08004843
4844 if (is_volatile) {
4845 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4846 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004847}
4848
4849void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4850 HandleFieldGet(instruction, instruction->GetFieldInfo());
4851}
4852
4853void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4854 HandleFieldGet(instruction, instruction->GetFieldInfo());
4855}
4856
4857void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4858 HandleFieldSet(instruction, instruction->GetFieldInfo());
4859}
4860
4861void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004862 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004863}
4864
Alexey Frunze15958152017-02-09 19:08:30 -08004865void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4866 HInstruction* instruction,
4867 Location out,
4868 uint32_t offset,
4869 Location maybe_temp,
4870 ReadBarrierOption read_barrier_option) {
4871 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4872 if (read_barrier_option == kWithReadBarrier) {
4873 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004874 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4875 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4876 }
Alexey Frunze15958152017-02-09 19:08:30 -08004877 if (kUseBakerReadBarrier) {
4878 // Load with fast path based Baker's read barrier.
4879 // /* HeapReference<Object> */ out = *(out + offset)
4880 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4881 out,
4882 out_reg,
4883 offset,
4884 maybe_temp,
4885 /* needs_null_check */ false);
4886 } else {
4887 // Load with slow path based read barrier.
4888 // Save the value of `out` into `maybe_temp` before overwriting it
4889 // in the following move operation, as we will need it for the
4890 // read barrier below.
4891 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4892 // /* HeapReference<Object> */ out = *(out + offset)
4893 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4894 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4895 }
4896 } else {
4897 // Plain load with no read barrier.
4898 // /* HeapReference<Object> */ out = *(out + offset)
4899 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4900 __ MaybeUnpoisonHeapReference(out_reg);
4901 }
4902}
4903
4904void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4905 HInstruction* instruction,
4906 Location out,
4907 Location obj,
4908 uint32_t offset,
4909 Location maybe_temp,
4910 ReadBarrierOption read_barrier_option) {
4911 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4912 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4913 if (read_barrier_option == kWithReadBarrier) {
4914 CHECK(kEmitCompilerReadBarrier);
4915 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004916 if (!kBakerReadBarrierThunksEnableForFields) {
4917 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4918 }
Alexey Frunze15958152017-02-09 19:08:30 -08004919 // Load with fast path based Baker's read barrier.
4920 // /* HeapReference<Object> */ out = *(obj + offset)
4921 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4922 out,
4923 obj_reg,
4924 offset,
4925 maybe_temp,
4926 /* needs_null_check */ false);
4927 } else {
4928 // Load with slow path based read barrier.
4929 // /* HeapReference<Object> */ out = *(obj + offset)
4930 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4931 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4932 }
4933 } else {
4934 // Plain load with no read barrier.
4935 // /* HeapReference<Object> */ out = *(obj + offset)
4936 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4937 __ MaybeUnpoisonHeapReference(out_reg);
4938 }
4939}
4940
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004941static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4942 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4943 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4944 return reg - V0;
4945 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4946 return 13 + (reg - S2);
4947 } else if (reg == S8) { // One more.
4948 return 19;
4949 }
4950 LOG(FATAL) << "Unexpected register " << reg;
4951 UNREACHABLE();
4952}
4953
4954static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4955 int num = GetBakerMarkThunkNumber(reg) +
4956 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4957 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4958}
4959
4960static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4961 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4962 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4963}
4964
4965void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4966 Location root,
4967 GpuRegister obj,
4968 uint32_t offset,
4969 ReadBarrierOption read_barrier_option,
4970 Mips64Label* label_low) {
4971 if (label_low != nullptr) {
4972 DCHECK_EQ(offset, 0x5678u);
4973 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004974 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004975 if (read_barrier_option == kWithReadBarrier) {
4976 DCHECK(kEmitCompilerReadBarrier);
4977 if (kUseBakerReadBarrier) {
4978 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4979 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004980 if (kBakerReadBarrierThunksEnableForGcRoots) {
4981 // Note that we do not actually check the value of `GetIsGcMarking()`
4982 // to decide whether to mark the loaded GC root or not. Instead, we
4983 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4984 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4985 // vice versa.
4986 //
4987 // We use thunks for the slow path. That thunk checks the reference
4988 // and jumps to the entrypoint if needed.
4989 //
4990 // temp = Thread::Current()->pReadBarrierMarkReg00
4991 // // AKA &art_quick_read_barrier_mark_introspection.
4992 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4993 // if (temp != nullptr) {
4994 // temp = &gc_root_thunk<root_reg>
4995 // root = temp(root)
4996 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004997
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004998 const int32_t entry_point_offset =
4999 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5000 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5001 int16_t offset_low = Low16Bits(offset);
5002 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5003 // extension in lwu.
5004 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5005 GpuRegister base = short_offset ? obj : TMP;
5006 // Loading the entrypoint does not require a load acquire since it is only changed when
5007 // threads are suspended or running a checkpoint.
5008 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5009 if (!short_offset) {
5010 DCHECK(!label_low);
5011 __ Daui(base, obj, offset_high);
5012 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005013 Mips64Label skip_call;
5014 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005015 if (label_low != nullptr) {
5016 DCHECK(short_offset);
5017 __ Bind(label_low);
5018 }
5019 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5020 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5021 // in delay slot.
5022 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005023 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005024 } else {
5025 // Note that we do not actually check the value of `GetIsGcMarking()`
5026 // to decide whether to mark the loaded GC root or not. Instead, we
5027 // load into `temp` (T9) the read barrier mark entry point corresponding
5028 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5029 // is false, and vice versa.
5030 //
5031 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5032 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5033 // if (temp != null) {
5034 // root = temp(root)
5035 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005036
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005037 if (label_low != nullptr) {
5038 __ Bind(label_low);
5039 }
5040 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5041 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5042 static_assert(
5043 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5044 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5045 "have different sizes.");
5046 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5047 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5048 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005049
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005050 // Slow path marking the GC root `root`.
5051 Location temp = Location::RegisterLocation(T9);
5052 SlowPathCodeMIPS64* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005053 new (GetGraph()->GetAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005054 instruction,
5055 root,
5056 /*entrypoint*/ temp);
5057 codegen_->AddSlowPath(slow_path);
5058
5059 const int32_t entry_point_offset =
5060 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5061 // Loading the entrypoint does not require a load acquire since it is only changed when
5062 // threads are suspended or running a checkpoint.
5063 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5064 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5065 __ Bind(slow_path->GetExitLabel());
5066 }
Alexey Frunze15958152017-02-09 19:08:30 -08005067 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005068 if (label_low != nullptr) {
5069 __ Bind(label_low);
5070 }
Alexey Frunze15958152017-02-09 19:08:30 -08005071 // GC root loaded through a slow path for read barriers other
5072 // than Baker's.
5073 // /* GcRoot<mirror::Object>* */ root = obj + offset
5074 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5075 // /* mirror::Object* */ root = root->Read()
5076 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5077 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005078 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005079 if (label_low != nullptr) {
5080 __ Bind(label_low);
5081 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005082 // Plain GC root load with no read barrier.
5083 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5084 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5085 // Note that GC roots are not affected by heap poisoning, thus we
5086 // do not have to unpoison `root_reg` here.
5087 }
5088}
5089
Alexey Frunze15958152017-02-09 19:08:30 -08005090void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5091 Location ref,
5092 GpuRegister obj,
5093 uint32_t offset,
5094 Location temp,
5095 bool needs_null_check) {
5096 DCHECK(kEmitCompilerReadBarrier);
5097 DCHECK(kUseBakerReadBarrier);
5098
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005099 if (kBakerReadBarrierThunksEnableForFields) {
5100 // Note that we do not actually check the value of `GetIsGcMarking()`
5101 // to decide whether to mark the loaded reference or not. Instead, we
5102 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5103 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5104 // vice versa.
5105 //
5106 // We use thunks for the slow path. That thunk checks the reference
5107 // and jumps to the entrypoint if needed. If the holder is not gray,
5108 // it issues a load-load memory barrier and returns to the original
5109 // reference load.
5110 //
5111 // temp = Thread::Current()->pReadBarrierMarkReg00
5112 // // AKA &art_quick_read_barrier_mark_introspection.
5113 // if (temp != nullptr) {
5114 // temp = &field_array_thunk<holder_reg>
5115 // temp()
5116 // }
5117 // not_gray_return_address:
5118 // // If the offset is too large to fit into the lw instruction, we
5119 // // use an adjusted base register (TMP) here. This register
5120 // // receives bits 16 ... 31 of the offset before the thunk invocation
5121 // // and the thunk benefits from it.
5122 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5123 // gray_return_address:
5124
5125 DCHECK(temp.IsInvalid());
5126 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5127 const int32_t entry_point_offset =
5128 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5129 // There may have or may have not been a null check if the field offset is smaller than
5130 // the page size.
5131 // There must've been a null check in case it's actually a load from an array.
5132 // We will, however, perform an explicit null check in the thunk as it's easier to
5133 // do it than not.
5134 if (instruction->IsArrayGet()) {
5135 DCHECK(!needs_null_check);
5136 }
5137 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5138 // Loading the entrypoint does not require a load acquire since it is only changed when
5139 // threads are suspended or running a checkpoint.
5140 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5141 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005142 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005143 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005144 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005145 __ Nop(); // In forbidden slot.
5146 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005147 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005148 // /* HeapReference<Object> */ ref = *(obj + offset)
5149 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5150 } else {
5151 int16_t offset_low = Low16Bits(offset);
5152 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005153 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005154 __ Daui(TMP, obj, offset_high); // In delay slot.
5155 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005156 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005157 // /* HeapReference<Object> */ ref = *(obj + offset)
5158 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5159 }
5160 if (needs_null_check) {
5161 MaybeRecordImplicitNullCheck(instruction);
5162 }
5163 __ MaybeUnpoisonHeapReference(ref_reg);
5164 return;
5165 }
5166
Alexey Frunze15958152017-02-09 19:08:30 -08005167 // /* HeapReference<Object> */ ref = *(obj + offset)
5168 Location no_index = Location::NoLocation();
5169 ScaleFactor no_scale_factor = TIMES_1;
5170 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5171 ref,
5172 obj,
5173 offset,
5174 no_index,
5175 no_scale_factor,
5176 temp,
5177 needs_null_check);
5178}
5179
5180void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5181 Location ref,
5182 GpuRegister obj,
5183 uint32_t data_offset,
5184 Location index,
5185 Location temp,
5186 bool needs_null_check) {
5187 DCHECK(kEmitCompilerReadBarrier);
5188 DCHECK(kUseBakerReadBarrier);
5189
5190 static_assert(
5191 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5192 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005193 ScaleFactor scale_factor = TIMES_4;
5194
5195 if (kBakerReadBarrierThunksEnableForArrays) {
5196 // Note that we do not actually check the value of `GetIsGcMarking()`
5197 // to decide whether to mark the loaded reference or not. Instead, we
5198 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5199 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5200 // vice versa.
5201 //
5202 // We use thunks for the slow path. That thunk checks the reference
5203 // and jumps to the entrypoint if needed. If the holder is not gray,
5204 // it issues a load-load memory barrier and returns to the original
5205 // reference load.
5206 //
5207 // temp = Thread::Current()->pReadBarrierMarkReg00
5208 // // AKA &art_quick_read_barrier_mark_introspection.
5209 // if (temp != nullptr) {
5210 // temp = &field_array_thunk<holder_reg>
5211 // temp()
5212 // }
5213 // not_gray_return_address:
5214 // // The element address is pre-calculated in the TMP register before the
5215 // // thunk invocation and the thunk benefits from it.
5216 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5217 // gray_return_address:
5218
5219 DCHECK(temp.IsInvalid());
5220 DCHECK(index.IsValid());
5221 const int32_t entry_point_offset =
5222 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5223 // We will not do the explicit null check in the thunk as some form of a null check
5224 // must've been done earlier.
5225 DCHECK(!needs_null_check);
5226 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5227 // Loading the entrypoint does not require a load acquire since it is only changed when
5228 // threads are suspended or running a checkpoint.
5229 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005230 Mips64Label skip_call;
5231 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005232 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5233 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5234 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5235 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005236 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005237 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5238 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5239 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5240 __ MaybeUnpoisonHeapReference(ref_reg);
5241 return;
5242 }
5243
Alexey Frunze15958152017-02-09 19:08:30 -08005244 // /* HeapReference<Object> */ ref =
5245 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005246 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5247 ref,
5248 obj,
5249 data_offset,
5250 index,
5251 scale_factor,
5252 temp,
5253 needs_null_check);
5254}
5255
5256void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5257 Location ref,
5258 GpuRegister obj,
5259 uint32_t offset,
5260 Location index,
5261 ScaleFactor scale_factor,
5262 Location temp,
5263 bool needs_null_check,
5264 bool always_update_field) {
5265 DCHECK(kEmitCompilerReadBarrier);
5266 DCHECK(kUseBakerReadBarrier);
5267
5268 // In slow path based read barriers, the read barrier call is
5269 // inserted after the original load. However, in fast path based
5270 // Baker's read barriers, we need to perform the load of
5271 // mirror::Object::monitor_ *before* the original reference load.
5272 // This load-load ordering is required by the read barrier.
5273 // The fast path/slow path (for Baker's algorithm) should look like:
5274 //
5275 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5276 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5277 // HeapReference<Object> ref = *src; // Original reference load.
5278 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5279 // if (is_gray) {
5280 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5281 // }
5282 //
5283 // Note: the original implementation in ReadBarrier::Barrier is
5284 // slightly more complex as it performs additional checks that we do
5285 // not do here for performance reasons.
5286
5287 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5288 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5289 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5290
5291 // /* int32_t */ monitor = obj->monitor_
5292 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5293 if (needs_null_check) {
5294 MaybeRecordImplicitNullCheck(instruction);
5295 }
5296 // /* LockWord */ lock_word = LockWord(monitor)
5297 static_assert(sizeof(LockWord) == sizeof(int32_t),
5298 "art::LockWord and int32_t have different sizes.");
5299
5300 __ Sync(0); // Barrier to prevent load-load reordering.
5301
5302 // The actual reference load.
5303 if (index.IsValid()) {
5304 // Load types involving an "index": ArrayGet,
5305 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5306 // intrinsics.
5307 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5308 if (index.IsConstant()) {
5309 size_t computed_offset =
5310 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5311 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5312 } else {
5313 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005314 if (scale_factor == TIMES_1) {
5315 __ Daddu(TMP, index_reg, obj);
5316 } else {
5317 __ Dlsa(TMP, index_reg, obj, scale_factor);
5318 }
Alexey Frunze15958152017-02-09 19:08:30 -08005319 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5320 }
5321 } else {
5322 // /* HeapReference<Object> */ ref = *(obj + offset)
5323 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5324 }
5325
5326 // Object* ref = ref_addr->AsMirrorPtr()
5327 __ MaybeUnpoisonHeapReference(ref_reg);
5328
5329 // Slow path marking the object `ref` when it is gray.
5330 SlowPathCodeMIPS64* slow_path;
5331 if (always_update_field) {
5332 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5333 // of the form `obj + field_offset`, where `obj` is a register and
5334 // `field_offset` is a register. Thus `offset` and `scale_factor`
5335 // above are expected to be null in this code path.
5336 DCHECK_EQ(offset, 0u);
5337 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005338 slow_path = new (GetGraph()->GetAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005339 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5340 ref,
5341 obj,
5342 /* field_offset */ index,
5343 temp_reg);
5344 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005345 slow_path = new (GetGraph()->GetAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005346 }
5347 AddSlowPath(slow_path);
5348
5349 // if (rb_state == ReadBarrier::GrayState())
5350 // ref = ReadBarrier::Mark(ref);
5351 // Given the numeric representation, it's enough to check the low bit of the
5352 // rb_state. We do that by shifting the bit into the sign bit (31) and
5353 // performing a branch on less than zero.
5354 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5355 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5356 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5357 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5358 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5359 __ Bind(slow_path->GetExitLabel());
5360}
5361
5362void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5363 Location out,
5364 Location ref,
5365 Location obj,
5366 uint32_t offset,
5367 Location index) {
5368 DCHECK(kEmitCompilerReadBarrier);
5369
5370 // Insert a slow path based read barrier *after* the reference load.
5371 //
5372 // If heap poisoning is enabled, the unpoisoning of the loaded
5373 // reference will be carried out by the runtime within the slow
5374 // path.
5375 //
5376 // Note that `ref` currently does not get unpoisoned (when heap
5377 // poisoning is enabled), which is alright as the `ref` argument is
5378 // not used by the artReadBarrierSlow entry point.
5379 //
5380 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Markoca6fff82017-10-03 14:49:14 +01005381 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005382 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5383 AddSlowPath(slow_path);
5384
5385 __ Bc(slow_path->GetEntryLabel());
5386 __ Bind(slow_path->GetExitLabel());
5387}
5388
5389void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5390 Location out,
5391 Location ref,
5392 Location obj,
5393 uint32_t offset,
5394 Location index) {
5395 if (kEmitCompilerReadBarrier) {
5396 // Baker's read barriers shall be handled by the fast path
5397 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5398 DCHECK(!kUseBakerReadBarrier);
5399 // If heap poisoning is enabled, unpoisoning will be taken care of
5400 // by the runtime within the slow path.
5401 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5402 } else if (kPoisonHeapReferences) {
5403 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5404 }
5405}
5406
5407void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5408 Location out,
5409 Location root) {
5410 DCHECK(kEmitCompilerReadBarrier);
5411
5412 // Insert a slow path based read barrier *after* the GC root load.
5413 //
5414 // Note that GC roots are not affected by heap poisoning, so we do
5415 // not need to do anything special for this here.
5416 SlowPathCodeMIPS64* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005417 new (GetGraph()->GetAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005418 AddSlowPath(slow_path);
5419
5420 __ Bc(slow_path->GetEntryLabel());
5421 __ Bind(slow_path->GetExitLabel());
5422}
5423
Alexey Frunze4dda3372015-06-01 18:31:49 -07005424void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005425 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5426 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005427 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005428 switch (type_check_kind) {
5429 case TypeCheckKind::kExactCheck:
5430 case TypeCheckKind::kAbstractClassCheck:
5431 case TypeCheckKind::kClassHierarchyCheck:
5432 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005433 call_kind =
5434 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005435 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005436 break;
5437 case TypeCheckKind::kArrayCheck:
5438 case TypeCheckKind::kUnresolvedCheck:
5439 case TypeCheckKind::kInterfaceCheck:
5440 call_kind = LocationSummary::kCallOnSlowPath;
5441 break;
5442 }
5443
Vladimir Markoca6fff82017-10-03 14:49:14 +01005444 LocationSummary* locations =
5445 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005446 if (baker_read_barrier_slow_path) {
5447 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5448 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005449 locations->SetInAt(0, Location::RequiresRegister());
5450 locations->SetInAt(1, Location::RequiresRegister());
5451 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005452 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005453 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005454 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005455}
5456
5457void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005458 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005459 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005460 Location obj_loc = locations->InAt(0);
5461 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005462 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005463 Location out_loc = locations->Out();
5464 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5465 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5466 DCHECK_LE(num_temps, 1u);
5467 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005468 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5469 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5470 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5471 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005472 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005473 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005474
5475 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005476 // Avoid this check if we know `obj` is not null.
5477 if (instruction->MustDoNullCheck()) {
5478 __ Move(out, ZERO);
5479 __ Beqzc(obj, &done);
5480 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005481
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005482 switch (type_check_kind) {
5483 case TypeCheckKind::kExactCheck: {
5484 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005485 GenerateReferenceLoadTwoRegisters(instruction,
5486 out_loc,
5487 obj_loc,
5488 class_offset,
5489 maybe_temp_loc,
5490 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005491 // Classes must be equal for the instanceof to succeed.
5492 __ Xor(out, out, cls);
5493 __ Sltiu(out, out, 1);
5494 break;
5495 }
5496
5497 case TypeCheckKind::kAbstractClassCheck: {
5498 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005499 GenerateReferenceLoadTwoRegisters(instruction,
5500 out_loc,
5501 obj_loc,
5502 class_offset,
5503 maybe_temp_loc,
5504 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005505 // If the class is abstract, we eagerly fetch the super class of the
5506 // object to avoid doing a comparison we know will fail.
5507 Mips64Label loop;
5508 __ Bind(&loop);
5509 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005510 GenerateReferenceLoadOneRegister(instruction,
5511 out_loc,
5512 super_offset,
5513 maybe_temp_loc,
5514 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005515 // If `out` is null, we use it for the result, and jump to `done`.
5516 __ Beqzc(out, &done);
5517 __ Bnec(out, cls, &loop);
5518 __ LoadConst32(out, 1);
5519 break;
5520 }
5521
5522 case TypeCheckKind::kClassHierarchyCheck: {
5523 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005524 GenerateReferenceLoadTwoRegisters(instruction,
5525 out_loc,
5526 obj_loc,
5527 class_offset,
5528 maybe_temp_loc,
5529 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005530 // Walk over the class hierarchy to find a match.
5531 Mips64Label loop, success;
5532 __ Bind(&loop);
5533 __ Beqc(out, cls, &success);
5534 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005535 GenerateReferenceLoadOneRegister(instruction,
5536 out_loc,
5537 super_offset,
5538 maybe_temp_loc,
5539 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005540 __ Bnezc(out, &loop);
5541 // If `out` is null, we use it for the result, and jump to `done`.
5542 __ Bc(&done);
5543 __ Bind(&success);
5544 __ LoadConst32(out, 1);
5545 break;
5546 }
5547
5548 case TypeCheckKind::kArrayObjectCheck: {
5549 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005550 GenerateReferenceLoadTwoRegisters(instruction,
5551 out_loc,
5552 obj_loc,
5553 class_offset,
5554 maybe_temp_loc,
5555 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005556 // Do an exact check.
5557 Mips64Label success;
5558 __ Beqc(out, cls, &success);
5559 // Otherwise, we need to check that the object's class is a non-primitive array.
5560 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005561 GenerateReferenceLoadOneRegister(instruction,
5562 out_loc,
5563 component_offset,
5564 maybe_temp_loc,
5565 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005566 // If `out` is null, we use it for the result, and jump to `done`.
5567 __ Beqzc(out, &done);
5568 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5569 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5570 __ Sltiu(out, out, 1);
5571 __ Bc(&done);
5572 __ Bind(&success);
5573 __ LoadConst32(out, 1);
5574 break;
5575 }
5576
5577 case TypeCheckKind::kArrayCheck: {
5578 // No read barrier since the slow path will retry upon failure.
5579 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005580 GenerateReferenceLoadTwoRegisters(instruction,
5581 out_loc,
5582 obj_loc,
5583 class_offset,
5584 maybe_temp_loc,
5585 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005586 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Markoca6fff82017-10-03 14:49:14 +01005587 slow_path = new (GetGraph()->GetAllocator()) TypeCheckSlowPathMIPS64(instruction,
5588 /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005589 codegen_->AddSlowPath(slow_path);
5590 __ Bnec(out, cls, slow_path->GetEntryLabel());
5591 __ LoadConst32(out, 1);
5592 break;
5593 }
5594
5595 case TypeCheckKind::kUnresolvedCheck:
5596 case TypeCheckKind::kInterfaceCheck: {
5597 // Note that we indeed only call on slow path, but we always go
5598 // into the slow path for the unresolved and interface check
5599 // cases.
5600 //
5601 // We cannot directly call the InstanceofNonTrivial runtime
5602 // entry point without resorting to a type checking slow path
5603 // here (i.e. by calling InvokeRuntime directly), as it would
5604 // require to assign fixed registers for the inputs of this
5605 // HInstanceOf instruction (following the runtime calling
5606 // convention), which might be cluttered by the potential first
5607 // read barrier emission at the beginning of this method.
5608 //
5609 // TODO: Introduce a new runtime entry point taking the object
5610 // to test (instead of its class) as argument, and let it deal
5611 // with the read barrier issues. This will let us refactor this
5612 // case of the `switch` code as it was previously (with a direct
5613 // call to the runtime not using a type checking slow path).
5614 // This should also be beneficial for the other cases above.
5615 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Markoca6fff82017-10-03 14:49:14 +01005616 slow_path = new (GetGraph()->GetAllocator()) TypeCheckSlowPathMIPS64(instruction,
5617 /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005618 codegen_->AddSlowPath(slow_path);
5619 __ Bc(slow_path->GetEntryLabel());
5620 break;
5621 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005622 }
5623
5624 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005625
5626 if (slow_path != nullptr) {
5627 __ Bind(slow_path->GetExitLabel());
5628 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005629}
5630
5631void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005632 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005633 locations->SetOut(Location::ConstantLocation(constant));
5634}
5635
5636void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5637 // Will be generated at use site.
5638}
5639
5640void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005641 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005642 locations->SetOut(Location::ConstantLocation(constant));
5643}
5644
5645void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5646 // Will be generated at use site.
5647}
5648
Calin Juravle175dc732015-08-25 15:42:32 +01005649void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5650 // The trampoline uses the same calling convention as dex calling conventions,
5651 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5652 // the method_idx.
5653 HandleInvoke(invoke);
5654}
5655
5656void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5657 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5658}
5659
Alexey Frunze4dda3372015-06-01 18:31:49 -07005660void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5661 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5662 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5663}
5664
5665void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5666 HandleInvoke(invoke);
5667 // The register T0 is required to be used for the hidden argument in
5668 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5669 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5670}
5671
5672void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5673 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5674 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005675 Location receiver = invoke->GetLocations()->InAt(0);
5676 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005677 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005678
5679 // Set the hidden argument.
5680 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5681 invoke->GetDexMethodIndex());
5682
5683 // temp = object->GetClass();
5684 if (receiver.IsStackSlot()) {
5685 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5686 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5687 } else {
5688 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5689 }
5690 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005691 // Instead of simply (possibly) unpoisoning `temp` here, we should
5692 // emit a read barrier for the previous class reference load.
5693 // However this is not required in practice, as this is an
5694 // intermediate/temporary reference and because the current
5695 // concurrent copying collector keeps the from-space memory
5696 // intact/accessible until the end of the marking phase (the
5697 // concurrent copying collector may not in the future).
5698 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005699 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5700 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5701 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005702 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005703 // temp = temp->GetImtEntryAt(method_offset);
5704 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5705 // T9 = temp->GetEntryPoint();
5706 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5707 // T9();
5708 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005709 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005710 DCHECK(!codegen_->IsLeafMethod());
5711 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5712}
5713
5714void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005715 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5716 if (intrinsic.TryDispatch(invoke)) {
5717 return;
5718 }
5719
Alexey Frunze4dda3372015-06-01 18:31:49 -07005720 HandleInvoke(invoke);
5721}
5722
5723void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005724 // Explicit clinit checks triggered by static invokes must have been pruned by
5725 // art::PrepareForRegisterAllocation.
5726 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005727
Chris Larsen3039e382015-08-26 07:54:08 -07005728 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5729 if (intrinsic.TryDispatch(invoke)) {
5730 return;
5731 }
5732
Alexey Frunze4dda3372015-06-01 18:31:49 -07005733 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005734}
5735
Orion Hodsonac141392017-01-13 11:53:47 +00005736void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5737 HandleInvoke(invoke);
5738}
5739
5740void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5741 codegen_->GenerateInvokePolymorphicCall(invoke);
5742}
5743
Chris Larsen3039e382015-08-26 07:54:08 -07005744static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005745 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005746 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5747 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005748 return true;
5749 }
5750 return false;
5751}
5752
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005753HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005754 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005755 bool fallback_load = false;
5756 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005757 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005758 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005759 case HLoadString::LoadKind::kBssEntry:
5760 DCHECK(!Runtime::Current()->UseJitCompilation());
5761 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005762 case HLoadString::LoadKind::kJitTableAddress:
5763 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005764 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005765 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005766 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005767 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005768 }
5769 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005770 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005771 }
5772 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005773}
5774
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005775HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5776 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005777 bool fallback_load = false;
5778 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005779 case HLoadClass::LoadKind::kInvalid:
5780 LOG(FATAL) << "UNREACHABLE";
5781 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005782 case HLoadClass::LoadKind::kReferrersClass:
5783 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005784 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005785 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005786 case HLoadClass::LoadKind::kBssEntry:
5787 DCHECK(!Runtime::Current()->UseJitCompilation());
5788 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005789 case HLoadClass::LoadKind::kJitTableAddress:
5790 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005791 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005792 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005793 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005794 break;
5795 }
5796 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005797 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005798 }
5799 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800}
5801
Vladimir Markodc151b22015-10-15 18:02:30 +01005802HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5803 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005804 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005805 // On MIPS64 we support all dispatch types.
5806 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005807}
5808
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005809void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5810 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005811 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005812 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005813 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5814 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5815
Alexey Frunze19f6c692016-11-30 19:19:55 -08005816 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005817 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005818 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005819 uint32_t offset =
5820 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005821 __ LoadFromOffset(kLoadDoubleword,
5822 temp.AsRegister<GpuRegister>(),
5823 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005824 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005825 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005826 }
Vladimir Marko58155012015-08-19 12:49:41 +00005827 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005828 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005829 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005830 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5831 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005832 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005833 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005834 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5835 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5836 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005837 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5838 break;
5839 }
Vladimir Marko58155012015-08-19 12:49:41 +00005840 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005841 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5842 kLoadDoubleword,
5843 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005844 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005845 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005846 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005847 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005848 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5849 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5850 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005851 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5852 break;
5853 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005854 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5855 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5856 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005857 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005858 }
5859
Alexey Frunze19f6c692016-11-30 19:19:55 -08005860 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005861 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005862 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005863 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005864 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5865 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5866 __ LoadFromOffset(kLoadDoubleword,
5867 T9,
5868 callee_method.AsRegister<GpuRegister>(),
5869 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005870 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005871 // T9()
5872 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005873 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005874 break;
5875 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005876 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5877
Alexey Frunze4dda3372015-06-01 18:31:49 -07005878 DCHECK(!IsLeafMethod());
5879}
5880
5881void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005882 // Explicit clinit checks triggered by static invokes must have been pruned by
5883 // art::PrepareForRegisterAllocation.
5884 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005885
5886 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5887 return;
5888 }
5889
5890 LocationSummary* locations = invoke->GetLocations();
5891 codegen_->GenerateStaticOrDirectCall(invoke,
5892 locations->HasTemps()
5893 ? locations->GetTemp(0)
5894 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005895}
5896
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005897void CodeGeneratorMIPS64::GenerateVirtualCall(
5898 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005899 // Use the calling convention instead of the location of the receiver, as
5900 // intrinsics may have put the receiver in a different register. In the intrinsics
5901 // slow path, the arguments have been moved to the right place, so here we are
5902 // guaranteed that the receiver is the first register of the calling convention.
5903 InvokeDexCallingConvention calling_convention;
5904 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5905
Alexey Frunze53afca12015-11-05 16:34:23 -08005906 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005907 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5908 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5909 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005910 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005911
5912 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005913 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005914 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005915 // Instead of simply (possibly) unpoisoning `temp` here, we should
5916 // emit a read barrier for the previous class reference load.
5917 // However this is not required in practice, as this is an
5918 // intermediate/temporary reference and because the current
5919 // concurrent copying collector keeps the from-space memory
5920 // intact/accessible until the end of the marking phase (the
5921 // concurrent copying collector may not in the future).
5922 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005923 // temp = temp->GetMethodAt(method_offset);
5924 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5925 // T9 = temp->GetEntryPoint();
5926 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5927 // T9();
5928 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005929 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005930 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005931}
5932
5933void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5934 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5935 return;
5936 }
5937
5938 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005939 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005940}
5941
5942void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005943 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005944 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005945 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005946 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5947 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005948 return;
5949 }
Vladimir Marko41559982017-01-06 14:04:23 +00005950 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005951
Alexey Frunze15958152017-02-09 19:08:30 -08005952 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5953 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005954 ? LocationSummary::kCallOnSlowPath
5955 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005956 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005957 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5958 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5959 }
Vladimir Marko41559982017-01-06 14:04:23 +00005960 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005961 locations->SetInAt(0, Location::RequiresRegister());
5962 }
5963 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005964 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5965 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5966 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005967 // Request a temp to hold the BSS entry location for the slow path.
5968 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005969 RegisterSet caller_saves = RegisterSet::Empty();
5970 InvokeRuntimeCallingConvention calling_convention;
5971 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5972 locations->SetCustomSlowPathCallerSaves(caller_saves);
5973 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005974 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005975 }
5976 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005977}
5978
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005979// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5980// move.
5981void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005982 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005983 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005984 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005985 return;
5986 }
Vladimir Marko41559982017-01-06 14:04:23 +00005987 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005988
Vladimir Marko41559982017-01-06 14:04:23 +00005989 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005990 Location out_loc = locations->Out();
5991 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5992 GpuRegister current_method_reg = ZERO;
5993 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005994 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005995 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5996 }
5997
Alexey Frunze15958152017-02-09 19:08:30 -08005998 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5999 ? kWithoutReadBarrier
6000 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006001 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006002 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006003 switch (load_kind) {
6004 case HLoadClass::LoadKind::kReferrersClass:
6005 DCHECK(!cls->CanCallRuntime());
6006 DCHECK(!cls->MustGenerateClinitCheck());
6007 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6008 GenerateGcRootFieldLoad(cls,
6009 out_loc,
6010 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006011 ArtMethod::DeclaringClassOffset().Int32Value(),
6012 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006013 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006014 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006015 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006016 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006017 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006018 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006019 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6020 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6021 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006022 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6023 break;
6024 }
6025 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006026 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006027 uint32_t address = dchecked_integral_cast<uint32_t>(
6028 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6029 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006030 __ LoadLiteral(out,
6031 kLoadUnsignedWord,
6032 codegen_->DeduplicateBootImageAddressLiteral(address));
6033 break;
6034 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006035 case HLoadClass::LoadKind::kBootImageClassTable: {
6036 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6037 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6038 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6039 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6040 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6041 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6042 __ Lwu(out, AT, /* placeholder */ 0x5678);
6043 // Extract the reference from the slot data, i.e. clear the hash bits.
6044 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6045 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6046 if (masked_hash != 0) {
6047 __ Daddiu(out, out, -masked_hash);
6048 }
6049 break;
6050 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006051 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006052 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
6053 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6054 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
6055 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6056 GpuRegister temp = non_baker_read_barrier
6057 ? out
6058 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006059 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
6060 GenerateGcRootFieldLoad(cls,
6061 out_loc,
6062 temp,
6063 /* placeholder */ 0x5678,
6064 read_barrier_option,
6065 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006066 generate_null_check = true;
6067 break;
6068 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006069 case HLoadClass::LoadKind::kJitTableAddress:
6070 __ LoadLiteral(out,
6071 kLoadUnsignedWord,
6072 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6073 cls->GetTypeIndex(),
6074 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006075 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006076 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006077 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006078 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006079 LOG(FATAL) << "UNREACHABLE";
6080 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006081 }
6082
6083 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6084 DCHECK(cls->CanCallRuntime());
Vladimir Markoca6fff82017-10-03 14:49:14 +01006085 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006086 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006087 codegen_->AddSlowPath(slow_path);
6088 if (generate_null_check) {
6089 __ Beqzc(out, slow_path->GetEntryLabel());
6090 }
6091 if (cls->MustGenerateClinitCheck()) {
6092 GenerateClassInitializationCheck(slow_path, out);
6093 } else {
6094 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006095 }
6096 }
6097}
6098
David Brazdilcb1c0552015-08-04 16:22:25 +01006099static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006100 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006101}
6102
Alexey Frunze4dda3372015-06-01 18:31:49 -07006103void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6104 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006105 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006106 locations->SetOut(Location::RequiresRegister());
6107}
6108
6109void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6110 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006111 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6112}
6113
6114void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006115 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006116}
6117
6118void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6119 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006120}
6121
Alexey Frunze4dda3372015-06-01 18:31:49 -07006122void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006123 HLoadString::LoadKind load_kind = load->GetLoadKind();
6124 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006125 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006126 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006127 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006128 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006129 } else {
6130 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006131 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6132 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6133 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006134 // Request a temp to hold the BSS entry location for the slow path.
6135 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006136 RegisterSet caller_saves = RegisterSet::Empty();
6137 InvokeRuntimeCallingConvention calling_convention;
6138 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6139 locations->SetCustomSlowPathCallerSaves(caller_saves);
6140 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006141 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006142 }
6143 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006144 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006145}
6146
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006147// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6148// move.
6149void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006150 HLoadString::LoadKind load_kind = load->GetLoadKind();
6151 LocationSummary* locations = load->GetLocations();
6152 Location out_loc = locations->Out();
6153 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6154
6155 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006156 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6157 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006158 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006159 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006160 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6161 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6162 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006163 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006164 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006165 }
6166 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006167 uint32_t address = dchecked_integral_cast<uint32_t>(
6168 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6169 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006170 __ LoadLiteral(out,
6171 kLoadUnsignedWord,
6172 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006173 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006174 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006175 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006176 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006177 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006178 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006179 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6180 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006181 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6182 __ Lwu(out, AT, /* placeholder */ 0x5678);
6183 return;
6184 }
6185 case HLoadString::LoadKind::kBssEntry: {
6186 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6187 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6188 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6189 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6190 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006191 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6192 GpuRegister temp = non_baker_read_barrier
6193 ? out
6194 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006195 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08006196 GenerateGcRootFieldLoad(load,
6197 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006198 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08006199 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006200 kCompilerReadBarrierOption,
6201 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006202 SlowPathCodeMIPS64* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006203 new (GetGraph()->GetAllocator()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006204 codegen_->AddSlowPath(slow_path);
6205 __ Beqzc(out, slow_path->GetEntryLabel());
6206 __ Bind(slow_path->GetExitLabel());
6207 return;
6208 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006209 case HLoadString::LoadKind::kJitTableAddress:
6210 __ LoadLiteral(out,
6211 kLoadUnsignedWord,
6212 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6213 load->GetStringIndex(),
6214 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006215 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006216 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006217 default:
6218 break;
6219 }
6220
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006221 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006222 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006223 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006224 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006225 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6226 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6227 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006228}
6229
Alexey Frunze4dda3372015-06-01 18:31:49 -07006230void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006231 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006232 locations->SetOut(Location::ConstantLocation(constant));
6233}
6234
6235void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6236 // Will be generated at use site.
6237}
6238
6239void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006240 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6241 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006242 InvokeRuntimeCallingConvention calling_convention;
6243 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6244}
6245
6246void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006247 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006248 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006249 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006250 if (instruction->IsEnter()) {
6251 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6252 } else {
6253 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6254 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006255}
6256
6257void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6258 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006259 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006260 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006261 case DataType::Type::kInt32:
6262 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006263 locations->SetInAt(0, Location::RequiresRegister());
6264 locations->SetInAt(1, Location::RequiresRegister());
6265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6266 break;
6267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006268 case DataType::Type::kFloat32:
6269 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006270 locations->SetInAt(0, Location::RequiresFpuRegister());
6271 locations->SetInAt(1, Location::RequiresFpuRegister());
6272 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6273 break;
6274
6275 default:
6276 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6277 }
6278}
6279
6280void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006281 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006282 LocationSummary* locations = instruction->GetLocations();
6283
6284 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006285 case DataType::Type::kInt32:
6286 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006287 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6288 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6289 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006290 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006291 __ MulR6(dst, lhs, rhs);
6292 else
6293 __ Dmul(dst, lhs, rhs);
6294 break;
6295 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006296 case DataType::Type::kFloat32:
6297 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006298 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6299 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6300 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006302 __ MulS(dst, lhs, rhs);
6303 else
6304 __ MulD(dst, lhs, rhs);
6305 break;
6306 }
6307 default:
6308 LOG(FATAL) << "Unexpected mul type " << type;
6309 }
6310}
6311
6312void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6313 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006314 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006315 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006316 case DataType::Type::kInt32:
6317 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006318 locations->SetInAt(0, Location::RequiresRegister());
6319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6320 break;
6321
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 case DataType::Type::kFloat32:
6323 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006324 locations->SetInAt(0, Location::RequiresFpuRegister());
6325 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6326 break;
6327
6328 default:
6329 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6330 }
6331}
6332
6333void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006334 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006335 LocationSummary* locations = instruction->GetLocations();
6336
6337 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 case DataType::Type::kInt32:
6339 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006340 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6341 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006342 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006343 __ Subu(dst, ZERO, src);
6344 else
6345 __ Dsubu(dst, ZERO, src);
6346 break;
6347 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 case DataType::Type::kFloat32:
6349 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006350 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6351 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006352 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006353 __ NegS(dst, src);
6354 else
6355 __ NegD(dst, src);
6356 break;
6357 }
6358 default:
6359 LOG(FATAL) << "Unexpected neg type " << type;
6360 }
6361}
6362
6363void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006364 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6365 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006366 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006367 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006368 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6369 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006370}
6371
6372void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006373 // Note: if heap poisoning is enabled, the entry point takes care
6374 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006375 QuickEntrypointEnum entrypoint =
6376 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6377 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006378 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006379 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006380}
6381
6382void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006383 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6384 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006385 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006386 if (instruction->IsStringAlloc()) {
6387 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6388 } else {
6389 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006390 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006392}
6393
6394void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006395 // Note: if heap poisoning is enabled, the entry point takes care
6396 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006397 if (instruction->IsStringAlloc()) {
6398 // String is allocated through StringFactory. Call NewEmptyString entry point.
6399 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006400 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006401 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006402 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6403 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6404 __ Jalr(T9);
6405 __ Nop();
6406 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6407 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006408 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006409 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006410 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006411}
6412
6413void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006414 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006415 locations->SetInAt(0, Location::RequiresRegister());
6416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6417}
6418
6419void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006420 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006421 LocationSummary* locations = instruction->GetLocations();
6422
6423 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006424 case DataType::Type::kInt32:
6425 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006426 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6427 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6428 __ Nor(dst, src, ZERO);
6429 break;
6430 }
6431
6432 default:
6433 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6434 }
6435}
6436
6437void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006438 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006439 locations->SetInAt(0, Location::RequiresRegister());
6440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6441}
6442
6443void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6444 LocationSummary* locations = instruction->GetLocations();
6445 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6446 locations->InAt(0).AsRegister<GpuRegister>(),
6447 1);
6448}
6449
6450void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006451 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6452 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006453}
6454
Calin Juravle2ae48182016-03-16 14:05:09 +00006455void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6456 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006457 return;
6458 }
6459 Location obj = instruction->GetLocations()->InAt(0);
6460
6461 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006462 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006463}
6464
Calin Juravle2ae48182016-03-16 14:05:09 +00006465void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006466 SlowPathCodeMIPS64* slow_path =
6467 new (GetGraph()->GetAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006468 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006469
6470 Location obj = instruction->GetLocations()->InAt(0);
6471
6472 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6473}
6474
6475void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006476 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006477}
6478
6479void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6480 HandleBinaryOp(instruction);
6481}
6482
6483void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6484 HandleBinaryOp(instruction);
6485}
6486
6487void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6488 LOG(FATAL) << "Unreachable";
6489}
6490
6491void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006492 if (instruction->GetNext()->IsSuspendCheck() &&
6493 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6494 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6495 // The back edge will generate the suspend check.
6496 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6497 }
6498
Alexey Frunze4dda3372015-06-01 18:31:49 -07006499 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6500}
6501
6502void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006503 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006504 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6505 if (location.IsStackSlot()) {
6506 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6507 } else if (location.IsDoubleStackSlot()) {
6508 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6509 }
6510 locations->SetOut(location);
6511}
6512
6513void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6514 ATTRIBUTE_UNUSED) {
6515 // Nothing to do, the parameter is already at its location.
6516}
6517
6518void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6519 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006520 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006521 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6522}
6523
6524void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6525 ATTRIBUTE_UNUSED) {
6526 // Nothing to do, the method is already at its location.
6527}
6528
6529void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006530 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006531 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006532 locations->SetInAt(i, Location::Any());
6533 }
6534 locations->SetOut(Location::Any());
6535}
6536
6537void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6538 LOG(FATAL) << "Unreachable";
6539}
6540
6541void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006542 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006543 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006544 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6545 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006546 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006547
6548 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006549 case DataType::Type::kInt32:
6550 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006551 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006552 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6554 break;
6555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006556 case DataType::Type::kFloat32:
6557 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006558 InvokeRuntimeCallingConvention calling_convention;
6559 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6560 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6561 locations->SetOut(calling_convention.GetReturnLocation(type));
6562 break;
6563 }
6564
6565 default:
6566 LOG(FATAL) << "Unexpected rem type " << type;
6567 }
6568}
6569
6570void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006572
6573 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006574 case DataType::Type::kInt32:
6575 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006576 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006577 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006579 case DataType::Type::kFloat32:
6580 case DataType::Type::kFloat64: {
6581 QuickEntrypointEnum entrypoint =
6582 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006583 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006584 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006585 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6586 } else {
6587 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6588 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006589 break;
6590 }
6591 default:
6592 LOG(FATAL) << "Unexpected rem type " << type;
6593 }
6594}
6595
Igor Murashkind01745e2017-04-05 16:40:31 -07006596void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6597 constructor_fence->SetLocations(nullptr);
6598}
6599
6600void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6601 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6602 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6603}
6604
Alexey Frunze4dda3372015-06-01 18:31:49 -07006605void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6606 memory_barrier->SetLocations(nullptr);
6607}
6608
6609void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6610 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6611}
6612
6613void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006614 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006615 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006616 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6617}
6618
6619void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6620 codegen_->GenerateFrameExit();
6621}
6622
6623void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6624 ret->SetLocations(nullptr);
6625}
6626
6627void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6628 codegen_->GenerateFrameExit();
6629}
6630
Alexey Frunze92d90602015-12-18 18:16:36 -08006631void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6632 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006633}
6634
Alexey Frunze92d90602015-12-18 18:16:36 -08006635void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6636 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006637}
6638
Alexey Frunze4dda3372015-06-01 18:31:49 -07006639void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6640 HandleShift(shl);
6641}
6642
6643void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6644 HandleShift(shl);
6645}
6646
6647void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6648 HandleShift(shr);
6649}
6650
6651void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6652 HandleShift(shr);
6653}
6654
Alexey Frunze4dda3372015-06-01 18:31:49 -07006655void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6656 HandleBinaryOp(instruction);
6657}
6658
6659void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6660 HandleBinaryOp(instruction);
6661}
6662
6663void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6664 HandleFieldGet(instruction, instruction->GetFieldInfo());
6665}
6666
6667void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6668 HandleFieldGet(instruction, instruction->GetFieldInfo());
6669}
6670
6671void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6672 HandleFieldSet(instruction, instruction->GetFieldInfo());
6673}
6674
6675void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006676 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006677}
6678
Calin Juravlee460d1d2015-09-29 04:52:17 +01006679void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6680 HUnresolvedInstanceFieldGet* instruction) {
6681 FieldAccessCallingConventionMIPS64 calling_convention;
6682 codegen_->CreateUnresolvedFieldLocationSummary(
6683 instruction, instruction->GetFieldType(), calling_convention);
6684}
6685
6686void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6687 HUnresolvedInstanceFieldGet* instruction) {
6688 FieldAccessCallingConventionMIPS64 calling_convention;
6689 codegen_->GenerateUnresolvedFieldAccess(instruction,
6690 instruction->GetFieldType(),
6691 instruction->GetFieldIndex(),
6692 instruction->GetDexPc(),
6693 calling_convention);
6694}
6695
6696void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6697 HUnresolvedInstanceFieldSet* instruction) {
6698 FieldAccessCallingConventionMIPS64 calling_convention;
6699 codegen_->CreateUnresolvedFieldLocationSummary(
6700 instruction, instruction->GetFieldType(), calling_convention);
6701}
6702
6703void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6704 HUnresolvedInstanceFieldSet* instruction) {
6705 FieldAccessCallingConventionMIPS64 calling_convention;
6706 codegen_->GenerateUnresolvedFieldAccess(instruction,
6707 instruction->GetFieldType(),
6708 instruction->GetFieldIndex(),
6709 instruction->GetDexPc(),
6710 calling_convention);
6711}
6712
6713void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6714 HUnresolvedStaticFieldGet* instruction) {
6715 FieldAccessCallingConventionMIPS64 calling_convention;
6716 codegen_->CreateUnresolvedFieldLocationSummary(
6717 instruction, instruction->GetFieldType(), calling_convention);
6718}
6719
6720void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6721 HUnresolvedStaticFieldGet* instruction) {
6722 FieldAccessCallingConventionMIPS64 calling_convention;
6723 codegen_->GenerateUnresolvedFieldAccess(instruction,
6724 instruction->GetFieldType(),
6725 instruction->GetFieldIndex(),
6726 instruction->GetDexPc(),
6727 calling_convention);
6728}
6729
6730void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6731 HUnresolvedStaticFieldSet* instruction) {
6732 FieldAccessCallingConventionMIPS64 calling_convention;
6733 codegen_->CreateUnresolvedFieldLocationSummary(
6734 instruction, instruction->GetFieldType(), calling_convention);
6735}
6736
6737void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6738 HUnresolvedStaticFieldSet* instruction) {
6739 FieldAccessCallingConventionMIPS64 calling_convention;
6740 codegen_->GenerateUnresolvedFieldAccess(instruction,
6741 instruction->GetFieldType(),
6742 instruction->GetFieldIndex(),
6743 instruction->GetDexPc(),
6744 calling_convention);
6745}
6746
Alexey Frunze4dda3372015-06-01 18:31:49 -07006747void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006748 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6749 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006750 // In suspend check slow path, usually there are no caller-save registers at all.
6751 // If SIMD instructions are present, however, we force spilling all live SIMD
6752 // registers in full width (since the runtime only saves/restores lower part).
6753 locations->SetCustomSlowPathCallerSaves(
6754 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006755}
6756
6757void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6758 HBasicBlock* block = instruction->GetBlock();
6759 if (block->GetLoopInformation() != nullptr) {
6760 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6761 // The back edge will generate the suspend check.
6762 return;
6763 }
6764 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6765 // The goto will generate the suspend check.
6766 return;
6767 }
6768 GenerateSuspendCheck(instruction, nullptr);
6769}
6770
Alexey Frunze4dda3372015-06-01 18:31:49 -07006771void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006772 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6773 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006774 InvokeRuntimeCallingConvention calling_convention;
6775 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6776}
6777
6778void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006779 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006780 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6781}
6782
6783void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006784 DataType::Type input_type = conversion->GetInputType();
6785 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006786 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6787 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006789 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6790 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006791 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6792 }
6793
Vladimir Markoca6fff82017-10-03 14:49:14 +01006794 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006796 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006797 locations->SetInAt(0, Location::RequiresFpuRegister());
6798 } else {
6799 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006800 }
6801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006802 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006803 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006804 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006806 }
6807}
6808
6809void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6810 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006811 DataType::Type result_type = conversion->GetResultType();
6812 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006813
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006814 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6815 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006817 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006818 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6819 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6820
6821 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006822 case DataType::Type::kUint8:
6823 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006824 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006825 case DataType::Type::kInt8:
6826 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006827 // Type conversion from long to types narrower than int is a result of code
6828 // transformations. To avoid unpredictable results for SEB and SEH, we first
6829 // need to sign-extend the low 32-bit value into bits 32 through 63.
6830 __ Sll(dst, src, 0);
6831 __ Seb(dst, dst);
6832 } else {
6833 __ Seb(dst, src);
6834 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006835 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006836 case DataType::Type::kUint16:
6837 __ Andi(dst, src, 0xFFFF);
6838 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006839 case DataType::Type::kInt16:
6840 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006841 // Type conversion from long to types narrower than int is a result of code
6842 // transformations. To avoid unpredictable results for SEB and SEH, we first
6843 // need to sign-extend the low 32-bit value into bits 32 through 63.
6844 __ Sll(dst, src, 0);
6845 __ Seh(dst, dst);
6846 } else {
6847 __ Seh(dst, src);
6848 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006849 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006850 case DataType::Type::kInt32:
6851 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006852 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6853 // conversions, except when the input and output registers are the same and we are not
6854 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006855 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006856 __ Sll(dst, src, 0);
6857 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006858 break;
6859
6860 default:
6861 LOG(FATAL) << "Unexpected type conversion from " << input_type
6862 << " to " << result_type;
6863 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006864 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006865 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6866 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006867 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006868 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006869 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006870 __ Cvtsl(dst, FTMP);
6871 } else {
6872 __ Cvtdl(dst, FTMP);
6873 }
6874 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006875 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006876 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006877 __ Cvtsw(dst, FTMP);
6878 } else {
6879 __ Cvtdw(dst, FTMP);
6880 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006881 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006882 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6883 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006884 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6885 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006887 if (result_type == DataType::Type::kInt64) {
6888 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006889 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006890 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006891 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006892 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006893 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006894 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006895 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006896 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006897 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006898 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006899 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006900 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006901 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006902 } else if (DataType::IsFloatingPointType(result_type) &&
6903 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006904 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6905 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006906 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006907 __ Cvtsd(dst, src);
6908 } else {
6909 __ Cvtds(dst, src);
6910 }
6911 } else {
6912 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6913 << " to " << result_type;
6914 }
6915}
6916
6917void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6918 HandleShift(ushr);
6919}
6920
6921void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6922 HandleShift(ushr);
6923}
6924
6925void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6926 HandleBinaryOp(instruction);
6927}
6928
6929void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6930 HandleBinaryOp(instruction);
6931}
6932
6933void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6934 // Nothing to do, this should be removed during prepare for register allocator.
6935 LOG(FATAL) << "Unreachable";
6936}
6937
6938void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6939 // Nothing to do, this should be removed during prepare for register allocator.
6940 LOG(FATAL) << "Unreachable";
6941}
6942
6943void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006944 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006945}
6946
6947void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006948 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006949}
6950
6951void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006952 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006953}
6954
6955void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006956 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006957}
6958
6959void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006960 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006961}
6962
6963void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006964 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006965}
6966
6967void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006968 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006969}
6970
6971void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006972 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006973}
6974
6975void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006976 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006977}
6978
6979void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006980 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006981}
6982
6983void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006984 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006985}
6986
6987void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006988 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006989}
6990
Aart Bike9f37602015-10-09 11:15:55 -07006991void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006992 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006993}
6994
6995void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006997}
6998
6999void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007001}
7002
7003void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007005}
7006
7007void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007009}
7010
7011void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007013}
7014
7015void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007017}
7018
7019void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007021}
7022
Mark Mendellfe57faa2015-09-18 09:26:15 -04007023// Simple implementation of packed switch - generate cascaded compare/jumps.
7024void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7025 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007026 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007027 locations->SetInAt(0, Location::RequiresRegister());
7028}
7029
Alexey Frunze0960ac52016-12-20 17:24:59 -08007030void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7031 int32_t lower_bound,
7032 uint32_t num_entries,
7033 HBasicBlock* switch_block,
7034 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007035 // Create a set of compare/jumps.
7036 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007037 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007038 // Jump to default if index is negative
7039 // Note: We don't check the case that index is positive while value < lower_bound, because in
7040 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7041 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7042
Alexey Frunze0960ac52016-12-20 17:24:59 -08007043 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007044 // Jump to successors[0] if value == lower_bound.
7045 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7046 int32_t last_index = 0;
7047 for (; num_entries - last_index > 2; last_index += 2) {
7048 __ Addiu(temp_reg, temp_reg, -2);
7049 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7050 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7051 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7052 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7053 }
7054 if (num_entries - last_index == 2) {
7055 // The last missing case_value.
7056 __ Addiu(temp_reg, temp_reg, -1);
7057 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007058 }
7059
7060 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007061 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007062 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007063 }
7064}
7065
Alexey Frunze0960ac52016-12-20 17:24:59 -08007066void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7067 int32_t lower_bound,
7068 uint32_t num_entries,
7069 HBasicBlock* switch_block,
7070 HBasicBlock* default_block) {
7071 // Create a jump table.
7072 std::vector<Mips64Label*> labels(num_entries);
7073 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7074 for (uint32_t i = 0; i < num_entries; i++) {
7075 labels[i] = codegen_->GetLabelOf(successors[i]);
7076 }
7077 JumpTable* table = __ CreateJumpTable(std::move(labels));
7078
7079 // Is the value in range?
7080 __ Addiu32(TMP, value_reg, -lower_bound);
7081 __ LoadConst32(AT, num_entries);
7082 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7083
7084 // We are in the range of the table.
7085 // Load the target address from the jump table, indexing by the value.
7086 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007087 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007088 __ Lw(TMP, TMP, 0);
7089 // Compute the absolute target address by adding the table start address
7090 // (the table contains offsets to targets relative to its start).
7091 __ Daddu(TMP, TMP, AT);
7092 // And jump.
7093 __ Jr(TMP);
7094 __ Nop();
7095}
7096
7097void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7098 int32_t lower_bound = switch_instr->GetStartValue();
7099 uint32_t num_entries = switch_instr->GetNumEntries();
7100 LocationSummary* locations = switch_instr->GetLocations();
7101 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7102 HBasicBlock* switch_block = switch_instr->GetBlock();
7103 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7104
7105 if (num_entries > kPackedSwitchJumpTableThreshold) {
7106 GenTableBasedPackedSwitch(value_reg,
7107 lower_bound,
7108 num_entries,
7109 switch_block,
7110 default_block);
7111 } else {
7112 GenPackedSwitchWithCompares(value_reg,
7113 lower_bound,
7114 num_entries,
7115 switch_block,
7116 default_block);
7117 }
7118}
7119
Chris Larsenc9905a62017-03-13 17:06:18 -07007120void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7121 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007122 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007123 locations->SetInAt(0, Location::RequiresRegister());
7124 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007125}
7126
Chris Larsenc9905a62017-03-13 17:06:18 -07007127void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7128 LocationSummary* locations = instruction->GetLocations();
7129 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7130 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7131 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7132 __ LoadFromOffset(kLoadDoubleword,
7133 locations->Out().AsRegister<GpuRegister>(),
7134 locations->InAt(0).AsRegister<GpuRegister>(),
7135 method_offset);
7136 } else {
7137 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7138 instruction->GetIndex(), kMips64PointerSize));
7139 __ LoadFromOffset(kLoadDoubleword,
7140 locations->Out().AsRegister<GpuRegister>(),
7141 locations->InAt(0).AsRegister<GpuRegister>(),
7142 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7143 __ LoadFromOffset(kLoadDoubleword,
7144 locations->Out().AsRegister<GpuRegister>(),
7145 locations->Out().AsRegister<GpuRegister>(),
7146 method_offset);
7147 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007148}
7149
Alexey Frunze4dda3372015-06-01 18:31:49 -07007150} // namespace mips64
7151} // namespace art