blob: 687700380b74ba8cd82983677def42e762c08588 [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;
515 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
516 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;
913 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
914 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),
1044 move_resolver_(graph->GetArena(), this),
Goran Jakovljevic19680d32017-05-11 10:38:36 +02001045 assembler_(graph->GetArena(), &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>(),
1048 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001049 uint64_literals_(std::less<uint64_t>(),
1050 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001051 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001052 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001053 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001054 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001055 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001056 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001057 jit_string_patches_(StringReferenceValueComparator(),
1058 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1059 jit_class_patches_(TypeReferenceValueComparator(),
1060 graph->GetArena()->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 =
1838 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1839 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);
1863 LocationSummary* locations = new (GetGraph()->GetArena()) 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
1993 LocationSummary* locations = new (GetGraph()->GetArena()) 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 =
Alexey Frunze15958152017-02-09 19:08:30 -08002122 new (GetGraph()->GetArena()) 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) {
2388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2389 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
Alexey Frunze4dda3372015-06-01 18:31:49 -07002432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2433 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) {
2546 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2547 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 =
2703 new (GetGraph()->GetArena()) 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
2754 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002755 locations->SetInAt(0, Location::RequiresRegister());
2756 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002757 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002758}
2759
2760void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002761 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002762 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002763 Location obj_loc = locations->InAt(0);
2764 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002765 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002766 Location temp_loc = locations->GetTemp(0);
2767 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2768 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2769 DCHECK_LE(num_temps, 2u);
2770 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002771 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2772 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2773 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2774 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2775 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2776 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2777 const uint32_t object_array_data_offset =
2778 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2779 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002780
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002781 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2782 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2783 // read barriers is done for performance and code size reasons.
2784 bool is_type_check_slow_path_fatal = false;
2785 if (!kEmitCompilerReadBarrier) {
2786 is_type_check_slow_path_fatal =
2787 (type_check_kind == TypeCheckKind::kExactCheck ||
2788 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2789 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2790 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2791 !instruction->CanThrowIntoCatchBlock();
2792 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002793 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002794 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2795 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002796 codegen_->AddSlowPath(slow_path);
2797
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002798 // Avoid this check if we know `obj` is not null.
2799 if (instruction->MustDoNullCheck()) {
2800 __ Beqzc(obj, &done);
2801 }
2802
2803 switch (type_check_kind) {
2804 case TypeCheckKind::kExactCheck:
2805 case TypeCheckKind::kArrayCheck: {
2806 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002807 GenerateReferenceLoadTwoRegisters(instruction,
2808 temp_loc,
2809 obj_loc,
2810 class_offset,
2811 maybe_temp2_loc,
2812 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002813 // Jump to slow path for throwing the exception or doing a
2814 // more involved array check.
2815 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2816 break;
2817 }
2818
2819 case TypeCheckKind::kAbstractClassCheck: {
2820 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002821 GenerateReferenceLoadTwoRegisters(instruction,
2822 temp_loc,
2823 obj_loc,
2824 class_offset,
2825 maybe_temp2_loc,
2826 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002827 // If the class is abstract, we eagerly fetch the super class of the
2828 // object to avoid doing a comparison we know will fail.
2829 Mips64Label loop;
2830 __ Bind(&loop);
2831 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002832 GenerateReferenceLoadOneRegister(instruction,
2833 temp_loc,
2834 super_offset,
2835 maybe_temp2_loc,
2836 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002837 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2838 // exception.
2839 __ Beqzc(temp, slow_path->GetEntryLabel());
2840 // Otherwise, compare the classes.
2841 __ Bnec(temp, cls, &loop);
2842 break;
2843 }
2844
2845 case TypeCheckKind::kClassHierarchyCheck: {
2846 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002847 GenerateReferenceLoadTwoRegisters(instruction,
2848 temp_loc,
2849 obj_loc,
2850 class_offset,
2851 maybe_temp2_loc,
2852 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002853 // Walk over the class hierarchy to find a match.
2854 Mips64Label loop;
2855 __ Bind(&loop);
2856 __ Beqc(temp, cls, &done);
2857 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002858 GenerateReferenceLoadOneRegister(instruction,
2859 temp_loc,
2860 super_offset,
2861 maybe_temp2_loc,
2862 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002863 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2864 // exception. Otherwise, jump to the beginning of the loop.
2865 __ Bnezc(temp, &loop);
2866 __ Bc(slow_path->GetEntryLabel());
2867 break;
2868 }
2869
2870 case TypeCheckKind::kArrayObjectCheck: {
2871 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002872 GenerateReferenceLoadTwoRegisters(instruction,
2873 temp_loc,
2874 obj_loc,
2875 class_offset,
2876 maybe_temp2_loc,
2877 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002878 // Do an exact check.
2879 __ Beqc(temp, cls, &done);
2880 // Otherwise, we need to check that the object's class is a non-primitive array.
2881 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002882 GenerateReferenceLoadOneRegister(instruction,
2883 temp_loc,
2884 component_offset,
2885 maybe_temp2_loc,
2886 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002887 // If the component type is null, jump to the slow path to throw the exception.
2888 __ Beqzc(temp, slow_path->GetEntryLabel());
2889 // Otherwise, the object is indeed an array, further check that this component
2890 // type is not a primitive type.
2891 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2892 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2893 __ Bnezc(temp, slow_path->GetEntryLabel());
2894 break;
2895 }
2896
2897 case TypeCheckKind::kUnresolvedCheck:
2898 // We always go into the type check slow path for the unresolved check case.
2899 // We cannot directly call the CheckCast runtime entry point
2900 // without resorting to a type checking slow path here (i.e. by
2901 // calling InvokeRuntime directly), as it would require to
2902 // assign fixed registers for the inputs of this HInstanceOf
2903 // instruction (following the runtime calling convention), which
2904 // might be cluttered by the potential first read barrier
2905 // emission at the beginning of this method.
2906 __ Bc(slow_path->GetEntryLabel());
2907 break;
2908
2909 case TypeCheckKind::kInterfaceCheck: {
2910 // Avoid read barriers to improve performance of the fast path. We can not get false
2911 // positives by doing this.
2912 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002913 GenerateReferenceLoadTwoRegisters(instruction,
2914 temp_loc,
2915 obj_loc,
2916 class_offset,
2917 maybe_temp2_loc,
2918 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002919 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002920 GenerateReferenceLoadTwoRegisters(instruction,
2921 temp_loc,
2922 temp_loc,
2923 iftable_offset,
2924 maybe_temp2_loc,
2925 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002926 // Iftable is never null.
2927 __ Lw(TMP, temp, array_length_offset);
2928 // Loop through the iftable and check if any class matches.
2929 Mips64Label loop;
2930 __ Bind(&loop);
2931 __ Beqzc(TMP, slow_path->GetEntryLabel());
2932 __ Lwu(AT, temp, object_array_data_offset);
2933 __ MaybeUnpoisonHeapReference(AT);
2934 // Go to next interface.
2935 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2936 __ Addiu(TMP, TMP, -2);
2937 // Compare the classes and continue the loop if they do not match.
2938 __ Bnec(AT, cls, &loop);
2939 break;
2940 }
2941 }
2942
2943 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002944 __ Bind(slow_path->GetExitLabel());
2945}
2946
2947void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2948 LocationSummary* locations =
2949 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2950 locations->SetInAt(0, Location::RequiresRegister());
2951 if (check->HasUses()) {
2952 locations->SetOut(Location::SameAsFirstInput());
2953 }
2954}
2955
2956void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2957 // We assume the class is not null.
2958 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2959 check->GetLoadClass(),
2960 check,
2961 check->GetDexPc(),
2962 true);
2963 codegen_->AddSlowPath(slow_path);
2964 GenerateClassInitializationCheck(slow_path,
2965 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2966}
2967
2968void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970
Alexey Frunze299a9392015-12-08 16:08:02 -08002971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972
2973 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002975 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002978 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002979 case DataType::Type::kInt32:
2980 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002981 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002982 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2984 break;
2985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kFloat32:
2987 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08002988 locations->SetInAt(0, Location::RequiresFpuRegister());
2989 locations->SetInAt(1, Location::RequiresFpuRegister());
2990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002991 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002992
2993 default:
2994 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2995 }
2996}
2997
2998void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2999 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003000 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002
3003 // 0 if: left == right
3004 // 1 if: left > right
3005 // -1 if: left < right
3006 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003008 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003009 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003011 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kInt32:
3013 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003014 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003015 Location rhs_location = locations->InAt(1);
3016 bool use_imm = rhs_location.IsConstant();
3017 GpuRegister rhs = ZERO;
3018 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003020 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3021 if (value != 0) {
3022 rhs = AT;
3023 __ LoadConst64(rhs, value);
3024 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003025 } else {
3026 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3027 if (value != 0) {
3028 rhs = AT;
3029 __ LoadConst32(rhs, value);
3030 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003031 }
3032 } else {
3033 rhs = rhs_location.AsRegister<GpuRegister>();
3034 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003035 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003036 __ Slt(res, rhs, lhs);
3037 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003038 break;
3039 }
3040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003042 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3043 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3044 Mips64Label done;
3045 __ CmpEqS(FTMP, lhs, rhs);
3046 __ LoadConst32(res, 0);
3047 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003048 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003049 __ CmpLtS(FTMP, lhs, rhs);
3050 __ LoadConst32(res, -1);
3051 __ Bc1nez(FTMP, &done);
3052 __ LoadConst32(res, 1);
3053 } else {
3054 __ CmpLtS(FTMP, rhs, lhs);
3055 __ LoadConst32(res, 1);
3056 __ Bc1nez(FTMP, &done);
3057 __ LoadConst32(res, -1);
3058 }
3059 __ Bind(&done);
3060 break;
3061 }
3062
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003063 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003064 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3065 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3066 Mips64Label done;
3067 __ CmpEqD(FTMP, lhs, rhs);
3068 __ LoadConst32(res, 0);
3069 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003070 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003071 __ CmpLtD(FTMP, lhs, rhs);
3072 __ LoadConst32(res, -1);
3073 __ Bc1nez(FTMP, &done);
3074 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003075 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003076 __ CmpLtD(FTMP, rhs, lhs);
3077 __ LoadConst32(res, 1);
3078 __ Bc1nez(FTMP, &done);
3079 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003080 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003081 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003082 break;
3083 }
3084
3085 default:
3086 LOG(FATAL) << "Unimplemented compare type " << in_type;
3087 }
3088}
3089
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003090void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003092 switch (instruction->InputAt(0)->GetType()) {
3093 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003095 locations->SetInAt(0, Location::RequiresRegister());
3096 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3097 break;
3098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003099 case DataType::Type::kFloat32:
3100 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003101 locations->SetInAt(0, Location::RequiresFpuRegister());
3102 locations->SetInAt(1, Location::RequiresFpuRegister());
3103 break;
3104 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003105 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003106 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3107 }
3108}
3109
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003110void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003111 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003112 return;
3113 }
3114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003115 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003116 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003117 switch (type) {
3118 default:
3119 // Integer case.
3120 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3121 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003123 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3124 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kFloat32:
3126 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003127 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3128 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003129 }
3130}
3131
Alexey Frunzec857c742015-09-23 15:12:39 -07003132void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3133 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003134 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003135
3136 LocationSummary* locations = instruction->GetLocations();
3137 Location second = locations->InAt(1);
3138 DCHECK(second.IsConstant());
3139
3140 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3141 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3142 int64_t imm = Int64FromConstant(second.GetConstant());
3143 DCHECK(imm == 1 || imm == -1);
3144
3145 if (instruction->IsRem()) {
3146 __ Move(out, ZERO);
3147 } else {
3148 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003150 __ Subu(out, ZERO, dividend);
3151 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003152 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003153 __ Dsubu(out, ZERO, dividend);
3154 }
3155 } else if (out != dividend) {
3156 __ Move(out, dividend);
3157 }
3158 }
3159}
3160
3161void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3162 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003164
3165 LocationSummary* locations = instruction->GetLocations();
3166 Location second = locations->InAt(1);
3167 DCHECK(second.IsConstant());
3168
3169 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3170 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3171 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003172 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003173 int ctz_imm = CTZ(abs_imm);
3174
3175 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003176 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003177 if (ctz_imm == 1) {
3178 // Fast path for division by +/-2, which is very common.
3179 __ Srl(TMP, dividend, 31);
3180 } else {
3181 __ Sra(TMP, dividend, 31);
3182 __ Srl(TMP, TMP, 32 - ctz_imm);
3183 }
3184 __ Addu(out, dividend, TMP);
3185 __ Sra(out, out, ctz_imm);
3186 if (imm < 0) {
3187 __ Subu(out, ZERO, out);
3188 }
3189 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003190 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003191 if (ctz_imm == 1) {
3192 // Fast path for division by +/-2, which is very common.
3193 __ Dsrl32(TMP, dividend, 31);
3194 } else {
3195 __ Dsra32(TMP, dividend, 31);
3196 if (ctz_imm > 32) {
3197 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3198 } else {
3199 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3200 }
3201 }
3202 __ Daddu(out, dividend, TMP);
3203 if (ctz_imm < 32) {
3204 __ Dsra(out, out, ctz_imm);
3205 } else {
3206 __ Dsra32(out, out, ctz_imm - 32);
3207 }
3208 if (imm < 0) {
3209 __ Dsubu(out, ZERO, out);
3210 }
3211 }
3212 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003213 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003214 if (ctz_imm == 1) {
3215 // Fast path for modulo +/-2, which is very common.
3216 __ Sra(TMP, dividend, 31);
3217 __ Subu(out, dividend, TMP);
3218 __ Andi(out, out, 1);
3219 __ Addu(out, out, TMP);
3220 } else {
3221 __ Sra(TMP, dividend, 31);
3222 __ Srl(TMP, TMP, 32 - ctz_imm);
3223 __ Addu(out, dividend, TMP);
3224 if (IsUint<16>(abs_imm - 1)) {
3225 __ Andi(out, out, abs_imm - 1);
3226 } else {
3227 __ Sll(out, out, 32 - ctz_imm);
3228 __ Srl(out, out, 32 - ctz_imm);
3229 }
3230 __ Subu(out, out, TMP);
3231 }
3232 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003234 if (ctz_imm == 1) {
3235 // Fast path for modulo +/-2, which is very common.
3236 __ Dsra32(TMP, dividend, 31);
3237 __ Dsubu(out, dividend, TMP);
3238 __ Andi(out, out, 1);
3239 __ Daddu(out, out, TMP);
3240 } else {
3241 __ Dsra32(TMP, dividend, 31);
3242 if (ctz_imm > 32) {
3243 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3244 } else {
3245 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3246 }
3247 __ Daddu(out, dividend, TMP);
3248 if (IsUint<16>(abs_imm - 1)) {
3249 __ Andi(out, out, abs_imm - 1);
3250 } else {
3251 if (ctz_imm > 32) {
3252 __ Dsll(out, out, 64 - ctz_imm);
3253 __ Dsrl(out, out, 64 - ctz_imm);
3254 } else {
3255 __ Dsll32(out, out, 32 - ctz_imm);
3256 __ Dsrl32(out, out, 32 - ctz_imm);
3257 }
3258 }
3259 __ Dsubu(out, out, TMP);
3260 }
3261 }
3262 }
3263}
3264
3265void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3266 DCHECK(instruction->IsDiv() || instruction->IsRem());
3267
3268 LocationSummary* locations = instruction->GetLocations();
3269 Location second = locations->InAt(1);
3270 DCHECK(second.IsConstant());
3271
3272 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3273 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3274 int64_t imm = Int64FromConstant(second.GetConstant());
3275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003276 DataType::Type type = instruction->GetResultType();
3277 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003278
3279 int64_t magic;
3280 int shift;
3281 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003283 &magic,
3284 &shift);
3285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003286 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003287 __ LoadConst32(TMP, magic);
3288 __ MuhR6(TMP, dividend, TMP);
3289
3290 if (imm > 0 && magic < 0) {
3291 __ Addu(TMP, TMP, dividend);
3292 } else if (imm < 0 && magic > 0) {
3293 __ Subu(TMP, TMP, dividend);
3294 }
3295
3296 if (shift != 0) {
3297 __ Sra(TMP, TMP, shift);
3298 }
3299
3300 if (instruction->IsDiv()) {
3301 __ Sra(out, TMP, 31);
3302 __ Subu(out, TMP, out);
3303 } else {
3304 __ Sra(AT, TMP, 31);
3305 __ Subu(AT, TMP, AT);
3306 __ LoadConst32(TMP, imm);
3307 __ MulR6(TMP, AT, TMP);
3308 __ Subu(out, dividend, TMP);
3309 }
3310 } else {
3311 __ LoadConst64(TMP, magic);
3312 __ Dmuh(TMP, dividend, TMP);
3313
3314 if (imm > 0 && magic < 0) {
3315 __ Daddu(TMP, TMP, dividend);
3316 } else if (imm < 0 && magic > 0) {
3317 __ Dsubu(TMP, TMP, dividend);
3318 }
3319
3320 if (shift >= 32) {
3321 __ Dsra32(TMP, TMP, shift - 32);
3322 } else if (shift > 0) {
3323 __ Dsra(TMP, TMP, shift);
3324 }
3325
3326 if (instruction->IsDiv()) {
3327 __ Dsra32(out, TMP, 31);
3328 __ Dsubu(out, TMP, out);
3329 } else {
3330 __ Dsra32(AT, TMP, 31);
3331 __ Dsubu(AT, TMP, AT);
3332 __ LoadConst64(TMP, imm);
3333 __ Dmul(TMP, AT, TMP);
3334 __ Dsubu(out, dividend, TMP);
3335 }
3336 }
3337}
3338
3339void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3340 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003341 DataType::Type type = instruction->GetResultType();
3342 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003343
3344 LocationSummary* locations = instruction->GetLocations();
3345 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3346 Location second = locations->InAt(1);
3347
3348 if (second.IsConstant()) {
3349 int64_t imm = Int64FromConstant(second.GetConstant());
3350 if (imm == 0) {
3351 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3352 } else if (imm == 1 || imm == -1) {
3353 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003354 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003355 DivRemByPowerOfTwo(instruction);
3356 } else {
3357 DCHECK(imm <= -2 || imm >= 2);
3358 GenerateDivRemWithAnyConstant(instruction);
3359 }
3360 } else {
3361 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3362 GpuRegister divisor = second.AsRegister<GpuRegister>();
3363 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003364 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003365 __ DivR6(out, dividend, divisor);
3366 else
3367 __ Ddiv(out, dividend, divisor);
3368 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003369 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003370 __ ModR6(out, dividend, divisor);
3371 else
3372 __ Dmod(out, dividend, divisor);
3373 }
3374 }
3375}
3376
Alexey Frunze4dda3372015-06-01 18:31:49 -07003377void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3378 LocationSummary* locations =
3379 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3380 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 case DataType::Type::kInt32:
3382 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003383 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003384 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3386 break;
3387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003388 case DataType::Type::kFloat32:
3389 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003390 locations->SetInAt(0, Location::RequiresFpuRegister());
3391 locations->SetInAt(1, Location::RequiresFpuRegister());
3392 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3393 break;
3394
3395 default:
3396 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3397 }
3398}
3399
3400void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003401 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003402 LocationSummary* locations = instruction->GetLocations();
3403
3404 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003405 case DataType::Type::kInt32:
3406 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003407 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003408 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003409 case DataType::Type::kFloat32:
3410 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003411 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3412 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3413 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003414 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003415 __ DivS(dst, lhs, rhs);
3416 else
3417 __ DivD(dst, lhs, rhs);
3418 break;
3419 }
3420 default:
3421 LOG(FATAL) << "Unexpected div type " << type;
3422 }
3423}
3424
3425void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003426 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003427 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003428}
3429
3430void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3431 SlowPathCodeMIPS64* slow_path =
3432 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3433 codegen_->AddSlowPath(slow_path);
3434 Location value = instruction->GetLocations()->InAt(0);
3435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003436 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003437
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003438 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003439 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003440 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003441 }
3442
3443 if (value.IsConstant()) {
3444 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3445 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003446 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003447 } else {
3448 // A division by a non-null constant is valid. We don't need to perform
3449 // any check, so simply fall through.
3450 }
3451 } else {
3452 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3453 }
3454}
3455
3456void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3457 LocationSummary* locations =
3458 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3459 locations->SetOut(Location::ConstantLocation(constant));
3460}
3461
3462void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3463 // Will be generated at use site.
3464}
3465
3466void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3467 exit->SetLocations(nullptr);
3468}
3469
3470void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3471}
3472
3473void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3474 LocationSummary* locations =
3475 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3476 locations->SetOut(Location::ConstantLocation(constant));
3477}
3478
3479void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3480 // Will be generated at use site.
3481}
3482
David Brazdilfc6a86a2015-06-26 10:33:45 +00003483void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003484 DCHECK(!successor->IsExitBlock());
3485 HBasicBlock* block = got->GetBlock();
3486 HInstruction* previous = got->GetPrevious();
3487 HLoopInformation* info = block->GetLoopInformation();
3488
3489 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3490 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3491 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) {
4258 LocationSummary* locations = new (GetGraph()->GetArena()) 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) {
4275 LocationSummary* locations = new (GetGraph()->GetArena())
4276 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) {
4597 LocationSummary* locations = new (GetGraph()->GetArena())
4598 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) {
4610 LocationSummary* locations = new (GetGraph()->GetArena()) 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) {
4630 new (GetGraph()->GetArena()) LocationSummary(info);
4631}
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);
Alexey Frunze15958152017-02-09 19:08:30 -08004646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4647 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 =
4764 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4765 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 =
5053 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
5054 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);
5338 slow_path = new (GetGraph()->GetArena())
5339 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5340 ref,
5341 obj,
5342 /* field_offset */ index,
5343 temp_reg);
5344 } else {
5345 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
5346 }
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.
5381 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
5382 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 =
5417 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
5418 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
Alexey Frunze4dda3372015-06-01 18:31:49 -07005444 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005445 if (baker_read_barrier_slow_path) {
5446 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5447 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005448 locations->SetInAt(0, Location::RequiresRegister());
5449 locations->SetInAt(1, Location::RequiresRegister());
5450 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005451 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005452 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005453 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005454}
5455
5456void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005457 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005458 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005459 Location obj_loc = locations->InAt(0);
5460 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005461 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005462 Location out_loc = locations->Out();
5463 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5464 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5465 DCHECK_LE(num_temps, 1u);
5466 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005467 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5468 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5469 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5470 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005471 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005472 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005473
5474 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005475 // Avoid this check if we know `obj` is not null.
5476 if (instruction->MustDoNullCheck()) {
5477 __ Move(out, ZERO);
5478 __ Beqzc(obj, &done);
5479 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005480
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005481 switch (type_check_kind) {
5482 case TypeCheckKind::kExactCheck: {
5483 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005484 GenerateReferenceLoadTwoRegisters(instruction,
5485 out_loc,
5486 obj_loc,
5487 class_offset,
5488 maybe_temp_loc,
5489 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005490 // Classes must be equal for the instanceof to succeed.
5491 __ Xor(out, out, cls);
5492 __ Sltiu(out, out, 1);
5493 break;
5494 }
5495
5496 case TypeCheckKind::kAbstractClassCheck: {
5497 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005498 GenerateReferenceLoadTwoRegisters(instruction,
5499 out_loc,
5500 obj_loc,
5501 class_offset,
5502 maybe_temp_loc,
5503 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005504 // If the class is abstract, we eagerly fetch the super class of the
5505 // object to avoid doing a comparison we know will fail.
5506 Mips64Label loop;
5507 __ Bind(&loop);
5508 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005509 GenerateReferenceLoadOneRegister(instruction,
5510 out_loc,
5511 super_offset,
5512 maybe_temp_loc,
5513 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005514 // If `out` is null, we use it for the result, and jump to `done`.
5515 __ Beqzc(out, &done);
5516 __ Bnec(out, cls, &loop);
5517 __ LoadConst32(out, 1);
5518 break;
5519 }
5520
5521 case TypeCheckKind::kClassHierarchyCheck: {
5522 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005523 GenerateReferenceLoadTwoRegisters(instruction,
5524 out_loc,
5525 obj_loc,
5526 class_offset,
5527 maybe_temp_loc,
5528 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005529 // Walk over the class hierarchy to find a match.
5530 Mips64Label loop, success;
5531 __ Bind(&loop);
5532 __ Beqc(out, cls, &success);
5533 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005534 GenerateReferenceLoadOneRegister(instruction,
5535 out_loc,
5536 super_offset,
5537 maybe_temp_loc,
5538 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005539 __ Bnezc(out, &loop);
5540 // If `out` is null, we use it for the result, and jump to `done`.
5541 __ Bc(&done);
5542 __ Bind(&success);
5543 __ LoadConst32(out, 1);
5544 break;
5545 }
5546
5547 case TypeCheckKind::kArrayObjectCheck: {
5548 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005549 GenerateReferenceLoadTwoRegisters(instruction,
5550 out_loc,
5551 obj_loc,
5552 class_offset,
5553 maybe_temp_loc,
5554 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005555 // Do an exact check.
5556 Mips64Label success;
5557 __ Beqc(out, cls, &success);
5558 // Otherwise, we need to check that the object's class is a non-primitive array.
5559 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005560 GenerateReferenceLoadOneRegister(instruction,
5561 out_loc,
5562 component_offset,
5563 maybe_temp_loc,
5564 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005565 // If `out` is null, we use it for the result, and jump to `done`.
5566 __ Beqzc(out, &done);
5567 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5568 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5569 __ Sltiu(out, out, 1);
5570 __ Bc(&done);
5571 __ Bind(&success);
5572 __ LoadConst32(out, 1);
5573 break;
5574 }
5575
5576 case TypeCheckKind::kArrayCheck: {
5577 // No read barrier since the slow path will retry upon failure.
5578 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005579 GenerateReferenceLoadTwoRegisters(instruction,
5580 out_loc,
5581 obj_loc,
5582 class_offset,
5583 maybe_temp_loc,
5584 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005585 DCHECK(locations->OnlyCallsOnSlowPath());
5586 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5587 /* is_fatal */ false);
5588 codegen_->AddSlowPath(slow_path);
5589 __ Bnec(out, cls, slow_path->GetEntryLabel());
5590 __ LoadConst32(out, 1);
5591 break;
5592 }
5593
5594 case TypeCheckKind::kUnresolvedCheck:
5595 case TypeCheckKind::kInterfaceCheck: {
5596 // Note that we indeed only call on slow path, but we always go
5597 // into the slow path for the unresolved and interface check
5598 // cases.
5599 //
5600 // We cannot directly call the InstanceofNonTrivial runtime
5601 // entry point without resorting to a type checking slow path
5602 // here (i.e. by calling InvokeRuntime directly), as it would
5603 // require to assign fixed registers for the inputs of this
5604 // HInstanceOf instruction (following the runtime calling
5605 // convention), which might be cluttered by the potential first
5606 // read barrier emission at the beginning of this method.
5607 //
5608 // TODO: Introduce a new runtime entry point taking the object
5609 // to test (instead of its class) as argument, and let it deal
5610 // with the read barrier issues. This will let us refactor this
5611 // case of the `switch` code as it was previously (with a direct
5612 // call to the runtime not using a type checking slow path).
5613 // This should also be beneficial for the other cases above.
5614 DCHECK(locations->OnlyCallsOnSlowPath());
5615 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5616 /* is_fatal */ false);
5617 codegen_->AddSlowPath(slow_path);
5618 __ Bc(slow_path->GetEntryLabel());
5619 break;
5620 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005621 }
5622
5623 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005624
5625 if (slow_path != nullptr) {
5626 __ Bind(slow_path->GetExitLabel());
5627 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005628}
5629
5630void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
5631 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5632 locations->SetOut(Location::ConstantLocation(constant));
5633}
5634
5635void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5636 // Will be generated at use site.
5637}
5638
5639void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
5640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5641 locations->SetOut(Location::ConstantLocation(constant));
5642}
5643
5644void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5645 // Will be generated at use site.
5646}
5647
Calin Juravle175dc732015-08-25 15:42:32 +01005648void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5649 // The trampoline uses the same calling convention as dex calling conventions,
5650 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5651 // the method_idx.
5652 HandleInvoke(invoke);
5653}
5654
5655void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5656 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5657}
5658
Alexey Frunze4dda3372015-06-01 18:31:49 -07005659void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5660 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5661 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5662}
5663
5664void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5665 HandleInvoke(invoke);
5666 // The register T0 is required to be used for the hidden argument in
5667 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5668 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5669}
5670
5671void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5672 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5673 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005674 Location receiver = invoke->GetLocations()->InAt(0);
5675 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005676 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005677
5678 // Set the hidden argument.
5679 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5680 invoke->GetDexMethodIndex());
5681
5682 // temp = object->GetClass();
5683 if (receiver.IsStackSlot()) {
5684 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5685 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5686 } else {
5687 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5688 }
5689 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005690 // Instead of simply (possibly) unpoisoning `temp` here, we should
5691 // emit a read barrier for the previous class reference load.
5692 // However this is not required in practice, as this is an
5693 // intermediate/temporary reference and because the current
5694 // concurrent copying collector keeps the from-space memory
5695 // intact/accessible until the end of the marking phase (the
5696 // concurrent copying collector may not in the future).
5697 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005698 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5699 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5700 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005701 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005702 // temp = temp->GetImtEntryAt(method_offset);
5703 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5704 // T9 = temp->GetEntryPoint();
5705 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5706 // T9();
5707 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005708 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005709 DCHECK(!codegen_->IsLeafMethod());
5710 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5711}
5712
5713void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005714 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5715 if (intrinsic.TryDispatch(invoke)) {
5716 return;
5717 }
5718
Alexey Frunze4dda3372015-06-01 18:31:49 -07005719 HandleInvoke(invoke);
5720}
5721
5722void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005723 // Explicit clinit checks triggered by static invokes must have been pruned by
5724 // art::PrepareForRegisterAllocation.
5725 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005726
Chris Larsen3039e382015-08-26 07:54:08 -07005727 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5728 if (intrinsic.TryDispatch(invoke)) {
5729 return;
5730 }
5731
Alexey Frunze4dda3372015-06-01 18:31:49 -07005732 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005733}
5734
Orion Hodsonac141392017-01-13 11:53:47 +00005735void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5736 HandleInvoke(invoke);
5737}
5738
5739void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5740 codegen_->GenerateInvokePolymorphicCall(invoke);
5741}
5742
Chris Larsen3039e382015-08-26 07:54:08 -07005743static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005744 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005745 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5746 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005747 return true;
5748 }
5749 return false;
5750}
5751
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005752HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005753 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005754 bool fallback_load = false;
5755 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005756 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005757 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005758 case HLoadString::LoadKind::kBssEntry:
5759 DCHECK(!Runtime::Current()->UseJitCompilation());
5760 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005761 case HLoadString::LoadKind::kJitTableAddress:
5762 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005763 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005764 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005765 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005766 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005767 }
5768 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005769 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005770 }
5771 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005772}
5773
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005774HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5775 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005776 bool fallback_load = false;
5777 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005778 case HLoadClass::LoadKind::kInvalid:
5779 LOG(FATAL) << "UNREACHABLE";
5780 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005781 case HLoadClass::LoadKind::kReferrersClass:
5782 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005783 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005784 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005785 case HLoadClass::LoadKind::kBssEntry:
5786 DCHECK(!Runtime::Current()->UseJitCompilation());
5787 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005788 case HLoadClass::LoadKind::kJitTableAddress:
5789 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005790 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005791 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005792 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005793 break;
5794 }
5795 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005796 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005797 }
5798 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005799}
5800
Vladimir Markodc151b22015-10-15 18:02:30 +01005801HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5802 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005803 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005804 // On MIPS64 we support all dispatch types.
5805 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005806}
5807
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005808void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5809 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005810 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005811 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005812 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5813 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5814
Alexey Frunze19f6c692016-11-30 19:19:55 -08005815 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005816 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005817 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005818 uint32_t offset =
5819 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005820 __ LoadFromOffset(kLoadDoubleword,
5821 temp.AsRegister<GpuRegister>(),
5822 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005823 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005824 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005825 }
Vladimir Marko58155012015-08-19 12:49:41 +00005826 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005827 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005828 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005829 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5830 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005831 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005832 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005833 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5834 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5835 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005836 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5837 break;
5838 }
Vladimir Marko58155012015-08-19 12:49:41 +00005839 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005840 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5841 kLoadDoubleword,
5842 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005843 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005844 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005845 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005846 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005847 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5848 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5849 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005850 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5851 break;
5852 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005853 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5854 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5855 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005856 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005857 }
5858
Alexey Frunze19f6c692016-11-30 19:19:55 -08005859 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005860 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005861 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005862 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005863 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5864 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5865 __ LoadFromOffset(kLoadDoubleword,
5866 T9,
5867 callee_method.AsRegister<GpuRegister>(),
5868 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005869 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005870 // T9()
5871 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005872 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005873 break;
5874 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005875 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5876
Alexey Frunze4dda3372015-06-01 18:31:49 -07005877 DCHECK(!IsLeafMethod());
5878}
5879
5880void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005881 // Explicit clinit checks triggered by static invokes must have been pruned by
5882 // art::PrepareForRegisterAllocation.
5883 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005884
5885 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5886 return;
5887 }
5888
5889 LocationSummary* locations = invoke->GetLocations();
5890 codegen_->GenerateStaticOrDirectCall(invoke,
5891 locations->HasTemps()
5892 ? locations->GetTemp(0)
5893 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005894}
5895
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005896void CodeGeneratorMIPS64::GenerateVirtualCall(
5897 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005898 // Use the calling convention instead of the location of the receiver, as
5899 // intrinsics may have put the receiver in a different register. In the intrinsics
5900 // slow path, the arguments have been moved to the right place, so here we are
5901 // guaranteed that the receiver is the first register of the calling convention.
5902 InvokeDexCallingConvention calling_convention;
5903 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5904
Alexey Frunze53afca12015-11-05 16:34:23 -08005905 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005906 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5907 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5908 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005909 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005910
5911 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005912 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005913 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005914 // Instead of simply (possibly) unpoisoning `temp` here, we should
5915 // emit a read barrier for the previous class reference load.
5916 // However this is not required in practice, as this is an
5917 // intermediate/temporary reference and because the current
5918 // concurrent copying collector keeps the from-space memory
5919 // intact/accessible until the end of the marking phase (the
5920 // concurrent copying collector may not in the future).
5921 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005922 // temp = temp->GetMethodAt(method_offset);
5923 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5924 // T9 = temp->GetEntryPoint();
5925 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5926 // T9();
5927 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005928 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005929 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005930}
5931
5932void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5933 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5934 return;
5935 }
5936
5937 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005938 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005939}
5940
5941void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005942 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005943 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005944 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005945 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5946 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005947 return;
5948 }
Vladimir Marko41559982017-01-06 14:04:23 +00005949 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005950
Alexey Frunze15958152017-02-09 19:08:30 -08005951 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5952 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005953 ? LocationSummary::kCallOnSlowPath
5954 : LocationSummary::kNoCall;
5955 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005956 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5957 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5958 }
Vladimir Marko41559982017-01-06 14:04:23 +00005959 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005960 locations->SetInAt(0, Location::RequiresRegister());
5961 }
5962 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005963 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5964 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5965 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005966 // Request a temp to hold the BSS entry location for the slow path.
5967 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005968 RegisterSet caller_saves = RegisterSet::Empty();
5969 InvokeRuntimeCallingConvention calling_convention;
5970 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5971 locations->SetCustomSlowPathCallerSaves(caller_saves);
5972 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005973 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005974 }
5975 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005976}
5977
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005978// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5979// move.
5980void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005981 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005982 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005983 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005984 return;
5985 }
Vladimir Marko41559982017-01-06 14:04:23 +00005986 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005987
Vladimir Marko41559982017-01-06 14:04:23 +00005988 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005989 Location out_loc = locations->Out();
5990 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5991 GpuRegister current_method_reg = ZERO;
5992 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005993 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005994 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5995 }
5996
Alexey Frunze15958152017-02-09 19:08:30 -08005997 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5998 ? kWithoutReadBarrier
5999 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006000 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006001 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006002 switch (load_kind) {
6003 case HLoadClass::LoadKind::kReferrersClass:
6004 DCHECK(!cls->CanCallRuntime());
6005 DCHECK(!cls->MustGenerateClinitCheck());
6006 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6007 GenerateGcRootFieldLoad(cls,
6008 out_loc,
6009 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006010 ArtMethod::DeclaringClassOffset().Int32Value(),
6011 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006012 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006013 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006014 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006015 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006016 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006017 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006018 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6019 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6020 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006021 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6022 break;
6023 }
6024 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006025 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006026 uint32_t address = dchecked_integral_cast<uint32_t>(
6027 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6028 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006029 __ LoadLiteral(out,
6030 kLoadUnsignedWord,
6031 codegen_->DeduplicateBootImageAddressLiteral(address));
6032 break;
6033 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006034 case HLoadClass::LoadKind::kBootImageClassTable: {
6035 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6036 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6037 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6038 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6039 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6040 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6041 __ Lwu(out, AT, /* placeholder */ 0x5678);
6042 // Extract the reference from the slot data, i.e. clear the hash bits.
6043 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6044 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6045 if (masked_hash != 0) {
6046 __ Daddiu(out, out, -masked_hash);
6047 }
6048 break;
6049 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006050 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006051 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
6052 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6053 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
6054 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6055 GpuRegister temp = non_baker_read_barrier
6056 ? out
6057 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006058 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
6059 GenerateGcRootFieldLoad(cls,
6060 out_loc,
6061 temp,
6062 /* placeholder */ 0x5678,
6063 read_barrier_option,
6064 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006065 generate_null_check = true;
6066 break;
6067 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006068 case HLoadClass::LoadKind::kJitTableAddress:
6069 __ LoadLiteral(out,
6070 kLoadUnsignedWord,
6071 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6072 cls->GetTypeIndex(),
6073 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006074 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006075 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006076 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006077 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006078 LOG(FATAL) << "UNREACHABLE";
6079 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006080 }
6081
6082 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6083 DCHECK(cls->CanCallRuntime());
6084 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006085 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006086 codegen_->AddSlowPath(slow_path);
6087 if (generate_null_check) {
6088 __ Beqzc(out, slow_path->GetEntryLabel());
6089 }
6090 if (cls->MustGenerateClinitCheck()) {
6091 GenerateClassInitializationCheck(slow_path, out);
6092 } else {
6093 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006094 }
6095 }
6096}
6097
David Brazdilcb1c0552015-08-04 16:22:25 +01006098static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006099 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006100}
6101
Alexey Frunze4dda3372015-06-01 18:31:49 -07006102void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6103 LocationSummary* locations =
6104 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6105 locations->SetOut(Location::RequiresRegister());
6106}
6107
6108void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6109 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006110 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6111}
6112
6113void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
6114 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6115}
6116
6117void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6118 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006119}
6120
Alexey Frunze4dda3372015-06-01 18:31:49 -07006121void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006122 HLoadString::LoadKind load_kind = load->GetLoadKind();
6123 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006124 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006125 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006126 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006127 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006128 } else {
6129 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006130 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6131 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6132 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006133 // Request a temp to hold the BSS entry location for the slow path.
6134 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006135 RegisterSet caller_saves = RegisterSet::Empty();
6136 InvokeRuntimeCallingConvention calling_convention;
6137 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6138 locations->SetCustomSlowPathCallerSaves(caller_saves);
6139 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006140 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006141 }
6142 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006143 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006144}
6145
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006146// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6147// move.
6148void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006149 HLoadString::LoadKind load_kind = load->GetLoadKind();
6150 LocationSummary* locations = load->GetLocations();
6151 Location out_loc = locations->Out();
6152 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6153
6154 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006155 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6156 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006157 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006158 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006159 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6160 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6161 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006162 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006163 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006164 }
6165 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006166 uint32_t address = dchecked_integral_cast<uint32_t>(
6167 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6168 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006169 __ LoadLiteral(out,
6170 kLoadUnsignedWord,
6171 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006172 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006173 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006174 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006175 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006176 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006177 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006178 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6179 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006180 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6181 __ Lwu(out, AT, /* placeholder */ 0x5678);
6182 return;
6183 }
6184 case HLoadString::LoadKind::kBssEntry: {
6185 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6186 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6187 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6188 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6189 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006190 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6191 GpuRegister temp = non_baker_read_barrier
6192 ? out
6193 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006194 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08006195 GenerateGcRootFieldLoad(load,
6196 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006197 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08006198 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006199 kCompilerReadBarrierOption,
6200 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006201 SlowPathCodeMIPS64* slow_path =
6202 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006203 codegen_->AddSlowPath(slow_path);
6204 __ Beqzc(out, slow_path->GetEntryLabel());
6205 __ Bind(slow_path->GetExitLabel());
6206 return;
6207 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006208 case HLoadString::LoadKind::kJitTableAddress:
6209 __ LoadLiteral(out,
6210 kLoadUnsignedWord,
6211 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6212 load->GetStringIndex(),
6213 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006214 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006215 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006216 default:
6217 break;
6218 }
6219
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006220 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006221 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006222 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006223 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006224 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6225 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6226 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006227}
6228
Alexey Frunze4dda3372015-06-01 18:31:49 -07006229void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
6230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6231 locations->SetOut(Location::ConstantLocation(constant));
6232}
6233
6234void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6235 // Will be generated at use site.
6236}
6237
6238void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
6239 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006240 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006241 InvokeRuntimeCallingConvention calling_convention;
6242 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6243}
6244
6245void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006246 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006247 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006248 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006249 if (instruction->IsEnter()) {
6250 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6251 } else {
6252 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6253 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006254}
6255
6256void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6257 LocationSummary* locations =
6258 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
6259 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 case DataType::Type::kInt32:
6261 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006262 locations->SetInAt(0, Location::RequiresRegister());
6263 locations->SetInAt(1, Location::RequiresRegister());
6264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6265 break;
6266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006267 case DataType::Type::kFloat32:
6268 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006269 locations->SetInAt(0, Location::RequiresFpuRegister());
6270 locations->SetInAt(1, Location::RequiresFpuRegister());
6271 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6272 break;
6273
6274 default:
6275 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6276 }
6277}
6278
6279void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006280 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006281 LocationSummary* locations = instruction->GetLocations();
6282
6283 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006284 case DataType::Type::kInt32:
6285 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006286 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6287 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6288 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006289 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006290 __ MulR6(dst, lhs, rhs);
6291 else
6292 __ Dmul(dst, lhs, rhs);
6293 break;
6294 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 case DataType::Type::kFloat32:
6296 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006297 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6298 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6299 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006300 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006301 __ MulS(dst, lhs, rhs);
6302 else
6303 __ MulD(dst, lhs, rhs);
6304 break;
6305 }
6306 default:
6307 LOG(FATAL) << "Unexpected mul type " << type;
6308 }
6309}
6310
6311void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6312 LocationSummary* locations =
6313 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
6314 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006315 case DataType::Type::kInt32:
6316 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006317 locations->SetInAt(0, Location::RequiresRegister());
6318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6319 break;
6320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 case DataType::Type::kFloat32:
6322 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006323 locations->SetInAt(0, Location::RequiresFpuRegister());
6324 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6325 break;
6326
6327 default:
6328 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6329 }
6330}
6331
6332void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006333 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006334 LocationSummary* locations = instruction->GetLocations();
6335
6336 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 case DataType::Type::kInt32:
6338 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006339 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6340 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006341 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006342 __ Subu(dst, ZERO, src);
6343 else
6344 __ Dsubu(dst, ZERO, src);
6345 break;
6346 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006347 case DataType::Type::kFloat32:
6348 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006349 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6350 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006351 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006352 __ NegS(dst, src);
6353 else
6354 __ NegD(dst, src);
6355 break;
6356 }
6357 default:
6358 LOG(FATAL) << "Unexpected neg type " << type;
6359 }
6360}
6361
6362void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
6363 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006364 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006365 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006367 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6368 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006369}
6370
6371void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006372 // Note: if heap poisoning is enabled, the entry point takes care
6373 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006374 QuickEntrypointEnum entrypoint =
6375 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6376 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006377 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006378 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006379}
6380
6381void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
6382 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006383 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006384 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006385 if (instruction->IsStringAlloc()) {
6386 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6387 } else {
6388 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006389 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006390 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006391}
6392
6393void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006394 // Note: if heap poisoning is enabled, the entry point takes care
6395 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006396 if (instruction->IsStringAlloc()) {
6397 // String is allocated through StringFactory. Call NewEmptyString entry point.
6398 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006399 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006400 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006401 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6402 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6403 __ Jalr(T9);
6404 __ Nop();
6405 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6406 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006407 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006408 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006409 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006410}
6411
6412void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
6413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6414 locations->SetInAt(0, Location::RequiresRegister());
6415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6416}
6417
6418void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006419 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006420 LocationSummary* locations = instruction->GetLocations();
6421
6422 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006423 case DataType::Type::kInt32:
6424 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006425 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6426 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6427 __ Nor(dst, src, ZERO);
6428 break;
6429 }
6430
6431 default:
6432 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6433 }
6434}
6435
6436void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6438 locations->SetInAt(0, Location::RequiresRegister());
6439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6440}
6441
6442void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6443 LocationSummary* locations = instruction->GetLocations();
6444 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6445 locations->InAt(0).AsRegister<GpuRegister>(),
6446 1);
6447}
6448
6449void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006450 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6451 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006452}
6453
Calin Juravle2ae48182016-03-16 14:05:09 +00006454void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6455 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006456 return;
6457 }
6458 Location obj = instruction->GetLocations()->InAt(0);
6459
6460 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006461 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006462}
6463
Calin Juravle2ae48182016-03-16 14:05:09 +00006464void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006465 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006466 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006467
6468 Location obj = instruction->GetLocations()->InAt(0);
6469
6470 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6471}
6472
6473void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006474 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006475}
6476
6477void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6478 HandleBinaryOp(instruction);
6479}
6480
6481void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6482 HandleBinaryOp(instruction);
6483}
6484
6485void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6486 LOG(FATAL) << "Unreachable";
6487}
6488
6489void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
6490 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6491}
6492
6493void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
6494 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6495 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6496 if (location.IsStackSlot()) {
6497 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6498 } else if (location.IsDoubleStackSlot()) {
6499 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6500 }
6501 locations->SetOut(location);
6502}
6503
6504void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6505 ATTRIBUTE_UNUSED) {
6506 // Nothing to do, the parameter is already at its location.
6507}
6508
6509void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6510 LocationSummary* locations =
6511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6512 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6513}
6514
6515void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6516 ATTRIBUTE_UNUSED) {
6517 // Nothing to do, the method is already at its location.
6518}
6519
6520void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
6521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006522 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006523 locations->SetInAt(i, Location::Any());
6524 }
6525 locations->SetOut(Location::Any());
6526}
6527
6528void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6529 LOG(FATAL) << "Unreachable";
6530}
6531
6532void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006533 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006534 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006535 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6536 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6538
6539 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006540 case DataType::Type::kInt32:
6541 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006542 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006543 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6545 break;
6546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006547 case DataType::Type::kFloat32:
6548 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006549 InvokeRuntimeCallingConvention calling_convention;
6550 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6551 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6552 locations->SetOut(calling_convention.GetReturnLocation(type));
6553 break;
6554 }
6555
6556 default:
6557 LOG(FATAL) << "Unexpected rem type " << type;
6558 }
6559}
6560
6561void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006562 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006563
6564 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006565 case DataType::Type::kInt32:
6566 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006567 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006568 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006570 case DataType::Type::kFloat32:
6571 case DataType::Type::kFloat64: {
6572 QuickEntrypointEnum entrypoint =
6573 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006574 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006575 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006576 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6577 } else {
6578 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6579 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006580 break;
6581 }
6582 default:
6583 LOG(FATAL) << "Unexpected rem type " << type;
6584 }
6585}
6586
Igor Murashkind01745e2017-04-05 16:40:31 -07006587void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6588 constructor_fence->SetLocations(nullptr);
6589}
6590
6591void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6592 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6593 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6594}
6595
Alexey Frunze4dda3372015-06-01 18:31:49 -07006596void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6597 memory_barrier->SetLocations(nullptr);
6598}
6599
6600void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6601 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6602}
6603
6604void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
6605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006606 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006607 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6608}
6609
6610void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6611 codegen_->GenerateFrameExit();
6612}
6613
6614void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6615 ret->SetLocations(nullptr);
6616}
6617
6618void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6619 codegen_->GenerateFrameExit();
6620}
6621
Alexey Frunze92d90602015-12-18 18:16:36 -08006622void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6623 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006624}
6625
Alexey Frunze92d90602015-12-18 18:16:36 -08006626void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6627 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006628}
6629
Alexey Frunze4dda3372015-06-01 18:31:49 -07006630void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6631 HandleShift(shl);
6632}
6633
6634void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6635 HandleShift(shl);
6636}
6637
6638void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6639 HandleShift(shr);
6640}
6641
6642void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6643 HandleShift(shr);
6644}
6645
Alexey Frunze4dda3372015-06-01 18:31:49 -07006646void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6647 HandleBinaryOp(instruction);
6648}
6649
6650void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6651 HandleBinaryOp(instruction);
6652}
6653
6654void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6655 HandleFieldGet(instruction, instruction->GetFieldInfo());
6656}
6657
6658void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6659 HandleFieldGet(instruction, instruction->GetFieldInfo());
6660}
6661
6662void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6663 HandleFieldSet(instruction, instruction->GetFieldInfo());
6664}
6665
6666void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006667 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006668}
6669
Calin Juravlee460d1d2015-09-29 04:52:17 +01006670void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6671 HUnresolvedInstanceFieldGet* instruction) {
6672 FieldAccessCallingConventionMIPS64 calling_convention;
6673 codegen_->CreateUnresolvedFieldLocationSummary(
6674 instruction, instruction->GetFieldType(), calling_convention);
6675}
6676
6677void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6678 HUnresolvedInstanceFieldGet* instruction) {
6679 FieldAccessCallingConventionMIPS64 calling_convention;
6680 codegen_->GenerateUnresolvedFieldAccess(instruction,
6681 instruction->GetFieldType(),
6682 instruction->GetFieldIndex(),
6683 instruction->GetDexPc(),
6684 calling_convention);
6685}
6686
6687void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6688 HUnresolvedInstanceFieldSet* instruction) {
6689 FieldAccessCallingConventionMIPS64 calling_convention;
6690 codegen_->CreateUnresolvedFieldLocationSummary(
6691 instruction, instruction->GetFieldType(), calling_convention);
6692}
6693
6694void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6695 HUnresolvedInstanceFieldSet* instruction) {
6696 FieldAccessCallingConventionMIPS64 calling_convention;
6697 codegen_->GenerateUnresolvedFieldAccess(instruction,
6698 instruction->GetFieldType(),
6699 instruction->GetFieldIndex(),
6700 instruction->GetDexPc(),
6701 calling_convention);
6702}
6703
6704void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6705 HUnresolvedStaticFieldGet* instruction) {
6706 FieldAccessCallingConventionMIPS64 calling_convention;
6707 codegen_->CreateUnresolvedFieldLocationSummary(
6708 instruction, instruction->GetFieldType(), calling_convention);
6709}
6710
6711void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6712 HUnresolvedStaticFieldGet* instruction) {
6713 FieldAccessCallingConventionMIPS64 calling_convention;
6714 codegen_->GenerateUnresolvedFieldAccess(instruction,
6715 instruction->GetFieldType(),
6716 instruction->GetFieldIndex(),
6717 instruction->GetDexPc(),
6718 calling_convention);
6719}
6720
6721void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6722 HUnresolvedStaticFieldSet* instruction) {
6723 FieldAccessCallingConventionMIPS64 calling_convention;
6724 codegen_->CreateUnresolvedFieldLocationSummary(
6725 instruction, instruction->GetFieldType(), calling_convention);
6726}
6727
6728void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6729 HUnresolvedStaticFieldSet* instruction) {
6730 FieldAccessCallingConventionMIPS64 calling_convention;
6731 codegen_->GenerateUnresolvedFieldAccess(instruction,
6732 instruction->GetFieldType(),
6733 instruction->GetFieldIndex(),
6734 instruction->GetDexPc(),
6735 calling_convention);
6736}
6737
Alexey Frunze4dda3372015-06-01 18:31:49 -07006738void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006739 LocationSummary* locations =
6740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006741 // In suspend check slow path, usually there are no caller-save registers at all.
6742 // If SIMD instructions are present, however, we force spilling all live SIMD
6743 // registers in full width (since the runtime only saves/restores lower part).
6744 locations->SetCustomSlowPathCallerSaves(
6745 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006746}
6747
6748void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6749 HBasicBlock* block = instruction->GetBlock();
6750 if (block->GetLoopInformation() != nullptr) {
6751 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6752 // The back edge will generate the suspend check.
6753 return;
6754 }
6755 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6756 // The goto will generate the suspend check.
6757 return;
6758 }
6759 GenerateSuspendCheck(instruction, nullptr);
6760}
6761
Alexey Frunze4dda3372015-06-01 18:31:49 -07006762void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
6763 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006764 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006765 InvokeRuntimeCallingConvention calling_convention;
6766 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6767}
6768
6769void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006770 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006771 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6772}
6773
6774void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006775 DataType::Type input_type = conversion->GetInputType();
6776 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006777 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6778 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006780 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6781 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006782 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6783 }
6784
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
6786
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006787 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006788 locations->SetInAt(0, Location::RequiresFpuRegister());
6789 } else {
6790 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006791 }
6792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006793 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006794 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006795 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006796 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006797 }
6798}
6799
6800void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6801 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006802 DataType::Type result_type = conversion->GetResultType();
6803 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006804
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006805 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6806 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006808 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006809 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6810 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6811
6812 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006813 case DataType::Type::kUint8:
6814 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006815 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006816 case DataType::Type::kInt8:
6817 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006818 // Type conversion from long to types narrower than int is a result of code
6819 // transformations. To avoid unpredictable results for SEB and SEH, we first
6820 // need to sign-extend the low 32-bit value into bits 32 through 63.
6821 __ Sll(dst, src, 0);
6822 __ Seb(dst, dst);
6823 } else {
6824 __ Seb(dst, src);
6825 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006826 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006827 case DataType::Type::kUint16:
6828 __ Andi(dst, src, 0xFFFF);
6829 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006830 case DataType::Type::kInt16:
6831 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006832 // Type conversion from long to types narrower than int is a result of code
6833 // transformations. To avoid unpredictable results for SEB and SEH, we first
6834 // need to sign-extend the low 32-bit value into bits 32 through 63.
6835 __ Sll(dst, src, 0);
6836 __ Seh(dst, dst);
6837 } else {
6838 __ Seh(dst, src);
6839 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006840 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006841 case DataType::Type::kInt32:
6842 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006843 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6844 // conversions, except when the input and output registers are the same and we are not
6845 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006846 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006847 __ Sll(dst, src, 0);
6848 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006849 break;
6850
6851 default:
6852 LOG(FATAL) << "Unexpected type conversion from " << input_type
6853 << " to " << result_type;
6854 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006855 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006856 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6857 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006858 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006859 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006860 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006861 __ Cvtsl(dst, FTMP);
6862 } else {
6863 __ Cvtdl(dst, FTMP);
6864 }
6865 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006866 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006867 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006868 __ Cvtsw(dst, FTMP);
6869 } else {
6870 __ Cvtdw(dst, FTMP);
6871 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006872 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006873 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6874 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006875 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6876 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006878 if (result_type == DataType::Type::kInt64) {
6879 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006880 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006881 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006882 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006883 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006884 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006885 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006886 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006887 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006888 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006889 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006890 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006891 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006892 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006893 } else if (DataType::IsFloatingPointType(result_type) &&
6894 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006895 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6896 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006897 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006898 __ Cvtsd(dst, src);
6899 } else {
6900 __ Cvtds(dst, src);
6901 }
6902 } else {
6903 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6904 << " to " << result_type;
6905 }
6906}
6907
6908void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6909 HandleShift(ushr);
6910}
6911
6912void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6913 HandleShift(ushr);
6914}
6915
6916void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6917 HandleBinaryOp(instruction);
6918}
6919
6920void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6921 HandleBinaryOp(instruction);
6922}
6923
6924void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6925 // Nothing to do, this should be removed during prepare for register allocator.
6926 LOG(FATAL) << "Unreachable";
6927}
6928
6929void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6930 // Nothing to do, this should be removed during prepare for register allocator.
6931 LOG(FATAL) << "Unreachable";
6932}
6933
6934void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006935 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006936}
6937
6938void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006939 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006940}
6941
6942void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006943 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006944}
6945
6946void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006947 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006948}
6949
6950void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006951 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006952}
6953
6954void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006955 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006956}
6957
6958void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006959 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006960}
6961
6962void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006963 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006964}
6965
6966void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006967 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006968}
6969
6970void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006971 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006972}
6973
6974void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006975 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006976}
6977
6978void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006979 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006980}
6981
Aart Bike9f37602015-10-09 11:15:55 -07006982void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006983 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006984}
6985
6986void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006987 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006988}
6989
6990void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006991 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006992}
6993
6994void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006995 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006996}
6997
6998void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006999 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007000}
7001
7002void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007003 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007004}
7005
7006void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007007 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007008}
7009
7010void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007012}
7013
Mark Mendellfe57faa2015-09-18 09:26:15 -04007014// Simple implementation of packed switch - generate cascaded compare/jumps.
7015void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7016 LocationSummary* locations =
7017 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7018 locations->SetInAt(0, Location::RequiresRegister());
7019}
7020
Alexey Frunze0960ac52016-12-20 17:24:59 -08007021void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7022 int32_t lower_bound,
7023 uint32_t num_entries,
7024 HBasicBlock* switch_block,
7025 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007026 // Create a set of compare/jumps.
7027 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007028 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007029 // Jump to default if index is negative
7030 // Note: We don't check the case that index is positive while value < lower_bound, because in
7031 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7032 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7033
Alexey Frunze0960ac52016-12-20 17:24:59 -08007034 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007035 // Jump to successors[0] if value == lower_bound.
7036 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7037 int32_t last_index = 0;
7038 for (; num_entries - last_index > 2; last_index += 2) {
7039 __ Addiu(temp_reg, temp_reg, -2);
7040 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7041 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7042 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7043 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7044 }
7045 if (num_entries - last_index == 2) {
7046 // The last missing case_value.
7047 __ Addiu(temp_reg, temp_reg, -1);
7048 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007049 }
7050
7051 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007052 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007053 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007054 }
7055}
7056
Alexey Frunze0960ac52016-12-20 17:24:59 -08007057void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7058 int32_t lower_bound,
7059 uint32_t num_entries,
7060 HBasicBlock* switch_block,
7061 HBasicBlock* default_block) {
7062 // Create a jump table.
7063 std::vector<Mips64Label*> labels(num_entries);
7064 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7065 for (uint32_t i = 0; i < num_entries; i++) {
7066 labels[i] = codegen_->GetLabelOf(successors[i]);
7067 }
7068 JumpTable* table = __ CreateJumpTable(std::move(labels));
7069
7070 // Is the value in range?
7071 __ Addiu32(TMP, value_reg, -lower_bound);
7072 __ LoadConst32(AT, num_entries);
7073 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7074
7075 // We are in the range of the table.
7076 // Load the target address from the jump table, indexing by the value.
7077 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007078 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007079 __ Lw(TMP, TMP, 0);
7080 // Compute the absolute target address by adding the table start address
7081 // (the table contains offsets to targets relative to its start).
7082 __ Daddu(TMP, TMP, AT);
7083 // And jump.
7084 __ Jr(TMP);
7085 __ Nop();
7086}
7087
7088void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7089 int32_t lower_bound = switch_instr->GetStartValue();
7090 uint32_t num_entries = switch_instr->GetNumEntries();
7091 LocationSummary* locations = switch_instr->GetLocations();
7092 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7093 HBasicBlock* switch_block = switch_instr->GetBlock();
7094 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7095
7096 if (num_entries > kPackedSwitchJumpTableThreshold) {
7097 GenTableBasedPackedSwitch(value_reg,
7098 lower_bound,
7099 num_entries,
7100 switch_block,
7101 default_block);
7102 } else {
7103 GenPackedSwitchWithCompares(value_reg,
7104 lower_bound,
7105 num_entries,
7106 switch_block,
7107 default_block);
7108 }
7109}
7110
Chris Larsenc9905a62017-03-13 17:06:18 -07007111void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7112 LocationSummary* locations =
7113 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7114 locations->SetInAt(0, Location::RequiresRegister());
7115 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007116}
7117
Chris Larsenc9905a62017-03-13 17:06:18 -07007118void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7119 LocationSummary* locations = instruction->GetLocations();
7120 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7121 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7122 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7123 __ LoadFromOffset(kLoadDoubleword,
7124 locations->Out().AsRegister<GpuRegister>(),
7125 locations->InAt(0).AsRegister<GpuRegister>(),
7126 method_offset);
7127 } else {
7128 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7129 instruction->GetIndex(), kMips64PointerSize));
7130 __ LoadFromOffset(kLoadDoubleword,
7131 locations->Out().AsRegister<GpuRegister>(),
7132 locations->InAt(0).AsRegister<GpuRegister>(),
7133 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7134 __ LoadFromOffset(kLoadDoubleword,
7135 locations->Out().AsRegister<GpuRegister>(),
7136 locations->Out().AsRegister<GpuRegister>(),
7137 method_offset);
7138 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007139}
7140
Alexey Frunze4dda3372015-06-01 18:31:49 -07007141} // namespace mips64
7142} // namespace art