blob: e8ae2db019e551d73cdf651d296083cefa151c72 [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"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "mirror/array-inl.h"
31#include "mirror/class-inl.h"
32#include "offsets.h"
33#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070034#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070035#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "utils/stack_checks.h"
37
38namespace art {
39namespace mips64 {
40
41static constexpr int kCurrentMethodStackOffset = 0;
42static constexpr GpuRegister kMethodRegisterArgument = A0;
43
Alexey Frunze4147fcc2017-06-17 19:57:27 -070044// Flags controlling the use of thunks for Baker read barriers.
45constexpr bool kBakerReadBarrierThunksEnableForFields = true;
46constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
47constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
48
Alexey Frunze4dda3372015-06-01 18:31:49 -070049Location Mips64ReturnLocation(Primitive::Type return_type) {
50 switch (return_type) {
51 case Primitive::kPrimBoolean:
52 case Primitive::kPrimByte:
53 case Primitive::kPrimChar:
54 case Primitive::kPrimShort:
55 case Primitive::kPrimInt:
56 case Primitive::kPrimNot:
57 case Primitive::kPrimLong:
58 return Location::RegisterLocation(V0);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
71 return Mips64ReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80 if (type == Primitive::kPrimVoid) {
81 LOG(FATAL) << "Unexpected parameter type " << type;
82 }
83
84 if (Primitive::IsFloatingPointType(type) &&
85 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
86 next_location = Location::FpuRegisterLocation(
87 calling_convention.GetFpuRegisterAt(float_index_++));
88 gp_index_++;
89 } else if (!Primitive::IsFloatingPointType(type) &&
90 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
91 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
92 float_index_++;
93 } else {
94 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
95 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
96 : Location::StackSlot(stack_offset);
97 }
98
99 // Space on the stack is reserved for all arguments.
100 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
101
Alexey Frunze4dda3372015-06-01 18:31:49 -0700102 return next_location;
103}
104
105Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
106 return Mips64ReturnLocation(type);
107}
108
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100109// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
110#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700111#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700112
113class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
114 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000115 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700116
117 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100118 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700119 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
120 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000121 if (instruction_->CanThrowIntoCatchBlock()) {
122 // Live registers will be restored in the catch block if caught.
123 SaveLiveRegisters(codegen, instruction_->GetLocations());
124 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700125 // We're moving two locations to locations that could overlap, so we need a parallel
126 // move resolver.
127 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100128 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700129 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
130 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
133 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100134 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
135 ? kQuickThrowStringBounds
136 : kQuickThrowArrayBounds;
137 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100138 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700139 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
140 }
141
Alexandre Rames8158f282015-08-07 10:26:17 +0100142 bool IsFatal() const OVERRIDE { return true; }
143
Roland Levillain46648892015-06-19 16:07:18 +0100144 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
145
Alexey Frunze4dda3372015-06-01 18:31:49 -0700146 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700147 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
148};
149
150class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
151 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700152 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
153 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700154
155 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
156 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
157 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100158 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700159 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
160 }
161
Alexandre Rames8158f282015-08-07 10:26:17 +0100162 bool IsFatal() const OVERRIDE { return true; }
163
Roland Levillain46648892015-06-19 16:07:18 +0100164 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
165
Alexey Frunze4dda3372015-06-01 18:31:49 -0700166 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700167 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
168};
169
170class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
171 public:
172 LoadClassSlowPathMIPS64(HLoadClass* cls,
173 HInstruction* at,
174 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700175 bool do_clinit,
176 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr)
177 : SlowPathCodeMIPS64(at),
178 cls_(cls),
179 dex_pc_(dex_pc),
180 do_clinit_(do_clinit),
181 bss_info_high_(bss_info_high) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700182 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
183 }
184
185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000186 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700187 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700188 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700189 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
190 InvokeRuntimeCallingConvention calling_convention;
191 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
192 const bool is_load_class_bss_entry =
193 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700194 __ Bind(GetEntryLabel());
195 SaveLiveRegisters(codegen, locations);
196
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700197 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
198 GpuRegister entry_address = kNoGpuRegister;
199 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
200 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
201 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
202 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
203 // kSaveEverything call.
204 entry_address = temp_is_a0 ? out.AsRegister<GpuRegister>() : temp;
205 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
206 if (temp_is_a0) {
207 __ Move(entry_address, temp);
208 }
209 }
210
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000211 dex::TypeIndex type_index = cls_->GetTypeIndex();
212 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100213 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
214 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000215 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700216 if (do_clinit_) {
217 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
218 } else {
219 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
220 }
221
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700222 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
223 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
224 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
225 DCHECK(bss_info_high_);
226 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
227 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
228 __ Bind(&info_low->label);
229 __ StoreToOffset(kStoreWord,
230 calling_convention.GetRegisterAt(0),
231 entry_address,
232 /* placeholder */ 0x5678);
233 }
234
Alexey Frunze4dda3372015-06-01 18:31:49 -0700235 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700236 if (out.IsValid()) {
237 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000238 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700239 mips64_codegen->MoveLocation(out,
240 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
241 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700242 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700243 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700244
245 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
246 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
247 // For non-Baker read barriers we need to re-calculate the address of
248 // the class entry.
249 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000250 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700251 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
252 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
253 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
254 __ StoreToOffset(kStoreWord, out.AsRegister<GpuRegister>(), TMP, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000255 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700256 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700257 }
258
Roland Levillain46648892015-06-19 16:07:18 +0100259 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
260
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 private:
262 // The class this slow path will load.
263 HLoadClass* const cls_;
264
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 // The dex PC of `at_`.
266 const uint32_t dex_pc_;
267
268 // Whether to initialize the class.
269 const bool do_clinit_;
270
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700271 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
272 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
273
Alexey Frunze4dda3372015-06-01 18:31:49 -0700274 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
275};
276
277class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
278 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700279 explicit LoadStringSlowPathMIPS64(HLoadString* instruction,
280 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high)
281 : SlowPathCodeMIPS64(instruction), bss_info_high_(bss_info_high) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700284 DCHECK(instruction_->IsLoadString());
285 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700286 LocationSummary* locations = instruction_->GetLocations();
287 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700288 HLoadString* load = instruction_->AsLoadString();
289 const dex::StringIndex string_index = load->GetStringIndex();
290 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700291 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700292 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
293 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 __ Bind(GetEntryLabel());
295 SaveLiveRegisters(codegen, locations);
296
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700297 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
298 GpuRegister entry_address = kNoGpuRegister;
299 if (baker_or_no_read_barriers) {
300 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
301 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
302 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
303 // kSaveEverything call.
304 entry_address = temp_is_a0 ? out : temp;
305 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
306 if (temp_is_a0) {
307 __ Move(entry_address, temp);
308 }
309 }
310
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000311 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100312 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 instruction_,
314 instruction_->GetDexPc(),
315 this);
316 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700317
318 // Store the resolved string to the BSS entry.
319 if (baker_or_no_read_barriers) {
320 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
321 DCHECK(bss_info_high_);
322 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100323 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(),
324 string_index,
325 bss_info_high_);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700326 __ Bind(&info_low->label);
327 __ StoreToOffset(kStoreWord,
328 calling_convention.GetRegisterAt(0),
329 entry_address,
330 /* placeholder */ 0x5678);
331 }
332
Alexey Frunze4dda3372015-06-01 18:31:49 -0700333 Primitive::Type type = instruction_->GetType();
334 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700335 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700336 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700337 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800338
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700339 // Store the resolved string to the BSS entry.
340 if (!baker_or_no_read_barriers) {
341 // For non-Baker read barriers we need to re-calculate the address of
342 // the string entry.
343 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100344 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700345 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100346 mips64_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700347 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, info_low);
348 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
349 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700350 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 }
352
Roland Levillain46648892015-06-19 16:07:18 +0100353 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
354
Alexey Frunze4dda3372015-06-01 18:31:49 -0700355 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700356 // Pointer to the high half PC-relative patch info.
357 const CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high_;
358
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
360};
361
362class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
363 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000364 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700365
366 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
367 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
368 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000369 if (instruction_->CanThrowIntoCatchBlock()) {
370 // Live registers will be restored in the catch block if caught.
371 SaveLiveRegisters(codegen, instruction_->GetLocations());
372 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100373 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 instruction_,
375 instruction_->GetDexPc(),
376 this);
377 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
378 }
379
Alexandre Rames8158f282015-08-07 10:26:17 +0100380 bool IsFatal() const OVERRIDE { return true; }
381
Roland Levillain46648892015-06-19 16:07:18 +0100382 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
383
Alexey Frunze4dda3372015-06-01 18:31:49 -0700384 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
386};
387
388class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
389 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100390 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000391 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200394 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
396 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200397 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100398 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200400 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700402 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700404 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700405 }
406 }
407
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700408 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 DCHECK(successor_ == nullptr);
410 return &return_label_;
411 }
412
Roland Levillain46648892015-06-19 16:07:18 +0100413 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
414
Alexey Frunze4dda3372015-06-01 18:31:49 -0700415 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700416 // If not null, the block to branch to after the suspend check.
417 HBasicBlock* const successor_;
418
419 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700420 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700421
422 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
423};
424
425class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
426 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800427 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
428 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700429
430 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
431 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800432
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100433 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700434 DCHECK(instruction_->IsCheckCast()
435 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
436 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
437
438 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800439 if (!is_fatal_) {
440 SaveLiveRegisters(codegen, locations);
441 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700442
443 // We're moving two locations to locations that could overlap, so we need a parallel
444 // move resolver.
445 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800446 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700447 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
448 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800449 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700450 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
451 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700452 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100453 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800454 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700455 Primitive::Type ret_type = instruction_->GetType();
456 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
457 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700458 } else {
459 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800460 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
461 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700462 }
463
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800464 if (!is_fatal_) {
465 RestoreLiveRegisters(codegen, locations);
466 __ Bc(GetExitLabel());
467 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700468 }
469
Roland Levillain46648892015-06-19 16:07:18 +0100470 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
471
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800472 bool IsFatal() const OVERRIDE { return is_fatal_; }
473
Alexey Frunze4dda3372015-06-01 18:31:49 -0700474 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800475 const bool is_fatal_;
476
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
478};
479
480class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
481 public:
Aart Bik42249c32016-01-07 15:33:50 -0800482 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000483 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700484
485 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800486 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700487 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100488 LocationSummary* locations = instruction_->GetLocations();
489 SaveLiveRegisters(codegen, locations);
490 InvokeRuntimeCallingConvention calling_convention;
491 __ LoadConst32(calling_convention.GetRegisterAt(0),
492 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100493 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100494 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700495 }
496
Roland Levillain46648892015-06-19 16:07:18 +0100497 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
498
Alexey Frunze4dda3372015-06-01 18:31:49 -0700499 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700500 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
501};
502
Alexey Frunze15958152017-02-09 19:08:30 -0800503class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
504 public:
505 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
506
507 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
508 LocationSummary* locations = instruction_->GetLocations();
509 __ Bind(GetEntryLabel());
510 SaveLiveRegisters(codegen, locations);
511
512 InvokeRuntimeCallingConvention calling_convention;
513 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
514 parallel_move.AddMove(
515 locations->InAt(0),
516 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
517 Primitive::kPrimNot,
518 nullptr);
519 parallel_move.AddMove(
520 locations->InAt(1),
521 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
522 Primitive::kPrimInt,
523 nullptr);
524 parallel_move.AddMove(
525 locations->InAt(2),
526 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
527 Primitive::kPrimNot,
528 nullptr);
529 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
530
531 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
532 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
533 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
534 RestoreLiveRegisters(codegen, locations);
535 __ Bc(GetExitLabel());
536 }
537
538 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
539
540 private:
541 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
542};
543
544// Slow path marking an object reference `ref` during a read
545// barrier. The field `obj.field` in the object `obj` holding this
546// reference does not get updated by this slow path after marking (see
547// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
548//
549// This means that after the execution of this slow path, `ref` will
550// always be up-to-date, but `obj.field` may not; i.e., after the
551// flip, `ref` will be a to-space reference, but `obj.field` will
552// probably still be a from-space reference (unless it gets updated by
553// another thread, or if another thread installed another object
554// reference (different from `ref`) in `obj.field`).
555//
556// If `entrypoint` is a valid location it is assumed to already be
557// holding the entrypoint. The case where the entrypoint is passed in
558// is for the GcRoot read barrier.
559class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
560 public:
561 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
562 Location ref,
563 Location entrypoint = Location::NoLocation())
564 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
565 DCHECK(kEmitCompilerReadBarrier);
566 }
567
568 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
569
570 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
571 LocationSummary* locations = instruction_->GetLocations();
572 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
573 DCHECK(locations->CanCall());
574 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
575 DCHECK(instruction_->IsInstanceFieldGet() ||
576 instruction_->IsStaticFieldGet() ||
577 instruction_->IsArrayGet() ||
578 instruction_->IsArraySet() ||
579 instruction_->IsLoadClass() ||
580 instruction_->IsLoadString() ||
581 instruction_->IsInstanceOf() ||
582 instruction_->IsCheckCast() ||
583 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
584 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
585 << "Unexpected instruction in read barrier marking slow path: "
586 << instruction_->DebugName();
587
588 __ Bind(GetEntryLabel());
589 // No need to save live registers; it's taken care of by the
590 // entrypoint. Also, there is no need to update the stack mask,
591 // as this runtime call will not trigger a garbage collection.
592 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
593 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
594 (S2 <= ref_reg && ref_reg <= S7) ||
595 (ref_reg == S8)) << ref_reg;
596 // "Compact" slow path, saving two moves.
597 //
598 // Instead of using the standard runtime calling convention (input
599 // and output in A0 and V0 respectively):
600 //
601 // A0 <- ref
602 // V0 <- ReadBarrierMark(A0)
603 // ref <- V0
604 //
605 // we just use rX (the register containing `ref`) as input and output
606 // of a dedicated entrypoint:
607 //
608 // rX <- ReadBarrierMarkRegX(rX)
609 //
610 if (entrypoint_.IsValid()) {
611 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
612 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
613 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
614 __ Nop();
615 } else {
616 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100617 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800618 // This runtime call does not require a stack map.
619 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
620 instruction_,
621 this);
622 }
623 __ Bc(GetExitLabel());
624 }
625
626 private:
627 // The location (register) of the marked object reference.
628 const Location ref_;
629
630 // The location of the entrypoint if already loaded.
631 const Location entrypoint_;
632
633 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
634};
635
636// Slow path marking an object reference `ref` during a read barrier,
637// and if needed, atomically updating the field `obj.field` in the
638// object `obj` holding this reference after marking (contrary to
639// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
640// `obj.field`).
641//
642// This means that after the execution of this slow path, both `ref`
643// and `obj.field` will be up-to-date; i.e., after the flip, both will
644// hold the same to-space reference (unless another thread installed
645// another object reference (different from `ref`) in `obj.field`).
646class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
647 public:
648 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
649 Location ref,
650 GpuRegister obj,
651 Location field_offset,
652 GpuRegister temp1)
653 : SlowPathCodeMIPS64(instruction),
654 ref_(ref),
655 obj_(obj),
656 field_offset_(field_offset),
657 temp1_(temp1) {
658 DCHECK(kEmitCompilerReadBarrier);
659 }
660
661 const char* GetDescription() const OVERRIDE {
662 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
663 }
664
665 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
666 LocationSummary* locations = instruction_->GetLocations();
667 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
668 DCHECK(locations->CanCall());
669 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
670 // This slow path is only used by the UnsafeCASObject intrinsic.
671 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
672 << "Unexpected instruction in read barrier marking and field updating slow path: "
673 << instruction_->DebugName();
674 DCHECK(instruction_->GetLocations()->Intrinsified());
675 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
676 DCHECK(field_offset_.IsRegister()) << field_offset_;
677
678 __ Bind(GetEntryLabel());
679
680 // Save the old reference.
681 // Note that we cannot use AT or TMP to save the old reference, as those
682 // are used by the code that follows, but we need the old reference after
683 // the call to the ReadBarrierMarkRegX entry point.
684 DCHECK_NE(temp1_, AT);
685 DCHECK_NE(temp1_, TMP);
686 __ Move(temp1_, ref_reg);
687
688 // No need to save live registers; it's taken care of by the
689 // entrypoint. Also, there is no need to update the stack mask,
690 // as this runtime call will not trigger a garbage collection.
691 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
692 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
693 (S2 <= ref_reg && ref_reg <= S7) ||
694 (ref_reg == S8)) << ref_reg;
695 // "Compact" slow path, saving two moves.
696 //
697 // Instead of using the standard runtime calling convention (input
698 // and output in A0 and V0 respectively):
699 //
700 // A0 <- ref
701 // V0 <- ReadBarrierMark(A0)
702 // ref <- V0
703 //
704 // we just use rX (the register containing `ref`) as input and output
705 // of a dedicated entrypoint:
706 //
707 // rX <- ReadBarrierMarkRegX(rX)
708 //
709 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100710 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800711 // This runtime call does not require a stack map.
712 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
713 instruction_,
714 this);
715
716 // If the new reference is different from the old reference,
717 // update the field in the holder (`*(obj_ + field_offset_)`).
718 //
719 // Note that this field could also hold a different object, if
720 // another thread had concurrently changed it. In that case, the
721 // the compare-and-set (CAS) loop below would abort, leaving the
722 // field as-is.
723 Mips64Label done;
724 __ Beqc(temp1_, ref_reg, &done);
725
726 // Update the the holder's field atomically. This may fail if
727 // mutator updates before us, but it's OK. This is achieved
728 // using a strong compare-and-set (CAS) operation with relaxed
729 // memory synchronization ordering, where the expected value is
730 // the old reference and the desired value is the new reference.
731
732 // Convenience aliases.
733 GpuRegister base = obj_;
734 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
735 GpuRegister expected = temp1_;
736 GpuRegister value = ref_reg;
737 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
738 GpuRegister tmp = AT; // Value in memory.
739
740 __ Daddu(tmp_ptr, base, offset);
741
742 if (kPoisonHeapReferences) {
743 __ PoisonHeapReference(expected);
744 // Do not poison `value` if it is the same register as
745 // `expected`, which has just been poisoned.
746 if (value != expected) {
747 __ PoisonHeapReference(value);
748 }
749 }
750
751 // do {
752 // tmp = [r_ptr] - expected;
753 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
754
755 Mips64Label loop_head, exit_loop;
756 __ Bind(&loop_head);
757 __ Ll(tmp, tmp_ptr);
758 // The LL instruction sign-extends the 32-bit value, but
759 // 32-bit references must be zero-extended. Zero-extend `tmp`.
760 __ Dext(tmp, tmp, 0, 32);
761 __ Bnec(tmp, expected, &exit_loop);
762 __ Move(tmp, value);
763 __ Sc(tmp, tmp_ptr);
764 __ Beqzc(tmp, &loop_head);
765 __ Bind(&exit_loop);
766
767 if (kPoisonHeapReferences) {
768 __ UnpoisonHeapReference(expected);
769 // Do not unpoison `value` if it is the same register as
770 // `expected`, which has just been unpoisoned.
771 if (value != expected) {
772 __ UnpoisonHeapReference(value);
773 }
774 }
775
776 __ Bind(&done);
777 __ Bc(GetExitLabel());
778 }
779
780 private:
781 // The location (register) of the marked object reference.
782 const Location ref_;
783 // The register containing the object holding the marked object reference field.
784 const GpuRegister obj_;
785 // The location of the offset of the marked reference field within `obj_`.
786 Location field_offset_;
787
788 const GpuRegister temp1_;
789
790 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
791};
792
793// Slow path generating a read barrier for a heap reference.
794class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
795 public:
796 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
797 Location out,
798 Location ref,
799 Location obj,
800 uint32_t offset,
801 Location index)
802 : SlowPathCodeMIPS64(instruction),
803 out_(out),
804 ref_(ref),
805 obj_(obj),
806 offset_(offset),
807 index_(index) {
808 DCHECK(kEmitCompilerReadBarrier);
809 // If `obj` is equal to `out` or `ref`, it means the initial object
810 // has been overwritten by (or after) the heap object reference load
811 // to be instrumented, e.g.:
812 //
813 // __ LoadFromOffset(kLoadWord, out, out, offset);
814 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
815 //
816 // In that case, we have lost the information about the original
817 // object, and the emitted read barrier cannot work properly.
818 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
819 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
820 }
821
822 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
823 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
824 LocationSummary* locations = instruction_->GetLocations();
825 Primitive::Type type = Primitive::kPrimNot;
826 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
827 DCHECK(locations->CanCall());
828 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
829 DCHECK(instruction_->IsInstanceFieldGet() ||
830 instruction_->IsStaticFieldGet() ||
831 instruction_->IsArrayGet() ||
832 instruction_->IsInstanceOf() ||
833 instruction_->IsCheckCast() ||
834 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
835 << "Unexpected instruction in read barrier for heap reference slow path: "
836 << instruction_->DebugName();
837
838 __ Bind(GetEntryLabel());
839 SaveLiveRegisters(codegen, locations);
840
841 // We may have to change the index's value, but as `index_` is a
842 // constant member (like other "inputs" of this slow path),
843 // introduce a copy of it, `index`.
844 Location index = index_;
845 if (index_.IsValid()) {
846 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
847 if (instruction_->IsArrayGet()) {
848 // Compute the actual memory offset and store it in `index`.
849 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
850 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
851 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
852 // We are about to change the value of `index_reg` (see the
853 // calls to art::mips64::Mips64Assembler::Sll and
854 // art::mips64::MipsAssembler::Addiu32 below), but it has
855 // not been saved by the previous call to
856 // art::SlowPathCode::SaveLiveRegisters, as it is a
857 // callee-save register --
858 // art::SlowPathCode::SaveLiveRegisters does not consider
859 // callee-save registers, as it has been designed with the
860 // assumption that callee-save registers are supposed to be
861 // handled by the called function. So, as a callee-save
862 // register, `index_reg` _would_ eventually be saved onto
863 // the stack, but it would be too late: we would have
864 // changed its value earlier. Therefore, we manually save
865 // it here into another freely available register,
866 // `free_reg`, chosen of course among the caller-save
867 // registers (as a callee-save `free_reg` register would
868 // exhibit the same problem).
869 //
870 // Note we could have requested a temporary register from
871 // the register allocator instead; but we prefer not to, as
872 // this is a slow path, and we know we can find a
873 // caller-save register that is available.
874 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
875 __ Move(free_reg, index_reg);
876 index_reg = free_reg;
877 index = Location::RegisterLocation(index_reg);
878 } else {
879 // The initial register stored in `index_` has already been
880 // saved in the call to art::SlowPathCode::SaveLiveRegisters
881 // (as it is not a callee-save register), so we can freely
882 // use it.
883 }
884 // Shifting the index value contained in `index_reg` by the scale
885 // factor (2) cannot overflow in practice, as the runtime is
886 // unable to allocate object arrays with a size larger than
887 // 2^26 - 1 (that is, 2^28 - 4 bytes).
888 __ Sll(index_reg, index_reg, TIMES_4);
889 static_assert(
890 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
891 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
892 __ Addiu32(index_reg, index_reg, offset_);
893 } else {
894 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
895 // intrinsics, `index_` is not shifted by a scale factor of 2
896 // (as in the case of ArrayGet), as it is actually an offset
897 // to an object field within an object.
898 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
899 DCHECK(instruction_->GetLocations()->Intrinsified());
900 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
901 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
902 << instruction_->AsInvoke()->GetIntrinsic();
903 DCHECK_EQ(offset_, 0U);
904 DCHECK(index_.IsRegister());
905 }
906 }
907
908 // We're moving two or three locations to locations that could
909 // overlap, so we need a parallel move resolver.
910 InvokeRuntimeCallingConvention calling_convention;
911 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
912 parallel_move.AddMove(ref_,
913 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
914 Primitive::kPrimNot,
915 nullptr);
916 parallel_move.AddMove(obj_,
917 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
918 Primitive::kPrimNot,
919 nullptr);
920 if (index.IsValid()) {
921 parallel_move.AddMove(index,
922 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
923 Primitive::kPrimInt,
924 nullptr);
925 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
926 } else {
927 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
928 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
929 }
930 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
931 instruction_,
932 instruction_->GetDexPc(),
933 this);
934 CheckEntrypointTypes<
935 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
936 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
937
938 RestoreLiveRegisters(codegen, locations);
939 __ Bc(GetExitLabel());
940 }
941
942 const char* GetDescription() const OVERRIDE {
943 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
944 }
945
946 private:
947 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
948 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
949 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
950 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
951 if (i != ref &&
952 i != obj &&
953 !codegen->IsCoreCalleeSaveRegister(i) &&
954 !codegen->IsBlockedCoreRegister(i)) {
955 return static_cast<GpuRegister>(i);
956 }
957 }
958 // We shall never fail to find a free caller-save register, as
959 // there are more than two core caller-save registers on MIPS64
960 // (meaning it is possible to find one which is different from
961 // `ref` and `obj`).
962 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
963 LOG(FATAL) << "Could not find a free caller-save register";
964 UNREACHABLE();
965 }
966
967 const Location out_;
968 const Location ref_;
969 const Location obj_;
970 const uint32_t offset_;
971 // An additional location containing an index to an array.
972 // Only used for HArrayGet and the UnsafeGetObject &
973 // UnsafeGetObjectVolatile intrinsics.
974 const Location index_;
975
976 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
977};
978
979// Slow path generating a read barrier for a GC root.
980class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
981 public:
982 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
983 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
984 DCHECK(kEmitCompilerReadBarrier);
985 }
986
987 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
988 LocationSummary* locations = instruction_->GetLocations();
989 Primitive::Type type = Primitive::kPrimNot;
990 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
991 DCHECK(locations->CanCall());
992 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
993 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
994 << "Unexpected instruction in read barrier for GC root slow path: "
995 << instruction_->DebugName();
996
997 __ Bind(GetEntryLabel());
998 SaveLiveRegisters(codegen, locations);
999
1000 InvokeRuntimeCallingConvention calling_convention;
1001 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
1002 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1003 root_,
1004 Primitive::kPrimNot);
1005 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1006 instruction_,
1007 instruction_->GetDexPc(),
1008 this);
1009 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1010 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1011
1012 RestoreLiveRegisters(codegen, locations);
1013 __ Bc(GetExitLabel());
1014 }
1015
1016 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
1017
1018 private:
1019 const Location out_;
1020 const Location root_;
1021
1022 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
1023};
1024
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
1026 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001027 const CompilerOptions& compiler_options,
1028 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001029 : CodeGenerator(graph,
1030 kNumberOfGpuRegisters,
1031 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001032 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001033 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1034 arraysize(kCoreCalleeSaves)),
1035 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1036 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001037 compiler_options,
1038 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001039 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001040 location_builder_(graph, this),
1041 instruction_visitor_(graph, this),
1042 move_resolver_(graph->GetArena(), this),
Goran Jakovljevic19680d32017-05-11 10:38:36 +02001043 assembler_(graph->GetArena(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001044 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001045 uint32_literals_(std::less<uint32_t>(),
1046 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -08001047 uint64_literals_(std::less<uint64_t>(),
1048 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001049 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001050 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -08001051 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001052 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001053 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001054 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001055 jit_string_patches_(StringReferenceValueComparator(),
1056 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1057 jit_class_patches_(TypeReferenceValueComparator(),
1058 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001059 // Save RA (containing the return address) to mimic Quick.
1060 AddAllocatedRegister(Location::RegisterLocation(RA));
1061}
1062
1063#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001064// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1065#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001066#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -07001067
1068void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001069 // Ensure that we fix up branches.
1070 __ FinalizeCode();
1071
1072 // Adjust native pc offsets in stack maps.
1073 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001074 uint32_t old_position =
1075 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001076 uint32_t new_position = __ GetAdjustedPosition(old_position);
1077 DCHECK_GE(new_position, old_position);
1078 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1079 }
1080
1081 // Adjust pc offsets for the disassembly information.
1082 if (disasm_info_ != nullptr) {
1083 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1084 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1085 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1086 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1087 it.second.start = __ GetAdjustedPosition(it.second.start);
1088 it.second.end = __ GetAdjustedPosition(it.second.end);
1089 }
1090 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1091 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1092 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1093 }
1094 }
1095
Alexey Frunze4dda3372015-06-01 18:31:49 -07001096 CodeGenerator::Finalize(allocator);
1097}
1098
1099Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1100 return codegen_->GetAssembler();
1101}
1102
1103void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001104 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001105 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1106}
1107
1108void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001109 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001110 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1111}
1112
1113void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1114 // Pop reg
1115 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001116 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001117}
1118
1119void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1120 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001121 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001122 __ Sd(GpuRegister(reg), SP, 0);
1123}
1124
1125void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1126 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1127 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1128 // Allocate a scratch register other than TMP, if available.
1129 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1130 // automatically unspilled when the scratch scope object is destroyed).
1131 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1132 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001133 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001134 __ LoadFromOffset(load_type,
1135 GpuRegister(ensure_scratch.GetRegister()),
1136 SP,
1137 index1 + stack_offset);
1138 __ LoadFromOffset(load_type,
1139 TMP,
1140 SP,
1141 index2 + stack_offset);
1142 __ StoreToOffset(store_type,
1143 GpuRegister(ensure_scratch.GetRegister()),
1144 SP,
1145 index2 + stack_offset);
1146 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1147}
1148
1149static dwarf::Reg DWARFReg(GpuRegister reg) {
1150 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1151}
1152
David Srbeckyba702002016-02-01 18:15:29 +00001153static dwarf::Reg DWARFReg(FpuRegister reg) {
1154 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1155}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001156
1157void CodeGeneratorMIPS64::GenerateFrameEntry() {
1158 __ Bind(&frame_entry_label_);
1159
1160 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1161
1162 if (do_overflow_check) {
1163 __ LoadFromOffset(kLoadWord,
1164 ZERO,
1165 SP,
1166 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1167 RecordPcInfo(nullptr, 0);
1168 }
1169
Alexey Frunze4dda3372015-06-01 18:31:49 -07001170 if (HasEmptyFrame()) {
1171 return;
1172 }
1173
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001174 // Make sure the frame size isn't unreasonably large.
1175 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1176 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1177 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001178
1179 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001180
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001181 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182 __ IncreaseFrameSize(ofs);
1183
1184 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1185 GpuRegister reg = kCoreCalleeSaves[i];
1186 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001187 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001188 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001189 __ cfi().RelOffset(DWARFReg(reg), ofs);
1190 }
1191 }
1192
1193 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1194 FpuRegister reg = kFpuCalleeSaves[i];
1195 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001196 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001197 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001198 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001199 }
1200 }
1201
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001202 // Save the current method if we need it. Note that we do not
1203 // do this in HCurrentMethod, as the instruction might have been removed
1204 // in the SSA graph.
1205 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001206 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001207 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001208
1209 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1210 // Initialize should_deoptimize flag to 0.
1211 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1212 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001213}
1214
1215void CodeGeneratorMIPS64::GenerateFrameExit() {
1216 __ cfi().RememberState();
1217
Alexey Frunze4dda3372015-06-01 18:31:49 -07001218 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001220
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001221 // For better instruction scheduling restore RA before other registers.
1222 uint32_t ofs = GetFrameSize();
1223 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 GpuRegister reg = kCoreCalleeSaves[i];
1225 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001226 ofs -= kMips64DoublewordSize;
1227 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 __ cfi().Restore(DWARFReg(reg));
1229 }
1230 }
1231
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001232 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1233 FpuRegister reg = kFpuCalleeSaves[i];
1234 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1235 ofs -= kMips64DoublewordSize;
1236 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1237 __ cfi().Restore(DWARFReg(reg));
1238 }
1239 }
1240
1241 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001242 }
1243
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001244 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001245
1246 __ cfi().RestoreState();
1247 __ cfi().DefCFAOffset(GetFrameSize());
1248}
1249
1250void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1251 __ Bind(GetLabelOf(block));
1252}
1253
1254void CodeGeneratorMIPS64::MoveLocation(Location destination,
1255 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001256 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001257 if (source.Equals(destination)) {
1258 return;
1259 }
1260
1261 // A valid move can always be inferred from the destination and source
1262 // locations. When moving from and to a register, the argument type can be
1263 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001264 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001265 DCHECK_EQ(unspecified_type, false);
1266
1267 if (destination.IsRegister() || destination.IsFpuRegister()) {
1268 if (unspecified_type) {
1269 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1270 if (source.IsStackSlot() ||
1271 (src_cst != nullptr && (src_cst->IsIntConstant()
1272 || src_cst->IsFloatConstant()
1273 || src_cst->IsNullConstant()))) {
1274 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001276 } else {
1277 // If the source is a double stack slot or a 64bit constant, a 64bit
1278 // type is appropriate. Else the source is a register, and since the
1279 // type has not been specified, we chose a 64bit type to force a 64bit
1280 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001282 }
1283 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001284 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1285 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001286 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1287 // Move to GPR/FPR from stack
1288 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 __ LoadFpuFromOffset(load_type,
1291 destination.AsFpuRegister<FpuRegister>(),
1292 SP,
1293 source.GetStackIndex());
1294 } else {
1295 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1296 __ LoadFromOffset(load_type,
1297 destination.AsRegister<GpuRegister>(),
1298 SP,
1299 source.GetStackIndex());
1300 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001301 } else if (source.IsSIMDStackSlot()) {
1302 __ LoadFpuFromOffset(kLoadQuadword,
1303 destination.AsFpuRegister<FpuRegister>(),
1304 SP,
1305 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001306 } else if (source.IsConstant()) {
1307 // Move to GPR/FPR from constant
1308 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001309 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001310 gpr = destination.AsRegister<GpuRegister>();
1311 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001312 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001313 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1314 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1315 gpr = ZERO;
1316 } else {
1317 __ LoadConst32(gpr, value);
1318 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001320 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1321 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1322 gpr = ZERO;
1323 } else {
1324 __ LoadConst64(gpr, value);
1325 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001326 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001327 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001328 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001329 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1331 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001332 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001333 if (destination.IsRegister()) {
1334 // Move to GPR from GPR
1335 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1336 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001337 DCHECK(destination.IsFpuRegister());
1338 if (Primitive::Is64BitType(dst_type)) {
1339 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1340 } else {
1341 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1342 }
1343 }
1344 } else if (source.IsFpuRegister()) {
1345 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001346 if (GetGraph()->HasSIMD()) {
1347 __ MoveV(VectorRegisterFrom(destination),
1348 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001349 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001350 // Move to FPR from FPR
1351 if (dst_type == Primitive::kPrimFloat) {
1352 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1353 } else {
1354 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
1355 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1356 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001357 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001358 } else {
1359 DCHECK(destination.IsRegister());
1360 if (Primitive::Is64BitType(dst_type)) {
1361 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1362 } else {
1363 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1364 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001365 }
1366 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001367 } else if (destination.IsSIMDStackSlot()) {
1368 if (source.IsFpuRegister()) {
1369 __ StoreFpuToOffset(kStoreQuadword,
1370 source.AsFpuRegister<FpuRegister>(),
1371 SP,
1372 destination.GetStackIndex());
1373 } else {
1374 DCHECK(source.IsSIMDStackSlot());
1375 __ LoadFpuFromOffset(kLoadQuadword,
1376 FTMP,
1377 SP,
1378 source.GetStackIndex());
1379 __ StoreFpuToOffset(kStoreQuadword,
1380 FTMP,
1381 SP,
1382 destination.GetStackIndex());
1383 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001384 } else { // The destination is not a register. It must be a stack slot.
1385 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1386 if (source.IsRegister() || source.IsFpuRegister()) {
1387 if (unspecified_type) {
1388 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001389 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001390 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001391 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001392 }
1393 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001394 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1395 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001396 // Move to stack from GPR/FPR
1397 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1398 if (source.IsRegister()) {
1399 __ StoreToOffset(store_type,
1400 source.AsRegister<GpuRegister>(),
1401 SP,
1402 destination.GetStackIndex());
1403 } else {
1404 __ StoreFpuToOffset(store_type,
1405 source.AsFpuRegister<FpuRegister>(),
1406 SP,
1407 destination.GetStackIndex());
1408 }
1409 } else if (source.IsConstant()) {
1410 // Move to stack from constant
1411 HConstant* src_cst = source.GetConstant();
1412 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001413 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001415 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1416 if (value != 0) {
1417 gpr = TMP;
1418 __ LoadConst32(gpr, value);
1419 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001420 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001421 DCHECK(destination.IsDoubleStackSlot());
1422 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1423 if (value != 0) {
1424 gpr = TMP;
1425 __ LoadConst64(gpr, value);
1426 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001427 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001428 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001429 } else {
1430 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1431 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1432 // Move to stack from stack
1433 if (destination.IsStackSlot()) {
1434 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1435 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1436 } else {
1437 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1438 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1439 }
1440 }
1441 }
1442}
1443
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001444void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001445 DCHECK(!loc1.IsConstant());
1446 DCHECK(!loc2.IsConstant());
1447
1448 if (loc1.Equals(loc2)) {
1449 return;
1450 }
1451
1452 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1453 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1454 bool is_fp_reg1 = loc1.IsFpuRegister();
1455 bool is_fp_reg2 = loc2.IsFpuRegister();
1456
1457 if (loc2.IsRegister() && loc1.IsRegister()) {
1458 // Swap 2 GPRs
1459 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1460 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1461 __ Move(TMP, r2);
1462 __ Move(r2, r1);
1463 __ Move(r1, TMP);
1464 } else if (is_fp_reg2 && is_fp_reg1) {
1465 // Swap 2 FPRs
1466 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1467 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001468 if (type == Primitive::kPrimFloat) {
1469 __ MovS(FTMP, r1);
1470 __ MovS(r1, r2);
1471 __ MovS(r2, FTMP);
1472 } else {
1473 DCHECK_EQ(type, Primitive::kPrimDouble);
1474 __ MovD(FTMP, r1);
1475 __ MovD(r1, r2);
1476 __ MovD(r2, FTMP);
1477 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001478 } else if (is_slot1 != is_slot2) {
1479 // Swap GPR/FPR and stack slot
1480 Location reg_loc = is_slot1 ? loc2 : loc1;
1481 Location mem_loc = is_slot1 ? loc1 : loc2;
1482 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1483 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1484 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1485 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1486 if (reg_loc.IsFpuRegister()) {
1487 __ StoreFpuToOffset(store_type,
1488 reg_loc.AsFpuRegister<FpuRegister>(),
1489 SP,
1490 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001491 if (mem_loc.IsStackSlot()) {
1492 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1493 } else {
1494 DCHECK(mem_loc.IsDoubleStackSlot());
1495 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1496 }
1497 } else {
1498 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1499 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1500 }
1501 } else if (is_slot1 && is_slot2) {
1502 move_resolver_.Exchange(loc1.GetStackIndex(),
1503 loc2.GetStackIndex(),
1504 loc1.IsDoubleStackSlot());
1505 } else {
1506 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1507 }
1508}
1509
Calin Juravle175dc732015-08-25 15:42:32 +01001510void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1511 DCHECK(location.IsRegister());
1512 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1513}
1514
Calin Juravlee460d1d2015-09-29 04:52:17 +01001515void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1516 if (location.IsRegister()) {
1517 locations->AddTemp(location);
1518 } else {
1519 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1520 }
1521}
1522
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001523void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1524 GpuRegister value,
1525 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001526 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001527 GpuRegister card = AT;
1528 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001529 if (value_can_be_null) {
1530 __ Beqzc(value, &done);
1531 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001532 __ LoadFromOffset(kLoadDoubleword,
1533 card,
1534 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001535 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001536 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1537 __ Daddu(temp, card, temp);
1538 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001539 if (value_can_be_null) {
1540 __ Bind(&done);
1541 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001542}
1543
Alexey Frunze19f6c692016-11-30 19:19:55 -08001544template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1545inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1546 const ArenaDeque<PcRelativePatchInfo>& infos,
1547 ArenaVector<LinkerPatch>* linker_patches) {
1548 for (const PcRelativePatchInfo& info : infos) {
1549 const DexFile& dex_file = info.target_dex_file;
1550 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001551 DCHECK(info.label.IsBound());
1552 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1553 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1554 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1555 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001556 }
1557}
1558
1559void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1560 DCHECK(linker_patches->empty());
1561 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001562 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001563 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001564 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001565 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001566 pc_relative_string_patches_.size() +
1567 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001568 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001569 if (GetCompilerOptions().IsBootImage()) {
1570 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Alexey Frunzef63f5692016-12-13 17:43:11 -08001571 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001572 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1573 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001574 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1575 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001576 } else {
1577 DCHECK(pc_relative_method_patches_.empty());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01001578 EmitPcRelativeLinkerPatches<LinkerPatch::TypeClassTablePatch>(pc_relative_type_patches_,
1579 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001580 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(pc_relative_string_patches_,
1581 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001582 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001583 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1584 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001585 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1586 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001587 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
1588 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001589 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001590}
1591
Vladimir Marko65979462017-05-19 17:25:12 +01001592CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001593 MethodReference target_method,
1594 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001595 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001596 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001597 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001598 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001599}
1600
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001601CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001602 MethodReference target_method,
1603 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001604 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001605 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001606 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001607 &method_bss_entry_patches_);
1608}
1609
Alexey Frunzef63f5692016-12-13 17:43:11 -08001610CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001611 const DexFile& dex_file,
1612 dex::TypeIndex type_index,
1613 const PcRelativePatchInfo* info_high) {
1614 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001615}
1616
Vladimir Marko1998cd02017-01-13 13:02:58 +00001617CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001618 const DexFile& dex_file,
1619 dex::TypeIndex type_index,
1620 const PcRelativePatchInfo* info_high) {
1621 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001622}
1623
Vladimir Marko65979462017-05-19 17:25:12 +01001624CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001625 const DexFile& dex_file,
1626 dex::StringIndex string_index,
1627 const PcRelativePatchInfo* info_high) {
1628 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001629}
1630
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001631CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1632 const DexFile& dex_file,
1633 dex::StringIndex string_index,
1634 const PcRelativePatchInfo* info_high) {
1635 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1636}
1637
Alexey Frunze19f6c692016-11-30 19:19:55 -08001638CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001639 const DexFile& dex_file,
1640 uint32_t offset_or_index,
1641 const PcRelativePatchInfo* info_high,
1642 ArenaDeque<PcRelativePatchInfo>* patches) {
1643 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001644 return &patches->back();
1645}
1646
Alexey Frunzef63f5692016-12-13 17:43:11 -08001647Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1648 return map->GetOrCreate(
1649 value,
1650 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1651}
1652
Alexey Frunze19f6c692016-11-30 19:19:55 -08001653Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1654 return uint64_literals_.GetOrCreate(
1655 value,
1656 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1657}
1658
Alexey Frunzef63f5692016-12-13 17:43:11 -08001659Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001660 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001661}
1662
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001663void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1664 GpuRegister out,
1665 PcRelativePatchInfo* info_low) {
1666 DCHECK(!info_high->patch_info_high);
1667 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001668 // Add the high half of a 32-bit offset to PC.
1669 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001670 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001671 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001672 if (info_low != nullptr) {
1673 DCHECK_EQ(info_low->patch_info_high, info_high);
1674 __ Bind(&info_low->label);
1675 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001676}
1677
Alexey Frunze627c1a02017-01-30 19:28:14 -08001678Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1679 dex::StringIndex string_index,
1680 Handle<mirror::String> handle) {
1681 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1682 reinterpret_cast64<uint64_t>(handle.GetReference()));
1683 return jit_string_patches_.GetOrCreate(
1684 StringReference(&dex_file, string_index),
1685 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1686}
1687
1688Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1689 dex::TypeIndex type_index,
1690 Handle<mirror::Class> handle) {
1691 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1692 reinterpret_cast64<uint64_t>(handle.GetReference()));
1693 return jit_class_patches_.GetOrCreate(
1694 TypeReference(&dex_file, type_index),
1695 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1696}
1697
1698void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1699 const uint8_t* roots_data,
1700 const Literal* literal,
1701 uint64_t index_in_table) const {
1702 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1703 uintptr_t address =
1704 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1705 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1706}
1707
1708void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1709 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001710 const StringReference& string_reference = entry.first;
1711 Literal* table_entry_literal = entry.second;
1712 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001713 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001714 uint64_t index_in_table = it->second;
1715 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001716 }
1717 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001718 const TypeReference& type_reference = entry.first;
1719 Literal* table_entry_literal = entry.second;
1720 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001721 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001722 uint64_t index_in_table = it->second;
1723 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001724 }
1725}
1726
David Brazdil58282f42016-01-14 12:45:10 +00001727void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1729 blocked_core_registers_[ZERO] = true;
1730 blocked_core_registers_[K0] = true;
1731 blocked_core_registers_[K1] = true;
1732 blocked_core_registers_[GP] = true;
1733 blocked_core_registers_[SP] = true;
1734 blocked_core_registers_[RA] = true;
1735
Lazar Trsicd9672662015-09-03 17:33:01 +02001736 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1737 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738 blocked_core_registers_[AT] = true;
1739 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001740 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001741 blocked_fpu_registers_[FTMP] = true;
1742
1743 // Reserve suspend and thread registers.
1744 blocked_core_registers_[S0] = true;
1745 blocked_core_registers_[TR] = true;
1746
1747 // Reserve T9 for function calls
1748 blocked_core_registers_[T9] = true;
1749
Goran Jakovljevic782be112016-06-21 12:39:04 +02001750 if (GetGraph()->IsDebuggable()) {
1751 // Stubs do not save callee-save floating point registers. If the graph
1752 // is debuggable, we need to deal with these registers differently. For
1753 // now, just block them.
1754 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1755 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1756 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001757 }
1758}
1759
Alexey Frunze4dda3372015-06-01 18:31:49 -07001760size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1761 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001762 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001763}
1764
1765size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1766 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001767 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001768}
1769
1770size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001771 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1772 FpuRegister(reg_id),
1773 SP,
1774 stack_index);
1775 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001776}
1777
1778size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001779 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1780 FpuRegister(reg_id),
1781 SP,
1782 stack_index);
1783 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784}
1785
1786void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001787 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001788}
1789
1790void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001791 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001792}
1793
Calin Juravle175dc732015-08-25 15:42:32 +01001794void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001795 HInstruction* instruction,
1796 uint32_t dex_pc,
1797 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001798 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001799 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001800 if (EntrypointRequiresStackMap(entrypoint)) {
1801 RecordPcInfo(instruction, dex_pc, slow_path);
1802 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803}
1804
Alexey Frunze15958152017-02-09 19:08:30 -08001805void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1806 HInstruction* instruction,
1807 SlowPathCode* slow_path) {
1808 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1809 GenerateInvokeRuntime(entry_point_offset);
1810}
1811
1812void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1813 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1814 __ Jalr(T9);
1815 __ Nop();
1816}
1817
Alexey Frunze4dda3372015-06-01 18:31:49 -07001818void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1819 GpuRegister class_reg) {
1820 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1821 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1822 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001823 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1824 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001825 __ Bind(slow_path->GetExitLabel());
1826}
1827
1828void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1829 __ Sync(0); // only stype 0 is supported
1830}
1831
1832void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1833 HBasicBlock* successor) {
1834 SuspendCheckSlowPathMIPS64* slow_path =
1835 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1836 codegen_->AddSlowPath(slow_path);
1837
1838 __ LoadFromOffset(kLoadUnsignedHalfword,
1839 TMP,
1840 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001841 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001842 if (successor == nullptr) {
1843 __ Bnezc(TMP, slow_path->GetEntryLabel());
1844 __ Bind(slow_path->GetReturnLabel());
1845 } else {
1846 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001847 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001848 // slow_path will return to GetLabelOf(successor).
1849 }
1850}
1851
1852InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1853 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001854 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001855 assembler_(codegen->GetAssembler()),
1856 codegen_(codegen) {}
1857
1858void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1859 DCHECK_EQ(instruction->InputCount(), 2U);
1860 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1861 Primitive::Type type = instruction->GetResultType();
1862 switch (type) {
1863 case Primitive::kPrimInt:
1864 case Primitive::kPrimLong: {
1865 locations->SetInAt(0, Location::RequiresRegister());
1866 HInstruction* right = instruction->InputAt(1);
1867 bool can_use_imm = false;
1868 if (right->IsConstant()) {
1869 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1870 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1871 can_use_imm = IsUint<16>(imm);
1872 } else if (instruction->IsAdd()) {
1873 can_use_imm = IsInt<16>(imm);
1874 } else {
1875 DCHECK(instruction->IsSub());
1876 can_use_imm = IsInt<16>(-imm);
1877 }
1878 }
1879 if (can_use_imm)
1880 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1881 else
1882 locations->SetInAt(1, Location::RequiresRegister());
1883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1884 }
1885 break;
1886
1887 case Primitive::kPrimFloat:
1888 case Primitive::kPrimDouble:
1889 locations->SetInAt(0, Location::RequiresFpuRegister());
1890 locations->SetInAt(1, Location::RequiresFpuRegister());
1891 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1896 }
1897}
1898
1899void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1900 Primitive::Type type = instruction->GetType();
1901 LocationSummary* locations = instruction->GetLocations();
1902
1903 switch (type) {
1904 case Primitive::kPrimInt:
1905 case Primitive::kPrimLong: {
1906 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1907 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1908 Location rhs_location = locations->InAt(1);
1909
1910 GpuRegister rhs_reg = ZERO;
1911 int64_t rhs_imm = 0;
1912 bool use_imm = rhs_location.IsConstant();
1913 if (use_imm) {
1914 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1915 } else {
1916 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1917 }
1918
1919 if (instruction->IsAnd()) {
1920 if (use_imm)
1921 __ Andi(dst, lhs, rhs_imm);
1922 else
1923 __ And(dst, lhs, rhs_reg);
1924 } else if (instruction->IsOr()) {
1925 if (use_imm)
1926 __ Ori(dst, lhs, rhs_imm);
1927 else
1928 __ Or(dst, lhs, rhs_reg);
1929 } else if (instruction->IsXor()) {
1930 if (use_imm)
1931 __ Xori(dst, lhs, rhs_imm);
1932 else
1933 __ Xor(dst, lhs, rhs_reg);
1934 } else if (instruction->IsAdd()) {
1935 if (type == Primitive::kPrimInt) {
1936 if (use_imm)
1937 __ Addiu(dst, lhs, rhs_imm);
1938 else
1939 __ Addu(dst, lhs, rhs_reg);
1940 } else {
1941 if (use_imm)
1942 __ Daddiu(dst, lhs, rhs_imm);
1943 else
1944 __ Daddu(dst, lhs, rhs_reg);
1945 }
1946 } else {
1947 DCHECK(instruction->IsSub());
1948 if (type == Primitive::kPrimInt) {
1949 if (use_imm)
1950 __ Addiu(dst, lhs, -rhs_imm);
1951 else
1952 __ Subu(dst, lhs, rhs_reg);
1953 } else {
1954 if (use_imm)
1955 __ Daddiu(dst, lhs, -rhs_imm);
1956 else
1957 __ Dsubu(dst, lhs, rhs_reg);
1958 }
1959 }
1960 break;
1961 }
1962 case Primitive::kPrimFloat:
1963 case Primitive::kPrimDouble: {
1964 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1965 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1966 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1967 if (instruction->IsAdd()) {
1968 if (type == Primitive::kPrimFloat)
1969 __ AddS(dst, lhs, rhs);
1970 else
1971 __ AddD(dst, lhs, rhs);
1972 } else if (instruction->IsSub()) {
1973 if (type == Primitive::kPrimFloat)
1974 __ SubS(dst, lhs, rhs);
1975 else
1976 __ SubD(dst, lhs, rhs);
1977 } else {
1978 LOG(FATAL) << "Unexpected floating-point binary operation";
1979 }
1980 break;
1981 }
1982 default:
1983 LOG(FATAL) << "Unexpected binary operation type " << type;
1984 }
1985}
1986
1987void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001988 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001989
1990 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1991 Primitive::Type type = instr->GetResultType();
1992 switch (type) {
1993 case Primitive::kPrimInt:
1994 case Primitive::kPrimLong: {
1995 locations->SetInAt(0, Location::RequiresRegister());
1996 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001997 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001998 break;
1999 }
2000 default:
2001 LOG(FATAL) << "Unexpected shift type " << type;
2002 }
2003}
2004
2005void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002006 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002007 LocationSummary* locations = instr->GetLocations();
2008 Primitive::Type type = instr->GetType();
2009
2010 switch (type) {
2011 case Primitive::kPrimInt:
2012 case Primitive::kPrimLong: {
2013 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2014 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2015 Location rhs_location = locations->InAt(1);
2016
2017 GpuRegister rhs_reg = ZERO;
2018 int64_t rhs_imm = 0;
2019 bool use_imm = rhs_location.IsConstant();
2020 if (use_imm) {
2021 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2022 } else {
2023 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2024 }
2025
2026 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002027 uint32_t shift_value = rhs_imm &
2028 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002029
Alexey Frunze92d90602015-12-18 18:16:36 -08002030 if (shift_value == 0) {
2031 if (dst != lhs) {
2032 __ Move(dst, lhs);
2033 }
2034 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002035 if (instr->IsShl()) {
2036 __ Sll(dst, lhs, shift_value);
2037 } else if (instr->IsShr()) {
2038 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002039 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002041 } else {
2042 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043 }
2044 } else {
2045 if (shift_value < 32) {
2046 if (instr->IsShl()) {
2047 __ Dsll(dst, lhs, shift_value);
2048 } else if (instr->IsShr()) {
2049 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002050 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002051 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002052 } else {
2053 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002054 }
2055 } else {
2056 shift_value -= 32;
2057 if (instr->IsShl()) {
2058 __ Dsll32(dst, lhs, shift_value);
2059 } else if (instr->IsShr()) {
2060 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002061 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002062 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002063 } else {
2064 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002065 }
2066 }
2067 }
2068 } else {
2069 if (type == Primitive::kPrimInt) {
2070 if (instr->IsShl()) {
2071 __ Sllv(dst, lhs, rhs_reg);
2072 } else if (instr->IsShr()) {
2073 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002074 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002075 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002076 } else {
2077 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002078 }
2079 } else {
2080 if (instr->IsShl()) {
2081 __ Dsllv(dst, lhs, rhs_reg);
2082 } else if (instr->IsShr()) {
2083 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002084 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002085 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002086 } else {
2087 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002088 }
2089 }
2090 }
2091 break;
2092 }
2093 default:
2094 LOG(FATAL) << "Unexpected shift operation type " << type;
2095 }
2096}
2097
2098void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2099 HandleBinaryOp(instruction);
2100}
2101
2102void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2103 HandleBinaryOp(instruction);
2104}
2105
2106void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2107 HandleBinaryOp(instruction);
2108}
2109
2110void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2111 HandleBinaryOp(instruction);
2112}
2113
2114void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002115 Primitive::Type type = instruction->GetType();
2116 bool object_array_get_with_read_barrier =
2117 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002118 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002119 new (GetGraph()->GetArena()) LocationSummary(instruction,
2120 object_array_get_with_read_barrier
2121 ? LocationSummary::kCallOnSlowPath
2122 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002123 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2124 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2125 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002126 locations->SetInAt(0, Location::RequiresRegister());
2127 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002128 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002129 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2130 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002131 // The output overlaps in the case of an object array get with
2132 // read barriers enabled: we do not want the move to overwrite the
2133 // array's location, as we need it to emit the read barrier.
2134 locations->SetOut(Location::RequiresRegister(),
2135 object_array_get_with_read_barrier
2136 ? Location::kOutputOverlap
2137 : Location::kNoOutputOverlap);
2138 }
2139 // We need a temporary register for the read barrier marking slow
2140 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2141 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002142 bool temp_needed = instruction->GetIndex()->IsConstant()
2143 ? !kBakerReadBarrierThunksEnableForFields
2144 : !kBakerReadBarrierThunksEnableForArrays;
2145 if (temp_needed) {
2146 locations->AddTemp(Location::RequiresRegister());
2147 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002148 }
2149}
2150
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002151static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2152 auto null_checker = [codegen, instruction]() {
2153 codegen->MaybeRecordImplicitNullCheck(instruction);
2154 };
2155 return null_checker;
2156}
2157
Alexey Frunze4dda3372015-06-01 18:31:49 -07002158void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2159 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002160 Location obj_loc = locations->InAt(0);
2161 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2162 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002163 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002164 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002165 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002167 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002168 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2169 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002170 switch (type) {
2171 case Primitive::kPrimBoolean: {
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
2184 case Primitive::kPrimByte: {
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
2197 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002198 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199 if (index.IsConstant()) {
2200 size_t offset =
2201 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002202 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002204 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002205 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002206 }
2207 break;
2208 }
2209
2210 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002211 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002212 if (maybe_compressed_char_at) {
2213 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002214 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002215 __ Dext(TMP, TMP, 0, 1);
2216 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2217 "Expecting 0=compressed, 1=uncompressed");
2218 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002219 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002220 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2221 if (maybe_compressed_char_at) {
2222 Mips64Label uncompressed_load, done;
2223 __ Bnezc(TMP, &uncompressed_load);
2224 __ LoadFromOffset(kLoadUnsignedByte,
2225 out,
2226 obj,
2227 data_offset + (const_index << TIMES_1));
2228 __ Bc(&done);
2229 __ Bind(&uncompressed_load);
2230 __ LoadFromOffset(kLoadUnsignedHalfword,
2231 out,
2232 obj,
2233 data_offset + (const_index << TIMES_2));
2234 __ Bind(&done);
2235 } else {
2236 __ LoadFromOffset(kLoadUnsignedHalfword,
2237 out,
2238 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002239 data_offset + (const_index << TIMES_2),
2240 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002241 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002242 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002243 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2244 if (maybe_compressed_char_at) {
2245 Mips64Label uncompressed_load, done;
2246 __ Bnezc(TMP, &uncompressed_load);
2247 __ Daddu(TMP, obj, index_reg);
2248 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2249 __ Bc(&done);
2250 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002251 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002252 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2253 __ Bind(&done);
2254 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002255 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002256 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002257 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002258 }
2259 break;
2260 }
2261
Alexey Frunze15958152017-02-09 19:08:30 -08002262 case Primitive::kPrimInt: {
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>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002265 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2266 if (index.IsConstant()) {
2267 size_t offset =
2268 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002269 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002270 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002271 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002272 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002273 }
2274 break;
2275 }
2276
Alexey Frunze15958152017-02-09 19:08:30 -08002277 case Primitive::kPrimNot: {
2278 static_assert(
2279 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2280 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2281 // /* HeapReference<Object> */ out =
2282 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2283 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002284 bool temp_needed = index.IsConstant()
2285 ? !kBakerReadBarrierThunksEnableForFields
2286 : !kBakerReadBarrierThunksEnableForArrays;
2287 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002288 // Note that a potential implicit null check is handled in this
2289 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002290 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2291 if (index.IsConstant()) {
2292 // Array load with a constant index can be treated as a field load.
2293 size_t offset =
2294 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2295 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2296 out_loc,
2297 obj,
2298 offset,
2299 temp,
2300 /* needs_null_check */ false);
2301 } else {
2302 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2303 out_loc,
2304 obj,
2305 data_offset,
2306 index,
2307 temp,
2308 /* needs_null_check */ false);
2309 }
Alexey Frunze15958152017-02-09 19:08:30 -08002310 } else {
2311 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2312 if (index.IsConstant()) {
2313 size_t offset =
2314 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2315 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2316 // If read barriers are enabled, emit read barriers other than
2317 // Baker's using a slow path (and also unpoison the loaded
2318 // reference, if heap poisoning is enabled).
2319 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2320 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002321 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002322 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2323 // If read barriers are enabled, emit read barriers other than
2324 // Baker's using a slow path (and also unpoison the loaded
2325 // reference, if heap poisoning is enabled).
2326 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2327 out_loc,
2328 out_loc,
2329 obj_loc,
2330 data_offset,
2331 index);
2332 }
2333 }
2334 break;
2335 }
2336
Alexey Frunze4dda3372015-06-01 18:31:49 -07002337 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002338 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002339 if (index.IsConstant()) {
2340 size_t offset =
2341 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002342 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002344 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002345 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002346 }
2347 break;
2348 }
2349
2350 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002351 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002352 if (index.IsConstant()) {
2353 size_t offset =
2354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002355 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002356 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002357 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002358 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002359 }
2360 break;
2361 }
2362
2363 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002364 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002365 if (index.IsConstant()) {
2366 size_t offset =
2367 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002368 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002369 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002370 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002371 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002372 }
2373 break;
2374 }
2375
2376 case Primitive::kPrimVoid:
2377 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2378 UNREACHABLE();
2379 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002380}
2381
2382void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2383 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2384 locations->SetInAt(0, Location::RequiresRegister());
2385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2386}
2387
2388void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2389 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002390 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002391 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2392 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2393 __ LoadFromOffset(kLoadWord, out, obj, offset);
2394 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002395 // Mask out compression flag from String's array length.
2396 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2397 __ Srl(out, out, 1u);
2398 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002399}
2400
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002401Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2402 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2403 ? Location::ConstantLocation(instruction->AsConstant())
2404 : Location::RequiresRegister();
2405}
2406
2407Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2408 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2409 // We can store a non-zero float or double constant without first loading it into the FPU,
2410 // but we should only prefer this if the constant has a single use.
2411 if (instruction->IsConstant() &&
2412 (instruction->AsConstant()->IsZeroBitPattern() ||
2413 instruction->GetUses().HasExactlyOneElement())) {
2414 return Location::ConstantLocation(instruction->AsConstant());
2415 // Otherwise fall through and require an FPU register for the constant.
2416 }
2417 return Location::RequiresFpuRegister();
2418}
2419
Alexey Frunze4dda3372015-06-01 18:31:49 -07002420void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002421 Primitive::Type value_type = instruction->GetComponentType();
2422
2423 bool needs_write_barrier =
2424 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2425 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2426
Alexey Frunze4dda3372015-06-01 18:31:49 -07002427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2428 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002429 may_need_runtime_call_for_type_check ?
2430 LocationSummary::kCallOnSlowPath :
2431 LocationSummary::kNoCall);
2432
2433 locations->SetInAt(0, Location::RequiresRegister());
2434 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2435 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2436 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002437 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002438 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2439 }
2440 if (needs_write_barrier) {
2441 // Temporary register for the write barrier.
2442 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002443 }
2444}
2445
2446void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2447 LocationSummary* locations = instruction->GetLocations();
2448 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2449 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002450 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002451 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002452 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002453 bool needs_write_barrier =
2454 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002455 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002456 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002457
2458 switch (value_type) {
2459 case Primitive::kPrimBoolean:
2460 case Primitive::kPrimByte: {
2461 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002462 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002463 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002464 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002465 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2466 }
2467 if (value_location.IsConstant()) {
2468 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2469 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2470 } else {
2471 GpuRegister value = value_location.AsRegister<GpuRegister>();
2472 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002473 }
2474 break;
2475 }
2476
2477 case Primitive::kPrimShort:
2478 case Primitive::kPrimChar: {
2479 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002480 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002481 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002482 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002483 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002484 }
2485 if (value_location.IsConstant()) {
2486 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2487 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2488 } else {
2489 GpuRegister value = value_location.AsRegister<GpuRegister>();
2490 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002491 }
2492 break;
2493 }
2494
Alexey Frunze15958152017-02-09 19:08:30 -08002495 case Primitive::kPrimInt: {
2496 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2497 if (index.IsConstant()) {
2498 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2499 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002500 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002501 }
2502 if (value_location.IsConstant()) {
2503 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2504 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2505 } else {
2506 GpuRegister value = value_location.AsRegister<GpuRegister>();
2507 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2508 }
2509 break;
2510 }
2511
Alexey Frunze4dda3372015-06-01 18:31:49 -07002512 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002513 if (value_location.IsConstant()) {
2514 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002516 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002517 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002519 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002520 }
Alexey Frunze15958152017-02-09 19:08:30 -08002521 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2522 DCHECK_EQ(value, 0);
2523 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2524 DCHECK(!needs_write_barrier);
2525 DCHECK(!may_need_runtime_call_for_type_check);
2526 break;
2527 }
2528
2529 DCHECK(needs_write_barrier);
2530 GpuRegister value = value_location.AsRegister<GpuRegister>();
2531 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2532 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2533 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2534 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2535 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2536 Mips64Label done;
2537 SlowPathCodeMIPS64* slow_path = nullptr;
2538
2539 if (may_need_runtime_call_for_type_check) {
2540 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2541 codegen_->AddSlowPath(slow_path);
2542 if (instruction->GetValueCanBeNull()) {
2543 Mips64Label non_zero;
2544 __ Bnezc(value, &non_zero);
2545 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2546 if (index.IsConstant()) {
2547 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002548 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002549 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002550 }
Alexey Frunze15958152017-02-09 19:08:30 -08002551 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2552 __ Bc(&done);
2553 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002554 }
Alexey Frunze15958152017-02-09 19:08:30 -08002555
2556 // Note that when read barriers are enabled, the type checks
2557 // are performed without read barriers. This is fine, even in
2558 // the case where a class object is in the from-space after
2559 // the flip, as a comparison involving such a type would not
2560 // produce a false positive; it may of course produce a false
2561 // negative, in which case we would take the ArraySet slow
2562 // path.
2563
2564 // /* HeapReference<Class> */ temp1 = obj->klass_
2565 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2566 __ MaybeUnpoisonHeapReference(temp1);
2567
2568 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2569 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2570 // /* HeapReference<Class> */ temp2 = value->klass_
2571 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2572 // If heap poisoning is enabled, no need to unpoison `temp1`
2573 // nor `temp2`, as we are comparing two poisoned references.
2574
2575 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2576 Mips64Label do_put;
2577 __ Beqc(temp1, temp2, &do_put);
2578 // If heap poisoning is enabled, the `temp1` reference has
2579 // not been unpoisoned yet; unpoison it now.
2580 __ MaybeUnpoisonHeapReference(temp1);
2581
2582 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2583 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2584 // If heap poisoning is enabled, no need to unpoison
2585 // `temp1`, as we are comparing against null below.
2586 __ Bnezc(temp1, slow_path->GetEntryLabel());
2587 __ Bind(&do_put);
2588 } else {
2589 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2590 }
2591 }
2592
2593 GpuRegister source = value;
2594 if (kPoisonHeapReferences) {
2595 // Note that in the case where `value` is a null reference,
2596 // we do not enter this block, as a null reference does not
2597 // need poisoning.
2598 __ Move(temp1, value);
2599 __ PoisonHeapReference(temp1);
2600 source = temp1;
2601 }
2602
2603 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2604 if (index.IsConstant()) {
2605 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002606 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002607 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002608 }
2609 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2610
2611 if (!may_need_runtime_call_for_type_check) {
2612 codegen_->MaybeRecordImplicitNullCheck(instruction);
2613 }
2614
2615 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2616
2617 if (done.IsLinked()) {
2618 __ Bind(&done);
2619 }
2620
2621 if (slow_path != nullptr) {
2622 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002623 }
2624 break;
2625 }
2626
2627 case Primitive::kPrimLong: {
2628 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002629 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002630 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002631 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002632 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002633 }
2634 if (value_location.IsConstant()) {
2635 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2636 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2637 } else {
2638 GpuRegister value = value_location.AsRegister<GpuRegister>();
2639 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002640 }
2641 break;
2642 }
2643
2644 case Primitive::kPrimFloat: {
2645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002646 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002647 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002648 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002649 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002650 }
2651 if (value_location.IsConstant()) {
2652 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2653 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2654 } else {
2655 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2656 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657 }
2658 break;
2659 }
2660
2661 case Primitive::kPrimDouble: {
2662 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002663 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002664 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002665 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002666 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002667 }
2668 if (value_location.IsConstant()) {
2669 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2670 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2671 } else {
2672 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2673 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674 }
2675 break;
2676 }
2677
2678 case Primitive::kPrimVoid:
2679 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2680 UNREACHABLE();
2681 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002682}
2683
2684void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002685 RegisterSet caller_saves = RegisterSet::Empty();
2686 InvokeRuntimeCallingConvention calling_convention;
2687 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2688 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2689 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002690 locations->SetInAt(0, Location::RequiresRegister());
2691 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002692}
2693
2694void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2695 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002696 BoundsCheckSlowPathMIPS64* slow_path =
2697 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002698 codegen_->AddSlowPath(slow_path);
2699
2700 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2701 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2702
2703 // length is limited by the maximum positive signed 32-bit integer.
2704 // Unsigned comparison of length and index checks for index < 0
2705 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002706 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002707}
2708
Alexey Frunze15958152017-02-09 19:08:30 -08002709// Temp is used for read barrier.
2710static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2711 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002712 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002713 (kUseBakerReadBarrier ||
2714 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2715 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2716 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2717 return 1;
2718 }
2719 return 0;
2720}
2721
2722// Extra temp is used for read barrier.
2723static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2724 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2725}
2726
Alexey Frunze4dda3372015-06-01 18:31:49 -07002727void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002728 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2729 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2730
2731 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2732 switch (type_check_kind) {
2733 case TypeCheckKind::kExactCheck:
2734 case TypeCheckKind::kAbstractClassCheck:
2735 case TypeCheckKind::kClassHierarchyCheck:
2736 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002737 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002738 ? LocationSummary::kCallOnSlowPath
2739 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2740 break;
2741 case TypeCheckKind::kArrayCheck:
2742 case TypeCheckKind::kUnresolvedCheck:
2743 case TypeCheckKind::kInterfaceCheck:
2744 call_kind = LocationSummary::kCallOnSlowPath;
2745 break;
2746 }
2747
2748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002749 locations->SetInAt(0, Location::RequiresRegister());
2750 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002751 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002752}
2753
2754void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002755 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002756 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002757 Location obj_loc = locations->InAt(0);
2758 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002759 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002760 Location temp_loc = locations->GetTemp(0);
2761 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2762 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2763 DCHECK_LE(num_temps, 2u);
2764 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002765 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2766 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2767 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2768 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2769 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2770 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2771 const uint32_t object_array_data_offset =
2772 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2773 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002774
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002775 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2776 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2777 // read barriers is done for performance and code size reasons.
2778 bool is_type_check_slow_path_fatal = false;
2779 if (!kEmitCompilerReadBarrier) {
2780 is_type_check_slow_path_fatal =
2781 (type_check_kind == TypeCheckKind::kExactCheck ||
2782 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2783 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2784 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2785 !instruction->CanThrowIntoCatchBlock();
2786 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002787 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002788 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2789 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002790 codegen_->AddSlowPath(slow_path);
2791
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002792 // Avoid this check if we know `obj` is not null.
2793 if (instruction->MustDoNullCheck()) {
2794 __ Beqzc(obj, &done);
2795 }
2796
2797 switch (type_check_kind) {
2798 case TypeCheckKind::kExactCheck:
2799 case TypeCheckKind::kArrayCheck: {
2800 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002801 GenerateReferenceLoadTwoRegisters(instruction,
2802 temp_loc,
2803 obj_loc,
2804 class_offset,
2805 maybe_temp2_loc,
2806 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002807 // Jump to slow path for throwing the exception or doing a
2808 // more involved array check.
2809 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2810 break;
2811 }
2812
2813 case TypeCheckKind::kAbstractClassCheck: {
2814 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002815 GenerateReferenceLoadTwoRegisters(instruction,
2816 temp_loc,
2817 obj_loc,
2818 class_offset,
2819 maybe_temp2_loc,
2820 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002821 // If the class is abstract, we eagerly fetch the super class of the
2822 // object to avoid doing a comparison we know will fail.
2823 Mips64Label loop;
2824 __ Bind(&loop);
2825 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002826 GenerateReferenceLoadOneRegister(instruction,
2827 temp_loc,
2828 super_offset,
2829 maybe_temp2_loc,
2830 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002831 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2832 // exception.
2833 __ Beqzc(temp, slow_path->GetEntryLabel());
2834 // Otherwise, compare the classes.
2835 __ Bnec(temp, cls, &loop);
2836 break;
2837 }
2838
2839 case TypeCheckKind::kClassHierarchyCheck: {
2840 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002841 GenerateReferenceLoadTwoRegisters(instruction,
2842 temp_loc,
2843 obj_loc,
2844 class_offset,
2845 maybe_temp2_loc,
2846 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002847 // Walk over the class hierarchy to find a match.
2848 Mips64Label loop;
2849 __ Bind(&loop);
2850 __ Beqc(temp, cls, &done);
2851 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002852 GenerateReferenceLoadOneRegister(instruction,
2853 temp_loc,
2854 super_offset,
2855 maybe_temp2_loc,
2856 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002857 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2858 // exception. Otherwise, jump to the beginning of the loop.
2859 __ Bnezc(temp, &loop);
2860 __ Bc(slow_path->GetEntryLabel());
2861 break;
2862 }
2863
2864 case TypeCheckKind::kArrayObjectCheck: {
2865 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002866 GenerateReferenceLoadTwoRegisters(instruction,
2867 temp_loc,
2868 obj_loc,
2869 class_offset,
2870 maybe_temp2_loc,
2871 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002872 // Do an exact check.
2873 __ Beqc(temp, cls, &done);
2874 // Otherwise, we need to check that the object's class is a non-primitive array.
2875 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002876 GenerateReferenceLoadOneRegister(instruction,
2877 temp_loc,
2878 component_offset,
2879 maybe_temp2_loc,
2880 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002881 // If the component type is null, jump to the slow path to throw the exception.
2882 __ Beqzc(temp, slow_path->GetEntryLabel());
2883 // Otherwise, the object is indeed an array, further check that this component
2884 // type is not a primitive type.
2885 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2886 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2887 __ Bnezc(temp, slow_path->GetEntryLabel());
2888 break;
2889 }
2890
2891 case TypeCheckKind::kUnresolvedCheck:
2892 // We always go into the type check slow path for the unresolved check case.
2893 // We cannot directly call the CheckCast runtime entry point
2894 // without resorting to a type checking slow path here (i.e. by
2895 // calling InvokeRuntime directly), as it would require to
2896 // assign fixed registers for the inputs of this HInstanceOf
2897 // instruction (following the runtime calling convention), which
2898 // might be cluttered by the potential first read barrier
2899 // emission at the beginning of this method.
2900 __ Bc(slow_path->GetEntryLabel());
2901 break;
2902
2903 case TypeCheckKind::kInterfaceCheck: {
2904 // Avoid read barriers to improve performance of the fast path. We can not get false
2905 // positives by doing this.
2906 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002907 GenerateReferenceLoadTwoRegisters(instruction,
2908 temp_loc,
2909 obj_loc,
2910 class_offset,
2911 maybe_temp2_loc,
2912 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002913 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002914 GenerateReferenceLoadTwoRegisters(instruction,
2915 temp_loc,
2916 temp_loc,
2917 iftable_offset,
2918 maybe_temp2_loc,
2919 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002920 // Iftable is never null.
2921 __ Lw(TMP, temp, array_length_offset);
2922 // Loop through the iftable and check if any class matches.
2923 Mips64Label loop;
2924 __ Bind(&loop);
2925 __ Beqzc(TMP, slow_path->GetEntryLabel());
2926 __ Lwu(AT, temp, object_array_data_offset);
2927 __ MaybeUnpoisonHeapReference(AT);
2928 // Go to next interface.
2929 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2930 __ Addiu(TMP, TMP, -2);
2931 // Compare the classes and continue the loop if they do not match.
2932 __ Bnec(AT, cls, &loop);
2933 break;
2934 }
2935 }
2936
2937 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938 __ Bind(slow_path->GetExitLabel());
2939}
2940
2941void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2942 LocationSummary* locations =
2943 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2944 locations->SetInAt(0, Location::RequiresRegister());
2945 if (check->HasUses()) {
2946 locations->SetOut(Location::SameAsFirstInput());
2947 }
2948}
2949
2950void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2951 // We assume the class is not null.
2952 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2953 check->GetLoadClass(),
2954 check,
2955 check->GetDexPc(),
2956 true);
2957 codegen_->AddSlowPath(slow_path);
2958 GenerateClassInitializationCheck(slow_path,
2959 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2960}
2961
2962void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2963 Primitive::Type in_type = compare->InputAt(0)->GetType();
2964
Alexey Frunze299a9392015-12-08 16:08:02 -08002965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002966
2967 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002968 case Primitive::kPrimBoolean:
2969 case Primitive::kPrimByte:
2970 case Primitive::kPrimShort:
2971 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002972 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002973 case Primitive::kPrimLong:
2974 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002975 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002976 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2977 break;
2978
2979 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002980 case Primitive::kPrimDouble:
2981 locations->SetInAt(0, Location::RequiresFpuRegister());
2982 locations->SetInAt(1, Location::RequiresFpuRegister());
2983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002984 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002985
2986 default:
2987 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2988 }
2989}
2990
2991void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2992 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002993 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002994 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2995
2996 // 0 if: left == right
2997 // 1 if: left > right
2998 // -1 if: left < right
2999 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003000 case Primitive::kPrimBoolean:
3001 case Primitive::kPrimByte:
3002 case Primitive::kPrimShort:
3003 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003004 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003005 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003006 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003007 Location rhs_location = locations->InAt(1);
3008 bool use_imm = rhs_location.IsConstant();
3009 GpuRegister rhs = ZERO;
3010 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003011 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08003012 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3013 if (value != 0) {
3014 rhs = AT;
3015 __ LoadConst64(rhs, value);
3016 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003017 } else {
3018 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3019 if (value != 0) {
3020 rhs = AT;
3021 __ LoadConst32(rhs, value);
3022 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003023 }
3024 } else {
3025 rhs = rhs_location.AsRegister<GpuRegister>();
3026 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003027 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003028 __ Slt(res, rhs, lhs);
3029 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003030 break;
3031 }
3032
Alexey Frunze299a9392015-12-08 16:08:02 -08003033 case Primitive::kPrimFloat: {
3034 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3035 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3036 Mips64Label done;
3037 __ CmpEqS(FTMP, lhs, rhs);
3038 __ LoadConst32(res, 0);
3039 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003040 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003041 __ CmpLtS(FTMP, lhs, rhs);
3042 __ LoadConst32(res, -1);
3043 __ Bc1nez(FTMP, &done);
3044 __ LoadConst32(res, 1);
3045 } else {
3046 __ CmpLtS(FTMP, rhs, lhs);
3047 __ LoadConst32(res, 1);
3048 __ Bc1nez(FTMP, &done);
3049 __ LoadConst32(res, -1);
3050 }
3051 __ Bind(&done);
3052 break;
3053 }
3054
Alexey Frunze4dda3372015-06-01 18:31:49 -07003055 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003056 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3057 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3058 Mips64Label done;
3059 __ CmpEqD(FTMP, lhs, rhs);
3060 __ LoadConst32(res, 0);
3061 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003062 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003063 __ CmpLtD(FTMP, lhs, rhs);
3064 __ LoadConst32(res, -1);
3065 __ Bc1nez(FTMP, &done);
3066 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003067 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003068 __ CmpLtD(FTMP, rhs, lhs);
3069 __ LoadConst32(res, 1);
3070 __ Bc1nez(FTMP, &done);
3071 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003072 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003073 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003074 break;
3075 }
3076
3077 default:
3078 LOG(FATAL) << "Unimplemented compare type " << in_type;
3079 }
3080}
3081
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003082void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003084 switch (instruction->InputAt(0)->GetType()) {
3085 default:
3086 case Primitive::kPrimLong:
3087 locations->SetInAt(0, Location::RequiresRegister());
3088 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3089 break;
3090
3091 case Primitive::kPrimFloat:
3092 case Primitive::kPrimDouble:
3093 locations->SetInAt(0, Location::RequiresFpuRegister());
3094 locations->SetInAt(1, Location::RequiresFpuRegister());
3095 break;
3096 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003097 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3099 }
3100}
3101
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003102void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003103 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003104 return;
3105 }
3106
Alexey Frunze299a9392015-12-08 16:08:02 -08003107 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003108 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003109 switch (type) {
3110 default:
3111 // Integer case.
3112 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3113 return;
3114 case Primitive::kPrimLong:
3115 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3116 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08003117 case Primitive::kPrimFloat:
3118 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003119 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3120 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003121 }
3122}
3123
Alexey Frunzec857c742015-09-23 15:12:39 -07003124void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3125 DCHECK(instruction->IsDiv() || instruction->IsRem());
3126 Primitive::Type type = instruction->GetResultType();
3127
3128 LocationSummary* locations = instruction->GetLocations();
3129 Location second = locations->InAt(1);
3130 DCHECK(second.IsConstant());
3131
3132 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3133 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3134 int64_t imm = Int64FromConstant(second.GetConstant());
3135 DCHECK(imm == 1 || imm == -1);
3136
3137 if (instruction->IsRem()) {
3138 __ Move(out, ZERO);
3139 } else {
3140 if (imm == -1) {
3141 if (type == Primitive::kPrimInt) {
3142 __ Subu(out, ZERO, dividend);
3143 } else {
3144 DCHECK_EQ(type, Primitive::kPrimLong);
3145 __ Dsubu(out, ZERO, dividend);
3146 }
3147 } else if (out != dividend) {
3148 __ Move(out, dividend);
3149 }
3150 }
3151}
3152
3153void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3154 DCHECK(instruction->IsDiv() || instruction->IsRem());
3155 Primitive::Type type = instruction->GetResultType();
3156
3157 LocationSummary* locations = instruction->GetLocations();
3158 Location second = locations->InAt(1);
3159 DCHECK(second.IsConstant());
3160
3161 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3162 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3163 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003164 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003165 int ctz_imm = CTZ(abs_imm);
3166
3167 if (instruction->IsDiv()) {
3168 if (type == Primitive::kPrimInt) {
3169 if (ctz_imm == 1) {
3170 // Fast path for division by +/-2, which is very common.
3171 __ Srl(TMP, dividend, 31);
3172 } else {
3173 __ Sra(TMP, dividend, 31);
3174 __ Srl(TMP, TMP, 32 - ctz_imm);
3175 }
3176 __ Addu(out, dividend, TMP);
3177 __ Sra(out, out, ctz_imm);
3178 if (imm < 0) {
3179 __ Subu(out, ZERO, out);
3180 }
3181 } else {
3182 DCHECK_EQ(type, Primitive::kPrimLong);
3183 if (ctz_imm == 1) {
3184 // Fast path for division by +/-2, which is very common.
3185 __ Dsrl32(TMP, dividend, 31);
3186 } else {
3187 __ Dsra32(TMP, dividend, 31);
3188 if (ctz_imm > 32) {
3189 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3190 } else {
3191 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3192 }
3193 }
3194 __ Daddu(out, dividend, TMP);
3195 if (ctz_imm < 32) {
3196 __ Dsra(out, out, ctz_imm);
3197 } else {
3198 __ Dsra32(out, out, ctz_imm - 32);
3199 }
3200 if (imm < 0) {
3201 __ Dsubu(out, ZERO, out);
3202 }
3203 }
3204 } else {
3205 if (type == Primitive::kPrimInt) {
3206 if (ctz_imm == 1) {
3207 // Fast path for modulo +/-2, which is very common.
3208 __ Sra(TMP, dividend, 31);
3209 __ Subu(out, dividend, TMP);
3210 __ Andi(out, out, 1);
3211 __ Addu(out, out, TMP);
3212 } else {
3213 __ Sra(TMP, dividend, 31);
3214 __ Srl(TMP, TMP, 32 - ctz_imm);
3215 __ Addu(out, dividend, TMP);
3216 if (IsUint<16>(abs_imm - 1)) {
3217 __ Andi(out, out, abs_imm - 1);
3218 } else {
3219 __ Sll(out, out, 32 - ctz_imm);
3220 __ Srl(out, out, 32 - ctz_imm);
3221 }
3222 __ Subu(out, out, TMP);
3223 }
3224 } else {
3225 DCHECK_EQ(type, Primitive::kPrimLong);
3226 if (ctz_imm == 1) {
3227 // Fast path for modulo +/-2, which is very common.
3228 __ Dsra32(TMP, dividend, 31);
3229 __ Dsubu(out, dividend, TMP);
3230 __ Andi(out, out, 1);
3231 __ Daddu(out, out, TMP);
3232 } else {
3233 __ Dsra32(TMP, dividend, 31);
3234 if (ctz_imm > 32) {
3235 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3236 } else {
3237 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3238 }
3239 __ Daddu(out, dividend, TMP);
3240 if (IsUint<16>(abs_imm - 1)) {
3241 __ Andi(out, out, abs_imm - 1);
3242 } else {
3243 if (ctz_imm > 32) {
3244 __ Dsll(out, out, 64 - ctz_imm);
3245 __ Dsrl(out, out, 64 - ctz_imm);
3246 } else {
3247 __ Dsll32(out, out, 32 - ctz_imm);
3248 __ Dsrl32(out, out, 32 - ctz_imm);
3249 }
3250 }
3251 __ Dsubu(out, out, TMP);
3252 }
3253 }
3254 }
3255}
3256
3257void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3258 DCHECK(instruction->IsDiv() || instruction->IsRem());
3259
3260 LocationSummary* locations = instruction->GetLocations();
3261 Location second = locations->InAt(1);
3262 DCHECK(second.IsConstant());
3263
3264 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3265 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3266 int64_t imm = Int64FromConstant(second.GetConstant());
3267
3268 Primitive::Type type = instruction->GetResultType();
3269 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3270
3271 int64_t magic;
3272 int shift;
3273 CalculateMagicAndShiftForDivRem(imm,
3274 (type == Primitive::kPrimLong),
3275 &magic,
3276 &shift);
3277
3278 if (type == Primitive::kPrimInt) {
3279 __ LoadConst32(TMP, magic);
3280 __ MuhR6(TMP, dividend, TMP);
3281
3282 if (imm > 0 && magic < 0) {
3283 __ Addu(TMP, TMP, dividend);
3284 } else if (imm < 0 && magic > 0) {
3285 __ Subu(TMP, TMP, dividend);
3286 }
3287
3288 if (shift != 0) {
3289 __ Sra(TMP, TMP, shift);
3290 }
3291
3292 if (instruction->IsDiv()) {
3293 __ Sra(out, TMP, 31);
3294 __ Subu(out, TMP, out);
3295 } else {
3296 __ Sra(AT, TMP, 31);
3297 __ Subu(AT, TMP, AT);
3298 __ LoadConst32(TMP, imm);
3299 __ MulR6(TMP, AT, TMP);
3300 __ Subu(out, dividend, TMP);
3301 }
3302 } else {
3303 __ LoadConst64(TMP, magic);
3304 __ Dmuh(TMP, dividend, TMP);
3305
3306 if (imm > 0 && magic < 0) {
3307 __ Daddu(TMP, TMP, dividend);
3308 } else if (imm < 0 && magic > 0) {
3309 __ Dsubu(TMP, TMP, dividend);
3310 }
3311
3312 if (shift >= 32) {
3313 __ Dsra32(TMP, TMP, shift - 32);
3314 } else if (shift > 0) {
3315 __ Dsra(TMP, TMP, shift);
3316 }
3317
3318 if (instruction->IsDiv()) {
3319 __ Dsra32(out, TMP, 31);
3320 __ Dsubu(out, TMP, out);
3321 } else {
3322 __ Dsra32(AT, TMP, 31);
3323 __ Dsubu(AT, TMP, AT);
3324 __ LoadConst64(TMP, imm);
3325 __ Dmul(TMP, AT, TMP);
3326 __ Dsubu(out, dividend, TMP);
3327 }
3328 }
3329}
3330
3331void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3332 DCHECK(instruction->IsDiv() || instruction->IsRem());
3333 Primitive::Type type = instruction->GetResultType();
3334 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3335
3336 LocationSummary* locations = instruction->GetLocations();
3337 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3338 Location second = locations->InAt(1);
3339
3340 if (second.IsConstant()) {
3341 int64_t imm = Int64FromConstant(second.GetConstant());
3342 if (imm == 0) {
3343 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3344 } else if (imm == 1 || imm == -1) {
3345 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003346 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003347 DivRemByPowerOfTwo(instruction);
3348 } else {
3349 DCHECK(imm <= -2 || imm >= 2);
3350 GenerateDivRemWithAnyConstant(instruction);
3351 }
3352 } else {
3353 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3354 GpuRegister divisor = second.AsRegister<GpuRegister>();
3355 if (instruction->IsDiv()) {
3356 if (type == Primitive::kPrimInt)
3357 __ DivR6(out, dividend, divisor);
3358 else
3359 __ Ddiv(out, dividend, divisor);
3360 } else {
3361 if (type == Primitive::kPrimInt)
3362 __ ModR6(out, dividend, divisor);
3363 else
3364 __ Dmod(out, dividend, divisor);
3365 }
3366 }
3367}
3368
Alexey Frunze4dda3372015-06-01 18:31:49 -07003369void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3370 LocationSummary* locations =
3371 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3372 switch (div->GetResultType()) {
3373 case Primitive::kPrimInt:
3374 case Primitive::kPrimLong:
3375 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003376 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3378 break;
3379
3380 case Primitive::kPrimFloat:
3381 case Primitive::kPrimDouble:
3382 locations->SetInAt(0, Location::RequiresFpuRegister());
3383 locations->SetInAt(1, Location::RequiresFpuRegister());
3384 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3385 break;
3386
3387 default:
3388 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3389 }
3390}
3391
3392void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3393 Primitive::Type type = instruction->GetType();
3394 LocationSummary* locations = instruction->GetLocations();
3395
3396 switch (type) {
3397 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003398 case Primitive::kPrimLong:
3399 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003400 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003401 case Primitive::kPrimFloat:
3402 case Primitive::kPrimDouble: {
3403 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3404 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3405 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3406 if (type == Primitive::kPrimFloat)
3407 __ DivS(dst, lhs, rhs);
3408 else
3409 __ DivD(dst, lhs, rhs);
3410 break;
3411 }
3412 default:
3413 LOG(FATAL) << "Unexpected div type " << type;
3414 }
3415}
3416
3417void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003418 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003419 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003420}
3421
3422void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3423 SlowPathCodeMIPS64* slow_path =
3424 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3425 codegen_->AddSlowPath(slow_path);
3426 Location value = instruction->GetLocations()->InAt(0);
3427
3428 Primitive::Type type = instruction->GetType();
3429
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003430 if (!Primitive::IsIntegralType(type)) {
3431 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003432 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003433 }
3434
3435 if (value.IsConstant()) {
3436 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3437 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003438 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003439 } else {
3440 // A division by a non-null constant is valid. We don't need to perform
3441 // any check, so simply fall through.
3442 }
3443 } else {
3444 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3445 }
3446}
3447
3448void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3449 LocationSummary* locations =
3450 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3451 locations->SetOut(Location::ConstantLocation(constant));
3452}
3453
3454void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3455 // Will be generated at use site.
3456}
3457
3458void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3459 exit->SetLocations(nullptr);
3460}
3461
3462void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3463}
3464
3465void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3466 LocationSummary* locations =
3467 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3468 locations->SetOut(Location::ConstantLocation(constant));
3469}
3470
3471void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3472 // Will be generated at use site.
3473}
3474
David Brazdilfc6a86a2015-06-26 10:33:45 +00003475void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003476 DCHECK(!successor->IsExitBlock());
3477 HBasicBlock* block = got->GetBlock();
3478 HInstruction* previous = got->GetPrevious();
3479 HLoopInformation* info = block->GetLoopInformation();
3480
3481 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3482 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3483 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3484 return;
3485 }
3486 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3487 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3488 }
3489 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003490 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003491 }
3492}
3493
David Brazdilfc6a86a2015-06-26 10:33:45 +00003494void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3495 got->SetLocations(nullptr);
3496}
3497
3498void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3499 HandleGoto(got, got->GetSuccessor());
3500}
3501
3502void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3503 try_boundary->SetLocations(nullptr);
3504}
3505
3506void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3507 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3508 if (!successor->IsExitBlock()) {
3509 HandleGoto(try_boundary, successor);
3510 }
3511}
3512
Alexey Frunze299a9392015-12-08 16:08:02 -08003513void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3514 bool is64bit,
3515 LocationSummary* locations) {
3516 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3517 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3518 Location rhs_location = locations->InAt(1);
3519 GpuRegister rhs_reg = ZERO;
3520 int64_t rhs_imm = 0;
3521 bool use_imm = rhs_location.IsConstant();
3522 if (use_imm) {
3523 if (is64bit) {
3524 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3525 } else {
3526 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3527 }
3528 } else {
3529 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3530 }
3531 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3532
3533 switch (cond) {
3534 case kCondEQ:
3535 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003536 if (use_imm && IsInt<16>(-rhs_imm)) {
3537 if (rhs_imm == 0) {
3538 if (cond == kCondEQ) {
3539 __ Sltiu(dst, lhs, 1);
3540 } else {
3541 __ Sltu(dst, ZERO, lhs);
3542 }
3543 } else {
3544 if (is64bit) {
3545 __ Daddiu(dst, lhs, -rhs_imm);
3546 } else {
3547 __ Addiu(dst, lhs, -rhs_imm);
3548 }
3549 if (cond == kCondEQ) {
3550 __ Sltiu(dst, dst, 1);
3551 } else {
3552 __ Sltu(dst, ZERO, dst);
3553 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003554 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003555 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003556 if (use_imm && IsUint<16>(rhs_imm)) {
3557 __ Xori(dst, lhs, rhs_imm);
3558 } else {
3559 if (use_imm) {
3560 rhs_reg = TMP;
3561 __ LoadConst64(rhs_reg, rhs_imm);
3562 }
3563 __ Xor(dst, lhs, rhs_reg);
3564 }
3565 if (cond == kCondEQ) {
3566 __ Sltiu(dst, dst, 1);
3567 } else {
3568 __ Sltu(dst, ZERO, dst);
3569 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003570 }
3571 break;
3572
3573 case kCondLT:
3574 case kCondGE:
3575 if (use_imm && IsInt<16>(rhs_imm)) {
3576 __ Slti(dst, lhs, rhs_imm);
3577 } else {
3578 if (use_imm) {
3579 rhs_reg = TMP;
3580 __ LoadConst64(rhs_reg, rhs_imm);
3581 }
3582 __ Slt(dst, lhs, rhs_reg);
3583 }
3584 if (cond == kCondGE) {
3585 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3586 // only the slt instruction but no sge.
3587 __ Xori(dst, dst, 1);
3588 }
3589 break;
3590
3591 case kCondLE:
3592 case kCondGT:
3593 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3594 // Simulate lhs <= rhs via lhs < rhs + 1.
3595 __ Slti(dst, lhs, rhs_imm_plus_one);
3596 if (cond == kCondGT) {
3597 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3598 // only the slti instruction but no sgti.
3599 __ Xori(dst, dst, 1);
3600 }
3601 } else {
3602 if (use_imm) {
3603 rhs_reg = TMP;
3604 __ LoadConst64(rhs_reg, rhs_imm);
3605 }
3606 __ Slt(dst, rhs_reg, lhs);
3607 if (cond == kCondLE) {
3608 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3609 // only the slt instruction but no sle.
3610 __ Xori(dst, dst, 1);
3611 }
3612 }
3613 break;
3614
3615 case kCondB:
3616 case kCondAE:
3617 if (use_imm && IsInt<16>(rhs_imm)) {
3618 // Sltiu sign-extends its 16-bit immediate operand before
3619 // the comparison and thus lets us compare directly with
3620 // unsigned values in the ranges [0, 0x7fff] and
3621 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3622 __ Sltiu(dst, lhs, rhs_imm);
3623 } else {
3624 if (use_imm) {
3625 rhs_reg = TMP;
3626 __ LoadConst64(rhs_reg, rhs_imm);
3627 }
3628 __ Sltu(dst, lhs, rhs_reg);
3629 }
3630 if (cond == kCondAE) {
3631 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3632 // only the sltu instruction but no sgeu.
3633 __ Xori(dst, dst, 1);
3634 }
3635 break;
3636
3637 case kCondBE:
3638 case kCondA:
3639 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3640 // Simulate lhs <= rhs via lhs < rhs + 1.
3641 // Note that this only works if rhs + 1 does not overflow
3642 // to 0, hence the check above.
3643 // Sltiu sign-extends its 16-bit immediate operand before
3644 // the comparison and thus lets us compare directly with
3645 // unsigned values in the ranges [0, 0x7fff] and
3646 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3647 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3648 if (cond == kCondA) {
3649 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3650 // only the sltiu instruction but no sgtiu.
3651 __ Xori(dst, dst, 1);
3652 }
3653 } else {
3654 if (use_imm) {
3655 rhs_reg = TMP;
3656 __ LoadConst64(rhs_reg, rhs_imm);
3657 }
3658 __ Sltu(dst, rhs_reg, lhs);
3659 if (cond == kCondBE) {
3660 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3661 // only the sltu instruction but no sleu.
3662 __ Xori(dst, dst, 1);
3663 }
3664 }
3665 break;
3666 }
3667}
3668
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003669bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3670 bool is64bit,
3671 LocationSummary* input_locations,
3672 GpuRegister dst) {
3673 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3674 Location rhs_location = input_locations->InAt(1);
3675 GpuRegister rhs_reg = ZERO;
3676 int64_t rhs_imm = 0;
3677 bool use_imm = rhs_location.IsConstant();
3678 if (use_imm) {
3679 if (is64bit) {
3680 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3681 } else {
3682 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3683 }
3684 } else {
3685 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3686 }
3687 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3688
3689 switch (cond) {
3690 case kCondEQ:
3691 case kCondNE:
3692 if (use_imm && IsInt<16>(-rhs_imm)) {
3693 if (is64bit) {
3694 __ Daddiu(dst, lhs, -rhs_imm);
3695 } else {
3696 __ Addiu(dst, lhs, -rhs_imm);
3697 }
3698 } else if (use_imm && IsUint<16>(rhs_imm)) {
3699 __ Xori(dst, lhs, rhs_imm);
3700 } else {
3701 if (use_imm) {
3702 rhs_reg = TMP;
3703 __ LoadConst64(rhs_reg, rhs_imm);
3704 }
3705 __ Xor(dst, lhs, rhs_reg);
3706 }
3707 return (cond == kCondEQ);
3708
3709 case kCondLT:
3710 case kCondGE:
3711 if (use_imm && IsInt<16>(rhs_imm)) {
3712 __ Slti(dst, lhs, rhs_imm);
3713 } else {
3714 if (use_imm) {
3715 rhs_reg = TMP;
3716 __ LoadConst64(rhs_reg, rhs_imm);
3717 }
3718 __ Slt(dst, lhs, rhs_reg);
3719 }
3720 return (cond == kCondGE);
3721
3722 case kCondLE:
3723 case kCondGT:
3724 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3725 // Simulate lhs <= rhs via lhs < rhs + 1.
3726 __ Slti(dst, lhs, rhs_imm_plus_one);
3727 return (cond == kCondGT);
3728 } else {
3729 if (use_imm) {
3730 rhs_reg = TMP;
3731 __ LoadConst64(rhs_reg, rhs_imm);
3732 }
3733 __ Slt(dst, rhs_reg, lhs);
3734 return (cond == kCondLE);
3735 }
3736
3737 case kCondB:
3738 case kCondAE:
3739 if (use_imm && IsInt<16>(rhs_imm)) {
3740 // Sltiu sign-extends its 16-bit immediate operand before
3741 // the comparison and thus lets us compare directly with
3742 // unsigned values in the ranges [0, 0x7fff] and
3743 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3744 __ Sltiu(dst, lhs, rhs_imm);
3745 } else {
3746 if (use_imm) {
3747 rhs_reg = TMP;
3748 __ LoadConst64(rhs_reg, rhs_imm);
3749 }
3750 __ Sltu(dst, lhs, rhs_reg);
3751 }
3752 return (cond == kCondAE);
3753
3754 case kCondBE:
3755 case kCondA:
3756 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3757 // Simulate lhs <= rhs via lhs < rhs + 1.
3758 // Note that this only works if rhs + 1 does not overflow
3759 // to 0, hence the check above.
3760 // Sltiu sign-extends its 16-bit immediate operand before
3761 // the comparison and thus lets us compare directly with
3762 // unsigned values in the ranges [0, 0x7fff] and
3763 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3764 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3765 return (cond == kCondA);
3766 } else {
3767 if (use_imm) {
3768 rhs_reg = TMP;
3769 __ LoadConst64(rhs_reg, rhs_imm);
3770 }
3771 __ Sltu(dst, rhs_reg, lhs);
3772 return (cond == kCondBE);
3773 }
3774 }
3775}
3776
Alexey Frunze299a9392015-12-08 16:08:02 -08003777void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3778 bool is64bit,
3779 LocationSummary* locations,
3780 Mips64Label* label) {
3781 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3782 Location rhs_location = locations->InAt(1);
3783 GpuRegister rhs_reg = ZERO;
3784 int64_t rhs_imm = 0;
3785 bool use_imm = rhs_location.IsConstant();
3786 if (use_imm) {
3787 if (is64bit) {
3788 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3789 } else {
3790 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3791 }
3792 } else {
3793 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3794 }
3795
3796 if (use_imm && rhs_imm == 0) {
3797 switch (cond) {
3798 case kCondEQ:
3799 case kCondBE: // <= 0 if zero
3800 __ Beqzc(lhs, label);
3801 break;
3802 case kCondNE:
3803 case kCondA: // > 0 if non-zero
3804 __ Bnezc(lhs, label);
3805 break;
3806 case kCondLT:
3807 __ Bltzc(lhs, label);
3808 break;
3809 case kCondGE:
3810 __ Bgezc(lhs, label);
3811 break;
3812 case kCondLE:
3813 __ Blezc(lhs, label);
3814 break;
3815 case kCondGT:
3816 __ Bgtzc(lhs, label);
3817 break;
3818 case kCondB: // always false
3819 break;
3820 case kCondAE: // always true
3821 __ Bc(label);
3822 break;
3823 }
3824 } else {
3825 if (use_imm) {
3826 rhs_reg = TMP;
3827 __ LoadConst64(rhs_reg, rhs_imm);
3828 }
3829 switch (cond) {
3830 case kCondEQ:
3831 __ Beqc(lhs, rhs_reg, label);
3832 break;
3833 case kCondNE:
3834 __ Bnec(lhs, rhs_reg, label);
3835 break;
3836 case kCondLT:
3837 __ Bltc(lhs, rhs_reg, label);
3838 break;
3839 case kCondGE:
3840 __ Bgec(lhs, rhs_reg, label);
3841 break;
3842 case kCondLE:
3843 __ Bgec(rhs_reg, lhs, label);
3844 break;
3845 case kCondGT:
3846 __ Bltc(rhs_reg, lhs, label);
3847 break;
3848 case kCondB:
3849 __ Bltuc(lhs, rhs_reg, label);
3850 break;
3851 case kCondAE:
3852 __ Bgeuc(lhs, rhs_reg, label);
3853 break;
3854 case kCondBE:
3855 __ Bgeuc(rhs_reg, lhs, label);
3856 break;
3857 case kCondA:
3858 __ Bltuc(rhs_reg, lhs, label);
3859 break;
3860 }
3861 }
3862}
3863
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003864void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3865 bool gt_bias,
3866 Primitive::Type type,
3867 LocationSummary* locations) {
3868 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3869 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3870 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3871 if (type == Primitive::kPrimFloat) {
3872 switch (cond) {
3873 case kCondEQ:
3874 __ CmpEqS(FTMP, lhs, rhs);
3875 __ Mfc1(dst, FTMP);
3876 __ Andi(dst, dst, 1);
3877 break;
3878 case kCondNE:
3879 __ CmpEqS(FTMP, lhs, rhs);
3880 __ Mfc1(dst, FTMP);
3881 __ Addiu(dst, dst, 1);
3882 break;
3883 case kCondLT:
3884 if (gt_bias) {
3885 __ CmpLtS(FTMP, lhs, rhs);
3886 } else {
3887 __ CmpUltS(FTMP, lhs, rhs);
3888 }
3889 __ Mfc1(dst, FTMP);
3890 __ Andi(dst, dst, 1);
3891 break;
3892 case kCondLE:
3893 if (gt_bias) {
3894 __ CmpLeS(FTMP, lhs, rhs);
3895 } else {
3896 __ CmpUleS(FTMP, lhs, rhs);
3897 }
3898 __ Mfc1(dst, FTMP);
3899 __ Andi(dst, dst, 1);
3900 break;
3901 case kCondGT:
3902 if (gt_bias) {
3903 __ CmpUltS(FTMP, rhs, lhs);
3904 } else {
3905 __ CmpLtS(FTMP, rhs, lhs);
3906 }
3907 __ Mfc1(dst, FTMP);
3908 __ Andi(dst, dst, 1);
3909 break;
3910 case kCondGE:
3911 if (gt_bias) {
3912 __ CmpUleS(FTMP, rhs, lhs);
3913 } else {
3914 __ CmpLeS(FTMP, rhs, lhs);
3915 }
3916 __ Mfc1(dst, FTMP);
3917 __ Andi(dst, dst, 1);
3918 break;
3919 default:
3920 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3921 UNREACHABLE();
3922 }
3923 } else {
3924 DCHECK_EQ(type, Primitive::kPrimDouble);
3925 switch (cond) {
3926 case kCondEQ:
3927 __ CmpEqD(FTMP, lhs, rhs);
3928 __ Mfc1(dst, FTMP);
3929 __ Andi(dst, dst, 1);
3930 break;
3931 case kCondNE:
3932 __ CmpEqD(FTMP, lhs, rhs);
3933 __ Mfc1(dst, FTMP);
3934 __ Addiu(dst, dst, 1);
3935 break;
3936 case kCondLT:
3937 if (gt_bias) {
3938 __ CmpLtD(FTMP, lhs, rhs);
3939 } else {
3940 __ CmpUltD(FTMP, lhs, rhs);
3941 }
3942 __ Mfc1(dst, FTMP);
3943 __ Andi(dst, dst, 1);
3944 break;
3945 case kCondLE:
3946 if (gt_bias) {
3947 __ CmpLeD(FTMP, lhs, rhs);
3948 } else {
3949 __ CmpUleD(FTMP, lhs, rhs);
3950 }
3951 __ Mfc1(dst, FTMP);
3952 __ Andi(dst, dst, 1);
3953 break;
3954 case kCondGT:
3955 if (gt_bias) {
3956 __ CmpUltD(FTMP, rhs, lhs);
3957 } else {
3958 __ CmpLtD(FTMP, rhs, lhs);
3959 }
3960 __ Mfc1(dst, FTMP);
3961 __ Andi(dst, dst, 1);
3962 break;
3963 case kCondGE:
3964 if (gt_bias) {
3965 __ CmpUleD(FTMP, rhs, lhs);
3966 } else {
3967 __ CmpLeD(FTMP, rhs, lhs);
3968 }
3969 __ Mfc1(dst, FTMP);
3970 __ Andi(dst, dst, 1);
3971 break;
3972 default:
3973 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3974 UNREACHABLE();
3975 }
3976 }
3977}
3978
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003979bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
3980 bool gt_bias,
3981 Primitive::Type type,
3982 LocationSummary* input_locations,
3983 FpuRegister dst) {
3984 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
3985 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
3986 if (type == Primitive::kPrimFloat) {
3987 switch (cond) {
3988 case kCondEQ:
3989 __ CmpEqS(dst, lhs, rhs);
3990 return false;
3991 case kCondNE:
3992 __ CmpEqS(dst, lhs, rhs);
3993 return true;
3994 case kCondLT:
3995 if (gt_bias) {
3996 __ CmpLtS(dst, lhs, rhs);
3997 } else {
3998 __ CmpUltS(dst, lhs, rhs);
3999 }
4000 return false;
4001 case kCondLE:
4002 if (gt_bias) {
4003 __ CmpLeS(dst, lhs, rhs);
4004 } else {
4005 __ CmpUleS(dst, lhs, rhs);
4006 }
4007 return false;
4008 case kCondGT:
4009 if (gt_bias) {
4010 __ CmpUltS(dst, rhs, lhs);
4011 } else {
4012 __ CmpLtS(dst, rhs, lhs);
4013 }
4014 return false;
4015 case kCondGE:
4016 if (gt_bias) {
4017 __ CmpUleS(dst, rhs, lhs);
4018 } else {
4019 __ CmpLeS(dst, rhs, lhs);
4020 }
4021 return false;
4022 default:
4023 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4024 UNREACHABLE();
4025 }
4026 } else {
4027 DCHECK_EQ(type, Primitive::kPrimDouble);
4028 switch (cond) {
4029 case kCondEQ:
4030 __ CmpEqD(dst, lhs, rhs);
4031 return false;
4032 case kCondNE:
4033 __ CmpEqD(dst, lhs, rhs);
4034 return true;
4035 case kCondLT:
4036 if (gt_bias) {
4037 __ CmpLtD(dst, lhs, rhs);
4038 } else {
4039 __ CmpUltD(dst, lhs, rhs);
4040 }
4041 return false;
4042 case kCondLE:
4043 if (gt_bias) {
4044 __ CmpLeD(dst, lhs, rhs);
4045 } else {
4046 __ CmpUleD(dst, lhs, rhs);
4047 }
4048 return false;
4049 case kCondGT:
4050 if (gt_bias) {
4051 __ CmpUltD(dst, rhs, lhs);
4052 } else {
4053 __ CmpLtD(dst, rhs, lhs);
4054 }
4055 return false;
4056 case kCondGE:
4057 if (gt_bias) {
4058 __ CmpUleD(dst, rhs, lhs);
4059 } else {
4060 __ CmpLeD(dst, rhs, lhs);
4061 }
4062 return false;
4063 default:
4064 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4065 UNREACHABLE();
4066 }
4067 }
4068}
4069
Alexey Frunze299a9392015-12-08 16:08:02 -08004070void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4071 bool gt_bias,
4072 Primitive::Type type,
4073 LocationSummary* locations,
4074 Mips64Label* label) {
4075 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4076 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
4077 if (type == Primitive::kPrimFloat) {
4078 switch (cond) {
4079 case kCondEQ:
4080 __ CmpEqS(FTMP, lhs, rhs);
4081 __ Bc1nez(FTMP, label);
4082 break;
4083 case kCondNE:
4084 __ CmpEqS(FTMP, lhs, rhs);
4085 __ Bc1eqz(FTMP, label);
4086 break;
4087 case kCondLT:
4088 if (gt_bias) {
4089 __ CmpLtS(FTMP, lhs, rhs);
4090 } else {
4091 __ CmpUltS(FTMP, lhs, rhs);
4092 }
4093 __ Bc1nez(FTMP, label);
4094 break;
4095 case kCondLE:
4096 if (gt_bias) {
4097 __ CmpLeS(FTMP, lhs, rhs);
4098 } else {
4099 __ CmpUleS(FTMP, lhs, rhs);
4100 }
4101 __ Bc1nez(FTMP, label);
4102 break;
4103 case kCondGT:
4104 if (gt_bias) {
4105 __ CmpUltS(FTMP, rhs, lhs);
4106 } else {
4107 __ CmpLtS(FTMP, rhs, lhs);
4108 }
4109 __ Bc1nez(FTMP, label);
4110 break;
4111 case kCondGE:
4112 if (gt_bias) {
4113 __ CmpUleS(FTMP, rhs, lhs);
4114 } else {
4115 __ CmpLeS(FTMP, rhs, lhs);
4116 }
4117 __ Bc1nez(FTMP, label);
4118 break;
4119 default:
4120 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004121 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004122 }
4123 } else {
4124 DCHECK_EQ(type, Primitive::kPrimDouble);
4125 switch (cond) {
4126 case kCondEQ:
4127 __ CmpEqD(FTMP, lhs, rhs);
4128 __ Bc1nez(FTMP, label);
4129 break;
4130 case kCondNE:
4131 __ CmpEqD(FTMP, lhs, rhs);
4132 __ Bc1eqz(FTMP, label);
4133 break;
4134 case kCondLT:
4135 if (gt_bias) {
4136 __ CmpLtD(FTMP, lhs, rhs);
4137 } else {
4138 __ CmpUltD(FTMP, lhs, rhs);
4139 }
4140 __ Bc1nez(FTMP, label);
4141 break;
4142 case kCondLE:
4143 if (gt_bias) {
4144 __ CmpLeD(FTMP, lhs, rhs);
4145 } else {
4146 __ CmpUleD(FTMP, lhs, rhs);
4147 }
4148 __ Bc1nez(FTMP, label);
4149 break;
4150 case kCondGT:
4151 if (gt_bias) {
4152 __ CmpUltD(FTMP, rhs, lhs);
4153 } else {
4154 __ CmpLtD(FTMP, rhs, lhs);
4155 }
4156 __ Bc1nez(FTMP, label);
4157 break;
4158 case kCondGE:
4159 if (gt_bias) {
4160 __ CmpUleD(FTMP, rhs, lhs);
4161 } else {
4162 __ CmpLeD(FTMP, rhs, lhs);
4163 }
4164 __ Bc1nez(FTMP, label);
4165 break;
4166 default:
4167 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004168 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004169 }
4170 }
4171}
4172
Alexey Frunze4dda3372015-06-01 18:31:49 -07004173void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004174 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004175 Mips64Label* true_target,
4176 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004177 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004178
David Brazdil0debae72015-11-12 18:37:00 +00004179 if (true_target == nullptr && false_target == nullptr) {
4180 // Nothing to do. The code always falls through.
4181 return;
4182 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004183 // Constant condition, statically compared against "true" (integer value 1).
4184 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004185 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004186 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004187 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004188 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004189 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004190 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004191 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004192 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004193 }
David Brazdil0debae72015-11-12 18:37:00 +00004194 return;
4195 }
4196
4197 // The following code generates these patterns:
4198 // (1) true_target == nullptr && false_target != nullptr
4199 // - opposite condition true => branch to false_target
4200 // (2) true_target != nullptr && false_target == nullptr
4201 // - condition true => branch to true_target
4202 // (3) true_target != nullptr && false_target != nullptr
4203 // - condition true => branch to true_target
4204 // - branch to false_target
4205 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004206 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004207 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004208 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004209 if (true_target == nullptr) {
4210 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4211 } else {
4212 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4213 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004214 } else {
4215 // The condition instruction has not been materialized, use its inputs as
4216 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004217 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004218 Primitive::Type type = condition->InputAt(0)->GetType();
4219 LocationSummary* locations = cond->GetLocations();
4220 IfCondition if_cond = condition->GetCondition();
4221 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004222
David Brazdil0debae72015-11-12 18:37:00 +00004223 if (true_target == nullptr) {
4224 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004225 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004226 }
4227
Alexey Frunze299a9392015-12-08 16:08:02 -08004228 switch (type) {
4229 default:
4230 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4231 break;
4232 case Primitive::kPrimLong:
4233 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4234 break;
4235 case Primitive::kPrimFloat:
4236 case Primitive::kPrimDouble:
4237 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4238 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004239 }
4240 }
David Brazdil0debae72015-11-12 18:37:00 +00004241
4242 // If neither branch falls through (case 3), the conditional branch to `true_target`
4243 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4244 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004245 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004246 }
4247}
4248
4249void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
4250 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004251 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004252 locations->SetInAt(0, Location::RequiresRegister());
4253 }
4254}
4255
4256void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004257 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4258 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004259 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004260 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004261 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004262 nullptr : codegen_->GetLabelOf(false_successor);
4263 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004264}
4265
4266void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
4267 LocationSummary* locations = new (GetGraph()->GetArena())
4268 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004269 InvokeRuntimeCallingConvention calling_convention;
4270 RegisterSet caller_saves = RegisterSet::Empty();
4271 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4272 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004273 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004274 locations->SetInAt(0, Location::RequiresRegister());
4275 }
4276}
4277
4278void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004279 SlowPathCodeMIPS64* slow_path =
4280 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004281 GenerateTestAndBranch(deoptimize,
4282 /* condition_input_index */ 0,
4283 slow_path->GetEntryLabel(),
4284 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004285}
4286
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004287// This function returns true if a conditional move can be generated for HSelect.
4288// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4289// branches and regular moves.
4290//
4291// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4292//
4293// While determining feasibility of a conditional move and setting inputs/outputs
4294// are two distinct tasks, this function does both because they share quite a bit
4295// of common logic.
4296static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4297 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4298 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4299 HCondition* condition = cond->AsCondition();
4300
4301 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4302 Primitive::Type dst_type = select->GetType();
4303
4304 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4305 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4306 bool is_true_value_zero_constant =
4307 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4308 bool is_false_value_zero_constant =
4309 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4310
4311 bool can_move_conditionally = false;
4312 bool use_const_for_false_in = false;
4313 bool use_const_for_true_in = false;
4314
4315 if (!cond->IsConstant()) {
4316 if (!Primitive::IsFloatingPointType(cond_type)) {
4317 if (!Primitive::IsFloatingPointType(dst_type)) {
4318 // Moving int/long on int/long condition.
4319 if (is_true_value_zero_constant) {
4320 // seleqz out_reg, false_reg, cond_reg
4321 can_move_conditionally = true;
4322 use_const_for_true_in = true;
4323 } else if (is_false_value_zero_constant) {
4324 // selnez out_reg, true_reg, cond_reg
4325 can_move_conditionally = true;
4326 use_const_for_false_in = true;
4327 } else if (materialized) {
4328 // Not materializing unmaterialized int conditions
4329 // to keep the instruction count low.
4330 // selnez AT, true_reg, cond_reg
4331 // seleqz TMP, false_reg, cond_reg
4332 // or out_reg, AT, TMP
4333 can_move_conditionally = true;
4334 }
4335 } else {
4336 // Moving float/double on int/long condition.
4337 if (materialized) {
4338 // Not materializing unmaterialized int conditions
4339 // to keep the instruction count low.
4340 can_move_conditionally = true;
4341 if (is_true_value_zero_constant) {
4342 // sltu TMP, ZERO, cond_reg
4343 // mtc1 TMP, temp_cond_reg
4344 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4345 use_const_for_true_in = true;
4346 } else if (is_false_value_zero_constant) {
4347 // sltu TMP, ZERO, cond_reg
4348 // mtc1 TMP, temp_cond_reg
4349 // selnez.fmt out_reg, true_reg, temp_cond_reg
4350 use_const_for_false_in = true;
4351 } else {
4352 // sltu TMP, ZERO, cond_reg
4353 // mtc1 TMP, temp_cond_reg
4354 // sel.fmt temp_cond_reg, false_reg, true_reg
4355 // mov.fmt out_reg, temp_cond_reg
4356 }
4357 }
4358 }
4359 } else {
4360 if (!Primitive::IsFloatingPointType(dst_type)) {
4361 // Moving int/long on float/double condition.
4362 can_move_conditionally = true;
4363 if (is_true_value_zero_constant) {
4364 // mfc1 TMP, temp_cond_reg
4365 // seleqz out_reg, false_reg, TMP
4366 use_const_for_true_in = true;
4367 } else if (is_false_value_zero_constant) {
4368 // mfc1 TMP, temp_cond_reg
4369 // selnez out_reg, true_reg, TMP
4370 use_const_for_false_in = true;
4371 } else {
4372 // mfc1 TMP, temp_cond_reg
4373 // selnez AT, true_reg, TMP
4374 // seleqz TMP, false_reg, TMP
4375 // or out_reg, AT, TMP
4376 }
4377 } else {
4378 // Moving float/double on float/double condition.
4379 can_move_conditionally = true;
4380 if (is_true_value_zero_constant) {
4381 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4382 use_const_for_true_in = true;
4383 } else if (is_false_value_zero_constant) {
4384 // selnez.fmt out_reg, true_reg, temp_cond_reg
4385 use_const_for_false_in = true;
4386 } else {
4387 // sel.fmt temp_cond_reg, false_reg, true_reg
4388 // mov.fmt out_reg, temp_cond_reg
4389 }
4390 }
4391 }
4392 }
4393
4394 if (can_move_conditionally) {
4395 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4396 } else {
4397 DCHECK(!use_const_for_false_in);
4398 DCHECK(!use_const_for_true_in);
4399 }
4400
4401 if (locations_to_set != nullptr) {
4402 if (use_const_for_false_in) {
4403 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4404 } else {
4405 locations_to_set->SetInAt(0,
4406 Primitive::IsFloatingPointType(dst_type)
4407 ? Location::RequiresFpuRegister()
4408 : Location::RequiresRegister());
4409 }
4410 if (use_const_for_true_in) {
4411 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4412 } else {
4413 locations_to_set->SetInAt(1,
4414 Primitive::IsFloatingPointType(dst_type)
4415 ? Location::RequiresFpuRegister()
4416 : Location::RequiresRegister());
4417 }
4418 if (materialized) {
4419 locations_to_set->SetInAt(2, Location::RequiresRegister());
4420 }
4421
4422 if (can_move_conditionally) {
4423 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4424 ? Location::RequiresFpuRegister()
4425 : Location::RequiresRegister());
4426 } else {
4427 locations_to_set->SetOut(Location::SameAsFirstInput());
4428 }
4429 }
4430
4431 return can_move_conditionally;
4432}
4433
4434
4435void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4436 LocationSummary* locations = select->GetLocations();
4437 Location dst = locations->Out();
4438 Location false_src = locations->InAt(0);
4439 Location true_src = locations->InAt(1);
4440 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4441 GpuRegister cond_reg = TMP;
4442 FpuRegister fcond_reg = FTMP;
4443 Primitive::Type cond_type = Primitive::kPrimInt;
4444 bool cond_inverted = false;
4445 Primitive::Type dst_type = select->GetType();
4446
4447 if (IsBooleanValueOrMaterializedCondition(cond)) {
4448 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4449 } else {
4450 HCondition* condition = cond->AsCondition();
4451 LocationSummary* cond_locations = cond->GetLocations();
4452 IfCondition if_cond = condition->GetCondition();
4453 cond_type = condition->InputAt(0)->GetType();
4454 switch (cond_type) {
4455 default:
4456 cond_inverted = MaterializeIntLongCompare(if_cond,
4457 /* is64bit */ false,
4458 cond_locations,
4459 cond_reg);
4460 break;
4461 case Primitive::kPrimLong:
4462 cond_inverted = MaterializeIntLongCompare(if_cond,
4463 /* is64bit */ true,
4464 cond_locations,
4465 cond_reg);
4466 break;
4467 case Primitive::kPrimFloat:
4468 case Primitive::kPrimDouble:
4469 cond_inverted = MaterializeFpCompare(if_cond,
4470 condition->IsGtBias(),
4471 cond_type,
4472 cond_locations,
4473 fcond_reg);
4474 break;
4475 }
4476 }
4477
4478 if (true_src.IsConstant()) {
4479 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4480 }
4481 if (false_src.IsConstant()) {
4482 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4483 }
4484
4485 switch (dst_type) {
4486 default:
4487 if (Primitive::IsFloatingPointType(cond_type)) {
4488 __ Mfc1(cond_reg, fcond_reg);
4489 }
4490 if (true_src.IsConstant()) {
4491 if (cond_inverted) {
4492 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4493 } else {
4494 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4495 }
4496 } else if (false_src.IsConstant()) {
4497 if (cond_inverted) {
4498 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4499 } else {
4500 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4501 }
4502 } else {
4503 DCHECK_NE(cond_reg, AT);
4504 if (cond_inverted) {
4505 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4506 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4507 } else {
4508 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4509 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4510 }
4511 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4512 }
4513 break;
4514 case Primitive::kPrimFloat: {
4515 if (!Primitive::IsFloatingPointType(cond_type)) {
4516 // sel*.fmt tests bit 0 of the condition register, account for that.
4517 __ Sltu(TMP, ZERO, cond_reg);
4518 __ Mtc1(TMP, fcond_reg);
4519 }
4520 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4521 if (true_src.IsConstant()) {
4522 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4523 if (cond_inverted) {
4524 __ SelnezS(dst_reg, src_reg, fcond_reg);
4525 } else {
4526 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4527 }
4528 } else if (false_src.IsConstant()) {
4529 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4530 if (cond_inverted) {
4531 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4532 } else {
4533 __ SelnezS(dst_reg, src_reg, fcond_reg);
4534 }
4535 } else {
4536 if (cond_inverted) {
4537 __ SelS(fcond_reg,
4538 true_src.AsFpuRegister<FpuRegister>(),
4539 false_src.AsFpuRegister<FpuRegister>());
4540 } else {
4541 __ SelS(fcond_reg,
4542 false_src.AsFpuRegister<FpuRegister>(),
4543 true_src.AsFpuRegister<FpuRegister>());
4544 }
4545 __ MovS(dst_reg, fcond_reg);
4546 }
4547 break;
4548 }
4549 case Primitive::kPrimDouble: {
4550 if (!Primitive::IsFloatingPointType(cond_type)) {
4551 // sel*.fmt tests bit 0 of the condition register, account for that.
4552 __ Sltu(TMP, ZERO, cond_reg);
4553 __ Mtc1(TMP, fcond_reg);
4554 }
4555 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4556 if (true_src.IsConstant()) {
4557 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4558 if (cond_inverted) {
4559 __ SelnezD(dst_reg, src_reg, fcond_reg);
4560 } else {
4561 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4562 }
4563 } else if (false_src.IsConstant()) {
4564 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4565 if (cond_inverted) {
4566 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4567 } else {
4568 __ SelnezD(dst_reg, src_reg, fcond_reg);
4569 }
4570 } else {
4571 if (cond_inverted) {
4572 __ SelD(fcond_reg,
4573 true_src.AsFpuRegister<FpuRegister>(),
4574 false_src.AsFpuRegister<FpuRegister>());
4575 } else {
4576 __ SelD(fcond_reg,
4577 false_src.AsFpuRegister<FpuRegister>(),
4578 true_src.AsFpuRegister<FpuRegister>());
4579 }
4580 __ MovD(dst_reg, fcond_reg);
4581 }
4582 break;
4583 }
4584 }
4585}
4586
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004587void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4588 LocationSummary* locations = new (GetGraph()->GetArena())
4589 LocationSummary(flag, LocationSummary::kNoCall);
4590 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004591}
4592
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004593void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4594 __ LoadFromOffset(kLoadWord,
4595 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4596 SP,
4597 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004598}
4599
David Brazdil74eb1b22015-12-14 11:44:01 +00004600void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
4601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004602 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004603}
4604
4605void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004606 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4607 GenConditionalMove(select);
4608 } else {
4609 LocationSummary* locations = select->GetLocations();
4610 Mips64Label false_target;
4611 GenerateTestAndBranch(select,
4612 /* condition_input_index */ 2,
4613 /* true_target */ nullptr,
4614 &false_target);
4615 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4616 __ Bind(&false_target);
4617 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004618}
4619
David Srbecky0cf44932015-12-09 14:09:59 +00004620void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4621 new (GetGraph()->GetArena()) LocationSummary(info);
4622}
4623
David Srbeckyd28f4a02016-03-14 17:14:24 +00004624void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4625 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004626}
4627
4628void CodeGeneratorMIPS64::GenerateNop() {
4629 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004630}
4631
Alexey Frunze4dda3372015-06-01 18:31:49 -07004632void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004633 const FieldInfo& field_info) {
4634 Primitive::Type field_type = field_info.GetFieldType();
4635 bool object_field_get_with_read_barrier =
4636 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
4637 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4638 instruction,
4639 object_field_get_with_read_barrier
4640 ? LocationSummary::kCallOnSlowPath
4641 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004642 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4643 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4644 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004645 locations->SetInAt(0, Location::RequiresRegister());
4646 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4647 locations->SetOut(Location::RequiresFpuRegister());
4648 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004649 // The output overlaps in the case of an object field get with
4650 // read barriers enabled: we do not want the move to overwrite the
4651 // object's location, as we need it to emit the read barrier.
4652 locations->SetOut(Location::RequiresRegister(),
4653 object_field_get_with_read_barrier
4654 ? Location::kOutputOverlap
4655 : Location::kNoOutputOverlap);
4656 }
4657 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4658 // We need a temporary register for the read barrier marking slow
4659 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004660 if (!kBakerReadBarrierThunksEnableForFields) {
4661 locations->AddTemp(Location::RequiresRegister());
4662 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004663 }
4664}
4665
4666void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4667 const FieldInfo& field_info) {
4668 Primitive::Type type = field_info.GetFieldType();
4669 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004670 Location obj_loc = locations->InAt(0);
4671 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4672 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004673 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004674 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004675 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004676 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4677
Alexey Frunze4dda3372015-06-01 18:31:49 -07004678 switch (type) {
4679 case Primitive::kPrimBoolean:
4680 load_type = kLoadUnsignedByte;
4681 break;
4682 case Primitive::kPrimByte:
4683 load_type = kLoadSignedByte;
4684 break;
4685 case Primitive::kPrimShort:
4686 load_type = kLoadSignedHalfword;
4687 break;
4688 case Primitive::kPrimChar:
4689 load_type = kLoadUnsignedHalfword;
4690 break;
4691 case Primitive::kPrimInt:
4692 case Primitive::kPrimFloat:
4693 load_type = kLoadWord;
4694 break;
4695 case Primitive::kPrimLong:
4696 case Primitive::kPrimDouble:
4697 load_type = kLoadDoubleword;
4698 break;
4699 case Primitive::kPrimNot:
4700 load_type = kLoadUnsignedWord;
4701 break;
4702 case Primitive::kPrimVoid:
4703 LOG(FATAL) << "Unreachable type " << type;
4704 UNREACHABLE();
4705 }
4706 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004707 DCHECK(dst_loc.IsRegister());
4708 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4709 if (type == Primitive::kPrimNot) {
4710 // /* HeapReference<Object> */ dst = *(obj + offset)
4711 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004712 Location temp_loc =
4713 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004714 // Note that a potential implicit null check is handled in this
4715 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4716 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4717 dst_loc,
4718 obj,
4719 offset,
4720 temp_loc,
4721 /* needs_null_check */ true);
4722 if (is_volatile) {
4723 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4724 }
4725 } else {
4726 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4727 if (is_volatile) {
4728 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4729 }
4730 // If read barriers are enabled, emit read barriers other than
4731 // Baker's using a slow path (and also unpoison the loaded
4732 // reference, if heap poisoning is enabled).
4733 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4734 }
4735 } else {
4736 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4737 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004738 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004739 DCHECK(dst_loc.IsFpuRegister());
4740 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004741 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004742 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004743
Alexey Frunze15958152017-02-09 19:08:30 -08004744 // Memory barriers, in the case of references, are handled in the
4745 // previous switch statement.
4746 if (is_volatile && (type != Primitive::kPrimNot)) {
4747 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004748 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004749}
4750
4751void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4752 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4753 LocationSummary* locations =
4754 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4755 locations->SetInAt(0, Location::RequiresRegister());
4756 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004757 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004758 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004759 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004760 }
4761}
4762
4763void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004764 const FieldInfo& field_info,
4765 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004766 Primitive::Type type = field_info.GetFieldType();
4767 LocationSummary* locations = instruction->GetLocations();
4768 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004769 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004770 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004771 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004772 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4773 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004774 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4775
Alexey Frunze4dda3372015-06-01 18:31:49 -07004776 switch (type) {
4777 case Primitive::kPrimBoolean:
4778 case Primitive::kPrimByte:
4779 store_type = kStoreByte;
4780 break;
4781 case Primitive::kPrimShort:
4782 case Primitive::kPrimChar:
4783 store_type = kStoreHalfword;
4784 break;
4785 case Primitive::kPrimInt:
4786 case Primitive::kPrimFloat:
4787 case Primitive::kPrimNot:
4788 store_type = kStoreWord;
4789 break;
4790 case Primitive::kPrimLong:
4791 case Primitive::kPrimDouble:
4792 store_type = kStoreDoubleword;
4793 break;
4794 case Primitive::kPrimVoid:
4795 LOG(FATAL) << "Unreachable type " << type;
4796 UNREACHABLE();
4797 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004798
Alexey Frunze15958152017-02-09 19:08:30 -08004799 if (is_volatile) {
4800 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4801 }
4802
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004803 if (value_location.IsConstant()) {
4804 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4805 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4806 } else {
4807 if (!Primitive::IsFloatingPointType(type)) {
4808 DCHECK(value_location.IsRegister());
4809 GpuRegister src = value_location.AsRegister<GpuRegister>();
4810 if (kPoisonHeapReferences && needs_write_barrier) {
4811 // Note that in the case where `value` is a null reference,
4812 // we do not enter this block, as a null reference does not
4813 // need poisoning.
4814 DCHECK_EQ(type, Primitive::kPrimNot);
4815 __ PoisonHeapReference(TMP, src);
4816 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4817 } else {
4818 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4819 }
4820 } else {
4821 DCHECK(value_location.IsFpuRegister());
4822 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4823 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4824 }
4825 }
Alexey Frunze15958152017-02-09 19:08:30 -08004826
Alexey Frunzec061de12017-02-14 13:27:23 -08004827 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004828 DCHECK(value_location.IsRegister());
4829 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004830 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004831 }
Alexey Frunze15958152017-02-09 19:08:30 -08004832
4833 if (is_volatile) {
4834 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4835 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004836}
4837
4838void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4839 HandleFieldGet(instruction, instruction->GetFieldInfo());
4840}
4841
4842void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4843 HandleFieldGet(instruction, instruction->GetFieldInfo());
4844}
4845
4846void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4847 HandleFieldSet(instruction, instruction->GetFieldInfo());
4848}
4849
4850void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004851 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004852}
4853
Alexey Frunze15958152017-02-09 19:08:30 -08004854void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4855 HInstruction* instruction,
4856 Location out,
4857 uint32_t offset,
4858 Location maybe_temp,
4859 ReadBarrierOption read_barrier_option) {
4860 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4861 if (read_barrier_option == kWithReadBarrier) {
4862 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004863 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4864 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4865 }
Alexey Frunze15958152017-02-09 19:08:30 -08004866 if (kUseBakerReadBarrier) {
4867 // Load with fast path based Baker's read barrier.
4868 // /* HeapReference<Object> */ out = *(out + offset)
4869 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4870 out,
4871 out_reg,
4872 offset,
4873 maybe_temp,
4874 /* needs_null_check */ false);
4875 } else {
4876 // Load with slow path based read barrier.
4877 // Save the value of `out` into `maybe_temp` before overwriting it
4878 // in the following move operation, as we will need it for the
4879 // read barrier below.
4880 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4881 // /* HeapReference<Object> */ out = *(out + offset)
4882 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4883 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4884 }
4885 } else {
4886 // Plain load with no read barrier.
4887 // /* HeapReference<Object> */ out = *(out + offset)
4888 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4889 __ MaybeUnpoisonHeapReference(out_reg);
4890 }
4891}
4892
4893void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4894 HInstruction* instruction,
4895 Location out,
4896 Location obj,
4897 uint32_t offset,
4898 Location maybe_temp,
4899 ReadBarrierOption read_barrier_option) {
4900 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4901 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4902 if (read_barrier_option == kWithReadBarrier) {
4903 CHECK(kEmitCompilerReadBarrier);
4904 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004905 if (!kBakerReadBarrierThunksEnableForFields) {
4906 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4907 }
Alexey Frunze15958152017-02-09 19:08:30 -08004908 // Load with fast path based Baker's read barrier.
4909 // /* HeapReference<Object> */ out = *(obj + offset)
4910 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4911 out,
4912 obj_reg,
4913 offset,
4914 maybe_temp,
4915 /* needs_null_check */ false);
4916 } else {
4917 // Load with slow path based read barrier.
4918 // /* HeapReference<Object> */ out = *(obj + offset)
4919 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4920 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4921 }
4922 } else {
4923 // Plain load with no read barrier.
4924 // /* HeapReference<Object> */ out = *(obj + offset)
4925 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4926 __ MaybeUnpoisonHeapReference(out_reg);
4927 }
4928}
4929
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004930static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4931 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4932 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4933 return reg - V0;
4934 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4935 return 13 + (reg - S2);
4936 } else if (reg == S8) { // One more.
4937 return 19;
4938 }
4939 LOG(FATAL) << "Unexpected register " << reg;
4940 UNREACHABLE();
4941}
4942
4943static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4944 int num = GetBakerMarkThunkNumber(reg) +
4945 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4946 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4947}
4948
4949static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4950 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4951 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4952}
4953
4954void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4955 Location root,
4956 GpuRegister obj,
4957 uint32_t offset,
4958 ReadBarrierOption read_barrier_option,
4959 Mips64Label* label_low) {
4960 if (label_low != nullptr) {
4961 DCHECK_EQ(offset, 0x5678u);
4962 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004963 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004964 if (read_barrier_option == kWithReadBarrier) {
4965 DCHECK(kEmitCompilerReadBarrier);
4966 if (kUseBakerReadBarrier) {
4967 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4968 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004969 if (kBakerReadBarrierThunksEnableForGcRoots) {
4970 // Note that we do not actually check the value of `GetIsGcMarking()`
4971 // to decide whether to mark the loaded GC root or not. Instead, we
4972 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4973 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4974 // vice versa.
4975 //
4976 // We use thunks for the slow path. That thunk checks the reference
4977 // and jumps to the entrypoint if needed.
4978 //
4979 // temp = Thread::Current()->pReadBarrierMarkReg00
4980 // // AKA &art_quick_read_barrier_mark_introspection.
4981 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4982 // if (temp != nullptr) {
4983 // temp = &gc_root_thunk<root_reg>
4984 // root = temp(root)
4985 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004986
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004987 const int32_t entry_point_offset =
4988 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4989 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4990 int16_t offset_low = Low16Bits(offset);
4991 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
4992 // extension in lwu.
4993 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4994 GpuRegister base = short_offset ? obj : TMP;
4995 // Loading the entrypoint does not require a load acquire since it is only changed when
4996 // threads are suspended or running a checkpoint.
4997 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4998 if (!short_offset) {
4999 DCHECK(!label_low);
5000 __ Daui(base, obj, offset_high);
5001 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005002 Mips64Label skip_call;
5003 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005004 if (label_low != nullptr) {
5005 DCHECK(short_offset);
5006 __ Bind(label_low);
5007 }
5008 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5009 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5010 // in delay slot.
5011 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005012 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005013 } else {
5014 // Note that we do not actually check the value of `GetIsGcMarking()`
5015 // to decide whether to mark the loaded GC root or not. Instead, we
5016 // load into `temp` (T9) the read barrier mark entry point corresponding
5017 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5018 // is false, and vice versa.
5019 //
5020 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5021 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5022 // if (temp != null) {
5023 // root = temp(root)
5024 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005025
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005026 if (label_low != nullptr) {
5027 __ Bind(label_low);
5028 }
5029 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5030 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5031 static_assert(
5032 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5033 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5034 "have different sizes.");
5035 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5036 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5037 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005038
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005039 // Slow path marking the GC root `root`.
5040 Location temp = Location::RegisterLocation(T9);
5041 SlowPathCodeMIPS64* slow_path =
5042 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
5043 instruction,
5044 root,
5045 /*entrypoint*/ temp);
5046 codegen_->AddSlowPath(slow_path);
5047
5048 const int32_t entry_point_offset =
5049 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5050 // Loading the entrypoint does not require a load acquire since it is only changed when
5051 // threads are suspended or running a checkpoint.
5052 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5053 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5054 __ Bind(slow_path->GetExitLabel());
5055 }
Alexey Frunze15958152017-02-09 19:08:30 -08005056 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005057 if (label_low != nullptr) {
5058 __ Bind(label_low);
5059 }
Alexey Frunze15958152017-02-09 19:08:30 -08005060 // GC root loaded through a slow path for read barriers other
5061 // than Baker's.
5062 // /* GcRoot<mirror::Object>* */ root = obj + offset
5063 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5064 // /* mirror::Object* */ root = root->Read()
5065 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5066 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005067 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005068 if (label_low != nullptr) {
5069 __ Bind(label_low);
5070 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005071 // Plain GC root load with no read barrier.
5072 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5073 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5074 // Note that GC roots are not affected by heap poisoning, thus we
5075 // do not have to unpoison `root_reg` here.
5076 }
5077}
5078
Alexey Frunze15958152017-02-09 19:08:30 -08005079void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5080 Location ref,
5081 GpuRegister obj,
5082 uint32_t offset,
5083 Location temp,
5084 bool needs_null_check) {
5085 DCHECK(kEmitCompilerReadBarrier);
5086 DCHECK(kUseBakerReadBarrier);
5087
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005088 if (kBakerReadBarrierThunksEnableForFields) {
5089 // Note that we do not actually check the value of `GetIsGcMarking()`
5090 // to decide whether to mark the loaded reference or not. Instead, we
5091 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5092 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5093 // vice versa.
5094 //
5095 // We use thunks for the slow path. That thunk checks the reference
5096 // and jumps to the entrypoint if needed. If the holder is not gray,
5097 // it issues a load-load memory barrier and returns to the original
5098 // reference load.
5099 //
5100 // temp = Thread::Current()->pReadBarrierMarkReg00
5101 // // AKA &art_quick_read_barrier_mark_introspection.
5102 // if (temp != nullptr) {
5103 // temp = &field_array_thunk<holder_reg>
5104 // temp()
5105 // }
5106 // not_gray_return_address:
5107 // // If the offset is too large to fit into the lw instruction, we
5108 // // use an adjusted base register (TMP) here. This register
5109 // // receives bits 16 ... 31 of the offset before the thunk invocation
5110 // // and the thunk benefits from it.
5111 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5112 // gray_return_address:
5113
5114 DCHECK(temp.IsInvalid());
5115 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5116 const int32_t entry_point_offset =
5117 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5118 // There may have or may have not been a null check if the field offset is smaller than
5119 // the page size.
5120 // There must've been a null check in case it's actually a load from an array.
5121 // We will, however, perform an explicit null check in the thunk as it's easier to
5122 // do it than not.
5123 if (instruction->IsArrayGet()) {
5124 DCHECK(!needs_null_check);
5125 }
5126 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5127 // Loading the entrypoint does not require a load acquire since it is only changed when
5128 // threads are suspended or running a checkpoint.
5129 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5130 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005131 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005132 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005133 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005134 __ Nop(); // In forbidden slot.
5135 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005136 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005137 // /* HeapReference<Object> */ ref = *(obj + offset)
5138 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5139 } else {
5140 int16_t offset_low = Low16Bits(offset);
5141 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005142 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005143 __ Daui(TMP, obj, offset_high); // In delay slot.
5144 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005145 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005146 // /* HeapReference<Object> */ ref = *(obj + offset)
5147 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5148 }
5149 if (needs_null_check) {
5150 MaybeRecordImplicitNullCheck(instruction);
5151 }
5152 __ MaybeUnpoisonHeapReference(ref_reg);
5153 return;
5154 }
5155
Alexey Frunze15958152017-02-09 19:08:30 -08005156 // /* HeapReference<Object> */ ref = *(obj + offset)
5157 Location no_index = Location::NoLocation();
5158 ScaleFactor no_scale_factor = TIMES_1;
5159 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5160 ref,
5161 obj,
5162 offset,
5163 no_index,
5164 no_scale_factor,
5165 temp,
5166 needs_null_check);
5167}
5168
5169void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5170 Location ref,
5171 GpuRegister obj,
5172 uint32_t data_offset,
5173 Location index,
5174 Location temp,
5175 bool needs_null_check) {
5176 DCHECK(kEmitCompilerReadBarrier);
5177 DCHECK(kUseBakerReadBarrier);
5178
5179 static_assert(
5180 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5181 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005182 ScaleFactor scale_factor = TIMES_4;
5183
5184 if (kBakerReadBarrierThunksEnableForArrays) {
5185 // Note that we do not actually check the value of `GetIsGcMarking()`
5186 // to decide whether to mark the loaded reference or not. Instead, we
5187 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5188 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5189 // vice versa.
5190 //
5191 // We use thunks for the slow path. That thunk checks the reference
5192 // and jumps to the entrypoint if needed. If the holder is not gray,
5193 // it issues a load-load memory barrier and returns to the original
5194 // reference load.
5195 //
5196 // temp = Thread::Current()->pReadBarrierMarkReg00
5197 // // AKA &art_quick_read_barrier_mark_introspection.
5198 // if (temp != nullptr) {
5199 // temp = &field_array_thunk<holder_reg>
5200 // temp()
5201 // }
5202 // not_gray_return_address:
5203 // // The element address is pre-calculated in the TMP register before the
5204 // // thunk invocation and the thunk benefits from it.
5205 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5206 // gray_return_address:
5207
5208 DCHECK(temp.IsInvalid());
5209 DCHECK(index.IsValid());
5210 const int32_t entry_point_offset =
5211 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5212 // We will not do the explicit null check in the thunk as some form of a null check
5213 // must've been done earlier.
5214 DCHECK(!needs_null_check);
5215 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5216 // Loading the entrypoint does not require a load acquire since it is only changed when
5217 // threads are suspended or running a checkpoint.
5218 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005219 Mips64Label skip_call;
5220 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005221 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5222 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5223 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5224 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005225 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005226 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5227 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5228 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5229 __ MaybeUnpoisonHeapReference(ref_reg);
5230 return;
5231 }
5232
Alexey Frunze15958152017-02-09 19:08:30 -08005233 // /* HeapReference<Object> */ ref =
5234 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005235 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5236 ref,
5237 obj,
5238 data_offset,
5239 index,
5240 scale_factor,
5241 temp,
5242 needs_null_check);
5243}
5244
5245void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5246 Location ref,
5247 GpuRegister obj,
5248 uint32_t offset,
5249 Location index,
5250 ScaleFactor scale_factor,
5251 Location temp,
5252 bool needs_null_check,
5253 bool always_update_field) {
5254 DCHECK(kEmitCompilerReadBarrier);
5255 DCHECK(kUseBakerReadBarrier);
5256
5257 // In slow path based read barriers, the read barrier call is
5258 // inserted after the original load. However, in fast path based
5259 // Baker's read barriers, we need to perform the load of
5260 // mirror::Object::monitor_ *before* the original reference load.
5261 // This load-load ordering is required by the read barrier.
5262 // The fast path/slow path (for Baker's algorithm) should look like:
5263 //
5264 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5265 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5266 // HeapReference<Object> ref = *src; // Original reference load.
5267 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5268 // if (is_gray) {
5269 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5270 // }
5271 //
5272 // Note: the original implementation in ReadBarrier::Barrier is
5273 // slightly more complex as it performs additional checks that we do
5274 // not do here for performance reasons.
5275
5276 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5277 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5278 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5279
5280 // /* int32_t */ monitor = obj->monitor_
5281 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5282 if (needs_null_check) {
5283 MaybeRecordImplicitNullCheck(instruction);
5284 }
5285 // /* LockWord */ lock_word = LockWord(monitor)
5286 static_assert(sizeof(LockWord) == sizeof(int32_t),
5287 "art::LockWord and int32_t have different sizes.");
5288
5289 __ Sync(0); // Barrier to prevent load-load reordering.
5290
5291 // The actual reference load.
5292 if (index.IsValid()) {
5293 // Load types involving an "index": ArrayGet,
5294 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5295 // intrinsics.
5296 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5297 if (index.IsConstant()) {
5298 size_t computed_offset =
5299 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5300 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5301 } else {
5302 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005303 if (scale_factor == TIMES_1) {
5304 __ Daddu(TMP, index_reg, obj);
5305 } else {
5306 __ Dlsa(TMP, index_reg, obj, scale_factor);
5307 }
Alexey Frunze15958152017-02-09 19:08:30 -08005308 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5309 }
5310 } else {
5311 // /* HeapReference<Object> */ ref = *(obj + offset)
5312 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5313 }
5314
5315 // Object* ref = ref_addr->AsMirrorPtr()
5316 __ MaybeUnpoisonHeapReference(ref_reg);
5317
5318 // Slow path marking the object `ref` when it is gray.
5319 SlowPathCodeMIPS64* slow_path;
5320 if (always_update_field) {
5321 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5322 // of the form `obj + field_offset`, where `obj` is a register and
5323 // `field_offset` is a register. Thus `offset` and `scale_factor`
5324 // above are expected to be null in this code path.
5325 DCHECK_EQ(offset, 0u);
5326 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
5327 slow_path = new (GetGraph()->GetArena())
5328 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5329 ref,
5330 obj,
5331 /* field_offset */ index,
5332 temp_reg);
5333 } else {
5334 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
5335 }
5336 AddSlowPath(slow_path);
5337
5338 // if (rb_state == ReadBarrier::GrayState())
5339 // ref = ReadBarrier::Mark(ref);
5340 // Given the numeric representation, it's enough to check the low bit of the
5341 // rb_state. We do that by shifting the bit into the sign bit (31) and
5342 // performing a branch on less than zero.
5343 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5344 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5345 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5346 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5347 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5348 __ Bind(slow_path->GetExitLabel());
5349}
5350
5351void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5352 Location out,
5353 Location ref,
5354 Location obj,
5355 uint32_t offset,
5356 Location index) {
5357 DCHECK(kEmitCompilerReadBarrier);
5358
5359 // Insert a slow path based read barrier *after* the reference load.
5360 //
5361 // If heap poisoning is enabled, the unpoisoning of the loaded
5362 // reference will be carried out by the runtime within the slow
5363 // path.
5364 //
5365 // Note that `ref` currently does not get unpoisoned (when heap
5366 // poisoning is enabled), which is alright as the `ref` argument is
5367 // not used by the artReadBarrierSlow entry point.
5368 //
5369 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5370 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
5371 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5372 AddSlowPath(slow_path);
5373
5374 __ Bc(slow_path->GetEntryLabel());
5375 __ Bind(slow_path->GetExitLabel());
5376}
5377
5378void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5379 Location out,
5380 Location ref,
5381 Location obj,
5382 uint32_t offset,
5383 Location index) {
5384 if (kEmitCompilerReadBarrier) {
5385 // Baker's read barriers shall be handled by the fast path
5386 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5387 DCHECK(!kUseBakerReadBarrier);
5388 // If heap poisoning is enabled, unpoisoning will be taken care of
5389 // by the runtime within the slow path.
5390 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5391 } else if (kPoisonHeapReferences) {
5392 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5393 }
5394}
5395
5396void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5397 Location out,
5398 Location root) {
5399 DCHECK(kEmitCompilerReadBarrier);
5400
5401 // Insert a slow path based read barrier *after* the GC root load.
5402 //
5403 // Note that GC roots are not affected by heap poisoning, so we do
5404 // not need to do anything special for this here.
5405 SlowPathCodeMIPS64* slow_path =
5406 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
5407 AddSlowPath(slow_path);
5408
5409 __ Bc(slow_path->GetEntryLabel());
5410 __ Bind(slow_path->GetExitLabel());
5411}
5412
Alexey Frunze4dda3372015-06-01 18:31:49 -07005413void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005414 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5415 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005416 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005417 switch (type_check_kind) {
5418 case TypeCheckKind::kExactCheck:
5419 case TypeCheckKind::kAbstractClassCheck:
5420 case TypeCheckKind::kClassHierarchyCheck:
5421 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005422 call_kind =
5423 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005424 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005425 break;
5426 case TypeCheckKind::kArrayCheck:
5427 case TypeCheckKind::kUnresolvedCheck:
5428 case TypeCheckKind::kInterfaceCheck:
5429 call_kind = LocationSummary::kCallOnSlowPath;
5430 break;
5431 }
5432
Alexey Frunze4dda3372015-06-01 18:31:49 -07005433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005434 if (baker_read_barrier_slow_path) {
5435 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5436 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005437 locations->SetInAt(0, Location::RequiresRegister());
5438 locations->SetInAt(1, Location::RequiresRegister());
5439 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005440 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005441 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005442 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005443}
5444
5445void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005446 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005447 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005448 Location obj_loc = locations->InAt(0);
5449 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005450 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005451 Location out_loc = locations->Out();
5452 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5453 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5454 DCHECK_LE(num_temps, 1u);
5455 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005456 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5457 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5458 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5459 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005460 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005461 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005462
5463 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005464 // Avoid this check if we know `obj` is not null.
5465 if (instruction->MustDoNullCheck()) {
5466 __ Move(out, ZERO);
5467 __ Beqzc(obj, &done);
5468 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005469
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005470 switch (type_check_kind) {
5471 case TypeCheckKind::kExactCheck: {
5472 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005473 GenerateReferenceLoadTwoRegisters(instruction,
5474 out_loc,
5475 obj_loc,
5476 class_offset,
5477 maybe_temp_loc,
5478 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005479 // Classes must be equal for the instanceof to succeed.
5480 __ Xor(out, out, cls);
5481 __ Sltiu(out, out, 1);
5482 break;
5483 }
5484
5485 case TypeCheckKind::kAbstractClassCheck: {
5486 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005487 GenerateReferenceLoadTwoRegisters(instruction,
5488 out_loc,
5489 obj_loc,
5490 class_offset,
5491 maybe_temp_loc,
5492 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005493 // If the class is abstract, we eagerly fetch the super class of the
5494 // object to avoid doing a comparison we know will fail.
5495 Mips64Label loop;
5496 __ Bind(&loop);
5497 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005498 GenerateReferenceLoadOneRegister(instruction,
5499 out_loc,
5500 super_offset,
5501 maybe_temp_loc,
5502 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005503 // If `out` is null, we use it for the result, and jump to `done`.
5504 __ Beqzc(out, &done);
5505 __ Bnec(out, cls, &loop);
5506 __ LoadConst32(out, 1);
5507 break;
5508 }
5509
5510 case TypeCheckKind::kClassHierarchyCheck: {
5511 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005512 GenerateReferenceLoadTwoRegisters(instruction,
5513 out_loc,
5514 obj_loc,
5515 class_offset,
5516 maybe_temp_loc,
5517 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005518 // Walk over the class hierarchy to find a match.
5519 Mips64Label loop, success;
5520 __ Bind(&loop);
5521 __ Beqc(out, cls, &success);
5522 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005523 GenerateReferenceLoadOneRegister(instruction,
5524 out_loc,
5525 super_offset,
5526 maybe_temp_loc,
5527 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005528 __ Bnezc(out, &loop);
5529 // If `out` is null, we use it for the result, and jump to `done`.
5530 __ Bc(&done);
5531 __ Bind(&success);
5532 __ LoadConst32(out, 1);
5533 break;
5534 }
5535
5536 case TypeCheckKind::kArrayObjectCheck: {
5537 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005538 GenerateReferenceLoadTwoRegisters(instruction,
5539 out_loc,
5540 obj_loc,
5541 class_offset,
5542 maybe_temp_loc,
5543 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005544 // Do an exact check.
5545 Mips64Label success;
5546 __ Beqc(out, cls, &success);
5547 // Otherwise, we need to check that the object's class is a non-primitive array.
5548 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005549 GenerateReferenceLoadOneRegister(instruction,
5550 out_loc,
5551 component_offset,
5552 maybe_temp_loc,
5553 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005554 // If `out` is null, we use it for the result, and jump to `done`.
5555 __ Beqzc(out, &done);
5556 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5557 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5558 __ Sltiu(out, out, 1);
5559 __ Bc(&done);
5560 __ Bind(&success);
5561 __ LoadConst32(out, 1);
5562 break;
5563 }
5564
5565 case TypeCheckKind::kArrayCheck: {
5566 // No read barrier since the slow path will retry upon failure.
5567 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005568 GenerateReferenceLoadTwoRegisters(instruction,
5569 out_loc,
5570 obj_loc,
5571 class_offset,
5572 maybe_temp_loc,
5573 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005574 DCHECK(locations->OnlyCallsOnSlowPath());
5575 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5576 /* is_fatal */ false);
5577 codegen_->AddSlowPath(slow_path);
5578 __ Bnec(out, cls, slow_path->GetEntryLabel());
5579 __ LoadConst32(out, 1);
5580 break;
5581 }
5582
5583 case TypeCheckKind::kUnresolvedCheck:
5584 case TypeCheckKind::kInterfaceCheck: {
5585 // Note that we indeed only call on slow path, but we always go
5586 // into the slow path for the unresolved and interface check
5587 // cases.
5588 //
5589 // We cannot directly call the InstanceofNonTrivial runtime
5590 // entry point without resorting to a type checking slow path
5591 // here (i.e. by calling InvokeRuntime directly), as it would
5592 // require to assign fixed registers for the inputs of this
5593 // HInstanceOf instruction (following the runtime calling
5594 // convention), which might be cluttered by the potential first
5595 // read barrier emission at the beginning of this method.
5596 //
5597 // TODO: Introduce a new runtime entry point taking the object
5598 // to test (instead of its class) as argument, and let it deal
5599 // with the read barrier issues. This will let us refactor this
5600 // case of the `switch` code as it was previously (with a direct
5601 // call to the runtime not using a type checking slow path).
5602 // This should also be beneficial for the other cases above.
5603 DCHECK(locations->OnlyCallsOnSlowPath());
5604 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
5605 /* is_fatal */ false);
5606 codegen_->AddSlowPath(slow_path);
5607 __ Bc(slow_path->GetEntryLabel());
5608 break;
5609 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005610 }
5611
5612 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005613
5614 if (slow_path != nullptr) {
5615 __ Bind(slow_path->GetExitLabel());
5616 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005617}
5618
5619void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
5620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5621 locations->SetOut(Location::ConstantLocation(constant));
5622}
5623
5624void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5625 // Will be generated at use site.
5626}
5627
5628void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
5629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5630 locations->SetOut(Location::ConstantLocation(constant));
5631}
5632
5633void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5634 // Will be generated at use site.
5635}
5636
Calin Juravle175dc732015-08-25 15:42:32 +01005637void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5638 // The trampoline uses the same calling convention as dex calling conventions,
5639 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5640 // the method_idx.
5641 HandleInvoke(invoke);
5642}
5643
5644void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5645 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5646}
5647
Alexey Frunze4dda3372015-06-01 18:31:49 -07005648void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5649 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5650 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5651}
5652
5653void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5654 HandleInvoke(invoke);
5655 // The register T0 is required to be used for the hidden argument in
5656 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5657 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5658}
5659
5660void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5661 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5662 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005663 Location receiver = invoke->GetLocations()->InAt(0);
5664 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005665 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005666
5667 // Set the hidden argument.
5668 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5669 invoke->GetDexMethodIndex());
5670
5671 // temp = object->GetClass();
5672 if (receiver.IsStackSlot()) {
5673 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5674 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5675 } else {
5676 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5677 }
5678 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005679 // Instead of simply (possibly) unpoisoning `temp` here, we should
5680 // emit a read barrier for the previous class reference load.
5681 // However this is not required in practice, as this is an
5682 // intermediate/temporary reference and because the current
5683 // concurrent copying collector keeps the from-space memory
5684 // intact/accessible until the end of the marking phase (the
5685 // concurrent copying collector may not in the future).
5686 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005687 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5688 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5689 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005690 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005691 // temp = temp->GetImtEntryAt(method_offset);
5692 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5693 // T9 = temp->GetEntryPoint();
5694 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5695 // T9();
5696 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005697 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005698 DCHECK(!codegen_->IsLeafMethod());
5699 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5700}
5701
5702void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005703 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5704 if (intrinsic.TryDispatch(invoke)) {
5705 return;
5706 }
5707
Alexey Frunze4dda3372015-06-01 18:31:49 -07005708 HandleInvoke(invoke);
5709}
5710
5711void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005712 // Explicit clinit checks triggered by static invokes must have been pruned by
5713 // art::PrepareForRegisterAllocation.
5714 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005715
Chris Larsen3039e382015-08-26 07:54:08 -07005716 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5717 if (intrinsic.TryDispatch(invoke)) {
5718 return;
5719 }
5720
Alexey Frunze4dda3372015-06-01 18:31:49 -07005721 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005722}
5723
Orion Hodsonac141392017-01-13 11:53:47 +00005724void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5725 HandleInvoke(invoke);
5726}
5727
5728void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5729 codegen_->GenerateInvokePolymorphicCall(invoke);
5730}
5731
Chris Larsen3039e382015-08-26 07:54:08 -07005732static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005733 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005734 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5735 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005736 return true;
5737 }
5738 return false;
5739}
5740
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005741HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005742 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005743 bool fallback_load = false;
5744 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005745 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005746 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005747 case HLoadString::LoadKind::kBssEntry:
5748 DCHECK(!Runtime::Current()->UseJitCompilation());
5749 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005750 case HLoadString::LoadKind::kJitTableAddress:
5751 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005752 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005753 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005754 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005755 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005756 }
5757 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005758 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005759 }
5760 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005761}
5762
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005763HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5764 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005765 bool fallback_load = false;
5766 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005767 case HLoadClass::LoadKind::kInvalid:
5768 LOG(FATAL) << "UNREACHABLE";
5769 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005770 case HLoadClass::LoadKind::kReferrersClass:
5771 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005772 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005773 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005774 case HLoadClass::LoadKind::kBssEntry:
5775 DCHECK(!Runtime::Current()->UseJitCompilation());
5776 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005777 case HLoadClass::LoadKind::kJitTableAddress:
5778 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005779 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005780 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005781 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005782 break;
5783 }
5784 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005785 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005786 }
5787 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005788}
5789
Vladimir Markodc151b22015-10-15 18:02:30 +01005790HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5791 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005792 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005793 // On MIPS64 we support all dispatch types.
5794 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005795}
5796
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005797void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5798 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005799 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005800 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005801 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5802 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5803
Alexey Frunze19f6c692016-11-30 19:19:55 -08005804 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005805 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005806 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005807 uint32_t offset =
5808 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005809 __ LoadFromOffset(kLoadDoubleword,
5810 temp.AsRegister<GpuRegister>(),
5811 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005812 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005813 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005814 }
Vladimir Marko58155012015-08-19 12:49:41 +00005815 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005816 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005817 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005818 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5819 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005820 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005821 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005822 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5823 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5824 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005825 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5826 break;
5827 }
Vladimir Marko58155012015-08-19 12:49:41 +00005828 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005829 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5830 kLoadDoubleword,
5831 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005832 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005833 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005834 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005835 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005836 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5837 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5838 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005839 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5840 break;
5841 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005842 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5843 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5844 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005845 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005846 }
5847
Alexey Frunze19f6c692016-11-30 19:19:55 -08005848 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005849 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005850 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005851 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005852 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5853 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5854 __ LoadFromOffset(kLoadDoubleword,
5855 T9,
5856 callee_method.AsRegister<GpuRegister>(),
5857 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005858 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005859 // T9()
5860 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005861 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005862 break;
5863 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005864 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5865
Alexey Frunze4dda3372015-06-01 18:31:49 -07005866 DCHECK(!IsLeafMethod());
5867}
5868
5869void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005870 // Explicit clinit checks triggered by static invokes must have been pruned by
5871 // art::PrepareForRegisterAllocation.
5872 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005873
5874 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5875 return;
5876 }
5877
5878 LocationSummary* locations = invoke->GetLocations();
5879 codegen_->GenerateStaticOrDirectCall(invoke,
5880 locations->HasTemps()
5881 ? locations->GetTemp(0)
5882 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005883}
5884
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005885void CodeGeneratorMIPS64::GenerateVirtualCall(
5886 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005887 // Use the calling convention instead of the location of the receiver, as
5888 // intrinsics may have put the receiver in a different register. In the intrinsics
5889 // slow path, the arguments have been moved to the right place, so here we are
5890 // guaranteed that the receiver is the first register of the calling convention.
5891 InvokeDexCallingConvention calling_convention;
5892 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5893
Alexey Frunze53afca12015-11-05 16:34:23 -08005894 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005895 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5896 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5897 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005898 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005899
5900 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005901 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005902 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005903 // Instead of simply (possibly) unpoisoning `temp` here, we should
5904 // emit a read barrier for the previous class reference load.
5905 // However this is not required in practice, as this is an
5906 // intermediate/temporary reference and because the current
5907 // concurrent copying collector keeps the from-space memory
5908 // intact/accessible until the end of the marking phase (the
5909 // concurrent copying collector may not in the future).
5910 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005911 // temp = temp->GetMethodAt(method_offset);
5912 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5913 // T9 = temp->GetEntryPoint();
5914 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5915 // T9();
5916 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005917 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005918 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005919}
5920
5921void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5922 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5923 return;
5924 }
5925
5926 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005927 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005928}
5929
5930void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005931 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005932 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005933 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005934 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5935 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005936 return;
5937 }
Vladimir Marko41559982017-01-06 14:04:23 +00005938 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005939
Alexey Frunze15958152017-02-09 19:08:30 -08005940 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5941 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005942 ? LocationSummary::kCallOnSlowPath
5943 : LocationSummary::kNoCall;
5944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005945 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5946 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5947 }
Vladimir Marko41559982017-01-06 14:04:23 +00005948 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005949 locations->SetInAt(0, Location::RequiresRegister());
5950 }
5951 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005952 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5953 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5954 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005955 // Request a temp to hold the BSS entry location for the slow path.
5956 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005957 RegisterSet caller_saves = RegisterSet::Empty();
5958 InvokeRuntimeCallingConvention calling_convention;
5959 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5960 locations->SetCustomSlowPathCallerSaves(caller_saves);
5961 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005962 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005963 }
5964 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005965}
5966
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005967// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5968// move.
5969void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005970 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005971 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005972 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005973 return;
5974 }
Vladimir Marko41559982017-01-06 14:04:23 +00005975 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005976
Vladimir Marko41559982017-01-06 14:04:23 +00005977 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005978 Location out_loc = locations->Out();
5979 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5980 GpuRegister current_method_reg = ZERO;
5981 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005982 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005983 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5984 }
5985
Alexey Frunze15958152017-02-09 19:08:30 -08005986 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5987 ? kWithoutReadBarrier
5988 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005989 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005990 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005991 switch (load_kind) {
5992 case HLoadClass::LoadKind::kReferrersClass:
5993 DCHECK(!cls->CanCallRuntime());
5994 DCHECK(!cls->MustGenerateClinitCheck());
5995 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5996 GenerateGcRootFieldLoad(cls,
5997 out_loc,
5998 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005999 ArtMethod::DeclaringClassOffset().Int32Value(),
6000 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006001 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006002 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006003 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006004 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006005 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006006 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006007 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6008 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6009 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006010 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6011 break;
6012 }
6013 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006014 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006015 uint32_t address = dchecked_integral_cast<uint32_t>(
6016 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6017 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006018 __ LoadLiteral(out,
6019 kLoadUnsignedWord,
6020 codegen_->DeduplicateBootImageAddressLiteral(address));
6021 break;
6022 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006023 case HLoadClass::LoadKind::kBootImageClassTable: {
6024 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6025 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6026 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6027 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6028 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6029 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6030 __ Lwu(out, AT, /* placeholder */ 0x5678);
6031 // Extract the reference from the slot data, i.e. clear the hash bits.
6032 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6033 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6034 if (masked_hash != 0) {
6035 __ Daddiu(out, out, -masked_hash);
6036 }
6037 break;
6038 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006039 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006040 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
6041 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6042 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
6043 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6044 GpuRegister temp = non_baker_read_barrier
6045 ? out
6046 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006047 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, temp);
6048 GenerateGcRootFieldLoad(cls,
6049 out_loc,
6050 temp,
6051 /* placeholder */ 0x5678,
6052 read_barrier_option,
6053 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006054 generate_null_check = true;
6055 break;
6056 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006057 case HLoadClass::LoadKind::kJitTableAddress:
6058 __ LoadLiteral(out,
6059 kLoadUnsignedWord,
6060 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6061 cls->GetTypeIndex(),
6062 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006063 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006064 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006065 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006066 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006067 LOG(FATAL) << "UNREACHABLE";
6068 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006069 }
6070
6071 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6072 DCHECK(cls->CanCallRuntime());
6073 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006074 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006075 codegen_->AddSlowPath(slow_path);
6076 if (generate_null_check) {
6077 __ Beqzc(out, slow_path->GetEntryLabel());
6078 }
6079 if (cls->MustGenerateClinitCheck()) {
6080 GenerateClassInitializationCheck(slow_path, out);
6081 } else {
6082 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006083 }
6084 }
6085}
6086
David Brazdilcb1c0552015-08-04 16:22:25 +01006087static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006088 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006089}
6090
Alexey Frunze4dda3372015-06-01 18:31:49 -07006091void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6092 LocationSummary* locations =
6093 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6094 locations->SetOut(Location::RequiresRegister());
6095}
6096
6097void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6098 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006099 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6100}
6101
6102void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
6103 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6104}
6105
6106void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6107 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006108}
6109
Alexey Frunze4dda3372015-06-01 18:31:49 -07006110void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006111 HLoadString::LoadKind load_kind = load->GetLoadKind();
6112 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006114 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006115 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006116 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006117 } else {
6118 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006119 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6120 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6121 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006122 // Request a temp to hold the BSS entry location for the slow path.
6123 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006124 RegisterSet caller_saves = RegisterSet::Empty();
6125 InvokeRuntimeCallingConvention calling_convention;
6126 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6127 locations->SetCustomSlowPathCallerSaves(caller_saves);
6128 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006129 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006130 }
6131 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006132 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006133}
6134
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006135// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6136// move.
6137void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006138 HLoadString::LoadKind load_kind = load->GetLoadKind();
6139 LocationSummary* locations = load->GetLocations();
6140 Location out_loc = locations->Out();
6141 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6142
6143 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006144 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6145 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006146 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006147 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006148 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6149 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6150 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006151 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006152 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006153 }
6154 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006155 uint32_t address = dchecked_integral_cast<uint32_t>(
6156 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6157 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006158 __ LoadLiteral(out,
6159 kLoadUnsignedWord,
6160 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006161 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006162 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006163 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006164 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006165 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006166 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006167 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6168 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006169 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6170 __ Lwu(out, AT, /* placeholder */ 0x5678);
6171 return;
6172 }
6173 case HLoadString::LoadKind::kBssEntry: {
6174 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6175 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6176 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6177 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6178 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006179 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
6180 GpuRegister temp = non_baker_read_barrier
6181 ? out
6182 : locations->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006183 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, temp);
Alexey Frunze15958152017-02-09 19:08:30 -08006184 GenerateGcRootFieldLoad(load,
6185 out_loc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006186 temp,
Alexey Frunze15958152017-02-09 19:08:30 -08006187 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006188 kCompilerReadBarrierOption,
6189 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006190 SlowPathCodeMIPS64* slow_path =
6191 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load, info_high);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006192 codegen_->AddSlowPath(slow_path);
6193 __ Beqzc(out, slow_path->GetEntryLabel());
6194 __ Bind(slow_path->GetExitLabel());
6195 return;
6196 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006197 case HLoadString::LoadKind::kJitTableAddress:
6198 __ LoadLiteral(out,
6199 kLoadUnsignedWord,
6200 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6201 load->GetStringIndex(),
6202 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006203 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006204 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006205 default:
6206 break;
6207 }
6208
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006209 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006210 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006211 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006212 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006213 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6214 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6215 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006216}
6217
Alexey Frunze4dda3372015-06-01 18:31:49 -07006218void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
6219 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6220 locations->SetOut(Location::ConstantLocation(constant));
6221}
6222
6223void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6224 // Will be generated at use site.
6225}
6226
6227void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
6228 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006229 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006230 InvokeRuntimeCallingConvention calling_convention;
6231 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6232}
6233
6234void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006235 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006236 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006237 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006238 if (instruction->IsEnter()) {
6239 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6240 } else {
6241 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6242 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006243}
6244
6245void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6246 LocationSummary* locations =
6247 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
6248 switch (mul->GetResultType()) {
6249 case Primitive::kPrimInt:
6250 case Primitive::kPrimLong:
6251 locations->SetInAt(0, Location::RequiresRegister());
6252 locations->SetInAt(1, Location::RequiresRegister());
6253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6254 break;
6255
6256 case Primitive::kPrimFloat:
6257 case Primitive::kPrimDouble:
6258 locations->SetInAt(0, Location::RequiresFpuRegister());
6259 locations->SetInAt(1, Location::RequiresFpuRegister());
6260 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6261 break;
6262
6263 default:
6264 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6265 }
6266}
6267
6268void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
6269 Primitive::Type type = instruction->GetType();
6270 LocationSummary* locations = instruction->GetLocations();
6271
6272 switch (type) {
6273 case Primitive::kPrimInt:
6274 case Primitive::kPrimLong: {
6275 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6276 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6277 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
6278 if (type == Primitive::kPrimInt)
6279 __ MulR6(dst, lhs, rhs);
6280 else
6281 __ Dmul(dst, lhs, rhs);
6282 break;
6283 }
6284 case Primitive::kPrimFloat:
6285 case Primitive::kPrimDouble: {
6286 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6287 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6288 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
6289 if (type == Primitive::kPrimFloat)
6290 __ MulS(dst, lhs, rhs);
6291 else
6292 __ MulD(dst, lhs, rhs);
6293 break;
6294 }
6295 default:
6296 LOG(FATAL) << "Unexpected mul type " << type;
6297 }
6298}
6299
6300void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6301 LocationSummary* locations =
6302 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
6303 switch (neg->GetResultType()) {
6304 case Primitive::kPrimInt:
6305 case Primitive::kPrimLong:
6306 locations->SetInAt(0, Location::RequiresRegister());
6307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6308 break;
6309
6310 case Primitive::kPrimFloat:
6311 case Primitive::kPrimDouble:
6312 locations->SetInAt(0, Location::RequiresFpuRegister());
6313 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6314 break;
6315
6316 default:
6317 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6318 }
6319}
6320
6321void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
6322 Primitive::Type type = instruction->GetType();
6323 LocationSummary* locations = instruction->GetLocations();
6324
6325 switch (type) {
6326 case Primitive::kPrimInt:
6327 case Primitive::kPrimLong: {
6328 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6329 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6330 if (type == Primitive::kPrimInt)
6331 __ Subu(dst, ZERO, src);
6332 else
6333 __ Dsubu(dst, ZERO, src);
6334 break;
6335 }
6336 case Primitive::kPrimFloat:
6337 case Primitive::kPrimDouble: {
6338 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6339 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6340 if (type == Primitive::kPrimFloat)
6341 __ NegS(dst, src);
6342 else
6343 __ NegD(dst, src);
6344 break;
6345 }
6346 default:
6347 LOG(FATAL) << "Unexpected neg type " << type;
6348 }
6349}
6350
6351void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
6352 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006354 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006355 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006356 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6357 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006358}
6359
6360void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006361 // Note: if heap poisoning is enabled, the entry point takes care
6362 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006363 QuickEntrypointEnum entrypoint =
6364 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6365 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006366 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006367 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006368}
6369
6370void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
6371 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006372 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006373 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006374 if (instruction->IsStringAlloc()) {
6375 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6376 } else {
6377 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006378 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006379 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
6380}
6381
6382void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006383 // Note: if heap poisoning is enabled, the entry point takes care
6384 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006385 if (instruction->IsStringAlloc()) {
6386 // String is allocated through StringFactory. Call NewEmptyString entry point.
6387 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006388 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006389 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006390 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6391 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6392 __ Jalr(T9);
6393 __ Nop();
6394 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6395 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006396 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006397 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006398 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006399}
6400
6401void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
6402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6403 locations->SetInAt(0, Location::RequiresRegister());
6404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6405}
6406
6407void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
6408 Primitive::Type type = instruction->GetType();
6409 LocationSummary* locations = instruction->GetLocations();
6410
6411 switch (type) {
6412 case Primitive::kPrimInt:
6413 case Primitive::kPrimLong: {
6414 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6415 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6416 __ Nor(dst, src, ZERO);
6417 break;
6418 }
6419
6420 default:
6421 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6422 }
6423}
6424
6425void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6426 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6427 locations->SetInAt(0, Location::RequiresRegister());
6428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6429}
6430
6431void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6432 LocationSummary* locations = instruction->GetLocations();
6433 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6434 locations->InAt(0).AsRegister<GpuRegister>(),
6435 1);
6436}
6437
6438void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006439 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6440 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006441}
6442
Calin Juravle2ae48182016-03-16 14:05:09 +00006443void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6444 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006445 return;
6446 }
6447 Location obj = instruction->GetLocations()->InAt(0);
6448
6449 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006450 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006451}
6452
Calin Juravle2ae48182016-03-16 14:05:09 +00006453void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006454 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006455 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006456
6457 Location obj = instruction->GetLocations()->InAt(0);
6458
6459 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6460}
6461
6462void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006463 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006464}
6465
6466void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6467 HandleBinaryOp(instruction);
6468}
6469
6470void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6471 HandleBinaryOp(instruction);
6472}
6473
6474void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6475 LOG(FATAL) << "Unreachable";
6476}
6477
6478void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
6479 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6480}
6481
6482void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
6483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6484 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6485 if (location.IsStackSlot()) {
6486 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6487 } else if (location.IsDoubleStackSlot()) {
6488 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6489 }
6490 locations->SetOut(location);
6491}
6492
6493void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6494 ATTRIBUTE_UNUSED) {
6495 // Nothing to do, the parameter is already at its location.
6496}
6497
6498void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6499 LocationSummary* locations =
6500 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6501 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6502}
6503
6504void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6505 ATTRIBUTE_UNUSED) {
6506 // Nothing to do, the method is already at its location.
6507}
6508
6509void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
6510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006511 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006512 locations->SetInAt(i, Location::Any());
6513 }
6514 locations->SetOut(Location::Any());
6515}
6516
6517void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6518 LOG(FATAL) << "Unreachable";
6519}
6520
6521void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
6522 Primitive::Type type = rem->GetResultType();
6523 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006524 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6525 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6527
6528 switch (type) {
6529 case Primitive::kPrimInt:
6530 case Primitive::kPrimLong:
6531 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006532 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6534 break;
6535
6536 case Primitive::kPrimFloat:
6537 case Primitive::kPrimDouble: {
6538 InvokeRuntimeCallingConvention calling_convention;
6539 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6540 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6541 locations->SetOut(calling_convention.GetReturnLocation(type));
6542 break;
6543 }
6544
6545 default:
6546 LOG(FATAL) << "Unexpected rem type " << type;
6547 }
6548}
6549
6550void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
6551 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006552
6553 switch (type) {
6554 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07006555 case Primitive::kPrimLong:
6556 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006557 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006558
6559 case Primitive::kPrimFloat:
6560 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01006561 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
6562 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006563 if (type == Primitive::kPrimFloat) {
6564 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6565 } else {
6566 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6567 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006568 break;
6569 }
6570 default:
6571 LOG(FATAL) << "Unexpected rem type " << type;
6572 }
6573}
6574
Igor Murashkind01745e2017-04-05 16:40:31 -07006575void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6576 constructor_fence->SetLocations(nullptr);
6577}
6578
6579void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6580 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6581 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6582}
6583
Alexey Frunze4dda3372015-06-01 18:31:49 -07006584void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6585 memory_barrier->SetLocations(nullptr);
6586}
6587
6588void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6589 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6590}
6591
6592void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
6593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6594 Primitive::Type return_type = ret->InputAt(0)->GetType();
6595 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6596}
6597
6598void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6599 codegen_->GenerateFrameExit();
6600}
6601
6602void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6603 ret->SetLocations(nullptr);
6604}
6605
6606void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6607 codegen_->GenerateFrameExit();
6608}
6609
Alexey Frunze92d90602015-12-18 18:16:36 -08006610void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6611 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006612}
6613
Alexey Frunze92d90602015-12-18 18:16:36 -08006614void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6615 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006616}
6617
Alexey Frunze4dda3372015-06-01 18:31:49 -07006618void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6619 HandleShift(shl);
6620}
6621
6622void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6623 HandleShift(shl);
6624}
6625
6626void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6627 HandleShift(shr);
6628}
6629
6630void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6631 HandleShift(shr);
6632}
6633
Alexey Frunze4dda3372015-06-01 18:31:49 -07006634void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6635 HandleBinaryOp(instruction);
6636}
6637
6638void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6639 HandleBinaryOp(instruction);
6640}
6641
6642void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6643 HandleFieldGet(instruction, instruction->GetFieldInfo());
6644}
6645
6646void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6647 HandleFieldGet(instruction, instruction->GetFieldInfo());
6648}
6649
6650void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6651 HandleFieldSet(instruction, instruction->GetFieldInfo());
6652}
6653
6654void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006655 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006656}
6657
Calin Juravlee460d1d2015-09-29 04:52:17 +01006658void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6659 HUnresolvedInstanceFieldGet* instruction) {
6660 FieldAccessCallingConventionMIPS64 calling_convention;
6661 codegen_->CreateUnresolvedFieldLocationSummary(
6662 instruction, instruction->GetFieldType(), calling_convention);
6663}
6664
6665void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6666 HUnresolvedInstanceFieldGet* instruction) {
6667 FieldAccessCallingConventionMIPS64 calling_convention;
6668 codegen_->GenerateUnresolvedFieldAccess(instruction,
6669 instruction->GetFieldType(),
6670 instruction->GetFieldIndex(),
6671 instruction->GetDexPc(),
6672 calling_convention);
6673}
6674
6675void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6676 HUnresolvedInstanceFieldSet* instruction) {
6677 FieldAccessCallingConventionMIPS64 calling_convention;
6678 codegen_->CreateUnresolvedFieldLocationSummary(
6679 instruction, instruction->GetFieldType(), calling_convention);
6680}
6681
6682void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6683 HUnresolvedInstanceFieldSet* instruction) {
6684 FieldAccessCallingConventionMIPS64 calling_convention;
6685 codegen_->GenerateUnresolvedFieldAccess(instruction,
6686 instruction->GetFieldType(),
6687 instruction->GetFieldIndex(),
6688 instruction->GetDexPc(),
6689 calling_convention);
6690}
6691
6692void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6693 HUnresolvedStaticFieldGet* instruction) {
6694 FieldAccessCallingConventionMIPS64 calling_convention;
6695 codegen_->CreateUnresolvedFieldLocationSummary(
6696 instruction, instruction->GetFieldType(), calling_convention);
6697}
6698
6699void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6700 HUnresolvedStaticFieldGet* instruction) {
6701 FieldAccessCallingConventionMIPS64 calling_convention;
6702 codegen_->GenerateUnresolvedFieldAccess(instruction,
6703 instruction->GetFieldType(),
6704 instruction->GetFieldIndex(),
6705 instruction->GetDexPc(),
6706 calling_convention);
6707}
6708
6709void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6710 HUnresolvedStaticFieldSet* instruction) {
6711 FieldAccessCallingConventionMIPS64 calling_convention;
6712 codegen_->CreateUnresolvedFieldLocationSummary(
6713 instruction, instruction->GetFieldType(), calling_convention);
6714}
6715
6716void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6717 HUnresolvedStaticFieldSet* instruction) {
6718 FieldAccessCallingConventionMIPS64 calling_convention;
6719 codegen_->GenerateUnresolvedFieldAccess(instruction,
6720 instruction->GetFieldType(),
6721 instruction->GetFieldIndex(),
6722 instruction->GetDexPc(),
6723 calling_convention);
6724}
6725
Alexey Frunze4dda3372015-06-01 18:31:49 -07006726void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006727 LocationSummary* locations =
6728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006729 // In suspend check slow path, usually there are no caller-save registers at all.
6730 // If SIMD instructions are present, however, we force spilling all live SIMD
6731 // registers in full width (since the runtime only saves/restores lower part).
6732 locations->SetCustomSlowPathCallerSaves(
6733 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006734}
6735
6736void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6737 HBasicBlock* block = instruction->GetBlock();
6738 if (block->GetLoopInformation() != nullptr) {
6739 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6740 // The back edge will generate the suspend check.
6741 return;
6742 }
6743 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6744 // The goto will generate the suspend check.
6745 return;
6746 }
6747 GenerateSuspendCheck(instruction, nullptr);
6748}
6749
Alexey Frunze4dda3372015-06-01 18:31:49 -07006750void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
6751 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006752 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006753 InvokeRuntimeCallingConvention calling_convention;
6754 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6755}
6756
6757void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006758 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006759 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6760}
6761
6762void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6763 Primitive::Type input_type = conversion->GetInputType();
6764 Primitive::Type result_type = conversion->GetResultType();
6765 DCHECK_NE(input_type, result_type);
6766
6767 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6768 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6769 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6770 }
6771
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
6773
6774 if (Primitive::IsFloatingPointType(input_type)) {
6775 locations->SetInAt(0, Location::RequiresFpuRegister());
6776 } else {
6777 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006778 }
6779
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006780 if (Primitive::IsFloatingPointType(result_type)) {
6781 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006782 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006784 }
6785}
6786
6787void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6788 LocationSummary* locations = conversion->GetLocations();
6789 Primitive::Type result_type = conversion->GetResultType();
6790 Primitive::Type input_type = conversion->GetInputType();
6791
6792 DCHECK_NE(input_type, result_type);
6793
6794 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6795 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6796 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6797
6798 switch (result_type) {
6799 case Primitive::kPrimChar:
6800 __ Andi(dst, src, 0xFFFF);
6801 break;
6802 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006803 if (input_type == Primitive::kPrimLong) {
6804 // Type conversion from long to types narrower than int is a result of code
6805 // transformations. To avoid unpredictable results for SEB and SEH, we first
6806 // need to sign-extend the low 32-bit value into bits 32 through 63.
6807 __ Sll(dst, src, 0);
6808 __ Seb(dst, dst);
6809 } else {
6810 __ Seb(dst, src);
6811 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006812 break;
6813 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00006814 if (input_type == Primitive::kPrimLong) {
6815 // Type conversion from long to types narrower than int is a result of code
6816 // transformations. To avoid unpredictable results for SEB and SEH, we first
6817 // need to sign-extend the low 32-bit value into bits 32 through 63.
6818 __ Sll(dst, src, 0);
6819 __ Seh(dst, dst);
6820 } else {
6821 __ Seh(dst, src);
6822 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006823 break;
6824 case Primitive::kPrimInt:
6825 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006826 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6827 // conversions, except when the input and output registers are the same and we are not
6828 // converting longs to shorter types. In these cases, do nothing.
6829 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
6830 __ Sll(dst, src, 0);
6831 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006832 break;
6833
6834 default:
6835 LOG(FATAL) << "Unexpected type conversion from " << input_type
6836 << " to " << result_type;
6837 }
6838 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006839 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6840 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6841 if (input_type == Primitive::kPrimLong) {
6842 __ Dmtc1(src, FTMP);
6843 if (result_type == Primitive::kPrimFloat) {
6844 __ Cvtsl(dst, FTMP);
6845 } else {
6846 __ Cvtdl(dst, FTMP);
6847 }
6848 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006849 __ Mtc1(src, FTMP);
6850 if (result_type == Primitive::kPrimFloat) {
6851 __ Cvtsw(dst, FTMP);
6852 } else {
6853 __ Cvtdw(dst, FTMP);
6854 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006855 }
6856 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6857 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006858 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6859 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006860
6861 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006862 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006863 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006864 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006865 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006866 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006867 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006868 } else {
6869 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006870 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006871 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006872 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006873 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006874 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006875 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006876 } else if (Primitive::IsFloatingPointType(result_type) &&
6877 Primitive::IsFloatingPointType(input_type)) {
6878 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6879 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6880 if (result_type == Primitive::kPrimFloat) {
6881 __ Cvtsd(dst, src);
6882 } else {
6883 __ Cvtds(dst, src);
6884 }
6885 } else {
6886 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6887 << " to " << result_type;
6888 }
6889}
6890
6891void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6892 HandleShift(ushr);
6893}
6894
6895void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6896 HandleShift(ushr);
6897}
6898
6899void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6900 HandleBinaryOp(instruction);
6901}
6902
6903void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6904 HandleBinaryOp(instruction);
6905}
6906
6907void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6908 // Nothing to do, this should be removed during prepare for register allocator.
6909 LOG(FATAL) << "Unreachable";
6910}
6911
6912void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6913 // Nothing to do, this should be removed during prepare for register allocator.
6914 LOG(FATAL) << "Unreachable";
6915}
6916
6917void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006918 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006919}
6920
6921void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006922 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006923}
6924
6925void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006926 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006927}
6928
6929void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006930 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006931}
6932
6933void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006934 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006935}
6936
6937void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006938 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006939}
6940
6941void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006942 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006943}
6944
6945void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006946 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006947}
6948
6949void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006950 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006951}
6952
6953void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006954 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006955}
6956
6957void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006958 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006959}
6960
6961void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006962 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006963}
6964
Aart Bike9f37602015-10-09 11:15:55 -07006965void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006966 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006967}
6968
6969void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006970 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006971}
6972
6973void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006974 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006975}
6976
6977void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006978 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006979}
6980
6981void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006982 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006983}
6984
6985void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006986 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006987}
6988
6989void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006990 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006991}
6992
6993void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006994 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006995}
6996
Mark Mendellfe57faa2015-09-18 09:26:15 -04006997// Simple implementation of packed switch - generate cascaded compare/jumps.
6998void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6999 LocationSummary* locations =
7000 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7001 locations->SetInAt(0, Location::RequiresRegister());
7002}
7003
Alexey Frunze0960ac52016-12-20 17:24:59 -08007004void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7005 int32_t lower_bound,
7006 uint32_t num_entries,
7007 HBasicBlock* switch_block,
7008 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007009 // Create a set of compare/jumps.
7010 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007011 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007012 // Jump to default if index is negative
7013 // Note: We don't check the case that index is positive while value < lower_bound, because in
7014 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7015 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7016
Alexey Frunze0960ac52016-12-20 17:24:59 -08007017 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007018 // Jump to successors[0] if value == lower_bound.
7019 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7020 int32_t last_index = 0;
7021 for (; num_entries - last_index > 2; last_index += 2) {
7022 __ Addiu(temp_reg, temp_reg, -2);
7023 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7024 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7025 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7026 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7027 }
7028 if (num_entries - last_index == 2) {
7029 // The last missing case_value.
7030 __ Addiu(temp_reg, temp_reg, -1);
7031 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007032 }
7033
7034 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007035 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007036 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007037 }
7038}
7039
Alexey Frunze0960ac52016-12-20 17:24:59 -08007040void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7041 int32_t lower_bound,
7042 uint32_t num_entries,
7043 HBasicBlock* switch_block,
7044 HBasicBlock* default_block) {
7045 // Create a jump table.
7046 std::vector<Mips64Label*> labels(num_entries);
7047 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7048 for (uint32_t i = 0; i < num_entries; i++) {
7049 labels[i] = codegen_->GetLabelOf(successors[i]);
7050 }
7051 JumpTable* table = __ CreateJumpTable(std::move(labels));
7052
7053 // Is the value in range?
7054 __ Addiu32(TMP, value_reg, -lower_bound);
7055 __ LoadConst32(AT, num_entries);
7056 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7057
7058 // We are in the range of the table.
7059 // Load the target address from the jump table, indexing by the value.
7060 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007061 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007062 __ Lw(TMP, TMP, 0);
7063 // Compute the absolute target address by adding the table start address
7064 // (the table contains offsets to targets relative to its start).
7065 __ Daddu(TMP, TMP, AT);
7066 // And jump.
7067 __ Jr(TMP);
7068 __ Nop();
7069}
7070
7071void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7072 int32_t lower_bound = switch_instr->GetStartValue();
7073 uint32_t num_entries = switch_instr->GetNumEntries();
7074 LocationSummary* locations = switch_instr->GetLocations();
7075 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7076 HBasicBlock* switch_block = switch_instr->GetBlock();
7077 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7078
7079 if (num_entries > kPackedSwitchJumpTableThreshold) {
7080 GenTableBasedPackedSwitch(value_reg,
7081 lower_bound,
7082 num_entries,
7083 switch_block,
7084 default_block);
7085 } else {
7086 GenPackedSwitchWithCompares(value_reg,
7087 lower_bound,
7088 num_entries,
7089 switch_block,
7090 default_block);
7091 }
7092}
7093
Chris Larsenc9905a62017-03-13 17:06:18 -07007094void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7095 LocationSummary* locations =
7096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7097 locations->SetInAt(0, Location::RequiresRegister());
7098 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007099}
7100
Chris Larsenc9905a62017-03-13 17:06:18 -07007101void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7102 LocationSummary* locations = instruction->GetLocations();
7103 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7104 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7105 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7106 __ LoadFromOffset(kLoadDoubleword,
7107 locations->Out().AsRegister<GpuRegister>(),
7108 locations->InAt(0).AsRegister<GpuRegister>(),
7109 method_offset);
7110 } else {
7111 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7112 instruction->GetIndex(), kMips64PointerSize));
7113 __ LoadFromOffset(kLoadDoubleword,
7114 locations->Out().AsRegister<GpuRegister>(),
7115 locations->InAt(0).AsRegister<GpuRegister>(),
7116 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7117 __ LoadFromOffset(kLoadDoubleword,
7118 locations->Out().AsRegister<GpuRegister>(),
7119 locations->Out().AsRegister<GpuRegister>(),
7120 method_offset);
7121 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007122}
7123
Alexey Frunze4dda3372015-06-01 18:31:49 -07007124} // namespace mips64
7125} // namespace art