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