blob: 81d86a9a3f9b4997fcb28cde66ac0ea1349c0fb1 [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"
Vladimir Markoeebb8212018-06-05 14:57:24 +010027#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070028#include "heap_poisoning.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070029#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070030#include "intrinsics_mips64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010031#include "linker/linker_patch.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "mirror/array-inl.h"
33#include "mirror/class-inl.h"
34#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010035#include "stack_map_stream.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070037#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070038#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070039#include "utils/stack_checks.h"
40
41namespace art {
42namespace mips64 {
43
44static constexpr int kCurrentMethodStackOffset = 0;
45static constexpr GpuRegister kMethodRegisterArgument = A0;
46
Alexey Frunze4147fcc2017-06-17 19:57:27 -070047// Flags controlling the use of thunks for Baker read barriers.
48constexpr bool kBakerReadBarrierThunksEnableForFields = true;
49constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
50constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
51
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010052Location Mips64ReturnLocation(DataType::Type return_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070053 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010054 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kInt8:
57 case DataType::Type::kUint16:
58 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080059 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010060 case DataType::Type::kInt32:
61 case DataType::Type::kReference:
Aart Bik66c158e2018-01-31 12:55:04 -080062 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070064 return Location::RegisterLocation(V0);
65
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kFloat32:
67 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070068 return Location::FpuRegisterLocation(F0);
69
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010070 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070071 return Location();
72 }
73 UNREACHABLE();
74}
75
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010076Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070077 return Mips64ReturnLocation(type);
78}
79
80Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
81 return Location::RegisterLocation(kMethodRegisterArgument);
82}
83
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010084Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070085 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010086 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070087 LOG(FATAL) << "Unexpected parameter type " << type;
88 }
89
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070091 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
92 next_location = Location::FpuRegisterLocation(
93 calling_convention.GetFpuRegisterAt(float_index_++));
94 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070096 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
97 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
98 float_index_++;
99 } else {
100 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100101 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
102 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700103 }
104
105 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700107
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108 return next_location;
109}
110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100111Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700112 return Mips64ReturnLocation(type);
113}
114
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100115// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
116#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700117#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700118
119class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
120 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000121 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122
123 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100124 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700125 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
126 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000127 if (instruction_->CanThrowIntoCatchBlock()) {
128 // Live registers will be restored in the catch block if caught.
129 SaveLiveRegisters(codegen, instruction_->GetLocations());
130 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700131 // We're moving two locations to locations that could overlap, so we need a parallel
132 // move resolver.
133 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100137 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700138 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100140 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
141 ? kQuickThrowStringBounds
142 : kQuickThrowArrayBounds;
143 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100144 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
146 }
147
Alexandre Rames8158f282015-08-07 10:26:17 +0100148 bool IsFatal() const OVERRIDE { return true; }
149
Roland Levillain46648892015-06-19 16:07:18 +0100150 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
151
Alexey Frunze4dda3372015-06-01 18:31:49 -0700152 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700153 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
154};
155
156class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
157 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700158 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
159 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160
161 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
162 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
163 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100164 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700165 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
166 }
167
Alexandre Rames8158f282015-08-07 10:26:17 +0100168 bool IsFatal() const OVERRIDE { return true; }
169
Roland Levillain46648892015-06-19 16:07:18 +0100170 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
171
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700173 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
174};
175
176class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
177 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100178 LoadClassSlowPathMIPS64(HLoadClass* cls, HInstruction* at)
179 : SlowPathCodeMIPS64(at), cls_(cls) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700180 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100181 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700182 }
183
184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000185 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700186 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100187 const uint32_t dex_pc = instruction_->GetDexPc();
188 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
189 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
190
Alexey Frunze4dda3372015-06-01 18:31:49 -0700191 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 __ Bind(GetEntryLabel());
193 SaveLiveRegisters(codegen, locations);
194
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100195 InvokeRuntimeCallingConvention calling_convention;
196 if (must_resolve_type) {
197 DCHECK(IsSameDexFile(cls_->GetDexFile(), mips64_codegen->GetGraph()->GetDexFile()));
198 dex::TypeIndex type_index = cls_->GetTypeIndex();
199 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
200 mips64_codegen->InvokeRuntime(kQuickInitializeType, instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700201 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100202 // If we also must_do_clinit, the resolved type is now in the correct register.
203 } else {
204 DCHECK(must_do_clinit);
205 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
206 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
207 source,
208 cls_->GetType());
209 }
210 if (must_do_clinit) {
211 mips64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
212 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
214
215 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700216 if (out.IsValid()) {
217 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100218 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700219 mips64_codegen->MoveLocation(out,
220 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
221 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700222 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700223 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700224
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700225 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700226 }
227
Roland Levillain46648892015-06-19 16:07:18 +0100228 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
229
Alexey Frunze4dda3372015-06-01 18:31:49 -0700230 private:
231 // The class this slow path will load.
232 HLoadClass* const cls_;
233
Alexey Frunze4dda3372015-06-01 18:31:49 -0700234 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
235};
236
237class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
238 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000239 explicit LoadStringSlowPathMIPS64(HLoadString* instruction)
240 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700241
242 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700243 DCHECK(instruction_->IsLoadString());
244 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 LocationSummary* locations = instruction_->GetLocations();
246 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000247 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700248 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700249 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700250 __ Bind(GetEntryLabel());
251 SaveLiveRegisters(codegen, locations);
252
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000253 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100254 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700255 instruction_,
256 instruction_->GetDexPc(),
257 this);
258 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100260 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700262 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700263 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700264 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800265
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700266 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700267 }
268
Roland Levillain46648892015-06-19 16:07:18 +0100269 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
270
Alexey Frunze4dda3372015-06-01 18:31:49 -0700271 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700272 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
273};
274
275class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
276 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000277 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700278
279 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
280 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
281 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000282 if (instruction_->CanThrowIntoCatchBlock()) {
283 // Live registers will be restored in the catch block if caught.
284 SaveLiveRegisters(codegen, instruction_->GetLocations());
285 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100286 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700287 instruction_,
288 instruction_->GetDexPc(),
289 this);
290 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
291 }
292
Alexandre Rames8158f282015-08-07 10:26:17 +0100293 bool IsFatal() const OVERRIDE { return true; }
294
Roland Levillain46648892015-06-19 16:07:18 +0100295 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
296
Alexey Frunze4dda3372015-06-01 18:31:49 -0700297 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700298 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
299};
300
301class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
302 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100303 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000304 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700305
306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200307 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700308 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
309 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200310 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100311 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200313 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700317 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318 }
319 }
320
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700321 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 DCHECK(successor_ == nullptr);
323 return &return_label_;
324 }
325
Roland Levillain46648892015-06-19 16:07:18 +0100326 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
327
Chris Larsena2045912017-11-02 12:39:54 -0700328 HBasicBlock* GetSuccessor() const {
329 return successor_;
330 }
331
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700333 // If not null, the block to branch to after the suspend check.
334 HBasicBlock* const successor_;
335
336 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700337 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700338
339 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
340};
341
342class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
343 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800344 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
345 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700346
347 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
348 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800349
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 DCHECK(instruction_->IsCheckCast()
352 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
353 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
354
355 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800356 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800357 SaveLiveRegisters(codegen, locations);
358 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359
360 // We're moving two locations to locations that could overlap, so we need a parallel
361 // move resolver.
362 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100365 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800366 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100368 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700369 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100370 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800371 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100372 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700373 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
374 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700375 } else {
376 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800377 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
378 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 }
380
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800381 if (!is_fatal_) {
382 RestoreLiveRegisters(codegen, locations);
383 __ Bc(GetExitLabel());
384 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385 }
386
Roland Levillain46648892015-06-19 16:07:18 +0100387 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
388
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800389 bool IsFatal() const OVERRIDE { return is_fatal_; }
390
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800392 const bool is_fatal_;
393
Alexey Frunze4dda3372015-06-01 18:31:49 -0700394 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
395};
396
397class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
398 public:
Aart Bik42249c32016-01-07 15:33:50 -0800399 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000400 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401
402 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800403 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700404 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 LocationSummary* locations = instruction_->GetLocations();
406 SaveLiveRegisters(codegen, locations);
407 InvokeRuntimeCallingConvention calling_convention;
408 __ LoadConst32(calling_convention.GetRegisterAt(0),
409 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100410 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100411 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700412 }
413
Roland Levillain46648892015-06-19 16:07:18 +0100414 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
415
Alexey Frunze4dda3372015-06-01 18:31:49 -0700416 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700417 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
418};
419
Alexey Frunze15958152017-02-09 19:08:30 -0800420class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
421 public:
422 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
423
424 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
425 LocationSummary* locations = instruction_->GetLocations();
426 __ Bind(GetEntryLabel());
427 SaveLiveRegisters(codegen, locations);
428
429 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100430 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800431 parallel_move.AddMove(
432 locations->InAt(0),
433 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100434 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800435 nullptr);
436 parallel_move.AddMove(
437 locations->InAt(1),
438 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100439 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800440 nullptr);
441 parallel_move.AddMove(
442 locations->InAt(2),
443 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100444 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800445 nullptr);
446 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
447
448 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
449 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
450 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
451 RestoreLiveRegisters(codegen, locations);
452 __ Bc(GetExitLabel());
453 }
454
455 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
456
457 private:
458 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
459};
460
461// Slow path marking an object reference `ref` during a read
462// barrier. The field `obj.field` in the object `obj` holding this
463// reference does not get updated by this slow path after marking (see
464// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
465//
466// This means that after the execution of this slow path, `ref` will
467// always be up-to-date, but `obj.field` may not; i.e., after the
468// flip, `ref` will be a to-space reference, but `obj.field` will
469// probably still be a from-space reference (unless it gets updated by
470// another thread, or if another thread installed another object
471// reference (different from `ref`) in `obj.field`).
472//
473// If `entrypoint` is a valid location it is assumed to already be
474// holding the entrypoint. The case where the entrypoint is passed in
475// is for the GcRoot read barrier.
476class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
477 public:
478 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
479 Location ref,
480 Location entrypoint = Location::NoLocation())
481 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
482 DCHECK(kEmitCompilerReadBarrier);
483 }
484
485 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
486
487 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
488 LocationSummary* locations = instruction_->GetLocations();
489 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
490 DCHECK(locations->CanCall());
491 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
492 DCHECK(instruction_->IsInstanceFieldGet() ||
493 instruction_->IsStaticFieldGet() ||
494 instruction_->IsArrayGet() ||
495 instruction_->IsArraySet() ||
496 instruction_->IsLoadClass() ||
497 instruction_->IsLoadString() ||
498 instruction_->IsInstanceOf() ||
499 instruction_->IsCheckCast() ||
500 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
501 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
502 << "Unexpected instruction in read barrier marking slow path: "
503 << instruction_->DebugName();
504
505 __ Bind(GetEntryLabel());
506 // No need to save live registers; it's taken care of by the
507 // entrypoint. Also, there is no need to update the stack mask,
508 // as this runtime call will not trigger a garbage collection.
509 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
510 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
511 (S2 <= ref_reg && ref_reg <= S7) ||
512 (ref_reg == S8)) << ref_reg;
513 // "Compact" slow path, saving two moves.
514 //
515 // Instead of using the standard runtime calling convention (input
516 // and output in A0 and V0 respectively):
517 //
518 // A0 <- ref
519 // V0 <- ReadBarrierMark(A0)
520 // ref <- V0
521 //
522 // we just use rX (the register containing `ref`) as input and output
523 // of a dedicated entrypoint:
524 //
525 // rX <- ReadBarrierMarkRegX(rX)
526 //
527 if (entrypoint_.IsValid()) {
528 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
529 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
530 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
531 __ Nop();
532 } else {
533 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100534 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800535 // This runtime call does not require a stack map.
536 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
537 instruction_,
538 this);
539 }
540 __ Bc(GetExitLabel());
541 }
542
543 private:
544 // The location (register) of the marked object reference.
545 const Location ref_;
546
547 // The location of the entrypoint if already loaded.
548 const Location entrypoint_;
549
550 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
551};
552
553// Slow path marking an object reference `ref` during a read barrier,
554// and if needed, atomically updating the field `obj.field` in the
555// object `obj` holding this reference after marking (contrary to
556// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
557// `obj.field`).
558//
559// This means that after the execution of this slow path, both `ref`
560// and `obj.field` will be up-to-date; i.e., after the flip, both will
561// hold the same to-space reference (unless another thread installed
562// another object reference (different from `ref`) in `obj.field`).
563class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
564 public:
565 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
566 Location ref,
567 GpuRegister obj,
568 Location field_offset,
569 GpuRegister temp1)
570 : SlowPathCodeMIPS64(instruction),
571 ref_(ref),
572 obj_(obj),
573 field_offset_(field_offset),
574 temp1_(temp1) {
575 DCHECK(kEmitCompilerReadBarrier);
576 }
577
578 const char* GetDescription() const OVERRIDE {
579 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
580 }
581
582 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
583 LocationSummary* locations = instruction_->GetLocations();
584 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
585 DCHECK(locations->CanCall());
586 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
587 // This slow path is only used by the UnsafeCASObject intrinsic.
588 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
589 << "Unexpected instruction in read barrier marking and field updating slow path: "
590 << instruction_->DebugName();
591 DCHECK(instruction_->GetLocations()->Intrinsified());
592 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
593 DCHECK(field_offset_.IsRegister()) << field_offset_;
594
595 __ Bind(GetEntryLabel());
596
597 // Save the old reference.
598 // Note that we cannot use AT or TMP to save the old reference, as those
599 // are used by the code that follows, but we need the old reference after
600 // the call to the ReadBarrierMarkRegX entry point.
601 DCHECK_NE(temp1_, AT);
602 DCHECK_NE(temp1_, TMP);
603 __ Move(temp1_, ref_reg);
604
605 // No need to save live registers; it's taken care of by the
606 // entrypoint. Also, there is no need to update the stack mask,
607 // as this runtime call will not trigger a garbage collection.
608 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
609 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
610 (S2 <= ref_reg && ref_reg <= S7) ||
611 (ref_reg == S8)) << ref_reg;
612 // "Compact" slow path, saving two moves.
613 //
614 // Instead of using the standard runtime calling convention (input
615 // and output in A0 and V0 respectively):
616 //
617 // A0 <- ref
618 // V0 <- ReadBarrierMark(A0)
619 // ref <- V0
620 //
621 // we just use rX (the register containing `ref`) as input and output
622 // of a dedicated entrypoint:
623 //
624 // rX <- ReadBarrierMarkRegX(rX)
625 //
626 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100627 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800628 // This runtime call does not require a stack map.
629 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
630 instruction_,
631 this);
632
633 // If the new reference is different from the old reference,
634 // update the field in the holder (`*(obj_ + field_offset_)`).
635 //
636 // Note that this field could also hold a different object, if
637 // another thread had concurrently changed it. In that case, the
638 // the compare-and-set (CAS) loop below would abort, leaving the
639 // field as-is.
640 Mips64Label done;
641 __ Beqc(temp1_, ref_reg, &done);
642
643 // Update the the holder's field atomically. This may fail if
644 // mutator updates before us, but it's OK. This is achieved
645 // using a strong compare-and-set (CAS) operation with relaxed
646 // memory synchronization ordering, where the expected value is
647 // the old reference and the desired value is the new reference.
648
649 // Convenience aliases.
650 GpuRegister base = obj_;
651 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
652 GpuRegister expected = temp1_;
653 GpuRegister value = ref_reg;
654 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
655 GpuRegister tmp = AT; // Value in memory.
656
657 __ Daddu(tmp_ptr, base, offset);
658
659 if (kPoisonHeapReferences) {
660 __ PoisonHeapReference(expected);
661 // Do not poison `value` if it is the same register as
662 // `expected`, which has just been poisoned.
663 if (value != expected) {
664 __ PoisonHeapReference(value);
665 }
666 }
667
668 // do {
669 // tmp = [r_ptr] - expected;
670 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
671
672 Mips64Label loop_head, exit_loop;
673 __ Bind(&loop_head);
674 __ Ll(tmp, tmp_ptr);
675 // The LL instruction sign-extends the 32-bit value, but
676 // 32-bit references must be zero-extended. Zero-extend `tmp`.
677 __ Dext(tmp, tmp, 0, 32);
678 __ Bnec(tmp, expected, &exit_loop);
679 __ Move(tmp, value);
680 __ Sc(tmp, tmp_ptr);
681 __ Beqzc(tmp, &loop_head);
682 __ Bind(&exit_loop);
683
684 if (kPoisonHeapReferences) {
685 __ UnpoisonHeapReference(expected);
686 // Do not unpoison `value` if it is the same register as
687 // `expected`, which has just been unpoisoned.
688 if (value != expected) {
689 __ UnpoisonHeapReference(value);
690 }
691 }
692
693 __ Bind(&done);
694 __ Bc(GetExitLabel());
695 }
696
697 private:
698 // The location (register) of the marked object reference.
699 const Location ref_;
700 // The register containing the object holding the marked object reference field.
701 const GpuRegister obj_;
702 // The location of the offset of the marked reference field within `obj_`.
703 Location field_offset_;
704
705 const GpuRegister temp1_;
706
707 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
708};
709
710// Slow path generating a read barrier for a heap reference.
711class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
712 public:
713 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
714 Location out,
715 Location ref,
716 Location obj,
717 uint32_t offset,
718 Location index)
719 : SlowPathCodeMIPS64(instruction),
720 out_(out),
721 ref_(ref),
722 obj_(obj),
723 offset_(offset),
724 index_(index) {
725 DCHECK(kEmitCompilerReadBarrier);
726 // If `obj` is equal to `out` or `ref`, it means the initial object
727 // has been overwritten by (or after) the heap object reference load
728 // to be instrumented, e.g.:
729 //
730 // __ LoadFromOffset(kLoadWord, out, out, offset);
731 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
732 //
733 // In that case, we have lost the information about the original
734 // object, and the emitted read barrier cannot work properly.
735 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
736 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
737 }
738
739 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
740 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
741 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100742 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800743 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
744 DCHECK(locations->CanCall());
745 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
746 DCHECK(instruction_->IsInstanceFieldGet() ||
747 instruction_->IsStaticFieldGet() ||
748 instruction_->IsArrayGet() ||
749 instruction_->IsInstanceOf() ||
750 instruction_->IsCheckCast() ||
751 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
752 << "Unexpected instruction in read barrier for heap reference slow path: "
753 << instruction_->DebugName();
754
755 __ Bind(GetEntryLabel());
756 SaveLiveRegisters(codegen, locations);
757
758 // We may have to change the index's value, but as `index_` is a
759 // constant member (like other "inputs" of this slow path),
760 // introduce a copy of it, `index`.
761 Location index = index_;
762 if (index_.IsValid()) {
763 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
764 if (instruction_->IsArrayGet()) {
765 // Compute the actual memory offset and store it in `index`.
766 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
767 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
768 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
769 // We are about to change the value of `index_reg` (see the
770 // calls to art::mips64::Mips64Assembler::Sll and
771 // art::mips64::MipsAssembler::Addiu32 below), but it has
772 // not been saved by the previous call to
773 // art::SlowPathCode::SaveLiveRegisters, as it is a
774 // callee-save register --
775 // art::SlowPathCode::SaveLiveRegisters does not consider
776 // callee-save registers, as it has been designed with the
777 // assumption that callee-save registers are supposed to be
778 // handled by the called function. So, as a callee-save
779 // register, `index_reg` _would_ eventually be saved onto
780 // the stack, but it would be too late: we would have
781 // changed its value earlier. Therefore, we manually save
782 // it here into another freely available register,
783 // `free_reg`, chosen of course among the caller-save
784 // registers (as a callee-save `free_reg` register would
785 // exhibit the same problem).
786 //
787 // Note we could have requested a temporary register from
788 // the register allocator instead; but we prefer not to, as
789 // this is a slow path, and we know we can find a
790 // caller-save register that is available.
791 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
792 __ Move(free_reg, index_reg);
793 index_reg = free_reg;
794 index = Location::RegisterLocation(index_reg);
795 } else {
796 // The initial register stored in `index_` has already been
797 // saved in the call to art::SlowPathCode::SaveLiveRegisters
798 // (as it is not a callee-save register), so we can freely
799 // use it.
800 }
801 // Shifting the index value contained in `index_reg` by the scale
802 // factor (2) cannot overflow in practice, as the runtime is
803 // unable to allocate object arrays with a size larger than
804 // 2^26 - 1 (that is, 2^28 - 4 bytes).
805 __ Sll(index_reg, index_reg, TIMES_4);
806 static_assert(
807 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
808 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
809 __ Addiu32(index_reg, index_reg, offset_);
810 } else {
811 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
812 // intrinsics, `index_` is not shifted by a scale factor of 2
813 // (as in the case of ArrayGet), as it is actually an offset
814 // to an object field within an object.
815 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
816 DCHECK(instruction_->GetLocations()->Intrinsified());
817 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
818 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
819 << instruction_->AsInvoke()->GetIntrinsic();
820 DCHECK_EQ(offset_, 0U);
821 DCHECK(index_.IsRegister());
822 }
823 }
824
825 // We're moving two or three locations to locations that could
826 // overlap, so we need a parallel move resolver.
827 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100828 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800829 parallel_move.AddMove(ref_,
830 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100831 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800832 nullptr);
833 parallel_move.AddMove(obj_,
834 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100835 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800836 nullptr);
837 if (index.IsValid()) {
838 parallel_move.AddMove(index,
839 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100840 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800841 nullptr);
842 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
843 } else {
844 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
845 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
846 }
847 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
848 instruction_,
849 instruction_->GetDexPc(),
850 this);
851 CheckEntrypointTypes<
852 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
853 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
854
855 RestoreLiveRegisters(codegen, locations);
856 __ Bc(GetExitLabel());
857 }
858
859 const char* GetDescription() const OVERRIDE {
860 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
861 }
862
863 private:
864 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
865 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
866 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
867 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
868 if (i != ref &&
869 i != obj &&
870 !codegen->IsCoreCalleeSaveRegister(i) &&
871 !codegen->IsBlockedCoreRegister(i)) {
872 return static_cast<GpuRegister>(i);
873 }
874 }
875 // We shall never fail to find a free caller-save register, as
876 // there are more than two core caller-save registers on MIPS64
877 // (meaning it is possible to find one which is different from
878 // `ref` and `obj`).
879 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
880 LOG(FATAL) << "Could not find a free caller-save register";
881 UNREACHABLE();
882 }
883
884 const Location out_;
885 const Location ref_;
886 const Location obj_;
887 const uint32_t offset_;
888 // An additional location containing an index to an array.
889 // Only used for HArrayGet and the UnsafeGetObject &
890 // UnsafeGetObjectVolatile intrinsics.
891 const Location index_;
892
893 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
894};
895
896// Slow path generating a read barrier for a GC root.
897class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
898 public:
899 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
900 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
901 DCHECK(kEmitCompilerReadBarrier);
902 }
903
904 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
905 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100906 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800907 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
908 DCHECK(locations->CanCall());
909 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
910 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
911 << "Unexpected instruction in read barrier for GC root slow path: "
912 << instruction_->DebugName();
913
914 __ Bind(GetEntryLabel());
915 SaveLiveRegisters(codegen, locations);
916
917 InvokeRuntimeCallingConvention calling_convention;
918 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
919 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
920 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100921 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800922 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
923 instruction_,
924 instruction_->GetDexPc(),
925 this);
926 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
927 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
928
929 RestoreLiveRegisters(codegen, locations);
930 __ Bc(GetExitLabel());
931 }
932
933 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
934
935 private:
936 const Location out_;
937 const Location root_;
938
939 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
940};
941
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100943 const CompilerOptions& compiler_options,
944 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700945 : CodeGenerator(graph,
946 kNumberOfGpuRegisters,
947 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000948 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700949 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
950 arraysize(kCoreCalleeSaves)),
951 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
952 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100953 compiler_options,
954 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100955 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956 location_builder_(graph, this),
957 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100958 move_resolver_(graph->GetAllocator(), this),
Vladimir Markoa0431112018-06-25 09:32:54 +0100959 assembler_(graph->GetAllocator(),
960 compiler_options.GetInstructionSetFeatures()->AsMips64InstructionSetFeatures()),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800961 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800963 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100964 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000965 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100966 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000967 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100968 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000969 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +0100971 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800972 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100973 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800974 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100975 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976 // Save RA (containing the return address) to mimic Quick.
977 AddAllocatedRegister(Location::RegisterLocation(RA));
978}
979
980#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100981// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
982#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700983#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700984
985void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700986 // Ensure that we fix up branches.
987 __ FinalizeCode();
988
989 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100990 StackMapStream* stack_map_stream = GetStackMapStream();
991 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100992 uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700993 uint32_t new_position = __ GetAdjustedPosition(old_position);
994 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100995 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700996 }
997
998 // Adjust pc offsets for the disassembly information.
999 if (disasm_info_ != nullptr) {
1000 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1001 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1002 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1003 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1004 it.second.start = __ GetAdjustedPosition(it.second.start);
1005 it.second.end = __ GetAdjustedPosition(it.second.end);
1006 }
1007 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1008 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1009 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1010 }
1011 }
1012
Alexey Frunze4dda3372015-06-01 18:31:49 -07001013 CodeGenerator::Finalize(allocator);
1014}
1015
1016Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1017 return codegen_->GetAssembler();
1018}
1019
1020void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001021 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001022 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1023}
1024
1025void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001026 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001027 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1028}
1029
1030void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1031 // Pop reg
1032 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001033 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001034}
1035
1036void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1037 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001038 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001039 __ Sd(GpuRegister(reg), SP, 0);
1040}
1041
1042void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1043 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1044 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1045 // Allocate a scratch register other than TMP, if available.
1046 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1047 // automatically unspilled when the scratch scope object is destroyed).
1048 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1049 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001050 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001051 __ LoadFromOffset(load_type,
1052 GpuRegister(ensure_scratch.GetRegister()),
1053 SP,
1054 index1 + stack_offset);
1055 __ LoadFromOffset(load_type,
1056 TMP,
1057 SP,
1058 index2 + stack_offset);
1059 __ StoreToOffset(store_type,
1060 GpuRegister(ensure_scratch.GetRegister()),
1061 SP,
1062 index2 + stack_offset);
1063 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1064}
1065
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001066void ParallelMoveResolverMIPS64::ExchangeQuadSlots(int index1, int index2) {
1067 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, index1);
1068 __ LoadFpuFromOffset(kLoadQuadword, FTMP2, SP, index2);
1069 __ StoreFpuToOffset(kStoreQuadword, FTMP, SP, index2);
1070 __ StoreFpuToOffset(kStoreQuadword, FTMP2, SP, index1);
1071}
1072
Alexey Frunze4dda3372015-06-01 18:31:49 -07001073static dwarf::Reg DWARFReg(GpuRegister reg) {
1074 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1075}
1076
David Srbeckyba702002016-02-01 18:15:29 +00001077static dwarf::Reg DWARFReg(FpuRegister reg) {
1078 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1079}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001080
1081void CodeGeneratorMIPS64::GenerateFrameEntry() {
1082 __ Bind(&frame_entry_label_);
1083
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001084 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001085 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1086 __ Addiu(TMP, TMP, 1);
1087 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001088 }
1089
Vladimir Marko33bff252017-11-01 14:35:42 +00001090 bool do_overflow_check =
1091 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001092
1093 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001094 __ LoadFromOffset(
1095 kLoadWord,
1096 ZERO,
1097 SP,
1098 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001099 RecordPcInfo(nullptr, 0);
1100 }
1101
Alexey Frunze4dda3372015-06-01 18:31:49 -07001102 if (HasEmptyFrame()) {
1103 return;
1104 }
1105
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001107 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1108 LOG(FATAL) << "Stack frame larger than "
1109 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001110 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001111
1112 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001113
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001114 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001115 __ IncreaseFrameSize(ofs);
1116
1117 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1118 GpuRegister reg = kCoreCalleeSaves[i];
1119 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001120 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001121 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001122 __ cfi().RelOffset(DWARFReg(reg), ofs);
1123 }
1124 }
1125
1126 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1127 FpuRegister reg = kFpuCalleeSaves[i];
1128 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001129 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001130 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001131 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132 }
1133 }
1134
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001135 // Save the current method if we need it. Note that we do not
1136 // do this in HCurrentMethod, as the instruction might have been removed
1137 // in the SSA graph.
1138 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001139 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001140 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001141
1142 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1143 // Initialize should_deoptimize flag to 0.
1144 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1145 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001146}
1147
1148void CodeGeneratorMIPS64::GenerateFrameExit() {
1149 __ cfi().RememberState();
1150
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001152 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001154 // For better instruction scheduling restore RA before other registers.
1155 uint32_t ofs = GetFrameSize();
1156 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001157 GpuRegister reg = kCoreCalleeSaves[i];
1158 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001159 ofs -= kMips64DoublewordSize;
1160 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001161 __ cfi().Restore(DWARFReg(reg));
1162 }
1163 }
1164
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001165 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1166 FpuRegister reg = kFpuCalleeSaves[i];
1167 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1168 ofs -= kMips64DoublewordSize;
1169 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1170 __ cfi().Restore(DWARFReg(reg));
1171 }
1172 }
1173
1174 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001175 }
1176
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001177 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001178
1179 __ cfi().RestoreState();
1180 __ cfi().DefCFAOffset(GetFrameSize());
1181}
1182
1183void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1184 __ Bind(GetLabelOf(block));
1185}
1186
1187void CodeGeneratorMIPS64::MoveLocation(Location destination,
1188 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001189 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001190 if (source.Equals(destination)) {
1191 return;
1192 }
1193
1194 // A valid move can always be inferred from the destination and source
1195 // locations. When moving from and to a register, the argument type can be
1196 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001197 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001198 DCHECK_EQ(unspecified_type, false);
1199
1200 if (destination.IsRegister() || destination.IsFpuRegister()) {
1201 if (unspecified_type) {
1202 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1203 if (source.IsStackSlot() ||
1204 (src_cst != nullptr && (src_cst->IsIntConstant()
1205 || src_cst->IsFloatConstant()
1206 || src_cst->IsNullConstant()))) {
1207 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001208 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001209 } else {
1210 // If the source is a double stack slot or a 64bit constant, a 64bit
1211 // type is appropriate. Else the source is a register, and since the
1212 // type has not been specified, we chose a 64bit type to force a 64bit
1213 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 }
1216 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1218 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1220 // Move to GPR/FPR from stack
1221 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001222 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001223 __ LoadFpuFromOffset(load_type,
1224 destination.AsFpuRegister<FpuRegister>(),
1225 SP,
1226 source.GetStackIndex());
1227 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001228 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001229 __ LoadFromOffset(load_type,
1230 destination.AsRegister<GpuRegister>(),
1231 SP,
1232 source.GetStackIndex());
1233 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001234 } else if (source.IsSIMDStackSlot()) {
1235 __ LoadFpuFromOffset(kLoadQuadword,
1236 destination.AsFpuRegister<FpuRegister>(),
1237 SP,
1238 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 } else if (source.IsConstant()) {
1240 // Move to GPR/FPR from constant
1241 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001242 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001243 gpr = destination.AsRegister<GpuRegister>();
1244 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001246 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001248 gpr = ZERO;
1249 } else {
1250 __ LoadConst32(gpr, value);
1251 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001252 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001253 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001254 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001255 gpr = ZERO;
1256 } else {
1257 __ LoadConst64(gpr, value);
1258 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001259 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001260 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001261 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001262 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1264 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001265 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001266 if (destination.IsRegister()) {
1267 // Move to GPR from GPR
1268 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1269 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001270 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001271 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1273 } else {
1274 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1275 }
1276 }
1277 } else if (source.IsFpuRegister()) {
1278 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001279 if (GetGraph()->HasSIMD()) {
1280 __ MoveV(VectorRegisterFrom(destination),
1281 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001282 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001283 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001284 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001285 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1286 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001287 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001288 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1289 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291 } else {
1292 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001293 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001294 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1295 } else {
1296 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1297 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001298 }
1299 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001300 } else if (destination.IsSIMDStackSlot()) {
1301 if (source.IsFpuRegister()) {
1302 __ StoreFpuToOffset(kStoreQuadword,
1303 source.AsFpuRegister<FpuRegister>(),
1304 SP,
1305 destination.GetStackIndex());
1306 } else {
1307 DCHECK(source.IsSIMDStackSlot());
1308 __ LoadFpuFromOffset(kLoadQuadword,
1309 FTMP,
1310 SP,
1311 source.GetStackIndex());
1312 __ StoreFpuToOffset(kStoreQuadword,
1313 FTMP,
1314 SP,
1315 destination.GetStackIndex());
1316 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001317 } else { // The destination is not a register. It must be a stack slot.
1318 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1319 if (source.IsRegister() || source.IsFpuRegister()) {
1320 if (unspecified_type) {
1321 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001322 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001323 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001324 dst_type =
1325 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001326 }
1327 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001328 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1329 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 // Move to stack from GPR/FPR
1331 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1332 if (source.IsRegister()) {
1333 __ StoreToOffset(store_type,
1334 source.AsRegister<GpuRegister>(),
1335 SP,
1336 destination.GetStackIndex());
1337 } else {
1338 __ StoreFpuToOffset(store_type,
1339 source.AsFpuRegister<FpuRegister>(),
1340 SP,
1341 destination.GetStackIndex());
1342 }
1343 } else if (source.IsConstant()) {
1344 // Move to stack from constant
1345 HConstant* src_cst = source.GetConstant();
1346 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001347 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001348 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001349 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1350 if (value != 0) {
1351 gpr = TMP;
1352 __ LoadConst32(gpr, value);
1353 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001354 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001355 DCHECK(destination.IsDoubleStackSlot());
1356 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1357 if (value != 0) {
1358 gpr = TMP;
1359 __ LoadConst64(gpr, value);
1360 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001361 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001362 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001363 } else {
1364 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1365 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1366 // Move to stack from stack
1367 if (destination.IsStackSlot()) {
1368 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1369 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1370 } else {
1371 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1372 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1373 }
1374 }
1375 }
1376}
1377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001378void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001379 DCHECK(!loc1.IsConstant());
1380 DCHECK(!loc2.IsConstant());
1381
1382 if (loc1.Equals(loc2)) {
1383 return;
1384 }
1385
1386 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1387 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001388 bool is_simd1 = loc1.IsSIMDStackSlot();
1389 bool is_simd2 = loc2.IsSIMDStackSlot();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001390 bool is_fp_reg1 = loc1.IsFpuRegister();
1391 bool is_fp_reg2 = loc2.IsFpuRegister();
1392
1393 if (loc2.IsRegister() && loc1.IsRegister()) {
1394 // Swap 2 GPRs
1395 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1396 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1397 __ Move(TMP, r2);
1398 __ Move(r2, r1);
1399 __ Move(r1, TMP);
1400 } else if (is_fp_reg2 && is_fp_reg1) {
1401 // Swap 2 FPRs
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001402 if (GetGraph()->HasSIMD()) {
1403 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1404 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1405 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001406 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001407 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1408 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
1409 if (type == DataType::Type::kFloat32) {
1410 __ MovS(FTMP, r1);
1411 __ MovS(r1, r2);
1412 __ MovS(r2, FTMP);
1413 } else {
1414 DCHECK_EQ(type, DataType::Type::kFloat64);
1415 __ MovD(FTMP, r1);
1416 __ MovD(r1, r2);
1417 __ MovD(r2, FTMP);
1418 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001419 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001420 } else if (is_slot1 != is_slot2) {
1421 // Swap GPR/FPR and stack slot
1422 Location reg_loc = is_slot1 ? loc2 : loc1;
1423 Location mem_loc = is_slot1 ? loc1 : loc2;
1424 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1425 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001426 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001427 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1428 if (reg_loc.IsFpuRegister()) {
1429 __ StoreFpuToOffset(store_type,
1430 reg_loc.AsFpuRegister<FpuRegister>(),
1431 SP,
1432 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001433 if (mem_loc.IsStackSlot()) {
1434 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1435 } else {
1436 DCHECK(mem_loc.IsDoubleStackSlot());
1437 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1438 }
1439 } else {
1440 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1441 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1442 }
1443 } else if (is_slot1 && is_slot2) {
1444 move_resolver_.Exchange(loc1.GetStackIndex(),
1445 loc2.GetStackIndex(),
1446 loc1.IsDoubleStackSlot());
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001447 } else if (is_simd1 && is_simd2) {
1448 move_resolver_.ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
1449 } else if ((is_fp_reg1 && is_simd2) || (is_fp_reg2 && is_simd1)) {
1450 Location fp_reg_loc = is_fp_reg1 ? loc1 : loc2;
1451 Location mem_loc = is_fp_reg1 ? loc2 : loc1;
1452 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, mem_loc.GetStackIndex());
1453 __ StoreFpuToOffset(kStoreQuadword,
1454 fp_reg_loc.AsFpuRegister<FpuRegister>(),
1455 SP,
1456 mem_loc.GetStackIndex());
1457 __ MoveV(VectorRegisterFrom(fp_reg_loc), static_cast<VectorRegister>(FTMP));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001458 } else {
1459 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1460 }
1461}
1462
Calin Juravle175dc732015-08-25 15:42:32 +01001463void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1464 DCHECK(location.IsRegister());
1465 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1466}
1467
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1469 if (location.IsRegister()) {
1470 locations->AddTemp(location);
1471 } else {
1472 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1473 }
1474}
1475
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001476void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1477 GpuRegister value,
1478 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001479 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001480 GpuRegister card = AT;
1481 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001482 if (value_can_be_null) {
1483 __ Beqzc(value, &done);
1484 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001485 __ LoadFromOffset(kLoadDoubleword,
1486 card,
1487 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001488 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001489 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1490 __ Daddu(temp, card, temp);
1491 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001492 if (value_can_be_null) {
1493 __ Bind(&done);
1494 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001495}
1496
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001497template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001498inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1499 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001500 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001501 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001502 const DexFile* dex_file = info.target_dex_file;
Alexey Frunze19f6c692016-11-30 19:19:55 -08001503 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001504 DCHECK(info.label.IsBound());
1505 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1506 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1507 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001508 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001509 }
1510}
1511
Vladimir Marko6fd16062018-06-26 11:02:04 +01001512template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1513linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1514 const DexFile* target_dex_file,
1515 uint32_t pc_insn_offset,
1516 uint32_t boot_image_offset) {
1517 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1518 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001519}
1520
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001521void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001522 DCHECK(linker_patches->empty());
1523 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001524 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001525 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001526 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001527 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001528 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001529 string_bss_entry_patches_.size() +
1530 boot_image_intrinsic_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001531 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001532 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001533 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001534 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001535 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001536 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001537 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001538 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001539 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1540 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001541 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001542 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00001543 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001544 DCHECK(boot_image_type_patches_.empty());
1545 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001546 DCHECK(boot_image_intrinsic_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001547 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001548 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1549 method_bss_entry_patches_, linker_patches);
1550 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1551 type_bss_entry_patches_, linker_patches);
1552 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1553 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001554 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001555}
1556
Vladimir Marko6fd16062018-06-26 11:02:04 +01001557CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageIntrinsicPatch(
1558 uint32_t intrinsic_data,
1559 const PcRelativePatchInfo* info_high) {
1560 return NewPcRelativePatch(
1561 /* dex_file */ nullptr, intrinsic_data, info_high, &boot_image_intrinsic_patches_);
1562}
1563
Vladimir Markob066d432018-01-03 13:14:37 +00001564CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageRelRoPatch(
1565 uint32_t boot_image_offset,
1566 const PcRelativePatchInfo* info_high) {
1567 return NewPcRelativePatch(
1568 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1569}
1570
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001571CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001572 MethodReference target_method,
1573 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001574 return NewPcRelativePatch(
1575 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001576}
1577
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001578CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001579 MethodReference target_method,
1580 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001581 return NewPcRelativePatch(
1582 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001583}
1584
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001585CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001586 const DexFile& dex_file,
1587 dex::TypeIndex type_index,
1588 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001589 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001590}
1591
Vladimir Marko1998cd02017-01-13 13:02:58 +00001592CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001593 const DexFile& dex_file,
1594 dex::TypeIndex type_index,
1595 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001596 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001597}
1598
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001599CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001600 const DexFile& dex_file,
1601 dex::StringIndex string_index,
1602 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001603 return NewPcRelativePatch(
1604 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001605}
1606
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001607CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1608 const DexFile& dex_file,
1609 dex::StringIndex string_index,
1610 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001611 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001612}
1613
Alexey Frunze19f6c692016-11-30 19:19:55 -08001614CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001615 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001616 uint32_t offset_or_index,
1617 const PcRelativePatchInfo* info_high,
1618 ArenaDeque<PcRelativePatchInfo>* patches) {
1619 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001620 return &patches->back();
1621}
1622
Alexey Frunzef63f5692016-12-13 17:43:11 -08001623Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1624 return map->GetOrCreate(
1625 value,
1626 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1627}
1628
Alexey Frunze19f6c692016-11-30 19:19:55 -08001629Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1630 return uint64_literals_.GetOrCreate(
1631 value,
1632 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1633}
1634
Alexey Frunzef63f5692016-12-13 17:43:11 -08001635Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001636 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001637}
1638
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001639void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1640 GpuRegister out,
1641 PcRelativePatchInfo* info_low) {
1642 DCHECK(!info_high->patch_info_high);
1643 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001644 // Add the high half of a 32-bit offset to PC.
1645 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001646 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001647 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001648 if (info_low != nullptr) {
1649 DCHECK_EQ(info_low->patch_info_high, info_high);
1650 __ Bind(&info_low->label);
1651 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001652}
1653
Vladimir Marko6fd16062018-06-26 11:02:04 +01001654void CodeGeneratorMIPS64::LoadBootImageAddress(GpuRegister reg, uint32_t boot_image_reference) {
1655 if (GetCompilerOptions().IsBootImage()) {
1656 PcRelativePatchInfo* info_high = NewBootImageIntrinsicPatch(boot_image_reference);
1657 PcRelativePatchInfo* info_low = NewBootImageIntrinsicPatch(boot_image_reference, info_high);
1658 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
1659 __ Daddiu(reg, AT, /* placeholder */ 0x5678);
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001660 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001661 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_reference);
1662 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_reference, info_high);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001663 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
1664 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1665 __ Lwu(reg, AT, /* placeholder */ 0x5678);
1666 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001667 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001668 gc::Heap* heap = Runtime::Current()->GetHeap();
1669 DCHECK(!heap->GetBootImageSpaces().empty());
1670 uintptr_t address =
Vladimir Marko6fd16062018-06-26 11:02:04 +01001671 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001672 __ LoadLiteral(reg, kLoadDoubleword, DeduplicateBootImageAddressLiteral(address));
1673 }
1674}
1675
Vladimir Marko6fd16062018-06-26 11:02:04 +01001676void CodeGeneratorMIPS64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1677 uint32_t boot_image_offset) {
1678 DCHECK(invoke->IsStatic());
1679 InvokeRuntimeCallingConvention calling_convention;
1680 GpuRegister argument = calling_convention.GetRegisterAt(0);
1681 if (GetCompilerOptions().IsBootImage()) {
1682 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1683 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1684 MethodReference target_method = invoke->GetTargetMethod();
1685 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1686 PcRelativePatchInfo* info_high = NewBootImageTypePatch(*target_method.dex_file, type_idx);
1687 PcRelativePatchInfo* info_low =
1688 NewBootImageTypePatch(*target_method.dex_file, type_idx, info_high);
1689 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
1690 __ Daddiu(argument, AT, /* placeholder */ 0x5678);
1691 } else {
1692 LoadBootImageAddress(argument, boot_image_offset);
1693 }
1694 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1695 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1696}
1697
Alexey Frunze627c1a02017-01-30 19:28:14 -08001698Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1699 dex::StringIndex string_index,
1700 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001701 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001702 return jit_string_patches_.GetOrCreate(
1703 StringReference(&dex_file, string_index),
1704 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1705}
1706
1707Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1708 dex::TypeIndex type_index,
1709 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001710 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001711 return jit_class_patches_.GetOrCreate(
1712 TypeReference(&dex_file, type_index),
1713 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1714}
1715
1716void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1717 const uint8_t* roots_data,
1718 const Literal* literal,
1719 uint64_t index_in_table) const {
1720 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1721 uintptr_t address =
1722 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1723 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1724}
1725
1726void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1727 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001728 const StringReference& string_reference = entry.first;
1729 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001730 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001731 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001732 }
1733 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001734 const TypeReference& type_reference = entry.first;
1735 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001736 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001737 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001738 }
1739}
1740
David Brazdil58282f42016-01-14 12:45:10 +00001741void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001742 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1743 blocked_core_registers_[ZERO] = true;
1744 blocked_core_registers_[K0] = true;
1745 blocked_core_registers_[K1] = true;
1746 blocked_core_registers_[GP] = true;
1747 blocked_core_registers_[SP] = true;
1748 blocked_core_registers_[RA] = true;
1749
Lazar Trsicd9672662015-09-03 17:33:01 +02001750 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1751 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001752 blocked_core_registers_[AT] = true;
1753 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001754 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001755 blocked_fpu_registers_[FTMP] = true;
1756
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001757 if (GetInstructionSetFeatures().HasMsa()) {
1758 // To be used just for MSA instructions.
1759 blocked_fpu_registers_[FTMP2] = true;
1760 }
1761
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762 // Reserve suspend and thread registers.
1763 blocked_core_registers_[S0] = true;
1764 blocked_core_registers_[TR] = true;
1765
1766 // Reserve T9 for function calls
1767 blocked_core_registers_[T9] = true;
1768
Goran Jakovljevic782be112016-06-21 12:39:04 +02001769 if (GetGraph()->IsDebuggable()) {
1770 // Stubs do not save callee-save floating point registers. If the graph
1771 // is debuggable, we need to deal with these registers differently. For
1772 // now, just block them.
1773 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1774 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1775 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001776 }
1777}
1778
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1780 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001781 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001782}
1783
1784size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1785 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001786 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787}
1788
1789size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001790 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1791 FpuRegister(reg_id),
1792 SP,
1793 stack_index);
1794 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001795}
1796
1797size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001798 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1799 FpuRegister(reg_id),
1800 SP,
1801 stack_index);
1802 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803}
1804
1805void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001806 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001807}
1808
1809void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001810 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811}
1812
Vladimir Markoa0431112018-06-25 09:32:54 +01001813const Mips64InstructionSetFeatures& CodeGeneratorMIPS64::GetInstructionSetFeatures() const {
1814 return *GetCompilerOptions().GetInstructionSetFeatures()->AsMips64InstructionSetFeatures();
1815}
1816
Calin Juravle175dc732015-08-25 15:42:32 +01001817void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001818 HInstruction* instruction,
1819 uint32_t dex_pc,
1820 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001821 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001822 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001823 if (EntrypointRequiresStackMap(entrypoint)) {
1824 RecordPcInfo(instruction, dex_pc, slow_path);
1825 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001826}
1827
Alexey Frunze15958152017-02-09 19:08:30 -08001828void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1829 HInstruction* instruction,
1830 SlowPathCode* slow_path) {
1831 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1832 GenerateInvokeRuntime(entry_point_offset);
1833}
1834
1835void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1836 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1837 __ Jalr(T9);
1838 __ Nop();
1839}
1840
Alexey Frunze4dda3372015-06-01 18:31:49 -07001841void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1842 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001843 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1844 const size_t status_byte_offset =
1845 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1846 constexpr uint32_t shifted_initialized_value =
1847 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1848
1849 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001850 __ Sltiu(TMP, TMP, shifted_initialized_value);
1851 __ Bnezc(TMP, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001852 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1853 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001854 __ Bind(slow_path->GetExitLabel());
1855}
1856
Vladimir Marko175e7862018-03-27 09:03:13 +00001857void InstructionCodeGeneratorMIPS64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1858 GpuRegister temp) {
1859 uint32_t path_to_root = check->GetBitstringPathToRoot();
1860 uint32_t mask = check->GetBitstringMask();
1861 DCHECK(IsPowerOfTwo(mask + 1));
1862 size_t mask_bits = WhichPowerOf2(mask + 1);
1863
1864 if (mask_bits == 16u) {
1865 // Load only the bitstring part of the status word.
1866 __ LoadFromOffset(
1867 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1868 // Compare the bitstring bits using XOR.
1869 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1870 } else {
1871 // /* uint32_t */ temp = temp->status_
1872 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1873 // Compare the bitstring bits using XOR.
1874 if (IsUint<16>(path_to_root)) {
1875 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1876 } else {
1877 __ LoadConst32(TMP, path_to_root);
1878 __ Xor(temp, temp, TMP);
1879 }
1880 // Shift out bits that do not contribute to the comparison.
1881 __ Sll(temp, temp, 32 - mask_bits);
1882 }
1883}
1884
Alexey Frunze4dda3372015-06-01 18:31:49 -07001885void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1886 __ Sync(0); // only stype 0 is supported
1887}
1888
1889void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1890 HBasicBlock* successor) {
1891 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001892 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1893
1894 if (slow_path == nullptr) {
1895 slow_path =
1896 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1897 instruction->SetSlowPath(slow_path);
1898 codegen_->AddSlowPath(slow_path);
1899 if (successor != nullptr) {
1900 DCHECK(successor->IsLoopHeader());
1901 }
1902 } else {
1903 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1904 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001905
1906 __ LoadFromOffset(kLoadUnsignedHalfword,
1907 TMP,
1908 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001909 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001910 if (successor == nullptr) {
1911 __ Bnezc(TMP, slow_path->GetEntryLabel());
1912 __ Bind(slow_path->GetReturnLabel());
1913 } else {
1914 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001915 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001916 // slow_path will return to GetLabelOf(successor).
1917 }
1918}
1919
1920InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1921 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001922 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001923 assembler_(codegen->GetAssembler()),
1924 codegen_(codegen) {}
1925
1926void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1927 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001928 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001929 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001930 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kInt32:
1932 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001933 locations->SetInAt(0, Location::RequiresRegister());
1934 HInstruction* right = instruction->InputAt(1);
1935 bool can_use_imm = false;
1936 if (right->IsConstant()) {
1937 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1938 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1939 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001940 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001941 DCHECK(instruction->IsAdd() || instruction->IsSub());
1942 bool single_use = right->GetUses().HasExactlyOneElement();
1943 if (instruction->IsSub()) {
1944 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1945 imm = -imm;
1946 }
1947 }
1948 if (type == DataType::Type::kInt32) {
1949 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1950 } else {
1951 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1952 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001953 }
1954 }
1955 if (can_use_imm)
1956 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1957 else
1958 locations->SetInAt(1, Location::RequiresRegister());
1959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1960 }
1961 break;
1962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001963 case DataType::Type::kFloat32:
1964 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001965 locations->SetInAt(0, Location::RequiresFpuRegister());
1966 locations->SetInAt(1, Location::RequiresFpuRegister());
1967 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1968 break;
1969
1970 default:
1971 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1972 }
1973}
1974
1975void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001977 LocationSummary* locations = instruction->GetLocations();
1978
1979 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 case DataType::Type::kInt32:
1981 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001982 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1983 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1984 Location rhs_location = locations->InAt(1);
1985
1986 GpuRegister rhs_reg = ZERO;
1987 int64_t rhs_imm = 0;
1988 bool use_imm = rhs_location.IsConstant();
1989 if (use_imm) {
1990 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1991 } else {
1992 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1993 }
1994
1995 if (instruction->IsAnd()) {
1996 if (use_imm)
1997 __ Andi(dst, lhs, rhs_imm);
1998 else
1999 __ And(dst, lhs, rhs_reg);
2000 } else if (instruction->IsOr()) {
2001 if (use_imm)
2002 __ Ori(dst, lhs, rhs_imm);
2003 else
2004 __ Or(dst, lhs, rhs_reg);
2005 } else if (instruction->IsXor()) {
2006 if (use_imm)
2007 __ Xori(dst, lhs, rhs_imm);
2008 else
2009 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002010 } else if (instruction->IsAdd() || instruction->IsSub()) {
2011 if (instruction->IsSub()) {
2012 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002013 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01002015 if (use_imm) {
2016 if (IsInt<16>(rhs_imm)) {
2017 __ Addiu(dst, lhs, rhs_imm);
2018 } else {
2019 int16_t rhs_imm_high = High16Bits(rhs_imm);
2020 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2021 if (rhs_imm_low < 0) {
2022 rhs_imm_high += 1;
2023 }
2024 __ Aui(dst, lhs, rhs_imm_high);
2025 if (rhs_imm_low != 0) {
2026 __ Addiu(dst, dst, rhs_imm_low);
2027 }
2028 }
2029 } else {
2030 if (instruction->IsAdd()) {
2031 __ Addu(dst, lhs, rhs_reg);
2032 } else {
2033 DCHECK(instruction->IsSub());
2034 __ Subu(dst, lhs, rhs_reg);
2035 }
2036 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002037 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002038 if (use_imm) {
2039 if (IsInt<16>(rhs_imm)) {
2040 __ Daddiu(dst, lhs, rhs_imm);
2041 } else if (IsInt<32>(rhs_imm)) {
2042 int16_t rhs_imm_high = High16Bits(rhs_imm);
2043 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2044 bool overflow_hi16 = false;
2045 if (rhs_imm_low < 0) {
2046 rhs_imm_high += 1;
2047 overflow_hi16 = (rhs_imm_high == -32768);
2048 }
2049 __ Daui(dst, lhs, rhs_imm_high);
2050 if (rhs_imm_low != 0) {
2051 __ Daddiu(dst, dst, rhs_imm_low);
2052 }
2053 if (overflow_hi16) {
2054 __ Dahi(dst, 1);
2055 }
2056 } else {
2057 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
2058 if (rhs_imm_low < 0) {
2059 rhs_imm += (INT64_C(1) << 16);
2060 }
2061 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
2062 if (rhs_imm_upper < 0) {
2063 rhs_imm += (INT64_C(1) << 32);
2064 }
2065 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
2066 if (rhs_imm_high < 0) {
2067 rhs_imm += (INT64_C(1) << 48);
2068 }
2069 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
2070 GpuRegister tmp = lhs;
2071 if (rhs_imm_low != 0) {
2072 __ Daddiu(dst, tmp, rhs_imm_low);
2073 tmp = dst;
2074 }
2075 // Dahi and Dati must use the same input and output register, so we have to initialize
2076 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
2077 // Daui(dst, lhs, 0).
2078 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
2079 __ Daui(dst, tmp, rhs_imm_upper);
2080 }
2081 if (rhs_imm_high != 0) {
2082 __ Dahi(dst, rhs_imm_high);
2083 }
2084 if (rhs_imm_top != 0) {
2085 __ Dati(dst, rhs_imm_top);
2086 }
2087 }
2088 } else if (instruction->IsAdd()) {
2089 __ Daddu(dst, lhs, rhs_reg);
2090 } else {
2091 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002092 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002093 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002094 }
2095 }
2096 break;
2097 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 case DataType::Type::kFloat32:
2099 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002100 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2101 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2102 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2103 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002105 __ AddS(dst, lhs, rhs);
2106 else
2107 __ AddD(dst, lhs, rhs);
2108 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002110 __ SubS(dst, lhs, rhs);
2111 else
2112 __ SubD(dst, lhs, rhs);
2113 } else {
2114 LOG(FATAL) << "Unexpected floating-point binary operation";
2115 }
2116 break;
2117 }
2118 default:
2119 LOG(FATAL) << "Unexpected binary operation type " << type;
2120 }
2121}
2122
2123void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002124 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002125
Vladimir Markoca6fff82017-10-03 14:49:14 +01002126 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002128 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002129 case DataType::Type::kInt32:
2130 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 locations->SetInAt(0, Location::RequiresRegister());
2132 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002134 break;
2135 }
2136 default:
2137 LOG(FATAL) << "Unexpected shift type " << type;
2138 }
2139}
2140
2141void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002142 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002143 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002145
2146 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 case DataType::Type::kInt32:
2148 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002149 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2150 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2151 Location rhs_location = locations->InAt(1);
2152
2153 GpuRegister rhs_reg = ZERO;
2154 int64_t rhs_imm = 0;
2155 bool use_imm = rhs_location.IsConstant();
2156 if (use_imm) {
2157 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2158 } else {
2159 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2160 }
2161
2162 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002163 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002165
Alexey Frunze92d90602015-12-18 18:16:36 -08002166 if (shift_value == 0) {
2167 if (dst != lhs) {
2168 __ Move(dst, lhs);
2169 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002170 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002171 if (instr->IsShl()) {
2172 __ Sll(dst, lhs, shift_value);
2173 } else if (instr->IsShr()) {
2174 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002175 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002176 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002177 } else {
2178 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 }
2180 } else {
2181 if (shift_value < 32) {
2182 if (instr->IsShl()) {
2183 __ Dsll(dst, lhs, shift_value);
2184 } else if (instr->IsShr()) {
2185 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002186 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002187 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002188 } else {
2189 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 }
2191 } else {
2192 shift_value -= 32;
2193 if (instr->IsShl()) {
2194 __ Dsll32(dst, lhs, shift_value);
2195 } else if (instr->IsShr()) {
2196 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002197 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002198 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002199 } else {
2200 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002201 }
2202 }
2203 }
2204 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002205 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002206 if (instr->IsShl()) {
2207 __ Sllv(dst, lhs, rhs_reg);
2208 } else if (instr->IsShr()) {
2209 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002210 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002211 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002212 } else {
2213 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002214 }
2215 } else {
2216 if (instr->IsShl()) {
2217 __ Dsllv(dst, lhs, rhs_reg);
2218 } else if (instr->IsShr()) {
2219 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002220 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002222 } else {
2223 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002224 }
2225 }
2226 }
2227 break;
2228 }
2229 default:
2230 LOG(FATAL) << "Unexpected shift operation type " << type;
2231 }
2232}
2233
2234void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2235 HandleBinaryOp(instruction);
2236}
2237
2238void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2239 HandleBinaryOp(instruction);
2240}
2241
2242void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2243 HandleBinaryOp(instruction);
2244}
2245
2246void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2247 HandleBinaryOp(instruction);
2248}
2249
2250void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002251 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002252 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002253 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002254 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002255 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2256 object_array_get_with_read_barrier
2257 ? LocationSummary::kCallOnSlowPath
2258 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002259 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2260 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2261 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002262 locations->SetInAt(0, Location::RequiresRegister());
2263 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002265 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2266 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002267 // The output overlaps in the case of an object array get with
2268 // read barriers enabled: we do not want the move to overwrite the
2269 // array's location, as we need it to emit the read barrier.
2270 locations->SetOut(Location::RequiresRegister(),
2271 object_array_get_with_read_barrier
2272 ? Location::kOutputOverlap
2273 : Location::kNoOutputOverlap);
2274 }
2275 // We need a temporary register for the read barrier marking slow
2276 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2277 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002278 bool temp_needed = instruction->GetIndex()->IsConstant()
2279 ? !kBakerReadBarrierThunksEnableForFields
2280 : !kBakerReadBarrierThunksEnableForArrays;
2281 if (temp_needed) {
2282 locations->AddTemp(Location::RequiresRegister());
2283 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002284 }
2285}
2286
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002287static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2288 auto null_checker = [codegen, instruction]() {
2289 codegen->MaybeRecordImplicitNullCheck(instruction);
2290 };
2291 return null_checker;
2292}
2293
Alexey Frunze4dda3372015-06-01 18:31:49 -07002294void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2295 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002296 Location obj_loc = locations->InAt(0);
2297 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2298 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002299 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002300 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002301 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002304 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2305 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002306 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002307 case DataType::Type::kBool:
2308 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002309 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002310 if (index.IsConstant()) {
2311 size_t offset =
2312 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002313 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002314 } else {
2315 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002316 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002317 }
2318 break;
2319 }
2320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002322 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002323 if (index.IsConstant()) {
2324 size_t offset =
2325 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002326 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002327 } else {
2328 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002329 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002330 }
2331 break;
2332 }
2333
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002334 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002335 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002336 if (maybe_compressed_char_at) {
2337 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002338 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002339 __ Dext(TMP, TMP, 0, 1);
2340 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2341 "Expecting 0=compressed, 1=uncompressed");
2342 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002344 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2345 if (maybe_compressed_char_at) {
2346 Mips64Label uncompressed_load, done;
2347 __ Bnezc(TMP, &uncompressed_load);
2348 __ LoadFromOffset(kLoadUnsignedByte,
2349 out,
2350 obj,
2351 data_offset + (const_index << TIMES_1));
2352 __ Bc(&done);
2353 __ Bind(&uncompressed_load);
2354 __ LoadFromOffset(kLoadUnsignedHalfword,
2355 out,
2356 obj,
2357 data_offset + (const_index << TIMES_2));
2358 __ Bind(&done);
2359 } else {
2360 __ LoadFromOffset(kLoadUnsignedHalfword,
2361 out,
2362 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002363 data_offset + (const_index << TIMES_2),
2364 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002365 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002366 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002367 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2368 if (maybe_compressed_char_at) {
2369 Mips64Label uncompressed_load, done;
2370 __ Bnezc(TMP, &uncompressed_load);
2371 __ Daddu(TMP, obj, index_reg);
2372 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2373 __ Bc(&done);
2374 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002375 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002376 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2377 __ Bind(&done);
2378 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002379 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002380 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002381 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002382 }
2383 break;
2384 }
2385
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002386 case DataType::Type::kInt16: {
2387 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2388 if (index.IsConstant()) {
2389 size_t offset =
2390 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2391 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2392 } else {
2393 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2394 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2395 }
2396 break;
2397 }
2398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002401 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002402 LoadOperandType load_type =
2403 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002404 if (index.IsConstant()) {
2405 size_t offset =
2406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002407 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002408 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002409 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002410 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002411 }
2412 break;
2413 }
2414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002416 static_assert(
2417 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2418 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2419 // /* HeapReference<Object> */ out =
2420 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2421 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002422 bool temp_needed = index.IsConstant()
2423 ? !kBakerReadBarrierThunksEnableForFields
2424 : !kBakerReadBarrierThunksEnableForArrays;
2425 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002426 // Note that a potential implicit null check is handled in this
2427 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002428 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2429 if (index.IsConstant()) {
2430 // Array load with a constant index can be treated as a field load.
2431 size_t offset =
2432 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2433 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2434 out_loc,
2435 obj,
2436 offset,
2437 temp,
2438 /* needs_null_check */ false);
2439 } else {
2440 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2441 out_loc,
2442 obj,
2443 data_offset,
2444 index,
2445 temp,
2446 /* needs_null_check */ false);
2447 }
Alexey Frunze15958152017-02-09 19:08:30 -08002448 } else {
2449 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2450 if (index.IsConstant()) {
2451 size_t offset =
2452 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2453 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2454 // If read barriers are enabled, emit read barriers other than
2455 // Baker's using a slow path (and also unpoison the loaded
2456 // reference, if heap poisoning is enabled).
2457 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2458 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002459 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002460 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2461 // If read barriers are enabled, emit read barriers other than
2462 // Baker's using a slow path (and also unpoison the loaded
2463 // reference, if heap poisoning is enabled).
2464 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2465 out_loc,
2466 out_loc,
2467 obj_loc,
2468 data_offset,
2469 index);
2470 }
2471 }
2472 break;
2473 }
2474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002476 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002477 if (index.IsConstant()) {
2478 size_t offset =
2479 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002480 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002482 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002483 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002484 }
2485 break;
2486 }
2487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002488 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002489 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002490 if (index.IsConstant()) {
2491 size_t offset =
2492 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002493 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002494 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002495 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002496 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002497 }
2498 break;
2499 }
2500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002502 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002503 if (index.IsConstant()) {
2504 size_t offset =
2505 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002506 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002507 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002508 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002509 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002510 }
2511 break;
2512 }
2513
Aart Bik66c158e2018-01-31 12:55:04 -08002514 case DataType::Type::kUint32:
2515 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2518 UNREACHABLE();
2519 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002520}
2521
2522void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002523 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002524 locations->SetInAt(0, Location::RequiresRegister());
2525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2526}
2527
2528void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2529 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002530 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002531 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2532 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2533 __ LoadFromOffset(kLoadWord, out, obj, offset);
2534 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002535 // Mask out compression flag from String's array length.
2536 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2537 __ Srl(out, out, 1u);
2538 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002539}
2540
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002541Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2542 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2543 ? Location::ConstantLocation(instruction->AsConstant())
2544 : Location::RequiresRegister();
2545}
2546
2547Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2548 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2549 // We can store a non-zero float or double constant without first loading it into the FPU,
2550 // but we should only prefer this if the constant has a single use.
2551 if (instruction->IsConstant() &&
2552 (instruction->AsConstant()->IsZeroBitPattern() ||
2553 instruction->GetUses().HasExactlyOneElement())) {
2554 return Location::ConstantLocation(instruction->AsConstant());
2555 // Otherwise fall through and require an FPU register for the constant.
2556 }
2557 return Location::RequiresFpuRegister();
2558}
2559
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002562
2563 bool needs_write_barrier =
2564 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2565 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2566
Vladimir Markoca6fff82017-10-03 14:49:14 +01002567 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002568 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002569 may_need_runtime_call_for_type_check ?
2570 LocationSummary::kCallOnSlowPath :
2571 LocationSummary::kNoCall);
2572
2573 locations->SetInAt(0, Location::RequiresRegister());
2574 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002576 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002577 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002578 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2579 }
2580 if (needs_write_barrier) {
2581 // Temporary register for the write barrier.
2582 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002583 }
2584}
2585
2586void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2587 LocationSummary* locations = instruction->GetLocations();
2588 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2589 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002590 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002592 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002593 bool needs_write_barrier =
2594 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002595 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002596 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002597
2598 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002599 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002600 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002601 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002604 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002605 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002606 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2607 }
2608 if (value_location.IsConstant()) {
2609 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2610 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2611 } else {
2612 GpuRegister value = value_location.AsRegister<GpuRegister>();
2613 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002614 }
2615 break;
2616 }
2617
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002618 case DataType::Type::kUint16:
2619 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002620 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002621 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002622 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002623 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002624 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002625 }
2626 if (value_location.IsConstant()) {
2627 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2628 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2629 } else {
2630 GpuRegister value = value_location.AsRegister<GpuRegister>();
2631 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002632 }
2633 break;
2634 }
2635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002637 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2638 if (index.IsConstant()) {
2639 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2640 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002641 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002642 }
2643 if (value_location.IsConstant()) {
2644 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2645 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2646 } else {
2647 GpuRegister value = value_location.AsRegister<GpuRegister>();
2648 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2649 }
2650 break;
2651 }
2652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002653 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002654 if (value_location.IsConstant()) {
2655 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002656 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002658 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002659 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002660 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002661 }
Alexey Frunze15958152017-02-09 19:08:30 -08002662 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2663 DCHECK_EQ(value, 0);
2664 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2665 DCHECK(!needs_write_barrier);
2666 DCHECK(!may_need_runtime_call_for_type_check);
2667 break;
2668 }
2669
2670 DCHECK(needs_write_barrier);
2671 GpuRegister value = value_location.AsRegister<GpuRegister>();
2672 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2673 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2674 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2675 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2676 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2677 Mips64Label done;
2678 SlowPathCodeMIPS64* slow_path = nullptr;
2679
2680 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002681 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002682 codegen_->AddSlowPath(slow_path);
2683 if (instruction->GetValueCanBeNull()) {
2684 Mips64Label non_zero;
2685 __ Bnezc(value, &non_zero);
2686 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2687 if (index.IsConstant()) {
2688 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002689 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002690 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002691 }
Alexey Frunze15958152017-02-09 19:08:30 -08002692 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2693 __ Bc(&done);
2694 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002695 }
Alexey Frunze15958152017-02-09 19:08:30 -08002696
2697 // Note that when read barriers are enabled, the type checks
2698 // are performed without read barriers. This is fine, even in
2699 // the case where a class object is in the from-space after
2700 // the flip, as a comparison involving such a type would not
2701 // produce a false positive; it may of course produce a false
2702 // negative, in which case we would take the ArraySet slow
2703 // path.
2704
2705 // /* HeapReference<Class> */ temp1 = obj->klass_
2706 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2707 __ MaybeUnpoisonHeapReference(temp1);
2708
2709 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2710 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2711 // /* HeapReference<Class> */ temp2 = value->klass_
2712 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2713 // If heap poisoning is enabled, no need to unpoison `temp1`
2714 // nor `temp2`, as we are comparing two poisoned references.
2715
2716 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2717 Mips64Label do_put;
2718 __ Beqc(temp1, temp2, &do_put);
2719 // If heap poisoning is enabled, the `temp1` reference has
2720 // not been unpoisoned yet; unpoison it now.
2721 __ MaybeUnpoisonHeapReference(temp1);
2722
2723 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2724 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2725 // If heap poisoning is enabled, no need to unpoison
2726 // `temp1`, as we are comparing against null below.
2727 __ Bnezc(temp1, slow_path->GetEntryLabel());
2728 __ Bind(&do_put);
2729 } else {
2730 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2731 }
2732 }
2733
2734 GpuRegister source = value;
2735 if (kPoisonHeapReferences) {
2736 // Note that in the case where `value` is a null reference,
2737 // we do not enter this block, as a null reference does not
2738 // need poisoning.
2739 __ Move(temp1, value);
2740 __ PoisonHeapReference(temp1);
2741 source = temp1;
2742 }
2743
2744 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2745 if (index.IsConstant()) {
2746 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002747 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002748 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002749 }
2750 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2751
2752 if (!may_need_runtime_call_for_type_check) {
2753 codegen_->MaybeRecordImplicitNullCheck(instruction);
2754 }
2755
2756 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2757
2758 if (done.IsLinked()) {
2759 __ Bind(&done);
2760 }
2761
2762 if (slow_path != nullptr) {
2763 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002764 }
2765 break;
2766 }
2767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002768 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002770 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002771 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002772 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002773 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002774 }
2775 if (value_location.IsConstant()) {
2776 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2777 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2778 } else {
2779 GpuRegister value = value_location.AsRegister<GpuRegister>();
2780 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002781 }
2782 break;
2783 }
2784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002786 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002787 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002788 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002789 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002790 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002791 }
2792 if (value_location.IsConstant()) {
2793 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2794 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2795 } else {
2796 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2797 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002798 }
2799 break;
2800 }
2801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002803 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002804 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002805 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002806 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002807 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002808 }
2809 if (value_location.IsConstant()) {
2810 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2811 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2812 } else {
2813 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2814 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002815 }
2816 break;
2817 }
2818
Aart Bik66c158e2018-01-31 12:55:04 -08002819 case DataType::Type::kUint32:
2820 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002822 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2823 UNREACHABLE();
2824 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002825}
2826
2827void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002828 RegisterSet caller_saves = RegisterSet::Empty();
2829 InvokeRuntimeCallingConvention calling_convention;
2830 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2831 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2832 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002833
2834 HInstruction* index = instruction->InputAt(0);
2835 HInstruction* length = instruction->InputAt(1);
2836
2837 bool const_index = false;
2838 bool const_length = false;
2839
2840 if (index->IsConstant()) {
2841 if (length->IsConstant()) {
2842 const_index = true;
2843 const_length = true;
2844 } else {
2845 int32_t index_value = index->AsIntConstant()->GetValue();
2846 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2847 const_index = true;
2848 }
2849 }
2850 } else if (length->IsConstant()) {
2851 int32_t length_value = length->AsIntConstant()->GetValue();
2852 if (IsUint<15>(length_value)) {
2853 const_length = true;
2854 }
2855 }
2856
2857 locations->SetInAt(0, const_index
2858 ? Location::ConstantLocation(index->AsConstant())
2859 : Location::RequiresRegister());
2860 locations->SetInAt(1, const_length
2861 ? Location::ConstantLocation(length->AsConstant())
2862 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002863}
2864
2865void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2866 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002867 Location index_loc = locations->InAt(0);
2868 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002869
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002870 if (length_loc.IsConstant()) {
2871 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2872 if (index_loc.IsConstant()) {
2873 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2874 if (index < 0 || index >= length) {
2875 BoundsCheckSlowPathMIPS64* slow_path =
2876 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2877 codegen_->AddSlowPath(slow_path);
2878 __ Bc(slow_path->GetEntryLabel());
2879 } else {
2880 // Nothing to be done.
2881 }
2882 return;
2883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002884
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002885 BoundsCheckSlowPathMIPS64* slow_path =
2886 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2887 codegen_->AddSlowPath(slow_path);
2888 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2889 if (length == 0) {
2890 __ Bc(slow_path->GetEntryLabel());
2891 } else if (length == 1) {
2892 __ Bnezc(index, slow_path->GetEntryLabel());
2893 } else {
2894 DCHECK(IsUint<15>(length)) << length;
2895 __ Sltiu(TMP, index, length);
2896 __ Beqzc(TMP, slow_path->GetEntryLabel());
2897 }
2898 } else {
2899 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2900 BoundsCheckSlowPathMIPS64* slow_path =
2901 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2902 codegen_->AddSlowPath(slow_path);
2903 if (index_loc.IsConstant()) {
2904 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2905 if (index < 0) {
2906 __ Bc(slow_path->GetEntryLabel());
2907 } else if (index == 0) {
2908 __ Blezc(length, slow_path->GetEntryLabel());
2909 } else {
2910 DCHECK(IsInt<16>(index + 1)) << index;
2911 __ Sltiu(TMP, length, index + 1);
2912 __ Bnezc(TMP, slow_path->GetEntryLabel());
2913 }
2914 } else {
2915 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2916 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2917 }
2918 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002919}
2920
Alexey Frunze15958152017-02-09 19:08:30 -08002921// Temp is used for read barrier.
2922static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2923 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002924 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002925 (kUseBakerReadBarrier ||
2926 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2927 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2928 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2929 return 1;
2930 }
2931 return 0;
2932}
2933
2934// Extra temp is used for read barrier.
2935static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2936 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2937}
2938
Alexey Frunze4dda3372015-06-01 18:31:49 -07002939void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002940 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002941 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002942 LocationSummary* locations =
2943 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002944 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00002945 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
2946 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2947 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2948 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
2949 } else {
2950 locations->SetInAt(1, Location::RequiresRegister());
2951 }
Alexey Frunze15958152017-02-09 19:08:30 -08002952 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002953}
2954
2955void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002956 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002957 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002958 Location obj_loc = locations->InAt(0);
2959 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00002960 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08002961 Location temp_loc = locations->GetTemp(0);
2962 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2963 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2964 DCHECK_LE(num_temps, 2u);
2965 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002966 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2967 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2968 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2969 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2970 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2971 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2972 const uint32_t object_array_data_offset =
2973 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2974 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002975
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002976 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002977 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002978 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2979 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002980 codegen_->AddSlowPath(slow_path);
2981
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002982 // Avoid this check if we know `obj` is not null.
2983 if (instruction->MustDoNullCheck()) {
2984 __ Beqzc(obj, &done);
2985 }
2986
2987 switch (type_check_kind) {
2988 case TypeCheckKind::kExactCheck:
2989 case TypeCheckKind::kArrayCheck: {
2990 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002991 GenerateReferenceLoadTwoRegisters(instruction,
2992 temp_loc,
2993 obj_loc,
2994 class_offset,
2995 maybe_temp2_loc,
2996 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002997 // Jump to slow path for throwing the exception or doing a
2998 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00002999 __ Bnec(temp, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003000 break;
3001 }
3002
3003 case TypeCheckKind::kAbstractClassCheck: {
3004 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003005 GenerateReferenceLoadTwoRegisters(instruction,
3006 temp_loc,
3007 obj_loc,
3008 class_offset,
3009 maybe_temp2_loc,
3010 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003011 // If the class is abstract, we eagerly fetch the super class of the
3012 // object to avoid doing a comparison we know will fail.
3013 Mips64Label loop;
3014 __ Bind(&loop);
3015 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003016 GenerateReferenceLoadOneRegister(instruction,
3017 temp_loc,
3018 super_offset,
3019 maybe_temp2_loc,
3020 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003021 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3022 // exception.
3023 __ Beqzc(temp, slow_path->GetEntryLabel());
3024 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00003025 __ Bnec(temp, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003026 break;
3027 }
3028
3029 case TypeCheckKind::kClassHierarchyCheck: {
3030 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003031 GenerateReferenceLoadTwoRegisters(instruction,
3032 temp_loc,
3033 obj_loc,
3034 class_offset,
3035 maybe_temp2_loc,
3036 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003037 // Walk over the class hierarchy to find a match.
3038 Mips64Label loop;
3039 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00003040 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003041 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003042 GenerateReferenceLoadOneRegister(instruction,
3043 temp_loc,
3044 super_offset,
3045 maybe_temp2_loc,
3046 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003047 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3048 // exception. Otherwise, jump to the beginning of the loop.
3049 __ Bnezc(temp, &loop);
3050 __ Bc(slow_path->GetEntryLabel());
3051 break;
3052 }
3053
3054 case TypeCheckKind::kArrayObjectCheck: {
3055 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003056 GenerateReferenceLoadTwoRegisters(instruction,
3057 temp_loc,
3058 obj_loc,
3059 class_offset,
3060 maybe_temp2_loc,
3061 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003062 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003063 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003064 // Otherwise, we need to check that the object's class is a non-primitive array.
3065 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003066 GenerateReferenceLoadOneRegister(instruction,
3067 temp_loc,
3068 component_offset,
3069 maybe_temp2_loc,
3070 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003071 // If the component type is null, jump to the slow path to throw the exception.
3072 __ Beqzc(temp, slow_path->GetEntryLabel());
3073 // Otherwise, the object is indeed an array, further check that this component
3074 // type is not a primitive type.
3075 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3076 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3077 __ Bnezc(temp, slow_path->GetEntryLabel());
3078 break;
3079 }
3080
3081 case TypeCheckKind::kUnresolvedCheck:
3082 // We always go into the type check slow path for the unresolved check case.
3083 // We cannot directly call the CheckCast runtime entry point
3084 // without resorting to a type checking slow path here (i.e. by
3085 // calling InvokeRuntime directly), as it would require to
3086 // assign fixed registers for the inputs of this HInstanceOf
3087 // instruction (following the runtime calling convention), which
3088 // might be cluttered by the potential first read barrier
3089 // emission at the beginning of this method.
3090 __ Bc(slow_path->GetEntryLabel());
3091 break;
3092
3093 case TypeCheckKind::kInterfaceCheck: {
3094 // Avoid read barriers to improve performance of the fast path. We can not get false
3095 // positives by doing this.
3096 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003097 GenerateReferenceLoadTwoRegisters(instruction,
3098 temp_loc,
3099 obj_loc,
3100 class_offset,
3101 maybe_temp2_loc,
3102 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003103 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003104 GenerateReferenceLoadTwoRegisters(instruction,
3105 temp_loc,
3106 temp_loc,
3107 iftable_offset,
3108 maybe_temp2_loc,
3109 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003110 // Iftable is never null.
3111 __ Lw(TMP, temp, array_length_offset);
3112 // Loop through the iftable and check if any class matches.
3113 Mips64Label loop;
3114 __ Bind(&loop);
3115 __ Beqzc(TMP, slow_path->GetEntryLabel());
3116 __ Lwu(AT, temp, object_array_data_offset);
3117 __ MaybeUnpoisonHeapReference(AT);
3118 // Go to next interface.
3119 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3120 __ Addiu(TMP, TMP, -2);
3121 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003122 __ Bnec(AT, cls.AsRegister<GpuRegister>(), &loop);
3123 break;
3124 }
3125
3126 case TypeCheckKind::kBitstringCheck: {
3127 // /* HeapReference<Class> */ temp = obj->klass_
3128 GenerateReferenceLoadTwoRegisters(instruction,
3129 temp_loc,
3130 obj_loc,
3131 class_offset,
3132 maybe_temp2_loc,
3133 kWithoutReadBarrier);
3134
3135 GenerateBitstringTypeCheckCompare(instruction, temp);
3136 __ Bnezc(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003137 break;
3138 }
3139 }
3140
3141 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142 __ Bind(slow_path->GetExitLabel());
3143}
3144
3145void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3146 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003147 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003148 locations->SetInAt(0, Location::RequiresRegister());
3149 if (check->HasUses()) {
3150 locations->SetOut(Location::SameAsFirstInput());
3151 }
3152}
3153
3154void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3155 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003156 SlowPathCodeMIPS64* slow_path =
3157 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(check->GetLoadClass(), check);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003158 codegen_->AddSlowPath(slow_path);
3159 GenerateClassInitializationCheck(slow_path,
3160 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3161}
3162
3163void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003164 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003165
Vladimir Markoca6fff82017-10-03 14:49:14 +01003166 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003167
3168 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003169 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003170 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003172 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003173 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 case DataType::Type::kInt32:
3175 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003177 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3179 break;
3180
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003181 case DataType::Type::kFloat32:
3182 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003183 locations->SetInAt(0, Location::RequiresFpuRegister());
3184 locations->SetInAt(1, Location::RequiresFpuRegister());
3185 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003186 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003187
3188 default:
3189 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3190 }
3191}
3192
3193void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3194 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003195 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003196 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003197
3198 // 0 if: left == right
3199 // 1 if: left > right
3200 // -1 if: left < right
3201 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003202 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003203 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003204 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003206 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003207 case DataType::Type::kInt32:
3208 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003209 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003210 Location rhs_location = locations->InAt(1);
3211 bool use_imm = rhs_location.IsConstant();
3212 GpuRegister rhs = ZERO;
3213 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003215 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3216 if (value != 0) {
3217 rhs = AT;
3218 __ LoadConst64(rhs, value);
3219 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003220 } else {
3221 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3222 if (value != 0) {
3223 rhs = AT;
3224 __ LoadConst32(rhs, value);
3225 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003226 }
3227 } else {
3228 rhs = rhs_location.AsRegister<GpuRegister>();
3229 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003230 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003231 __ Slt(res, rhs, lhs);
3232 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003233 break;
3234 }
3235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003236 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003237 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3238 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3239 Mips64Label done;
3240 __ CmpEqS(FTMP, lhs, rhs);
3241 __ LoadConst32(res, 0);
3242 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003243 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003244 __ CmpLtS(FTMP, lhs, rhs);
3245 __ LoadConst32(res, -1);
3246 __ Bc1nez(FTMP, &done);
3247 __ LoadConst32(res, 1);
3248 } else {
3249 __ CmpLtS(FTMP, rhs, lhs);
3250 __ LoadConst32(res, 1);
3251 __ Bc1nez(FTMP, &done);
3252 __ LoadConst32(res, -1);
3253 }
3254 __ Bind(&done);
3255 break;
3256 }
3257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003258 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003259 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3260 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3261 Mips64Label done;
3262 __ CmpEqD(FTMP, lhs, rhs);
3263 __ LoadConst32(res, 0);
3264 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003265 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003266 __ CmpLtD(FTMP, lhs, rhs);
3267 __ LoadConst32(res, -1);
3268 __ Bc1nez(FTMP, &done);
3269 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003270 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003271 __ CmpLtD(FTMP, rhs, lhs);
3272 __ LoadConst32(res, 1);
3273 __ Bc1nez(FTMP, &done);
3274 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003275 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003276 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003277 break;
3278 }
3279
3280 default:
3281 LOG(FATAL) << "Unimplemented compare type " << in_type;
3282 }
3283}
3284
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003285void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003286 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003287 switch (instruction->InputAt(0)->GetType()) {
3288 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003289 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003290 locations->SetInAt(0, Location::RequiresRegister());
3291 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3292 break;
3293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 case DataType::Type::kFloat32:
3295 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003296 locations->SetInAt(0, Location::RequiresFpuRegister());
3297 locations->SetInAt(1, Location::RequiresFpuRegister());
3298 break;
3299 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003300 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3302 }
3303}
3304
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003305void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003306 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003307 return;
3308 }
3309
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003310 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003311 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003312 switch (type) {
3313 default:
3314 // Integer case.
3315 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3316 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003317 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003318 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3319 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003320 case DataType::Type::kFloat32:
3321 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003322 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3323 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003324 }
3325}
3326
Alexey Frunzec857c742015-09-23 15:12:39 -07003327void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3328 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003330
3331 LocationSummary* locations = instruction->GetLocations();
3332 Location second = locations->InAt(1);
3333 DCHECK(second.IsConstant());
3334
3335 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3336 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3337 int64_t imm = Int64FromConstant(second.GetConstant());
3338 DCHECK(imm == 1 || imm == -1);
3339
3340 if (instruction->IsRem()) {
3341 __ Move(out, ZERO);
3342 } else {
3343 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003344 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003345 __ Subu(out, ZERO, dividend);
3346 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003347 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003348 __ Dsubu(out, ZERO, dividend);
3349 }
3350 } else if (out != dividend) {
3351 __ Move(out, dividend);
3352 }
3353 }
3354}
3355
3356void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3357 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003358 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003359
3360 LocationSummary* locations = instruction->GetLocations();
3361 Location second = locations->InAt(1);
3362 DCHECK(second.IsConstant());
3363
3364 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3365 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3366 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003367 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003368 int ctz_imm = CTZ(abs_imm);
3369
3370 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003371 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003372 if (ctz_imm == 1) {
3373 // Fast path for division by +/-2, which is very common.
3374 __ Srl(TMP, dividend, 31);
3375 } else {
3376 __ Sra(TMP, dividend, 31);
3377 __ Srl(TMP, TMP, 32 - ctz_imm);
3378 }
3379 __ Addu(out, dividend, TMP);
3380 __ Sra(out, out, ctz_imm);
3381 if (imm < 0) {
3382 __ Subu(out, ZERO, out);
3383 }
3384 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003385 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003386 if (ctz_imm == 1) {
3387 // Fast path for division by +/-2, which is very common.
3388 __ Dsrl32(TMP, dividend, 31);
3389 } else {
3390 __ Dsra32(TMP, dividend, 31);
3391 if (ctz_imm > 32) {
3392 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3393 } else {
3394 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3395 }
3396 }
3397 __ Daddu(out, dividend, TMP);
3398 if (ctz_imm < 32) {
3399 __ Dsra(out, out, ctz_imm);
3400 } else {
3401 __ Dsra32(out, out, ctz_imm - 32);
3402 }
3403 if (imm < 0) {
3404 __ Dsubu(out, ZERO, out);
3405 }
3406 }
3407 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003408 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003409 if (ctz_imm == 1) {
3410 // Fast path for modulo +/-2, which is very common.
3411 __ Sra(TMP, dividend, 31);
3412 __ Subu(out, dividend, TMP);
3413 __ Andi(out, out, 1);
3414 __ Addu(out, out, TMP);
3415 } else {
3416 __ Sra(TMP, dividend, 31);
3417 __ Srl(TMP, TMP, 32 - ctz_imm);
3418 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003419 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003420 __ Subu(out, out, TMP);
3421 }
3422 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003423 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003424 if (ctz_imm == 1) {
3425 // Fast path for modulo +/-2, which is very common.
3426 __ Dsra32(TMP, dividend, 31);
3427 __ Dsubu(out, dividend, TMP);
3428 __ Andi(out, out, 1);
3429 __ Daddu(out, out, TMP);
3430 } else {
3431 __ Dsra32(TMP, dividend, 31);
3432 if (ctz_imm > 32) {
3433 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3434 } else {
3435 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3436 }
3437 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003438 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003439 __ Dsubu(out, out, TMP);
3440 }
3441 }
3442 }
3443}
3444
3445void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3446 DCHECK(instruction->IsDiv() || instruction->IsRem());
3447
3448 LocationSummary* locations = instruction->GetLocations();
3449 Location second = locations->InAt(1);
3450 DCHECK(second.IsConstant());
3451
3452 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3453 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3454 int64_t imm = Int64FromConstant(second.GetConstant());
3455
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003456 DataType::Type type = instruction->GetResultType();
3457 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003458
3459 int64_t magic;
3460 int shift;
3461 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003463 &magic,
3464 &shift);
3465
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003467 __ LoadConst32(TMP, magic);
3468 __ MuhR6(TMP, dividend, TMP);
3469
3470 if (imm > 0 && magic < 0) {
3471 __ Addu(TMP, TMP, dividend);
3472 } else if (imm < 0 && magic > 0) {
3473 __ Subu(TMP, TMP, dividend);
3474 }
3475
3476 if (shift != 0) {
3477 __ Sra(TMP, TMP, shift);
3478 }
3479
3480 if (instruction->IsDiv()) {
3481 __ Sra(out, TMP, 31);
3482 __ Subu(out, TMP, out);
3483 } else {
3484 __ Sra(AT, TMP, 31);
3485 __ Subu(AT, TMP, AT);
3486 __ LoadConst32(TMP, imm);
3487 __ MulR6(TMP, AT, TMP);
3488 __ Subu(out, dividend, TMP);
3489 }
3490 } else {
3491 __ LoadConst64(TMP, magic);
3492 __ Dmuh(TMP, dividend, TMP);
3493
3494 if (imm > 0 && magic < 0) {
3495 __ Daddu(TMP, TMP, dividend);
3496 } else if (imm < 0 && magic > 0) {
3497 __ Dsubu(TMP, TMP, dividend);
3498 }
3499
3500 if (shift >= 32) {
3501 __ Dsra32(TMP, TMP, shift - 32);
3502 } else if (shift > 0) {
3503 __ Dsra(TMP, TMP, shift);
3504 }
3505
3506 if (instruction->IsDiv()) {
3507 __ Dsra32(out, TMP, 31);
3508 __ Dsubu(out, TMP, out);
3509 } else {
3510 __ Dsra32(AT, TMP, 31);
3511 __ Dsubu(AT, TMP, AT);
3512 __ LoadConst64(TMP, imm);
3513 __ Dmul(TMP, AT, TMP);
3514 __ Dsubu(out, dividend, TMP);
3515 }
3516 }
3517}
3518
3519void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3520 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 DataType::Type type = instruction->GetResultType();
3522 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003523
3524 LocationSummary* locations = instruction->GetLocations();
3525 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3526 Location second = locations->InAt(1);
3527
3528 if (second.IsConstant()) {
3529 int64_t imm = Int64FromConstant(second.GetConstant());
3530 if (imm == 0) {
3531 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3532 } else if (imm == 1 || imm == -1) {
3533 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003534 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003535 DivRemByPowerOfTwo(instruction);
3536 } else {
3537 DCHECK(imm <= -2 || imm >= 2);
3538 GenerateDivRemWithAnyConstant(instruction);
3539 }
3540 } else {
3541 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3542 GpuRegister divisor = second.AsRegister<GpuRegister>();
3543 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003544 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003545 __ DivR6(out, dividend, divisor);
3546 else
3547 __ Ddiv(out, dividend, divisor);
3548 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003549 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003550 __ ModR6(out, dividend, divisor);
3551 else
3552 __ Dmod(out, dividend, divisor);
3553 }
3554 }
3555}
3556
Alexey Frunze4dda3372015-06-01 18:31:49 -07003557void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3558 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003559 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003560 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003561 case DataType::Type::kInt32:
3562 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003563 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003564 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3566 break;
3567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003568 case DataType::Type::kFloat32:
3569 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003570 locations->SetInAt(0, Location::RequiresFpuRegister());
3571 locations->SetInAt(1, Location::RequiresFpuRegister());
3572 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3573 break;
3574
3575 default:
3576 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3577 }
3578}
3579
3580void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003582 LocationSummary* locations = instruction->GetLocations();
3583
3584 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 case DataType::Type::kInt32:
3586 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003587 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003589 case DataType::Type::kFloat32:
3590 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003591 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3592 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3593 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003595 __ DivS(dst, lhs, rhs);
3596 else
3597 __ DivD(dst, lhs, rhs);
3598 break;
3599 }
3600 default:
3601 LOG(FATAL) << "Unexpected div type " << type;
3602 }
3603}
3604
3605void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003606 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003607 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003608}
3609
3610void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3611 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003612 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003613 codegen_->AddSlowPath(slow_path);
3614 Location value = instruction->GetLocations()->InAt(0);
3615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003616 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003619 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003620 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003621 }
3622
3623 if (value.IsConstant()) {
3624 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3625 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003626 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003627 } else {
3628 // A division by a non-null constant is valid. We don't need to perform
3629 // any check, so simply fall through.
3630 }
3631 } else {
3632 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3633 }
3634}
3635
3636void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3637 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003638 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003639 locations->SetOut(Location::ConstantLocation(constant));
3640}
3641
3642void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3643 // Will be generated at use site.
3644}
3645
3646void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3647 exit->SetLocations(nullptr);
3648}
3649
3650void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3651}
3652
3653void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3654 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003655 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003656 locations->SetOut(Location::ConstantLocation(constant));
3657}
3658
3659void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3660 // Will be generated at use site.
3661}
3662
David Brazdilfc6a86a2015-06-26 10:33:45 +00003663void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003664 if (successor->IsExitBlock()) {
3665 DCHECK(got->GetPrevious()->AlwaysThrows());
3666 return; // no code needed
3667 }
3668
Alexey Frunze4dda3372015-06-01 18:31:49 -07003669 HBasicBlock* block = got->GetBlock();
3670 HInstruction* previous = got->GetPrevious();
3671 HLoopInformation* info = block->GetLoopInformation();
3672
3673 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01003674 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3675 __ Ld(AT, SP, kCurrentMethodStackOffset);
3676 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3677 __ Addiu(TMP, TMP, 1);
3678 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3679 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003680 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3681 return;
3682 }
3683 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3684 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3685 }
3686 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003687 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003688 }
3689}
3690
David Brazdilfc6a86a2015-06-26 10:33:45 +00003691void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3692 got->SetLocations(nullptr);
3693}
3694
3695void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3696 HandleGoto(got, got->GetSuccessor());
3697}
3698
3699void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3700 try_boundary->SetLocations(nullptr);
3701}
3702
3703void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3704 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3705 if (!successor->IsExitBlock()) {
3706 HandleGoto(try_boundary, successor);
3707 }
3708}
3709
Alexey Frunze299a9392015-12-08 16:08:02 -08003710void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3711 bool is64bit,
3712 LocationSummary* locations) {
3713 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3714 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3715 Location rhs_location = locations->InAt(1);
3716 GpuRegister rhs_reg = ZERO;
3717 int64_t rhs_imm = 0;
3718 bool use_imm = rhs_location.IsConstant();
3719 if (use_imm) {
3720 if (is64bit) {
3721 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3722 } else {
3723 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3724 }
3725 } else {
3726 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3727 }
3728 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3729
3730 switch (cond) {
3731 case kCondEQ:
3732 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003733 if (use_imm && IsInt<16>(-rhs_imm)) {
3734 if (rhs_imm == 0) {
3735 if (cond == kCondEQ) {
3736 __ Sltiu(dst, lhs, 1);
3737 } else {
3738 __ Sltu(dst, ZERO, lhs);
3739 }
3740 } else {
3741 if (is64bit) {
3742 __ Daddiu(dst, lhs, -rhs_imm);
3743 } else {
3744 __ Addiu(dst, lhs, -rhs_imm);
3745 }
3746 if (cond == kCondEQ) {
3747 __ Sltiu(dst, dst, 1);
3748 } else {
3749 __ Sltu(dst, ZERO, dst);
3750 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003751 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003752 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003753 if (use_imm && IsUint<16>(rhs_imm)) {
3754 __ Xori(dst, lhs, rhs_imm);
3755 } else {
3756 if (use_imm) {
3757 rhs_reg = TMP;
3758 __ LoadConst64(rhs_reg, rhs_imm);
3759 }
3760 __ Xor(dst, lhs, rhs_reg);
3761 }
3762 if (cond == kCondEQ) {
3763 __ Sltiu(dst, dst, 1);
3764 } else {
3765 __ Sltu(dst, ZERO, dst);
3766 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003767 }
3768 break;
3769
3770 case kCondLT:
3771 case kCondGE:
3772 if (use_imm && IsInt<16>(rhs_imm)) {
3773 __ Slti(dst, lhs, rhs_imm);
3774 } else {
3775 if (use_imm) {
3776 rhs_reg = TMP;
3777 __ LoadConst64(rhs_reg, rhs_imm);
3778 }
3779 __ Slt(dst, lhs, rhs_reg);
3780 }
3781 if (cond == kCondGE) {
3782 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3783 // only the slt instruction but no sge.
3784 __ Xori(dst, dst, 1);
3785 }
3786 break;
3787
3788 case kCondLE:
3789 case kCondGT:
3790 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3791 // Simulate lhs <= rhs via lhs < rhs + 1.
3792 __ Slti(dst, lhs, rhs_imm_plus_one);
3793 if (cond == kCondGT) {
3794 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3795 // only the slti instruction but no sgti.
3796 __ Xori(dst, dst, 1);
3797 }
3798 } else {
3799 if (use_imm) {
3800 rhs_reg = TMP;
3801 __ LoadConst64(rhs_reg, rhs_imm);
3802 }
3803 __ Slt(dst, rhs_reg, lhs);
3804 if (cond == kCondLE) {
3805 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3806 // only the slt instruction but no sle.
3807 __ Xori(dst, dst, 1);
3808 }
3809 }
3810 break;
3811
3812 case kCondB:
3813 case kCondAE:
3814 if (use_imm && IsInt<16>(rhs_imm)) {
3815 // Sltiu sign-extends its 16-bit immediate operand before
3816 // the comparison and thus lets us compare directly with
3817 // unsigned values in the ranges [0, 0x7fff] and
3818 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3819 __ Sltiu(dst, lhs, rhs_imm);
3820 } else {
3821 if (use_imm) {
3822 rhs_reg = TMP;
3823 __ LoadConst64(rhs_reg, rhs_imm);
3824 }
3825 __ Sltu(dst, lhs, rhs_reg);
3826 }
3827 if (cond == kCondAE) {
3828 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3829 // only the sltu instruction but no sgeu.
3830 __ Xori(dst, dst, 1);
3831 }
3832 break;
3833
3834 case kCondBE:
3835 case kCondA:
3836 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3837 // Simulate lhs <= rhs via lhs < rhs + 1.
3838 // Note that this only works if rhs + 1 does not overflow
3839 // to 0, hence the check above.
3840 // Sltiu sign-extends its 16-bit immediate operand before
3841 // the comparison and thus lets us compare directly with
3842 // unsigned values in the ranges [0, 0x7fff] and
3843 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3844 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3845 if (cond == kCondA) {
3846 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3847 // only the sltiu instruction but no sgtiu.
3848 __ Xori(dst, dst, 1);
3849 }
3850 } else {
3851 if (use_imm) {
3852 rhs_reg = TMP;
3853 __ LoadConst64(rhs_reg, rhs_imm);
3854 }
3855 __ Sltu(dst, rhs_reg, lhs);
3856 if (cond == kCondBE) {
3857 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3858 // only the sltu instruction but no sleu.
3859 __ Xori(dst, dst, 1);
3860 }
3861 }
3862 break;
3863 }
3864}
3865
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003866bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3867 bool is64bit,
3868 LocationSummary* input_locations,
3869 GpuRegister dst) {
3870 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3871 Location rhs_location = input_locations->InAt(1);
3872 GpuRegister rhs_reg = ZERO;
3873 int64_t rhs_imm = 0;
3874 bool use_imm = rhs_location.IsConstant();
3875 if (use_imm) {
3876 if (is64bit) {
3877 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3878 } else {
3879 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3880 }
3881 } else {
3882 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3883 }
3884 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3885
3886 switch (cond) {
3887 case kCondEQ:
3888 case kCondNE:
3889 if (use_imm && IsInt<16>(-rhs_imm)) {
3890 if (is64bit) {
3891 __ Daddiu(dst, lhs, -rhs_imm);
3892 } else {
3893 __ Addiu(dst, lhs, -rhs_imm);
3894 }
3895 } else if (use_imm && IsUint<16>(rhs_imm)) {
3896 __ Xori(dst, lhs, rhs_imm);
3897 } else {
3898 if (use_imm) {
3899 rhs_reg = TMP;
3900 __ LoadConst64(rhs_reg, rhs_imm);
3901 }
3902 __ Xor(dst, lhs, rhs_reg);
3903 }
3904 return (cond == kCondEQ);
3905
3906 case kCondLT:
3907 case kCondGE:
3908 if (use_imm && IsInt<16>(rhs_imm)) {
3909 __ Slti(dst, lhs, rhs_imm);
3910 } else {
3911 if (use_imm) {
3912 rhs_reg = TMP;
3913 __ LoadConst64(rhs_reg, rhs_imm);
3914 }
3915 __ Slt(dst, lhs, rhs_reg);
3916 }
3917 return (cond == kCondGE);
3918
3919 case kCondLE:
3920 case kCondGT:
3921 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3922 // Simulate lhs <= rhs via lhs < rhs + 1.
3923 __ Slti(dst, lhs, rhs_imm_plus_one);
3924 return (cond == kCondGT);
3925 } else {
3926 if (use_imm) {
3927 rhs_reg = TMP;
3928 __ LoadConst64(rhs_reg, rhs_imm);
3929 }
3930 __ Slt(dst, rhs_reg, lhs);
3931 return (cond == kCondLE);
3932 }
3933
3934 case kCondB:
3935 case kCondAE:
3936 if (use_imm && IsInt<16>(rhs_imm)) {
3937 // Sltiu sign-extends its 16-bit immediate operand before
3938 // the comparison and thus lets us compare directly with
3939 // unsigned values in the ranges [0, 0x7fff] and
3940 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3941 __ Sltiu(dst, lhs, rhs_imm);
3942 } else {
3943 if (use_imm) {
3944 rhs_reg = TMP;
3945 __ LoadConst64(rhs_reg, rhs_imm);
3946 }
3947 __ Sltu(dst, lhs, rhs_reg);
3948 }
3949 return (cond == kCondAE);
3950
3951 case kCondBE:
3952 case kCondA:
3953 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3954 // Simulate lhs <= rhs via lhs < rhs + 1.
3955 // Note that this only works if rhs + 1 does not overflow
3956 // to 0, hence the check above.
3957 // Sltiu sign-extends its 16-bit immediate operand before
3958 // the comparison and thus lets us compare directly with
3959 // unsigned values in the ranges [0, 0x7fff] and
3960 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3961 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3962 return (cond == kCondA);
3963 } else {
3964 if (use_imm) {
3965 rhs_reg = TMP;
3966 __ LoadConst64(rhs_reg, rhs_imm);
3967 }
3968 __ Sltu(dst, rhs_reg, lhs);
3969 return (cond == kCondBE);
3970 }
3971 }
3972}
3973
Alexey Frunze299a9392015-12-08 16:08:02 -08003974void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3975 bool is64bit,
3976 LocationSummary* locations,
3977 Mips64Label* label) {
3978 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3979 Location rhs_location = locations->InAt(1);
3980 GpuRegister rhs_reg = ZERO;
3981 int64_t rhs_imm = 0;
3982 bool use_imm = rhs_location.IsConstant();
3983 if (use_imm) {
3984 if (is64bit) {
3985 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3986 } else {
3987 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3988 }
3989 } else {
3990 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3991 }
3992
3993 if (use_imm && rhs_imm == 0) {
3994 switch (cond) {
3995 case kCondEQ:
3996 case kCondBE: // <= 0 if zero
3997 __ Beqzc(lhs, label);
3998 break;
3999 case kCondNE:
4000 case kCondA: // > 0 if non-zero
4001 __ Bnezc(lhs, label);
4002 break;
4003 case kCondLT:
4004 __ Bltzc(lhs, label);
4005 break;
4006 case kCondGE:
4007 __ Bgezc(lhs, label);
4008 break;
4009 case kCondLE:
4010 __ Blezc(lhs, label);
4011 break;
4012 case kCondGT:
4013 __ Bgtzc(lhs, label);
4014 break;
4015 case kCondB: // always false
4016 break;
4017 case kCondAE: // always true
4018 __ Bc(label);
4019 break;
4020 }
4021 } else {
4022 if (use_imm) {
4023 rhs_reg = TMP;
4024 __ LoadConst64(rhs_reg, rhs_imm);
4025 }
4026 switch (cond) {
4027 case kCondEQ:
4028 __ Beqc(lhs, rhs_reg, label);
4029 break;
4030 case kCondNE:
4031 __ Bnec(lhs, rhs_reg, label);
4032 break;
4033 case kCondLT:
4034 __ Bltc(lhs, rhs_reg, label);
4035 break;
4036 case kCondGE:
4037 __ Bgec(lhs, rhs_reg, label);
4038 break;
4039 case kCondLE:
4040 __ Bgec(rhs_reg, lhs, label);
4041 break;
4042 case kCondGT:
4043 __ Bltc(rhs_reg, lhs, label);
4044 break;
4045 case kCondB:
4046 __ Bltuc(lhs, rhs_reg, label);
4047 break;
4048 case kCondAE:
4049 __ Bgeuc(lhs, rhs_reg, label);
4050 break;
4051 case kCondBE:
4052 __ Bgeuc(rhs_reg, lhs, label);
4053 break;
4054 case kCondA:
4055 __ Bltuc(rhs_reg, lhs, label);
4056 break;
4057 }
4058 }
4059}
4060
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004061void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
4062 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004064 LocationSummary* locations) {
4065 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4066 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4067 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004069 switch (cond) {
4070 case kCondEQ:
4071 __ CmpEqS(FTMP, lhs, rhs);
4072 __ Mfc1(dst, FTMP);
4073 __ Andi(dst, dst, 1);
4074 break;
4075 case kCondNE:
4076 __ CmpEqS(FTMP, lhs, rhs);
4077 __ Mfc1(dst, FTMP);
4078 __ Addiu(dst, dst, 1);
4079 break;
4080 case kCondLT:
4081 if (gt_bias) {
4082 __ CmpLtS(FTMP, lhs, rhs);
4083 } else {
4084 __ CmpUltS(FTMP, lhs, rhs);
4085 }
4086 __ Mfc1(dst, FTMP);
4087 __ Andi(dst, dst, 1);
4088 break;
4089 case kCondLE:
4090 if (gt_bias) {
4091 __ CmpLeS(FTMP, lhs, rhs);
4092 } else {
4093 __ CmpUleS(FTMP, lhs, rhs);
4094 }
4095 __ Mfc1(dst, FTMP);
4096 __ Andi(dst, dst, 1);
4097 break;
4098 case kCondGT:
4099 if (gt_bias) {
4100 __ CmpUltS(FTMP, rhs, lhs);
4101 } else {
4102 __ CmpLtS(FTMP, rhs, lhs);
4103 }
4104 __ Mfc1(dst, FTMP);
4105 __ Andi(dst, dst, 1);
4106 break;
4107 case kCondGE:
4108 if (gt_bias) {
4109 __ CmpUleS(FTMP, rhs, lhs);
4110 } else {
4111 __ CmpLeS(FTMP, rhs, lhs);
4112 }
4113 __ Mfc1(dst, FTMP);
4114 __ Andi(dst, dst, 1);
4115 break;
4116 default:
4117 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4118 UNREACHABLE();
4119 }
4120 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004122 switch (cond) {
4123 case kCondEQ:
4124 __ CmpEqD(FTMP, lhs, rhs);
4125 __ Mfc1(dst, FTMP);
4126 __ Andi(dst, dst, 1);
4127 break;
4128 case kCondNE:
4129 __ CmpEqD(FTMP, lhs, rhs);
4130 __ Mfc1(dst, FTMP);
4131 __ Addiu(dst, dst, 1);
4132 break;
4133 case kCondLT:
4134 if (gt_bias) {
4135 __ CmpLtD(FTMP, lhs, rhs);
4136 } else {
4137 __ CmpUltD(FTMP, lhs, rhs);
4138 }
4139 __ Mfc1(dst, FTMP);
4140 __ Andi(dst, dst, 1);
4141 break;
4142 case kCondLE:
4143 if (gt_bias) {
4144 __ CmpLeD(FTMP, lhs, rhs);
4145 } else {
4146 __ CmpUleD(FTMP, lhs, rhs);
4147 }
4148 __ Mfc1(dst, FTMP);
4149 __ Andi(dst, dst, 1);
4150 break;
4151 case kCondGT:
4152 if (gt_bias) {
4153 __ CmpUltD(FTMP, rhs, lhs);
4154 } else {
4155 __ CmpLtD(FTMP, rhs, lhs);
4156 }
4157 __ Mfc1(dst, FTMP);
4158 __ Andi(dst, dst, 1);
4159 break;
4160 case kCondGE:
4161 if (gt_bias) {
4162 __ CmpUleD(FTMP, rhs, lhs);
4163 } else {
4164 __ CmpLeD(FTMP, rhs, lhs);
4165 }
4166 __ Mfc1(dst, FTMP);
4167 __ Andi(dst, dst, 1);
4168 break;
4169 default:
4170 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4171 UNREACHABLE();
4172 }
4173 }
4174}
4175
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004176bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4177 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004178 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004179 LocationSummary* input_locations,
4180 FpuRegister dst) {
4181 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4182 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004183 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004184 switch (cond) {
4185 case kCondEQ:
4186 __ CmpEqS(dst, lhs, rhs);
4187 return false;
4188 case kCondNE:
4189 __ CmpEqS(dst, lhs, rhs);
4190 return true;
4191 case kCondLT:
4192 if (gt_bias) {
4193 __ CmpLtS(dst, lhs, rhs);
4194 } else {
4195 __ CmpUltS(dst, lhs, rhs);
4196 }
4197 return false;
4198 case kCondLE:
4199 if (gt_bias) {
4200 __ CmpLeS(dst, lhs, rhs);
4201 } else {
4202 __ CmpUleS(dst, lhs, rhs);
4203 }
4204 return false;
4205 case kCondGT:
4206 if (gt_bias) {
4207 __ CmpUltS(dst, rhs, lhs);
4208 } else {
4209 __ CmpLtS(dst, rhs, lhs);
4210 }
4211 return false;
4212 case kCondGE:
4213 if (gt_bias) {
4214 __ CmpUleS(dst, rhs, lhs);
4215 } else {
4216 __ CmpLeS(dst, rhs, lhs);
4217 }
4218 return false;
4219 default:
4220 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4221 UNREACHABLE();
4222 }
4223 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004224 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004225 switch (cond) {
4226 case kCondEQ:
4227 __ CmpEqD(dst, lhs, rhs);
4228 return false;
4229 case kCondNE:
4230 __ CmpEqD(dst, lhs, rhs);
4231 return true;
4232 case kCondLT:
4233 if (gt_bias) {
4234 __ CmpLtD(dst, lhs, rhs);
4235 } else {
4236 __ CmpUltD(dst, lhs, rhs);
4237 }
4238 return false;
4239 case kCondLE:
4240 if (gt_bias) {
4241 __ CmpLeD(dst, lhs, rhs);
4242 } else {
4243 __ CmpUleD(dst, lhs, rhs);
4244 }
4245 return false;
4246 case kCondGT:
4247 if (gt_bias) {
4248 __ CmpUltD(dst, rhs, lhs);
4249 } else {
4250 __ CmpLtD(dst, rhs, lhs);
4251 }
4252 return false;
4253 case kCondGE:
4254 if (gt_bias) {
4255 __ CmpUleD(dst, rhs, lhs);
4256 } else {
4257 __ CmpLeD(dst, rhs, lhs);
4258 }
4259 return false;
4260 default:
4261 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4262 UNREACHABLE();
4263 }
4264 }
4265}
4266
Alexey Frunze299a9392015-12-08 16:08:02 -08004267void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4268 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004269 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004270 LocationSummary* locations,
4271 Mips64Label* label) {
4272 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4273 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004274 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004275 switch (cond) {
4276 case kCondEQ:
4277 __ CmpEqS(FTMP, lhs, rhs);
4278 __ Bc1nez(FTMP, label);
4279 break;
4280 case kCondNE:
4281 __ CmpEqS(FTMP, lhs, rhs);
4282 __ Bc1eqz(FTMP, label);
4283 break;
4284 case kCondLT:
4285 if (gt_bias) {
4286 __ CmpLtS(FTMP, lhs, rhs);
4287 } else {
4288 __ CmpUltS(FTMP, lhs, rhs);
4289 }
4290 __ Bc1nez(FTMP, label);
4291 break;
4292 case kCondLE:
4293 if (gt_bias) {
4294 __ CmpLeS(FTMP, lhs, rhs);
4295 } else {
4296 __ CmpUleS(FTMP, lhs, rhs);
4297 }
4298 __ Bc1nez(FTMP, label);
4299 break;
4300 case kCondGT:
4301 if (gt_bias) {
4302 __ CmpUltS(FTMP, rhs, lhs);
4303 } else {
4304 __ CmpLtS(FTMP, rhs, lhs);
4305 }
4306 __ Bc1nez(FTMP, label);
4307 break;
4308 case kCondGE:
4309 if (gt_bias) {
4310 __ CmpUleS(FTMP, rhs, lhs);
4311 } else {
4312 __ CmpLeS(FTMP, rhs, lhs);
4313 }
4314 __ Bc1nez(FTMP, label);
4315 break;
4316 default:
4317 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004318 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004319 }
4320 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004321 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004322 switch (cond) {
4323 case kCondEQ:
4324 __ CmpEqD(FTMP, lhs, rhs);
4325 __ Bc1nez(FTMP, label);
4326 break;
4327 case kCondNE:
4328 __ CmpEqD(FTMP, lhs, rhs);
4329 __ Bc1eqz(FTMP, label);
4330 break;
4331 case kCondLT:
4332 if (gt_bias) {
4333 __ CmpLtD(FTMP, lhs, rhs);
4334 } else {
4335 __ CmpUltD(FTMP, lhs, rhs);
4336 }
4337 __ Bc1nez(FTMP, label);
4338 break;
4339 case kCondLE:
4340 if (gt_bias) {
4341 __ CmpLeD(FTMP, lhs, rhs);
4342 } else {
4343 __ CmpUleD(FTMP, lhs, rhs);
4344 }
4345 __ Bc1nez(FTMP, label);
4346 break;
4347 case kCondGT:
4348 if (gt_bias) {
4349 __ CmpUltD(FTMP, rhs, lhs);
4350 } else {
4351 __ CmpLtD(FTMP, rhs, lhs);
4352 }
4353 __ Bc1nez(FTMP, label);
4354 break;
4355 case kCondGE:
4356 if (gt_bias) {
4357 __ CmpUleD(FTMP, rhs, lhs);
4358 } else {
4359 __ CmpLeD(FTMP, rhs, lhs);
4360 }
4361 __ Bc1nez(FTMP, label);
4362 break;
4363 default:
4364 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004365 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004366 }
4367 }
4368}
4369
Alexey Frunze4dda3372015-06-01 18:31:49 -07004370void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004371 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004372 Mips64Label* true_target,
4373 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004374 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004375
David Brazdil0debae72015-11-12 18:37:00 +00004376 if (true_target == nullptr && false_target == nullptr) {
4377 // Nothing to do. The code always falls through.
4378 return;
4379 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004380 // Constant condition, statically compared against "true" (integer value 1).
4381 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004382 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004383 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004384 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004385 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004386 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004387 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004388 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004389 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004390 }
David Brazdil0debae72015-11-12 18:37:00 +00004391 return;
4392 }
4393
4394 // The following code generates these patterns:
4395 // (1) true_target == nullptr && false_target != nullptr
4396 // - opposite condition true => branch to false_target
4397 // (2) true_target != nullptr && false_target == nullptr
4398 // - condition true => branch to true_target
4399 // (3) true_target != nullptr && false_target != nullptr
4400 // - condition true => branch to true_target
4401 // - branch to false_target
4402 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004403 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004404 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004405 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004406 if (true_target == nullptr) {
4407 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4408 } else {
4409 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4410 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004411 } else {
4412 // The condition instruction has not been materialized, use its inputs as
4413 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004414 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004415 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004416 LocationSummary* locations = cond->GetLocations();
4417 IfCondition if_cond = condition->GetCondition();
4418 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004419
David Brazdil0debae72015-11-12 18:37:00 +00004420 if (true_target == nullptr) {
4421 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004422 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004423 }
4424
Alexey Frunze299a9392015-12-08 16:08:02 -08004425 switch (type) {
4426 default:
4427 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4428 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004429 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004430 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4431 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004432 case DataType::Type::kFloat32:
4433 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004434 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4435 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004436 }
4437 }
David Brazdil0debae72015-11-12 18:37:00 +00004438
4439 // If neither branch falls through (case 3), the conditional branch to `true_target`
4440 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4441 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004442 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004443 }
4444}
4445
4446void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004447 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004448 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004449 locations->SetInAt(0, Location::RequiresRegister());
4450 }
4451}
4452
4453void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004454 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4455 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004456 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004457 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004458 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004459 nullptr : codegen_->GetLabelOf(false_successor);
4460 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004461}
4462
4463void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004464 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004465 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004466 InvokeRuntimeCallingConvention calling_convention;
4467 RegisterSet caller_saves = RegisterSet::Empty();
4468 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4469 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004470 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004471 locations->SetInAt(0, Location::RequiresRegister());
4472 }
4473}
4474
4475void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004476 SlowPathCodeMIPS64* slow_path =
4477 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004478 GenerateTestAndBranch(deoptimize,
4479 /* condition_input_index */ 0,
4480 slow_path->GetEntryLabel(),
4481 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004482}
4483
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004484// This function returns true if a conditional move can be generated for HSelect.
4485// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4486// branches and regular moves.
4487//
4488// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4489//
4490// While determining feasibility of a conditional move and setting inputs/outputs
4491// are two distinct tasks, this function does both because they share quite a bit
4492// of common logic.
4493static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4494 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4495 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4496 HCondition* condition = cond->AsCondition();
4497
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004498 DataType::Type cond_type =
4499 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4500 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004501
4502 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4503 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4504 bool is_true_value_zero_constant =
4505 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4506 bool is_false_value_zero_constant =
4507 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4508
4509 bool can_move_conditionally = false;
4510 bool use_const_for_false_in = false;
4511 bool use_const_for_true_in = false;
4512
4513 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004514 if (!DataType::IsFloatingPointType(cond_type)) {
4515 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004516 // Moving int/long on int/long condition.
4517 if (is_true_value_zero_constant) {
4518 // seleqz out_reg, false_reg, cond_reg
4519 can_move_conditionally = true;
4520 use_const_for_true_in = true;
4521 } else if (is_false_value_zero_constant) {
4522 // selnez out_reg, true_reg, cond_reg
4523 can_move_conditionally = true;
4524 use_const_for_false_in = true;
4525 } else if (materialized) {
4526 // Not materializing unmaterialized int conditions
4527 // to keep the instruction count low.
4528 // selnez AT, true_reg, cond_reg
4529 // seleqz TMP, false_reg, cond_reg
4530 // or out_reg, AT, TMP
4531 can_move_conditionally = true;
4532 }
4533 } else {
4534 // Moving float/double on int/long condition.
4535 if (materialized) {
4536 // Not materializing unmaterialized int conditions
4537 // to keep the instruction count low.
4538 can_move_conditionally = true;
4539 if (is_true_value_zero_constant) {
4540 // sltu TMP, ZERO, cond_reg
4541 // mtc1 TMP, temp_cond_reg
4542 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4543 use_const_for_true_in = true;
4544 } else if (is_false_value_zero_constant) {
4545 // sltu TMP, ZERO, cond_reg
4546 // mtc1 TMP, temp_cond_reg
4547 // selnez.fmt out_reg, true_reg, temp_cond_reg
4548 use_const_for_false_in = true;
4549 } else {
4550 // sltu TMP, ZERO, cond_reg
4551 // mtc1 TMP, temp_cond_reg
4552 // sel.fmt temp_cond_reg, false_reg, true_reg
4553 // mov.fmt out_reg, temp_cond_reg
4554 }
4555 }
4556 }
4557 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004558 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004559 // Moving int/long on float/double condition.
4560 can_move_conditionally = true;
4561 if (is_true_value_zero_constant) {
4562 // mfc1 TMP, temp_cond_reg
4563 // seleqz out_reg, false_reg, TMP
4564 use_const_for_true_in = true;
4565 } else if (is_false_value_zero_constant) {
4566 // mfc1 TMP, temp_cond_reg
4567 // selnez out_reg, true_reg, TMP
4568 use_const_for_false_in = true;
4569 } else {
4570 // mfc1 TMP, temp_cond_reg
4571 // selnez AT, true_reg, TMP
4572 // seleqz TMP, false_reg, TMP
4573 // or out_reg, AT, TMP
4574 }
4575 } else {
4576 // Moving float/double on float/double condition.
4577 can_move_conditionally = true;
4578 if (is_true_value_zero_constant) {
4579 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4580 use_const_for_true_in = true;
4581 } else if (is_false_value_zero_constant) {
4582 // selnez.fmt out_reg, true_reg, temp_cond_reg
4583 use_const_for_false_in = true;
4584 } else {
4585 // sel.fmt temp_cond_reg, false_reg, true_reg
4586 // mov.fmt out_reg, temp_cond_reg
4587 }
4588 }
4589 }
4590 }
4591
4592 if (can_move_conditionally) {
4593 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4594 } else {
4595 DCHECK(!use_const_for_false_in);
4596 DCHECK(!use_const_for_true_in);
4597 }
4598
4599 if (locations_to_set != nullptr) {
4600 if (use_const_for_false_in) {
4601 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4602 } else {
4603 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004605 ? Location::RequiresFpuRegister()
4606 : Location::RequiresRegister());
4607 }
4608 if (use_const_for_true_in) {
4609 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4610 } else {
4611 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004612 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004613 ? Location::RequiresFpuRegister()
4614 : Location::RequiresRegister());
4615 }
4616 if (materialized) {
4617 locations_to_set->SetInAt(2, Location::RequiresRegister());
4618 }
4619
4620 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004621 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004622 ? Location::RequiresFpuRegister()
4623 : Location::RequiresRegister());
4624 } else {
4625 locations_to_set->SetOut(Location::SameAsFirstInput());
4626 }
4627 }
4628
4629 return can_move_conditionally;
4630}
4631
4632
4633void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4634 LocationSummary* locations = select->GetLocations();
4635 Location dst = locations->Out();
4636 Location false_src = locations->InAt(0);
4637 Location true_src = locations->InAt(1);
4638 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4639 GpuRegister cond_reg = TMP;
4640 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004642 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004643 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004644
4645 if (IsBooleanValueOrMaterializedCondition(cond)) {
4646 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4647 } else {
4648 HCondition* condition = cond->AsCondition();
4649 LocationSummary* cond_locations = cond->GetLocations();
4650 IfCondition if_cond = condition->GetCondition();
4651 cond_type = condition->InputAt(0)->GetType();
4652 switch (cond_type) {
4653 default:
4654 cond_inverted = MaterializeIntLongCompare(if_cond,
4655 /* is64bit */ false,
4656 cond_locations,
4657 cond_reg);
4658 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004659 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004660 cond_inverted = MaterializeIntLongCompare(if_cond,
4661 /* is64bit */ true,
4662 cond_locations,
4663 cond_reg);
4664 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004665 case DataType::Type::kFloat32:
4666 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004667 cond_inverted = MaterializeFpCompare(if_cond,
4668 condition->IsGtBias(),
4669 cond_type,
4670 cond_locations,
4671 fcond_reg);
4672 break;
4673 }
4674 }
4675
4676 if (true_src.IsConstant()) {
4677 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4678 }
4679 if (false_src.IsConstant()) {
4680 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4681 }
4682
4683 switch (dst_type) {
4684 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004685 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004686 __ Mfc1(cond_reg, fcond_reg);
4687 }
4688 if (true_src.IsConstant()) {
4689 if (cond_inverted) {
4690 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4691 } else {
4692 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4693 }
4694 } else if (false_src.IsConstant()) {
4695 if (cond_inverted) {
4696 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4697 } else {
4698 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4699 }
4700 } else {
4701 DCHECK_NE(cond_reg, AT);
4702 if (cond_inverted) {
4703 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4704 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4705 } else {
4706 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4707 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4708 }
4709 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4710 }
4711 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004712 case DataType::Type::kFloat32: {
4713 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004714 // sel*.fmt tests bit 0 of the condition register, account for that.
4715 __ Sltu(TMP, ZERO, cond_reg);
4716 __ Mtc1(TMP, fcond_reg);
4717 }
4718 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4719 if (true_src.IsConstant()) {
4720 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4721 if (cond_inverted) {
4722 __ SelnezS(dst_reg, src_reg, fcond_reg);
4723 } else {
4724 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4725 }
4726 } else if (false_src.IsConstant()) {
4727 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4728 if (cond_inverted) {
4729 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4730 } else {
4731 __ SelnezS(dst_reg, src_reg, fcond_reg);
4732 }
4733 } else {
4734 if (cond_inverted) {
4735 __ SelS(fcond_reg,
4736 true_src.AsFpuRegister<FpuRegister>(),
4737 false_src.AsFpuRegister<FpuRegister>());
4738 } else {
4739 __ SelS(fcond_reg,
4740 false_src.AsFpuRegister<FpuRegister>(),
4741 true_src.AsFpuRegister<FpuRegister>());
4742 }
4743 __ MovS(dst_reg, fcond_reg);
4744 }
4745 break;
4746 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 case DataType::Type::kFloat64: {
4748 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004749 // sel*.fmt tests bit 0 of the condition register, account for that.
4750 __ Sltu(TMP, ZERO, cond_reg);
4751 __ Mtc1(TMP, fcond_reg);
4752 }
4753 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4754 if (true_src.IsConstant()) {
4755 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4756 if (cond_inverted) {
4757 __ SelnezD(dst_reg, src_reg, fcond_reg);
4758 } else {
4759 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4760 }
4761 } else if (false_src.IsConstant()) {
4762 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4763 if (cond_inverted) {
4764 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4765 } else {
4766 __ SelnezD(dst_reg, src_reg, fcond_reg);
4767 }
4768 } else {
4769 if (cond_inverted) {
4770 __ SelD(fcond_reg,
4771 true_src.AsFpuRegister<FpuRegister>(),
4772 false_src.AsFpuRegister<FpuRegister>());
4773 } else {
4774 __ SelD(fcond_reg,
4775 false_src.AsFpuRegister<FpuRegister>(),
4776 true_src.AsFpuRegister<FpuRegister>());
4777 }
4778 __ MovD(dst_reg, fcond_reg);
4779 }
4780 break;
4781 }
4782 }
4783}
4784
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004785void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004786 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004787 LocationSummary(flag, LocationSummary::kNoCall);
4788 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004789}
4790
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004791void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4792 __ LoadFromOffset(kLoadWord,
4793 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4794 SP,
4795 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004796}
4797
David Brazdil74eb1b22015-12-14 11:44:01 +00004798void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004799 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004800 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004801}
4802
4803void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004804 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4805 GenConditionalMove(select);
4806 } else {
4807 LocationSummary* locations = select->GetLocations();
4808 Mips64Label false_target;
4809 GenerateTestAndBranch(select,
4810 /* condition_input_index */ 2,
4811 /* true_target */ nullptr,
4812 &false_target);
4813 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4814 __ Bind(&false_target);
4815 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004816}
4817
David Srbecky0cf44932015-12-09 14:09:59 +00004818void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004819 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004820}
4821
David Srbeckyd28f4a02016-03-14 17:14:24 +00004822void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4823 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004824}
4825
4826void CodeGeneratorMIPS64::GenerateNop() {
4827 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004828}
4829
Alexey Frunze4dda3372015-06-01 18:31:49 -07004830void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004831 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004833 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004835 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004836 instruction,
4837 object_field_get_with_read_barrier
4838 ? LocationSummary::kCallOnSlowPath
4839 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004840 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4841 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4842 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004843 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004844 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004845 locations->SetOut(Location::RequiresFpuRegister());
4846 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004847 // The output overlaps in the case of an object field get with
4848 // read barriers enabled: we do not want the move to overwrite the
4849 // object's location, as we need it to emit the read barrier.
4850 locations->SetOut(Location::RequiresRegister(),
4851 object_field_get_with_read_barrier
4852 ? Location::kOutputOverlap
4853 : Location::kNoOutputOverlap);
4854 }
4855 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4856 // We need a temporary register for the read barrier marking slow
4857 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004858 if (!kBakerReadBarrierThunksEnableForFields) {
4859 locations->AddTemp(Location::RequiresRegister());
4860 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004861 }
4862}
4863
4864void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4865 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004866 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4867 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004868 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004869 Location obj_loc = locations->InAt(0);
4870 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4871 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004872 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004873 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004874 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004875 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4876
Alexey Frunze4dda3372015-06-01 18:31:49 -07004877 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004878 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004879 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004880 load_type = kLoadUnsignedByte;
4881 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004882 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004883 load_type = kLoadSignedByte;
4884 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004885 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004886 load_type = kLoadUnsignedHalfword;
4887 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004888 case DataType::Type::kInt16:
4889 load_type = kLoadSignedHalfword;
4890 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004891 case DataType::Type::kInt32:
4892 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004893 load_type = kLoadWord;
4894 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004895 case DataType::Type::kInt64:
4896 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004897 load_type = kLoadDoubleword;
4898 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004899 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004900 load_type = kLoadUnsignedWord;
4901 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004902 case DataType::Type::kUint32:
4903 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004904 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004905 LOG(FATAL) << "Unreachable type " << type;
4906 UNREACHABLE();
4907 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004908 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004909 DCHECK(dst_loc.IsRegister());
4910 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004911 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004912 // /* HeapReference<Object> */ dst = *(obj + offset)
4913 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004914 Location temp_loc =
4915 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004916 // Note that a potential implicit null check is handled in this
4917 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4918 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4919 dst_loc,
4920 obj,
4921 offset,
4922 temp_loc,
4923 /* needs_null_check */ true);
4924 if (is_volatile) {
4925 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4926 }
4927 } else {
4928 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4929 if (is_volatile) {
4930 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4931 }
4932 // If read barriers are enabled, emit read barriers other than
4933 // Baker's using a slow path (and also unpoison the loaded
4934 // reference, if heap poisoning is enabled).
4935 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4936 }
4937 } else {
4938 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4939 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004940 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004941 DCHECK(dst_loc.IsFpuRegister());
4942 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004943 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004944 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004945
Alexey Frunze15958152017-02-09 19:08:30 -08004946 // Memory barriers, in the case of references, are handled in the
4947 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004948 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004949 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004950 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004951}
4952
4953void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4954 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4955 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004956 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004957 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004958 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004959 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004960 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004961 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004962 }
4963}
4964
4965void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004966 const FieldInfo& field_info,
4967 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004968 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004969 LocationSummary* locations = instruction->GetLocations();
4970 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004971 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004972 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004973 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004974 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4975 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004976 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4977
Alexey Frunze4dda3372015-06-01 18:31:49 -07004978 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004979 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004980 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004981 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004982 store_type = kStoreByte;
4983 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004984 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004985 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004986 store_type = kStoreHalfword;
4987 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004988 case DataType::Type::kInt32:
4989 case DataType::Type::kFloat32:
4990 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004991 store_type = kStoreWord;
4992 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004993 case DataType::Type::kInt64:
4994 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004995 store_type = kStoreDoubleword;
4996 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004997 case DataType::Type::kUint32:
4998 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004999 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07005000 LOG(FATAL) << "Unreachable type " << type;
5001 UNREACHABLE();
5002 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005003
Alexey Frunze15958152017-02-09 19:08:30 -08005004 if (is_volatile) {
5005 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
5006 }
5007
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01005008 if (value_location.IsConstant()) {
5009 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
5010 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
5011 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01005013 DCHECK(value_location.IsRegister());
5014 GpuRegister src = value_location.AsRegister<GpuRegister>();
5015 if (kPoisonHeapReferences && needs_write_barrier) {
5016 // Note that in the case where `value` is a null reference,
5017 // we do not enter this block, as a null reference does not
5018 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005019 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01005020 __ PoisonHeapReference(TMP, src);
5021 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
5022 } else {
5023 __ StoreToOffset(store_type, src, obj, offset, null_checker);
5024 }
5025 } else {
5026 DCHECK(value_location.IsFpuRegister());
5027 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
5028 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
5029 }
5030 }
Alexey Frunze15958152017-02-09 19:08:30 -08005031
Alexey Frunzec061de12017-02-14 13:27:23 -08005032 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01005033 DCHECK(value_location.IsRegister());
5034 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005035 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005036 }
Alexey Frunze15958152017-02-09 19:08:30 -08005037
5038 if (is_volatile) {
5039 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5040 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005041}
5042
5043void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5044 HandleFieldGet(instruction, instruction->GetFieldInfo());
5045}
5046
5047void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5048 HandleFieldGet(instruction, instruction->GetFieldInfo());
5049}
5050
5051void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5052 HandleFieldSet(instruction, instruction->GetFieldInfo());
5053}
5054
5055void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005056 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005057}
5058
Alexey Frunze15958152017-02-09 19:08:30 -08005059void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
5060 HInstruction* instruction,
5061 Location out,
5062 uint32_t offset,
5063 Location maybe_temp,
5064 ReadBarrierOption read_barrier_option) {
5065 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5066 if (read_barrier_option == kWithReadBarrier) {
5067 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005068 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
5069 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5070 }
Alexey Frunze15958152017-02-09 19:08:30 -08005071 if (kUseBakerReadBarrier) {
5072 // Load with fast path based Baker's read barrier.
5073 // /* HeapReference<Object> */ out = *(out + offset)
5074 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5075 out,
5076 out_reg,
5077 offset,
5078 maybe_temp,
5079 /* needs_null_check */ false);
5080 } else {
5081 // Load with slow path based read barrier.
5082 // Save the value of `out` into `maybe_temp` before overwriting it
5083 // in the following move operation, as we will need it for the
5084 // read barrier below.
5085 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
5086 // /* HeapReference<Object> */ out = *(out + offset)
5087 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5088 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5089 }
5090 } else {
5091 // Plain load with no read barrier.
5092 // /* HeapReference<Object> */ out = *(out + offset)
5093 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5094 __ MaybeUnpoisonHeapReference(out_reg);
5095 }
5096}
5097
5098void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
5099 HInstruction* instruction,
5100 Location out,
5101 Location obj,
5102 uint32_t offset,
5103 Location maybe_temp,
5104 ReadBarrierOption read_barrier_option) {
5105 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5106 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
5107 if (read_barrier_option == kWithReadBarrier) {
5108 CHECK(kEmitCompilerReadBarrier);
5109 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005110 if (!kBakerReadBarrierThunksEnableForFields) {
5111 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5112 }
Alexey Frunze15958152017-02-09 19:08:30 -08005113 // Load with fast path based Baker's read barrier.
5114 // /* HeapReference<Object> */ out = *(obj + offset)
5115 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5116 out,
5117 obj_reg,
5118 offset,
5119 maybe_temp,
5120 /* needs_null_check */ false);
5121 } else {
5122 // Load with slow path based read barrier.
5123 // /* HeapReference<Object> */ out = *(obj + offset)
5124 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5125 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5126 }
5127 } else {
5128 // Plain load with no read barrier.
5129 // /* HeapReference<Object> */ out = *(obj + offset)
5130 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5131 __ MaybeUnpoisonHeapReference(out_reg);
5132 }
5133}
5134
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005135static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5136 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5137 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5138 return reg - V0;
5139 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5140 return 13 + (reg - S2);
5141 } else if (reg == S8) { // One more.
5142 return 19;
5143 }
5144 LOG(FATAL) << "Unexpected register " << reg;
5145 UNREACHABLE();
5146}
5147
5148static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5149 int num = GetBakerMarkThunkNumber(reg) +
5150 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5151 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5152}
5153
5154static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5155 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5156 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5157}
5158
5159void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5160 Location root,
5161 GpuRegister obj,
5162 uint32_t offset,
5163 ReadBarrierOption read_barrier_option,
5164 Mips64Label* label_low) {
5165 if (label_low != nullptr) {
5166 DCHECK_EQ(offset, 0x5678u);
5167 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005168 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005169 if (read_barrier_option == kWithReadBarrier) {
5170 DCHECK(kEmitCompilerReadBarrier);
5171 if (kUseBakerReadBarrier) {
5172 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5173 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005174 if (kBakerReadBarrierThunksEnableForGcRoots) {
5175 // Note that we do not actually check the value of `GetIsGcMarking()`
5176 // to decide whether to mark the loaded GC root or not. Instead, we
5177 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5178 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5179 // vice versa.
5180 //
5181 // We use thunks for the slow path. That thunk checks the reference
5182 // and jumps to the entrypoint if needed.
5183 //
5184 // temp = Thread::Current()->pReadBarrierMarkReg00
5185 // // AKA &art_quick_read_barrier_mark_introspection.
5186 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5187 // if (temp != nullptr) {
5188 // temp = &gc_root_thunk<root_reg>
5189 // root = temp(root)
5190 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005191
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005192 const int32_t entry_point_offset =
5193 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5194 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5195 int16_t offset_low = Low16Bits(offset);
5196 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5197 // extension in lwu.
5198 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5199 GpuRegister base = short_offset ? obj : TMP;
5200 // Loading the entrypoint does not require a load acquire since it is only changed when
5201 // threads are suspended or running a checkpoint.
5202 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5203 if (!short_offset) {
5204 DCHECK(!label_low);
5205 __ Daui(base, obj, offset_high);
5206 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005207 Mips64Label skip_call;
5208 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005209 if (label_low != nullptr) {
5210 DCHECK(short_offset);
5211 __ Bind(label_low);
5212 }
5213 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5214 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5215 // in delay slot.
5216 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005217 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005218 } else {
5219 // Note that we do not actually check the value of `GetIsGcMarking()`
5220 // to decide whether to mark the loaded GC root or not. Instead, we
5221 // load into `temp` (T9) the read barrier mark entry point corresponding
5222 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5223 // is false, and vice versa.
5224 //
5225 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5226 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5227 // if (temp != null) {
5228 // root = temp(root)
5229 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005230
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005231 if (label_low != nullptr) {
5232 __ Bind(label_low);
5233 }
5234 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5235 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5236 static_assert(
5237 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5238 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5239 "have different sizes.");
5240 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5241 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5242 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005243
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005244 // Slow path marking the GC root `root`.
5245 Location temp = Location::RegisterLocation(T9);
5246 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005247 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005248 instruction,
5249 root,
5250 /*entrypoint*/ temp);
5251 codegen_->AddSlowPath(slow_path);
5252
5253 const int32_t entry_point_offset =
5254 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5255 // Loading the entrypoint does not require a load acquire since it is only changed when
5256 // threads are suspended or running a checkpoint.
5257 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5258 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5259 __ Bind(slow_path->GetExitLabel());
5260 }
Alexey Frunze15958152017-02-09 19:08:30 -08005261 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005262 if (label_low != nullptr) {
5263 __ Bind(label_low);
5264 }
Alexey Frunze15958152017-02-09 19:08:30 -08005265 // GC root loaded through a slow path for read barriers other
5266 // than Baker's.
5267 // /* GcRoot<mirror::Object>* */ root = obj + offset
5268 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5269 // /* mirror::Object* */ root = root->Read()
5270 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5271 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005272 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005273 if (label_low != nullptr) {
5274 __ Bind(label_low);
5275 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005276 // Plain GC root load with no read barrier.
5277 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5278 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5279 // Note that GC roots are not affected by heap poisoning, thus we
5280 // do not have to unpoison `root_reg` here.
5281 }
5282}
5283
Alexey Frunze15958152017-02-09 19:08:30 -08005284void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5285 Location ref,
5286 GpuRegister obj,
5287 uint32_t offset,
5288 Location temp,
5289 bool needs_null_check) {
5290 DCHECK(kEmitCompilerReadBarrier);
5291 DCHECK(kUseBakerReadBarrier);
5292
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005293 if (kBakerReadBarrierThunksEnableForFields) {
5294 // Note that we do not actually check the value of `GetIsGcMarking()`
5295 // to decide whether to mark the loaded reference or not. Instead, we
5296 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5297 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5298 // vice versa.
5299 //
5300 // We use thunks for the slow path. That thunk checks the reference
5301 // and jumps to the entrypoint if needed. If the holder is not gray,
5302 // it issues a load-load memory barrier and returns to the original
5303 // reference load.
5304 //
5305 // temp = Thread::Current()->pReadBarrierMarkReg00
5306 // // AKA &art_quick_read_barrier_mark_introspection.
5307 // if (temp != nullptr) {
5308 // temp = &field_array_thunk<holder_reg>
5309 // temp()
5310 // }
5311 // not_gray_return_address:
5312 // // If the offset is too large to fit into the lw instruction, we
5313 // // use an adjusted base register (TMP) here. This register
5314 // // receives bits 16 ... 31 of the offset before the thunk invocation
5315 // // and the thunk benefits from it.
5316 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5317 // gray_return_address:
5318
5319 DCHECK(temp.IsInvalid());
5320 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5321 const int32_t entry_point_offset =
5322 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5323 // There may have or may have not been a null check if the field offset is smaller than
5324 // the page size.
5325 // There must've been a null check in case it's actually a load from an array.
5326 // We will, however, perform an explicit null check in the thunk as it's easier to
5327 // do it than not.
5328 if (instruction->IsArrayGet()) {
5329 DCHECK(!needs_null_check);
5330 }
5331 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5332 // Loading the entrypoint does not require a load acquire since it is only changed when
5333 // threads are suspended or running a checkpoint.
5334 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5335 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005336 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005337 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005338 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005339 __ Nop(); // In forbidden slot.
5340 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005341 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005342 // /* HeapReference<Object> */ ref = *(obj + offset)
5343 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5344 } else {
5345 int16_t offset_low = Low16Bits(offset);
5346 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005347 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005348 __ Daui(TMP, obj, offset_high); // In delay slot.
5349 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005350 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005351 // /* HeapReference<Object> */ ref = *(obj + offset)
5352 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5353 }
5354 if (needs_null_check) {
5355 MaybeRecordImplicitNullCheck(instruction);
5356 }
5357 __ MaybeUnpoisonHeapReference(ref_reg);
5358 return;
5359 }
5360
Alexey Frunze15958152017-02-09 19:08:30 -08005361 // /* HeapReference<Object> */ ref = *(obj + offset)
5362 Location no_index = Location::NoLocation();
5363 ScaleFactor no_scale_factor = TIMES_1;
5364 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5365 ref,
5366 obj,
5367 offset,
5368 no_index,
5369 no_scale_factor,
5370 temp,
5371 needs_null_check);
5372}
5373
5374void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5375 Location ref,
5376 GpuRegister obj,
5377 uint32_t data_offset,
5378 Location index,
5379 Location temp,
5380 bool needs_null_check) {
5381 DCHECK(kEmitCompilerReadBarrier);
5382 DCHECK(kUseBakerReadBarrier);
5383
5384 static_assert(
5385 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5386 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005387 ScaleFactor scale_factor = TIMES_4;
5388
5389 if (kBakerReadBarrierThunksEnableForArrays) {
5390 // Note that we do not actually check the value of `GetIsGcMarking()`
5391 // to decide whether to mark the loaded reference or not. Instead, we
5392 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5393 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5394 // vice versa.
5395 //
5396 // We use thunks for the slow path. That thunk checks the reference
5397 // and jumps to the entrypoint if needed. If the holder is not gray,
5398 // it issues a load-load memory barrier and returns to the original
5399 // reference load.
5400 //
5401 // temp = Thread::Current()->pReadBarrierMarkReg00
5402 // // AKA &art_quick_read_barrier_mark_introspection.
5403 // if (temp != nullptr) {
5404 // temp = &field_array_thunk<holder_reg>
5405 // temp()
5406 // }
5407 // not_gray_return_address:
5408 // // The element address is pre-calculated in the TMP register before the
5409 // // thunk invocation and the thunk benefits from it.
5410 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5411 // gray_return_address:
5412
5413 DCHECK(temp.IsInvalid());
5414 DCHECK(index.IsValid());
5415 const int32_t entry_point_offset =
5416 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5417 // We will not do the explicit null check in the thunk as some form of a null check
5418 // must've been done earlier.
5419 DCHECK(!needs_null_check);
5420 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5421 // Loading the entrypoint does not require a load acquire since it is only changed when
5422 // threads are suspended or running a checkpoint.
5423 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005424 Mips64Label skip_call;
5425 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005426 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5427 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5428 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5429 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005430 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005431 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5432 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5433 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5434 __ MaybeUnpoisonHeapReference(ref_reg);
5435 return;
5436 }
5437
Alexey Frunze15958152017-02-09 19:08:30 -08005438 // /* HeapReference<Object> */ ref =
5439 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005440 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5441 ref,
5442 obj,
5443 data_offset,
5444 index,
5445 scale_factor,
5446 temp,
5447 needs_null_check);
5448}
5449
5450void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5451 Location ref,
5452 GpuRegister obj,
5453 uint32_t offset,
5454 Location index,
5455 ScaleFactor scale_factor,
5456 Location temp,
5457 bool needs_null_check,
5458 bool always_update_field) {
5459 DCHECK(kEmitCompilerReadBarrier);
5460 DCHECK(kUseBakerReadBarrier);
5461
5462 // In slow path based read barriers, the read barrier call is
5463 // inserted after the original load. However, in fast path based
5464 // Baker's read barriers, we need to perform the load of
5465 // mirror::Object::monitor_ *before* the original reference load.
5466 // This load-load ordering is required by the read barrier.
5467 // The fast path/slow path (for Baker's algorithm) should look like:
5468 //
5469 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5470 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5471 // HeapReference<Object> ref = *src; // Original reference load.
5472 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5473 // if (is_gray) {
5474 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5475 // }
5476 //
5477 // Note: the original implementation in ReadBarrier::Barrier is
5478 // slightly more complex as it performs additional checks that we do
5479 // not do here for performance reasons.
5480
5481 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5482 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5483 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5484
5485 // /* int32_t */ monitor = obj->monitor_
5486 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5487 if (needs_null_check) {
5488 MaybeRecordImplicitNullCheck(instruction);
5489 }
5490 // /* LockWord */ lock_word = LockWord(monitor)
5491 static_assert(sizeof(LockWord) == sizeof(int32_t),
5492 "art::LockWord and int32_t have different sizes.");
5493
5494 __ Sync(0); // Barrier to prevent load-load reordering.
5495
5496 // The actual reference load.
5497 if (index.IsValid()) {
5498 // Load types involving an "index": ArrayGet,
5499 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5500 // intrinsics.
5501 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5502 if (index.IsConstant()) {
5503 size_t computed_offset =
5504 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5505 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5506 } else {
5507 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005508 if (scale_factor == TIMES_1) {
5509 __ Daddu(TMP, index_reg, obj);
5510 } else {
5511 __ Dlsa(TMP, index_reg, obj, scale_factor);
5512 }
Alexey Frunze15958152017-02-09 19:08:30 -08005513 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5514 }
5515 } else {
5516 // /* HeapReference<Object> */ ref = *(obj + offset)
5517 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5518 }
5519
5520 // Object* ref = ref_addr->AsMirrorPtr()
5521 __ MaybeUnpoisonHeapReference(ref_reg);
5522
5523 // Slow path marking the object `ref` when it is gray.
5524 SlowPathCodeMIPS64* slow_path;
5525 if (always_update_field) {
5526 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5527 // of the form `obj + field_offset`, where `obj` is a register and
5528 // `field_offset` is a register. Thus `offset` and `scale_factor`
5529 // above are expected to be null in this code path.
5530 DCHECK_EQ(offset, 0u);
5531 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005532 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005533 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5534 ref,
5535 obj,
5536 /* field_offset */ index,
5537 temp_reg);
5538 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005539 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005540 }
5541 AddSlowPath(slow_path);
5542
5543 // if (rb_state == ReadBarrier::GrayState())
5544 // ref = ReadBarrier::Mark(ref);
5545 // Given the numeric representation, it's enough to check the low bit of the
5546 // rb_state. We do that by shifting the bit into the sign bit (31) and
5547 // performing a branch on less than zero.
5548 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5549 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5550 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5551 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5552 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5553 __ Bind(slow_path->GetExitLabel());
5554}
5555
5556void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5557 Location out,
5558 Location ref,
5559 Location obj,
5560 uint32_t offset,
5561 Location index) {
5562 DCHECK(kEmitCompilerReadBarrier);
5563
5564 // Insert a slow path based read barrier *after* the reference load.
5565 //
5566 // If heap poisoning is enabled, the unpoisoning of the loaded
5567 // reference will be carried out by the runtime within the slow
5568 // path.
5569 //
5570 // Note that `ref` currently does not get unpoisoned (when heap
5571 // poisoning is enabled), which is alright as the `ref` argument is
5572 // not used by the artReadBarrierSlow entry point.
5573 //
5574 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005575 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005576 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5577 AddSlowPath(slow_path);
5578
5579 __ Bc(slow_path->GetEntryLabel());
5580 __ Bind(slow_path->GetExitLabel());
5581}
5582
5583void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5584 Location out,
5585 Location ref,
5586 Location obj,
5587 uint32_t offset,
5588 Location index) {
5589 if (kEmitCompilerReadBarrier) {
5590 // Baker's read barriers shall be handled by the fast path
5591 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5592 DCHECK(!kUseBakerReadBarrier);
5593 // If heap poisoning is enabled, unpoisoning will be taken care of
5594 // by the runtime within the slow path.
5595 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5596 } else if (kPoisonHeapReferences) {
5597 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5598 }
5599}
5600
5601void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5602 Location out,
5603 Location root) {
5604 DCHECK(kEmitCompilerReadBarrier);
5605
5606 // Insert a slow path based read barrier *after* the GC root load.
5607 //
5608 // Note that GC roots are not affected by heap poisoning, so we do
5609 // not need to do anything special for this here.
5610 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005611 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005612 AddSlowPath(slow_path);
5613
5614 __ Bc(slow_path->GetEntryLabel());
5615 __ Bind(slow_path->GetExitLabel());
5616}
5617
Alexey Frunze4dda3372015-06-01 18:31:49 -07005618void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005619 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5620 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005621 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005622 switch (type_check_kind) {
5623 case TypeCheckKind::kExactCheck:
5624 case TypeCheckKind::kAbstractClassCheck:
5625 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005626 case TypeCheckKind::kArrayObjectCheck: {
5627 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5628 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5629 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005630 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005631 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005632 case TypeCheckKind::kArrayCheck:
5633 case TypeCheckKind::kUnresolvedCheck:
5634 case TypeCheckKind::kInterfaceCheck:
5635 call_kind = LocationSummary::kCallOnSlowPath;
5636 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00005637 case TypeCheckKind::kBitstringCheck:
5638 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005639 }
5640
Vladimir Markoca6fff82017-10-03 14:49:14 +01005641 LocationSummary* locations =
5642 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005643 if (baker_read_barrier_slow_path) {
5644 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5645 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005646 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00005647 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
5648 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
5649 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
5650 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
5651 } else {
5652 locations->SetInAt(1, Location::RequiresRegister());
5653 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005654 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005655 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005656 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005657 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005658}
5659
5660void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005661 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005662 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005663 Location obj_loc = locations->InAt(0);
5664 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00005665 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08005666 Location out_loc = locations->Out();
5667 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5668 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5669 DCHECK_LE(num_temps, 1u);
5670 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005671 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5672 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5673 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5674 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005675 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005676 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005677
5678 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005679 // Avoid this check if we know `obj` is not null.
5680 if (instruction->MustDoNullCheck()) {
5681 __ Move(out, ZERO);
5682 __ Beqzc(obj, &done);
5683 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005684
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005685 switch (type_check_kind) {
5686 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005687 ReadBarrierOption read_barrier_option =
5688 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005689 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005690 GenerateReferenceLoadTwoRegisters(instruction,
5691 out_loc,
5692 obj_loc,
5693 class_offset,
5694 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005695 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005696 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00005697 __ Xor(out, out, cls.AsRegister<GpuRegister>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005698 __ Sltiu(out, out, 1);
5699 break;
5700 }
5701
5702 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005703 ReadBarrierOption read_barrier_option =
5704 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005705 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005706 GenerateReferenceLoadTwoRegisters(instruction,
5707 out_loc,
5708 obj_loc,
5709 class_offset,
5710 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005711 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005712 // If the class is abstract, we eagerly fetch the super class of the
5713 // object to avoid doing a comparison we know will fail.
5714 Mips64Label loop;
5715 __ Bind(&loop);
5716 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005717 GenerateReferenceLoadOneRegister(instruction,
5718 out_loc,
5719 super_offset,
5720 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005721 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005722 // If `out` is null, we use it for the result, and jump to `done`.
5723 __ Beqzc(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00005724 __ Bnec(out, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005725 __ LoadConst32(out, 1);
5726 break;
5727 }
5728
5729 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005730 ReadBarrierOption read_barrier_option =
5731 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005732 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005733 GenerateReferenceLoadTwoRegisters(instruction,
5734 out_loc,
5735 obj_loc,
5736 class_offset,
5737 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005738 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005739 // Walk over the class hierarchy to find a match.
5740 Mips64Label loop, success;
5741 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00005742 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005743 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005744 GenerateReferenceLoadOneRegister(instruction,
5745 out_loc,
5746 super_offset,
5747 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005748 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005749 __ Bnezc(out, &loop);
5750 // If `out` is null, we use it for the result, and jump to `done`.
5751 __ Bc(&done);
5752 __ Bind(&success);
5753 __ LoadConst32(out, 1);
5754 break;
5755 }
5756
5757 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005758 ReadBarrierOption read_barrier_option =
5759 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005760 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005761 GenerateReferenceLoadTwoRegisters(instruction,
5762 out_loc,
5763 obj_loc,
5764 class_offset,
5765 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005766 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005767 // Do an exact check.
5768 Mips64Label success;
Vladimir Marko175e7862018-03-27 09:03:13 +00005769 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005770 // Otherwise, we need to check that the object's class is a non-primitive array.
5771 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005772 GenerateReferenceLoadOneRegister(instruction,
5773 out_loc,
5774 component_offset,
5775 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005776 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005777 // If `out` is null, we use it for the result, and jump to `done`.
5778 __ Beqzc(out, &done);
5779 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5780 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5781 __ Sltiu(out, out, 1);
5782 __ Bc(&done);
5783 __ Bind(&success);
5784 __ LoadConst32(out, 1);
5785 break;
5786 }
5787
5788 case TypeCheckKind::kArrayCheck: {
5789 // No read barrier since the slow path will retry upon failure.
5790 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005791 GenerateReferenceLoadTwoRegisters(instruction,
5792 out_loc,
5793 obj_loc,
5794 class_offset,
5795 maybe_temp_loc,
5796 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005797 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005798 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5799 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005800 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00005801 __ Bnec(out, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005802 __ LoadConst32(out, 1);
5803 break;
5804 }
5805
5806 case TypeCheckKind::kUnresolvedCheck:
5807 case TypeCheckKind::kInterfaceCheck: {
5808 // Note that we indeed only call on slow path, but we always go
5809 // into the slow path for the unresolved and interface check
5810 // cases.
5811 //
5812 // We cannot directly call the InstanceofNonTrivial runtime
5813 // entry point without resorting to a type checking slow path
5814 // here (i.e. by calling InvokeRuntime directly), as it would
5815 // require to assign fixed registers for the inputs of this
5816 // HInstanceOf instruction (following the runtime calling
5817 // convention), which might be cluttered by the potential first
5818 // read barrier emission at the beginning of this method.
5819 //
5820 // TODO: Introduce a new runtime entry point taking the object
5821 // to test (instead of its class) as argument, and let it deal
5822 // with the read barrier issues. This will let us refactor this
5823 // case of the `switch` code as it was previously (with a direct
5824 // call to the runtime not using a type checking slow path).
5825 // This should also be beneficial for the other cases above.
5826 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005827 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5828 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005829 codegen_->AddSlowPath(slow_path);
5830 __ Bc(slow_path->GetEntryLabel());
5831 break;
5832 }
Vladimir Marko175e7862018-03-27 09:03:13 +00005833
5834 case TypeCheckKind::kBitstringCheck: {
5835 // /* HeapReference<Class> */ temp = obj->klass_
5836 GenerateReferenceLoadTwoRegisters(instruction,
5837 out_loc,
5838 obj_loc,
5839 class_offset,
5840 maybe_temp_loc,
5841 kWithoutReadBarrier);
5842
5843 GenerateBitstringTypeCheckCompare(instruction, out);
5844 __ Sltiu(out, out, 1);
5845 break;
5846 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005847 }
5848
5849 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005850
5851 if (slow_path != nullptr) {
5852 __ Bind(slow_path->GetExitLabel());
5853 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005854}
5855
5856void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005857 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005858 locations->SetOut(Location::ConstantLocation(constant));
5859}
5860
5861void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5862 // Will be generated at use site.
5863}
5864
5865void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005866 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005867 locations->SetOut(Location::ConstantLocation(constant));
5868}
5869
5870void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5871 // Will be generated at use site.
5872}
5873
Calin Juravle175dc732015-08-25 15:42:32 +01005874void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5875 // The trampoline uses the same calling convention as dex calling conventions,
5876 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5877 // the method_idx.
5878 HandleInvoke(invoke);
5879}
5880
5881void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5882 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5883}
5884
Alexey Frunze4dda3372015-06-01 18:31:49 -07005885void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5886 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5887 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5888}
5889
5890void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5891 HandleInvoke(invoke);
5892 // The register T0 is required to be used for the hidden argument in
5893 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5894 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5895}
5896
5897void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5898 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5899 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005900 Location receiver = invoke->GetLocations()->InAt(0);
5901 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005902 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005903
5904 // Set the hidden argument.
5905 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5906 invoke->GetDexMethodIndex());
5907
5908 // temp = object->GetClass();
5909 if (receiver.IsStackSlot()) {
5910 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5911 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5912 } else {
5913 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5914 }
5915 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005916 // Instead of simply (possibly) unpoisoning `temp` here, we should
5917 // emit a read barrier for the previous class reference load.
5918 // However this is not required in practice, as this is an
5919 // intermediate/temporary reference and because the current
5920 // concurrent copying collector keeps the from-space memory
5921 // intact/accessible until the end of the marking phase (the
5922 // concurrent copying collector may not in the future).
5923 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005924 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5925 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5926 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005927 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005928 // temp = temp->GetImtEntryAt(method_offset);
5929 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5930 // T9 = temp->GetEntryPoint();
5931 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5932 // T9();
5933 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005934 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005935 DCHECK(!codegen_->IsLeafMethod());
5936 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5937}
5938
5939void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005940 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5941 if (intrinsic.TryDispatch(invoke)) {
5942 return;
5943 }
5944
Alexey Frunze4dda3372015-06-01 18:31:49 -07005945 HandleInvoke(invoke);
5946}
5947
5948void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005949 // Explicit clinit checks triggered by static invokes must have been pruned by
5950 // art::PrepareForRegisterAllocation.
5951 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005952
Chris Larsen3039e382015-08-26 07:54:08 -07005953 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5954 if (intrinsic.TryDispatch(invoke)) {
5955 return;
5956 }
5957
Alexey Frunze4dda3372015-06-01 18:31:49 -07005958 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005959}
5960
Orion Hodsonac141392017-01-13 11:53:47 +00005961void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5962 HandleInvoke(invoke);
5963}
5964
5965void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5966 codegen_->GenerateInvokePolymorphicCall(invoke);
5967}
5968
Orion Hodson4c8e12e2018-05-18 08:33:20 +01005969void LocationsBuilderMIPS64::VisitInvokeCustom(HInvokeCustom* invoke) {
5970 HandleInvoke(invoke);
5971}
5972
5973void InstructionCodeGeneratorMIPS64::VisitInvokeCustom(HInvokeCustom* invoke) {
5974 codegen_->GenerateInvokeCustomCall(invoke);
5975}
5976
Chris Larsen3039e382015-08-26 07:54:08 -07005977static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005978 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005979 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5980 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005981 return true;
5982 }
5983 return false;
5984}
5985
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005986HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005987 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005988 bool fallback_load = false;
5989 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005990 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005991 case HLoadString::LoadKind::kBootImageRelRo:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005992 case HLoadString::LoadKind::kBssEntry:
5993 DCHECK(!Runtime::Current()->UseJitCompilation());
5994 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005995 case HLoadString::LoadKind::kJitBootImageAddress:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005996 case HLoadString::LoadKind::kJitTableAddress:
5997 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005998 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005999 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01006000 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006001 }
6002 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006003 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006004 }
6005 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006006}
6007
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006008HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
6009 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006010 bool fallback_load = false;
6011 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006012 case HLoadClass::LoadKind::kInvalid:
6013 LOG(FATAL) << "UNREACHABLE";
6014 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006015 case HLoadClass::LoadKind::kReferrersClass:
6016 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006017 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006018 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006019 case HLoadClass::LoadKind::kBssEntry:
6020 DCHECK(!Runtime::Current()->UseJitCompilation());
6021 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006022 case HLoadClass::LoadKind::kJitBootImageAddress:
Alexey Frunzef63f5692016-12-13 17:43:11 -08006023 case HLoadClass::LoadKind::kJitTableAddress:
6024 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006025 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006026 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08006027 break;
6028 }
6029 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006030 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006031 }
6032 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006033}
6034
Vladimir Markodc151b22015-10-15 18:02:30 +01006035HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
6036 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006037 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08006038 // On MIPS64 we support all dispatch types.
6039 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006040}
6041
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006042void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
6043 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006044 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00006045 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08006046 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
6047 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
6048
Alexey Frunze19f6c692016-11-30 19:19:55 -08006049 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006050 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00006051 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006052 uint32_t offset =
6053 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00006054 __ LoadFromOffset(kLoadDoubleword,
6055 temp.AsRegister<GpuRegister>(),
6056 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006057 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00006058 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006059 }
Vladimir Marko58155012015-08-19 12:49:41 +00006060 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006061 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006062 break;
Vladimir Marko65979462017-05-19 17:25:12 +01006063 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
6064 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006065 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006066 NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006067 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006068 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006069 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01006070 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6071 break;
6072 }
Vladimir Markob066d432018-01-03 13:14:37 +00006073 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006074 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00006075 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
6076 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
6077 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6078 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
6079 __ Lwu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6080 break;
6081 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006082 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006083 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006084 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006085 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
6086 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
6087 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08006088 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6089 break;
6090 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006091 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
6092 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
6093 kLoadDoubleword,
6094 DeduplicateUint64Literal(invoke->GetMethodAddress()));
6095 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006096 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
6097 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
6098 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07006099 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006100 }
6101
Alexey Frunze19f6c692016-11-30 19:19:55 -08006102 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00006103 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006104 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00006105 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006106 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6107 // T9 = callee_method->entry_point_from_quick_compiled_code_;
6108 __ LoadFromOffset(kLoadDoubleword,
6109 T9,
6110 callee_method.AsRegister<GpuRegister>(),
6111 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006112 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006113 // T9()
6114 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006115 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00006116 break;
6117 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006118 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
6119
Alexey Frunze4dda3372015-06-01 18:31:49 -07006120 DCHECK(!IsLeafMethod());
6121}
6122
6123void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006124 // Explicit clinit checks triggered by static invokes must have been pruned by
6125 // art::PrepareForRegisterAllocation.
6126 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006127
6128 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6129 return;
6130 }
6131
6132 LocationSummary* locations = invoke->GetLocations();
6133 codegen_->GenerateStaticOrDirectCall(invoke,
6134 locations->HasTemps()
6135 ? locations->GetTemp(0)
6136 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006137}
6138
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006139void CodeGeneratorMIPS64::GenerateVirtualCall(
6140 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006141 // Use the calling convention instead of the location of the receiver, as
6142 // intrinsics may have put the receiver in a different register. In the intrinsics
6143 // slow path, the arguments have been moved to the right place, so here we are
6144 // guaranteed that the receiver is the first register of the calling convention.
6145 InvokeDexCallingConvention calling_convention;
6146 GpuRegister receiver = calling_convention.GetRegisterAt(0);
6147
Alexey Frunze53afca12015-11-05 16:34:23 -08006148 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006149 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6150 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
6151 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006152 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006153
6154 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006155 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08006156 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006157 // Instead of simply (possibly) unpoisoning `temp` here, we should
6158 // emit a read barrier for the previous class reference load.
6159 // However this is not required in practice, as this is an
6160 // intermediate/temporary reference and because the current
6161 // concurrent copying collector keeps the from-space memory
6162 // intact/accessible until the end of the marking phase (the
6163 // concurrent copying collector may not in the future).
6164 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006165 // temp = temp->GetMethodAt(method_offset);
6166 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6167 // T9 = temp->GetEntryPoint();
6168 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6169 // T9();
6170 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006171 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006172 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006173}
6174
6175void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6176 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6177 return;
6178 }
6179
6180 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006181 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006182}
6183
6184void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006185 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006186 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006187 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006188 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6189 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006190 return;
6191 }
Vladimir Marko41559982017-01-06 14:04:23 +00006192 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006193
Alexey Frunze15958152017-02-09 19:08:30 -08006194 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6195 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006196 ? LocationSummary::kCallOnSlowPath
6197 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006198 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006199 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6200 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6201 }
Vladimir Marko41559982017-01-06 14:04:23 +00006202 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006203 locations->SetInAt(0, Location::RequiresRegister());
6204 }
6205 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006206 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6207 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6208 // Rely on the type resolution or initialization and marking to save everything we need.
6209 RegisterSet caller_saves = RegisterSet::Empty();
6210 InvokeRuntimeCallingConvention calling_convention;
6211 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6212 locations->SetCustomSlowPathCallerSaves(caller_saves);
6213 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006214 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006215 }
6216 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006217}
6218
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006219// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6220// move.
6221void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006222 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006223 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006224 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006225 return;
6226 }
Vladimir Marko41559982017-01-06 14:04:23 +00006227 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006228
Vladimir Marko41559982017-01-06 14:04:23 +00006229 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006230 Location out_loc = locations->Out();
6231 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6232 GpuRegister current_method_reg = ZERO;
6233 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006234 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006235 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6236 }
6237
Alexey Frunze15958152017-02-09 19:08:30 -08006238 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6239 ? kWithoutReadBarrier
6240 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006241 bool generate_null_check = false;
6242 switch (load_kind) {
6243 case HLoadClass::LoadKind::kReferrersClass:
6244 DCHECK(!cls->CanCallRuntime());
6245 DCHECK(!cls->MustGenerateClinitCheck());
6246 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6247 GenerateGcRootFieldLoad(cls,
6248 out_loc,
6249 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006250 ArtMethod::DeclaringClassOffset().Int32Value(),
6251 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006252 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006253 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006254 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006255 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006256 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006257 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006258 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006259 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006260 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006261 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6262 break;
6263 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006264 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006265 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006266 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006267 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006268 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006269 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006270 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006271 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6272 __ Lwu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006273 break;
6274 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006275 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006276 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6277 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006278 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6279 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006280 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006281 GenerateGcRootFieldLoad(cls,
6282 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006283 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006284 /* placeholder */ 0x5678,
6285 read_barrier_option,
6286 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006287 generate_null_check = true;
6288 break;
6289 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006290 case HLoadClass::LoadKind::kJitBootImageAddress: {
6291 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6292 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6293 DCHECK_NE(address, 0u);
6294 __ LoadLiteral(out,
6295 kLoadUnsignedWord,
6296 codegen_->DeduplicateBootImageAddressLiteral(address));
6297 break;
6298 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006299 case HLoadClass::LoadKind::kJitTableAddress:
6300 __ LoadLiteral(out,
6301 kLoadUnsignedWord,
6302 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6303 cls->GetTypeIndex(),
6304 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006305 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006306 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006307 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006308 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006309 LOG(FATAL) << "UNREACHABLE";
6310 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006311 }
6312
6313 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6314 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006315 SlowPathCodeMIPS64* slow_path =
6316 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(cls, cls);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006317 codegen_->AddSlowPath(slow_path);
6318 if (generate_null_check) {
6319 __ Beqzc(out, slow_path->GetEntryLabel());
6320 }
6321 if (cls->MustGenerateClinitCheck()) {
6322 GenerateClassInitializationCheck(slow_path, out);
6323 } else {
6324 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006325 }
6326 }
6327}
6328
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006329void LocationsBuilderMIPS64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6330 InvokeRuntimeCallingConvention calling_convention;
6331 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6332 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
6333}
6334
6335void InstructionCodeGeneratorMIPS64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6336 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6337}
6338
Orion Hodson18259d72018-04-12 11:18:23 +01006339void LocationsBuilderMIPS64::VisitLoadMethodType(HLoadMethodType* load) {
6340 InvokeRuntimeCallingConvention calling_convention;
6341 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6342 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
6343}
6344
6345void InstructionCodeGeneratorMIPS64::VisitLoadMethodType(HLoadMethodType* load) {
6346 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6347}
6348
David Brazdilcb1c0552015-08-04 16:22:25 +01006349static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006350 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006351}
6352
Alexey Frunze4dda3372015-06-01 18:31:49 -07006353void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6354 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006355 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006356 locations->SetOut(Location::RequiresRegister());
6357}
6358
6359void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6360 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006361 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6362}
6363
6364void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006365 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006366}
6367
6368void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6369 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006370}
6371
Alexey Frunze4dda3372015-06-01 18:31:49 -07006372void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006373 HLoadString::LoadKind load_kind = load->GetLoadKind();
6374 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006375 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006376 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006377 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006378 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006379 } else {
6380 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006381 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6382 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6383 // Rely on the pResolveString and marking to save everything we need.
6384 RegisterSet caller_saves = RegisterSet::Empty();
6385 InvokeRuntimeCallingConvention calling_convention;
6386 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6387 locations->SetCustomSlowPathCallerSaves(caller_saves);
6388 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006389 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006390 }
6391 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006392 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006393}
6394
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006395// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6396// move.
6397void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006398 HLoadString::LoadKind load_kind = load->GetLoadKind();
6399 LocationSummary* locations = load->GetLocations();
6400 Location out_loc = locations->Out();
6401 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6402
6403 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006404 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6405 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006406 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006407 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006408 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006409 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006410 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006411 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006412 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006413 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006414 case HLoadString::LoadKind::kBootImageRelRo: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006415 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006416 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006417 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006418 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006419 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006420 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006421 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6422 __ Lwu(out, AT, /* placeholder */ 0x5678);
6423 return;
6424 }
6425 case HLoadString::LoadKind::kBssEntry: {
6426 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6427 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6428 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6429 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6430 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006431 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006432 GenerateGcRootFieldLoad(load,
6433 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006434 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006435 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006436 kCompilerReadBarrierOption,
6437 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006438 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006439 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006440 codegen_->AddSlowPath(slow_path);
6441 __ Beqzc(out, slow_path->GetEntryLabel());
6442 __ Bind(slow_path->GetExitLabel());
6443 return;
6444 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006445 case HLoadString::LoadKind::kJitBootImageAddress: {
6446 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6447 DCHECK_NE(address, 0u);
6448 __ LoadLiteral(out,
6449 kLoadUnsignedWord,
6450 codegen_->DeduplicateBootImageAddressLiteral(address));
6451 return;
6452 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006453 case HLoadString::LoadKind::kJitTableAddress:
6454 __ LoadLiteral(out,
6455 kLoadUnsignedWord,
6456 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6457 load->GetStringIndex(),
6458 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006459 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006460 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006461 default:
6462 break;
6463 }
6464
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006465 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006466 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006467 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006468 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006469 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6470 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6471 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006472}
6473
Alexey Frunze4dda3372015-06-01 18:31:49 -07006474void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006475 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006476 locations->SetOut(Location::ConstantLocation(constant));
6477}
6478
6479void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6480 // Will be generated at use site.
6481}
6482
6483void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006484 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6485 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006486 InvokeRuntimeCallingConvention calling_convention;
6487 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6488}
6489
6490void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006491 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006492 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006493 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006494 if (instruction->IsEnter()) {
6495 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6496 } else {
6497 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6498 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006499}
6500
6501void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6502 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006503 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006504 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006505 case DataType::Type::kInt32:
6506 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006507 locations->SetInAt(0, Location::RequiresRegister());
6508 locations->SetInAt(1, Location::RequiresRegister());
6509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6510 break;
6511
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006512 case DataType::Type::kFloat32:
6513 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006514 locations->SetInAt(0, Location::RequiresFpuRegister());
6515 locations->SetInAt(1, Location::RequiresFpuRegister());
6516 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6517 break;
6518
6519 default:
6520 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6521 }
6522}
6523
6524void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006525 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006526 LocationSummary* locations = instruction->GetLocations();
6527
6528 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006529 case DataType::Type::kInt32:
6530 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006531 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6532 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6533 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006534 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006535 __ MulR6(dst, lhs, rhs);
6536 else
6537 __ Dmul(dst, lhs, rhs);
6538 break;
6539 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006540 case DataType::Type::kFloat32:
6541 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006542 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6543 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6544 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006546 __ MulS(dst, lhs, rhs);
6547 else
6548 __ MulD(dst, lhs, rhs);
6549 break;
6550 }
6551 default:
6552 LOG(FATAL) << "Unexpected mul type " << type;
6553 }
6554}
6555
6556void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6557 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006558 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006559 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006560 case DataType::Type::kInt32:
6561 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006562 locations->SetInAt(0, Location::RequiresRegister());
6563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6564 break;
6565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006566 case DataType::Type::kFloat32:
6567 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006568 locations->SetInAt(0, Location::RequiresFpuRegister());
6569 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6570 break;
6571
6572 default:
6573 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6574 }
6575}
6576
6577void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006578 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006579 LocationSummary* locations = instruction->GetLocations();
6580
6581 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006582 case DataType::Type::kInt32:
6583 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006584 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6585 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006586 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006587 __ Subu(dst, ZERO, src);
6588 else
6589 __ Dsubu(dst, ZERO, src);
6590 break;
6591 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006592 case DataType::Type::kFloat32:
6593 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006594 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6595 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006596 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006597 __ NegS(dst, src);
6598 else
6599 __ NegD(dst, src);
6600 break;
6601 }
6602 default:
6603 LOG(FATAL) << "Unexpected neg type " << type;
6604 }
6605}
6606
6607void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006608 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6609 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006610 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006611 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006612 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6613 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006614}
6615
6616void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006617 // Note: if heap poisoning is enabled, the entry point takes care
6618 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006619 QuickEntrypointEnum entrypoint =
6620 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6621 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006622 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006623 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006624}
6625
6626void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006627 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6628 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006629 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07006630 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006632}
6633
6634void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07006635 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
6636 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006637}
6638
6639void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006640 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006641 locations->SetInAt(0, Location::RequiresRegister());
6642 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6643}
6644
6645void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006646 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006647 LocationSummary* locations = instruction->GetLocations();
6648
6649 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 case DataType::Type::kInt32:
6651 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006652 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6653 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6654 __ Nor(dst, src, ZERO);
6655 break;
6656 }
6657
6658 default:
6659 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6660 }
6661}
6662
6663void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006664 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006665 locations->SetInAt(0, Location::RequiresRegister());
6666 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6667}
6668
6669void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6670 LocationSummary* locations = instruction->GetLocations();
6671 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6672 locations->InAt(0).AsRegister<GpuRegister>(),
6673 1);
6674}
6675
6676void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006677 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6678 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006679}
6680
Calin Juravle2ae48182016-03-16 14:05:09 +00006681void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6682 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006683 return;
6684 }
6685 Location obj = instruction->GetLocations()->InAt(0);
6686
6687 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006688 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006689}
6690
Calin Juravle2ae48182016-03-16 14:05:09 +00006691void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006692 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006693 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006694 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006695
6696 Location obj = instruction->GetLocations()->InAt(0);
6697
6698 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6699}
6700
6701void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006702 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006703}
6704
6705void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6706 HandleBinaryOp(instruction);
6707}
6708
6709void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6710 HandleBinaryOp(instruction);
6711}
6712
6713void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6714 LOG(FATAL) << "Unreachable";
6715}
6716
6717void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006718 if (instruction->GetNext()->IsSuspendCheck() &&
6719 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6720 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6721 // The back edge will generate the suspend check.
6722 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6723 }
6724
Alexey Frunze4dda3372015-06-01 18:31:49 -07006725 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6726}
6727
6728void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006729 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006730 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6731 if (location.IsStackSlot()) {
6732 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6733 } else if (location.IsDoubleStackSlot()) {
6734 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6735 }
6736 locations->SetOut(location);
6737}
6738
6739void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6740 ATTRIBUTE_UNUSED) {
6741 // Nothing to do, the parameter is already at its location.
6742}
6743
6744void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6745 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006746 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006747 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6748}
6749
6750void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6751 ATTRIBUTE_UNUSED) {
6752 // Nothing to do, the method is already at its location.
6753}
6754
6755void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006756 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006757 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006758 locations->SetInAt(i, Location::Any());
6759 }
6760 locations->SetOut(Location::Any());
6761}
6762
6763void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6764 LOG(FATAL) << "Unreachable";
6765}
6766
6767void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006768 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006769 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006770 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6771 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006772 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006773
6774 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006775 case DataType::Type::kInt32:
6776 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006777 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006778 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006779 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6780 break;
6781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006782 case DataType::Type::kFloat32:
6783 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006784 InvokeRuntimeCallingConvention calling_convention;
6785 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6786 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6787 locations->SetOut(calling_convention.GetReturnLocation(type));
6788 break;
6789 }
6790
6791 default:
6792 LOG(FATAL) << "Unexpected rem type " << type;
6793 }
6794}
6795
6796void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006797 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006798
6799 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006800 case DataType::Type::kInt32:
6801 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006802 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006803 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006805 case DataType::Type::kFloat32:
6806 case DataType::Type::kFloat64: {
6807 QuickEntrypointEnum entrypoint =
6808 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006809 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006810 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006811 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6812 } else {
6813 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6814 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006815 break;
6816 }
6817 default:
6818 LOG(FATAL) << "Unexpected rem type " << type;
6819 }
6820}
6821
Aart Bik1f8d51b2018-02-15 10:42:37 -08006822static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
6823 LocationSummary* locations = new (allocator) LocationSummary(minmax);
6824 switch (minmax->GetResultType()) {
6825 case DataType::Type::kInt32:
6826 case DataType::Type::kInt64:
6827 locations->SetInAt(0, Location::RequiresRegister());
6828 locations->SetInAt(1, Location::RequiresRegister());
6829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6830 break;
6831 case DataType::Type::kFloat32:
6832 case DataType::Type::kFloat64:
6833 locations->SetInAt(0, Location::RequiresFpuRegister());
6834 locations->SetInAt(1, Location::RequiresFpuRegister());
6835 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6836 break;
6837 default:
6838 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
6839 }
6840}
6841
Aart Bik351df3e2018-03-07 11:54:57 -08006842void InstructionCodeGeneratorMIPS64::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08006843 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6844 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
6845 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6846
6847 if (lhs == rhs) {
6848 if (out != lhs) {
6849 __ Move(out, lhs);
6850 }
6851 } else {
6852 // Some architectures, such as ARM and MIPS (prior to r6), have a
6853 // conditional move instruction which only changes the target
6854 // (output) register if the condition is true (MIPS prior to r6 had
6855 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
6856 // change the target (output) register. If the condition is true the
6857 // output register gets the contents of the "rs" register; otherwise,
6858 // the output register is set to zero. One consequence of this is
6859 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
6860 // needs to use a pair of SELEQZ/SELNEZ instructions. After
6861 // executing this pair of instructions one of the output registers
6862 // from the pair will necessarily contain zero. Then the code ORs the
6863 // output registers from the SELEQZ/SELNEZ instructions to get the
6864 // final result.
6865 //
6866 // The initial test to see if the output register is same as the
6867 // first input register is needed to make sure that value in the
6868 // first input register isn't clobbered before we've finished
6869 // computing the output value. The logic in the corresponding else
6870 // clause performs the same task but makes sure the second input
6871 // register isn't clobbered in the event that it's the same register
6872 // as the output register; the else clause also handles the case
6873 // where the output register is distinct from both the first, and the
6874 // second input registers.
6875 if (out == lhs) {
6876 __ Slt(AT, rhs, lhs);
6877 if (is_min) {
6878 __ Seleqz(out, lhs, AT);
6879 __ Selnez(AT, rhs, AT);
6880 } else {
6881 __ Selnez(out, lhs, AT);
6882 __ Seleqz(AT, rhs, AT);
6883 }
6884 } else {
6885 __ Slt(AT, lhs, rhs);
6886 if (is_min) {
6887 __ Seleqz(out, rhs, AT);
6888 __ Selnez(AT, lhs, AT);
6889 } else {
6890 __ Selnez(out, rhs, AT);
6891 __ Seleqz(AT, lhs, AT);
6892 }
6893 }
6894 __ Or(out, out, AT);
6895 }
6896}
6897
6898void InstructionCodeGeneratorMIPS64::GenerateMinMaxFP(LocationSummary* locations,
6899 bool is_min,
6900 DataType::Type type) {
6901 FpuRegister a = locations->InAt(0).AsFpuRegister<FpuRegister>();
6902 FpuRegister b = locations->InAt(1).AsFpuRegister<FpuRegister>();
6903 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6904
6905 Mips64Label noNaNs;
6906 Mips64Label done;
6907 FpuRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
6908
6909 // When Java computes min/max it prefers a NaN to a number; the
6910 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
6911 // the inputs is a NaN and the other is a valid number, the MIPS
6912 // instruction will return the number; Java wants the NaN value
6913 // returned. This is why there is extra logic preceding the use of
6914 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
6915 // NaN, return the NaN, otherwise return the min/max.
6916 if (type == DataType::Type::kFloat64) {
6917 __ CmpUnD(FTMP, a, b);
6918 __ Bc1eqz(FTMP, &noNaNs);
6919
6920 // One of the inputs is a NaN
6921 __ CmpEqD(ftmp, a, a);
6922 // If a == a then b is the NaN, otherwise a is the NaN.
6923 __ SelD(ftmp, a, b);
6924
6925 if (ftmp != out) {
6926 __ MovD(out, ftmp);
6927 }
6928
6929 __ Bc(&done);
6930
6931 __ Bind(&noNaNs);
6932
6933 if (is_min) {
6934 __ MinD(out, a, b);
6935 } else {
6936 __ MaxD(out, a, b);
6937 }
6938 } else {
6939 DCHECK_EQ(type, DataType::Type::kFloat32);
6940 __ CmpUnS(FTMP, a, b);
6941 __ Bc1eqz(FTMP, &noNaNs);
6942
6943 // One of the inputs is a NaN
6944 __ CmpEqS(ftmp, a, a);
6945 // If a == a then b is the NaN, otherwise a is the NaN.
6946 __ SelS(ftmp, a, b);
6947
6948 if (ftmp != out) {
6949 __ MovS(out, ftmp);
6950 }
6951
6952 __ Bc(&done);
6953
6954 __ Bind(&noNaNs);
6955
6956 if (is_min) {
6957 __ MinS(out, a, b);
6958 } else {
6959 __ MaxS(out, a, b);
6960 }
6961 }
6962
6963 __ Bind(&done);
6964}
6965
Aart Bik351df3e2018-03-07 11:54:57 -08006966void InstructionCodeGeneratorMIPS64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
6967 DataType::Type type = minmax->GetResultType();
6968 switch (type) {
6969 case DataType::Type::kInt32:
6970 case DataType::Type::kInt64:
6971 GenerateMinMaxInt(minmax->GetLocations(), is_min);
6972 break;
6973 case DataType::Type::kFloat32:
6974 case DataType::Type::kFloat64:
6975 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
6976 break;
6977 default:
6978 LOG(FATAL) << "Unexpected type for HMinMax " << type;
6979 }
6980}
6981
Aart Bik1f8d51b2018-02-15 10:42:37 -08006982void LocationsBuilderMIPS64::VisitMin(HMin* min) {
6983 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
6984}
6985
6986void InstructionCodeGeneratorMIPS64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08006987 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006988}
6989
6990void LocationsBuilderMIPS64::VisitMax(HMax* max) {
6991 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
6992}
6993
6994void InstructionCodeGeneratorMIPS64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08006995 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006996}
6997
Aart Bik3dad3412018-02-28 12:01:46 -08006998void LocationsBuilderMIPS64::VisitAbs(HAbs* abs) {
6999 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
7000 switch (abs->GetResultType()) {
7001 case DataType::Type::kInt32:
7002 case DataType::Type::kInt64:
7003 locations->SetInAt(0, Location::RequiresRegister());
7004 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7005 break;
7006 case DataType::Type::kFloat32:
7007 case DataType::Type::kFloat64:
7008 locations->SetInAt(0, Location::RequiresFpuRegister());
7009 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7010 break;
7011 default:
7012 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
7013 }
7014}
7015
7016void InstructionCodeGeneratorMIPS64::VisitAbs(HAbs* abs) {
7017 LocationSummary* locations = abs->GetLocations();
7018 switch (abs->GetResultType()) {
7019 case DataType::Type::kInt32: {
7020 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
7021 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
7022 __ Sra(AT, in, 31);
7023 __ Xor(out, in, AT);
7024 __ Subu(out, out, AT);
7025 break;
7026 }
7027 case DataType::Type::kInt64: {
7028 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
7029 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
7030 __ Dsra32(AT, in, 31);
7031 __ Xor(out, in, AT);
7032 __ Dsubu(out, out, AT);
7033 break;
7034 }
7035 case DataType::Type::kFloat32: {
7036 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
7037 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
7038 __ AbsS(out, in);
7039 break;
7040 }
7041 case DataType::Type::kFloat64: {
7042 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
7043 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
7044 __ AbsD(out, in);
7045 break;
7046 }
7047 default:
7048 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
7049 }
7050}
7051
Igor Murashkind01745e2017-04-05 16:40:31 -07007052void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
7053 constructor_fence->SetLocations(nullptr);
7054}
7055
7056void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
7057 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
7058 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
7059}
7060
Alexey Frunze4dda3372015-06-01 18:31:49 -07007061void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7062 memory_barrier->SetLocations(nullptr);
7063}
7064
7065void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7066 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7067}
7068
7069void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007070 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007071 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007072 locations->SetInAt(0, Mips64ReturnLocation(return_type));
7073}
7074
7075void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7076 codegen_->GenerateFrameExit();
7077}
7078
7079void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
7080 ret->SetLocations(nullptr);
7081}
7082
7083void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7084 codegen_->GenerateFrameExit();
7085}
7086
Alexey Frunze92d90602015-12-18 18:16:36 -08007087void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
7088 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007089}
7090
Alexey Frunze92d90602015-12-18 18:16:36 -08007091void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
7092 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007093}
7094
Alexey Frunze4dda3372015-06-01 18:31:49 -07007095void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
7096 HandleShift(shl);
7097}
7098
7099void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
7100 HandleShift(shl);
7101}
7102
7103void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
7104 HandleShift(shr);
7105}
7106
7107void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
7108 HandleShift(shr);
7109}
7110
Alexey Frunze4dda3372015-06-01 18:31:49 -07007111void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
7112 HandleBinaryOp(instruction);
7113}
7114
7115void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
7116 HandleBinaryOp(instruction);
7117}
7118
7119void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7120 HandleFieldGet(instruction, instruction->GetFieldInfo());
7121}
7122
7123void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7124 HandleFieldGet(instruction, instruction->GetFieldInfo());
7125}
7126
7127void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7128 HandleFieldSet(instruction, instruction->GetFieldInfo());
7129}
7130
7131void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01007132 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007133}
7134
Calin Juravlee460d1d2015-09-29 04:52:17 +01007135void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
7136 HUnresolvedInstanceFieldGet* instruction) {
7137 FieldAccessCallingConventionMIPS64 calling_convention;
7138 codegen_->CreateUnresolvedFieldLocationSummary(
7139 instruction, instruction->GetFieldType(), calling_convention);
7140}
7141
7142void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
7143 HUnresolvedInstanceFieldGet* instruction) {
7144 FieldAccessCallingConventionMIPS64 calling_convention;
7145 codegen_->GenerateUnresolvedFieldAccess(instruction,
7146 instruction->GetFieldType(),
7147 instruction->GetFieldIndex(),
7148 instruction->GetDexPc(),
7149 calling_convention);
7150}
7151
7152void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
7153 HUnresolvedInstanceFieldSet* instruction) {
7154 FieldAccessCallingConventionMIPS64 calling_convention;
7155 codegen_->CreateUnresolvedFieldLocationSummary(
7156 instruction, instruction->GetFieldType(), calling_convention);
7157}
7158
7159void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
7160 HUnresolvedInstanceFieldSet* instruction) {
7161 FieldAccessCallingConventionMIPS64 calling_convention;
7162 codegen_->GenerateUnresolvedFieldAccess(instruction,
7163 instruction->GetFieldType(),
7164 instruction->GetFieldIndex(),
7165 instruction->GetDexPc(),
7166 calling_convention);
7167}
7168
7169void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
7170 HUnresolvedStaticFieldGet* instruction) {
7171 FieldAccessCallingConventionMIPS64 calling_convention;
7172 codegen_->CreateUnresolvedFieldLocationSummary(
7173 instruction, instruction->GetFieldType(), calling_convention);
7174}
7175
7176void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
7177 HUnresolvedStaticFieldGet* instruction) {
7178 FieldAccessCallingConventionMIPS64 calling_convention;
7179 codegen_->GenerateUnresolvedFieldAccess(instruction,
7180 instruction->GetFieldType(),
7181 instruction->GetFieldIndex(),
7182 instruction->GetDexPc(),
7183 calling_convention);
7184}
7185
7186void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
7187 HUnresolvedStaticFieldSet* instruction) {
7188 FieldAccessCallingConventionMIPS64 calling_convention;
7189 codegen_->CreateUnresolvedFieldLocationSummary(
7190 instruction, instruction->GetFieldType(), calling_convention);
7191}
7192
7193void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
7194 HUnresolvedStaticFieldSet* instruction) {
7195 FieldAccessCallingConventionMIPS64 calling_convention;
7196 codegen_->GenerateUnresolvedFieldAccess(instruction,
7197 instruction->GetFieldType(),
7198 instruction->GetFieldIndex(),
7199 instruction->GetDexPc(),
7200 calling_convention);
7201}
7202
Alexey Frunze4dda3372015-06-01 18:31:49 -07007203void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007204 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7205 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02007206 // In suspend check slow path, usually there are no caller-save registers at all.
7207 // If SIMD instructions are present, however, we force spilling all live SIMD
7208 // registers in full width (since the runtime only saves/restores lower part).
7209 locations->SetCustomSlowPathCallerSaves(
7210 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007211}
7212
7213void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
7214 HBasicBlock* block = instruction->GetBlock();
7215 if (block->GetLoopInformation() != nullptr) {
7216 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7217 // The back edge will generate the suspend check.
7218 return;
7219 }
7220 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7221 // The goto will generate the suspend check.
7222 return;
7223 }
7224 GenerateSuspendCheck(instruction, nullptr);
7225}
7226
Alexey Frunze4dda3372015-06-01 18:31:49 -07007227void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007228 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7229 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007230 InvokeRuntimeCallingConvention calling_convention;
7231 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7232}
7233
7234void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01007235 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007236 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7237}
7238
7239void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007240 DataType::Type input_type = conversion->GetInputType();
7241 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007242 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7243 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007244
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007245 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
7246 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007247 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
7248 }
7249
Vladimir Markoca6fff82017-10-03 14:49:14 +01007250 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007251
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007252 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007253 locations->SetInAt(0, Location::RequiresFpuRegister());
7254 } else {
7255 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007256 }
7257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007258 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007259 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007260 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007262 }
7263}
7264
7265void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
7266 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007267 DataType::Type result_type = conversion->GetResultType();
7268 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007269
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007270 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7271 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007273 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007274 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7275 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
7276
7277 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007278 case DataType::Type::kUint8:
7279 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007280 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007281 case DataType::Type::kInt8:
7282 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007283 // Type conversion from long to types narrower than int is a result of code
7284 // transformations. To avoid unpredictable results for SEB and SEH, we first
7285 // need to sign-extend the low 32-bit value into bits 32 through 63.
7286 __ Sll(dst, src, 0);
7287 __ Seb(dst, dst);
7288 } else {
7289 __ Seb(dst, src);
7290 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007291 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007292 case DataType::Type::kUint16:
7293 __ Andi(dst, src, 0xFFFF);
7294 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007295 case DataType::Type::kInt16:
7296 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007297 // Type conversion from long to types narrower than int is a result of code
7298 // transformations. To avoid unpredictable results for SEB and SEH, we first
7299 // need to sign-extend the low 32-bit value into bits 32 through 63.
7300 __ Sll(dst, src, 0);
7301 __ Seh(dst, dst);
7302 } else {
7303 __ Seh(dst, src);
7304 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007305 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007306 case DataType::Type::kInt32:
7307 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007308 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
7309 // conversions, except when the input and output registers are the same and we are not
7310 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007311 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007312 __ Sll(dst, src, 0);
7313 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007314 break;
7315
7316 default:
7317 LOG(FATAL) << "Unexpected type conversion from " << input_type
7318 << " to " << result_type;
7319 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007320 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007321 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7322 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007323 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007324 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007325 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007326 __ Cvtsl(dst, FTMP);
7327 } else {
7328 __ Cvtdl(dst, FTMP);
7329 }
7330 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007331 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007332 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007333 __ Cvtsw(dst, FTMP);
7334 } else {
7335 __ Cvtdw(dst, FTMP);
7336 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007337 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007338 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
7339 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007340 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7341 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007342
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007343 if (result_type == DataType::Type::kInt64) {
7344 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007345 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007346 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007347 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007348 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007349 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007350 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007351 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007352 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007353 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007354 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007355 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007356 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007357 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007358 } else if (DataType::IsFloatingPointType(result_type) &&
7359 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007360 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7361 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007362 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007363 __ Cvtsd(dst, src);
7364 } else {
7365 __ Cvtds(dst, src);
7366 }
7367 } else {
7368 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7369 << " to " << result_type;
7370 }
7371}
7372
7373void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
7374 HandleShift(ushr);
7375}
7376
7377void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
7378 HandleShift(ushr);
7379}
7380
7381void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7382 HandleBinaryOp(instruction);
7383}
7384
7385void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7386 HandleBinaryOp(instruction);
7387}
7388
7389void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7390 // Nothing to do, this should be removed during prepare for register allocator.
7391 LOG(FATAL) << "Unreachable";
7392}
7393
7394void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7395 // Nothing to do, this should be removed during prepare for register allocator.
7396 LOG(FATAL) << "Unreachable";
7397}
7398
7399void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007400 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007401}
7402
7403void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007404 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007405}
7406
7407void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007408 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007409}
7410
7411void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007412 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007413}
7414
7415void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007416 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007417}
7418
7419void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007420 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007421}
7422
7423void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007424 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007425}
7426
7427void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007428 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007429}
7430
7431void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007432 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007433}
7434
7435void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007436 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007437}
7438
7439void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007440 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007441}
7442
7443void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007444 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007445}
7446
Aart Bike9f37602015-10-09 11:15:55 -07007447void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007448 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007449}
7450
7451void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007452 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007453}
7454
7455void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007456 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007457}
7458
7459void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007460 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007461}
7462
7463void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007464 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007465}
7466
7467void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007468 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007469}
7470
7471void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007472 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007473}
7474
7475void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007476 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007477}
7478
Mark Mendellfe57faa2015-09-18 09:26:15 -04007479// Simple implementation of packed switch - generate cascaded compare/jumps.
7480void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7481 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007482 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007483 locations->SetInAt(0, Location::RequiresRegister());
7484}
7485
Alexey Frunze0960ac52016-12-20 17:24:59 -08007486void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7487 int32_t lower_bound,
7488 uint32_t num_entries,
7489 HBasicBlock* switch_block,
7490 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007491 // Create a set of compare/jumps.
7492 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007493 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007494 // Jump to default if index is negative
7495 // Note: We don't check the case that index is positive while value < lower_bound, because in
7496 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7497 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7498
Alexey Frunze0960ac52016-12-20 17:24:59 -08007499 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007500 // Jump to successors[0] if value == lower_bound.
7501 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7502 int32_t last_index = 0;
7503 for (; num_entries - last_index > 2; last_index += 2) {
7504 __ Addiu(temp_reg, temp_reg, -2);
7505 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7506 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7507 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7508 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7509 }
7510 if (num_entries - last_index == 2) {
7511 // The last missing case_value.
7512 __ Addiu(temp_reg, temp_reg, -1);
7513 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007514 }
7515
7516 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007517 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007518 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007519 }
7520}
7521
Alexey Frunze0960ac52016-12-20 17:24:59 -08007522void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7523 int32_t lower_bound,
7524 uint32_t num_entries,
7525 HBasicBlock* switch_block,
7526 HBasicBlock* default_block) {
7527 // Create a jump table.
7528 std::vector<Mips64Label*> labels(num_entries);
7529 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7530 for (uint32_t i = 0; i < num_entries; i++) {
7531 labels[i] = codegen_->GetLabelOf(successors[i]);
7532 }
7533 JumpTable* table = __ CreateJumpTable(std::move(labels));
7534
7535 // Is the value in range?
7536 __ Addiu32(TMP, value_reg, -lower_bound);
7537 __ LoadConst32(AT, num_entries);
7538 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7539
7540 // We are in the range of the table.
7541 // Load the target address from the jump table, indexing by the value.
7542 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007543 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007544 __ Lw(TMP, TMP, 0);
7545 // Compute the absolute target address by adding the table start address
7546 // (the table contains offsets to targets relative to its start).
7547 __ Daddu(TMP, TMP, AT);
7548 // And jump.
7549 __ Jr(TMP);
7550 __ Nop();
7551}
7552
7553void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7554 int32_t lower_bound = switch_instr->GetStartValue();
7555 uint32_t num_entries = switch_instr->GetNumEntries();
7556 LocationSummary* locations = switch_instr->GetLocations();
7557 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7558 HBasicBlock* switch_block = switch_instr->GetBlock();
7559 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7560
7561 if (num_entries > kPackedSwitchJumpTableThreshold) {
7562 GenTableBasedPackedSwitch(value_reg,
7563 lower_bound,
7564 num_entries,
7565 switch_block,
7566 default_block);
7567 } else {
7568 GenPackedSwitchWithCompares(value_reg,
7569 lower_bound,
7570 num_entries,
7571 switch_block,
7572 default_block);
7573 }
7574}
7575
Chris Larsenc9905a62017-03-13 17:06:18 -07007576void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7577 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007578 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007579 locations->SetInAt(0, Location::RequiresRegister());
7580 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007581}
7582
Chris Larsenc9905a62017-03-13 17:06:18 -07007583void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7584 LocationSummary* locations = instruction->GetLocations();
7585 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7586 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7587 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7588 __ LoadFromOffset(kLoadDoubleword,
7589 locations->Out().AsRegister<GpuRegister>(),
7590 locations->InAt(0).AsRegister<GpuRegister>(),
7591 method_offset);
7592 } else {
7593 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7594 instruction->GetIndex(), kMips64PointerSize));
7595 __ LoadFromOffset(kLoadDoubleword,
7596 locations->Out().AsRegister<GpuRegister>(),
7597 locations->InAt(0).AsRegister<GpuRegister>(),
7598 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7599 __ LoadFromOffset(kLoadDoubleword,
7600 locations->Out().AsRegister<GpuRegister>(),
7601 locations->Out().AsRegister<GpuRegister>(),
7602 method_offset);
7603 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007604}
7605
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007606void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7607 ATTRIBUTE_UNUSED) {
7608 LOG(FATAL) << "Unreachable";
7609}
7610
7611void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7612 ATTRIBUTE_UNUSED) {
7613 LOG(FATAL) << "Unreachable";
7614}
7615
Alexey Frunze4dda3372015-06-01 18:31:49 -07007616} // namespace mips64
7617} // namespace art