blob: 6cbfa14f15f9eda96d689f545c2c4f63c20274cd [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 Marko61b92282017-10-11 13:23:17 +01004673 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4674 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004675 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004676 Location obj_loc = locations->InAt(0);
4677 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4678 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004679 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004680 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004681 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004682 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4683
Alexey Frunze4dda3372015-06-01 18:31:49 -07004684 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004685 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004686 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004687 load_type = kLoadUnsignedByte;
4688 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004689 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004690 load_type = kLoadSignedByte;
4691 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004693 load_type = kLoadUnsignedHalfword;
4694 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004695 case DataType::Type::kInt16:
4696 load_type = kLoadSignedHalfword;
4697 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004698 case DataType::Type::kInt32:
4699 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004700 load_type = kLoadWord;
4701 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004702 case DataType::Type::kInt64:
4703 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004704 load_type = kLoadDoubleword;
4705 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004706 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004707 load_type = kLoadUnsignedWord;
4708 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004709 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004710 LOG(FATAL) << "Unreachable type " << type;
4711 UNREACHABLE();
4712 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004713 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004714 DCHECK(dst_loc.IsRegister());
4715 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004716 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004717 // /* HeapReference<Object> */ dst = *(obj + offset)
4718 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004719 Location temp_loc =
4720 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004721 // Note that a potential implicit null check is handled in this
4722 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4723 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4724 dst_loc,
4725 obj,
4726 offset,
4727 temp_loc,
4728 /* needs_null_check */ true);
4729 if (is_volatile) {
4730 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4731 }
4732 } else {
4733 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4734 if (is_volatile) {
4735 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4736 }
4737 // If read barriers are enabled, emit read barriers other than
4738 // Baker's using a slow path (and also unpoison the loaded
4739 // reference, if heap poisoning is enabled).
4740 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4741 }
4742 } else {
4743 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4744 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004745 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004746 DCHECK(dst_loc.IsFpuRegister());
4747 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004748 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004749 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004750
Alexey Frunze15958152017-02-09 19:08:30 -08004751 // Memory barriers, in the case of references, are handled in the
4752 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004753 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004754 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004755 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004756}
4757
4758void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4759 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4760 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004761 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004762 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004763 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004764 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004765 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004766 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004767 }
4768}
4769
4770void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004771 const FieldInfo& field_info,
4772 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004773 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004774 LocationSummary* locations = instruction->GetLocations();
4775 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004776 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004777 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004778 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004779 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4780 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004781 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4782
Alexey Frunze4dda3372015-06-01 18:31:49 -07004783 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004784 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004785 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004786 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004787 store_type = kStoreByte;
4788 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004790 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004791 store_type = kStoreHalfword;
4792 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004793 case DataType::Type::kInt32:
4794 case DataType::Type::kFloat32:
4795 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004796 store_type = kStoreWord;
4797 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 case DataType::Type::kInt64:
4799 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004800 store_type = kStoreDoubleword;
4801 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004802 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004803 LOG(FATAL) << "Unreachable type " << type;
4804 UNREACHABLE();
4805 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004806
Alexey Frunze15958152017-02-09 19:08:30 -08004807 if (is_volatile) {
4808 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4809 }
4810
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004811 if (value_location.IsConstant()) {
4812 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4813 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4814 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004815 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004816 DCHECK(value_location.IsRegister());
4817 GpuRegister src = value_location.AsRegister<GpuRegister>();
4818 if (kPoisonHeapReferences && needs_write_barrier) {
4819 // Note that in the case where `value` is a null reference,
4820 // we do not enter this block, as a null reference does not
4821 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004822 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004823 __ PoisonHeapReference(TMP, src);
4824 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4825 } else {
4826 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4827 }
4828 } else {
4829 DCHECK(value_location.IsFpuRegister());
4830 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4831 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4832 }
4833 }
Alexey Frunze15958152017-02-09 19:08:30 -08004834
Alexey Frunzec061de12017-02-14 13:27:23 -08004835 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004836 DCHECK(value_location.IsRegister());
4837 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004838 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004839 }
Alexey Frunze15958152017-02-09 19:08:30 -08004840
4841 if (is_volatile) {
4842 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4843 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004844}
4845
4846void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4847 HandleFieldGet(instruction, instruction->GetFieldInfo());
4848}
4849
4850void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4851 HandleFieldGet(instruction, instruction->GetFieldInfo());
4852}
4853
4854void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4855 HandleFieldSet(instruction, instruction->GetFieldInfo());
4856}
4857
4858void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004859 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004860}
4861
Alexey Frunze15958152017-02-09 19:08:30 -08004862void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4863 HInstruction* instruction,
4864 Location out,
4865 uint32_t offset,
4866 Location maybe_temp,
4867 ReadBarrierOption read_barrier_option) {
4868 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4869 if (read_barrier_option == kWithReadBarrier) {
4870 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004871 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4872 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4873 }
Alexey Frunze15958152017-02-09 19:08:30 -08004874 if (kUseBakerReadBarrier) {
4875 // Load with fast path based Baker's read barrier.
4876 // /* HeapReference<Object> */ out = *(out + offset)
4877 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4878 out,
4879 out_reg,
4880 offset,
4881 maybe_temp,
4882 /* needs_null_check */ false);
4883 } else {
4884 // Load with slow path based read barrier.
4885 // Save the value of `out` into `maybe_temp` before overwriting it
4886 // in the following move operation, as we will need it for the
4887 // read barrier below.
4888 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4889 // /* HeapReference<Object> */ out = *(out + offset)
4890 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4891 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4892 }
4893 } else {
4894 // Plain load with no read barrier.
4895 // /* HeapReference<Object> */ out = *(out + offset)
4896 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4897 __ MaybeUnpoisonHeapReference(out_reg);
4898 }
4899}
4900
4901void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4902 HInstruction* instruction,
4903 Location out,
4904 Location obj,
4905 uint32_t offset,
4906 Location maybe_temp,
4907 ReadBarrierOption read_barrier_option) {
4908 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4909 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4910 if (read_barrier_option == kWithReadBarrier) {
4911 CHECK(kEmitCompilerReadBarrier);
4912 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004913 if (!kBakerReadBarrierThunksEnableForFields) {
4914 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4915 }
Alexey Frunze15958152017-02-09 19:08:30 -08004916 // Load with fast path based Baker's read barrier.
4917 // /* HeapReference<Object> */ out = *(obj + offset)
4918 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4919 out,
4920 obj_reg,
4921 offset,
4922 maybe_temp,
4923 /* needs_null_check */ false);
4924 } else {
4925 // Load with slow path based read barrier.
4926 // /* HeapReference<Object> */ out = *(obj + offset)
4927 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4928 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4929 }
4930 } else {
4931 // Plain load with no read barrier.
4932 // /* HeapReference<Object> */ out = *(obj + offset)
4933 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4934 __ MaybeUnpoisonHeapReference(out_reg);
4935 }
4936}
4937
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004938static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4939 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4940 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4941 return reg - V0;
4942 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4943 return 13 + (reg - S2);
4944 } else if (reg == S8) { // One more.
4945 return 19;
4946 }
4947 LOG(FATAL) << "Unexpected register " << reg;
4948 UNREACHABLE();
4949}
4950
4951static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4952 int num = GetBakerMarkThunkNumber(reg) +
4953 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4954 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4955}
4956
4957static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4958 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4959 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4960}
4961
4962void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4963 Location root,
4964 GpuRegister obj,
4965 uint32_t offset,
4966 ReadBarrierOption read_barrier_option,
4967 Mips64Label* label_low) {
4968 if (label_low != nullptr) {
4969 DCHECK_EQ(offset, 0x5678u);
4970 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004971 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004972 if (read_barrier_option == kWithReadBarrier) {
4973 DCHECK(kEmitCompilerReadBarrier);
4974 if (kUseBakerReadBarrier) {
4975 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4976 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004977 if (kBakerReadBarrierThunksEnableForGcRoots) {
4978 // Note that we do not actually check the value of `GetIsGcMarking()`
4979 // to decide whether to mark the loaded GC root or not. Instead, we
4980 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4981 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4982 // vice versa.
4983 //
4984 // We use thunks for the slow path. That thunk checks the reference
4985 // and jumps to the entrypoint if needed.
4986 //
4987 // temp = Thread::Current()->pReadBarrierMarkReg00
4988 // // AKA &art_quick_read_barrier_mark_introspection.
4989 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4990 // if (temp != nullptr) {
4991 // temp = &gc_root_thunk<root_reg>
4992 // root = temp(root)
4993 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004994
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004995 const int32_t entry_point_offset =
4996 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4997 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4998 int16_t offset_low = Low16Bits(offset);
4999 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5000 // extension in lwu.
5001 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5002 GpuRegister base = short_offset ? obj : TMP;
5003 // Loading the entrypoint does not require a load acquire since it is only changed when
5004 // threads are suspended or running a checkpoint.
5005 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5006 if (!short_offset) {
5007 DCHECK(!label_low);
5008 __ Daui(base, obj, offset_high);
5009 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005010 Mips64Label skip_call;
5011 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005012 if (label_low != nullptr) {
5013 DCHECK(short_offset);
5014 __ Bind(label_low);
5015 }
5016 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5017 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5018 // in delay slot.
5019 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005020 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005021 } else {
5022 // Note that we do not actually check the value of `GetIsGcMarking()`
5023 // to decide whether to mark the loaded GC root or not. Instead, we
5024 // load into `temp` (T9) the read barrier mark entry point corresponding
5025 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5026 // is false, and vice versa.
5027 //
5028 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5029 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5030 // if (temp != null) {
5031 // root = temp(root)
5032 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005033
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005034 if (label_low != nullptr) {
5035 __ Bind(label_low);
5036 }
5037 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5038 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5039 static_assert(
5040 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5041 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5042 "have different sizes.");
5043 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5044 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5045 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005046
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005047 // Slow path marking the GC root `root`.
5048 Location temp = Location::RegisterLocation(T9);
5049 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005050 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005051 instruction,
5052 root,
5053 /*entrypoint*/ temp);
5054 codegen_->AddSlowPath(slow_path);
5055
5056 const int32_t entry_point_offset =
5057 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5058 // Loading the entrypoint does not require a load acquire since it is only changed when
5059 // threads are suspended or running a checkpoint.
5060 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5061 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5062 __ Bind(slow_path->GetExitLabel());
5063 }
Alexey Frunze15958152017-02-09 19:08:30 -08005064 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005065 if (label_low != nullptr) {
5066 __ Bind(label_low);
5067 }
Alexey Frunze15958152017-02-09 19:08:30 -08005068 // GC root loaded through a slow path for read barriers other
5069 // than Baker's.
5070 // /* GcRoot<mirror::Object>* */ root = obj + offset
5071 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5072 // /* mirror::Object* */ root = root->Read()
5073 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5074 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005075 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005076 if (label_low != nullptr) {
5077 __ Bind(label_low);
5078 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005079 // Plain GC root load with no read barrier.
5080 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5081 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5082 // Note that GC roots are not affected by heap poisoning, thus we
5083 // do not have to unpoison `root_reg` here.
5084 }
5085}
5086
Alexey Frunze15958152017-02-09 19:08:30 -08005087void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5088 Location ref,
5089 GpuRegister obj,
5090 uint32_t offset,
5091 Location temp,
5092 bool needs_null_check) {
5093 DCHECK(kEmitCompilerReadBarrier);
5094 DCHECK(kUseBakerReadBarrier);
5095
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005096 if (kBakerReadBarrierThunksEnableForFields) {
5097 // Note that we do not actually check the value of `GetIsGcMarking()`
5098 // to decide whether to mark the loaded reference or not. Instead, we
5099 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5100 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5101 // vice versa.
5102 //
5103 // We use thunks for the slow path. That thunk checks the reference
5104 // and jumps to the entrypoint if needed. If the holder is not gray,
5105 // it issues a load-load memory barrier and returns to the original
5106 // reference load.
5107 //
5108 // temp = Thread::Current()->pReadBarrierMarkReg00
5109 // // AKA &art_quick_read_barrier_mark_introspection.
5110 // if (temp != nullptr) {
5111 // temp = &field_array_thunk<holder_reg>
5112 // temp()
5113 // }
5114 // not_gray_return_address:
5115 // // If the offset is too large to fit into the lw instruction, we
5116 // // use an adjusted base register (TMP) here. This register
5117 // // receives bits 16 ... 31 of the offset before the thunk invocation
5118 // // and the thunk benefits from it.
5119 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5120 // gray_return_address:
5121
5122 DCHECK(temp.IsInvalid());
5123 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5124 const int32_t entry_point_offset =
5125 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5126 // There may have or may have not been a null check if the field offset is smaller than
5127 // the page size.
5128 // There must've been a null check in case it's actually a load from an array.
5129 // We will, however, perform an explicit null check in the thunk as it's easier to
5130 // do it than not.
5131 if (instruction->IsArrayGet()) {
5132 DCHECK(!needs_null_check);
5133 }
5134 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5135 // Loading the entrypoint does not require a load acquire since it is only changed when
5136 // threads are suspended or running a checkpoint.
5137 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5138 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005139 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005140 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005141 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005142 __ Nop(); // In forbidden slot.
5143 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005144 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005145 // /* HeapReference<Object> */ ref = *(obj + offset)
5146 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5147 } else {
5148 int16_t offset_low = Low16Bits(offset);
5149 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005150 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005151 __ Daui(TMP, obj, offset_high); // In delay slot.
5152 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005153 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005154 // /* HeapReference<Object> */ ref = *(obj + offset)
5155 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5156 }
5157 if (needs_null_check) {
5158 MaybeRecordImplicitNullCheck(instruction);
5159 }
5160 __ MaybeUnpoisonHeapReference(ref_reg);
5161 return;
5162 }
5163
Alexey Frunze15958152017-02-09 19:08:30 -08005164 // /* HeapReference<Object> */ ref = *(obj + offset)
5165 Location no_index = Location::NoLocation();
5166 ScaleFactor no_scale_factor = TIMES_1;
5167 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5168 ref,
5169 obj,
5170 offset,
5171 no_index,
5172 no_scale_factor,
5173 temp,
5174 needs_null_check);
5175}
5176
5177void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5178 Location ref,
5179 GpuRegister obj,
5180 uint32_t data_offset,
5181 Location index,
5182 Location temp,
5183 bool needs_null_check) {
5184 DCHECK(kEmitCompilerReadBarrier);
5185 DCHECK(kUseBakerReadBarrier);
5186
5187 static_assert(
5188 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5189 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005190 ScaleFactor scale_factor = TIMES_4;
5191
5192 if (kBakerReadBarrierThunksEnableForArrays) {
5193 // Note that we do not actually check the value of `GetIsGcMarking()`
5194 // to decide whether to mark the loaded reference or not. Instead, we
5195 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5196 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5197 // vice versa.
5198 //
5199 // We use thunks for the slow path. That thunk checks the reference
5200 // and jumps to the entrypoint if needed. If the holder is not gray,
5201 // it issues a load-load memory barrier and returns to the original
5202 // reference load.
5203 //
5204 // temp = Thread::Current()->pReadBarrierMarkReg00
5205 // // AKA &art_quick_read_barrier_mark_introspection.
5206 // if (temp != nullptr) {
5207 // temp = &field_array_thunk<holder_reg>
5208 // temp()
5209 // }
5210 // not_gray_return_address:
5211 // // The element address is pre-calculated in the TMP register before the
5212 // // thunk invocation and the thunk benefits from it.
5213 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5214 // gray_return_address:
5215
5216 DCHECK(temp.IsInvalid());
5217 DCHECK(index.IsValid());
5218 const int32_t entry_point_offset =
5219 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5220 // We will not do the explicit null check in the thunk as some form of a null check
5221 // must've been done earlier.
5222 DCHECK(!needs_null_check);
5223 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5224 // Loading the entrypoint does not require a load acquire since it is only changed when
5225 // threads are suspended or running a checkpoint.
5226 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005227 Mips64Label skip_call;
5228 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005229 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5230 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5231 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5232 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005233 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005234 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5235 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5236 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5237 __ MaybeUnpoisonHeapReference(ref_reg);
5238 return;
5239 }
5240
Alexey Frunze15958152017-02-09 19:08:30 -08005241 // /* HeapReference<Object> */ ref =
5242 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005243 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5244 ref,
5245 obj,
5246 data_offset,
5247 index,
5248 scale_factor,
5249 temp,
5250 needs_null_check);
5251}
5252
5253void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5254 Location ref,
5255 GpuRegister obj,
5256 uint32_t offset,
5257 Location index,
5258 ScaleFactor scale_factor,
5259 Location temp,
5260 bool needs_null_check,
5261 bool always_update_field) {
5262 DCHECK(kEmitCompilerReadBarrier);
5263 DCHECK(kUseBakerReadBarrier);
5264
5265 // In slow path based read barriers, the read barrier call is
5266 // inserted after the original load. However, in fast path based
5267 // Baker's read barriers, we need to perform the load of
5268 // mirror::Object::monitor_ *before* the original reference load.
5269 // This load-load ordering is required by the read barrier.
5270 // The fast path/slow path (for Baker's algorithm) should look like:
5271 //
5272 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5273 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5274 // HeapReference<Object> ref = *src; // Original reference load.
5275 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5276 // if (is_gray) {
5277 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5278 // }
5279 //
5280 // Note: the original implementation in ReadBarrier::Barrier is
5281 // slightly more complex as it performs additional checks that we do
5282 // not do here for performance reasons.
5283
5284 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5285 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5286 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5287
5288 // /* int32_t */ monitor = obj->monitor_
5289 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5290 if (needs_null_check) {
5291 MaybeRecordImplicitNullCheck(instruction);
5292 }
5293 // /* LockWord */ lock_word = LockWord(monitor)
5294 static_assert(sizeof(LockWord) == sizeof(int32_t),
5295 "art::LockWord and int32_t have different sizes.");
5296
5297 __ Sync(0); // Barrier to prevent load-load reordering.
5298
5299 // The actual reference load.
5300 if (index.IsValid()) {
5301 // Load types involving an "index": ArrayGet,
5302 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5303 // intrinsics.
5304 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5305 if (index.IsConstant()) {
5306 size_t computed_offset =
5307 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5308 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5309 } else {
5310 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005311 if (scale_factor == TIMES_1) {
5312 __ Daddu(TMP, index_reg, obj);
5313 } else {
5314 __ Dlsa(TMP, index_reg, obj, scale_factor);
5315 }
Alexey Frunze15958152017-02-09 19:08:30 -08005316 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5317 }
5318 } else {
5319 // /* HeapReference<Object> */ ref = *(obj + offset)
5320 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5321 }
5322
5323 // Object* ref = ref_addr->AsMirrorPtr()
5324 __ MaybeUnpoisonHeapReference(ref_reg);
5325
5326 // Slow path marking the object `ref` when it is gray.
5327 SlowPathCodeMIPS64* slow_path;
5328 if (always_update_field) {
5329 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5330 // of the form `obj + field_offset`, where `obj` is a register and
5331 // `field_offset` is a register. Thus `offset` and `scale_factor`
5332 // above are expected to be null in this code path.
5333 DCHECK_EQ(offset, 0u);
5334 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005335 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005336 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5337 ref,
5338 obj,
5339 /* field_offset */ index,
5340 temp_reg);
5341 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005342 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005343 }
5344 AddSlowPath(slow_path);
5345
5346 // if (rb_state == ReadBarrier::GrayState())
5347 // ref = ReadBarrier::Mark(ref);
5348 // Given the numeric representation, it's enough to check the low bit of the
5349 // rb_state. We do that by shifting the bit into the sign bit (31) and
5350 // performing a branch on less than zero.
5351 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5352 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5353 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5354 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5355 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5356 __ Bind(slow_path->GetExitLabel());
5357}
5358
5359void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5360 Location out,
5361 Location ref,
5362 Location obj,
5363 uint32_t offset,
5364 Location index) {
5365 DCHECK(kEmitCompilerReadBarrier);
5366
5367 // Insert a slow path based read barrier *after* the reference load.
5368 //
5369 // If heap poisoning is enabled, the unpoisoning of the loaded
5370 // reference will be carried out by the runtime within the slow
5371 // path.
5372 //
5373 // Note that `ref` currently does not get unpoisoned (when heap
5374 // poisoning is enabled), which is alright as the `ref` argument is
5375 // not used by the artReadBarrierSlow entry point.
5376 //
5377 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005378 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005379 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5380 AddSlowPath(slow_path);
5381
5382 __ Bc(slow_path->GetEntryLabel());
5383 __ Bind(slow_path->GetExitLabel());
5384}
5385
5386void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5387 Location out,
5388 Location ref,
5389 Location obj,
5390 uint32_t offset,
5391 Location index) {
5392 if (kEmitCompilerReadBarrier) {
5393 // Baker's read barriers shall be handled by the fast path
5394 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5395 DCHECK(!kUseBakerReadBarrier);
5396 // If heap poisoning is enabled, unpoisoning will be taken care of
5397 // by the runtime within the slow path.
5398 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5399 } else if (kPoisonHeapReferences) {
5400 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5401 }
5402}
5403
5404void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5405 Location out,
5406 Location root) {
5407 DCHECK(kEmitCompilerReadBarrier);
5408
5409 // Insert a slow path based read barrier *after* the GC root load.
5410 //
5411 // Note that GC roots are not affected by heap poisoning, so we do
5412 // not need to do anything special for this here.
5413 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005414 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005415 AddSlowPath(slow_path);
5416
5417 __ Bc(slow_path->GetEntryLabel());
5418 __ Bind(slow_path->GetExitLabel());
5419}
5420
Alexey Frunze4dda3372015-06-01 18:31:49 -07005421void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005422 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5423 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005424 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005425 switch (type_check_kind) {
5426 case TypeCheckKind::kExactCheck:
5427 case TypeCheckKind::kAbstractClassCheck:
5428 case TypeCheckKind::kClassHierarchyCheck:
5429 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005430 call_kind =
5431 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005432 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005433 break;
5434 case TypeCheckKind::kArrayCheck:
5435 case TypeCheckKind::kUnresolvedCheck:
5436 case TypeCheckKind::kInterfaceCheck:
5437 call_kind = LocationSummary::kCallOnSlowPath;
5438 break;
5439 }
5440
Vladimir Markoca6fff82017-10-03 14:49:14 +01005441 LocationSummary* locations =
5442 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005443 if (baker_read_barrier_slow_path) {
5444 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5445 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005446 locations->SetInAt(0, Location::RequiresRegister());
5447 locations->SetInAt(1, Location::RequiresRegister());
5448 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005449 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005451 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005452}
5453
5454void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005455 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005456 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005457 Location obj_loc = locations->InAt(0);
5458 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005459 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005460 Location out_loc = locations->Out();
5461 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5462 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5463 DCHECK_LE(num_temps, 1u);
5464 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005465 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5466 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5467 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5468 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005469 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005470 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005471
5472 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005473 // Avoid this check if we know `obj` is not null.
5474 if (instruction->MustDoNullCheck()) {
5475 __ Move(out, ZERO);
5476 __ Beqzc(obj, &done);
5477 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005478
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005479 switch (type_check_kind) {
5480 case TypeCheckKind::kExactCheck: {
5481 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005482 GenerateReferenceLoadTwoRegisters(instruction,
5483 out_loc,
5484 obj_loc,
5485 class_offset,
5486 maybe_temp_loc,
5487 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005488 // Classes must be equal for the instanceof to succeed.
5489 __ Xor(out, out, cls);
5490 __ Sltiu(out, out, 1);
5491 break;
5492 }
5493
5494 case TypeCheckKind::kAbstractClassCheck: {
5495 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005496 GenerateReferenceLoadTwoRegisters(instruction,
5497 out_loc,
5498 obj_loc,
5499 class_offset,
5500 maybe_temp_loc,
5501 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005502 // If the class is abstract, we eagerly fetch the super class of the
5503 // object to avoid doing a comparison we know will fail.
5504 Mips64Label loop;
5505 __ Bind(&loop);
5506 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005507 GenerateReferenceLoadOneRegister(instruction,
5508 out_loc,
5509 super_offset,
5510 maybe_temp_loc,
5511 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005512 // If `out` is null, we use it for the result, and jump to `done`.
5513 __ Beqzc(out, &done);
5514 __ Bnec(out, cls, &loop);
5515 __ LoadConst32(out, 1);
5516 break;
5517 }
5518
5519 case TypeCheckKind::kClassHierarchyCheck: {
5520 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005521 GenerateReferenceLoadTwoRegisters(instruction,
5522 out_loc,
5523 obj_loc,
5524 class_offset,
5525 maybe_temp_loc,
5526 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005527 // Walk over the class hierarchy to find a match.
5528 Mips64Label loop, success;
5529 __ Bind(&loop);
5530 __ Beqc(out, cls, &success);
5531 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005532 GenerateReferenceLoadOneRegister(instruction,
5533 out_loc,
5534 super_offset,
5535 maybe_temp_loc,
5536 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005537 __ Bnezc(out, &loop);
5538 // If `out` is null, we use it for the result, and jump to `done`.
5539 __ Bc(&done);
5540 __ Bind(&success);
5541 __ LoadConst32(out, 1);
5542 break;
5543 }
5544
5545 case TypeCheckKind::kArrayObjectCheck: {
5546 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005547 GenerateReferenceLoadTwoRegisters(instruction,
5548 out_loc,
5549 obj_loc,
5550 class_offset,
5551 maybe_temp_loc,
5552 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005553 // Do an exact check.
5554 Mips64Label success;
5555 __ Beqc(out, cls, &success);
5556 // Otherwise, we need to check that the object's class is a non-primitive array.
5557 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005558 GenerateReferenceLoadOneRegister(instruction,
5559 out_loc,
5560 component_offset,
5561 maybe_temp_loc,
5562 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005563 // If `out` is null, we use it for the result, and jump to `done`.
5564 __ Beqzc(out, &done);
5565 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5566 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5567 __ Sltiu(out, out, 1);
5568 __ Bc(&done);
5569 __ Bind(&success);
5570 __ LoadConst32(out, 1);
5571 break;
5572 }
5573
5574 case TypeCheckKind::kArrayCheck: {
5575 // No read barrier since the slow path will retry upon failure.
5576 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005577 GenerateReferenceLoadTwoRegisters(instruction,
5578 out_loc,
5579 obj_loc,
5580 class_offset,
5581 maybe_temp_loc,
5582 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005583 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005584 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5585 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005586 codegen_->AddSlowPath(slow_path);
5587 __ Bnec(out, cls, slow_path->GetEntryLabel());
5588 __ LoadConst32(out, 1);
5589 break;
5590 }
5591
5592 case TypeCheckKind::kUnresolvedCheck:
5593 case TypeCheckKind::kInterfaceCheck: {
5594 // Note that we indeed only call on slow path, but we always go
5595 // into the slow path for the unresolved and interface check
5596 // cases.
5597 //
5598 // We cannot directly call the InstanceofNonTrivial runtime
5599 // entry point without resorting to a type checking slow path
5600 // here (i.e. by calling InvokeRuntime directly), as it would
5601 // require to assign fixed registers for the inputs of this
5602 // HInstanceOf instruction (following the runtime calling
5603 // convention), which might be cluttered by the potential first
5604 // read barrier emission at the beginning of this method.
5605 //
5606 // TODO: Introduce a new runtime entry point taking the object
5607 // to test (instead of its class) as argument, and let it deal
5608 // with the read barrier issues. This will let us refactor this
5609 // case of the `switch` code as it was previously (with a direct
5610 // call to the runtime not using a type checking slow path).
5611 // This should also be beneficial for the other cases above.
5612 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005613 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5614 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005615 codegen_->AddSlowPath(slow_path);
5616 __ Bc(slow_path->GetEntryLabel());
5617 break;
5618 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005619 }
5620
5621 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005622
5623 if (slow_path != nullptr) {
5624 __ Bind(slow_path->GetExitLabel());
5625 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005626}
5627
5628void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005629 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005630 locations->SetOut(Location::ConstantLocation(constant));
5631}
5632
5633void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5634 // Will be generated at use site.
5635}
5636
5637void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005638 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005639 locations->SetOut(Location::ConstantLocation(constant));
5640}
5641
5642void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5643 // Will be generated at use site.
5644}
5645
Calin Juravle175dc732015-08-25 15:42:32 +01005646void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5647 // The trampoline uses the same calling convention as dex calling conventions,
5648 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5649 // the method_idx.
5650 HandleInvoke(invoke);
5651}
5652
5653void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5654 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5655}
5656
Alexey Frunze4dda3372015-06-01 18:31:49 -07005657void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5658 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5659 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5660}
5661
5662void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5663 HandleInvoke(invoke);
5664 // The register T0 is required to be used for the hidden argument in
5665 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5666 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5667}
5668
5669void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5670 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5671 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005672 Location receiver = invoke->GetLocations()->InAt(0);
5673 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005674 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005675
5676 // Set the hidden argument.
5677 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5678 invoke->GetDexMethodIndex());
5679
5680 // temp = object->GetClass();
5681 if (receiver.IsStackSlot()) {
5682 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5683 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5684 } else {
5685 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5686 }
5687 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005688 // Instead of simply (possibly) unpoisoning `temp` here, we should
5689 // emit a read barrier for the previous class reference load.
5690 // However this is not required in practice, as this is an
5691 // intermediate/temporary reference and because the current
5692 // concurrent copying collector keeps the from-space memory
5693 // intact/accessible until the end of the marking phase (the
5694 // concurrent copying collector may not in the future).
5695 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005696 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5697 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5698 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005699 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005700 // temp = temp->GetImtEntryAt(method_offset);
5701 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5702 // T9 = temp->GetEntryPoint();
5703 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5704 // T9();
5705 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005706 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005707 DCHECK(!codegen_->IsLeafMethod());
5708 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5709}
5710
5711void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005712 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5713 if (intrinsic.TryDispatch(invoke)) {
5714 return;
5715 }
5716
Alexey Frunze4dda3372015-06-01 18:31:49 -07005717 HandleInvoke(invoke);
5718}
5719
5720void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005721 // Explicit clinit checks triggered by static invokes must have been pruned by
5722 // art::PrepareForRegisterAllocation.
5723 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005724
Chris Larsen3039e382015-08-26 07:54:08 -07005725 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5726 if (intrinsic.TryDispatch(invoke)) {
5727 return;
5728 }
5729
Alexey Frunze4dda3372015-06-01 18:31:49 -07005730 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005731}
5732
Orion Hodsonac141392017-01-13 11:53:47 +00005733void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5734 HandleInvoke(invoke);
5735}
5736
5737void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5738 codegen_->GenerateInvokePolymorphicCall(invoke);
5739}
5740
Chris Larsen3039e382015-08-26 07:54:08 -07005741static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005742 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005743 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5744 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005745 return true;
5746 }
5747 return false;
5748}
5749
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005750HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005751 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005752 bool fallback_load = false;
5753 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005754 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005755 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005756 case HLoadString::LoadKind::kBssEntry:
5757 DCHECK(!Runtime::Current()->UseJitCompilation());
5758 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005759 case HLoadString::LoadKind::kJitTableAddress:
5760 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005761 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005762 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005763 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005764 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005765 }
5766 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005767 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005768 }
5769 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005770}
5771
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005772HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5773 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005774 bool fallback_load = false;
5775 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005776 case HLoadClass::LoadKind::kInvalid:
5777 LOG(FATAL) << "UNREACHABLE";
5778 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005779 case HLoadClass::LoadKind::kReferrersClass:
5780 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005781 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005782 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005783 case HLoadClass::LoadKind::kBssEntry:
5784 DCHECK(!Runtime::Current()->UseJitCompilation());
5785 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005786 case HLoadClass::LoadKind::kJitTableAddress:
5787 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005788 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005789 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005790 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005791 break;
5792 }
5793 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005794 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005795 }
5796 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005797}
5798
Vladimir Markodc151b22015-10-15 18:02:30 +01005799HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5800 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005801 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005802 // On MIPS64 we support all dispatch types.
5803 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005804}
5805
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005806void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5807 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005808 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005809 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005810 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5811 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5812
Alexey Frunze19f6c692016-11-30 19:19:55 -08005813 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005814 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005815 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005816 uint32_t offset =
5817 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005818 __ LoadFromOffset(kLoadDoubleword,
5819 temp.AsRegister<GpuRegister>(),
5820 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005821 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005822 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005823 }
Vladimir Marko58155012015-08-19 12:49:41 +00005824 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005825 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005826 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005827 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5828 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005829 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005830 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005831 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5832 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5833 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005834 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5835 break;
5836 }
Vladimir Marko58155012015-08-19 12:49:41 +00005837 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005838 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5839 kLoadDoubleword,
5840 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005841 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005842 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005843 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005844 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005845 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5846 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5847 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005848 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5849 break;
5850 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005851 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5852 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5853 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005854 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005855 }
5856
Alexey Frunze19f6c692016-11-30 19:19:55 -08005857 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005858 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005859 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005860 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005861 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5862 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5863 __ LoadFromOffset(kLoadDoubleword,
5864 T9,
5865 callee_method.AsRegister<GpuRegister>(),
5866 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005867 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005868 // T9()
5869 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005870 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005871 break;
5872 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005873 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5874
Alexey Frunze4dda3372015-06-01 18:31:49 -07005875 DCHECK(!IsLeafMethod());
5876}
5877
5878void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005879 // Explicit clinit checks triggered by static invokes must have been pruned by
5880 // art::PrepareForRegisterAllocation.
5881 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005882
5883 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5884 return;
5885 }
5886
5887 LocationSummary* locations = invoke->GetLocations();
5888 codegen_->GenerateStaticOrDirectCall(invoke,
5889 locations->HasTemps()
5890 ? locations->GetTemp(0)
5891 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005892}
5893
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005894void CodeGeneratorMIPS64::GenerateVirtualCall(
5895 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005896 // Use the calling convention instead of the location of the receiver, as
5897 // intrinsics may have put the receiver in a different register. In the intrinsics
5898 // slow path, the arguments have been moved to the right place, so here we are
5899 // guaranteed that the receiver is the first register of the calling convention.
5900 InvokeDexCallingConvention calling_convention;
5901 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5902
Alexey Frunze53afca12015-11-05 16:34:23 -08005903 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005904 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5905 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5906 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005907 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005908
5909 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005910 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005911 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005912 // Instead of simply (possibly) unpoisoning `temp` here, we should
5913 // emit a read barrier for the previous class reference load.
5914 // However this is not required in practice, as this is an
5915 // intermediate/temporary reference and because the current
5916 // concurrent copying collector keeps the from-space memory
5917 // intact/accessible until the end of the marking phase (the
5918 // concurrent copying collector may not in the future).
5919 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005920 // temp = temp->GetMethodAt(method_offset);
5921 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5922 // T9 = temp->GetEntryPoint();
5923 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5924 // T9();
5925 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005926 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005927 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005928}
5929
5930void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5931 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5932 return;
5933 }
5934
5935 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005936 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005937}
5938
5939void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005940 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005941 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005942 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005943 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5944 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005945 return;
5946 }
Vladimir Marko41559982017-01-06 14:04:23 +00005947 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005948
Alexey Frunze15958152017-02-09 19:08:30 -08005949 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5950 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005951 ? LocationSummary::kCallOnSlowPath
5952 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005953 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005954 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5955 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5956 }
Vladimir Marko41559982017-01-06 14:04:23 +00005957 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005958 locations->SetInAt(0, Location::RequiresRegister());
5959 }
5960 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005961 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5962 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5963 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005964 // Request a temp to hold the BSS entry location for the slow path.
5965 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005966 RegisterSet caller_saves = RegisterSet::Empty();
5967 InvokeRuntimeCallingConvention calling_convention;
5968 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5969 locations->SetCustomSlowPathCallerSaves(caller_saves);
5970 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005971 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005972 }
5973 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005974}
5975
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005976// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5977// move.
5978void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005979 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005980 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005981 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005982 return;
5983 }
Vladimir Marko41559982017-01-06 14:04:23 +00005984 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005985
Vladimir Marko41559982017-01-06 14:04:23 +00005986 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005987 Location out_loc = locations->Out();
5988 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5989 GpuRegister current_method_reg = ZERO;
5990 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005991 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005992 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5993 }
5994
Alexey Frunze15958152017-02-09 19:08:30 -08005995 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5996 ? kWithoutReadBarrier
5997 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005998 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005999 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006000 switch (load_kind) {
6001 case HLoadClass::LoadKind::kReferrersClass:
6002 DCHECK(!cls->CanCallRuntime());
6003 DCHECK(!cls->MustGenerateClinitCheck());
6004 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6005 GenerateGcRootFieldLoad(cls,
6006 out_loc,
6007 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006008 ArtMethod::DeclaringClassOffset().Int32Value(),
6009 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006010 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006011 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006012 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006013 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006014 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006015 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006016 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6017 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6018 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006019 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6020 break;
6021 }
6022 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006023 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006024 uint32_t address = dchecked_integral_cast<uint32_t>(
6025 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6026 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006027 __ LoadLiteral(out,
6028 kLoadUnsignedWord,
6029 codegen_->DeduplicateBootImageAddressLiteral(address));
6030 break;
6031 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006032 case HLoadClass::LoadKind::kBootImageClassTable: {
6033 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6034 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6035 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6036 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6037 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6038 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6039 __ Lwu(out, AT, /* placeholder */ 0x5678);
6040 // Extract the reference from the slot data, i.e. clear the hash bits.
6041 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6042 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6043 if (masked_hash != 0) {
6044 __ Daddiu(out, out, -masked_hash);
6045 }
6046 break;
6047 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006048 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006049 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
6050 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6051 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
6052 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6053 GpuRegister temp = non_baker_read_barrier
6054 ? out
6055 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006056 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
6057 GenerateGcRootFieldLoad(cls,
6058 out_loc,
6059 temp,
6060 /* placeholder */ 0x5678,
6061 read_barrier_option,
6062 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006063 generate_null_check = true;
6064 break;
6065 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006066 case HLoadClass::LoadKind::kJitTableAddress:
6067 __ LoadLiteral(out,
6068 kLoadUnsignedWord,
6069 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6070 cls->GetTypeIndex(),
6071 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006072 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006073 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006074 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006075 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006076 LOG(FATAL) << "UNREACHABLE";
6077 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006078 }
6079
6080 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6081 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006082 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006083 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006084 codegen_->AddSlowPath(slow_path);
6085 if (generate_null_check) {
6086 __ Beqzc(out, slow_path->GetEntryLabel());
6087 }
6088 if (cls->MustGenerateClinitCheck()) {
6089 GenerateClassInitializationCheck(slow_path, out);
6090 } else {
6091 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006092 }
6093 }
6094}
6095
David Brazdilcb1c0552015-08-04 16:22:25 +01006096static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006097 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006098}
6099
Alexey Frunze4dda3372015-06-01 18:31:49 -07006100void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6101 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006102 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006103 locations->SetOut(Location::RequiresRegister());
6104}
6105
6106void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6107 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006108 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6109}
6110
6111void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006112 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006113}
6114
6115void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6116 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006117}
6118
Alexey Frunze4dda3372015-06-01 18:31:49 -07006119void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006120 HLoadString::LoadKind load_kind = load->GetLoadKind();
6121 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006122 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006123 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006124 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006125 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006126 } else {
6127 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006128 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6129 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6130 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006131 // Request a temp to hold the BSS entry location for the slow path.
6132 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006133 RegisterSet caller_saves = RegisterSet::Empty();
6134 InvokeRuntimeCallingConvention calling_convention;
6135 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6136 locations->SetCustomSlowPathCallerSaves(caller_saves);
6137 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006138 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006139 }
6140 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006141 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006142}
6143
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006144// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6145// move.
6146void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006147 HLoadString::LoadKind load_kind = load->GetLoadKind();
6148 LocationSummary* locations = load->GetLocations();
6149 Location out_loc = locations->Out();
6150 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6151
6152 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006153 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6154 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006155 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006156 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006157 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6158 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6159 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006160 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006161 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006162 }
6163 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006164 uint32_t address = dchecked_integral_cast<uint32_t>(
6165 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6166 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006167 __ LoadLiteral(out,
6168 kLoadUnsignedWord,
6169 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006170 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006171 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006172 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006173 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006174 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006175 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006176 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6177 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006178 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6179 __ Lwu(out, AT, /* placeholder */ 0x5678);
6180 return;
6181 }
6182 case HLoadString::LoadKind::kBssEntry: {
6183 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6184 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6185 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6186 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6187 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006188 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6189 GpuRegister temp = non_baker_read_barrier
6190 ? out
6191 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006192 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08006193 GenerateGcRootFieldLoad(load,
6194 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006195 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08006196 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006197 kCompilerReadBarrierOption,
6198 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006199 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006200 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006201 codegen_->AddSlowPath(slow_path);
6202 __ Beqzc(out, slow_path->GetEntryLabel());
6203 __ Bind(slow_path->GetExitLabel());
6204 return;
6205 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006206 case HLoadString::LoadKind::kJitTableAddress:
6207 __ LoadLiteral(out,
6208 kLoadUnsignedWord,
6209 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6210 load->GetStringIndex(),
6211 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006212 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006213 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006214 default:
6215 break;
6216 }
6217
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006218 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006219 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006220 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006221 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006222 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6223 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6224 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006225}
6226
Alexey Frunze4dda3372015-06-01 18:31:49 -07006227void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006228 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006229 locations->SetOut(Location::ConstantLocation(constant));
6230}
6231
6232void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6233 // Will be generated at use site.
6234}
6235
6236void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006237 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6238 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006239 InvokeRuntimeCallingConvention calling_convention;
6240 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6241}
6242
6243void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006244 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006245 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006246 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006247 if (instruction->IsEnter()) {
6248 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6249 } else {
6250 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6251 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006252}
6253
6254void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6255 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006256 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006257 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006258 case DataType::Type::kInt32:
6259 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006260 locations->SetInAt(0, Location::RequiresRegister());
6261 locations->SetInAt(1, Location::RequiresRegister());
6262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6263 break;
6264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 case DataType::Type::kFloat32:
6266 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006267 locations->SetInAt(0, Location::RequiresFpuRegister());
6268 locations->SetInAt(1, Location::RequiresFpuRegister());
6269 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6270 break;
6271
6272 default:
6273 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6274 }
6275}
6276
6277void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006279 LocationSummary* locations = instruction->GetLocations();
6280
6281 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 case DataType::Type::kInt32:
6283 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006284 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6285 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6286 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006287 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006288 __ MulR6(dst, lhs, rhs);
6289 else
6290 __ Dmul(dst, lhs, rhs);
6291 break;
6292 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006293 case DataType::Type::kFloat32:
6294 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006295 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6296 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6297 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006298 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006299 __ MulS(dst, lhs, rhs);
6300 else
6301 __ MulD(dst, lhs, rhs);
6302 break;
6303 }
6304 default:
6305 LOG(FATAL) << "Unexpected mul type " << type;
6306 }
6307}
6308
6309void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6310 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006311 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006312 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006313 case DataType::Type::kInt32:
6314 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006315 locations->SetInAt(0, Location::RequiresRegister());
6316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6317 break;
6318
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006319 case DataType::Type::kFloat32:
6320 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006321 locations->SetInAt(0, Location::RequiresFpuRegister());
6322 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6323 break;
6324
6325 default:
6326 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6327 }
6328}
6329
6330void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006332 LocationSummary* locations = instruction->GetLocations();
6333
6334 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006335 case DataType::Type::kInt32:
6336 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006337 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6338 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006340 __ Subu(dst, ZERO, src);
6341 else
6342 __ Dsubu(dst, ZERO, src);
6343 break;
6344 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 case DataType::Type::kFloat32:
6346 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006347 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6348 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006349 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006350 __ NegS(dst, src);
6351 else
6352 __ NegD(dst, src);
6353 break;
6354 }
6355 default:
6356 LOG(FATAL) << "Unexpected neg type " << type;
6357 }
6358}
6359
6360void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006361 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6362 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006363 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006364 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006365 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6366 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006367}
6368
6369void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006370 // Note: if heap poisoning is enabled, the entry point takes care
6371 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006372 QuickEntrypointEnum entrypoint =
6373 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6374 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006375 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006376 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006377}
6378
6379void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006380 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6381 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006382 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006383 if (instruction->IsStringAlloc()) {
6384 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6385 } else {
6386 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006387 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006388 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006389}
6390
6391void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006392 // Note: if heap poisoning is enabled, the entry point takes care
6393 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006394 if (instruction->IsStringAlloc()) {
6395 // String is allocated through StringFactory. Call NewEmptyString entry point.
6396 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006397 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006398 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006399 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6400 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6401 __ Jalr(T9);
6402 __ Nop();
6403 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6404 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006405 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006406 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006407 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006408}
6409
6410void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006411 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006412 locations->SetInAt(0, Location::RequiresRegister());
6413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6414}
6415
6416void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006417 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006418 LocationSummary* locations = instruction->GetLocations();
6419
6420 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006421 case DataType::Type::kInt32:
6422 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006423 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6424 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6425 __ Nor(dst, src, ZERO);
6426 break;
6427 }
6428
6429 default:
6430 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6431 }
6432}
6433
6434void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006435 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006436 locations->SetInAt(0, Location::RequiresRegister());
6437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6438}
6439
6440void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6441 LocationSummary* locations = instruction->GetLocations();
6442 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6443 locations->InAt(0).AsRegister<GpuRegister>(),
6444 1);
6445}
6446
6447void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006448 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6449 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006450}
6451
Calin Juravle2ae48182016-03-16 14:05:09 +00006452void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6453 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006454 return;
6455 }
6456 Location obj = instruction->GetLocations()->InAt(0);
6457
6458 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006459 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006460}
6461
Calin Juravle2ae48182016-03-16 14:05:09 +00006462void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006463 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006464 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006465 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006466
6467 Location obj = instruction->GetLocations()->InAt(0);
6468
6469 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6470}
6471
6472void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006473 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006474}
6475
6476void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6477 HandleBinaryOp(instruction);
6478}
6479
6480void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6481 HandleBinaryOp(instruction);
6482}
6483
6484void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6485 LOG(FATAL) << "Unreachable";
6486}
6487
6488void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006489 if (instruction->GetNext()->IsSuspendCheck() &&
6490 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6491 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6492 // The back edge will generate the suspend check.
6493 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6494 }
6495
Alexey Frunze4dda3372015-06-01 18:31:49 -07006496 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6497}
6498
6499void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006500 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006501 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6502 if (location.IsStackSlot()) {
6503 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6504 } else if (location.IsDoubleStackSlot()) {
6505 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6506 }
6507 locations->SetOut(location);
6508}
6509
6510void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6511 ATTRIBUTE_UNUSED) {
6512 // Nothing to do, the parameter is already at its location.
6513}
6514
6515void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6516 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006517 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006518 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6519}
6520
6521void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6522 ATTRIBUTE_UNUSED) {
6523 // Nothing to do, the method is already at its location.
6524}
6525
6526void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006527 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006528 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006529 locations->SetInAt(i, Location::Any());
6530 }
6531 locations->SetOut(Location::Any());
6532}
6533
6534void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6535 LOG(FATAL) << "Unreachable";
6536}
6537
6538void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006539 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006540 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006541 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6542 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006543 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006544
6545 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006546 case DataType::Type::kInt32:
6547 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006548 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006549 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006550 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6551 break;
6552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006553 case DataType::Type::kFloat32:
6554 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006555 InvokeRuntimeCallingConvention calling_convention;
6556 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6557 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6558 locations->SetOut(calling_convention.GetReturnLocation(type));
6559 break;
6560 }
6561
6562 default:
6563 LOG(FATAL) << "Unexpected rem type " << type;
6564 }
6565}
6566
6567void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006568 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006569
6570 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 case DataType::Type::kInt32:
6572 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006573 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006574 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006576 case DataType::Type::kFloat32:
6577 case DataType::Type::kFloat64: {
6578 QuickEntrypointEnum entrypoint =
6579 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006580 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006581 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006582 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6583 } else {
6584 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6585 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006586 break;
6587 }
6588 default:
6589 LOG(FATAL) << "Unexpected rem type " << type;
6590 }
6591}
6592
Igor Murashkind01745e2017-04-05 16:40:31 -07006593void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6594 constructor_fence->SetLocations(nullptr);
6595}
6596
6597void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6598 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6599 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6600}
6601
Alexey Frunze4dda3372015-06-01 18:31:49 -07006602void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6603 memory_barrier->SetLocations(nullptr);
6604}
6605
6606void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6607 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6608}
6609
6610void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006611 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006612 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006613 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6614}
6615
6616void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6617 codegen_->GenerateFrameExit();
6618}
6619
6620void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6621 ret->SetLocations(nullptr);
6622}
6623
6624void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6625 codegen_->GenerateFrameExit();
6626}
6627
Alexey Frunze92d90602015-12-18 18:16:36 -08006628void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6629 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006630}
6631
Alexey Frunze92d90602015-12-18 18:16:36 -08006632void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6633 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006634}
6635
Alexey Frunze4dda3372015-06-01 18:31:49 -07006636void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6637 HandleShift(shl);
6638}
6639
6640void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6641 HandleShift(shl);
6642}
6643
6644void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6645 HandleShift(shr);
6646}
6647
6648void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6649 HandleShift(shr);
6650}
6651
Alexey Frunze4dda3372015-06-01 18:31:49 -07006652void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6653 HandleBinaryOp(instruction);
6654}
6655
6656void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6657 HandleBinaryOp(instruction);
6658}
6659
6660void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6661 HandleFieldGet(instruction, instruction->GetFieldInfo());
6662}
6663
6664void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6665 HandleFieldGet(instruction, instruction->GetFieldInfo());
6666}
6667
6668void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6669 HandleFieldSet(instruction, instruction->GetFieldInfo());
6670}
6671
6672void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006673 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006674}
6675
Calin Juravlee460d1d2015-09-29 04:52:17 +01006676void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6677 HUnresolvedInstanceFieldGet* instruction) {
6678 FieldAccessCallingConventionMIPS64 calling_convention;
6679 codegen_->CreateUnresolvedFieldLocationSummary(
6680 instruction, instruction->GetFieldType(), calling_convention);
6681}
6682
6683void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6684 HUnresolvedInstanceFieldGet* instruction) {
6685 FieldAccessCallingConventionMIPS64 calling_convention;
6686 codegen_->GenerateUnresolvedFieldAccess(instruction,
6687 instruction->GetFieldType(),
6688 instruction->GetFieldIndex(),
6689 instruction->GetDexPc(),
6690 calling_convention);
6691}
6692
6693void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6694 HUnresolvedInstanceFieldSet* instruction) {
6695 FieldAccessCallingConventionMIPS64 calling_convention;
6696 codegen_->CreateUnresolvedFieldLocationSummary(
6697 instruction, instruction->GetFieldType(), calling_convention);
6698}
6699
6700void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6701 HUnresolvedInstanceFieldSet* instruction) {
6702 FieldAccessCallingConventionMIPS64 calling_convention;
6703 codegen_->GenerateUnresolvedFieldAccess(instruction,
6704 instruction->GetFieldType(),
6705 instruction->GetFieldIndex(),
6706 instruction->GetDexPc(),
6707 calling_convention);
6708}
6709
6710void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6711 HUnresolvedStaticFieldGet* instruction) {
6712 FieldAccessCallingConventionMIPS64 calling_convention;
6713 codegen_->CreateUnresolvedFieldLocationSummary(
6714 instruction, instruction->GetFieldType(), calling_convention);
6715}
6716
6717void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6718 HUnresolvedStaticFieldGet* instruction) {
6719 FieldAccessCallingConventionMIPS64 calling_convention;
6720 codegen_->GenerateUnresolvedFieldAccess(instruction,
6721 instruction->GetFieldType(),
6722 instruction->GetFieldIndex(),
6723 instruction->GetDexPc(),
6724 calling_convention);
6725}
6726
6727void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6728 HUnresolvedStaticFieldSet* instruction) {
6729 FieldAccessCallingConventionMIPS64 calling_convention;
6730 codegen_->CreateUnresolvedFieldLocationSummary(
6731 instruction, instruction->GetFieldType(), calling_convention);
6732}
6733
6734void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6735 HUnresolvedStaticFieldSet* instruction) {
6736 FieldAccessCallingConventionMIPS64 calling_convention;
6737 codegen_->GenerateUnresolvedFieldAccess(instruction,
6738 instruction->GetFieldType(),
6739 instruction->GetFieldIndex(),
6740 instruction->GetDexPc(),
6741 calling_convention);
6742}
6743
Alexey Frunze4dda3372015-06-01 18:31:49 -07006744void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006745 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6746 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006747 // In suspend check slow path, usually there are no caller-save registers at all.
6748 // If SIMD instructions are present, however, we force spilling all live SIMD
6749 // registers in full width (since the runtime only saves/restores lower part).
6750 locations->SetCustomSlowPathCallerSaves(
6751 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006752}
6753
6754void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6755 HBasicBlock* block = instruction->GetBlock();
6756 if (block->GetLoopInformation() != nullptr) {
6757 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6758 // The back edge will generate the suspend check.
6759 return;
6760 }
6761 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6762 // The goto will generate the suspend check.
6763 return;
6764 }
6765 GenerateSuspendCheck(instruction, nullptr);
6766}
6767
Alexey Frunze4dda3372015-06-01 18:31:49 -07006768void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006769 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6770 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006771 InvokeRuntimeCallingConvention calling_convention;
6772 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6773}
6774
6775void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006776 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006777 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6778}
6779
6780void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 DataType::Type input_type = conversion->GetInputType();
6782 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006783 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6784 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006786 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6787 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006788 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6789 }
6790
Vladimir Markoca6fff82017-10-03 14:49:14 +01006791 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006793 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006794 locations->SetInAt(0, Location::RequiresFpuRegister());
6795 } else {
6796 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006797 }
6798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006799 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006800 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006801 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006803 }
6804}
6805
6806void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6807 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006808 DataType::Type result_type = conversion->GetResultType();
6809 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006810
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006811 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6812 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006814 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006815 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6816 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6817
6818 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006819 case DataType::Type::kUint8:
6820 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006821 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006822 case DataType::Type::kInt8:
6823 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006824 // Type conversion from long to types narrower than int is a result of code
6825 // transformations. To avoid unpredictable results for SEB and SEH, we first
6826 // need to sign-extend the low 32-bit value into bits 32 through 63.
6827 __ Sll(dst, src, 0);
6828 __ Seb(dst, dst);
6829 } else {
6830 __ Seb(dst, src);
6831 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006832 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006833 case DataType::Type::kUint16:
6834 __ Andi(dst, src, 0xFFFF);
6835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006836 case DataType::Type::kInt16:
6837 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006838 // Type conversion from long to types narrower than int is a result of code
6839 // transformations. To avoid unpredictable results for SEB and SEH, we first
6840 // need to sign-extend the low 32-bit value into bits 32 through 63.
6841 __ Sll(dst, src, 0);
6842 __ Seh(dst, dst);
6843 } else {
6844 __ Seh(dst, src);
6845 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006846 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006847 case DataType::Type::kInt32:
6848 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006849 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6850 // conversions, except when the input and output registers are the same and we are not
6851 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006852 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006853 __ Sll(dst, src, 0);
6854 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006855 break;
6856
6857 default:
6858 LOG(FATAL) << "Unexpected type conversion from " << input_type
6859 << " to " << result_type;
6860 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006861 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006862 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6863 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006864 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006865 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006867 __ Cvtsl(dst, FTMP);
6868 } else {
6869 __ Cvtdl(dst, FTMP);
6870 }
6871 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006872 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006873 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006874 __ Cvtsw(dst, FTMP);
6875 } else {
6876 __ Cvtdw(dst, FTMP);
6877 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006878 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006879 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6880 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006881 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6882 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006884 if (result_type == DataType::Type::kInt64) {
6885 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006886 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006887 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006888 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006889 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006890 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006891 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006892 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006893 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006894 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006895 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006896 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006897 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006898 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006899 } else if (DataType::IsFloatingPointType(result_type) &&
6900 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006901 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6902 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006903 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006904 __ Cvtsd(dst, src);
6905 } else {
6906 __ Cvtds(dst, src);
6907 }
6908 } else {
6909 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6910 << " to " << result_type;
6911 }
6912}
6913
6914void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6915 HandleShift(ushr);
6916}
6917
6918void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6919 HandleShift(ushr);
6920}
6921
6922void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6923 HandleBinaryOp(instruction);
6924}
6925
6926void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6927 HandleBinaryOp(instruction);
6928}
6929
6930void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6931 // Nothing to do, this should be removed during prepare for register allocator.
6932 LOG(FATAL) << "Unreachable";
6933}
6934
6935void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6936 // Nothing to do, this should be removed during prepare for register allocator.
6937 LOG(FATAL) << "Unreachable";
6938}
6939
6940void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006941 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006942}
6943
6944void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006945 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006946}
6947
6948void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006949 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006950}
6951
6952void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006953 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006954}
6955
6956void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006957 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006958}
6959
6960void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006961 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006962}
6963
6964void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006965 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006966}
6967
6968void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006969 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006970}
6971
6972void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006973 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006974}
6975
6976void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006977 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006978}
6979
6980void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006981 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006982}
6983
6984void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006985 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006986}
6987
Aart Bike9f37602015-10-09 11:15:55 -07006988void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006989 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006990}
6991
6992void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006993 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006994}
6995
6996void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006997 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006998}
6999
7000void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007001 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007002}
7003
7004void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007005 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007006}
7007
7008void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007009 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007010}
7011
7012void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007013 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007014}
7015
7016void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007017 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007018}
7019
Mark Mendellfe57faa2015-09-18 09:26:15 -04007020// Simple implementation of packed switch - generate cascaded compare/jumps.
7021void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7022 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007023 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007024 locations->SetInAt(0, Location::RequiresRegister());
7025}
7026
Alexey Frunze0960ac52016-12-20 17:24:59 -08007027void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7028 int32_t lower_bound,
7029 uint32_t num_entries,
7030 HBasicBlock* switch_block,
7031 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007032 // Create a set of compare/jumps.
7033 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007034 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007035 // Jump to default if index is negative
7036 // Note: We don't check the case that index is positive while value < lower_bound, because in
7037 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7038 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7039
Alexey Frunze0960ac52016-12-20 17:24:59 -08007040 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007041 // Jump to successors[0] if value == lower_bound.
7042 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7043 int32_t last_index = 0;
7044 for (; num_entries - last_index > 2; last_index += 2) {
7045 __ Addiu(temp_reg, temp_reg, -2);
7046 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7047 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7048 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7049 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7050 }
7051 if (num_entries - last_index == 2) {
7052 // The last missing case_value.
7053 __ Addiu(temp_reg, temp_reg, -1);
7054 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007055 }
7056
7057 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007058 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007059 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007060 }
7061}
7062
Alexey Frunze0960ac52016-12-20 17:24:59 -08007063void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7064 int32_t lower_bound,
7065 uint32_t num_entries,
7066 HBasicBlock* switch_block,
7067 HBasicBlock* default_block) {
7068 // Create a jump table.
7069 std::vector<Mips64Label*> labels(num_entries);
7070 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7071 for (uint32_t i = 0; i < num_entries; i++) {
7072 labels[i] = codegen_->GetLabelOf(successors[i]);
7073 }
7074 JumpTable* table = __ CreateJumpTable(std::move(labels));
7075
7076 // Is the value in range?
7077 __ Addiu32(TMP, value_reg, -lower_bound);
7078 __ LoadConst32(AT, num_entries);
7079 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7080
7081 // We are in the range of the table.
7082 // Load the target address from the jump table, indexing by the value.
7083 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007084 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007085 __ Lw(TMP, TMP, 0);
7086 // Compute the absolute target address by adding the table start address
7087 // (the table contains offsets to targets relative to its start).
7088 __ Daddu(TMP, TMP, AT);
7089 // And jump.
7090 __ Jr(TMP);
7091 __ Nop();
7092}
7093
7094void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7095 int32_t lower_bound = switch_instr->GetStartValue();
7096 uint32_t num_entries = switch_instr->GetNumEntries();
7097 LocationSummary* locations = switch_instr->GetLocations();
7098 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7099 HBasicBlock* switch_block = switch_instr->GetBlock();
7100 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7101
7102 if (num_entries > kPackedSwitchJumpTableThreshold) {
7103 GenTableBasedPackedSwitch(value_reg,
7104 lower_bound,
7105 num_entries,
7106 switch_block,
7107 default_block);
7108 } else {
7109 GenPackedSwitchWithCompares(value_reg,
7110 lower_bound,
7111 num_entries,
7112 switch_block,
7113 default_block);
7114 }
7115}
7116
Chris Larsenc9905a62017-03-13 17:06:18 -07007117void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7118 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007119 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007120 locations->SetInAt(0, Location::RequiresRegister());
7121 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007122}
7123
Chris Larsenc9905a62017-03-13 17:06:18 -07007124void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7125 LocationSummary* locations = instruction->GetLocations();
7126 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7127 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7128 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7129 __ LoadFromOffset(kLoadDoubleword,
7130 locations->Out().AsRegister<GpuRegister>(),
7131 locations->InAt(0).AsRegister<GpuRegister>(),
7132 method_offset);
7133 } else {
7134 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7135 instruction->GetIndex(), kMips64PointerSize));
7136 __ LoadFromOffset(kLoadDoubleword,
7137 locations->Out().AsRegister<GpuRegister>(),
7138 locations->InAt(0).AsRegister<GpuRegister>(),
7139 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7140 __ LoadFromOffset(kLoadDoubleword,
7141 locations->Out().AsRegister<GpuRegister>(),
7142 locations->Out().AsRegister<GpuRegister>(),
7143 method_offset);
7144 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007145}
7146
Alexey Frunze4dda3372015-06-01 18:31:49 -07007147} // namespace mips64
7148} // namespace art