blob: 557a1ec67d7c47e5f808d9eb51b509db79f4e1b8 [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"
Vladimir Marko174b2e22017-10-12 13:34:49 +010034#include "stack_map_stream.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070035#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070037#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070038#include "utils/stack_checks.h"
39
40namespace art {
41namespace mips64 {
42
43static constexpr int kCurrentMethodStackOffset = 0;
44static constexpr GpuRegister kMethodRegisterArgument = A0;
45
Alexey Frunze4147fcc2017-06-17 19:57:27 -070046// Flags controlling the use of thunks for Baker read barriers.
47constexpr bool kBakerReadBarrierThunksEnableForFields = true;
48constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
49constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
50
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051Location Mips64ReturnLocation(DataType::Type return_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070052 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010054 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 case DataType::Type::kInt8:
56 case DataType::Type::kUint16:
57 case DataType::Type::kInt16:
58 case DataType::Type::kInt32:
59 case DataType::Type::kReference:
60 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070061 return Location::RegisterLocation(V0);
62
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kFloat32:
64 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070065 return Location::FpuRegisterLocation(F0);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070068 return Location();
69 }
70 UNREACHABLE();
71}
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070074 return Mips64ReturnLocation(type);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
78 return Location::RegisterLocation(kMethodRegisterArgument);
79}
80
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070082 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070084 LOG(FATAL) << "Unexpected parameter type " << type;
85 }
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070088 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
89 next_location = Location::FpuRegisterLocation(
90 calling_convention.GetFpuRegisterAt(float_index_++));
91 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070093 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
94 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
95 float_index_++;
96 } else {
97 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010098 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
99 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700100 }
101
102 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
Alexey Frunze4dda3372015-06-01 18:31:49 -0700105 return next_location;
106}
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700109 return Mips64ReturnLocation(type);
110}
111
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100112// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
113#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700114#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115
116class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
117 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700119
120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100121 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
123 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000124 if (instruction_->CanThrowIntoCatchBlock()) {
125 // Live registers will be restored in the catch block if caught.
126 SaveLiveRegisters(codegen, instruction_->GetLocations());
127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 // We're moving two locations to locations that could overlap, so we need a parallel
129 // move resolver.
130 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100137 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
138 ? kQuickThrowStringBounds
139 : kQuickThrowArrayBounds;
140 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100141 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
143 }
144
Alexandre Rames8158f282015-08-07 10:26:17 +0100145 bool IsFatal() const OVERRIDE { return true; }
146
Roland Levillain46648892015-06-19 16:07:18 +0100147 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
148
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
151};
152
153class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
154 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700155 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
156 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
160 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100161 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
163 }
164
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 bool IsFatal() const OVERRIDE { return true; }
166
Roland Levillain46648892015-06-19 16:07:18 +0100167 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
168
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700170 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
171};
172
173class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
174 public:
175 LoadClassSlowPathMIPS64(HLoadClass* cls,
176 HInstruction* at,
177 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700178 bool do_clinit,
179 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr)
180 : SlowPathCodeMIPS64(at),
181 cls_(cls),
182 dex_pc_(dex_pc),
183 do_clinit_(do_clinit),
184 bss_info_high_(bss_info_high) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700185 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
186 }
187
188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000189 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700190 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700191 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700192 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
193 InvokeRuntimeCallingConvention calling_convention;
194 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
195 const bool is_load_class_bss_entry =
196 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700197 __ Bind(GetEntryLabel());
198 SaveLiveRegisters(codegen, locations);
199
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700200 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
201 GpuRegister entry_address = kNoGpuRegister;
202 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
203 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
204 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
205 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
206 // kSaveEverything call.
207 entry_address = temp_is_a0 ? out.AsRegister<GpuRegister>() : temp;
208 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
209 if (temp_is_a0) {
210 __ Move(entry_address, temp);
211 }
212 }
213
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000214 dex::TypeIndex type_index = cls_->GetTypeIndex();
215 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100216 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
217 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000218 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700219 if (do_clinit_) {
220 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
221 } else {
222 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
223 }
224
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700225 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
226 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
227 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
228 DCHECK(bss_info_high_);
229 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
230 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
231 __ Bind(&info_low->label);
232 __ StoreToOffset(kStoreWord,
233 calling_convention.GetRegisterAt(0),
234 entry_address,
235 /* placeholder */ 0x5678);
236 }
237
Alexey Frunze4dda3372015-06-01 18:31:49 -0700238 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700239 if (out.IsValid()) {
240 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100241 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700242 mips64_codegen->MoveLocation(out,
243 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
244 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700246 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700247
248 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
249 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
250 // For non-Baker read barriers we need to re-calculate the address of
251 // the class entry.
252 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000253 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700254 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
255 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
256 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
257 __ StoreToOffset(kStoreWord, out.AsRegister<GpuRegister>(), TMP, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000258 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700259 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 }
261
Roland Levillain46648892015-06-19 16:07:18 +0100262 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
263
Alexey Frunze4dda3372015-06-01 18:31:49 -0700264 private:
265 // The class this slow path will load.
266 HLoadClass* const cls_;
267
Alexey Frunze4dda3372015-06-01 18:31:49 -0700268 // The dex PC of `at_`.
269 const uint32_t dex_pc_;
270
271 // Whether to initialize the class.
272 const bool do_clinit_;
273
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700274 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
275 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
276
Alexey Frunze4dda3372015-06-01 18:31:49 -0700277 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
278};
279
280class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
281 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700282 explicit LoadStringSlowPathMIPS64(HLoadString* instruction,
283 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high)
284 : SlowPathCodeMIPS64(instruction), bss_info_high_(bss_info_high) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700287 DCHECK(instruction_->IsLoadString());
288 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700289 LocationSummary* locations = instruction_->GetLocations();
290 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700291 HLoadString* load = instruction_->AsLoadString();
292 const dex::StringIndex string_index = load->GetStringIndex();
293 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700295 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
296 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700297 __ Bind(GetEntryLabel());
298 SaveLiveRegisters(codegen, locations);
299
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700300 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
301 GpuRegister entry_address = kNoGpuRegister;
302 if (baker_or_no_read_barriers) {
303 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
304 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
305 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
306 // kSaveEverything call.
307 entry_address = temp_is_a0 ? out : temp;
308 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
309 if (temp_is_a0) {
310 __ Move(entry_address, temp);
311 }
312 }
313
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000314 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100315 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 instruction_,
317 instruction_->GetDexPc(),
318 this);
319 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700320
321 // Store the resolved string to the BSS entry.
322 if (baker_or_no_read_barriers) {
323 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
324 DCHECK(bss_info_high_);
325 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100326 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(),
327 string_index,
328 bss_info_high_);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700329 __ Bind(&info_low->label);
330 __ StoreToOffset(kStoreWord,
331 calling_convention.GetRegisterAt(0),
332 entry_address,
333 /* placeholder */ 0x5678);
334 }
335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100336 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700337 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700339 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800341
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700342 // Store the resolved string to the BSS entry.
343 if (!baker_or_no_read_barriers) {
344 // For non-Baker read barriers we need to re-calculate the address of
345 // the string entry.
346 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100347 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700348 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100349 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700350 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
351 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
352 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700353 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354 }
355
Roland Levillain46648892015-06-19 16:07:18 +0100356 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
357
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700359 // Pointer to the high half PC-relative patch info.
360 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
361
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
363};
364
365class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
366 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000367 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368
369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
370 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
371 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000372 if (instruction_->CanThrowIntoCatchBlock()) {
373 // Live registers will be restored in the catch block if caught.
374 SaveLiveRegisters(codegen, instruction_->GetLocations());
375 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100376 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 instruction_,
378 instruction_->GetDexPc(),
379 this);
380 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
381 }
382
Alexandre Rames8158f282015-08-07 10:26:17 +0100383 bool IsFatal() const OVERRIDE { return true; }
384
Roland Levillain46648892015-06-19 16:07:18 +0100385 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
386
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
389};
390
391class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
392 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100393 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395
396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200397 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
399 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200400 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100401 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700402 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200403 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700404 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700405 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700407 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700408 }
409 }
410
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700411 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700412 DCHECK(successor_ == nullptr);
413 return &return_label_;
414 }
415
Roland Levillain46648892015-06-19 16:07:18 +0100416 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
417
Alexey Frunze4dda3372015-06-01 18:31:49 -0700418 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700419 // If not null, the block to branch to after the suspend check.
420 HBasicBlock* const successor_;
421
422 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700423 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700424
425 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
426};
427
428class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
429 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800430 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
431 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700432
433 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
434 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800435
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100436 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700437 DCHECK(instruction_->IsCheckCast()
438 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
439 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
440
441 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800442 if (!is_fatal_) {
443 SaveLiveRegisters(codegen, locations);
444 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700445
446 // We're moving two locations to locations that could overlap, so we need a parallel
447 // move resolver.
448 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800449 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700450 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100451 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800452 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700453 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100454 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700455 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100456 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800457 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100458 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700459 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
460 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700461 } else {
462 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800463 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
464 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700465 }
466
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800467 if (!is_fatal_) {
468 RestoreLiveRegisters(codegen, locations);
469 __ Bc(GetExitLabel());
470 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700471 }
472
Roland Levillain46648892015-06-19 16:07:18 +0100473 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
474
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800475 bool IsFatal() const OVERRIDE { return is_fatal_; }
476
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800478 const bool is_fatal_;
479
Alexey Frunze4dda3372015-06-01 18:31:49 -0700480 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
481};
482
483class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
484 public:
Aart Bik42249c32016-01-07 15:33:50 -0800485 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000486 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700487
488 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800489 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700490 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100491 LocationSummary* locations = instruction_->GetLocations();
492 SaveLiveRegisters(codegen, locations);
493 InvokeRuntimeCallingConvention calling_convention;
494 __ LoadConst32(calling_convention.GetRegisterAt(0),
495 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100496 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100497 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700498 }
499
Roland Levillain46648892015-06-19 16:07:18 +0100500 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
501
Alexey Frunze4dda3372015-06-01 18:31:49 -0700502 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700503 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
504};
505
Alexey Frunze15958152017-02-09 19:08:30 -0800506class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
507 public:
508 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
509
510 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
511 LocationSummary* locations = instruction_->GetLocations();
512 __ Bind(GetEntryLabel());
513 SaveLiveRegisters(codegen, locations);
514
515 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100516 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800517 parallel_move.AddMove(
518 locations->InAt(0),
519 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100520 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(1),
524 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100525 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(2),
529 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800531 nullptr);
532 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
533
534 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
535 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
536 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
537 RestoreLiveRegisters(codegen, locations);
538 __ Bc(GetExitLabel());
539 }
540
541 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
542
543 private:
544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
545};
546
547// Slow path marking an object reference `ref` during a read
548// barrier. The field `obj.field` in the object `obj` holding this
549// reference does not get updated by this slow path after marking (see
550// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
551//
552// This means that after the execution of this slow path, `ref` will
553// always be up-to-date, but `obj.field` may not; i.e., after the
554// flip, `ref` will be a to-space reference, but `obj.field` will
555// probably still be a from-space reference (unless it gets updated by
556// another thread, or if another thread installed another object
557// reference (different from `ref`) in `obj.field`).
558//
559// If `entrypoint` is a valid location it is assumed to already be
560// holding the entrypoint. The case where the entrypoint is passed in
561// is for the GcRoot read barrier.
562class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
563 public:
564 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
565 Location ref,
566 Location entrypoint = Location::NoLocation())
567 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
568 DCHECK(kEmitCompilerReadBarrier);
569 }
570
571 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
572
573 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
574 LocationSummary* locations = instruction_->GetLocations();
575 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
576 DCHECK(locations->CanCall());
577 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
578 DCHECK(instruction_->IsInstanceFieldGet() ||
579 instruction_->IsStaticFieldGet() ||
580 instruction_->IsArrayGet() ||
581 instruction_->IsArraySet() ||
582 instruction_->IsLoadClass() ||
583 instruction_->IsLoadString() ||
584 instruction_->IsInstanceOf() ||
585 instruction_->IsCheckCast() ||
586 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
587 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
588 << "Unexpected instruction in read barrier marking slow path: "
589 << instruction_->DebugName();
590
591 __ Bind(GetEntryLabel());
592 // No need to save live registers; it's taken care of by the
593 // entrypoint. Also, there is no need to update the stack mask,
594 // as this runtime call will not trigger a garbage collection.
595 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
596 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
597 (S2 <= ref_reg && ref_reg <= S7) ||
598 (ref_reg == S8)) << ref_reg;
599 // "Compact" slow path, saving two moves.
600 //
601 // Instead of using the standard runtime calling convention (input
602 // and output in A0 and V0 respectively):
603 //
604 // A0 <- ref
605 // V0 <- ReadBarrierMark(A0)
606 // ref <- V0
607 //
608 // we just use rX (the register containing `ref`) as input and output
609 // of a dedicated entrypoint:
610 //
611 // rX <- ReadBarrierMarkRegX(rX)
612 //
613 if (entrypoint_.IsValid()) {
614 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
615 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
616 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
617 __ Nop();
618 } else {
619 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100620 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800621 // This runtime call does not require a stack map.
622 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
623 instruction_,
624 this);
625 }
626 __ Bc(GetExitLabel());
627 }
628
629 private:
630 // The location (register) of the marked object reference.
631 const Location ref_;
632
633 // The location of the entrypoint if already loaded.
634 const Location entrypoint_;
635
636 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
637};
638
639// Slow path marking an object reference `ref` during a read barrier,
640// and if needed, atomically updating the field `obj.field` in the
641// object `obj` holding this reference after marking (contrary to
642// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
643// `obj.field`).
644//
645// This means that after the execution of this slow path, both `ref`
646// and `obj.field` will be up-to-date; i.e., after the flip, both will
647// hold the same to-space reference (unless another thread installed
648// another object reference (different from `ref`) in `obj.field`).
649class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
650 public:
651 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
652 Location ref,
653 GpuRegister obj,
654 Location field_offset,
655 GpuRegister temp1)
656 : SlowPathCodeMIPS64(instruction),
657 ref_(ref),
658 obj_(obj),
659 field_offset_(field_offset),
660 temp1_(temp1) {
661 DCHECK(kEmitCompilerReadBarrier);
662 }
663
664 const char* GetDescription() const OVERRIDE {
665 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
666 }
667
668 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
669 LocationSummary* locations = instruction_->GetLocations();
670 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
671 DCHECK(locations->CanCall());
672 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
673 // This slow path is only used by the UnsafeCASObject intrinsic.
674 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
675 << "Unexpected instruction in read barrier marking and field updating slow path: "
676 << instruction_->DebugName();
677 DCHECK(instruction_->GetLocations()->Intrinsified());
678 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
679 DCHECK(field_offset_.IsRegister()) << field_offset_;
680
681 __ Bind(GetEntryLabel());
682
683 // Save the old reference.
684 // Note that we cannot use AT or TMP to save the old reference, as those
685 // are used by the code that follows, but we need the old reference after
686 // the call to the ReadBarrierMarkRegX entry point.
687 DCHECK_NE(temp1_, AT);
688 DCHECK_NE(temp1_, TMP);
689 __ Move(temp1_, ref_reg);
690
691 // No need to save live registers; it's taken care of by the
692 // entrypoint. Also, there is no need to update the stack mask,
693 // as this runtime call will not trigger a garbage collection.
694 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
695 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
696 (S2 <= ref_reg && ref_reg <= S7) ||
697 (ref_reg == S8)) << ref_reg;
698 // "Compact" slow path, saving two moves.
699 //
700 // Instead of using the standard runtime calling convention (input
701 // and output in A0 and V0 respectively):
702 //
703 // A0 <- ref
704 // V0 <- ReadBarrierMark(A0)
705 // ref <- V0
706 //
707 // we just use rX (the register containing `ref`) as input and output
708 // of a dedicated entrypoint:
709 //
710 // rX <- ReadBarrierMarkRegX(rX)
711 //
712 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100713 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800714 // This runtime call does not require a stack map.
715 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
716 instruction_,
717 this);
718
719 // If the new reference is different from the old reference,
720 // update the field in the holder (`*(obj_ + field_offset_)`).
721 //
722 // Note that this field could also hold a different object, if
723 // another thread had concurrently changed it. In that case, the
724 // the compare-and-set (CAS) loop below would abort, leaving the
725 // field as-is.
726 Mips64Label done;
727 __ Beqc(temp1_, ref_reg, &done);
728
729 // Update the the holder's field atomically. This may fail if
730 // mutator updates before us, but it's OK. This is achieved
731 // using a strong compare-and-set (CAS) operation with relaxed
732 // memory synchronization ordering, where the expected value is
733 // the old reference and the desired value is the new reference.
734
735 // Convenience aliases.
736 GpuRegister base = obj_;
737 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
738 GpuRegister expected = temp1_;
739 GpuRegister value = ref_reg;
740 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
741 GpuRegister tmp = AT; // Value in memory.
742
743 __ Daddu(tmp_ptr, base, offset);
744
745 if (kPoisonHeapReferences) {
746 __ PoisonHeapReference(expected);
747 // Do not poison `value` if it is the same register as
748 // `expected`, which has just been poisoned.
749 if (value != expected) {
750 __ PoisonHeapReference(value);
751 }
752 }
753
754 // do {
755 // tmp = [r_ptr] - expected;
756 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
757
758 Mips64Label loop_head, exit_loop;
759 __ Bind(&loop_head);
760 __ Ll(tmp, tmp_ptr);
761 // The LL instruction sign-extends the 32-bit value, but
762 // 32-bit references must be zero-extended. Zero-extend `tmp`.
763 __ Dext(tmp, tmp, 0, 32);
764 __ Bnec(tmp, expected, &exit_loop);
765 __ Move(tmp, value);
766 __ Sc(tmp, tmp_ptr);
767 __ Beqzc(tmp, &loop_head);
768 __ Bind(&exit_loop);
769
770 if (kPoisonHeapReferences) {
771 __ UnpoisonHeapReference(expected);
772 // Do not unpoison `value` if it is the same register as
773 // `expected`, which has just been unpoisoned.
774 if (value != expected) {
775 __ UnpoisonHeapReference(value);
776 }
777 }
778
779 __ Bind(&done);
780 __ Bc(GetExitLabel());
781 }
782
783 private:
784 // The location (register) of the marked object reference.
785 const Location ref_;
786 // The register containing the object holding the marked object reference field.
787 const GpuRegister obj_;
788 // The location of the offset of the marked reference field within `obj_`.
789 Location field_offset_;
790
791 const GpuRegister temp1_;
792
793 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
794};
795
796// Slow path generating a read barrier for a heap reference.
797class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
798 public:
799 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
800 Location out,
801 Location ref,
802 Location obj,
803 uint32_t offset,
804 Location index)
805 : SlowPathCodeMIPS64(instruction),
806 out_(out),
807 ref_(ref),
808 obj_(obj),
809 offset_(offset),
810 index_(index) {
811 DCHECK(kEmitCompilerReadBarrier);
812 // If `obj` is equal to `out` or `ref`, it means the initial object
813 // has been overwritten by (or after) the heap object reference load
814 // to be instrumented, e.g.:
815 //
816 // __ LoadFromOffset(kLoadWord, out, out, offset);
817 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
818 //
819 // In that case, we have lost the information about the original
820 // object, and the emitted read barrier cannot work properly.
821 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
822 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
823 }
824
825 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
826 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
827 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800829 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
830 DCHECK(locations->CanCall());
831 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
832 DCHECK(instruction_->IsInstanceFieldGet() ||
833 instruction_->IsStaticFieldGet() ||
834 instruction_->IsArrayGet() ||
835 instruction_->IsInstanceOf() ||
836 instruction_->IsCheckCast() ||
837 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
838 << "Unexpected instruction in read barrier for heap reference slow path: "
839 << instruction_->DebugName();
840
841 __ Bind(GetEntryLabel());
842 SaveLiveRegisters(codegen, locations);
843
844 // We may have to change the index's value, but as `index_` is a
845 // constant member (like other "inputs" of this slow path),
846 // introduce a copy of it, `index`.
847 Location index = index_;
848 if (index_.IsValid()) {
849 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
850 if (instruction_->IsArrayGet()) {
851 // Compute the actual memory offset and store it in `index`.
852 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
853 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
854 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
855 // We are about to change the value of `index_reg` (see the
856 // calls to art::mips64::Mips64Assembler::Sll and
857 // art::mips64::MipsAssembler::Addiu32 below), but it has
858 // not been saved by the previous call to
859 // art::SlowPathCode::SaveLiveRegisters, as it is a
860 // callee-save register --
861 // art::SlowPathCode::SaveLiveRegisters does not consider
862 // callee-save registers, as it has been designed with the
863 // assumption that callee-save registers are supposed to be
864 // handled by the called function. So, as a callee-save
865 // register, `index_reg` _would_ eventually be saved onto
866 // the stack, but it would be too late: we would have
867 // changed its value earlier. Therefore, we manually save
868 // it here into another freely available register,
869 // `free_reg`, chosen of course among the caller-save
870 // registers (as a callee-save `free_reg` register would
871 // exhibit the same problem).
872 //
873 // Note we could have requested a temporary register from
874 // the register allocator instead; but we prefer not to, as
875 // this is a slow path, and we know we can find a
876 // caller-save register that is available.
877 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
878 __ Move(free_reg, index_reg);
879 index_reg = free_reg;
880 index = Location::RegisterLocation(index_reg);
881 } else {
882 // The initial register stored in `index_` has already been
883 // saved in the call to art::SlowPathCode::SaveLiveRegisters
884 // (as it is not a callee-save register), so we can freely
885 // use it.
886 }
887 // Shifting the index value contained in `index_reg` by the scale
888 // factor (2) cannot overflow in practice, as the runtime is
889 // unable to allocate object arrays with a size larger than
890 // 2^26 - 1 (that is, 2^28 - 4 bytes).
891 __ Sll(index_reg, index_reg, TIMES_4);
892 static_assert(
893 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
894 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
895 __ Addiu32(index_reg, index_reg, offset_);
896 } else {
897 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
898 // intrinsics, `index_` is not shifted by a scale factor of 2
899 // (as in the case of ArrayGet), as it is actually an offset
900 // to an object field within an object.
901 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
902 DCHECK(instruction_->GetLocations()->Intrinsified());
903 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
904 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
905 << instruction_->AsInvoke()->GetIntrinsic();
906 DCHECK_EQ(offset_, 0U);
907 DCHECK(index_.IsRegister());
908 }
909 }
910
911 // We're moving two or three locations to locations that could
912 // overlap, so we need a parallel move resolver.
913 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100914 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800915 parallel_move.AddMove(ref_,
916 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100917 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800918 nullptr);
919 parallel_move.AddMove(obj_,
920 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100921 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800922 nullptr);
923 if (index.IsValid()) {
924 parallel_move.AddMove(index,
925 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100926 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800927 nullptr);
928 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
929 } else {
930 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
931 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
932 }
933 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
934 instruction_,
935 instruction_->GetDexPc(),
936 this);
937 CheckEntrypointTypes<
938 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
939 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
940
941 RestoreLiveRegisters(codegen, locations);
942 __ Bc(GetExitLabel());
943 }
944
945 const char* GetDescription() const OVERRIDE {
946 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
947 }
948
949 private:
950 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
951 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
952 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
953 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
954 if (i != ref &&
955 i != obj &&
956 !codegen->IsCoreCalleeSaveRegister(i) &&
957 !codegen->IsBlockedCoreRegister(i)) {
958 return static_cast<GpuRegister>(i);
959 }
960 }
961 // We shall never fail to find a free caller-save register, as
962 // there are more than two core caller-save registers on MIPS64
963 // (meaning it is possible to find one which is different from
964 // `ref` and `obj`).
965 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
966 LOG(FATAL) << "Could not find a free caller-save register";
967 UNREACHABLE();
968 }
969
970 const Location out_;
971 const Location ref_;
972 const Location obj_;
973 const uint32_t offset_;
974 // An additional location containing an index to an array.
975 // Only used for HArrayGet and the UnsafeGetObject &
976 // UnsafeGetObjectVolatile intrinsics.
977 const Location index_;
978
979 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
980};
981
982// Slow path generating a read barrier for a GC root.
983class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
984 public:
985 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
986 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
987 DCHECK(kEmitCompilerReadBarrier);
988 }
989
990 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
991 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100992 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800993 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
994 DCHECK(locations->CanCall());
995 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
996 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
997 << "Unexpected instruction in read barrier for GC root slow path: "
998 << instruction_->DebugName();
999
1000 __ Bind(GetEntryLabel());
1001 SaveLiveRegisters(codegen, locations);
1002
1003 InvokeRuntimeCallingConvention calling_convention;
1004 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
1005 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1006 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001007 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001008 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1009 instruction_,
1010 instruction_->GetDexPc(),
1011 this);
1012 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1013 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1014
1015 RestoreLiveRegisters(codegen, locations);
1016 __ Bc(GetExitLabel());
1017 }
1018
1019 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
1020
1021 private:
1022 const Location out_;
1023 const Location root_;
1024
1025 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
1026};
1027
Alexey Frunze4dda3372015-06-01 18:31:49 -07001028CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
1029 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001030 const CompilerOptions& compiler_options,
1031 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032 : CodeGenerator(graph,
1033 kNumberOfGpuRegisters,
1034 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001035 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001036 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1037 arraysize(kCoreCalleeSaves)),
1038 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1039 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001040 compiler_options,
1041 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001042 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001043 location_builder_(graph, this),
1044 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001045 move_resolver_(graph->GetAllocator(), this),
1046 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001047 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001048 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001050 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001051 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1052 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1053 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1054 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1055 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1056 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1057 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001058 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001059 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001060 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001061 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001062 // Save RA (containing the return address) to mimic Quick.
1063 AddAllocatedRegister(Location::RegisterLocation(RA));
1064}
1065
1066#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001067// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1068#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001069#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -07001070
1071void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001072 // Ensure that we fix up branches.
1073 __ FinalizeCode();
1074
1075 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001076 StackMapStream* stack_map_stream = GetStackMapStream();
1077 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001078 uint32_t old_position =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001079 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001080 uint32_t new_position = __ GetAdjustedPosition(old_position);
1081 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001082 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001083 }
1084
1085 // Adjust pc offsets for the disassembly information.
1086 if (disasm_info_ != nullptr) {
1087 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1088 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1089 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1090 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1091 it.second.start = __ GetAdjustedPosition(it.second.start);
1092 it.second.end = __ GetAdjustedPosition(it.second.end);
1093 }
1094 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1095 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1096 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1097 }
1098 }
1099
Alexey Frunze4dda3372015-06-01 18:31:49 -07001100 CodeGenerator::Finalize(allocator);
1101}
1102
1103Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1104 return codegen_->GetAssembler();
1105}
1106
1107void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001108 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001109 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1110}
1111
1112void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001113 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001114 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1115}
1116
1117void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1118 // Pop reg
1119 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001120 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001121}
1122
1123void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1124 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001125 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001126 __ Sd(GpuRegister(reg), SP, 0);
1127}
1128
1129void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1130 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1131 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1132 // Allocate a scratch register other than TMP, if available.
1133 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1134 // automatically unspilled when the scratch scope object is destroyed).
1135 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1136 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001137 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138 __ LoadFromOffset(load_type,
1139 GpuRegister(ensure_scratch.GetRegister()),
1140 SP,
1141 index1 + stack_offset);
1142 __ LoadFromOffset(load_type,
1143 TMP,
1144 SP,
1145 index2 + stack_offset);
1146 __ StoreToOffset(store_type,
1147 GpuRegister(ensure_scratch.GetRegister()),
1148 SP,
1149 index2 + stack_offset);
1150 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1151}
1152
1153static dwarf::Reg DWARFReg(GpuRegister reg) {
1154 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1155}
1156
David Srbeckyba702002016-02-01 18:15:29 +00001157static dwarf::Reg DWARFReg(FpuRegister reg) {
1158 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1159}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001160
1161void CodeGeneratorMIPS64::GenerateFrameEntry() {
1162 __ Bind(&frame_entry_label_);
1163
1164 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1165
1166 if (do_overflow_check) {
1167 __ LoadFromOffset(kLoadWord,
1168 ZERO,
1169 SP,
1170 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1171 RecordPcInfo(nullptr, 0);
1172 }
1173
Alexey Frunze4dda3372015-06-01 18:31:49 -07001174 if (HasEmptyFrame()) {
1175 return;
1176 }
1177
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001178 // Make sure the frame size isn't unreasonably large.
1179 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1180 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1181 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182
1183 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001184
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001185 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186 __ IncreaseFrameSize(ofs);
1187
1188 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1189 GpuRegister reg = kCoreCalleeSaves[i];
1190 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001191 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001192 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001193 __ cfi().RelOffset(DWARFReg(reg), ofs);
1194 }
1195 }
1196
1197 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1198 FpuRegister reg = kFpuCalleeSaves[i];
1199 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001200 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001201 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001202 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001203 }
1204 }
1205
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001206 // Save the current method if we need it. Note that we do not
1207 // do this in HCurrentMethod, as the instruction might have been removed
1208 // in the SSA graph.
1209 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001210 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001211 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001212
1213 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1214 // Initialize should_deoptimize flag to 0.
1215 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1216 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001217}
1218
1219void CodeGeneratorMIPS64::GenerateFrameExit() {
1220 __ cfi().RememberState();
1221
Alexey Frunze4dda3372015-06-01 18:31:49 -07001222 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001223 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001225 // For better instruction scheduling restore RA before other registers.
1226 uint32_t ofs = GetFrameSize();
1227 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 GpuRegister reg = kCoreCalleeSaves[i];
1229 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001230 ofs -= kMips64DoublewordSize;
1231 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001232 __ cfi().Restore(DWARFReg(reg));
1233 }
1234 }
1235
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001236 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1237 FpuRegister reg = kFpuCalleeSaves[i];
1238 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1239 ofs -= kMips64DoublewordSize;
1240 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1241 __ cfi().Restore(DWARFReg(reg));
1242 }
1243 }
1244
1245 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001246 }
1247
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001248 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001249
1250 __ cfi().RestoreState();
1251 __ cfi().DefCFAOffset(GetFrameSize());
1252}
1253
1254void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1255 __ Bind(GetLabelOf(block));
1256}
1257
1258void CodeGeneratorMIPS64::MoveLocation(Location destination,
1259 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001260 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001261 if (source.Equals(destination)) {
1262 return;
1263 }
1264
1265 // A valid move can always be inferred from the destination and source
1266 // locations. When moving from and to a register, the argument type can be
1267 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001268 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001269 DCHECK_EQ(unspecified_type, false);
1270
1271 if (destination.IsRegister() || destination.IsFpuRegister()) {
1272 if (unspecified_type) {
1273 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1274 if (source.IsStackSlot() ||
1275 (src_cst != nullptr && (src_cst->IsIntConstant()
1276 || src_cst->IsFloatConstant()
1277 || src_cst->IsNullConstant()))) {
1278 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001279 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001280 } else {
1281 // If the source is a double stack slot or a 64bit constant, a 64bit
1282 // type is appropriate. Else the source is a register, and since the
1283 // type has not been specified, we chose a 64bit type to force a 64bit
1284 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001285 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001286 }
1287 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001288 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1289 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1291 // Move to GPR/FPR from stack
1292 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001293 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001294 __ LoadFpuFromOffset(load_type,
1295 destination.AsFpuRegister<FpuRegister>(),
1296 SP,
1297 source.GetStackIndex());
1298 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001299 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001300 __ LoadFromOffset(load_type,
1301 destination.AsRegister<GpuRegister>(),
1302 SP,
1303 source.GetStackIndex());
1304 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001305 } else if (source.IsSIMDStackSlot()) {
1306 __ LoadFpuFromOffset(kLoadQuadword,
1307 destination.AsFpuRegister<FpuRegister>(),
1308 SP,
1309 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001310 } else if (source.IsConstant()) {
1311 // Move to GPR/FPR from constant
1312 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001313 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001314 gpr = destination.AsRegister<GpuRegister>();
1315 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001316 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001317 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001318 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001319 gpr = ZERO;
1320 } else {
1321 __ LoadConst32(gpr, value);
1322 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001323 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001324 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001325 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001326 gpr = ZERO;
1327 } else {
1328 __ LoadConst64(gpr, value);
1329 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001331 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001332 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001333 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001334 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1335 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001336 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001337 if (destination.IsRegister()) {
1338 // Move to GPR from GPR
1339 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1340 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001341 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001342 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001343 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1344 } else {
1345 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1346 }
1347 }
1348 } else if (source.IsFpuRegister()) {
1349 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001350 if (GetGraph()->HasSIMD()) {
1351 __ MoveV(VectorRegisterFrom(destination),
1352 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001353 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001354 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001355 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001356 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1357 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001358 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001359 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1360 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001361 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001362 } else {
1363 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001364 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001365 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1366 } else {
1367 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1368 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001369 }
1370 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001371 } else if (destination.IsSIMDStackSlot()) {
1372 if (source.IsFpuRegister()) {
1373 __ StoreFpuToOffset(kStoreQuadword,
1374 source.AsFpuRegister<FpuRegister>(),
1375 SP,
1376 destination.GetStackIndex());
1377 } else {
1378 DCHECK(source.IsSIMDStackSlot());
1379 __ LoadFpuFromOffset(kLoadQuadword,
1380 FTMP,
1381 SP,
1382 source.GetStackIndex());
1383 __ StoreFpuToOffset(kStoreQuadword,
1384 FTMP,
1385 SP,
1386 destination.GetStackIndex());
1387 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001388 } else { // The destination is not a register. It must be a stack slot.
1389 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1390 if (source.IsRegister() || source.IsFpuRegister()) {
1391 if (unspecified_type) {
1392 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001393 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001394 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001395 dst_type =
1396 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001397 }
1398 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001399 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1400 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001401 // Move to stack from GPR/FPR
1402 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1403 if (source.IsRegister()) {
1404 __ StoreToOffset(store_type,
1405 source.AsRegister<GpuRegister>(),
1406 SP,
1407 destination.GetStackIndex());
1408 } else {
1409 __ StoreFpuToOffset(store_type,
1410 source.AsFpuRegister<FpuRegister>(),
1411 SP,
1412 destination.GetStackIndex());
1413 }
1414 } else if (source.IsConstant()) {
1415 // Move to stack from constant
1416 HConstant* src_cst = source.GetConstant();
1417 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001418 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001420 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1421 if (value != 0) {
1422 gpr = TMP;
1423 __ LoadConst32(gpr, value);
1424 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001426 DCHECK(destination.IsDoubleStackSlot());
1427 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1428 if (value != 0) {
1429 gpr = TMP;
1430 __ LoadConst64(gpr, value);
1431 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001432 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001433 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001434 } else {
1435 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1436 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1437 // Move to stack from stack
1438 if (destination.IsStackSlot()) {
1439 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1440 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1441 } else {
1442 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1443 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1444 }
1445 }
1446 }
1447}
1448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001450 DCHECK(!loc1.IsConstant());
1451 DCHECK(!loc2.IsConstant());
1452
1453 if (loc1.Equals(loc2)) {
1454 return;
1455 }
1456
1457 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1458 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1459 bool is_fp_reg1 = loc1.IsFpuRegister();
1460 bool is_fp_reg2 = loc2.IsFpuRegister();
1461
1462 if (loc2.IsRegister() && loc1.IsRegister()) {
1463 // Swap 2 GPRs
1464 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1465 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1466 __ Move(TMP, r2);
1467 __ Move(r2, r1);
1468 __ Move(r1, TMP);
1469 } else if (is_fp_reg2 && is_fp_reg1) {
1470 // Swap 2 FPRs
1471 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1472 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 if (type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001474 __ MovS(FTMP, r1);
1475 __ MovS(r1, r2);
1476 __ MovS(r2, FTMP);
1477 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001478 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001479 __ MovD(FTMP, r1);
1480 __ MovD(r1, r2);
1481 __ MovD(r2, FTMP);
1482 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001483 } else if (is_slot1 != is_slot2) {
1484 // Swap GPR/FPR and stack slot
1485 Location reg_loc = is_slot1 ? loc2 : loc1;
1486 Location mem_loc = is_slot1 ? loc1 : loc2;
1487 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1488 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001489 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001490 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1491 if (reg_loc.IsFpuRegister()) {
1492 __ StoreFpuToOffset(store_type,
1493 reg_loc.AsFpuRegister<FpuRegister>(),
1494 SP,
1495 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001496 if (mem_loc.IsStackSlot()) {
1497 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1498 } else {
1499 DCHECK(mem_loc.IsDoubleStackSlot());
1500 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1501 }
1502 } else {
1503 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1504 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1505 }
1506 } else if (is_slot1 && is_slot2) {
1507 move_resolver_.Exchange(loc1.GetStackIndex(),
1508 loc2.GetStackIndex(),
1509 loc1.IsDoubleStackSlot());
1510 } else {
1511 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1512 }
1513}
1514
Calin Juravle175dc732015-08-25 15:42:32 +01001515void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1516 DCHECK(location.IsRegister());
1517 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1518}
1519
Calin Juravlee460d1d2015-09-29 04:52:17 +01001520void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1521 if (location.IsRegister()) {
1522 locations->AddTemp(location);
1523 } else {
1524 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1525 }
1526}
1527
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001528void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1529 GpuRegister value,
1530 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001531 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001532 GpuRegister card = AT;
1533 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001534 if (value_can_be_null) {
1535 __ Beqzc(value, &done);
1536 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001537 __ LoadFromOffset(kLoadDoubleword,
1538 card,
1539 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001540 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001541 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1542 __ Daddu(temp, card, temp);
1543 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001544 if (value_can_be_null) {
1545 __ Bind(&done);
1546 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001547}
1548
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001549template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001550inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1551 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001552 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001553 for (const PcRelativePatchInfo& info : infos) {
1554 const DexFile& dex_file = info.target_dex_file;
1555 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001556 DCHECK(info.label.IsBound());
1557 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1558 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1559 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1560 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001561 }
1562}
1563
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001564void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001565 DCHECK(linker_patches->empty());
1566 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001567 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001568 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001569 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001570 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001571 pc_relative_string_patches_.size() +
1572 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001573 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001574 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001575 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1576 pc_relative_method_patches_, linker_patches);
1577 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1578 pc_relative_type_patches_, linker_patches);
1579 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1580 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001581 } else {
1582 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001583 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1584 pc_relative_type_patches_, linker_patches);
1585 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1586 pc_relative_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001587 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001588 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1589 method_bss_entry_patches_, linker_patches);
1590 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1591 type_bss_entry_patches_, linker_patches);
1592 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1593 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001594 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001595}
1596
Vladimir Marko65979462017-05-19 17:25:12 +01001597CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001598 MethodReference target_method,
1599 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001600 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001601 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001602 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001603 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001604}
1605
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001606CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001607 MethodReference target_method,
1608 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001609 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001610 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001611 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001612 &method_bss_entry_patches_);
1613}
1614
Alexey Frunzef63f5692016-12-13 17:43:11 -08001615CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001616 const DexFile& dex_file,
1617 dex::TypeIndex type_index,
1618 const PcRelativePatchInfo* info_high) {
1619 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001620}
1621
Vladimir Marko1998cd02017-01-13 13:02:58 +00001622CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001623 const DexFile& dex_file,
1624 dex::TypeIndex type_index,
1625 const PcRelativePatchInfo* info_high) {
1626 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001627}
1628
Vladimir Marko65979462017-05-19 17:25:12 +01001629CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001630 const DexFile& dex_file,
1631 dex::StringIndex string_index,
1632 const PcRelativePatchInfo* info_high) {
1633 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001634}
1635
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001636CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1637 const DexFile& dex_file,
1638 dex::StringIndex string_index,
1639 const PcRelativePatchInfo* info_high) {
1640 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1641}
1642
Alexey Frunze19f6c692016-11-30 19:19:55 -08001643CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001644 const DexFile& dex_file,
1645 uint32_t offset_or_index,
1646 const PcRelativePatchInfo* info_high,
1647 ArenaDeque<PcRelativePatchInfo>* patches) {
1648 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001649 return &patches->back();
1650}
1651
Alexey Frunzef63f5692016-12-13 17:43:11 -08001652Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1653 return map->GetOrCreate(
1654 value,
1655 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1656}
1657
Alexey Frunze19f6c692016-11-30 19:19:55 -08001658Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1659 return uint64_literals_.GetOrCreate(
1660 value,
1661 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1662}
1663
Alexey Frunzef63f5692016-12-13 17:43:11 -08001664Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001665 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001666}
1667
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001668void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1669 GpuRegister out,
1670 PcRelativePatchInfo* info_low) {
1671 DCHECK(!info_high->patch_info_high);
1672 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001673 // Add the high half of a 32-bit offset to PC.
1674 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001675 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001676 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001677 if (info_low != nullptr) {
1678 DCHECK_EQ(info_low->patch_info_high, info_high);
1679 __ Bind(&info_low->label);
1680 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001681}
1682
Alexey Frunze627c1a02017-01-30 19:28:14 -08001683Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1684 dex::StringIndex string_index,
1685 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001686 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001687 return jit_string_patches_.GetOrCreate(
1688 StringReference(&dex_file, string_index),
1689 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1690}
1691
1692Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1693 dex::TypeIndex type_index,
1694 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001695 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001696 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;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001715 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001716 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001717 }
1718 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001719 const TypeReference& type_reference = entry.first;
1720 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001721 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001722 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001723 }
1724}
1725
David Brazdil58282f42016-01-14 12:45:10 +00001726void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001727 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1728 blocked_core_registers_[ZERO] = true;
1729 blocked_core_registers_[K0] = true;
1730 blocked_core_registers_[K1] = true;
1731 blocked_core_registers_[GP] = true;
1732 blocked_core_registers_[SP] = true;
1733 blocked_core_registers_[RA] = true;
1734
Lazar Trsicd9672662015-09-03 17:33:01 +02001735 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1736 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001737 blocked_core_registers_[AT] = true;
1738 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001739 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740 blocked_fpu_registers_[FTMP] = true;
1741
1742 // Reserve suspend and thread registers.
1743 blocked_core_registers_[S0] = true;
1744 blocked_core_registers_[TR] = true;
1745
1746 // Reserve T9 for function calls
1747 blocked_core_registers_[T9] = true;
1748
Goran Jakovljevic782be112016-06-21 12:39:04 +02001749 if (GetGraph()->IsDebuggable()) {
1750 // Stubs do not save callee-save floating point registers. If the graph
1751 // is debuggable, we need to deal with these registers differently. For
1752 // now, just block them.
1753 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1754 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1755 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001756 }
1757}
1758
Alexey Frunze4dda3372015-06-01 18:31:49 -07001759size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1760 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001761 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762}
1763
1764size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1765 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001766 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001767}
1768
1769size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001770 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1771 FpuRegister(reg_id),
1772 SP,
1773 stack_index);
1774 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001775}
1776
1777size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001778 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1779 FpuRegister(reg_id),
1780 SP,
1781 stack_index);
1782 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001783}
1784
1785void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001786 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787}
1788
1789void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001790 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001791}
1792
Calin Juravle175dc732015-08-25 15:42:32 +01001793void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001794 HInstruction* instruction,
1795 uint32_t dex_pc,
1796 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001797 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001798 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001799 if (EntrypointRequiresStackMap(entrypoint)) {
1800 RecordPcInfo(instruction, dex_pc, slow_path);
1801 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001802}
1803
Alexey Frunze15958152017-02-09 19:08:30 -08001804void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1805 HInstruction* instruction,
1806 SlowPathCode* slow_path) {
1807 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1808 GenerateInvokeRuntime(entry_point_offset);
1809}
1810
1811void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1812 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1813 __ Jalr(T9);
1814 __ Nop();
1815}
1816
Alexey Frunze4dda3372015-06-01 18:31:49 -07001817void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1818 GpuRegister class_reg) {
1819 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1820 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1821 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001822 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1823 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001824 __ Bind(slow_path->GetExitLabel());
1825}
1826
1827void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1828 __ Sync(0); // only stype 0 is supported
1829}
1830
1831void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1832 HBasicBlock* successor) {
1833 SuspendCheckSlowPathMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001834 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001835 codegen_->AddSlowPath(slow_path);
1836
1837 __ LoadFromOffset(kLoadUnsignedHalfword,
1838 TMP,
1839 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001840 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001841 if (successor == nullptr) {
1842 __ Bnezc(TMP, slow_path->GetEntryLabel());
1843 __ Bind(slow_path->GetReturnLabel());
1844 } else {
1845 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001846 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001847 // slow_path will return to GetLabelOf(successor).
1848 }
1849}
1850
1851InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1852 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001853 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001854 assembler_(codegen->GetAssembler()),
1855 codegen_(codegen) {}
1856
1857void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1858 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001859 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001861 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001862 case DataType::Type::kInt32:
1863 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001864 locations->SetInAt(0, Location::RequiresRegister());
1865 HInstruction* right = instruction->InputAt(1);
1866 bool can_use_imm = false;
1867 if (right->IsConstant()) {
1868 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1869 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1870 can_use_imm = IsUint<16>(imm);
1871 } else if (instruction->IsAdd()) {
1872 can_use_imm = IsInt<16>(imm);
1873 } else {
1874 DCHECK(instruction->IsSub());
1875 can_use_imm = IsInt<16>(-imm);
1876 }
1877 }
1878 if (can_use_imm)
1879 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1880 else
1881 locations->SetInAt(1, Location::RequiresRegister());
1882 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1883 }
1884 break;
1885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 case DataType::Type::kFloat32:
1887 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001888 locations->SetInAt(0, Location::RequiresFpuRegister());
1889 locations->SetInAt(1, Location::RequiresFpuRegister());
1890 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1891 break;
1892
1893 default:
1894 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1895 }
1896}
1897
1898void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001899 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001900 LocationSummary* locations = instruction->GetLocations();
1901
1902 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001903 case DataType::Type::kInt32:
1904 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001905 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1906 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1907 Location rhs_location = locations->InAt(1);
1908
1909 GpuRegister rhs_reg = ZERO;
1910 int64_t rhs_imm = 0;
1911 bool use_imm = rhs_location.IsConstant();
1912 if (use_imm) {
1913 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1914 } else {
1915 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1916 }
1917
1918 if (instruction->IsAnd()) {
1919 if (use_imm)
1920 __ Andi(dst, lhs, rhs_imm);
1921 else
1922 __ And(dst, lhs, rhs_reg);
1923 } else if (instruction->IsOr()) {
1924 if (use_imm)
1925 __ Ori(dst, lhs, rhs_imm);
1926 else
1927 __ Or(dst, lhs, rhs_reg);
1928 } else if (instruction->IsXor()) {
1929 if (use_imm)
1930 __ Xori(dst, lhs, rhs_imm);
1931 else
1932 __ Xor(dst, lhs, rhs_reg);
1933 } else if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001935 if (use_imm)
1936 __ Addiu(dst, lhs, rhs_imm);
1937 else
1938 __ Addu(dst, lhs, rhs_reg);
1939 } else {
1940 if (use_imm)
1941 __ Daddiu(dst, lhs, rhs_imm);
1942 else
1943 __ Daddu(dst, lhs, rhs_reg);
1944 }
1945 } else {
1946 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001948 if (use_imm)
1949 __ Addiu(dst, lhs, -rhs_imm);
1950 else
1951 __ Subu(dst, lhs, rhs_reg);
1952 } else {
1953 if (use_imm)
1954 __ Daddiu(dst, lhs, -rhs_imm);
1955 else
1956 __ Dsubu(dst, lhs, rhs_reg);
1957 }
1958 }
1959 break;
1960 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001961 case DataType::Type::kFloat32:
1962 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001963 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1964 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1965 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1966 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001967 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001968 __ AddS(dst, lhs, rhs);
1969 else
1970 __ AddD(dst, lhs, rhs);
1971 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001972 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001973 __ SubS(dst, lhs, rhs);
1974 else
1975 __ SubD(dst, lhs, rhs);
1976 } else {
1977 LOG(FATAL) << "Unexpected floating-point binary operation";
1978 }
1979 break;
1980 }
1981 default:
1982 LOG(FATAL) << "Unexpected binary operation type " << type;
1983 }
1984}
1985
1986void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001987 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001988
Vladimir Markoca6fff82017-10-03 14:49:14 +01001989 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001990 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001991 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001992 case DataType::Type::kInt32:
1993 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001994 locations->SetInAt(0, Location::RequiresRegister());
1995 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001997 break;
1998 }
1999 default:
2000 LOG(FATAL) << "Unexpected shift type " << type;
2001 }
2002}
2003
2004void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002005 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002006 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002008
2009 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 case DataType::Type::kInt32:
2011 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002012 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2013 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2014 Location rhs_location = locations->InAt(1);
2015
2016 GpuRegister rhs_reg = ZERO;
2017 int64_t rhs_imm = 0;
2018 bool use_imm = rhs_location.IsConstant();
2019 if (use_imm) {
2020 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2021 } else {
2022 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2023 }
2024
2025 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002026 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002028
Alexey Frunze92d90602015-12-18 18:16:36 -08002029 if (shift_value == 0) {
2030 if (dst != lhs) {
2031 __ Move(dst, lhs);
2032 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002034 if (instr->IsShl()) {
2035 __ Sll(dst, lhs, shift_value);
2036 } else if (instr->IsShr()) {
2037 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002038 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002039 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002040 } else {
2041 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002042 }
2043 } else {
2044 if (shift_value < 32) {
2045 if (instr->IsShl()) {
2046 __ Dsll(dst, lhs, shift_value);
2047 } else if (instr->IsShr()) {
2048 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002049 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002051 } else {
2052 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002053 }
2054 } else {
2055 shift_value -= 32;
2056 if (instr->IsShl()) {
2057 __ Dsll32(dst, lhs, shift_value);
2058 } else if (instr->IsShr()) {
2059 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002060 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002062 } else {
2063 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002064 }
2065 }
2066 }
2067 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002069 if (instr->IsShl()) {
2070 __ Sllv(dst, lhs, rhs_reg);
2071 } else if (instr->IsShr()) {
2072 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002073 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002075 } else {
2076 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002077 }
2078 } else {
2079 if (instr->IsShl()) {
2080 __ Dsllv(dst, lhs, rhs_reg);
2081 } else if (instr->IsShr()) {
2082 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002083 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002084 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002085 } else {
2086 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002087 }
2088 }
2089 }
2090 break;
2091 }
2092 default:
2093 LOG(FATAL) << "Unexpected shift operation type " << type;
2094 }
2095}
2096
2097void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2098 HandleBinaryOp(instruction);
2099}
2100
2101void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2102 HandleBinaryOp(instruction);
2103}
2104
2105void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2106 HandleBinaryOp(instruction);
2107}
2108
2109void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2110 HandleBinaryOp(instruction);
2111}
2112
2113void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002115 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002117 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002118 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2119 object_array_get_with_read_barrier
2120 ? LocationSummary::kCallOnSlowPath
2121 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002122 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2123 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2124 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002125 locations->SetInAt(0, Location::RequiresRegister());
2126 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002128 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2129 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002130 // The output overlaps in the case of an object array get with
2131 // read barriers enabled: we do not want the move to overwrite the
2132 // array's location, as we need it to emit the read barrier.
2133 locations->SetOut(Location::RequiresRegister(),
2134 object_array_get_with_read_barrier
2135 ? Location::kOutputOverlap
2136 : Location::kNoOutputOverlap);
2137 }
2138 // We need a temporary register for the read barrier marking slow
2139 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2140 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002141 bool temp_needed = instruction->GetIndex()->IsConstant()
2142 ? !kBakerReadBarrierThunksEnableForFields
2143 : !kBakerReadBarrierThunksEnableForArrays;
2144 if (temp_needed) {
2145 locations->AddTemp(Location::RequiresRegister());
2146 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002147 }
2148}
2149
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002150static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2151 auto null_checker = [codegen, instruction]() {
2152 codegen->MaybeRecordImplicitNullCheck(instruction);
2153 };
2154 return null_checker;
2155}
2156
Alexey Frunze4dda3372015-06-01 18:31:49 -07002157void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2158 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002159 Location obj_loc = locations->InAt(0);
2160 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2161 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002162 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002163 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002164 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002166 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002167 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2168 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002169 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002170 case DataType::Type::kBool:
2171 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002172 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002173 if (index.IsConstant()) {
2174 size_t offset =
2175 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002176 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002177 } else {
2178 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002179 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002180 }
2181 break;
2182 }
2183
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002185 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002186 if (index.IsConstant()) {
2187 size_t offset =
2188 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002189 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 } else {
2191 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002192 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002193 }
2194 break;
2195 }
2196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002197 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002198 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002199 if (maybe_compressed_char_at) {
2200 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002201 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002202 __ Dext(TMP, TMP, 0, 1);
2203 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2204 "Expecting 0=compressed, 1=uncompressed");
2205 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002206 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002207 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2208 if (maybe_compressed_char_at) {
2209 Mips64Label uncompressed_load, done;
2210 __ Bnezc(TMP, &uncompressed_load);
2211 __ LoadFromOffset(kLoadUnsignedByte,
2212 out,
2213 obj,
2214 data_offset + (const_index << TIMES_1));
2215 __ Bc(&done);
2216 __ Bind(&uncompressed_load);
2217 __ LoadFromOffset(kLoadUnsignedHalfword,
2218 out,
2219 obj,
2220 data_offset + (const_index << TIMES_2));
2221 __ Bind(&done);
2222 } else {
2223 __ LoadFromOffset(kLoadUnsignedHalfword,
2224 out,
2225 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002226 data_offset + (const_index << TIMES_2),
2227 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002228 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002229 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002230 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2231 if (maybe_compressed_char_at) {
2232 Mips64Label uncompressed_load, done;
2233 __ Bnezc(TMP, &uncompressed_load);
2234 __ Daddu(TMP, obj, index_reg);
2235 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2236 __ Bc(&done);
2237 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002238 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002239 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2240 __ Bind(&done);
2241 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002242 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002243 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002244 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002245 }
2246 break;
2247 }
2248
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002249 case DataType::Type::kInt16: {
2250 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2251 if (index.IsConstant()) {
2252 size_t offset =
2253 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2254 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2255 } else {
2256 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2257 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2258 }
2259 break;
2260 }
2261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002263 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002264 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002265 LoadOperandType load_type =
2266 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 if (index.IsConstant()) {
2268 size_t offset =
2269 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002270 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002271 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002272 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002273 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002274 }
2275 break;
2276 }
2277
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002279 static_assert(
2280 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2281 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2282 // /* HeapReference<Object> */ out =
2283 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2284 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002285 bool temp_needed = index.IsConstant()
2286 ? !kBakerReadBarrierThunksEnableForFields
2287 : !kBakerReadBarrierThunksEnableForArrays;
2288 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002289 // Note that a potential implicit null check is handled in this
2290 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002291 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2292 if (index.IsConstant()) {
2293 // Array load with a constant index can be treated as a field load.
2294 size_t offset =
2295 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2296 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2297 out_loc,
2298 obj,
2299 offset,
2300 temp,
2301 /* needs_null_check */ false);
2302 } else {
2303 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2304 out_loc,
2305 obj,
2306 data_offset,
2307 index,
2308 temp,
2309 /* needs_null_check */ false);
2310 }
Alexey Frunze15958152017-02-09 19:08:30 -08002311 } else {
2312 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2313 if (index.IsConstant()) {
2314 size_t offset =
2315 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2316 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2317 // If read barriers are enabled, emit read barriers other than
2318 // Baker's using a slow path (and also unpoison the loaded
2319 // reference, if heap poisoning is enabled).
2320 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2321 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002322 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002323 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2324 // If read barriers are enabled, emit read barriers other than
2325 // Baker's using a slow path (and also unpoison the loaded
2326 // reference, if heap poisoning is enabled).
2327 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2328 out_loc,
2329 out_loc,
2330 obj_loc,
2331 data_offset,
2332 index);
2333 }
2334 }
2335 break;
2336 }
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002339 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002340 if (index.IsConstant()) {
2341 size_t offset =
2342 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002343 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002344 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002345 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002346 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002347 }
2348 break;
2349 }
2350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002352 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002353 if (index.IsConstant()) {
2354 size_t offset =
2355 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002356 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002357 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002358 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002359 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002360 }
2361 break;
2362 }
2363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002364 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002365 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002366 if (index.IsConstant()) {
2367 size_t offset =
2368 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002369 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002370 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002371 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002372 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002373 }
2374 break;
2375 }
2376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002378 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2379 UNREACHABLE();
2380 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002381}
2382
2383void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002384 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002385 locations->SetInAt(0, Location::RequiresRegister());
2386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2387}
2388
2389void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2390 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002391 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002392 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2393 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2394 __ LoadFromOffset(kLoadWord, out, obj, offset);
2395 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002396 // Mask out compression flag from String's array length.
2397 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2398 __ Srl(out, out, 1u);
2399 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400}
2401
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002402Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2403 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2404 ? Location::ConstantLocation(instruction->AsConstant())
2405 : Location::RequiresRegister();
2406}
2407
2408Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2409 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2410 // We can store a non-zero float or double constant without first loading it into the FPU,
2411 // but we should only prefer this if the constant has a single use.
2412 if (instruction->IsConstant() &&
2413 (instruction->AsConstant()->IsZeroBitPattern() ||
2414 instruction->GetUses().HasExactlyOneElement())) {
2415 return Location::ConstantLocation(instruction->AsConstant());
2416 // Otherwise fall through and require an FPU register for the constant.
2417 }
2418 return Location::RequiresFpuRegister();
2419}
2420
Alexey Frunze4dda3372015-06-01 18:31:49 -07002421void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002423
2424 bool needs_write_barrier =
2425 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2426 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2427
Vladimir Markoca6fff82017-10-03 14:49:14 +01002428 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002429 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002430 may_need_runtime_call_for_type_check ?
2431 LocationSummary::kCallOnSlowPath :
2432 LocationSummary::kNoCall);
2433
2434 locations->SetInAt(0, Location::RequiresRegister());
2435 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002437 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002438 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002439 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2440 }
2441 if (needs_write_barrier) {
2442 // Temporary register for the write barrier.
2443 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002444 }
2445}
2446
2447void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2448 LocationSummary* locations = instruction->GetLocations();
2449 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2450 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002451 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002453 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002454 bool needs_write_barrier =
2455 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002456 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002457 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002458
2459 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002460 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002461 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002463 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002464 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002465 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002466 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002467 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2468 }
2469 if (value_location.IsConstant()) {
2470 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2471 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2472 } else {
2473 GpuRegister value = value_location.AsRegister<GpuRegister>();
2474 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002475 }
2476 break;
2477 }
2478
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002479 case DataType::Type::kUint16:
2480 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002482 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002483 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002484 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002485 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002486 }
2487 if (value_location.IsConstant()) {
2488 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2489 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2490 } else {
2491 GpuRegister value = value_location.AsRegister<GpuRegister>();
2492 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002493 }
2494 break;
2495 }
2496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002497 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002498 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2499 if (index.IsConstant()) {
2500 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2501 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002502 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002503 }
2504 if (value_location.IsConstant()) {
2505 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2506 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2507 } else {
2508 GpuRegister value = value_location.AsRegister<GpuRegister>();
2509 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2510 }
2511 break;
2512 }
2513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002514 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002515 if (value_location.IsConstant()) {
2516 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002519 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002520 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002521 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002522 }
Alexey Frunze15958152017-02-09 19:08:30 -08002523 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2524 DCHECK_EQ(value, 0);
2525 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2526 DCHECK(!needs_write_barrier);
2527 DCHECK(!may_need_runtime_call_for_type_check);
2528 break;
2529 }
2530
2531 DCHECK(needs_write_barrier);
2532 GpuRegister value = value_location.AsRegister<GpuRegister>();
2533 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2534 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2535 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2536 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2537 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2538 Mips64Label done;
2539 SlowPathCodeMIPS64* slow_path = nullptr;
2540
2541 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002542 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002543 codegen_->AddSlowPath(slow_path);
2544 if (instruction->GetValueCanBeNull()) {
2545 Mips64Label non_zero;
2546 __ Bnezc(value, &non_zero);
2547 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2548 if (index.IsConstant()) {
2549 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002550 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002551 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002552 }
Alexey Frunze15958152017-02-09 19:08:30 -08002553 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2554 __ Bc(&done);
2555 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002556 }
Alexey Frunze15958152017-02-09 19:08:30 -08002557
2558 // Note that when read barriers are enabled, the type checks
2559 // are performed without read barriers. This is fine, even in
2560 // the case where a class object is in the from-space after
2561 // the flip, as a comparison involving such a type would not
2562 // produce a false positive; it may of course produce a false
2563 // negative, in which case we would take the ArraySet slow
2564 // path.
2565
2566 // /* HeapReference<Class> */ temp1 = obj->klass_
2567 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2568 __ MaybeUnpoisonHeapReference(temp1);
2569
2570 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2571 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2572 // /* HeapReference<Class> */ temp2 = value->klass_
2573 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2574 // If heap poisoning is enabled, no need to unpoison `temp1`
2575 // nor `temp2`, as we are comparing two poisoned references.
2576
2577 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2578 Mips64Label do_put;
2579 __ Beqc(temp1, temp2, &do_put);
2580 // If heap poisoning is enabled, the `temp1` reference has
2581 // not been unpoisoned yet; unpoison it now.
2582 __ MaybeUnpoisonHeapReference(temp1);
2583
2584 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2585 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2586 // If heap poisoning is enabled, no need to unpoison
2587 // `temp1`, as we are comparing against null below.
2588 __ Bnezc(temp1, slow_path->GetEntryLabel());
2589 __ Bind(&do_put);
2590 } else {
2591 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2592 }
2593 }
2594
2595 GpuRegister source = value;
2596 if (kPoisonHeapReferences) {
2597 // Note that in the case where `value` is a null reference,
2598 // we do not enter this block, as a null reference does not
2599 // need poisoning.
2600 __ Move(temp1, value);
2601 __ PoisonHeapReference(temp1);
2602 source = temp1;
2603 }
2604
2605 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2606 if (index.IsConstant()) {
2607 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002608 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002609 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002610 }
2611 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2612
2613 if (!may_need_runtime_call_for_type_check) {
2614 codegen_->MaybeRecordImplicitNullCheck(instruction);
2615 }
2616
2617 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2618
2619 if (done.IsLinked()) {
2620 __ Bind(&done);
2621 }
2622
2623 if (slow_path != nullptr) {
2624 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002625 }
2626 break;
2627 }
2628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002629 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002630 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002631 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002632 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002633 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002634 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002635 }
2636 if (value_location.IsConstant()) {
2637 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2638 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2639 } else {
2640 GpuRegister value = value_location.AsRegister<GpuRegister>();
2641 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002642 }
2643 break;
2644 }
2645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002647 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002648 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002649 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002650 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002651 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002652 }
2653 if (value_location.IsConstant()) {
2654 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2655 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2656 } else {
2657 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2658 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002659 }
2660 break;
2661 }
2662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002664 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002665 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002666 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002667 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002668 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002669 }
2670 if (value_location.IsConstant()) {
2671 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2672 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2673 } else {
2674 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2675 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002676 }
2677 break;
2678 }
2679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002681 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2682 UNREACHABLE();
2683 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002684}
2685
2686void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002687 RegisterSet caller_saves = RegisterSet::Empty();
2688 InvokeRuntimeCallingConvention calling_convention;
2689 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2690 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2691 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002692 locations->SetInAt(0, Location::RequiresRegister());
2693 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002694}
2695
2696void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2697 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002698 BoundsCheckSlowPathMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002699 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002700 codegen_->AddSlowPath(slow_path);
2701
2702 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2703 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2704
2705 // length is limited by the maximum positive signed 32-bit integer.
2706 // Unsigned comparison of length and index checks for index < 0
2707 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002708 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002709}
2710
Alexey Frunze15958152017-02-09 19:08:30 -08002711// Temp is used for read barrier.
2712static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2713 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002714 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002715 (kUseBakerReadBarrier ||
2716 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2717 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2718 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2719 return 1;
2720 }
2721 return 0;
2722}
2723
2724// Extra temp is used for read barrier.
2725static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2726 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2727}
2728
Alexey Frunze4dda3372015-06-01 18:31:49 -07002729void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002730 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2731 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2732
2733 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2734 switch (type_check_kind) {
2735 case TypeCheckKind::kExactCheck:
2736 case TypeCheckKind::kAbstractClassCheck:
2737 case TypeCheckKind::kClassHierarchyCheck:
2738 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002739 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002740 ? LocationSummary::kCallOnSlowPath
2741 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2742 break;
2743 case TypeCheckKind::kArrayCheck:
2744 case TypeCheckKind::kUnresolvedCheck:
2745 case TypeCheckKind::kInterfaceCheck:
2746 call_kind = LocationSummary::kCallOnSlowPath;
2747 break;
2748 }
2749
Vladimir Markoca6fff82017-10-03 14:49:14 +01002750 LocationSummary* locations =
2751 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002752 locations->SetInAt(0, Location::RequiresRegister());
2753 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002754 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002755}
2756
2757void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002758 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002759 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002760 Location obj_loc = locations->InAt(0);
2761 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002762 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002763 Location temp_loc = locations->GetTemp(0);
2764 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2765 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2766 DCHECK_LE(num_temps, 2u);
2767 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002768 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2769 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2770 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2771 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2772 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2773 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2774 const uint32_t object_array_data_offset =
2775 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2776 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002777
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002778 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2779 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2780 // read barriers is done for performance and code size reasons.
2781 bool is_type_check_slow_path_fatal = false;
2782 if (!kEmitCompilerReadBarrier) {
2783 is_type_check_slow_path_fatal =
2784 (type_check_kind == TypeCheckKind::kExactCheck ||
2785 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2786 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2787 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2788 !instruction->CanThrowIntoCatchBlock();
2789 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002790 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002791 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2792 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002793 codegen_->AddSlowPath(slow_path);
2794
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002795 // Avoid this check if we know `obj` is not null.
2796 if (instruction->MustDoNullCheck()) {
2797 __ Beqzc(obj, &done);
2798 }
2799
2800 switch (type_check_kind) {
2801 case TypeCheckKind::kExactCheck:
2802 case TypeCheckKind::kArrayCheck: {
2803 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002804 GenerateReferenceLoadTwoRegisters(instruction,
2805 temp_loc,
2806 obj_loc,
2807 class_offset,
2808 maybe_temp2_loc,
2809 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002810 // Jump to slow path for throwing the exception or doing a
2811 // more involved array check.
2812 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2813 break;
2814 }
2815
2816 case TypeCheckKind::kAbstractClassCheck: {
2817 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002818 GenerateReferenceLoadTwoRegisters(instruction,
2819 temp_loc,
2820 obj_loc,
2821 class_offset,
2822 maybe_temp2_loc,
2823 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002824 // If the class is abstract, we eagerly fetch the super class of the
2825 // object to avoid doing a comparison we know will fail.
2826 Mips64Label loop;
2827 __ Bind(&loop);
2828 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002829 GenerateReferenceLoadOneRegister(instruction,
2830 temp_loc,
2831 super_offset,
2832 maybe_temp2_loc,
2833 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002834 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2835 // exception.
2836 __ Beqzc(temp, slow_path->GetEntryLabel());
2837 // Otherwise, compare the classes.
2838 __ Bnec(temp, cls, &loop);
2839 break;
2840 }
2841
2842 case TypeCheckKind::kClassHierarchyCheck: {
2843 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002844 GenerateReferenceLoadTwoRegisters(instruction,
2845 temp_loc,
2846 obj_loc,
2847 class_offset,
2848 maybe_temp2_loc,
2849 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002850 // Walk over the class hierarchy to find a match.
2851 Mips64Label loop;
2852 __ Bind(&loop);
2853 __ Beqc(temp, cls, &done);
2854 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002855 GenerateReferenceLoadOneRegister(instruction,
2856 temp_loc,
2857 super_offset,
2858 maybe_temp2_loc,
2859 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002860 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2861 // exception. Otherwise, jump to the beginning of the loop.
2862 __ Bnezc(temp, &loop);
2863 __ Bc(slow_path->GetEntryLabel());
2864 break;
2865 }
2866
2867 case TypeCheckKind::kArrayObjectCheck: {
2868 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002869 GenerateReferenceLoadTwoRegisters(instruction,
2870 temp_loc,
2871 obj_loc,
2872 class_offset,
2873 maybe_temp2_loc,
2874 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002875 // Do an exact check.
2876 __ Beqc(temp, cls, &done);
2877 // Otherwise, we need to check that the object's class is a non-primitive array.
2878 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002879 GenerateReferenceLoadOneRegister(instruction,
2880 temp_loc,
2881 component_offset,
2882 maybe_temp2_loc,
2883 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002884 // If the component type is null, jump to the slow path to throw the exception.
2885 __ Beqzc(temp, slow_path->GetEntryLabel());
2886 // Otherwise, the object is indeed an array, further check that this component
2887 // type is not a primitive type.
2888 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2889 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2890 __ Bnezc(temp, slow_path->GetEntryLabel());
2891 break;
2892 }
2893
2894 case TypeCheckKind::kUnresolvedCheck:
2895 // We always go into the type check slow path for the unresolved check case.
2896 // We cannot directly call the CheckCast runtime entry point
2897 // without resorting to a type checking slow path here (i.e. by
2898 // calling InvokeRuntime directly), as it would require to
2899 // assign fixed registers for the inputs of this HInstanceOf
2900 // instruction (following the runtime calling convention), which
2901 // might be cluttered by the potential first read barrier
2902 // emission at the beginning of this method.
2903 __ Bc(slow_path->GetEntryLabel());
2904 break;
2905
2906 case TypeCheckKind::kInterfaceCheck: {
2907 // Avoid read barriers to improve performance of the fast path. We can not get false
2908 // positives by doing this.
2909 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002910 GenerateReferenceLoadTwoRegisters(instruction,
2911 temp_loc,
2912 obj_loc,
2913 class_offset,
2914 maybe_temp2_loc,
2915 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002916 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002917 GenerateReferenceLoadTwoRegisters(instruction,
2918 temp_loc,
2919 temp_loc,
2920 iftable_offset,
2921 maybe_temp2_loc,
2922 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002923 // Iftable is never null.
2924 __ Lw(TMP, temp, array_length_offset);
2925 // Loop through the iftable and check if any class matches.
2926 Mips64Label loop;
2927 __ Bind(&loop);
2928 __ Beqzc(TMP, slow_path->GetEntryLabel());
2929 __ Lwu(AT, temp, object_array_data_offset);
2930 __ MaybeUnpoisonHeapReference(AT);
2931 // Go to next interface.
2932 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2933 __ Addiu(TMP, TMP, -2);
2934 // Compare the classes and continue the loop if they do not match.
2935 __ Bnec(AT, cls, &loop);
2936 break;
2937 }
2938 }
2939
2940 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941 __ Bind(slow_path->GetExitLabel());
2942}
2943
2944void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2945 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002946 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002947 locations->SetInAt(0, Location::RequiresRegister());
2948 if (check->HasUses()) {
2949 locations->SetOut(Location::SameAsFirstInput());
2950 }
2951}
2952
2953void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2954 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01002955 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002956 check->GetLoadClass(),
2957 check,
2958 check->GetDexPc(),
2959 true);
2960 codegen_->AddSlowPath(slow_path);
2961 GenerateClassInitializationCheck(slow_path,
2962 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2963}
2964
2965void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002967
Vladimir Markoca6fff82017-10-03 14:49:14 +01002968 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002969
2970 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002971 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002972 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002975 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kInt32:
2977 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002978 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002979 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2981 break;
2982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kFloat32:
2984 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08002985 locations->SetInAt(0, Location::RequiresFpuRegister());
2986 locations->SetInAt(1, Location::RequiresFpuRegister());
2987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002988 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002989
2990 default:
2991 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2992 }
2993}
2994
2995void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2996 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002997 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002999
3000 // 0 if: left == right
3001 // 1 if: left > right
3002 // -1 if: left < right
3003 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003005 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003006 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003008 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003009 case DataType::Type::kInt32:
3010 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003011 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003012 Location rhs_location = locations->InAt(1);
3013 bool use_imm = rhs_location.IsConstant();
3014 GpuRegister rhs = ZERO;
3015 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003017 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3018 if (value != 0) {
3019 rhs = AT;
3020 __ LoadConst64(rhs, value);
3021 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003022 } else {
3023 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3024 if (value != 0) {
3025 rhs = AT;
3026 __ LoadConst32(rhs, value);
3027 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003028 }
3029 } else {
3030 rhs = rhs_location.AsRegister<GpuRegister>();
3031 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003032 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003033 __ Slt(res, rhs, lhs);
3034 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003035 break;
3036 }
3037
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003038 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003039 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3040 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3041 Mips64Label done;
3042 __ CmpEqS(FTMP, lhs, rhs);
3043 __ LoadConst32(res, 0);
3044 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003045 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003046 __ CmpLtS(FTMP, lhs, rhs);
3047 __ LoadConst32(res, -1);
3048 __ Bc1nez(FTMP, &done);
3049 __ LoadConst32(res, 1);
3050 } else {
3051 __ CmpLtS(FTMP, rhs, lhs);
3052 __ LoadConst32(res, 1);
3053 __ Bc1nez(FTMP, &done);
3054 __ LoadConst32(res, -1);
3055 }
3056 __ Bind(&done);
3057 break;
3058 }
3059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003061 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3062 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3063 Mips64Label done;
3064 __ CmpEqD(FTMP, lhs, rhs);
3065 __ LoadConst32(res, 0);
3066 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003067 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003068 __ CmpLtD(FTMP, lhs, rhs);
3069 __ LoadConst32(res, -1);
3070 __ Bc1nez(FTMP, &done);
3071 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003072 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003073 __ CmpLtD(FTMP, rhs, lhs);
3074 __ LoadConst32(res, 1);
3075 __ Bc1nez(FTMP, &done);
3076 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003077 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003078 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003079 break;
3080 }
3081
3082 default:
3083 LOG(FATAL) << "Unimplemented compare type " << in_type;
3084 }
3085}
3086
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003087void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003088 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003089 switch (instruction->InputAt(0)->GetType()) {
3090 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003092 locations->SetInAt(0, Location::RequiresRegister());
3093 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3094 break;
3095
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 case DataType::Type::kFloat32:
3097 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003098 locations->SetInAt(0, Location::RequiresFpuRegister());
3099 locations->SetInAt(1, Location::RequiresFpuRegister());
3100 break;
3101 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003102 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3104 }
3105}
3106
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003107void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003108 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003109 return;
3110 }
3111
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003113 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003114 switch (type) {
3115 default:
3116 // Integer case.
3117 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3118 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003119 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003120 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3121 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kFloat32:
3123 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003124 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3125 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003126 }
3127}
3128
Alexey Frunzec857c742015-09-23 15:12:39 -07003129void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3130 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003131 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003132
3133 LocationSummary* locations = instruction->GetLocations();
3134 Location second = locations->InAt(1);
3135 DCHECK(second.IsConstant());
3136
3137 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3138 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3139 int64_t imm = Int64FromConstant(second.GetConstant());
3140 DCHECK(imm == 1 || imm == -1);
3141
3142 if (instruction->IsRem()) {
3143 __ Move(out, ZERO);
3144 } else {
3145 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003147 __ Subu(out, ZERO, dividend);
3148 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003150 __ Dsubu(out, ZERO, dividend);
3151 }
3152 } else if (out != dividend) {
3153 __ Move(out, dividend);
3154 }
3155 }
3156}
3157
3158void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3159 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003161
3162 LocationSummary* locations = instruction->GetLocations();
3163 Location second = locations->InAt(1);
3164 DCHECK(second.IsConstant());
3165
3166 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3167 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3168 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003169 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003170 int ctz_imm = CTZ(abs_imm);
3171
3172 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003173 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003174 if (ctz_imm == 1) {
3175 // Fast path for division by +/-2, which is very common.
3176 __ Srl(TMP, dividend, 31);
3177 } else {
3178 __ Sra(TMP, dividend, 31);
3179 __ Srl(TMP, TMP, 32 - ctz_imm);
3180 }
3181 __ Addu(out, dividend, TMP);
3182 __ Sra(out, out, ctz_imm);
3183 if (imm < 0) {
3184 __ Subu(out, ZERO, out);
3185 }
3186 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003187 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003188 if (ctz_imm == 1) {
3189 // Fast path for division by +/-2, which is very common.
3190 __ Dsrl32(TMP, dividend, 31);
3191 } else {
3192 __ Dsra32(TMP, dividend, 31);
3193 if (ctz_imm > 32) {
3194 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3195 } else {
3196 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3197 }
3198 }
3199 __ Daddu(out, dividend, TMP);
3200 if (ctz_imm < 32) {
3201 __ Dsra(out, out, ctz_imm);
3202 } else {
3203 __ Dsra32(out, out, ctz_imm - 32);
3204 }
3205 if (imm < 0) {
3206 __ Dsubu(out, ZERO, out);
3207 }
3208 }
3209 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003211 if (ctz_imm == 1) {
3212 // Fast path for modulo +/-2, which is very common.
3213 __ Sra(TMP, dividend, 31);
3214 __ Subu(out, dividend, TMP);
3215 __ Andi(out, out, 1);
3216 __ Addu(out, out, TMP);
3217 } else {
3218 __ Sra(TMP, dividend, 31);
3219 __ Srl(TMP, TMP, 32 - ctz_imm);
3220 __ Addu(out, dividend, TMP);
3221 if (IsUint<16>(abs_imm - 1)) {
3222 __ Andi(out, out, abs_imm - 1);
3223 } else {
3224 __ Sll(out, out, 32 - ctz_imm);
3225 __ Srl(out, out, 32 - ctz_imm);
3226 }
3227 __ Subu(out, out, TMP);
3228 }
3229 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003231 if (ctz_imm == 1) {
3232 // Fast path for modulo +/-2, which is very common.
3233 __ Dsra32(TMP, dividend, 31);
3234 __ Dsubu(out, dividend, TMP);
3235 __ Andi(out, out, 1);
3236 __ Daddu(out, out, TMP);
3237 } else {
3238 __ Dsra32(TMP, dividend, 31);
3239 if (ctz_imm > 32) {
3240 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3241 } else {
3242 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3243 }
3244 __ Daddu(out, dividend, TMP);
3245 if (IsUint<16>(abs_imm - 1)) {
3246 __ Andi(out, out, abs_imm - 1);
3247 } else {
3248 if (ctz_imm > 32) {
3249 __ Dsll(out, out, 64 - ctz_imm);
3250 __ Dsrl(out, out, 64 - ctz_imm);
3251 } else {
3252 __ Dsll32(out, out, 32 - ctz_imm);
3253 __ Dsrl32(out, out, 32 - ctz_imm);
3254 }
3255 }
3256 __ Dsubu(out, out, TMP);
3257 }
3258 }
3259 }
3260}
3261
3262void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3263 DCHECK(instruction->IsDiv() || instruction->IsRem());
3264
3265 LocationSummary* locations = instruction->GetLocations();
3266 Location second = locations->InAt(1);
3267 DCHECK(second.IsConstant());
3268
3269 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3270 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3271 int64_t imm = Int64FromConstant(second.GetConstant());
3272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003273 DataType::Type type = instruction->GetResultType();
3274 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003275
3276 int64_t magic;
3277 int shift;
3278 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003279 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003280 &magic,
3281 &shift);
3282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003283 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003284 __ LoadConst32(TMP, magic);
3285 __ MuhR6(TMP, dividend, TMP);
3286
3287 if (imm > 0 && magic < 0) {
3288 __ Addu(TMP, TMP, dividend);
3289 } else if (imm < 0 && magic > 0) {
3290 __ Subu(TMP, TMP, dividend);
3291 }
3292
3293 if (shift != 0) {
3294 __ Sra(TMP, TMP, shift);
3295 }
3296
3297 if (instruction->IsDiv()) {
3298 __ Sra(out, TMP, 31);
3299 __ Subu(out, TMP, out);
3300 } else {
3301 __ Sra(AT, TMP, 31);
3302 __ Subu(AT, TMP, AT);
3303 __ LoadConst32(TMP, imm);
3304 __ MulR6(TMP, AT, TMP);
3305 __ Subu(out, dividend, TMP);
3306 }
3307 } else {
3308 __ LoadConst64(TMP, magic);
3309 __ Dmuh(TMP, dividend, TMP);
3310
3311 if (imm > 0 && magic < 0) {
3312 __ Daddu(TMP, TMP, dividend);
3313 } else if (imm < 0 && magic > 0) {
3314 __ Dsubu(TMP, TMP, dividend);
3315 }
3316
3317 if (shift >= 32) {
3318 __ Dsra32(TMP, TMP, shift - 32);
3319 } else if (shift > 0) {
3320 __ Dsra(TMP, TMP, shift);
3321 }
3322
3323 if (instruction->IsDiv()) {
3324 __ Dsra32(out, TMP, 31);
3325 __ Dsubu(out, TMP, out);
3326 } else {
3327 __ Dsra32(AT, TMP, 31);
3328 __ Dsubu(AT, TMP, AT);
3329 __ LoadConst64(TMP, imm);
3330 __ Dmul(TMP, AT, TMP);
3331 __ Dsubu(out, dividend, TMP);
3332 }
3333 }
3334}
3335
3336void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3337 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 DataType::Type type = instruction->GetResultType();
3339 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003340
3341 LocationSummary* locations = instruction->GetLocations();
3342 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3343 Location second = locations->InAt(1);
3344
3345 if (second.IsConstant()) {
3346 int64_t imm = Int64FromConstant(second.GetConstant());
3347 if (imm == 0) {
3348 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3349 } else if (imm == 1 || imm == -1) {
3350 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003351 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003352 DivRemByPowerOfTwo(instruction);
3353 } else {
3354 DCHECK(imm <= -2 || imm >= 2);
3355 GenerateDivRemWithAnyConstant(instruction);
3356 }
3357 } else {
3358 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3359 GpuRegister divisor = second.AsRegister<GpuRegister>();
3360 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003361 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003362 __ DivR6(out, dividend, divisor);
3363 else
3364 __ Ddiv(out, dividend, divisor);
3365 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003366 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003367 __ ModR6(out, dividend, divisor);
3368 else
3369 __ Dmod(out, dividend, divisor);
3370 }
3371 }
3372}
3373
Alexey Frunze4dda3372015-06-01 18:31:49 -07003374void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3375 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003376 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003377 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003378 case DataType::Type::kInt32:
3379 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003380 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003381 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3383 break;
3384
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003385 case DataType::Type::kFloat32:
3386 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003387 locations->SetInAt(0, Location::RequiresFpuRegister());
3388 locations->SetInAt(1, Location::RequiresFpuRegister());
3389 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3390 break;
3391
3392 default:
3393 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3394 }
3395}
3396
3397void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003398 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003399 LocationSummary* locations = instruction->GetLocations();
3400
3401 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 case DataType::Type::kInt32:
3403 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003404 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003405 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003406 case DataType::Type::kFloat32:
3407 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003408 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3409 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3410 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003411 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003412 __ DivS(dst, lhs, rhs);
3413 else
3414 __ DivD(dst, lhs, rhs);
3415 break;
3416 }
3417 default:
3418 LOG(FATAL) << "Unexpected div type " << type;
3419 }
3420}
3421
3422void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003423 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003424 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003425}
3426
3427void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3428 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003429 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003430 codegen_->AddSlowPath(slow_path);
3431 Location value = instruction->GetLocations()->InAt(0);
3432
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003433 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003434
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003435 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003436 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003437 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003438 }
3439
3440 if (value.IsConstant()) {
3441 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3442 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003443 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003444 } else {
3445 // A division by a non-null constant is valid. We don't need to perform
3446 // any check, so simply fall through.
3447 }
3448 } else {
3449 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3450 }
3451}
3452
3453void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3454 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003455 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003456 locations->SetOut(Location::ConstantLocation(constant));
3457}
3458
3459void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3460 // Will be generated at use site.
3461}
3462
3463void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3464 exit->SetLocations(nullptr);
3465}
3466
3467void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3468}
3469
3470void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003472 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003473 locations->SetOut(Location::ConstantLocation(constant));
3474}
3475
3476void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3477 // Will be generated at use site.
3478}
3479
David Brazdilfc6a86a2015-06-26 10:33:45 +00003480void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003481 DCHECK(!successor->IsExitBlock());
3482 HBasicBlock* block = got->GetBlock();
3483 HInstruction* previous = got->GetPrevious();
3484 HLoopInformation* info = block->GetLoopInformation();
3485
3486 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003487 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3488 return;
3489 }
3490 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3491 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3492 }
3493 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003494 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003495 }
3496}
3497
David Brazdilfc6a86a2015-06-26 10:33:45 +00003498void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3499 got->SetLocations(nullptr);
3500}
3501
3502void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3503 HandleGoto(got, got->GetSuccessor());
3504}
3505
3506void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3507 try_boundary->SetLocations(nullptr);
3508}
3509
3510void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3511 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3512 if (!successor->IsExitBlock()) {
3513 HandleGoto(try_boundary, successor);
3514 }
3515}
3516
Alexey Frunze299a9392015-12-08 16:08:02 -08003517void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3518 bool is64bit,
3519 LocationSummary* locations) {
3520 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3521 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3522 Location rhs_location = locations->InAt(1);
3523 GpuRegister rhs_reg = ZERO;
3524 int64_t rhs_imm = 0;
3525 bool use_imm = rhs_location.IsConstant();
3526 if (use_imm) {
3527 if (is64bit) {
3528 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3529 } else {
3530 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3531 }
3532 } else {
3533 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3534 }
3535 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3536
3537 switch (cond) {
3538 case kCondEQ:
3539 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003540 if (use_imm && IsInt<16>(-rhs_imm)) {
3541 if (rhs_imm == 0) {
3542 if (cond == kCondEQ) {
3543 __ Sltiu(dst, lhs, 1);
3544 } else {
3545 __ Sltu(dst, ZERO, lhs);
3546 }
3547 } else {
3548 if (is64bit) {
3549 __ Daddiu(dst, lhs, -rhs_imm);
3550 } else {
3551 __ Addiu(dst, lhs, -rhs_imm);
3552 }
3553 if (cond == kCondEQ) {
3554 __ Sltiu(dst, dst, 1);
3555 } else {
3556 __ Sltu(dst, ZERO, dst);
3557 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003558 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003559 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003560 if (use_imm && IsUint<16>(rhs_imm)) {
3561 __ Xori(dst, lhs, rhs_imm);
3562 } else {
3563 if (use_imm) {
3564 rhs_reg = TMP;
3565 __ LoadConst64(rhs_reg, rhs_imm);
3566 }
3567 __ Xor(dst, lhs, rhs_reg);
3568 }
3569 if (cond == kCondEQ) {
3570 __ Sltiu(dst, dst, 1);
3571 } else {
3572 __ Sltu(dst, ZERO, dst);
3573 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003574 }
3575 break;
3576
3577 case kCondLT:
3578 case kCondGE:
3579 if (use_imm && IsInt<16>(rhs_imm)) {
3580 __ Slti(dst, lhs, rhs_imm);
3581 } else {
3582 if (use_imm) {
3583 rhs_reg = TMP;
3584 __ LoadConst64(rhs_reg, rhs_imm);
3585 }
3586 __ Slt(dst, lhs, rhs_reg);
3587 }
3588 if (cond == kCondGE) {
3589 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3590 // only the slt instruction but no sge.
3591 __ Xori(dst, dst, 1);
3592 }
3593 break;
3594
3595 case kCondLE:
3596 case kCondGT:
3597 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3598 // Simulate lhs <= rhs via lhs < rhs + 1.
3599 __ Slti(dst, lhs, rhs_imm_plus_one);
3600 if (cond == kCondGT) {
3601 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3602 // only the slti instruction but no sgti.
3603 __ Xori(dst, dst, 1);
3604 }
3605 } else {
3606 if (use_imm) {
3607 rhs_reg = TMP;
3608 __ LoadConst64(rhs_reg, rhs_imm);
3609 }
3610 __ Slt(dst, rhs_reg, lhs);
3611 if (cond == kCondLE) {
3612 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3613 // only the slt instruction but no sle.
3614 __ Xori(dst, dst, 1);
3615 }
3616 }
3617 break;
3618
3619 case kCondB:
3620 case kCondAE:
3621 if (use_imm && IsInt<16>(rhs_imm)) {
3622 // Sltiu sign-extends its 16-bit immediate operand before
3623 // the comparison and thus lets us compare directly with
3624 // unsigned values in the ranges [0, 0x7fff] and
3625 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3626 __ Sltiu(dst, lhs, rhs_imm);
3627 } else {
3628 if (use_imm) {
3629 rhs_reg = TMP;
3630 __ LoadConst64(rhs_reg, rhs_imm);
3631 }
3632 __ Sltu(dst, lhs, rhs_reg);
3633 }
3634 if (cond == kCondAE) {
3635 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3636 // only the sltu instruction but no sgeu.
3637 __ Xori(dst, dst, 1);
3638 }
3639 break;
3640
3641 case kCondBE:
3642 case kCondA:
3643 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3644 // Simulate lhs <= rhs via lhs < rhs + 1.
3645 // Note that this only works if rhs + 1 does not overflow
3646 // to 0, hence the check above.
3647 // Sltiu sign-extends its 16-bit immediate operand before
3648 // the comparison and thus lets us compare directly with
3649 // unsigned values in the ranges [0, 0x7fff] and
3650 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3651 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3652 if (cond == kCondA) {
3653 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3654 // only the sltiu instruction but no sgtiu.
3655 __ Xori(dst, dst, 1);
3656 }
3657 } else {
3658 if (use_imm) {
3659 rhs_reg = TMP;
3660 __ LoadConst64(rhs_reg, rhs_imm);
3661 }
3662 __ Sltu(dst, rhs_reg, lhs);
3663 if (cond == kCondBE) {
3664 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3665 // only the sltu instruction but no sleu.
3666 __ Xori(dst, dst, 1);
3667 }
3668 }
3669 break;
3670 }
3671}
3672
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003673bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3674 bool is64bit,
3675 LocationSummary* input_locations,
3676 GpuRegister dst) {
3677 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3678 Location rhs_location = input_locations->InAt(1);
3679 GpuRegister rhs_reg = ZERO;
3680 int64_t rhs_imm = 0;
3681 bool use_imm = rhs_location.IsConstant();
3682 if (use_imm) {
3683 if (is64bit) {
3684 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3685 } else {
3686 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3687 }
3688 } else {
3689 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3690 }
3691 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3692
3693 switch (cond) {
3694 case kCondEQ:
3695 case kCondNE:
3696 if (use_imm && IsInt<16>(-rhs_imm)) {
3697 if (is64bit) {
3698 __ Daddiu(dst, lhs, -rhs_imm);
3699 } else {
3700 __ Addiu(dst, lhs, -rhs_imm);
3701 }
3702 } else if (use_imm && IsUint<16>(rhs_imm)) {
3703 __ Xori(dst, lhs, rhs_imm);
3704 } else {
3705 if (use_imm) {
3706 rhs_reg = TMP;
3707 __ LoadConst64(rhs_reg, rhs_imm);
3708 }
3709 __ Xor(dst, lhs, rhs_reg);
3710 }
3711 return (cond == kCondEQ);
3712
3713 case kCondLT:
3714 case kCondGE:
3715 if (use_imm && IsInt<16>(rhs_imm)) {
3716 __ Slti(dst, lhs, rhs_imm);
3717 } else {
3718 if (use_imm) {
3719 rhs_reg = TMP;
3720 __ LoadConst64(rhs_reg, rhs_imm);
3721 }
3722 __ Slt(dst, lhs, rhs_reg);
3723 }
3724 return (cond == kCondGE);
3725
3726 case kCondLE:
3727 case kCondGT:
3728 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3729 // Simulate lhs <= rhs via lhs < rhs + 1.
3730 __ Slti(dst, lhs, rhs_imm_plus_one);
3731 return (cond == kCondGT);
3732 } else {
3733 if (use_imm) {
3734 rhs_reg = TMP;
3735 __ LoadConst64(rhs_reg, rhs_imm);
3736 }
3737 __ Slt(dst, rhs_reg, lhs);
3738 return (cond == kCondLE);
3739 }
3740
3741 case kCondB:
3742 case kCondAE:
3743 if (use_imm && IsInt<16>(rhs_imm)) {
3744 // Sltiu sign-extends its 16-bit immediate operand before
3745 // the comparison and thus lets us compare directly with
3746 // unsigned values in the ranges [0, 0x7fff] and
3747 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3748 __ Sltiu(dst, lhs, rhs_imm);
3749 } else {
3750 if (use_imm) {
3751 rhs_reg = TMP;
3752 __ LoadConst64(rhs_reg, rhs_imm);
3753 }
3754 __ Sltu(dst, lhs, rhs_reg);
3755 }
3756 return (cond == kCondAE);
3757
3758 case kCondBE:
3759 case kCondA:
3760 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3761 // Simulate lhs <= rhs via lhs < rhs + 1.
3762 // Note that this only works if rhs + 1 does not overflow
3763 // to 0, hence the check above.
3764 // Sltiu sign-extends its 16-bit immediate operand before
3765 // the comparison and thus lets us compare directly with
3766 // unsigned values in the ranges [0, 0x7fff] and
3767 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3768 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3769 return (cond == kCondA);
3770 } else {
3771 if (use_imm) {
3772 rhs_reg = TMP;
3773 __ LoadConst64(rhs_reg, rhs_imm);
3774 }
3775 __ Sltu(dst, rhs_reg, lhs);
3776 return (cond == kCondBE);
3777 }
3778 }
3779}
3780
Alexey Frunze299a9392015-12-08 16:08:02 -08003781void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3782 bool is64bit,
3783 LocationSummary* locations,
3784 Mips64Label* label) {
3785 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3786 Location rhs_location = locations->InAt(1);
3787 GpuRegister rhs_reg = ZERO;
3788 int64_t rhs_imm = 0;
3789 bool use_imm = rhs_location.IsConstant();
3790 if (use_imm) {
3791 if (is64bit) {
3792 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3793 } else {
3794 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3795 }
3796 } else {
3797 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3798 }
3799
3800 if (use_imm && rhs_imm == 0) {
3801 switch (cond) {
3802 case kCondEQ:
3803 case kCondBE: // <= 0 if zero
3804 __ Beqzc(lhs, label);
3805 break;
3806 case kCondNE:
3807 case kCondA: // > 0 if non-zero
3808 __ Bnezc(lhs, label);
3809 break;
3810 case kCondLT:
3811 __ Bltzc(lhs, label);
3812 break;
3813 case kCondGE:
3814 __ Bgezc(lhs, label);
3815 break;
3816 case kCondLE:
3817 __ Blezc(lhs, label);
3818 break;
3819 case kCondGT:
3820 __ Bgtzc(lhs, label);
3821 break;
3822 case kCondB: // always false
3823 break;
3824 case kCondAE: // always true
3825 __ Bc(label);
3826 break;
3827 }
3828 } else {
3829 if (use_imm) {
3830 rhs_reg = TMP;
3831 __ LoadConst64(rhs_reg, rhs_imm);
3832 }
3833 switch (cond) {
3834 case kCondEQ:
3835 __ Beqc(lhs, rhs_reg, label);
3836 break;
3837 case kCondNE:
3838 __ Bnec(lhs, rhs_reg, label);
3839 break;
3840 case kCondLT:
3841 __ Bltc(lhs, rhs_reg, label);
3842 break;
3843 case kCondGE:
3844 __ Bgec(lhs, rhs_reg, label);
3845 break;
3846 case kCondLE:
3847 __ Bgec(rhs_reg, lhs, label);
3848 break;
3849 case kCondGT:
3850 __ Bltc(rhs_reg, lhs, label);
3851 break;
3852 case kCondB:
3853 __ Bltuc(lhs, rhs_reg, label);
3854 break;
3855 case kCondAE:
3856 __ Bgeuc(lhs, rhs_reg, label);
3857 break;
3858 case kCondBE:
3859 __ Bgeuc(rhs_reg, lhs, label);
3860 break;
3861 case kCondA:
3862 __ Bltuc(rhs_reg, lhs, label);
3863 break;
3864 }
3865 }
3866}
3867
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003868void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3869 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003870 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003871 LocationSummary* locations) {
3872 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3873 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3874 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003875 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003876 switch (cond) {
3877 case kCondEQ:
3878 __ CmpEqS(FTMP, lhs, rhs);
3879 __ Mfc1(dst, FTMP);
3880 __ Andi(dst, dst, 1);
3881 break;
3882 case kCondNE:
3883 __ CmpEqS(FTMP, lhs, rhs);
3884 __ Mfc1(dst, FTMP);
3885 __ Addiu(dst, dst, 1);
3886 break;
3887 case kCondLT:
3888 if (gt_bias) {
3889 __ CmpLtS(FTMP, lhs, rhs);
3890 } else {
3891 __ CmpUltS(FTMP, lhs, rhs);
3892 }
3893 __ Mfc1(dst, FTMP);
3894 __ Andi(dst, dst, 1);
3895 break;
3896 case kCondLE:
3897 if (gt_bias) {
3898 __ CmpLeS(FTMP, lhs, rhs);
3899 } else {
3900 __ CmpUleS(FTMP, lhs, rhs);
3901 }
3902 __ Mfc1(dst, FTMP);
3903 __ Andi(dst, dst, 1);
3904 break;
3905 case kCondGT:
3906 if (gt_bias) {
3907 __ CmpUltS(FTMP, rhs, lhs);
3908 } else {
3909 __ CmpLtS(FTMP, rhs, lhs);
3910 }
3911 __ Mfc1(dst, FTMP);
3912 __ Andi(dst, dst, 1);
3913 break;
3914 case kCondGE:
3915 if (gt_bias) {
3916 __ CmpUleS(FTMP, rhs, lhs);
3917 } else {
3918 __ CmpLeS(FTMP, rhs, lhs);
3919 }
3920 __ Mfc1(dst, FTMP);
3921 __ Andi(dst, dst, 1);
3922 break;
3923 default:
3924 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3925 UNREACHABLE();
3926 }
3927 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003929 switch (cond) {
3930 case kCondEQ:
3931 __ CmpEqD(FTMP, lhs, rhs);
3932 __ Mfc1(dst, FTMP);
3933 __ Andi(dst, dst, 1);
3934 break;
3935 case kCondNE:
3936 __ CmpEqD(FTMP, lhs, rhs);
3937 __ Mfc1(dst, FTMP);
3938 __ Addiu(dst, dst, 1);
3939 break;
3940 case kCondLT:
3941 if (gt_bias) {
3942 __ CmpLtD(FTMP, lhs, rhs);
3943 } else {
3944 __ CmpUltD(FTMP, lhs, rhs);
3945 }
3946 __ Mfc1(dst, FTMP);
3947 __ Andi(dst, dst, 1);
3948 break;
3949 case kCondLE:
3950 if (gt_bias) {
3951 __ CmpLeD(FTMP, lhs, rhs);
3952 } else {
3953 __ CmpUleD(FTMP, lhs, rhs);
3954 }
3955 __ Mfc1(dst, FTMP);
3956 __ Andi(dst, dst, 1);
3957 break;
3958 case kCondGT:
3959 if (gt_bias) {
3960 __ CmpUltD(FTMP, rhs, lhs);
3961 } else {
3962 __ CmpLtD(FTMP, rhs, lhs);
3963 }
3964 __ Mfc1(dst, FTMP);
3965 __ Andi(dst, dst, 1);
3966 break;
3967 case kCondGE:
3968 if (gt_bias) {
3969 __ CmpUleD(FTMP, rhs, lhs);
3970 } else {
3971 __ CmpLeD(FTMP, rhs, lhs);
3972 }
3973 __ Mfc1(dst, FTMP);
3974 __ Andi(dst, dst, 1);
3975 break;
3976 default:
3977 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3978 UNREACHABLE();
3979 }
3980 }
3981}
3982
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003983bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
3984 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003985 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003986 LocationSummary* input_locations,
3987 FpuRegister dst) {
3988 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
3989 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003990 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003991 switch (cond) {
3992 case kCondEQ:
3993 __ CmpEqS(dst, lhs, rhs);
3994 return false;
3995 case kCondNE:
3996 __ CmpEqS(dst, lhs, rhs);
3997 return true;
3998 case kCondLT:
3999 if (gt_bias) {
4000 __ CmpLtS(dst, lhs, rhs);
4001 } else {
4002 __ CmpUltS(dst, lhs, rhs);
4003 }
4004 return false;
4005 case kCondLE:
4006 if (gt_bias) {
4007 __ CmpLeS(dst, lhs, rhs);
4008 } else {
4009 __ CmpUleS(dst, lhs, rhs);
4010 }
4011 return false;
4012 case kCondGT:
4013 if (gt_bias) {
4014 __ CmpUltS(dst, rhs, lhs);
4015 } else {
4016 __ CmpLtS(dst, rhs, lhs);
4017 }
4018 return false;
4019 case kCondGE:
4020 if (gt_bias) {
4021 __ CmpUleS(dst, rhs, lhs);
4022 } else {
4023 __ CmpLeS(dst, rhs, lhs);
4024 }
4025 return false;
4026 default:
4027 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4028 UNREACHABLE();
4029 }
4030 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004031 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004032 switch (cond) {
4033 case kCondEQ:
4034 __ CmpEqD(dst, lhs, rhs);
4035 return false;
4036 case kCondNE:
4037 __ CmpEqD(dst, lhs, rhs);
4038 return true;
4039 case kCondLT:
4040 if (gt_bias) {
4041 __ CmpLtD(dst, lhs, rhs);
4042 } else {
4043 __ CmpUltD(dst, lhs, rhs);
4044 }
4045 return false;
4046 case kCondLE:
4047 if (gt_bias) {
4048 __ CmpLeD(dst, lhs, rhs);
4049 } else {
4050 __ CmpUleD(dst, lhs, rhs);
4051 }
4052 return false;
4053 case kCondGT:
4054 if (gt_bias) {
4055 __ CmpUltD(dst, rhs, lhs);
4056 } else {
4057 __ CmpLtD(dst, rhs, lhs);
4058 }
4059 return false;
4060 case kCondGE:
4061 if (gt_bias) {
4062 __ CmpUleD(dst, rhs, lhs);
4063 } else {
4064 __ CmpLeD(dst, rhs, lhs);
4065 }
4066 return false;
4067 default:
4068 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4069 UNREACHABLE();
4070 }
4071 }
4072}
4073
Alexey Frunze299a9392015-12-08 16:08:02 -08004074void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4075 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004076 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004077 LocationSummary* locations,
4078 Mips64Label* label) {
4079 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4080 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004081 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004082 switch (cond) {
4083 case kCondEQ:
4084 __ CmpEqS(FTMP, lhs, rhs);
4085 __ Bc1nez(FTMP, label);
4086 break;
4087 case kCondNE:
4088 __ CmpEqS(FTMP, lhs, rhs);
4089 __ Bc1eqz(FTMP, label);
4090 break;
4091 case kCondLT:
4092 if (gt_bias) {
4093 __ CmpLtS(FTMP, lhs, rhs);
4094 } else {
4095 __ CmpUltS(FTMP, lhs, rhs);
4096 }
4097 __ Bc1nez(FTMP, label);
4098 break;
4099 case kCondLE:
4100 if (gt_bias) {
4101 __ CmpLeS(FTMP, lhs, rhs);
4102 } else {
4103 __ CmpUleS(FTMP, lhs, rhs);
4104 }
4105 __ Bc1nez(FTMP, label);
4106 break;
4107 case kCondGT:
4108 if (gt_bias) {
4109 __ CmpUltS(FTMP, rhs, lhs);
4110 } else {
4111 __ CmpLtS(FTMP, rhs, lhs);
4112 }
4113 __ Bc1nez(FTMP, label);
4114 break;
4115 case kCondGE:
4116 if (gt_bias) {
4117 __ CmpUleS(FTMP, rhs, lhs);
4118 } else {
4119 __ CmpLeS(FTMP, rhs, lhs);
4120 }
4121 __ Bc1nez(FTMP, label);
4122 break;
4123 default:
4124 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004125 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004126 }
4127 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004129 switch (cond) {
4130 case kCondEQ:
4131 __ CmpEqD(FTMP, lhs, rhs);
4132 __ Bc1nez(FTMP, label);
4133 break;
4134 case kCondNE:
4135 __ CmpEqD(FTMP, lhs, rhs);
4136 __ Bc1eqz(FTMP, label);
4137 break;
4138 case kCondLT:
4139 if (gt_bias) {
4140 __ CmpLtD(FTMP, lhs, rhs);
4141 } else {
4142 __ CmpUltD(FTMP, lhs, rhs);
4143 }
4144 __ Bc1nez(FTMP, label);
4145 break;
4146 case kCondLE:
4147 if (gt_bias) {
4148 __ CmpLeD(FTMP, lhs, rhs);
4149 } else {
4150 __ CmpUleD(FTMP, lhs, rhs);
4151 }
4152 __ Bc1nez(FTMP, label);
4153 break;
4154 case kCondGT:
4155 if (gt_bias) {
4156 __ CmpUltD(FTMP, rhs, lhs);
4157 } else {
4158 __ CmpLtD(FTMP, rhs, lhs);
4159 }
4160 __ Bc1nez(FTMP, label);
4161 break;
4162 case kCondGE:
4163 if (gt_bias) {
4164 __ CmpUleD(FTMP, rhs, lhs);
4165 } else {
4166 __ CmpLeD(FTMP, rhs, lhs);
4167 }
4168 __ Bc1nez(FTMP, label);
4169 break;
4170 default:
4171 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004172 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004173 }
4174 }
4175}
4176
Alexey Frunze4dda3372015-06-01 18:31:49 -07004177void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004178 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004179 Mips64Label* true_target,
4180 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004181 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004182
David Brazdil0debae72015-11-12 18:37:00 +00004183 if (true_target == nullptr && false_target == nullptr) {
4184 // Nothing to do. The code always falls through.
4185 return;
4186 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004187 // Constant condition, statically compared against "true" (integer value 1).
4188 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004189 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004190 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004191 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004192 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004193 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004194 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004195 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004196 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004197 }
David Brazdil0debae72015-11-12 18:37:00 +00004198 return;
4199 }
4200
4201 // The following code generates these patterns:
4202 // (1) true_target == nullptr && false_target != nullptr
4203 // - opposite condition true => branch to false_target
4204 // (2) true_target != nullptr && false_target == nullptr
4205 // - condition true => branch to true_target
4206 // (3) true_target != nullptr && false_target != nullptr
4207 // - condition true => branch to true_target
4208 // - branch to false_target
4209 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004210 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004211 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004212 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004213 if (true_target == nullptr) {
4214 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4215 } else {
4216 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4217 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004218 } else {
4219 // The condition instruction has not been materialized, use its inputs as
4220 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004221 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004222 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004223 LocationSummary* locations = cond->GetLocations();
4224 IfCondition if_cond = condition->GetCondition();
4225 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004226
David Brazdil0debae72015-11-12 18:37:00 +00004227 if (true_target == nullptr) {
4228 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004229 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004230 }
4231
Alexey Frunze299a9392015-12-08 16:08:02 -08004232 switch (type) {
4233 default:
4234 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4235 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004236 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004237 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4238 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004239 case DataType::Type::kFloat32:
4240 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004241 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4242 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004243 }
4244 }
David Brazdil0debae72015-11-12 18:37:00 +00004245
4246 // If neither branch falls through (case 3), the conditional branch to `true_target`
4247 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4248 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004249 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004250 }
4251}
4252
4253void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004254 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004255 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004256 locations->SetInAt(0, Location::RequiresRegister());
4257 }
4258}
4259
4260void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004261 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4262 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004263 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004264 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004265 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004266 nullptr : codegen_->GetLabelOf(false_successor);
4267 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004268}
4269
4270void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004271 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004272 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004273 InvokeRuntimeCallingConvention calling_convention;
4274 RegisterSet caller_saves = RegisterSet::Empty();
4275 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4276 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004277 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004278 locations->SetInAt(0, Location::RequiresRegister());
4279 }
4280}
4281
4282void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004283 SlowPathCodeMIPS64* slow_path =
4284 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004285 GenerateTestAndBranch(deoptimize,
4286 /* condition_input_index */ 0,
4287 slow_path->GetEntryLabel(),
4288 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004289}
4290
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004291// This function returns true if a conditional move can be generated for HSelect.
4292// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4293// branches and regular moves.
4294//
4295// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4296//
4297// While determining feasibility of a conditional move and setting inputs/outputs
4298// are two distinct tasks, this function does both because they share quite a bit
4299// of common logic.
4300static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4301 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4302 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4303 HCondition* condition = cond->AsCondition();
4304
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004305 DataType::Type cond_type =
4306 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4307 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004308
4309 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4310 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4311 bool is_true_value_zero_constant =
4312 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4313 bool is_false_value_zero_constant =
4314 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4315
4316 bool can_move_conditionally = false;
4317 bool use_const_for_false_in = false;
4318 bool use_const_for_true_in = false;
4319
4320 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004321 if (!DataType::IsFloatingPointType(cond_type)) {
4322 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004323 // Moving int/long on int/long condition.
4324 if (is_true_value_zero_constant) {
4325 // seleqz out_reg, false_reg, cond_reg
4326 can_move_conditionally = true;
4327 use_const_for_true_in = true;
4328 } else if (is_false_value_zero_constant) {
4329 // selnez out_reg, true_reg, cond_reg
4330 can_move_conditionally = true;
4331 use_const_for_false_in = true;
4332 } else if (materialized) {
4333 // Not materializing unmaterialized int conditions
4334 // to keep the instruction count low.
4335 // selnez AT, true_reg, cond_reg
4336 // seleqz TMP, false_reg, cond_reg
4337 // or out_reg, AT, TMP
4338 can_move_conditionally = true;
4339 }
4340 } else {
4341 // Moving float/double on int/long condition.
4342 if (materialized) {
4343 // Not materializing unmaterialized int conditions
4344 // to keep the instruction count low.
4345 can_move_conditionally = true;
4346 if (is_true_value_zero_constant) {
4347 // sltu TMP, ZERO, cond_reg
4348 // mtc1 TMP, temp_cond_reg
4349 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4350 use_const_for_true_in = true;
4351 } else if (is_false_value_zero_constant) {
4352 // sltu TMP, ZERO, cond_reg
4353 // mtc1 TMP, temp_cond_reg
4354 // selnez.fmt out_reg, true_reg, temp_cond_reg
4355 use_const_for_false_in = true;
4356 } else {
4357 // sltu TMP, ZERO, cond_reg
4358 // mtc1 TMP, temp_cond_reg
4359 // sel.fmt temp_cond_reg, false_reg, true_reg
4360 // mov.fmt out_reg, temp_cond_reg
4361 }
4362 }
4363 }
4364 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004365 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004366 // Moving int/long on float/double condition.
4367 can_move_conditionally = true;
4368 if (is_true_value_zero_constant) {
4369 // mfc1 TMP, temp_cond_reg
4370 // seleqz out_reg, false_reg, TMP
4371 use_const_for_true_in = true;
4372 } else if (is_false_value_zero_constant) {
4373 // mfc1 TMP, temp_cond_reg
4374 // selnez out_reg, true_reg, TMP
4375 use_const_for_false_in = true;
4376 } else {
4377 // mfc1 TMP, temp_cond_reg
4378 // selnez AT, true_reg, TMP
4379 // seleqz TMP, false_reg, TMP
4380 // or out_reg, AT, TMP
4381 }
4382 } else {
4383 // Moving float/double on float/double condition.
4384 can_move_conditionally = true;
4385 if (is_true_value_zero_constant) {
4386 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4387 use_const_for_true_in = true;
4388 } else if (is_false_value_zero_constant) {
4389 // selnez.fmt out_reg, true_reg, temp_cond_reg
4390 use_const_for_false_in = true;
4391 } else {
4392 // sel.fmt temp_cond_reg, false_reg, true_reg
4393 // mov.fmt out_reg, temp_cond_reg
4394 }
4395 }
4396 }
4397 }
4398
4399 if (can_move_conditionally) {
4400 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4401 } else {
4402 DCHECK(!use_const_for_false_in);
4403 DCHECK(!use_const_for_true_in);
4404 }
4405
4406 if (locations_to_set != nullptr) {
4407 if (use_const_for_false_in) {
4408 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4409 } else {
4410 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004411 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004412 ? Location::RequiresFpuRegister()
4413 : Location::RequiresRegister());
4414 }
4415 if (use_const_for_true_in) {
4416 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4417 } else {
4418 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004419 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004420 ? Location::RequiresFpuRegister()
4421 : Location::RequiresRegister());
4422 }
4423 if (materialized) {
4424 locations_to_set->SetInAt(2, Location::RequiresRegister());
4425 }
4426
4427 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004428 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004429 ? Location::RequiresFpuRegister()
4430 : Location::RequiresRegister());
4431 } else {
4432 locations_to_set->SetOut(Location::SameAsFirstInput());
4433 }
4434 }
4435
4436 return can_move_conditionally;
4437}
4438
4439
4440void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4441 LocationSummary* locations = select->GetLocations();
4442 Location dst = locations->Out();
4443 Location false_src = locations->InAt(0);
4444 Location true_src = locations->InAt(1);
4445 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4446 GpuRegister cond_reg = TMP;
4447 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004448 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004449 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004450 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004451
4452 if (IsBooleanValueOrMaterializedCondition(cond)) {
4453 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4454 } else {
4455 HCondition* condition = cond->AsCondition();
4456 LocationSummary* cond_locations = cond->GetLocations();
4457 IfCondition if_cond = condition->GetCondition();
4458 cond_type = condition->InputAt(0)->GetType();
4459 switch (cond_type) {
4460 default:
4461 cond_inverted = MaterializeIntLongCompare(if_cond,
4462 /* is64bit */ false,
4463 cond_locations,
4464 cond_reg);
4465 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004466 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004467 cond_inverted = MaterializeIntLongCompare(if_cond,
4468 /* is64bit */ true,
4469 cond_locations,
4470 cond_reg);
4471 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004472 case DataType::Type::kFloat32:
4473 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004474 cond_inverted = MaterializeFpCompare(if_cond,
4475 condition->IsGtBias(),
4476 cond_type,
4477 cond_locations,
4478 fcond_reg);
4479 break;
4480 }
4481 }
4482
4483 if (true_src.IsConstant()) {
4484 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4485 }
4486 if (false_src.IsConstant()) {
4487 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4488 }
4489
4490 switch (dst_type) {
4491 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004492 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004493 __ Mfc1(cond_reg, fcond_reg);
4494 }
4495 if (true_src.IsConstant()) {
4496 if (cond_inverted) {
4497 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4498 } else {
4499 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4500 }
4501 } else if (false_src.IsConstant()) {
4502 if (cond_inverted) {
4503 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4504 } else {
4505 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4506 }
4507 } else {
4508 DCHECK_NE(cond_reg, AT);
4509 if (cond_inverted) {
4510 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4511 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4512 } else {
4513 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4514 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4515 }
4516 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4517 }
4518 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004519 case DataType::Type::kFloat32: {
4520 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004521 // sel*.fmt tests bit 0 of the condition register, account for that.
4522 __ Sltu(TMP, ZERO, cond_reg);
4523 __ Mtc1(TMP, fcond_reg);
4524 }
4525 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4526 if (true_src.IsConstant()) {
4527 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4528 if (cond_inverted) {
4529 __ SelnezS(dst_reg, src_reg, fcond_reg);
4530 } else {
4531 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4532 }
4533 } else if (false_src.IsConstant()) {
4534 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4535 if (cond_inverted) {
4536 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4537 } else {
4538 __ SelnezS(dst_reg, src_reg, fcond_reg);
4539 }
4540 } else {
4541 if (cond_inverted) {
4542 __ SelS(fcond_reg,
4543 true_src.AsFpuRegister<FpuRegister>(),
4544 false_src.AsFpuRegister<FpuRegister>());
4545 } else {
4546 __ SelS(fcond_reg,
4547 false_src.AsFpuRegister<FpuRegister>(),
4548 true_src.AsFpuRegister<FpuRegister>());
4549 }
4550 __ MovS(dst_reg, fcond_reg);
4551 }
4552 break;
4553 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004554 case DataType::Type::kFloat64: {
4555 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004556 // sel*.fmt tests bit 0 of the condition register, account for that.
4557 __ Sltu(TMP, ZERO, cond_reg);
4558 __ Mtc1(TMP, fcond_reg);
4559 }
4560 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4561 if (true_src.IsConstant()) {
4562 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4563 if (cond_inverted) {
4564 __ SelnezD(dst_reg, src_reg, fcond_reg);
4565 } else {
4566 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4567 }
4568 } else if (false_src.IsConstant()) {
4569 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4570 if (cond_inverted) {
4571 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4572 } else {
4573 __ SelnezD(dst_reg, src_reg, fcond_reg);
4574 }
4575 } else {
4576 if (cond_inverted) {
4577 __ SelD(fcond_reg,
4578 true_src.AsFpuRegister<FpuRegister>(),
4579 false_src.AsFpuRegister<FpuRegister>());
4580 } else {
4581 __ SelD(fcond_reg,
4582 false_src.AsFpuRegister<FpuRegister>(),
4583 true_src.AsFpuRegister<FpuRegister>());
4584 }
4585 __ MovD(dst_reg, fcond_reg);
4586 }
4587 break;
4588 }
4589 }
4590}
4591
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004592void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004593 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004594 LocationSummary(flag, LocationSummary::kNoCall);
4595 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004596}
4597
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004598void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4599 __ LoadFromOffset(kLoadWord,
4600 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4601 SP,
4602 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004603}
4604
David Brazdil74eb1b22015-12-14 11:44:01 +00004605void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004606 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004607 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004608}
4609
4610void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004611 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4612 GenConditionalMove(select);
4613 } else {
4614 LocationSummary* locations = select->GetLocations();
4615 Mips64Label false_target;
4616 GenerateTestAndBranch(select,
4617 /* condition_input_index */ 2,
4618 /* true_target */ nullptr,
4619 &false_target);
4620 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4621 __ Bind(&false_target);
4622 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004623}
4624
David Srbecky0cf44932015-12-09 14:09:59 +00004625void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004626 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004627}
4628
David Srbeckyd28f4a02016-03-14 17:14:24 +00004629void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4630 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004631}
4632
4633void CodeGeneratorMIPS64::GenerateNop() {
4634 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004635}
4636
Alexey Frunze4dda3372015-06-01 18:31:49 -07004637void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004638 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004640 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004642 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004643 instruction,
4644 object_field_get_with_read_barrier
4645 ? LocationSummary::kCallOnSlowPath
4646 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004647 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4648 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4649 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004650 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004652 locations->SetOut(Location::RequiresFpuRegister());
4653 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004654 // The output overlaps in the case of an object field get with
4655 // read barriers enabled: we do not want the move to overwrite the
4656 // object's location, as we need it to emit the read barrier.
4657 locations->SetOut(Location::RequiresRegister(),
4658 object_field_get_with_read_barrier
4659 ? Location::kOutputOverlap
4660 : Location::kNoOutputOverlap);
4661 }
4662 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4663 // We need a temporary register for the read barrier marking slow
4664 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004665 if (!kBakerReadBarrierThunksEnableForFields) {
4666 locations->AddTemp(Location::RequiresRegister());
4667 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004668 }
4669}
4670
4671void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4672 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004673 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004674 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004675 Location obj_loc = locations->InAt(0);
4676 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4677 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004678 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004679 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004680 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004681 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4682
Alexey Frunze4dda3372015-06-01 18:31:49 -07004683 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004684 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004685 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004686 load_type = kLoadUnsignedByte;
4687 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004688 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004689 load_type = kLoadSignedByte;
4690 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004691 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004692 load_type = kLoadUnsignedHalfword;
4693 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004694 case DataType::Type::kInt16:
4695 load_type = kLoadSignedHalfword;
4696 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004697 case DataType::Type::kInt32:
4698 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004699 load_type = kLoadWord;
4700 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004701 case DataType::Type::kInt64:
4702 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004703 load_type = kLoadDoubleword;
4704 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004705 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004706 load_type = kLoadUnsignedWord;
4707 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004708 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004709 LOG(FATAL) << "Unreachable type " << type;
4710 UNREACHABLE();
4711 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004712 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004713 DCHECK(dst_loc.IsRegister());
4714 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004715 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004716 // /* HeapReference<Object> */ dst = *(obj + offset)
4717 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004718 Location temp_loc =
4719 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004720 // Note that a potential implicit null check is handled in this
4721 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4722 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4723 dst_loc,
4724 obj,
4725 offset,
4726 temp_loc,
4727 /* needs_null_check */ true);
4728 if (is_volatile) {
4729 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4730 }
4731 } else {
4732 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4733 if (is_volatile) {
4734 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4735 }
4736 // If read barriers are enabled, emit read barriers other than
4737 // Baker's using a slow path (and also unpoison the loaded
4738 // reference, if heap poisoning is enabled).
4739 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4740 }
4741 } else {
4742 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4743 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004744 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004745 DCHECK(dst_loc.IsFpuRegister());
4746 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004747 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004748 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004749
Alexey Frunze15958152017-02-09 19:08:30 -08004750 // Memory barriers, in the case of references, are handled in the
4751 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004752 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004753 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004754 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004755}
4756
4757void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4758 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4759 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004760 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004761 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004762 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004763 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004764 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004765 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004766 }
4767}
4768
4769void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004770 const FieldInfo& field_info,
4771 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004772 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004773 LocationSummary* locations = instruction->GetLocations();
4774 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004775 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004776 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004777 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004778 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4779 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004780 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4781
Alexey Frunze4dda3372015-06-01 18:31:49 -07004782 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004784 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004785 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004786 store_type = kStoreByte;
4787 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004788 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004789 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004790 store_type = kStoreHalfword;
4791 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kInt32:
4793 case DataType::Type::kFloat32:
4794 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004795 store_type = kStoreWord;
4796 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004797 case DataType::Type::kInt64:
4798 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004799 store_type = kStoreDoubleword;
4800 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004801 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004802 LOG(FATAL) << "Unreachable type " << type;
4803 UNREACHABLE();
4804 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004805
Alexey Frunze15958152017-02-09 19:08:30 -08004806 if (is_volatile) {
4807 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4808 }
4809
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004810 if (value_location.IsConstant()) {
4811 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4812 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4813 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004814 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004815 DCHECK(value_location.IsRegister());
4816 GpuRegister src = value_location.AsRegister<GpuRegister>();
4817 if (kPoisonHeapReferences && needs_write_barrier) {
4818 // Note that in the case where `value` is a null reference,
4819 // we do not enter this block, as a null reference does not
4820 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004821 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004822 __ PoisonHeapReference(TMP, src);
4823 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4824 } else {
4825 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4826 }
4827 } else {
4828 DCHECK(value_location.IsFpuRegister());
4829 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4830 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4831 }
4832 }
Alexey Frunze15958152017-02-09 19:08:30 -08004833
Alexey Frunzec061de12017-02-14 13:27:23 -08004834 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004835 DCHECK(value_location.IsRegister());
4836 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004837 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838 }
Alexey Frunze15958152017-02-09 19:08:30 -08004839
4840 if (is_volatile) {
4841 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4842 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004843}
4844
4845void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4846 HandleFieldGet(instruction, instruction->GetFieldInfo());
4847}
4848
4849void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4850 HandleFieldGet(instruction, instruction->GetFieldInfo());
4851}
4852
4853void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4854 HandleFieldSet(instruction, instruction->GetFieldInfo());
4855}
4856
4857void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004858 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004859}
4860
Alexey Frunze15958152017-02-09 19:08:30 -08004861void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4862 HInstruction* instruction,
4863 Location out,
4864 uint32_t offset,
4865 Location maybe_temp,
4866 ReadBarrierOption read_barrier_option) {
4867 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4868 if (read_barrier_option == kWithReadBarrier) {
4869 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004870 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4871 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4872 }
Alexey Frunze15958152017-02-09 19:08:30 -08004873 if (kUseBakerReadBarrier) {
4874 // Load with fast path based Baker's read barrier.
4875 // /* HeapReference<Object> */ out = *(out + offset)
4876 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4877 out,
4878 out_reg,
4879 offset,
4880 maybe_temp,
4881 /* needs_null_check */ false);
4882 } else {
4883 // Load with slow path based read barrier.
4884 // Save the value of `out` into `maybe_temp` before overwriting it
4885 // in the following move operation, as we will need it for the
4886 // read barrier below.
4887 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4888 // /* HeapReference<Object> */ out = *(out + offset)
4889 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4890 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4891 }
4892 } else {
4893 // Plain load with no read barrier.
4894 // /* HeapReference<Object> */ out = *(out + offset)
4895 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4896 __ MaybeUnpoisonHeapReference(out_reg);
4897 }
4898}
4899
4900void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4901 HInstruction* instruction,
4902 Location out,
4903 Location obj,
4904 uint32_t offset,
4905 Location maybe_temp,
4906 ReadBarrierOption read_barrier_option) {
4907 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4908 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4909 if (read_barrier_option == kWithReadBarrier) {
4910 CHECK(kEmitCompilerReadBarrier);
4911 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004912 if (!kBakerReadBarrierThunksEnableForFields) {
4913 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4914 }
Alexey Frunze15958152017-02-09 19:08:30 -08004915 // Load with fast path based Baker's read barrier.
4916 // /* HeapReference<Object> */ out = *(obj + offset)
4917 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4918 out,
4919 obj_reg,
4920 offset,
4921 maybe_temp,
4922 /* needs_null_check */ false);
4923 } else {
4924 // Load with slow path based read barrier.
4925 // /* HeapReference<Object> */ out = *(obj + offset)
4926 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4927 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4928 }
4929 } else {
4930 // Plain load with no read barrier.
4931 // /* HeapReference<Object> */ out = *(obj + offset)
4932 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4933 __ MaybeUnpoisonHeapReference(out_reg);
4934 }
4935}
4936
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004937static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4938 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4939 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4940 return reg - V0;
4941 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4942 return 13 + (reg - S2);
4943 } else if (reg == S8) { // One more.
4944 return 19;
4945 }
4946 LOG(FATAL) << "Unexpected register " << reg;
4947 UNREACHABLE();
4948}
4949
4950static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4951 int num = GetBakerMarkThunkNumber(reg) +
4952 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4953 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4954}
4955
4956static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4957 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4958 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4959}
4960
4961void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4962 Location root,
4963 GpuRegister obj,
4964 uint32_t offset,
4965 ReadBarrierOption read_barrier_option,
4966 Mips64Label* label_low) {
4967 if (label_low != nullptr) {
4968 DCHECK_EQ(offset, 0x5678u);
4969 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004970 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004971 if (read_barrier_option == kWithReadBarrier) {
4972 DCHECK(kEmitCompilerReadBarrier);
4973 if (kUseBakerReadBarrier) {
4974 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4975 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004976 if (kBakerReadBarrierThunksEnableForGcRoots) {
4977 // Note that we do not actually check the value of `GetIsGcMarking()`
4978 // to decide whether to mark the loaded GC root or not. Instead, we
4979 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4980 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4981 // vice versa.
4982 //
4983 // We use thunks for the slow path. That thunk checks the reference
4984 // and jumps to the entrypoint if needed.
4985 //
4986 // temp = Thread::Current()->pReadBarrierMarkReg00
4987 // // AKA &art_quick_read_barrier_mark_introspection.
4988 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4989 // if (temp != nullptr) {
4990 // temp = &gc_root_thunk<root_reg>
4991 // root = temp(root)
4992 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004993
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004994 const int32_t entry_point_offset =
4995 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4996 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4997 int16_t offset_low = Low16Bits(offset);
4998 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
4999 // extension in lwu.
5000 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5001 GpuRegister base = short_offset ? obj : TMP;
5002 // Loading the entrypoint does not require a load acquire since it is only changed when
5003 // threads are suspended or running a checkpoint.
5004 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5005 if (!short_offset) {
5006 DCHECK(!label_low);
5007 __ Daui(base, obj, offset_high);
5008 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005009 Mips64Label skip_call;
5010 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005011 if (label_low != nullptr) {
5012 DCHECK(short_offset);
5013 __ Bind(label_low);
5014 }
5015 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5016 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5017 // in delay slot.
5018 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005019 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005020 } else {
5021 // Note that we do not actually check the value of `GetIsGcMarking()`
5022 // to decide whether to mark the loaded GC root or not. Instead, we
5023 // load into `temp` (T9) the read barrier mark entry point corresponding
5024 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5025 // is false, and vice versa.
5026 //
5027 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5028 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5029 // if (temp != null) {
5030 // root = temp(root)
5031 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005032
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005033 if (label_low != nullptr) {
5034 __ Bind(label_low);
5035 }
5036 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5037 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5038 static_assert(
5039 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5040 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5041 "have different sizes.");
5042 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5043 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5044 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005045
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005046 // Slow path marking the GC root `root`.
5047 Location temp = Location::RegisterLocation(T9);
5048 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005049 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005050 instruction,
5051 root,
5052 /*entrypoint*/ temp);
5053 codegen_->AddSlowPath(slow_path);
5054
5055 const int32_t entry_point_offset =
5056 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5057 // Loading the entrypoint does not require a load acquire since it is only changed when
5058 // threads are suspended or running a checkpoint.
5059 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5060 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5061 __ Bind(slow_path->GetExitLabel());
5062 }
Alexey Frunze15958152017-02-09 19:08:30 -08005063 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005064 if (label_low != nullptr) {
5065 __ Bind(label_low);
5066 }
Alexey Frunze15958152017-02-09 19:08:30 -08005067 // GC root loaded through a slow path for read barriers other
5068 // than Baker's.
5069 // /* GcRoot<mirror::Object>* */ root = obj + offset
5070 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5071 // /* mirror::Object* */ root = root->Read()
5072 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5073 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005074 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005075 if (label_low != nullptr) {
5076 __ Bind(label_low);
5077 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005078 // Plain GC root load with no read barrier.
5079 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5080 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5081 // Note that GC roots are not affected by heap poisoning, thus we
5082 // do not have to unpoison `root_reg` here.
5083 }
5084}
5085
Alexey Frunze15958152017-02-09 19:08:30 -08005086void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5087 Location ref,
5088 GpuRegister obj,
5089 uint32_t offset,
5090 Location temp,
5091 bool needs_null_check) {
5092 DCHECK(kEmitCompilerReadBarrier);
5093 DCHECK(kUseBakerReadBarrier);
5094
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005095 if (kBakerReadBarrierThunksEnableForFields) {
5096 // Note that we do not actually check the value of `GetIsGcMarking()`
5097 // to decide whether to mark the loaded reference or not. Instead, we
5098 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5099 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5100 // vice versa.
5101 //
5102 // We use thunks for the slow path. That thunk checks the reference
5103 // and jumps to the entrypoint if needed. If the holder is not gray,
5104 // it issues a load-load memory barrier and returns to the original
5105 // reference load.
5106 //
5107 // temp = Thread::Current()->pReadBarrierMarkReg00
5108 // // AKA &art_quick_read_barrier_mark_introspection.
5109 // if (temp != nullptr) {
5110 // temp = &field_array_thunk<holder_reg>
5111 // temp()
5112 // }
5113 // not_gray_return_address:
5114 // // If the offset is too large to fit into the lw instruction, we
5115 // // use an adjusted base register (TMP) here. This register
5116 // // receives bits 16 ... 31 of the offset before the thunk invocation
5117 // // and the thunk benefits from it.
5118 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5119 // gray_return_address:
5120
5121 DCHECK(temp.IsInvalid());
5122 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5123 const int32_t entry_point_offset =
5124 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5125 // There may have or may have not been a null check if the field offset is smaller than
5126 // the page size.
5127 // There must've been a null check in case it's actually a load from an array.
5128 // We will, however, perform an explicit null check in the thunk as it's easier to
5129 // do it than not.
5130 if (instruction->IsArrayGet()) {
5131 DCHECK(!needs_null_check);
5132 }
5133 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5134 // Loading the entrypoint does not require a load acquire since it is only changed when
5135 // threads are suspended or running a checkpoint.
5136 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5137 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005138 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005139 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005140 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005141 __ Nop(); // In forbidden slot.
5142 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005143 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005144 // /* HeapReference<Object> */ ref = *(obj + offset)
5145 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5146 } else {
5147 int16_t offset_low = Low16Bits(offset);
5148 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005149 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005150 __ Daui(TMP, obj, offset_high); // In delay slot.
5151 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005152 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005153 // /* HeapReference<Object> */ ref = *(obj + offset)
5154 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5155 }
5156 if (needs_null_check) {
5157 MaybeRecordImplicitNullCheck(instruction);
5158 }
5159 __ MaybeUnpoisonHeapReference(ref_reg);
5160 return;
5161 }
5162
Alexey Frunze15958152017-02-09 19:08:30 -08005163 // /* HeapReference<Object> */ ref = *(obj + offset)
5164 Location no_index = Location::NoLocation();
5165 ScaleFactor no_scale_factor = TIMES_1;
5166 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5167 ref,
5168 obj,
5169 offset,
5170 no_index,
5171 no_scale_factor,
5172 temp,
5173 needs_null_check);
5174}
5175
5176void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5177 Location ref,
5178 GpuRegister obj,
5179 uint32_t data_offset,
5180 Location index,
5181 Location temp,
5182 bool needs_null_check) {
5183 DCHECK(kEmitCompilerReadBarrier);
5184 DCHECK(kUseBakerReadBarrier);
5185
5186 static_assert(
5187 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5188 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005189 ScaleFactor scale_factor = TIMES_4;
5190
5191 if (kBakerReadBarrierThunksEnableForArrays) {
5192 // Note that we do not actually check the value of `GetIsGcMarking()`
5193 // to decide whether to mark the loaded reference or not. Instead, we
5194 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5195 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5196 // vice versa.
5197 //
5198 // We use thunks for the slow path. That thunk checks the reference
5199 // and jumps to the entrypoint if needed. If the holder is not gray,
5200 // it issues a load-load memory barrier and returns to the original
5201 // reference load.
5202 //
5203 // temp = Thread::Current()->pReadBarrierMarkReg00
5204 // // AKA &art_quick_read_barrier_mark_introspection.
5205 // if (temp != nullptr) {
5206 // temp = &field_array_thunk<holder_reg>
5207 // temp()
5208 // }
5209 // not_gray_return_address:
5210 // // The element address is pre-calculated in the TMP register before the
5211 // // thunk invocation and the thunk benefits from it.
5212 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5213 // gray_return_address:
5214
5215 DCHECK(temp.IsInvalid());
5216 DCHECK(index.IsValid());
5217 const int32_t entry_point_offset =
5218 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5219 // We will not do the explicit null check in the thunk as some form of a null check
5220 // must've been done earlier.
5221 DCHECK(!needs_null_check);
5222 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5223 // Loading the entrypoint does not require a load acquire since it is only changed when
5224 // threads are suspended or running a checkpoint.
5225 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005226 Mips64Label skip_call;
5227 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005228 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5229 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5230 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5231 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005232 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005233 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5234 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5235 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5236 __ MaybeUnpoisonHeapReference(ref_reg);
5237 return;
5238 }
5239
Alexey Frunze15958152017-02-09 19:08:30 -08005240 // /* HeapReference<Object> */ ref =
5241 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005242 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5243 ref,
5244 obj,
5245 data_offset,
5246 index,
5247 scale_factor,
5248 temp,
5249 needs_null_check);
5250}
5251
5252void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5253 Location ref,
5254 GpuRegister obj,
5255 uint32_t offset,
5256 Location index,
5257 ScaleFactor scale_factor,
5258 Location temp,
5259 bool needs_null_check,
5260 bool always_update_field) {
5261 DCHECK(kEmitCompilerReadBarrier);
5262 DCHECK(kUseBakerReadBarrier);
5263
5264 // In slow path based read barriers, the read barrier call is
5265 // inserted after the original load. However, in fast path based
5266 // Baker's read barriers, we need to perform the load of
5267 // mirror::Object::monitor_ *before* the original reference load.
5268 // This load-load ordering is required by the read barrier.
5269 // The fast path/slow path (for Baker's algorithm) should look like:
5270 //
5271 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5272 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5273 // HeapReference<Object> ref = *src; // Original reference load.
5274 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5275 // if (is_gray) {
5276 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5277 // }
5278 //
5279 // Note: the original implementation in ReadBarrier::Barrier is
5280 // slightly more complex as it performs additional checks that we do
5281 // not do here for performance reasons.
5282
5283 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5284 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5285 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5286
5287 // /* int32_t */ monitor = obj->monitor_
5288 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5289 if (needs_null_check) {
5290 MaybeRecordImplicitNullCheck(instruction);
5291 }
5292 // /* LockWord */ lock_word = LockWord(monitor)
5293 static_assert(sizeof(LockWord) == sizeof(int32_t),
5294 "art::LockWord and int32_t have different sizes.");
5295
5296 __ Sync(0); // Barrier to prevent load-load reordering.
5297
5298 // The actual reference load.
5299 if (index.IsValid()) {
5300 // Load types involving an "index": ArrayGet,
5301 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5302 // intrinsics.
5303 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5304 if (index.IsConstant()) {
5305 size_t computed_offset =
5306 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5307 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5308 } else {
5309 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005310 if (scale_factor == TIMES_1) {
5311 __ Daddu(TMP, index_reg, obj);
5312 } else {
5313 __ Dlsa(TMP, index_reg, obj, scale_factor);
5314 }
Alexey Frunze15958152017-02-09 19:08:30 -08005315 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5316 }
5317 } else {
5318 // /* HeapReference<Object> */ ref = *(obj + offset)
5319 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5320 }
5321
5322 // Object* ref = ref_addr->AsMirrorPtr()
5323 __ MaybeUnpoisonHeapReference(ref_reg);
5324
5325 // Slow path marking the object `ref` when it is gray.
5326 SlowPathCodeMIPS64* slow_path;
5327 if (always_update_field) {
5328 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5329 // of the form `obj + field_offset`, where `obj` is a register and
5330 // `field_offset` is a register. Thus `offset` and `scale_factor`
5331 // above are expected to be null in this code path.
5332 DCHECK_EQ(offset, 0u);
5333 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005334 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005335 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5336 ref,
5337 obj,
5338 /* field_offset */ index,
5339 temp_reg);
5340 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005341 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005342 }
5343 AddSlowPath(slow_path);
5344
5345 // if (rb_state == ReadBarrier::GrayState())
5346 // ref = ReadBarrier::Mark(ref);
5347 // Given the numeric representation, it's enough to check the low bit of the
5348 // rb_state. We do that by shifting the bit into the sign bit (31) and
5349 // performing a branch on less than zero.
5350 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5351 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5352 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5353 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5354 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5355 __ Bind(slow_path->GetExitLabel());
5356}
5357
5358void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5359 Location out,
5360 Location ref,
5361 Location obj,
5362 uint32_t offset,
5363 Location index) {
5364 DCHECK(kEmitCompilerReadBarrier);
5365
5366 // Insert a slow path based read barrier *after* the reference load.
5367 //
5368 // If heap poisoning is enabled, the unpoisoning of the loaded
5369 // reference will be carried out by the runtime within the slow
5370 // path.
5371 //
5372 // Note that `ref` currently does not get unpoisoned (when heap
5373 // poisoning is enabled), which is alright as the `ref` argument is
5374 // not used by the artReadBarrierSlow entry point.
5375 //
5376 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005377 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005378 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5379 AddSlowPath(slow_path);
5380
5381 __ Bc(slow_path->GetEntryLabel());
5382 __ Bind(slow_path->GetExitLabel());
5383}
5384
5385void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5386 Location out,
5387 Location ref,
5388 Location obj,
5389 uint32_t offset,
5390 Location index) {
5391 if (kEmitCompilerReadBarrier) {
5392 // Baker's read barriers shall be handled by the fast path
5393 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5394 DCHECK(!kUseBakerReadBarrier);
5395 // If heap poisoning is enabled, unpoisoning will be taken care of
5396 // by the runtime within the slow path.
5397 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5398 } else if (kPoisonHeapReferences) {
5399 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5400 }
5401}
5402
5403void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5404 Location out,
5405 Location root) {
5406 DCHECK(kEmitCompilerReadBarrier);
5407
5408 // Insert a slow path based read barrier *after* the GC root load.
5409 //
5410 // Note that GC roots are not affected by heap poisoning, so we do
5411 // not need to do anything special for this here.
5412 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005413 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005414 AddSlowPath(slow_path);
5415
5416 __ Bc(slow_path->GetEntryLabel());
5417 __ Bind(slow_path->GetExitLabel());
5418}
5419
Alexey Frunze4dda3372015-06-01 18:31:49 -07005420void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005421 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5422 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005423 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005424 switch (type_check_kind) {
5425 case TypeCheckKind::kExactCheck:
5426 case TypeCheckKind::kAbstractClassCheck:
5427 case TypeCheckKind::kClassHierarchyCheck:
5428 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005429 call_kind =
5430 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005431 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005432 break;
5433 case TypeCheckKind::kArrayCheck:
5434 case TypeCheckKind::kUnresolvedCheck:
5435 case TypeCheckKind::kInterfaceCheck:
5436 call_kind = LocationSummary::kCallOnSlowPath;
5437 break;
5438 }
5439
Vladimir Markoca6fff82017-10-03 14:49:14 +01005440 LocationSummary* locations =
5441 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005442 if (baker_read_barrier_slow_path) {
5443 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5444 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005445 locations->SetInAt(0, Location::RequiresRegister());
5446 locations->SetInAt(1, Location::RequiresRegister());
5447 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005448 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005449 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005450 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005451}
5452
5453void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005454 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005455 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005456 Location obj_loc = locations->InAt(0);
5457 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005458 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005459 Location out_loc = locations->Out();
5460 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5461 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5462 DCHECK_LE(num_temps, 1u);
5463 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005464 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5465 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5466 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5467 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005468 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005469 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005470
5471 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005472 // Avoid this check if we know `obj` is not null.
5473 if (instruction->MustDoNullCheck()) {
5474 __ Move(out, ZERO);
5475 __ Beqzc(obj, &done);
5476 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005477
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005478 switch (type_check_kind) {
5479 case TypeCheckKind::kExactCheck: {
5480 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005481 GenerateReferenceLoadTwoRegisters(instruction,
5482 out_loc,
5483 obj_loc,
5484 class_offset,
5485 maybe_temp_loc,
5486 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005487 // Classes must be equal for the instanceof to succeed.
5488 __ Xor(out, out, cls);
5489 __ Sltiu(out, out, 1);
5490 break;
5491 }
5492
5493 case TypeCheckKind::kAbstractClassCheck: {
5494 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005495 GenerateReferenceLoadTwoRegisters(instruction,
5496 out_loc,
5497 obj_loc,
5498 class_offset,
5499 maybe_temp_loc,
5500 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005501 // If the class is abstract, we eagerly fetch the super class of the
5502 // object to avoid doing a comparison we know will fail.
5503 Mips64Label loop;
5504 __ Bind(&loop);
5505 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005506 GenerateReferenceLoadOneRegister(instruction,
5507 out_loc,
5508 super_offset,
5509 maybe_temp_loc,
5510 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005511 // If `out` is null, we use it for the result, and jump to `done`.
5512 __ Beqzc(out, &done);
5513 __ Bnec(out, cls, &loop);
5514 __ LoadConst32(out, 1);
5515 break;
5516 }
5517
5518 case TypeCheckKind::kClassHierarchyCheck: {
5519 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005520 GenerateReferenceLoadTwoRegisters(instruction,
5521 out_loc,
5522 obj_loc,
5523 class_offset,
5524 maybe_temp_loc,
5525 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005526 // Walk over the class hierarchy to find a match.
5527 Mips64Label loop, success;
5528 __ Bind(&loop);
5529 __ Beqc(out, cls, &success);
5530 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005531 GenerateReferenceLoadOneRegister(instruction,
5532 out_loc,
5533 super_offset,
5534 maybe_temp_loc,
5535 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005536 __ Bnezc(out, &loop);
5537 // If `out` is null, we use it for the result, and jump to `done`.
5538 __ Bc(&done);
5539 __ Bind(&success);
5540 __ LoadConst32(out, 1);
5541 break;
5542 }
5543
5544 case TypeCheckKind::kArrayObjectCheck: {
5545 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005546 GenerateReferenceLoadTwoRegisters(instruction,
5547 out_loc,
5548 obj_loc,
5549 class_offset,
5550 maybe_temp_loc,
5551 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005552 // Do an exact check.
5553 Mips64Label success;
5554 __ Beqc(out, cls, &success);
5555 // Otherwise, we need to check that the object's class is a non-primitive array.
5556 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005557 GenerateReferenceLoadOneRegister(instruction,
5558 out_loc,
5559 component_offset,
5560 maybe_temp_loc,
5561 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005562 // If `out` is null, we use it for the result, and jump to `done`.
5563 __ Beqzc(out, &done);
5564 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5565 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5566 __ Sltiu(out, out, 1);
5567 __ Bc(&done);
5568 __ Bind(&success);
5569 __ LoadConst32(out, 1);
5570 break;
5571 }
5572
5573 case TypeCheckKind::kArrayCheck: {
5574 // No read barrier since the slow path will retry upon failure.
5575 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005576 GenerateReferenceLoadTwoRegisters(instruction,
5577 out_loc,
5578 obj_loc,
5579 class_offset,
5580 maybe_temp_loc,
5581 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005582 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005583 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5584 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005585 codegen_->AddSlowPath(slow_path);
5586 __ Bnec(out, cls, slow_path->GetEntryLabel());
5587 __ LoadConst32(out, 1);
5588 break;
5589 }
5590
5591 case TypeCheckKind::kUnresolvedCheck:
5592 case TypeCheckKind::kInterfaceCheck: {
5593 // Note that we indeed only call on slow path, but we always go
5594 // into the slow path for the unresolved and interface check
5595 // cases.
5596 //
5597 // We cannot directly call the InstanceofNonTrivial runtime
5598 // entry point without resorting to a type checking slow path
5599 // here (i.e. by calling InvokeRuntime directly), as it would
5600 // require to assign fixed registers for the inputs of this
5601 // HInstanceOf instruction (following the runtime calling
5602 // convention), which might be cluttered by the potential first
5603 // read barrier emission at the beginning of this method.
5604 //
5605 // TODO: Introduce a new runtime entry point taking the object
5606 // to test (instead of its class) as argument, and let it deal
5607 // with the read barrier issues. This will let us refactor this
5608 // case of the `switch` code as it was previously (with a direct
5609 // call to the runtime not using a type checking slow path).
5610 // This should also be beneficial for the other cases above.
5611 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005612 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5613 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005614 codegen_->AddSlowPath(slow_path);
5615 __ Bc(slow_path->GetEntryLabel());
5616 break;
5617 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005618 }
5619
5620 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005621
5622 if (slow_path != nullptr) {
5623 __ Bind(slow_path->GetExitLabel());
5624 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005625}
5626
5627void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005628 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005629 locations->SetOut(Location::ConstantLocation(constant));
5630}
5631
5632void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5633 // Will be generated at use site.
5634}
5635
5636void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005637 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005638 locations->SetOut(Location::ConstantLocation(constant));
5639}
5640
5641void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5642 // Will be generated at use site.
5643}
5644
Calin Juravle175dc732015-08-25 15:42:32 +01005645void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5646 // The trampoline uses the same calling convention as dex calling conventions,
5647 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5648 // the method_idx.
5649 HandleInvoke(invoke);
5650}
5651
5652void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5653 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5654}
5655
Alexey Frunze4dda3372015-06-01 18:31:49 -07005656void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5657 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5658 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5659}
5660
5661void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5662 HandleInvoke(invoke);
5663 // The register T0 is required to be used for the hidden argument in
5664 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5665 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5666}
5667
5668void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5669 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5670 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005671 Location receiver = invoke->GetLocations()->InAt(0);
5672 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005673 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005674
5675 // Set the hidden argument.
5676 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5677 invoke->GetDexMethodIndex());
5678
5679 // temp = object->GetClass();
5680 if (receiver.IsStackSlot()) {
5681 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5682 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5683 } else {
5684 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5685 }
5686 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005687 // Instead of simply (possibly) unpoisoning `temp` here, we should
5688 // emit a read barrier for the previous class reference load.
5689 // However this is not required in practice, as this is an
5690 // intermediate/temporary reference and because the current
5691 // concurrent copying collector keeps the from-space memory
5692 // intact/accessible until the end of the marking phase (the
5693 // concurrent copying collector may not in the future).
5694 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005695 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5696 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5697 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005698 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005699 // temp = temp->GetImtEntryAt(method_offset);
5700 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5701 // T9 = temp->GetEntryPoint();
5702 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5703 // T9();
5704 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005705 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005706 DCHECK(!codegen_->IsLeafMethod());
5707 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5708}
5709
5710void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005711 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5712 if (intrinsic.TryDispatch(invoke)) {
5713 return;
5714 }
5715
Alexey Frunze4dda3372015-06-01 18:31:49 -07005716 HandleInvoke(invoke);
5717}
5718
5719void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005720 // Explicit clinit checks triggered by static invokes must have been pruned by
5721 // art::PrepareForRegisterAllocation.
5722 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005723
Chris Larsen3039e382015-08-26 07:54:08 -07005724 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5725 if (intrinsic.TryDispatch(invoke)) {
5726 return;
5727 }
5728
Alexey Frunze4dda3372015-06-01 18:31:49 -07005729 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005730}
5731
Orion Hodsonac141392017-01-13 11:53:47 +00005732void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5733 HandleInvoke(invoke);
5734}
5735
5736void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5737 codegen_->GenerateInvokePolymorphicCall(invoke);
5738}
5739
Chris Larsen3039e382015-08-26 07:54:08 -07005740static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005741 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005742 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5743 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005744 return true;
5745 }
5746 return false;
5747}
5748
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005750 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005751 bool fallback_load = false;
5752 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005753 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005754 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005755 case HLoadString::LoadKind::kBssEntry:
5756 DCHECK(!Runtime::Current()->UseJitCompilation());
5757 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005758 case HLoadString::LoadKind::kJitTableAddress:
5759 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005760 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005761 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005762 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005763 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005764 }
5765 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005766 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005767 }
5768 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769}
5770
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5772 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005773 bool fallback_load = false;
5774 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005775 case HLoadClass::LoadKind::kInvalid:
5776 LOG(FATAL) << "UNREACHABLE";
5777 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005778 case HLoadClass::LoadKind::kReferrersClass:
5779 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005780 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005781 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005782 case HLoadClass::LoadKind::kBssEntry:
5783 DCHECK(!Runtime::Current()->UseJitCompilation());
5784 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005785 case HLoadClass::LoadKind::kJitTableAddress:
5786 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005787 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005788 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005789 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005790 break;
5791 }
5792 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005793 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005794 }
5795 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005796}
5797
Vladimir Markodc151b22015-10-15 18:02:30 +01005798HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5799 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005800 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005801 // On MIPS64 we support all dispatch types.
5802 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005803}
5804
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005805void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5806 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005807 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005808 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005809 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5810 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5811
Alexey Frunze19f6c692016-11-30 19:19:55 -08005812 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005813 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005814 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005815 uint32_t offset =
5816 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005817 __ LoadFromOffset(kLoadDoubleword,
5818 temp.AsRegister<GpuRegister>(),
5819 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005820 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005821 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005822 }
Vladimir Marko58155012015-08-19 12:49:41 +00005823 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005824 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005825 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005826 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5827 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005828 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005829 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005830 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5831 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5832 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005833 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5834 break;
5835 }
Vladimir Marko58155012015-08-19 12:49:41 +00005836 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005837 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5838 kLoadDoubleword,
5839 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005840 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005841 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005842 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005843 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005844 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5845 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5846 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005847 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5848 break;
5849 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005850 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5851 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5852 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005853 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005854 }
5855
Alexey Frunze19f6c692016-11-30 19:19:55 -08005856 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005857 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005858 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005859 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005860 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5861 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5862 __ LoadFromOffset(kLoadDoubleword,
5863 T9,
5864 callee_method.AsRegister<GpuRegister>(),
5865 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005866 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005867 // T9()
5868 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005869 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005870 break;
5871 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005872 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5873
Alexey Frunze4dda3372015-06-01 18:31:49 -07005874 DCHECK(!IsLeafMethod());
5875}
5876
5877void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005878 // Explicit clinit checks triggered by static invokes must have been pruned by
5879 // art::PrepareForRegisterAllocation.
5880 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005881
5882 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5883 return;
5884 }
5885
5886 LocationSummary* locations = invoke->GetLocations();
5887 codegen_->GenerateStaticOrDirectCall(invoke,
5888 locations->HasTemps()
5889 ? locations->GetTemp(0)
5890 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005891}
5892
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005893void CodeGeneratorMIPS64::GenerateVirtualCall(
5894 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005895 // Use the calling convention instead of the location of the receiver, as
5896 // intrinsics may have put the receiver in a different register. In the intrinsics
5897 // slow path, the arguments have been moved to the right place, so here we are
5898 // guaranteed that the receiver is the first register of the calling convention.
5899 InvokeDexCallingConvention calling_convention;
5900 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5901
Alexey Frunze53afca12015-11-05 16:34:23 -08005902 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005903 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5904 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5905 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005906 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005907
5908 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005909 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005910 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005911 // Instead of simply (possibly) unpoisoning `temp` here, we should
5912 // emit a read barrier for the previous class reference load.
5913 // However this is not required in practice, as this is an
5914 // intermediate/temporary reference and because the current
5915 // concurrent copying collector keeps the from-space memory
5916 // intact/accessible until the end of the marking phase (the
5917 // concurrent copying collector may not in the future).
5918 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005919 // temp = temp->GetMethodAt(method_offset);
5920 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5921 // T9 = temp->GetEntryPoint();
5922 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5923 // T9();
5924 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005925 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005926 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005927}
5928
5929void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5930 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5931 return;
5932 }
5933
5934 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005935 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005936}
5937
5938void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005939 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005940 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005941 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005942 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5943 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005944 return;
5945 }
Vladimir Marko41559982017-01-06 14:04:23 +00005946 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005947
Alexey Frunze15958152017-02-09 19:08:30 -08005948 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5949 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005950 ? LocationSummary::kCallOnSlowPath
5951 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005952 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005953 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5954 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5955 }
Vladimir Marko41559982017-01-06 14:04:23 +00005956 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005957 locations->SetInAt(0, Location::RequiresRegister());
5958 }
5959 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005960 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5961 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5962 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005963 // Request a temp to hold the BSS entry location for the slow path.
5964 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005965 RegisterSet caller_saves = RegisterSet::Empty();
5966 InvokeRuntimeCallingConvention calling_convention;
5967 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5968 locations->SetCustomSlowPathCallerSaves(caller_saves);
5969 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005970 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005971 }
5972 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005973}
5974
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005975// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5976// move.
5977void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005978 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005979 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005980 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005981 return;
5982 }
Vladimir Marko41559982017-01-06 14:04:23 +00005983 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005984
Vladimir Marko41559982017-01-06 14:04:23 +00005985 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005986 Location out_loc = locations->Out();
5987 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5988 GpuRegister current_method_reg = ZERO;
5989 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005990 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005991 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5992 }
5993
Alexey Frunze15958152017-02-09 19:08:30 -08005994 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5995 ? kWithoutReadBarrier
5996 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005997 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005998 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005999 switch (load_kind) {
6000 case HLoadClass::LoadKind::kReferrersClass:
6001 DCHECK(!cls->CanCallRuntime());
6002 DCHECK(!cls->MustGenerateClinitCheck());
6003 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6004 GenerateGcRootFieldLoad(cls,
6005 out_loc,
6006 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006007 ArtMethod::DeclaringClassOffset().Int32Value(),
6008 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006009 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006010 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006011 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006012 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006013 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006014 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006015 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6016 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6017 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006018 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6019 break;
6020 }
6021 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006022 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006023 uint32_t address = dchecked_integral_cast<uint32_t>(
6024 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6025 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006026 __ LoadLiteral(out,
6027 kLoadUnsignedWord,
6028 codegen_->DeduplicateBootImageAddressLiteral(address));
6029 break;
6030 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006031 case HLoadClass::LoadKind::kBootImageClassTable: {
6032 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6033 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6034 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6035 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6036 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6037 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6038 __ Lwu(out, AT, /* placeholder */ 0x5678);
6039 // Extract the reference from the slot data, i.e. clear the hash bits.
6040 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6041 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6042 if (masked_hash != 0) {
6043 __ Daddiu(out, out, -masked_hash);
6044 }
6045 break;
6046 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006047 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006048 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
6049 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6050 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
6051 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6052 GpuRegister temp = non_baker_read_barrier
6053 ? out
6054 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006055 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
6056 GenerateGcRootFieldLoad(cls,
6057 out_loc,
6058 temp,
6059 /* placeholder */ 0x5678,
6060 read_barrier_option,
6061 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006062 generate_null_check = true;
6063 break;
6064 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006065 case HLoadClass::LoadKind::kJitTableAddress:
6066 __ LoadLiteral(out,
6067 kLoadUnsignedWord,
6068 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6069 cls->GetTypeIndex(),
6070 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006071 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006072 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006073 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006074 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006075 LOG(FATAL) << "UNREACHABLE";
6076 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006077 }
6078
6079 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6080 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006081 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006082 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006083 codegen_->AddSlowPath(slow_path);
6084 if (generate_null_check) {
6085 __ Beqzc(out, slow_path->GetEntryLabel());
6086 }
6087 if (cls->MustGenerateClinitCheck()) {
6088 GenerateClassInitializationCheck(slow_path, out);
6089 } else {
6090 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006091 }
6092 }
6093}
6094
David Brazdilcb1c0552015-08-04 16:22:25 +01006095static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006096 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006097}
6098
Alexey Frunze4dda3372015-06-01 18:31:49 -07006099void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6100 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006101 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006102 locations->SetOut(Location::RequiresRegister());
6103}
6104
6105void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6106 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006107 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6108}
6109
6110void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006111 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006112}
6113
6114void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6115 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006116}
6117
Alexey Frunze4dda3372015-06-01 18:31:49 -07006118void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006119 HLoadString::LoadKind load_kind = load->GetLoadKind();
6120 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006121 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006122 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006123 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006124 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006125 } else {
6126 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006127 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6128 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6129 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006130 // Request a temp to hold the BSS entry location for the slow path.
6131 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006132 RegisterSet caller_saves = RegisterSet::Empty();
6133 InvokeRuntimeCallingConvention calling_convention;
6134 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6135 locations->SetCustomSlowPathCallerSaves(caller_saves);
6136 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006137 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006138 }
6139 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006140 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006141}
6142
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006143// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6144// move.
6145void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006146 HLoadString::LoadKind load_kind = load->GetLoadKind();
6147 LocationSummary* locations = load->GetLocations();
6148 Location out_loc = locations->Out();
6149 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6150
6151 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006152 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6153 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006154 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006155 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006156 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6157 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6158 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006159 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006160 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006161 }
6162 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006163 uint32_t address = dchecked_integral_cast<uint32_t>(
6164 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6165 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006166 __ LoadLiteral(out,
6167 kLoadUnsignedWord,
6168 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006169 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006170 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006171 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006172 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006173 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006174 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006175 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6176 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006177 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6178 __ Lwu(out, AT, /* placeholder */ 0x5678);
6179 return;
6180 }
6181 case HLoadString::LoadKind::kBssEntry: {
6182 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6183 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6184 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6185 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6186 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006187 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6188 GpuRegister temp = non_baker_read_barrier
6189 ? out
6190 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006191 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08006192 GenerateGcRootFieldLoad(load,
6193 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006194 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08006195 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006196 kCompilerReadBarrierOption,
6197 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006198 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006199 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006200 codegen_->AddSlowPath(slow_path);
6201 __ Beqzc(out, slow_path->GetEntryLabel());
6202 __ Bind(slow_path->GetExitLabel());
6203 return;
6204 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006205 case HLoadString::LoadKind::kJitTableAddress:
6206 __ LoadLiteral(out,
6207 kLoadUnsignedWord,
6208 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6209 load->GetStringIndex(),
6210 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006211 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006212 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006213 default:
6214 break;
6215 }
6216
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006217 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006218 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006219 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006220 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006221 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6222 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6223 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006224}
6225
Alexey Frunze4dda3372015-06-01 18:31:49 -07006226void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006227 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006228 locations->SetOut(Location::ConstantLocation(constant));
6229}
6230
6231void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6232 // Will be generated at use site.
6233}
6234
6235void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006236 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6237 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006238 InvokeRuntimeCallingConvention calling_convention;
6239 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6240}
6241
6242void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006243 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006244 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006245 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006246 if (instruction->IsEnter()) {
6247 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6248 } else {
6249 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6250 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006251}
6252
6253void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6254 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006255 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006256 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006257 case DataType::Type::kInt32:
6258 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006259 locations->SetInAt(0, Location::RequiresRegister());
6260 locations->SetInAt(1, Location::RequiresRegister());
6261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6262 break;
6263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 case DataType::Type::kFloat32:
6265 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006266 locations->SetInAt(0, Location::RequiresFpuRegister());
6267 locations->SetInAt(1, Location::RequiresFpuRegister());
6268 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6269 break;
6270
6271 default:
6272 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6273 }
6274}
6275
6276void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006277 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006278 LocationSummary* locations = instruction->GetLocations();
6279
6280 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006281 case DataType::Type::kInt32:
6282 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006283 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6284 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6285 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006286 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006287 __ MulR6(dst, lhs, rhs);
6288 else
6289 __ Dmul(dst, lhs, rhs);
6290 break;
6291 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 case DataType::Type::kFloat32:
6293 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006294 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6295 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6296 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006298 __ MulS(dst, lhs, rhs);
6299 else
6300 __ MulD(dst, lhs, rhs);
6301 break;
6302 }
6303 default:
6304 LOG(FATAL) << "Unexpected mul type " << type;
6305 }
6306}
6307
6308void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6309 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006310 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006311 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006312 case DataType::Type::kInt32:
6313 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006314 locations->SetInAt(0, Location::RequiresRegister());
6315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6316 break;
6317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 case DataType::Type::kFloat32:
6319 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006320 locations->SetInAt(0, Location::RequiresFpuRegister());
6321 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6322 break;
6323
6324 default:
6325 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6326 }
6327}
6328
6329void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006330 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006331 LocationSummary* locations = instruction->GetLocations();
6332
6333 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006334 case DataType::Type::kInt32:
6335 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006336 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6337 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006339 __ Subu(dst, ZERO, src);
6340 else
6341 __ Dsubu(dst, ZERO, src);
6342 break;
6343 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006344 case DataType::Type::kFloat32:
6345 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006346 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6347 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006349 __ NegS(dst, src);
6350 else
6351 __ NegD(dst, src);
6352 break;
6353 }
6354 default:
6355 LOG(FATAL) << "Unexpected neg type " << type;
6356 }
6357}
6358
6359void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006360 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6361 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006362 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006363 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6365 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006366}
6367
6368void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006369 // Note: if heap poisoning is enabled, the entry point takes care
6370 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006371 QuickEntrypointEnum entrypoint =
6372 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6373 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006374 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006375 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006376}
6377
6378void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006379 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6380 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006381 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006382 if (instruction->IsStringAlloc()) {
6383 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6384 } else {
6385 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006386 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006387 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006388}
6389
6390void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006391 // Note: if heap poisoning is enabled, the entry point takes care
6392 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006393 if (instruction->IsStringAlloc()) {
6394 // String is allocated through StringFactory. Call NewEmptyString entry point.
6395 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006396 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006397 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006398 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6399 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6400 __ Jalr(T9);
6401 __ Nop();
6402 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6403 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006404 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006405 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006406 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006407}
6408
6409void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006410 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006411 locations->SetInAt(0, Location::RequiresRegister());
6412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6413}
6414
6415void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006416 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006417 LocationSummary* locations = instruction->GetLocations();
6418
6419 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006420 case DataType::Type::kInt32:
6421 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006422 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6423 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6424 __ Nor(dst, src, ZERO);
6425 break;
6426 }
6427
6428 default:
6429 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6430 }
6431}
6432
6433void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006434 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006435 locations->SetInAt(0, Location::RequiresRegister());
6436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6437}
6438
6439void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6440 LocationSummary* locations = instruction->GetLocations();
6441 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6442 locations->InAt(0).AsRegister<GpuRegister>(),
6443 1);
6444}
6445
6446void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006447 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6448 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006449}
6450
Calin Juravle2ae48182016-03-16 14:05:09 +00006451void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6452 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006453 return;
6454 }
6455 Location obj = instruction->GetLocations()->InAt(0);
6456
6457 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006458 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006459}
6460
Calin Juravle2ae48182016-03-16 14:05:09 +00006461void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006462 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006463 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006464 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006465
6466 Location obj = instruction->GetLocations()->InAt(0);
6467
6468 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6469}
6470
6471void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006472 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006473}
6474
6475void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6476 HandleBinaryOp(instruction);
6477}
6478
6479void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6480 HandleBinaryOp(instruction);
6481}
6482
6483void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6484 LOG(FATAL) << "Unreachable";
6485}
6486
6487void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006488 if (instruction->GetNext()->IsSuspendCheck() &&
6489 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6490 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6491 // The back edge will generate the suspend check.
6492 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6493 }
6494
Alexey Frunze4dda3372015-06-01 18:31:49 -07006495 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6496}
6497
6498void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006499 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006500 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6501 if (location.IsStackSlot()) {
6502 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6503 } else if (location.IsDoubleStackSlot()) {
6504 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6505 }
6506 locations->SetOut(location);
6507}
6508
6509void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6510 ATTRIBUTE_UNUSED) {
6511 // Nothing to do, the parameter is already at its location.
6512}
6513
6514void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6515 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006516 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006517 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6518}
6519
6520void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6521 ATTRIBUTE_UNUSED) {
6522 // Nothing to do, the method is already at its location.
6523}
6524
6525void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006526 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006527 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006528 locations->SetInAt(i, Location::Any());
6529 }
6530 locations->SetOut(Location::Any());
6531}
6532
6533void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6534 LOG(FATAL) << "Unreachable";
6535}
6536
6537void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006538 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006539 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006540 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6541 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006542 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006543
6544 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 case DataType::Type::kInt32:
6546 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006547 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006548 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6550 break;
6551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006552 case DataType::Type::kFloat32:
6553 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006554 InvokeRuntimeCallingConvention calling_convention;
6555 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6556 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6557 locations->SetOut(calling_convention.GetReturnLocation(type));
6558 break;
6559 }
6560
6561 default:
6562 LOG(FATAL) << "Unexpected rem type " << type;
6563 }
6564}
6565
6566void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006567 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006568
6569 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006570 case DataType::Type::kInt32:
6571 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006572 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006573 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006575 case DataType::Type::kFloat32:
6576 case DataType::Type::kFloat64: {
6577 QuickEntrypointEnum entrypoint =
6578 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006579 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006580 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006581 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6582 } else {
6583 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6584 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006585 break;
6586 }
6587 default:
6588 LOG(FATAL) << "Unexpected rem type " << type;
6589 }
6590}
6591
Igor Murashkind01745e2017-04-05 16:40:31 -07006592void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6593 constructor_fence->SetLocations(nullptr);
6594}
6595
6596void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6597 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6598 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6599}
6600
Alexey Frunze4dda3372015-06-01 18:31:49 -07006601void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6602 memory_barrier->SetLocations(nullptr);
6603}
6604
6605void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6606 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6607}
6608
6609void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006610 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006611 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006612 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6613}
6614
6615void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6616 codegen_->GenerateFrameExit();
6617}
6618
6619void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6620 ret->SetLocations(nullptr);
6621}
6622
6623void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6624 codegen_->GenerateFrameExit();
6625}
6626
Alexey Frunze92d90602015-12-18 18:16:36 -08006627void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6628 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006629}
6630
Alexey Frunze92d90602015-12-18 18:16:36 -08006631void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6632 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006633}
6634
Alexey Frunze4dda3372015-06-01 18:31:49 -07006635void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6636 HandleShift(shl);
6637}
6638
6639void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6640 HandleShift(shl);
6641}
6642
6643void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6644 HandleShift(shr);
6645}
6646
6647void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6648 HandleShift(shr);
6649}
6650
Alexey Frunze4dda3372015-06-01 18:31:49 -07006651void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6652 HandleBinaryOp(instruction);
6653}
6654
6655void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6656 HandleBinaryOp(instruction);
6657}
6658
6659void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6660 HandleFieldGet(instruction, instruction->GetFieldInfo());
6661}
6662
6663void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6664 HandleFieldGet(instruction, instruction->GetFieldInfo());
6665}
6666
6667void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6668 HandleFieldSet(instruction, instruction->GetFieldInfo());
6669}
6670
6671void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006672 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006673}
6674
Calin Juravlee460d1d2015-09-29 04:52:17 +01006675void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6676 HUnresolvedInstanceFieldGet* instruction) {
6677 FieldAccessCallingConventionMIPS64 calling_convention;
6678 codegen_->CreateUnresolvedFieldLocationSummary(
6679 instruction, instruction->GetFieldType(), calling_convention);
6680}
6681
6682void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6683 HUnresolvedInstanceFieldGet* instruction) {
6684 FieldAccessCallingConventionMIPS64 calling_convention;
6685 codegen_->GenerateUnresolvedFieldAccess(instruction,
6686 instruction->GetFieldType(),
6687 instruction->GetFieldIndex(),
6688 instruction->GetDexPc(),
6689 calling_convention);
6690}
6691
6692void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6693 HUnresolvedInstanceFieldSet* instruction) {
6694 FieldAccessCallingConventionMIPS64 calling_convention;
6695 codegen_->CreateUnresolvedFieldLocationSummary(
6696 instruction, instruction->GetFieldType(), calling_convention);
6697}
6698
6699void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6700 HUnresolvedInstanceFieldSet* instruction) {
6701 FieldAccessCallingConventionMIPS64 calling_convention;
6702 codegen_->GenerateUnresolvedFieldAccess(instruction,
6703 instruction->GetFieldType(),
6704 instruction->GetFieldIndex(),
6705 instruction->GetDexPc(),
6706 calling_convention);
6707}
6708
6709void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6710 HUnresolvedStaticFieldGet* instruction) {
6711 FieldAccessCallingConventionMIPS64 calling_convention;
6712 codegen_->CreateUnresolvedFieldLocationSummary(
6713 instruction, instruction->GetFieldType(), calling_convention);
6714}
6715
6716void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6717 HUnresolvedStaticFieldGet* instruction) {
6718 FieldAccessCallingConventionMIPS64 calling_convention;
6719 codegen_->GenerateUnresolvedFieldAccess(instruction,
6720 instruction->GetFieldType(),
6721 instruction->GetFieldIndex(),
6722 instruction->GetDexPc(),
6723 calling_convention);
6724}
6725
6726void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6727 HUnresolvedStaticFieldSet* instruction) {
6728 FieldAccessCallingConventionMIPS64 calling_convention;
6729 codegen_->CreateUnresolvedFieldLocationSummary(
6730 instruction, instruction->GetFieldType(), calling_convention);
6731}
6732
6733void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6734 HUnresolvedStaticFieldSet* instruction) {
6735 FieldAccessCallingConventionMIPS64 calling_convention;
6736 codegen_->GenerateUnresolvedFieldAccess(instruction,
6737 instruction->GetFieldType(),
6738 instruction->GetFieldIndex(),
6739 instruction->GetDexPc(),
6740 calling_convention);
6741}
6742
Alexey Frunze4dda3372015-06-01 18:31:49 -07006743void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006744 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6745 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006746 // In suspend check slow path, usually there are no caller-save registers at all.
6747 // If SIMD instructions are present, however, we force spilling all live SIMD
6748 // registers in full width (since the runtime only saves/restores lower part).
6749 locations->SetCustomSlowPathCallerSaves(
6750 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006751}
6752
6753void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6754 HBasicBlock* block = instruction->GetBlock();
6755 if (block->GetLoopInformation() != nullptr) {
6756 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6757 // The back edge will generate the suspend check.
6758 return;
6759 }
6760 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6761 // The goto will generate the suspend check.
6762 return;
6763 }
6764 GenerateSuspendCheck(instruction, nullptr);
6765}
6766
Alexey Frunze4dda3372015-06-01 18:31:49 -07006767void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006768 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6769 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006770 InvokeRuntimeCallingConvention calling_convention;
6771 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6772}
6773
6774void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006775 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006776 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6777}
6778
6779void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006780 DataType::Type input_type = conversion->GetInputType();
6781 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006782 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6783 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006785 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6786 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006787 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6788 }
6789
Vladimir Markoca6fff82017-10-03 14:49:14 +01006790 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006792 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006793 locations->SetInAt(0, Location::RequiresFpuRegister());
6794 } else {
6795 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006796 }
6797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006798 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006799 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006800 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006802 }
6803}
6804
6805void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6806 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006807 DataType::Type result_type = conversion->GetResultType();
6808 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006809
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006810 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6811 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006813 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006814 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6815 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6816
6817 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006818 case DataType::Type::kUint8:
6819 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006820 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006821 case DataType::Type::kInt8:
6822 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006823 // Type conversion from long to types narrower than int is a result of code
6824 // transformations. To avoid unpredictable results for SEB and SEH, we first
6825 // need to sign-extend the low 32-bit value into bits 32 through 63.
6826 __ Sll(dst, src, 0);
6827 __ Seb(dst, dst);
6828 } else {
6829 __ Seb(dst, src);
6830 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006831 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006832 case DataType::Type::kUint16:
6833 __ Andi(dst, src, 0xFFFF);
6834 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006835 case DataType::Type::kInt16:
6836 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006837 // Type conversion from long to types narrower than int is a result of code
6838 // transformations. To avoid unpredictable results for SEB and SEH, we first
6839 // need to sign-extend the low 32-bit value into bits 32 through 63.
6840 __ Sll(dst, src, 0);
6841 __ Seh(dst, dst);
6842 } else {
6843 __ Seh(dst, src);
6844 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006845 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006846 case DataType::Type::kInt32:
6847 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006848 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6849 // conversions, except when the input and output registers are the same and we are not
6850 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006851 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006852 __ Sll(dst, src, 0);
6853 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006854 break;
6855
6856 default:
6857 LOG(FATAL) << "Unexpected type conversion from " << input_type
6858 << " to " << result_type;
6859 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006860 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006861 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6862 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006863 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006864 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006865 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006866 __ Cvtsl(dst, FTMP);
6867 } else {
6868 __ Cvtdl(dst, FTMP);
6869 }
6870 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006871 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006872 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006873 __ Cvtsw(dst, FTMP);
6874 } else {
6875 __ Cvtdw(dst, FTMP);
6876 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006877 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006878 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6879 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006880 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6881 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006883 if (result_type == DataType::Type::kInt64) {
6884 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006885 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006886 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006887 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006888 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006889 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006890 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006891 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006892 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006893 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006894 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006895 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006896 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006897 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006898 } else if (DataType::IsFloatingPointType(result_type) &&
6899 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006900 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6901 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006902 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006903 __ Cvtsd(dst, src);
6904 } else {
6905 __ Cvtds(dst, src);
6906 }
6907 } else {
6908 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6909 << " to " << result_type;
6910 }
6911}
6912
6913void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6914 HandleShift(ushr);
6915}
6916
6917void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6918 HandleShift(ushr);
6919}
6920
6921void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6922 HandleBinaryOp(instruction);
6923}
6924
6925void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6926 HandleBinaryOp(instruction);
6927}
6928
6929void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6930 // Nothing to do, this should be removed during prepare for register allocator.
6931 LOG(FATAL) << "Unreachable";
6932}
6933
6934void InstructionCodeGeneratorMIPS64::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 LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006940 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006941}
6942
6943void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006944 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006945}
6946
6947void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006948 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006949}
6950
6951void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006952 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006953}
6954
6955void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006956 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006957}
6958
6959void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006960 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006961}
6962
6963void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006964 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006965}
6966
6967void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006968 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006969}
6970
6971void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006972 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006973}
6974
6975void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006976 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006977}
6978
6979void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006980 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006981}
6982
6983void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006984 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006985}
6986
Aart Bike9f37602015-10-09 11:15:55 -07006987void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006988 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006989}
6990
6991void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006992 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006993}
6994
6995void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006997}
6998
6999void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007001}
7002
7003void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007005}
7006
7007void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007009}
7010
7011void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007013}
7014
7015void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007017}
7018
Mark Mendellfe57faa2015-09-18 09:26:15 -04007019// Simple implementation of packed switch - generate cascaded compare/jumps.
7020void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7021 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007022 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007023 locations->SetInAt(0, Location::RequiresRegister());
7024}
7025
Alexey Frunze0960ac52016-12-20 17:24:59 -08007026void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7027 int32_t lower_bound,
7028 uint32_t num_entries,
7029 HBasicBlock* switch_block,
7030 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007031 // Create a set of compare/jumps.
7032 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007033 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007034 // Jump to default if index is negative
7035 // Note: We don't check the case that index is positive while value < lower_bound, because in
7036 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7037 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7038
Alexey Frunze0960ac52016-12-20 17:24:59 -08007039 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007040 // Jump to successors[0] if value == lower_bound.
7041 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7042 int32_t last_index = 0;
7043 for (; num_entries - last_index > 2; last_index += 2) {
7044 __ Addiu(temp_reg, temp_reg, -2);
7045 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7046 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7047 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7048 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7049 }
7050 if (num_entries - last_index == 2) {
7051 // The last missing case_value.
7052 __ Addiu(temp_reg, temp_reg, -1);
7053 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007054 }
7055
7056 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007057 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007058 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007059 }
7060}
7061
Alexey Frunze0960ac52016-12-20 17:24:59 -08007062void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7063 int32_t lower_bound,
7064 uint32_t num_entries,
7065 HBasicBlock* switch_block,
7066 HBasicBlock* default_block) {
7067 // Create a jump table.
7068 std::vector<Mips64Label*> labels(num_entries);
7069 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7070 for (uint32_t i = 0; i < num_entries; i++) {
7071 labels[i] = codegen_->GetLabelOf(successors[i]);
7072 }
7073 JumpTable* table = __ CreateJumpTable(std::move(labels));
7074
7075 // Is the value in range?
7076 __ Addiu32(TMP, value_reg, -lower_bound);
7077 __ LoadConst32(AT, num_entries);
7078 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7079
7080 // We are in the range of the table.
7081 // Load the target address from the jump table, indexing by the value.
7082 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007083 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007084 __ Lw(TMP, TMP, 0);
7085 // Compute the absolute target address by adding the table start address
7086 // (the table contains offsets to targets relative to its start).
7087 __ Daddu(TMP, TMP, AT);
7088 // And jump.
7089 __ Jr(TMP);
7090 __ Nop();
7091}
7092
7093void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7094 int32_t lower_bound = switch_instr->GetStartValue();
7095 uint32_t num_entries = switch_instr->GetNumEntries();
7096 LocationSummary* locations = switch_instr->GetLocations();
7097 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7098 HBasicBlock* switch_block = switch_instr->GetBlock();
7099 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7100
7101 if (num_entries > kPackedSwitchJumpTableThreshold) {
7102 GenTableBasedPackedSwitch(value_reg,
7103 lower_bound,
7104 num_entries,
7105 switch_block,
7106 default_block);
7107 } else {
7108 GenPackedSwitchWithCompares(value_reg,
7109 lower_bound,
7110 num_entries,
7111 switch_block,
7112 default_block);
7113 }
7114}
7115
Chris Larsenc9905a62017-03-13 17:06:18 -07007116void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7117 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007118 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007119 locations->SetInAt(0, Location::RequiresRegister());
7120 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007121}
7122
Chris Larsenc9905a62017-03-13 17:06:18 -07007123void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7124 LocationSummary* locations = instruction->GetLocations();
7125 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7126 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7127 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7128 __ LoadFromOffset(kLoadDoubleword,
7129 locations->Out().AsRegister<GpuRegister>(),
7130 locations->InAt(0).AsRegister<GpuRegister>(),
7131 method_offset);
7132 } else {
7133 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7134 instruction->GetIndex(), kMips64PointerSize));
7135 __ LoadFromOffset(kLoadDoubleword,
7136 locations->Out().AsRegister<GpuRegister>(),
7137 locations->InAt(0).AsRegister<GpuRegister>(),
7138 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7139 __ LoadFromOffset(kLoadDoubleword,
7140 locations->Out().AsRegister<GpuRegister>(),
7141 locations->Out().AsRegister<GpuRegister>(),
7142 method_offset);
7143 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007144}
7145
Alexey Frunze4dda3372015-06-01 18:31:49 -07007146} // namespace mips64
7147} // namespace art