blob: 9682377f5e01bb10abb809bbb9484f7f4c3d36a6 [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:
178 LoadClassSlowPathMIPS64(HLoadClass* cls,
179 HInstruction* at,
180 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000181 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700182 : SlowPathCodeMIPS64(at),
183 cls_(cls),
184 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000185 do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700186 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
187 }
188
189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000190 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700191 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700193 InvokeRuntimeCallingConvention calling_convention;
194 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700195 __ Bind(GetEntryLabel());
196 SaveLiveRegisters(codegen, locations);
197
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000198 dex::TypeIndex type_index = cls_->GetTypeIndex();
199 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100200 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
201 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000202 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700203 if (do_clinit_) {
204 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
205 } else {
206 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
207 }
208
209 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700210 if (out.IsValid()) {
211 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100212 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700213 mips64_codegen->MoveLocation(out,
214 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
215 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700216 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700218
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700219 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 }
221
Roland Levillain46648892015-06-19 16:07:18 +0100222 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
223
Alexey Frunze4dda3372015-06-01 18:31:49 -0700224 private:
225 // The class this slow path will load.
226 HLoadClass* const cls_;
227
Alexey Frunze4dda3372015-06-01 18:31:49 -0700228 // The dex PC of `at_`.
229 const uint32_t dex_pc_;
230
231 // Whether to initialize the class.
232 const bool do_clinit_;
233
234 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)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800971 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800973 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100974 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700975 // Save RA (containing the return address) to mimic Quick.
976 AddAllocatedRegister(Location::RegisterLocation(RA));
977}
978
979#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100980// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
981#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700982#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700983
984void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700985 // Ensure that we fix up branches.
986 __ FinalizeCode();
987
988 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100989 StackMapStream* stack_map_stream = GetStackMapStream();
990 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100991 uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700992 uint32_t new_position = __ GetAdjustedPosition(old_position);
993 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100994 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700995 }
996
997 // Adjust pc offsets for the disassembly information.
998 if (disasm_info_ != nullptr) {
999 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1000 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1001 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1002 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1003 it.second.start = __ GetAdjustedPosition(it.second.start);
1004 it.second.end = __ GetAdjustedPosition(it.second.end);
1005 }
1006 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1007 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1008 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1009 }
1010 }
1011
Alexey Frunze4dda3372015-06-01 18:31:49 -07001012 CodeGenerator::Finalize(allocator);
1013}
1014
1015Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1016 return codegen_->GetAssembler();
1017}
1018
1019void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001020 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001021 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1022}
1023
1024void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001025 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001026 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1027}
1028
1029void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1030 // Pop reg
1031 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001032 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001033}
1034
1035void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1036 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001037 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001038 __ Sd(GpuRegister(reg), SP, 0);
1039}
1040
1041void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1042 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1043 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1044 // Allocate a scratch register other than TMP, if available.
1045 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1046 // automatically unspilled when the scratch scope object is destroyed).
1047 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1048 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001049 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001050 __ LoadFromOffset(load_type,
1051 GpuRegister(ensure_scratch.GetRegister()),
1052 SP,
1053 index1 + stack_offset);
1054 __ LoadFromOffset(load_type,
1055 TMP,
1056 SP,
1057 index2 + stack_offset);
1058 __ StoreToOffset(store_type,
1059 GpuRegister(ensure_scratch.GetRegister()),
1060 SP,
1061 index2 + stack_offset);
1062 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1063}
1064
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001065void ParallelMoveResolverMIPS64::ExchangeQuadSlots(int index1, int index2) {
1066 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, index1);
1067 __ LoadFpuFromOffset(kLoadQuadword, FTMP2, SP, index2);
1068 __ StoreFpuToOffset(kStoreQuadword, FTMP, SP, index2);
1069 __ StoreFpuToOffset(kStoreQuadword, FTMP2, SP, index1);
1070}
1071
Alexey Frunze4dda3372015-06-01 18:31:49 -07001072static dwarf::Reg DWARFReg(GpuRegister reg) {
1073 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1074}
1075
David Srbeckyba702002016-02-01 18:15:29 +00001076static dwarf::Reg DWARFReg(FpuRegister reg) {
1077 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1078}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001079
1080void CodeGeneratorMIPS64::GenerateFrameEntry() {
1081 __ Bind(&frame_entry_label_);
1082
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001083 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001084 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1085 __ Addiu(TMP, TMP, 1);
1086 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001087 }
1088
Vladimir Marko33bff252017-11-01 14:35:42 +00001089 bool do_overflow_check =
1090 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091
1092 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001093 __ LoadFromOffset(
1094 kLoadWord,
1095 ZERO,
1096 SP,
1097 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 RecordPcInfo(nullptr, 0);
1099 }
1100
Alexey Frunze4dda3372015-06-01 18:31:49 -07001101 if (HasEmptyFrame()) {
1102 return;
1103 }
1104
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001105 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001106 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1107 LOG(FATAL) << "Stack frame larger than "
1108 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001109 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001110
1111 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001112
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001113 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001114 __ IncreaseFrameSize(ofs);
1115
1116 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1117 GpuRegister reg = kCoreCalleeSaves[i];
1118 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001119 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001120 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001121 __ cfi().RelOffset(DWARFReg(reg), ofs);
1122 }
1123 }
1124
1125 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1126 FpuRegister reg = kFpuCalleeSaves[i];
1127 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001128 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001129 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001130 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001131 }
1132 }
1133
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001134 // Save the current method if we need it. Note that we do not
1135 // do this in HCurrentMethod, as the instruction might have been removed
1136 // in the SSA graph.
1137 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001138 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001139 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001140
1141 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1142 // Initialize should_deoptimize flag to 0.
1143 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1144 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001145}
1146
1147void CodeGeneratorMIPS64::GenerateFrameExit() {
1148 __ cfi().RememberState();
1149
Alexey Frunze4dda3372015-06-01 18:31:49 -07001150 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001152
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001153 // For better instruction scheduling restore RA before other registers.
1154 uint32_t ofs = GetFrameSize();
1155 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001156 GpuRegister reg = kCoreCalleeSaves[i];
1157 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001158 ofs -= kMips64DoublewordSize;
1159 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001160 __ cfi().Restore(DWARFReg(reg));
1161 }
1162 }
1163
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001164 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1165 FpuRegister reg = kFpuCalleeSaves[i];
1166 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1167 ofs -= kMips64DoublewordSize;
1168 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1169 __ cfi().Restore(DWARFReg(reg));
1170 }
1171 }
1172
1173 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001174 }
1175
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001176 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001177
1178 __ cfi().RestoreState();
1179 __ cfi().DefCFAOffset(GetFrameSize());
1180}
1181
1182void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1183 __ Bind(GetLabelOf(block));
1184}
1185
1186void CodeGeneratorMIPS64::MoveLocation(Location destination,
1187 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001188 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001189 if (source.Equals(destination)) {
1190 return;
1191 }
1192
1193 // A valid move can always be inferred from the destination and source
1194 // locations. When moving from and to a register, the argument type can be
1195 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001196 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001197 DCHECK_EQ(unspecified_type, false);
1198
1199 if (destination.IsRegister() || destination.IsFpuRegister()) {
1200 if (unspecified_type) {
1201 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1202 if (source.IsStackSlot() ||
1203 (src_cst != nullptr && (src_cst->IsIntConstant()
1204 || src_cst->IsFloatConstant()
1205 || src_cst->IsNullConstant()))) {
1206 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001207 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001208 } else {
1209 // If the source is a double stack slot or a 64bit constant, a 64bit
1210 // type is appropriate. Else the source is a register, and since the
1211 // type has not been specified, we chose a 64bit type to force a 64bit
1212 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001214 }
1215 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1217 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001218 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1219 // Move to GPR/FPR from stack
1220 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001221 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001222 __ LoadFpuFromOffset(load_type,
1223 destination.AsFpuRegister<FpuRegister>(),
1224 SP,
1225 source.GetStackIndex());
1226 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 __ LoadFromOffset(load_type,
1229 destination.AsRegister<GpuRegister>(),
1230 SP,
1231 source.GetStackIndex());
1232 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001233 } else if (source.IsSIMDStackSlot()) {
1234 __ LoadFpuFromOffset(kLoadQuadword,
1235 destination.AsFpuRegister<FpuRegister>(),
1236 SP,
1237 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001238 } else if (source.IsConstant()) {
1239 // Move to GPR/FPR from constant
1240 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001241 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001242 gpr = destination.AsRegister<GpuRegister>();
1243 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001244 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001245 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001246 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001247 gpr = ZERO;
1248 } else {
1249 __ LoadConst32(gpr, value);
1250 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001252 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001253 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001254 gpr = ZERO;
1255 } else {
1256 __ LoadConst64(gpr, value);
1257 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001258 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001259 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001260 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001261 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001262 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1263 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001264 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001265 if (destination.IsRegister()) {
1266 // Move to GPR from GPR
1267 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1268 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001269 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1272 } else {
1273 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1274 }
1275 }
1276 } else if (source.IsFpuRegister()) {
1277 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001278 if (GetGraph()->HasSIMD()) {
1279 __ MoveV(VectorRegisterFrom(destination),
1280 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001281 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001282 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001284 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1285 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001286 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001287 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1288 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001289 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290 } else {
1291 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001292 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1294 } else {
1295 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1296 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 }
1298 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001299 } else if (destination.IsSIMDStackSlot()) {
1300 if (source.IsFpuRegister()) {
1301 __ StoreFpuToOffset(kStoreQuadword,
1302 source.AsFpuRegister<FpuRegister>(),
1303 SP,
1304 destination.GetStackIndex());
1305 } else {
1306 DCHECK(source.IsSIMDStackSlot());
1307 __ LoadFpuFromOffset(kLoadQuadword,
1308 FTMP,
1309 SP,
1310 source.GetStackIndex());
1311 __ StoreFpuToOffset(kStoreQuadword,
1312 FTMP,
1313 SP,
1314 destination.GetStackIndex());
1315 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001316 } else { // The destination is not a register. It must be a stack slot.
1317 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1318 if (source.IsRegister() || source.IsFpuRegister()) {
1319 if (unspecified_type) {
1320 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001321 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001322 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 dst_type =
1324 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001325 }
1326 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001327 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1328 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001329 // Move to stack from GPR/FPR
1330 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1331 if (source.IsRegister()) {
1332 __ StoreToOffset(store_type,
1333 source.AsRegister<GpuRegister>(),
1334 SP,
1335 destination.GetStackIndex());
1336 } else {
1337 __ StoreFpuToOffset(store_type,
1338 source.AsFpuRegister<FpuRegister>(),
1339 SP,
1340 destination.GetStackIndex());
1341 }
1342 } else if (source.IsConstant()) {
1343 // Move to stack from constant
1344 HConstant* src_cst = source.GetConstant();
1345 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001346 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001347 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001348 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1349 if (value != 0) {
1350 gpr = TMP;
1351 __ LoadConst32(gpr, value);
1352 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001353 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001354 DCHECK(destination.IsDoubleStackSlot());
1355 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1356 if (value != 0) {
1357 gpr = TMP;
1358 __ LoadConst64(gpr, value);
1359 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001360 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001361 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001362 } else {
1363 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1364 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1365 // Move to stack from stack
1366 if (destination.IsStackSlot()) {
1367 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1368 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1369 } else {
1370 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1371 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1372 }
1373 }
1374 }
1375}
1376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001377void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001378 DCHECK(!loc1.IsConstant());
1379 DCHECK(!loc2.IsConstant());
1380
1381 if (loc1.Equals(loc2)) {
1382 return;
1383 }
1384
1385 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1386 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001387 bool is_simd1 = loc1.IsSIMDStackSlot();
1388 bool is_simd2 = loc2.IsSIMDStackSlot();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001389 bool is_fp_reg1 = loc1.IsFpuRegister();
1390 bool is_fp_reg2 = loc2.IsFpuRegister();
1391
1392 if (loc2.IsRegister() && loc1.IsRegister()) {
1393 // Swap 2 GPRs
1394 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1395 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1396 __ Move(TMP, r2);
1397 __ Move(r2, r1);
1398 __ Move(r1, TMP);
1399 } else if (is_fp_reg2 && is_fp_reg1) {
1400 // Swap 2 FPRs
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001401 if (GetGraph()->HasSIMD()) {
1402 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1403 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1404 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001405 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001406 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1407 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
1408 if (type == DataType::Type::kFloat32) {
1409 __ MovS(FTMP, r1);
1410 __ MovS(r1, r2);
1411 __ MovS(r2, FTMP);
1412 } else {
1413 DCHECK_EQ(type, DataType::Type::kFloat64);
1414 __ MovD(FTMP, r1);
1415 __ MovD(r1, r2);
1416 __ MovD(r2, FTMP);
1417 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001418 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 } else if (is_slot1 != is_slot2) {
1420 // Swap GPR/FPR and stack slot
1421 Location reg_loc = is_slot1 ? loc2 : loc1;
1422 Location mem_loc = is_slot1 ? loc1 : loc2;
1423 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1424 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001425 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001426 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1427 if (reg_loc.IsFpuRegister()) {
1428 __ StoreFpuToOffset(store_type,
1429 reg_loc.AsFpuRegister<FpuRegister>(),
1430 SP,
1431 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001432 if (mem_loc.IsStackSlot()) {
1433 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1434 } else {
1435 DCHECK(mem_loc.IsDoubleStackSlot());
1436 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1437 }
1438 } else {
1439 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1440 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1441 }
1442 } else if (is_slot1 && is_slot2) {
1443 move_resolver_.Exchange(loc1.GetStackIndex(),
1444 loc2.GetStackIndex(),
1445 loc1.IsDoubleStackSlot());
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001446 } else if (is_simd1 && is_simd2) {
1447 move_resolver_.ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
1448 } else if ((is_fp_reg1 && is_simd2) || (is_fp_reg2 && is_simd1)) {
1449 Location fp_reg_loc = is_fp_reg1 ? loc1 : loc2;
1450 Location mem_loc = is_fp_reg1 ? loc2 : loc1;
1451 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, mem_loc.GetStackIndex());
1452 __ StoreFpuToOffset(kStoreQuadword,
1453 fp_reg_loc.AsFpuRegister<FpuRegister>(),
1454 SP,
1455 mem_loc.GetStackIndex());
1456 __ MoveV(VectorRegisterFrom(fp_reg_loc), static_cast<VectorRegister>(FTMP));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001457 } else {
1458 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1459 }
1460}
1461
Calin Juravle175dc732015-08-25 15:42:32 +01001462void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1463 DCHECK(location.IsRegister());
1464 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1465}
1466
Calin Juravlee460d1d2015-09-29 04:52:17 +01001467void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1468 if (location.IsRegister()) {
1469 locations->AddTemp(location);
1470 } else {
1471 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1472 }
1473}
1474
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001475void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1476 GpuRegister value,
1477 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001478 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001479 GpuRegister card = AT;
1480 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001481 if (value_can_be_null) {
1482 __ Beqzc(value, &done);
1483 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001484 __ LoadFromOffset(kLoadDoubleword,
1485 card,
1486 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001487 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001488 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1489 __ Daddu(temp, card, temp);
1490 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001491 if (value_can_be_null) {
1492 __ Bind(&done);
1493 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001494}
1495
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001496template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001497inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1498 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001499 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001500 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001501 const DexFile* dex_file = info.target_dex_file;
Alexey Frunze19f6c692016-11-30 19:19:55 -08001502 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001503 DCHECK(info.label.IsBound());
1504 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1505 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1506 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001507 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001508 }
1509}
1510
Vladimir Markob066d432018-01-03 13:14:37 +00001511linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
1512 const DexFile* target_dex_file,
1513 uint32_t pc_insn_offset,
1514 uint32_t boot_image_offset) {
1515 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
1516 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
1517}
1518
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001519void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001520 DCHECK(linker_patches->empty());
1521 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001522 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001523 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001524 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001525 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001526 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001527 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001528 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001529 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001530 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001531 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001532 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001533 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001534 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001535 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001536 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00001537 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
1538 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001539 DCHECK(boot_image_type_patches_.empty());
1540 DCHECK(boot_image_string_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001541 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001542 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1543 method_bss_entry_patches_, linker_patches);
1544 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1545 type_bss_entry_patches_, linker_patches);
1546 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1547 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001548 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001549}
1550
Vladimir Markob066d432018-01-03 13:14:37 +00001551CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageRelRoPatch(
1552 uint32_t boot_image_offset,
1553 const PcRelativePatchInfo* info_high) {
1554 return NewPcRelativePatch(
1555 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1556}
1557
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001558CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001559 MethodReference target_method,
1560 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001561 return NewPcRelativePatch(
1562 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001563}
1564
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001565CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001566 MethodReference target_method,
1567 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001568 return NewPcRelativePatch(
1569 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001570}
1571
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001572CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001573 const DexFile& dex_file,
1574 dex::TypeIndex type_index,
1575 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001576 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001577}
1578
Vladimir Marko1998cd02017-01-13 13:02:58 +00001579CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001580 const DexFile& dex_file,
1581 dex::TypeIndex type_index,
1582 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001583 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001584}
1585
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001586CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001587 const DexFile& dex_file,
1588 dex::StringIndex string_index,
1589 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001590 return NewPcRelativePatch(
1591 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001592}
1593
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001594CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1595 const DexFile& dex_file,
1596 dex::StringIndex string_index,
1597 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001598 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001599}
1600
Alexey Frunze19f6c692016-11-30 19:19:55 -08001601CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001602 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001603 uint32_t offset_or_index,
1604 const PcRelativePatchInfo* info_high,
1605 ArenaDeque<PcRelativePatchInfo>* patches) {
1606 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001607 return &patches->back();
1608}
1609
Alexey Frunzef63f5692016-12-13 17:43:11 -08001610Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1611 return map->GetOrCreate(
1612 value,
1613 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1614}
1615
Alexey Frunze19f6c692016-11-30 19:19:55 -08001616Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1617 return uint64_literals_.GetOrCreate(
1618 value,
1619 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1620}
1621
Alexey Frunzef63f5692016-12-13 17:43:11 -08001622Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001623 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001624}
1625
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001626void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1627 GpuRegister out,
1628 PcRelativePatchInfo* info_low) {
1629 DCHECK(!info_high->patch_info_high);
1630 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001631 // Add the high half of a 32-bit offset to PC.
1632 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001633 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001634 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001635 if (info_low != nullptr) {
1636 DCHECK_EQ(info_low->patch_info_high, info_high);
1637 __ Bind(&info_low->label);
1638 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001639}
1640
Vladimir Markoeebb8212018-06-05 14:57:24 +01001641void CodeGeneratorMIPS64::LoadBootImageAddress(GpuRegister reg, uint32_t boot_image_offset) {
1642 DCHECK(!GetCompilerOptions().IsBootImage());
1643 if (GetCompilerOptions().GetCompilePic()) {
1644 DCHECK(Runtime::Current()->IsAotCompiler());
1645 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
1646 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
1647 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
1648 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1649 __ Lwu(reg, AT, /* placeholder */ 0x5678);
1650 } else {
1651 gc::Heap* heap = Runtime::Current()->GetHeap();
1652 DCHECK(!heap->GetBootImageSpaces().empty());
1653 uintptr_t address =
1654 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_offset);
1655 __ LoadLiteral(reg, kLoadDoubleword, DeduplicateBootImageAddressLiteral(address));
1656 }
1657}
1658
Alexey Frunze627c1a02017-01-30 19:28:14 -08001659Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1660 dex::StringIndex string_index,
1661 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001662 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001663 return jit_string_patches_.GetOrCreate(
1664 StringReference(&dex_file, string_index),
1665 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1666}
1667
1668Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1669 dex::TypeIndex type_index,
1670 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001671 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001672 return jit_class_patches_.GetOrCreate(
1673 TypeReference(&dex_file, type_index),
1674 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1675}
1676
1677void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1678 const uint8_t* roots_data,
1679 const Literal* literal,
1680 uint64_t index_in_table) const {
1681 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1682 uintptr_t address =
1683 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1684 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1685}
1686
1687void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1688 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001689 const StringReference& string_reference = entry.first;
1690 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001691 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001692 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001693 }
1694 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001695 const TypeReference& type_reference = entry.first;
1696 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001697 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001698 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001699 }
1700}
1701
David Brazdil58282f42016-01-14 12:45:10 +00001702void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001703 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1704 blocked_core_registers_[ZERO] = true;
1705 blocked_core_registers_[K0] = true;
1706 blocked_core_registers_[K1] = true;
1707 blocked_core_registers_[GP] = true;
1708 blocked_core_registers_[SP] = true;
1709 blocked_core_registers_[RA] = true;
1710
Lazar Trsicd9672662015-09-03 17:33:01 +02001711 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1712 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001713 blocked_core_registers_[AT] = true;
1714 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001715 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001716 blocked_fpu_registers_[FTMP] = true;
1717
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001718 if (GetInstructionSetFeatures().HasMsa()) {
1719 // To be used just for MSA instructions.
1720 blocked_fpu_registers_[FTMP2] = true;
1721 }
1722
Alexey Frunze4dda3372015-06-01 18:31:49 -07001723 // Reserve suspend and thread registers.
1724 blocked_core_registers_[S0] = true;
1725 blocked_core_registers_[TR] = true;
1726
1727 // Reserve T9 for function calls
1728 blocked_core_registers_[T9] = true;
1729
Goran Jakovljevic782be112016-06-21 12:39:04 +02001730 if (GetGraph()->IsDebuggable()) {
1731 // Stubs do not save callee-save floating point registers. If the graph
1732 // is debuggable, we need to deal with these registers differently. For
1733 // now, just block them.
1734 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1735 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1736 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001737 }
1738}
1739
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1741 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001742 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001743}
1744
1745size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1746 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001747 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001748}
1749
1750size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001751 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1752 FpuRegister(reg_id),
1753 SP,
1754 stack_index);
1755 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001756}
1757
1758size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001759 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1760 FpuRegister(reg_id),
1761 SP,
1762 stack_index);
1763 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001764}
1765
1766void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001767 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001768}
1769
1770void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001771 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772}
1773
Vladimir Markoa0431112018-06-25 09:32:54 +01001774const Mips64InstructionSetFeatures& CodeGeneratorMIPS64::GetInstructionSetFeatures() const {
1775 return *GetCompilerOptions().GetInstructionSetFeatures()->AsMips64InstructionSetFeatures();
1776}
1777
Calin Juravle175dc732015-08-25 15:42:32 +01001778void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 HInstruction* instruction,
1780 uint32_t dex_pc,
1781 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001782 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001783 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001784 if (EntrypointRequiresStackMap(entrypoint)) {
1785 RecordPcInfo(instruction, dex_pc, slow_path);
1786 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001787}
1788
Alexey Frunze15958152017-02-09 19:08:30 -08001789void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1790 HInstruction* instruction,
1791 SlowPathCode* slow_path) {
1792 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1793 GenerateInvokeRuntime(entry_point_offset);
1794}
1795
1796void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1797 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1798 __ Jalr(T9);
1799 __ Nop();
1800}
1801
Alexey Frunze4dda3372015-06-01 18:31:49 -07001802void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1803 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001804 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1805 const size_t status_byte_offset =
1806 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1807 constexpr uint32_t shifted_initialized_value =
1808 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1809
1810 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001811 __ Sltiu(TMP, TMP, shifted_initialized_value);
1812 __ Bnezc(TMP, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001813 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1814 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001815 __ Bind(slow_path->GetExitLabel());
1816}
1817
Vladimir Marko175e7862018-03-27 09:03:13 +00001818void InstructionCodeGeneratorMIPS64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1819 GpuRegister temp) {
1820 uint32_t path_to_root = check->GetBitstringPathToRoot();
1821 uint32_t mask = check->GetBitstringMask();
1822 DCHECK(IsPowerOfTwo(mask + 1));
1823 size_t mask_bits = WhichPowerOf2(mask + 1);
1824
1825 if (mask_bits == 16u) {
1826 // Load only the bitstring part of the status word.
1827 __ LoadFromOffset(
1828 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1829 // Compare the bitstring bits using XOR.
1830 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1831 } else {
1832 // /* uint32_t */ temp = temp->status_
1833 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1834 // Compare the bitstring bits using XOR.
1835 if (IsUint<16>(path_to_root)) {
1836 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1837 } else {
1838 __ LoadConst32(TMP, path_to_root);
1839 __ Xor(temp, temp, TMP);
1840 }
1841 // Shift out bits that do not contribute to the comparison.
1842 __ Sll(temp, temp, 32 - mask_bits);
1843 }
1844}
1845
Alexey Frunze4dda3372015-06-01 18:31:49 -07001846void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1847 __ Sync(0); // only stype 0 is supported
1848}
1849
1850void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1851 HBasicBlock* successor) {
1852 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001853 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1854
1855 if (slow_path == nullptr) {
1856 slow_path =
1857 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1858 instruction->SetSlowPath(slow_path);
1859 codegen_->AddSlowPath(slow_path);
1860 if (successor != nullptr) {
1861 DCHECK(successor->IsLoopHeader());
1862 }
1863 } else {
1864 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1865 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001866
1867 __ LoadFromOffset(kLoadUnsignedHalfword,
1868 TMP,
1869 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001870 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001871 if (successor == nullptr) {
1872 __ Bnezc(TMP, slow_path->GetEntryLabel());
1873 __ Bind(slow_path->GetReturnLabel());
1874 } else {
1875 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001876 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001877 // slow_path will return to GetLabelOf(successor).
1878 }
1879}
1880
1881InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1882 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001883 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001884 assembler_(codegen->GetAssembler()),
1885 codegen_(codegen) {}
1886
1887void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1888 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001889 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001890 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001891 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001892 case DataType::Type::kInt32:
1893 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001894 locations->SetInAt(0, Location::RequiresRegister());
1895 HInstruction* right = instruction->InputAt(1);
1896 bool can_use_imm = false;
1897 if (right->IsConstant()) {
1898 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1899 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1900 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001901 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001902 DCHECK(instruction->IsAdd() || instruction->IsSub());
1903 bool single_use = right->GetUses().HasExactlyOneElement();
1904 if (instruction->IsSub()) {
1905 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1906 imm = -imm;
1907 }
1908 }
1909 if (type == DataType::Type::kInt32) {
1910 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1911 } else {
1912 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1913 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001914 }
1915 }
1916 if (can_use_imm)
1917 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1918 else
1919 locations->SetInAt(1, Location::RequiresRegister());
1920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1921 }
1922 break;
1923
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001924 case DataType::Type::kFloat32:
1925 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001926 locations->SetInAt(0, Location::RequiresFpuRegister());
1927 locations->SetInAt(1, Location::RequiresFpuRegister());
1928 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1929 break;
1930
1931 default:
1932 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1933 }
1934}
1935
1936void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001937 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001938 LocationSummary* locations = instruction->GetLocations();
1939
1940 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 case DataType::Type::kInt32:
1942 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001943 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1944 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1945 Location rhs_location = locations->InAt(1);
1946
1947 GpuRegister rhs_reg = ZERO;
1948 int64_t rhs_imm = 0;
1949 bool use_imm = rhs_location.IsConstant();
1950 if (use_imm) {
1951 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1952 } else {
1953 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1954 }
1955
1956 if (instruction->IsAnd()) {
1957 if (use_imm)
1958 __ Andi(dst, lhs, rhs_imm);
1959 else
1960 __ And(dst, lhs, rhs_reg);
1961 } else if (instruction->IsOr()) {
1962 if (use_imm)
1963 __ Ori(dst, lhs, rhs_imm);
1964 else
1965 __ Or(dst, lhs, rhs_reg);
1966 } else if (instruction->IsXor()) {
1967 if (use_imm)
1968 __ Xori(dst, lhs, rhs_imm);
1969 else
1970 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001971 } else if (instruction->IsAdd() || instruction->IsSub()) {
1972 if (instruction->IsSub()) {
1973 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001974 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001975 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001976 if (use_imm) {
1977 if (IsInt<16>(rhs_imm)) {
1978 __ Addiu(dst, lhs, rhs_imm);
1979 } else {
1980 int16_t rhs_imm_high = High16Bits(rhs_imm);
1981 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1982 if (rhs_imm_low < 0) {
1983 rhs_imm_high += 1;
1984 }
1985 __ Aui(dst, lhs, rhs_imm_high);
1986 if (rhs_imm_low != 0) {
1987 __ Addiu(dst, dst, rhs_imm_low);
1988 }
1989 }
1990 } else {
1991 if (instruction->IsAdd()) {
1992 __ Addu(dst, lhs, rhs_reg);
1993 } else {
1994 DCHECK(instruction->IsSub());
1995 __ Subu(dst, lhs, rhs_reg);
1996 }
1997 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001998 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001999 if (use_imm) {
2000 if (IsInt<16>(rhs_imm)) {
2001 __ Daddiu(dst, lhs, rhs_imm);
2002 } else if (IsInt<32>(rhs_imm)) {
2003 int16_t rhs_imm_high = High16Bits(rhs_imm);
2004 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2005 bool overflow_hi16 = false;
2006 if (rhs_imm_low < 0) {
2007 rhs_imm_high += 1;
2008 overflow_hi16 = (rhs_imm_high == -32768);
2009 }
2010 __ Daui(dst, lhs, rhs_imm_high);
2011 if (rhs_imm_low != 0) {
2012 __ Daddiu(dst, dst, rhs_imm_low);
2013 }
2014 if (overflow_hi16) {
2015 __ Dahi(dst, 1);
2016 }
2017 } else {
2018 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
2019 if (rhs_imm_low < 0) {
2020 rhs_imm += (INT64_C(1) << 16);
2021 }
2022 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
2023 if (rhs_imm_upper < 0) {
2024 rhs_imm += (INT64_C(1) << 32);
2025 }
2026 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
2027 if (rhs_imm_high < 0) {
2028 rhs_imm += (INT64_C(1) << 48);
2029 }
2030 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
2031 GpuRegister tmp = lhs;
2032 if (rhs_imm_low != 0) {
2033 __ Daddiu(dst, tmp, rhs_imm_low);
2034 tmp = dst;
2035 }
2036 // Dahi and Dati must use the same input and output register, so we have to initialize
2037 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
2038 // Daui(dst, lhs, 0).
2039 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
2040 __ Daui(dst, tmp, rhs_imm_upper);
2041 }
2042 if (rhs_imm_high != 0) {
2043 __ Dahi(dst, rhs_imm_high);
2044 }
2045 if (rhs_imm_top != 0) {
2046 __ Dati(dst, rhs_imm_top);
2047 }
2048 }
2049 } else if (instruction->IsAdd()) {
2050 __ Daddu(dst, lhs, rhs_reg);
2051 } else {
2052 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002053 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002054 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002055 }
2056 }
2057 break;
2058 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 case DataType::Type::kFloat32:
2060 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2062 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2063 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2064 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002066 __ AddS(dst, lhs, rhs);
2067 else
2068 __ AddD(dst, lhs, rhs);
2069 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002070 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002071 __ SubS(dst, lhs, rhs);
2072 else
2073 __ SubD(dst, lhs, rhs);
2074 } else {
2075 LOG(FATAL) << "Unexpected floating-point binary operation";
2076 }
2077 break;
2078 }
2079 default:
2080 LOG(FATAL) << "Unexpected binary operation type " << type;
2081 }
2082}
2083
2084void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002085 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002086
Vladimir Markoca6fff82017-10-03 14:49:14 +01002087 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002089 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 case DataType::Type::kInt32:
2091 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002092 locations->SetInAt(0, Location::RequiresRegister());
2093 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002094 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002095 break;
2096 }
2097 default:
2098 LOG(FATAL) << "Unexpected shift type " << type;
2099 }
2100}
2101
2102void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002103 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002104 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002105 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002106
2107 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 case DataType::Type::kInt32:
2109 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002110 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2111 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2112 Location rhs_location = locations->InAt(1);
2113
2114 GpuRegister rhs_reg = ZERO;
2115 int64_t rhs_imm = 0;
2116 bool use_imm = rhs_location.IsConstant();
2117 if (use_imm) {
2118 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2119 } else {
2120 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2121 }
2122
2123 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002124 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002125 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002126
Alexey Frunze92d90602015-12-18 18:16:36 -08002127 if (shift_value == 0) {
2128 if (dst != lhs) {
2129 __ Move(dst, lhs);
2130 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002132 if (instr->IsShl()) {
2133 __ Sll(dst, lhs, shift_value);
2134 } else if (instr->IsShr()) {
2135 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002136 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002137 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002138 } else {
2139 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002140 }
2141 } else {
2142 if (shift_value < 32) {
2143 if (instr->IsShl()) {
2144 __ Dsll(dst, lhs, shift_value);
2145 } else if (instr->IsShr()) {
2146 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002147 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002148 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002149 } else {
2150 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002151 }
2152 } else {
2153 shift_value -= 32;
2154 if (instr->IsShl()) {
2155 __ Dsll32(dst, lhs, shift_value);
2156 } else if (instr->IsShr()) {
2157 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002158 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002159 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002160 } else {
2161 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002162 }
2163 }
2164 }
2165 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002166 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002167 if (instr->IsShl()) {
2168 __ Sllv(dst, lhs, rhs_reg);
2169 } else if (instr->IsShr()) {
2170 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002171 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002172 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002173 } else {
2174 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002175 }
2176 } else {
2177 if (instr->IsShl()) {
2178 __ Dsllv(dst, lhs, rhs_reg);
2179 } else if (instr->IsShr()) {
2180 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002181 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002182 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002183 } else {
2184 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002185 }
2186 }
2187 }
2188 break;
2189 }
2190 default:
2191 LOG(FATAL) << "Unexpected shift operation type " << type;
2192 }
2193}
2194
2195void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2196 HandleBinaryOp(instruction);
2197}
2198
2199void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2200 HandleBinaryOp(instruction);
2201}
2202
2203void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2204 HandleBinaryOp(instruction);
2205}
2206
2207void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2208 HandleBinaryOp(instruction);
2209}
2210
2211void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002212 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002213 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002214 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002216 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2217 object_array_get_with_read_barrier
2218 ? LocationSummary::kCallOnSlowPath
2219 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002220 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2221 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2222 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002223 locations->SetInAt(0, Location::RequiresRegister());
2224 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002225 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002226 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2227 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002228 // The output overlaps in the case of an object array get with
2229 // read barriers enabled: we do not want the move to overwrite the
2230 // array's location, as we need it to emit the read barrier.
2231 locations->SetOut(Location::RequiresRegister(),
2232 object_array_get_with_read_barrier
2233 ? Location::kOutputOverlap
2234 : Location::kNoOutputOverlap);
2235 }
2236 // We need a temporary register for the read barrier marking slow
2237 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2238 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002239 bool temp_needed = instruction->GetIndex()->IsConstant()
2240 ? !kBakerReadBarrierThunksEnableForFields
2241 : !kBakerReadBarrierThunksEnableForArrays;
2242 if (temp_needed) {
2243 locations->AddTemp(Location::RequiresRegister());
2244 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002245 }
2246}
2247
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002248static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2249 auto null_checker = [codegen, instruction]() {
2250 codegen->MaybeRecordImplicitNullCheck(instruction);
2251 };
2252 return null_checker;
2253}
2254
Alexey Frunze4dda3372015-06-01 18:31:49 -07002255void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2256 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002257 Location obj_loc = locations->InAt(0);
2258 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2259 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002260 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002261 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002262 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002265 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2266 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002268 case DataType::Type::kBool:
2269 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002270 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002271 if (index.IsConstant()) {
2272 size_t offset =
2273 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002274 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002275 } else {
2276 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002277 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002278 }
2279 break;
2280 }
2281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002283 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002284 if (index.IsConstant()) {
2285 size_t offset =
2286 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002287 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002288 } else {
2289 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002290 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002291 }
2292 break;
2293 }
2294
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002296 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002297 if (maybe_compressed_char_at) {
2298 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002299 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002300 __ Dext(TMP, TMP, 0, 1);
2301 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2302 "Expecting 0=compressed, 1=uncompressed");
2303 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002304 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002305 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2306 if (maybe_compressed_char_at) {
2307 Mips64Label uncompressed_load, done;
2308 __ Bnezc(TMP, &uncompressed_load);
2309 __ LoadFromOffset(kLoadUnsignedByte,
2310 out,
2311 obj,
2312 data_offset + (const_index << TIMES_1));
2313 __ Bc(&done);
2314 __ Bind(&uncompressed_load);
2315 __ LoadFromOffset(kLoadUnsignedHalfword,
2316 out,
2317 obj,
2318 data_offset + (const_index << TIMES_2));
2319 __ Bind(&done);
2320 } else {
2321 __ LoadFromOffset(kLoadUnsignedHalfword,
2322 out,
2323 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002324 data_offset + (const_index << TIMES_2),
2325 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002326 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002327 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002328 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2329 if (maybe_compressed_char_at) {
2330 Mips64Label uncompressed_load, done;
2331 __ Bnezc(TMP, &uncompressed_load);
2332 __ Daddu(TMP, obj, index_reg);
2333 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2334 __ Bc(&done);
2335 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002336 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002337 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2338 __ Bind(&done);
2339 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002340 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002341 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002342 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 }
2344 break;
2345 }
2346
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002347 case DataType::Type::kInt16: {
2348 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2349 if (index.IsConstant()) {
2350 size_t offset =
2351 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2352 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2353 } else {
2354 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2355 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2356 }
2357 break;
2358 }
2359
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002361 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002362 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 LoadOperandType load_type =
2364 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002365 if (index.IsConstant()) {
2366 size_t offset =
2367 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002368 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002369 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002370 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002371 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002372 }
2373 break;
2374 }
2375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002377 static_assert(
2378 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2379 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2380 // /* HeapReference<Object> */ out =
2381 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2382 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002383 bool temp_needed = index.IsConstant()
2384 ? !kBakerReadBarrierThunksEnableForFields
2385 : !kBakerReadBarrierThunksEnableForArrays;
2386 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002387 // Note that a potential implicit null check is handled in this
2388 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002389 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2390 if (index.IsConstant()) {
2391 // Array load with a constant index can be treated as a field load.
2392 size_t offset =
2393 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2394 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2395 out_loc,
2396 obj,
2397 offset,
2398 temp,
2399 /* needs_null_check */ false);
2400 } else {
2401 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2402 out_loc,
2403 obj,
2404 data_offset,
2405 index,
2406 temp,
2407 /* needs_null_check */ false);
2408 }
Alexey Frunze15958152017-02-09 19:08:30 -08002409 } else {
2410 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2411 if (index.IsConstant()) {
2412 size_t offset =
2413 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2414 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2415 // If read barriers are enabled, emit read barriers other than
2416 // Baker's using a slow path (and also unpoison the loaded
2417 // reference, if heap poisoning is enabled).
2418 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2419 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002420 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002421 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2422 // If read barriers are enabled, emit read barriers other than
2423 // Baker's using a slow path (and also unpoison the loaded
2424 // reference, if heap poisoning is enabled).
2425 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2426 out_loc,
2427 out_loc,
2428 obj_loc,
2429 data_offset,
2430 index);
2431 }
2432 }
2433 break;
2434 }
2435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002437 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002438 if (index.IsConstant()) {
2439 size_t offset =
2440 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002441 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002442 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002443 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002444 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002445 }
2446 break;
2447 }
2448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002449 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002450 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002451 if (index.IsConstant()) {
2452 size_t offset =
2453 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002454 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002455 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002456 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002457 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002458 }
2459 break;
2460 }
2461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002463 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002464 if (index.IsConstant()) {
2465 size_t offset =
2466 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002467 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002469 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002470 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002471 }
2472 break;
2473 }
2474
Aart Bik66c158e2018-01-31 12:55:04 -08002475 case DataType::Type::kUint32:
2476 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002478 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2479 UNREACHABLE();
2480 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481}
2482
2483void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002484 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002485 locations->SetInAt(0, Location::RequiresRegister());
2486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2487}
2488
2489void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2490 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002491 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002492 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2493 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2494 __ LoadFromOffset(kLoadWord, out, obj, offset);
2495 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002496 // Mask out compression flag from String's array length.
2497 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2498 __ Srl(out, out, 1u);
2499 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002500}
2501
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002502Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2503 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2504 ? Location::ConstantLocation(instruction->AsConstant())
2505 : Location::RequiresRegister();
2506}
2507
2508Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2509 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2510 // We can store a non-zero float or double constant without first loading it into the FPU,
2511 // but we should only prefer this if the constant has a single use.
2512 if (instruction->IsConstant() &&
2513 (instruction->AsConstant()->IsZeroBitPattern() ||
2514 instruction->GetUses().HasExactlyOneElement())) {
2515 return Location::ConstantLocation(instruction->AsConstant());
2516 // Otherwise fall through and require an FPU register for the constant.
2517 }
2518 return Location::RequiresFpuRegister();
2519}
2520
Alexey Frunze4dda3372015-06-01 18:31:49 -07002521void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002523
2524 bool needs_write_barrier =
2525 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2526 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2527
Vladimir Markoca6fff82017-10-03 14:49:14 +01002528 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002529 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002530 may_need_runtime_call_for_type_check ?
2531 LocationSummary::kCallOnSlowPath :
2532 LocationSummary::kNoCall);
2533
2534 locations->SetInAt(0, Location::RequiresRegister());
2535 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002537 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002538 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002539 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2540 }
2541 if (needs_write_barrier) {
2542 // Temporary register for the write barrier.
2543 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002544 }
2545}
2546
2547void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2548 LocationSummary* locations = instruction->GetLocations();
2549 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2550 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002551 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002553 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002554 bool needs_write_barrier =
2555 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002556 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002557 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002558
2559 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002561 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002564 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002565 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002566 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002567 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2568 }
2569 if (value_location.IsConstant()) {
2570 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2571 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2572 } else {
2573 GpuRegister value = value_location.AsRegister<GpuRegister>();
2574 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002575 }
2576 break;
2577 }
2578
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002579 case DataType::Type::kUint16:
2580 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002581 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002582 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002583 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002584 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002585 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002586 }
2587 if (value_location.IsConstant()) {
2588 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2589 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2590 } else {
2591 GpuRegister value = value_location.AsRegister<GpuRegister>();
2592 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002593 }
2594 break;
2595 }
2596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002598 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2599 if (index.IsConstant()) {
2600 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2601 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002602 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002603 }
2604 if (value_location.IsConstant()) {
2605 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2606 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2607 } else {
2608 GpuRegister value = value_location.AsRegister<GpuRegister>();
2609 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2610 }
2611 break;
2612 }
2613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002615 if (value_location.IsConstant()) {
2616 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002617 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002618 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002619 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002620 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002621 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002622 }
Alexey Frunze15958152017-02-09 19:08:30 -08002623 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2624 DCHECK_EQ(value, 0);
2625 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2626 DCHECK(!needs_write_barrier);
2627 DCHECK(!may_need_runtime_call_for_type_check);
2628 break;
2629 }
2630
2631 DCHECK(needs_write_barrier);
2632 GpuRegister value = value_location.AsRegister<GpuRegister>();
2633 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2634 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2635 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2636 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2637 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2638 Mips64Label done;
2639 SlowPathCodeMIPS64* slow_path = nullptr;
2640
2641 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002642 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002643 codegen_->AddSlowPath(slow_path);
2644 if (instruction->GetValueCanBeNull()) {
2645 Mips64Label non_zero;
2646 __ Bnezc(value, &non_zero);
2647 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2648 if (index.IsConstant()) {
2649 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002650 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002651 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002652 }
Alexey Frunze15958152017-02-09 19:08:30 -08002653 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2654 __ Bc(&done);
2655 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002656 }
Alexey Frunze15958152017-02-09 19:08:30 -08002657
2658 // Note that when read barriers are enabled, the type checks
2659 // are performed without read barriers. This is fine, even in
2660 // the case where a class object is in the from-space after
2661 // the flip, as a comparison involving such a type would not
2662 // produce a false positive; it may of course produce a false
2663 // negative, in which case we would take the ArraySet slow
2664 // path.
2665
2666 // /* HeapReference<Class> */ temp1 = obj->klass_
2667 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2668 __ MaybeUnpoisonHeapReference(temp1);
2669
2670 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2671 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2672 // /* HeapReference<Class> */ temp2 = value->klass_
2673 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2674 // If heap poisoning is enabled, no need to unpoison `temp1`
2675 // nor `temp2`, as we are comparing two poisoned references.
2676
2677 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2678 Mips64Label do_put;
2679 __ Beqc(temp1, temp2, &do_put);
2680 // If heap poisoning is enabled, the `temp1` reference has
2681 // not been unpoisoned yet; unpoison it now.
2682 __ MaybeUnpoisonHeapReference(temp1);
2683
2684 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2685 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2686 // If heap poisoning is enabled, no need to unpoison
2687 // `temp1`, as we are comparing against null below.
2688 __ Bnezc(temp1, slow_path->GetEntryLabel());
2689 __ Bind(&do_put);
2690 } else {
2691 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2692 }
2693 }
2694
2695 GpuRegister source = value;
2696 if (kPoisonHeapReferences) {
2697 // Note that in the case where `value` is a null reference,
2698 // we do not enter this block, as a null reference does not
2699 // need poisoning.
2700 __ Move(temp1, value);
2701 __ PoisonHeapReference(temp1);
2702 source = temp1;
2703 }
2704
2705 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2706 if (index.IsConstant()) {
2707 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002708 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002709 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002710 }
2711 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2712
2713 if (!may_need_runtime_call_for_type_check) {
2714 codegen_->MaybeRecordImplicitNullCheck(instruction);
2715 }
2716
2717 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2718
2719 if (done.IsLinked()) {
2720 __ Bind(&done);
2721 }
2722
2723 if (slow_path != nullptr) {
2724 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002725 }
2726 break;
2727 }
2728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002730 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002731 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002732 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002733 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002734 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002735 }
2736 if (value_location.IsConstant()) {
2737 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2738 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2739 } else {
2740 GpuRegister value = value_location.AsRegister<GpuRegister>();
2741 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002742 }
2743 break;
2744 }
2745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002747 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002748 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002749 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002750 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002751 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002752 }
2753 if (value_location.IsConstant()) {
2754 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2755 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2756 } else {
2757 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2758 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002759 }
2760 break;
2761 }
2762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002764 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002765 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002766 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002767 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002768 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002769 }
2770 if (value_location.IsConstant()) {
2771 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2772 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2773 } else {
2774 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2775 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002776 }
2777 break;
2778 }
2779
Aart Bik66c158e2018-01-31 12:55:04 -08002780 case DataType::Type::kUint32:
2781 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002783 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2784 UNREACHABLE();
2785 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002786}
2787
2788void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002789 RegisterSet caller_saves = RegisterSet::Empty();
2790 InvokeRuntimeCallingConvention calling_convention;
2791 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2792 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2793 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002794
2795 HInstruction* index = instruction->InputAt(0);
2796 HInstruction* length = instruction->InputAt(1);
2797
2798 bool const_index = false;
2799 bool const_length = false;
2800
2801 if (index->IsConstant()) {
2802 if (length->IsConstant()) {
2803 const_index = true;
2804 const_length = true;
2805 } else {
2806 int32_t index_value = index->AsIntConstant()->GetValue();
2807 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2808 const_index = true;
2809 }
2810 }
2811 } else if (length->IsConstant()) {
2812 int32_t length_value = length->AsIntConstant()->GetValue();
2813 if (IsUint<15>(length_value)) {
2814 const_length = true;
2815 }
2816 }
2817
2818 locations->SetInAt(0, const_index
2819 ? Location::ConstantLocation(index->AsConstant())
2820 : Location::RequiresRegister());
2821 locations->SetInAt(1, const_length
2822 ? Location::ConstantLocation(length->AsConstant())
2823 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002824}
2825
2826void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2827 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002828 Location index_loc = locations->InAt(0);
2829 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002830
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002831 if (length_loc.IsConstant()) {
2832 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2833 if (index_loc.IsConstant()) {
2834 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2835 if (index < 0 || index >= length) {
2836 BoundsCheckSlowPathMIPS64* slow_path =
2837 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2838 codegen_->AddSlowPath(slow_path);
2839 __ Bc(slow_path->GetEntryLabel());
2840 } else {
2841 // Nothing to be done.
2842 }
2843 return;
2844 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002846 BoundsCheckSlowPathMIPS64* slow_path =
2847 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2848 codegen_->AddSlowPath(slow_path);
2849 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2850 if (length == 0) {
2851 __ Bc(slow_path->GetEntryLabel());
2852 } else if (length == 1) {
2853 __ Bnezc(index, slow_path->GetEntryLabel());
2854 } else {
2855 DCHECK(IsUint<15>(length)) << length;
2856 __ Sltiu(TMP, index, length);
2857 __ Beqzc(TMP, slow_path->GetEntryLabel());
2858 }
2859 } else {
2860 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2861 BoundsCheckSlowPathMIPS64* slow_path =
2862 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2863 codegen_->AddSlowPath(slow_path);
2864 if (index_loc.IsConstant()) {
2865 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2866 if (index < 0) {
2867 __ Bc(slow_path->GetEntryLabel());
2868 } else if (index == 0) {
2869 __ Blezc(length, slow_path->GetEntryLabel());
2870 } else {
2871 DCHECK(IsInt<16>(index + 1)) << index;
2872 __ Sltiu(TMP, length, index + 1);
2873 __ Bnezc(TMP, slow_path->GetEntryLabel());
2874 }
2875 } else {
2876 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2877 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2878 }
2879 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002880}
2881
Alexey Frunze15958152017-02-09 19:08:30 -08002882// Temp is used for read barrier.
2883static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2884 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002885 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002886 (kUseBakerReadBarrier ||
2887 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2888 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2889 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2890 return 1;
2891 }
2892 return 0;
2893}
2894
2895// Extra temp is used for read barrier.
2896static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2897 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2898}
2899
Alexey Frunze4dda3372015-06-01 18:31:49 -07002900void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002901 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002902 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002903 LocationSummary* locations =
2904 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002905 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00002906 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
2907 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2908 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2909 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
2910 } else {
2911 locations->SetInAt(1, Location::RequiresRegister());
2912 }
Alexey Frunze15958152017-02-09 19:08:30 -08002913 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002914}
2915
2916void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002917 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002918 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002919 Location obj_loc = locations->InAt(0);
2920 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00002921 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08002922 Location temp_loc = locations->GetTemp(0);
2923 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2924 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2925 DCHECK_LE(num_temps, 2u);
2926 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002927 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2928 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2929 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2930 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2931 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2932 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2933 const uint32_t object_array_data_offset =
2934 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2935 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002936
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002937 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002938 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002939 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2940 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941 codegen_->AddSlowPath(slow_path);
2942
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002943 // Avoid this check if we know `obj` is not null.
2944 if (instruction->MustDoNullCheck()) {
2945 __ Beqzc(obj, &done);
2946 }
2947
2948 switch (type_check_kind) {
2949 case TypeCheckKind::kExactCheck:
2950 case TypeCheckKind::kArrayCheck: {
2951 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002952 GenerateReferenceLoadTwoRegisters(instruction,
2953 temp_loc,
2954 obj_loc,
2955 class_offset,
2956 maybe_temp2_loc,
2957 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002958 // Jump to slow path for throwing the exception or doing a
2959 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00002960 __ Bnec(temp, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002961 break;
2962 }
2963
2964 case TypeCheckKind::kAbstractClassCheck: {
2965 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002966 GenerateReferenceLoadTwoRegisters(instruction,
2967 temp_loc,
2968 obj_loc,
2969 class_offset,
2970 maybe_temp2_loc,
2971 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002972 // If the class is abstract, we eagerly fetch the super class of the
2973 // object to avoid doing a comparison we know will fail.
2974 Mips64Label loop;
2975 __ Bind(&loop);
2976 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002977 GenerateReferenceLoadOneRegister(instruction,
2978 temp_loc,
2979 super_offset,
2980 maybe_temp2_loc,
2981 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002982 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2983 // exception.
2984 __ Beqzc(temp, slow_path->GetEntryLabel());
2985 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00002986 __ Bnec(temp, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002987 break;
2988 }
2989
2990 case TypeCheckKind::kClassHierarchyCheck: {
2991 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002992 GenerateReferenceLoadTwoRegisters(instruction,
2993 temp_loc,
2994 obj_loc,
2995 class_offset,
2996 maybe_temp2_loc,
2997 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002998 // Walk over the class hierarchy to find a match.
2999 Mips64Label loop;
3000 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00003001 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003002 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003003 GenerateReferenceLoadOneRegister(instruction,
3004 temp_loc,
3005 super_offset,
3006 maybe_temp2_loc,
3007 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003008 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3009 // exception. Otherwise, jump to the beginning of the loop.
3010 __ Bnezc(temp, &loop);
3011 __ Bc(slow_path->GetEntryLabel());
3012 break;
3013 }
3014
3015 case TypeCheckKind::kArrayObjectCheck: {
3016 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003017 GenerateReferenceLoadTwoRegisters(instruction,
3018 temp_loc,
3019 obj_loc,
3020 class_offset,
3021 maybe_temp2_loc,
3022 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003023 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003024 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003025 // Otherwise, we need to check that the object's class is a non-primitive array.
3026 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003027 GenerateReferenceLoadOneRegister(instruction,
3028 temp_loc,
3029 component_offset,
3030 maybe_temp2_loc,
3031 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003032 // If the component type is null, jump to the slow path to throw the exception.
3033 __ Beqzc(temp, slow_path->GetEntryLabel());
3034 // Otherwise, the object is indeed an array, further check that this component
3035 // type is not a primitive type.
3036 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3037 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3038 __ Bnezc(temp, slow_path->GetEntryLabel());
3039 break;
3040 }
3041
3042 case TypeCheckKind::kUnresolvedCheck:
3043 // We always go into the type check slow path for the unresolved check case.
3044 // We cannot directly call the CheckCast runtime entry point
3045 // without resorting to a type checking slow path here (i.e. by
3046 // calling InvokeRuntime directly), as it would require to
3047 // assign fixed registers for the inputs of this HInstanceOf
3048 // instruction (following the runtime calling convention), which
3049 // might be cluttered by the potential first read barrier
3050 // emission at the beginning of this method.
3051 __ Bc(slow_path->GetEntryLabel());
3052 break;
3053
3054 case TypeCheckKind::kInterfaceCheck: {
3055 // Avoid read barriers to improve performance of the fast path. We can not get false
3056 // positives by doing this.
3057 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003058 GenerateReferenceLoadTwoRegisters(instruction,
3059 temp_loc,
3060 obj_loc,
3061 class_offset,
3062 maybe_temp2_loc,
3063 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003064 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003065 GenerateReferenceLoadTwoRegisters(instruction,
3066 temp_loc,
3067 temp_loc,
3068 iftable_offset,
3069 maybe_temp2_loc,
3070 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003071 // Iftable is never null.
3072 __ Lw(TMP, temp, array_length_offset);
3073 // Loop through the iftable and check if any class matches.
3074 Mips64Label loop;
3075 __ Bind(&loop);
3076 __ Beqzc(TMP, slow_path->GetEntryLabel());
3077 __ Lwu(AT, temp, object_array_data_offset);
3078 __ MaybeUnpoisonHeapReference(AT);
3079 // Go to next interface.
3080 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3081 __ Addiu(TMP, TMP, -2);
3082 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003083 __ Bnec(AT, cls.AsRegister<GpuRegister>(), &loop);
3084 break;
3085 }
3086
3087 case TypeCheckKind::kBitstringCheck: {
3088 // /* HeapReference<Class> */ temp = obj->klass_
3089 GenerateReferenceLoadTwoRegisters(instruction,
3090 temp_loc,
3091 obj_loc,
3092 class_offset,
3093 maybe_temp2_loc,
3094 kWithoutReadBarrier);
3095
3096 GenerateBitstringTypeCheckCompare(instruction, temp);
3097 __ Bnezc(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003098 break;
3099 }
3100 }
3101
3102 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003103 __ Bind(slow_path->GetExitLabel());
3104}
3105
3106void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3107 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003108 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003109 locations->SetInAt(0, Location::RequiresRegister());
3110 if (check->HasUses()) {
3111 locations->SetOut(Location::SameAsFirstInput());
3112 }
3113}
3114
3115void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3116 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003117 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003118 check->GetLoadClass(),
3119 check,
3120 check->GetDexPc(),
3121 true);
3122 codegen_->AddSlowPath(slow_path);
3123 GenerateClassInitializationCheck(slow_path,
3124 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3125}
3126
3127void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003128 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003129
Vladimir Markoca6fff82017-10-03 14:49:14 +01003130 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003131
3132 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003134 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003136 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003137 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003138 case DataType::Type::kInt32:
3139 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003140 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003141 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3143 break;
3144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 case DataType::Type::kFloat32:
3146 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003147 locations->SetInAt(0, Location::RequiresFpuRegister());
3148 locations->SetInAt(1, Location::RequiresFpuRegister());
3149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003150 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003151
3152 default:
3153 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3154 }
3155}
3156
3157void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3158 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003159 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003161
3162 // 0 if: left == right
3163 // 1 if: left > right
3164 // -1 if: left < right
3165 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003166 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003167 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003168 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003169 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003170 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 case DataType::Type::kInt32:
3172 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003173 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003174 Location rhs_location = locations->InAt(1);
3175 bool use_imm = rhs_location.IsConstant();
3176 GpuRegister rhs = ZERO;
3177 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003178 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003179 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3180 if (value != 0) {
3181 rhs = AT;
3182 __ LoadConst64(rhs, value);
3183 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003184 } else {
3185 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3186 if (value != 0) {
3187 rhs = AT;
3188 __ LoadConst32(rhs, value);
3189 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003190 }
3191 } else {
3192 rhs = rhs_location.AsRegister<GpuRegister>();
3193 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003194 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003195 __ Slt(res, rhs, lhs);
3196 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003197 break;
3198 }
3199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003200 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003201 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3202 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3203 Mips64Label done;
3204 __ CmpEqS(FTMP, lhs, rhs);
3205 __ LoadConst32(res, 0);
3206 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003207 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003208 __ CmpLtS(FTMP, lhs, rhs);
3209 __ LoadConst32(res, -1);
3210 __ Bc1nez(FTMP, &done);
3211 __ LoadConst32(res, 1);
3212 } else {
3213 __ CmpLtS(FTMP, rhs, lhs);
3214 __ LoadConst32(res, 1);
3215 __ Bc1nez(FTMP, &done);
3216 __ LoadConst32(res, -1);
3217 }
3218 __ Bind(&done);
3219 break;
3220 }
3221
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003222 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003223 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3224 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3225 Mips64Label done;
3226 __ CmpEqD(FTMP, lhs, rhs);
3227 __ LoadConst32(res, 0);
3228 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003229 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003230 __ CmpLtD(FTMP, lhs, rhs);
3231 __ LoadConst32(res, -1);
3232 __ Bc1nez(FTMP, &done);
3233 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003234 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003235 __ CmpLtD(FTMP, rhs, lhs);
3236 __ LoadConst32(res, 1);
3237 __ Bc1nez(FTMP, &done);
3238 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003239 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003240 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003241 break;
3242 }
3243
3244 default:
3245 LOG(FATAL) << "Unimplemented compare type " << in_type;
3246 }
3247}
3248
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003249void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003250 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003251 switch (instruction->InputAt(0)->GetType()) {
3252 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003254 locations->SetInAt(0, Location::RequiresRegister());
3255 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3256 break;
3257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003258 case DataType::Type::kFloat32:
3259 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003260 locations->SetInAt(0, Location::RequiresFpuRegister());
3261 locations->SetInAt(1, Location::RequiresFpuRegister());
3262 break;
3263 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003264 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3266 }
3267}
3268
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003269void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003270 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003271 return;
3272 }
3273
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003274 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003275 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003276 switch (type) {
3277 default:
3278 // Integer case.
3279 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3280 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003281 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003282 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3283 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003284 case DataType::Type::kFloat32:
3285 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003286 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3287 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003288 }
3289}
3290
Alexey Frunzec857c742015-09-23 15:12:39 -07003291void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3292 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003293 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003294
3295 LocationSummary* locations = instruction->GetLocations();
3296 Location second = locations->InAt(1);
3297 DCHECK(second.IsConstant());
3298
3299 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3300 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3301 int64_t imm = Int64FromConstant(second.GetConstant());
3302 DCHECK(imm == 1 || imm == -1);
3303
3304 if (instruction->IsRem()) {
3305 __ Move(out, ZERO);
3306 } else {
3307 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003308 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003309 __ Subu(out, ZERO, dividend);
3310 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003311 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003312 __ Dsubu(out, ZERO, dividend);
3313 }
3314 } else if (out != dividend) {
3315 __ Move(out, dividend);
3316 }
3317 }
3318}
3319
3320void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3321 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003322 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003323
3324 LocationSummary* locations = instruction->GetLocations();
3325 Location second = locations->InAt(1);
3326 DCHECK(second.IsConstant());
3327
3328 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3329 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3330 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003331 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003332 int ctz_imm = CTZ(abs_imm);
3333
3334 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003335 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003336 if (ctz_imm == 1) {
3337 // Fast path for division by +/-2, which is very common.
3338 __ Srl(TMP, dividend, 31);
3339 } else {
3340 __ Sra(TMP, dividend, 31);
3341 __ Srl(TMP, TMP, 32 - ctz_imm);
3342 }
3343 __ Addu(out, dividend, TMP);
3344 __ Sra(out, out, ctz_imm);
3345 if (imm < 0) {
3346 __ Subu(out, ZERO, out);
3347 }
3348 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003349 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003350 if (ctz_imm == 1) {
3351 // Fast path for division by +/-2, which is very common.
3352 __ Dsrl32(TMP, dividend, 31);
3353 } else {
3354 __ Dsra32(TMP, dividend, 31);
3355 if (ctz_imm > 32) {
3356 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3357 } else {
3358 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3359 }
3360 }
3361 __ Daddu(out, dividend, TMP);
3362 if (ctz_imm < 32) {
3363 __ Dsra(out, out, ctz_imm);
3364 } else {
3365 __ Dsra32(out, out, ctz_imm - 32);
3366 }
3367 if (imm < 0) {
3368 __ Dsubu(out, ZERO, out);
3369 }
3370 }
3371 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003372 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003373 if (ctz_imm == 1) {
3374 // Fast path for modulo +/-2, which is very common.
3375 __ Sra(TMP, dividend, 31);
3376 __ Subu(out, dividend, TMP);
3377 __ Andi(out, out, 1);
3378 __ Addu(out, out, TMP);
3379 } else {
3380 __ Sra(TMP, dividend, 31);
3381 __ Srl(TMP, TMP, 32 - ctz_imm);
3382 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003383 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003384 __ Subu(out, out, TMP);
3385 }
3386 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003387 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003388 if (ctz_imm == 1) {
3389 // Fast path for modulo +/-2, which is very common.
3390 __ Dsra32(TMP, dividend, 31);
3391 __ Dsubu(out, dividend, TMP);
3392 __ Andi(out, out, 1);
3393 __ Daddu(out, out, TMP);
3394 } else {
3395 __ Dsra32(TMP, dividend, 31);
3396 if (ctz_imm > 32) {
3397 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3398 } else {
3399 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3400 }
3401 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003402 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003403 __ Dsubu(out, out, TMP);
3404 }
3405 }
3406 }
3407}
3408
3409void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3410 DCHECK(instruction->IsDiv() || instruction->IsRem());
3411
3412 LocationSummary* locations = instruction->GetLocations();
3413 Location second = locations->InAt(1);
3414 DCHECK(second.IsConstant());
3415
3416 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3417 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3418 int64_t imm = Int64FromConstant(second.GetConstant());
3419
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003420 DataType::Type type = instruction->GetResultType();
3421 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003422
3423 int64_t magic;
3424 int shift;
3425 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003426 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003427 &magic,
3428 &shift);
3429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003430 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003431 __ LoadConst32(TMP, magic);
3432 __ MuhR6(TMP, dividend, TMP);
3433
3434 if (imm > 0 && magic < 0) {
3435 __ Addu(TMP, TMP, dividend);
3436 } else if (imm < 0 && magic > 0) {
3437 __ Subu(TMP, TMP, dividend);
3438 }
3439
3440 if (shift != 0) {
3441 __ Sra(TMP, TMP, shift);
3442 }
3443
3444 if (instruction->IsDiv()) {
3445 __ Sra(out, TMP, 31);
3446 __ Subu(out, TMP, out);
3447 } else {
3448 __ Sra(AT, TMP, 31);
3449 __ Subu(AT, TMP, AT);
3450 __ LoadConst32(TMP, imm);
3451 __ MulR6(TMP, AT, TMP);
3452 __ Subu(out, dividend, TMP);
3453 }
3454 } else {
3455 __ LoadConst64(TMP, magic);
3456 __ Dmuh(TMP, dividend, TMP);
3457
3458 if (imm > 0 && magic < 0) {
3459 __ Daddu(TMP, TMP, dividend);
3460 } else if (imm < 0 && magic > 0) {
3461 __ Dsubu(TMP, TMP, dividend);
3462 }
3463
3464 if (shift >= 32) {
3465 __ Dsra32(TMP, TMP, shift - 32);
3466 } else if (shift > 0) {
3467 __ Dsra(TMP, TMP, shift);
3468 }
3469
3470 if (instruction->IsDiv()) {
3471 __ Dsra32(out, TMP, 31);
3472 __ Dsubu(out, TMP, out);
3473 } else {
3474 __ Dsra32(AT, TMP, 31);
3475 __ Dsubu(AT, TMP, AT);
3476 __ LoadConst64(TMP, imm);
3477 __ Dmul(TMP, AT, TMP);
3478 __ Dsubu(out, dividend, TMP);
3479 }
3480 }
3481}
3482
3483void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3484 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003485 DataType::Type type = instruction->GetResultType();
3486 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003487
3488 LocationSummary* locations = instruction->GetLocations();
3489 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3490 Location second = locations->InAt(1);
3491
3492 if (second.IsConstant()) {
3493 int64_t imm = Int64FromConstant(second.GetConstant());
3494 if (imm == 0) {
3495 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3496 } else if (imm == 1 || imm == -1) {
3497 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003498 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003499 DivRemByPowerOfTwo(instruction);
3500 } else {
3501 DCHECK(imm <= -2 || imm >= 2);
3502 GenerateDivRemWithAnyConstant(instruction);
3503 }
3504 } else {
3505 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3506 GpuRegister divisor = second.AsRegister<GpuRegister>();
3507 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003508 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003509 __ DivR6(out, dividend, divisor);
3510 else
3511 __ Ddiv(out, dividend, divisor);
3512 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003513 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003514 __ ModR6(out, dividend, divisor);
3515 else
3516 __ Dmod(out, dividend, divisor);
3517 }
3518 }
3519}
3520
Alexey Frunze4dda3372015-06-01 18:31:49 -07003521void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3522 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003523 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003524 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003525 case DataType::Type::kInt32:
3526 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003527 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003528 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3530 break;
3531
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003532 case DataType::Type::kFloat32:
3533 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003534 locations->SetInAt(0, Location::RequiresFpuRegister());
3535 locations->SetInAt(1, Location::RequiresFpuRegister());
3536 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3537 break;
3538
3539 default:
3540 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3541 }
3542}
3543
3544void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003545 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003546 LocationSummary* locations = instruction->GetLocations();
3547
3548 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003549 case DataType::Type::kInt32:
3550 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003551 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003552 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003553 case DataType::Type::kFloat32:
3554 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003555 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3556 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3557 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003558 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003559 __ DivS(dst, lhs, rhs);
3560 else
3561 __ DivD(dst, lhs, rhs);
3562 break;
3563 }
3564 default:
3565 LOG(FATAL) << "Unexpected div type " << type;
3566 }
3567}
3568
3569void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003570 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003571 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003572}
3573
3574void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3575 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003576 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003577 codegen_->AddSlowPath(slow_path);
3578 Location value = instruction->GetLocations()->InAt(0);
3579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003580 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003582 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003583 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003584 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003585 }
3586
3587 if (value.IsConstant()) {
3588 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3589 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003590 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003591 } else {
3592 // A division by a non-null constant is valid. We don't need to perform
3593 // any check, so simply fall through.
3594 }
3595 } else {
3596 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3597 }
3598}
3599
3600void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3601 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003602 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003603 locations->SetOut(Location::ConstantLocation(constant));
3604}
3605
3606void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3607 // Will be generated at use site.
3608}
3609
3610void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3611 exit->SetLocations(nullptr);
3612}
3613
3614void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3615}
3616
3617void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3618 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003619 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003620 locations->SetOut(Location::ConstantLocation(constant));
3621}
3622
3623void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3624 // Will be generated at use site.
3625}
3626
David Brazdilfc6a86a2015-06-26 10:33:45 +00003627void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003628 if (successor->IsExitBlock()) {
3629 DCHECK(got->GetPrevious()->AlwaysThrows());
3630 return; // no code needed
3631 }
3632
Alexey Frunze4dda3372015-06-01 18:31:49 -07003633 HBasicBlock* block = got->GetBlock();
3634 HInstruction* previous = got->GetPrevious();
3635 HLoopInformation* info = block->GetLoopInformation();
3636
3637 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01003638 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3639 __ Ld(AT, SP, kCurrentMethodStackOffset);
3640 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3641 __ Addiu(TMP, TMP, 1);
3642 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3643 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003644 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3645 return;
3646 }
3647 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3648 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3649 }
3650 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003651 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003652 }
3653}
3654
David Brazdilfc6a86a2015-06-26 10:33:45 +00003655void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3656 got->SetLocations(nullptr);
3657}
3658
3659void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3660 HandleGoto(got, got->GetSuccessor());
3661}
3662
3663void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3664 try_boundary->SetLocations(nullptr);
3665}
3666
3667void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3668 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3669 if (!successor->IsExitBlock()) {
3670 HandleGoto(try_boundary, successor);
3671 }
3672}
3673
Alexey Frunze299a9392015-12-08 16:08:02 -08003674void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3675 bool is64bit,
3676 LocationSummary* locations) {
3677 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3678 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3679 Location rhs_location = locations->InAt(1);
3680 GpuRegister rhs_reg = ZERO;
3681 int64_t rhs_imm = 0;
3682 bool use_imm = rhs_location.IsConstant();
3683 if (use_imm) {
3684 if (is64bit) {
3685 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3686 } else {
3687 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3688 }
3689 } else {
3690 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3691 }
3692 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3693
3694 switch (cond) {
3695 case kCondEQ:
3696 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003697 if (use_imm && IsInt<16>(-rhs_imm)) {
3698 if (rhs_imm == 0) {
3699 if (cond == kCondEQ) {
3700 __ Sltiu(dst, lhs, 1);
3701 } else {
3702 __ Sltu(dst, ZERO, lhs);
3703 }
3704 } else {
3705 if (is64bit) {
3706 __ Daddiu(dst, lhs, -rhs_imm);
3707 } else {
3708 __ Addiu(dst, lhs, -rhs_imm);
3709 }
3710 if (cond == kCondEQ) {
3711 __ Sltiu(dst, dst, 1);
3712 } else {
3713 __ Sltu(dst, ZERO, dst);
3714 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003715 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003716 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003717 if (use_imm && IsUint<16>(rhs_imm)) {
3718 __ Xori(dst, lhs, rhs_imm);
3719 } else {
3720 if (use_imm) {
3721 rhs_reg = TMP;
3722 __ LoadConst64(rhs_reg, rhs_imm);
3723 }
3724 __ Xor(dst, lhs, rhs_reg);
3725 }
3726 if (cond == kCondEQ) {
3727 __ Sltiu(dst, dst, 1);
3728 } else {
3729 __ Sltu(dst, ZERO, dst);
3730 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003731 }
3732 break;
3733
3734 case kCondLT:
3735 case kCondGE:
3736 if (use_imm && IsInt<16>(rhs_imm)) {
3737 __ Slti(dst, lhs, rhs_imm);
3738 } else {
3739 if (use_imm) {
3740 rhs_reg = TMP;
3741 __ LoadConst64(rhs_reg, rhs_imm);
3742 }
3743 __ Slt(dst, lhs, rhs_reg);
3744 }
3745 if (cond == kCondGE) {
3746 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3747 // only the slt instruction but no sge.
3748 __ Xori(dst, dst, 1);
3749 }
3750 break;
3751
3752 case kCondLE:
3753 case kCondGT:
3754 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3755 // Simulate lhs <= rhs via lhs < rhs + 1.
3756 __ Slti(dst, lhs, rhs_imm_plus_one);
3757 if (cond == kCondGT) {
3758 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3759 // only the slti instruction but no sgti.
3760 __ Xori(dst, dst, 1);
3761 }
3762 } else {
3763 if (use_imm) {
3764 rhs_reg = TMP;
3765 __ LoadConst64(rhs_reg, rhs_imm);
3766 }
3767 __ Slt(dst, rhs_reg, lhs);
3768 if (cond == kCondLE) {
3769 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3770 // only the slt instruction but no sle.
3771 __ Xori(dst, dst, 1);
3772 }
3773 }
3774 break;
3775
3776 case kCondB:
3777 case kCondAE:
3778 if (use_imm && IsInt<16>(rhs_imm)) {
3779 // Sltiu sign-extends its 16-bit immediate operand before
3780 // the comparison and thus lets us compare directly with
3781 // unsigned values in the ranges [0, 0x7fff] and
3782 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3783 __ Sltiu(dst, lhs, rhs_imm);
3784 } else {
3785 if (use_imm) {
3786 rhs_reg = TMP;
3787 __ LoadConst64(rhs_reg, rhs_imm);
3788 }
3789 __ Sltu(dst, lhs, rhs_reg);
3790 }
3791 if (cond == kCondAE) {
3792 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3793 // only the sltu instruction but no sgeu.
3794 __ Xori(dst, dst, 1);
3795 }
3796 break;
3797
3798 case kCondBE:
3799 case kCondA:
3800 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3801 // Simulate lhs <= rhs via lhs < rhs + 1.
3802 // Note that this only works if rhs + 1 does not overflow
3803 // to 0, hence the check above.
3804 // Sltiu sign-extends its 16-bit immediate operand before
3805 // the comparison and thus lets us compare directly with
3806 // unsigned values in the ranges [0, 0x7fff] and
3807 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3808 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3809 if (cond == kCondA) {
3810 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3811 // only the sltiu instruction but no sgtiu.
3812 __ Xori(dst, dst, 1);
3813 }
3814 } else {
3815 if (use_imm) {
3816 rhs_reg = TMP;
3817 __ LoadConst64(rhs_reg, rhs_imm);
3818 }
3819 __ Sltu(dst, rhs_reg, lhs);
3820 if (cond == kCondBE) {
3821 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3822 // only the sltu instruction but no sleu.
3823 __ Xori(dst, dst, 1);
3824 }
3825 }
3826 break;
3827 }
3828}
3829
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003830bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3831 bool is64bit,
3832 LocationSummary* input_locations,
3833 GpuRegister dst) {
3834 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3835 Location rhs_location = input_locations->InAt(1);
3836 GpuRegister rhs_reg = ZERO;
3837 int64_t rhs_imm = 0;
3838 bool use_imm = rhs_location.IsConstant();
3839 if (use_imm) {
3840 if (is64bit) {
3841 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3842 } else {
3843 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3844 }
3845 } else {
3846 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3847 }
3848 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3849
3850 switch (cond) {
3851 case kCondEQ:
3852 case kCondNE:
3853 if (use_imm && IsInt<16>(-rhs_imm)) {
3854 if (is64bit) {
3855 __ Daddiu(dst, lhs, -rhs_imm);
3856 } else {
3857 __ Addiu(dst, lhs, -rhs_imm);
3858 }
3859 } else if (use_imm && IsUint<16>(rhs_imm)) {
3860 __ Xori(dst, lhs, rhs_imm);
3861 } else {
3862 if (use_imm) {
3863 rhs_reg = TMP;
3864 __ LoadConst64(rhs_reg, rhs_imm);
3865 }
3866 __ Xor(dst, lhs, rhs_reg);
3867 }
3868 return (cond == kCondEQ);
3869
3870 case kCondLT:
3871 case kCondGE:
3872 if (use_imm && IsInt<16>(rhs_imm)) {
3873 __ Slti(dst, lhs, rhs_imm);
3874 } else {
3875 if (use_imm) {
3876 rhs_reg = TMP;
3877 __ LoadConst64(rhs_reg, rhs_imm);
3878 }
3879 __ Slt(dst, lhs, rhs_reg);
3880 }
3881 return (cond == kCondGE);
3882
3883 case kCondLE:
3884 case kCondGT:
3885 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3886 // Simulate lhs <= rhs via lhs < rhs + 1.
3887 __ Slti(dst, lhs, rhs_imm_plus_one);
3888 return (cond == kCondGT);
3889 } else {
3890 if (use_imm) {
3891 rhs_reg = TMP;
3892 __ LoadConst64(rhs_reg, rhs_imm);
3893 }
3894 __ Slt(dst, rhs_reg, lhs);
3895 return (cond == kCondLE);
3896 }
3897
3898 case kCondB:
3899 case kCondAE:
3900 if (use_imm && IsInt<16>(rhs_imm)) {
3901 // Sltiu sign-extends its 16-bit immediate operand before
3902 // the comparison and thus lets us compare directly with
3903 // unsigned values in the ranges [0, 0x7fff] and
3904 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3905 __ Sltiu(dst, lhs, rhs_imm);
3906 } else {
3907 if (use_imm) {
3908 rhs_reg = TMP;
3909 __ LoadConst64(rhs_reg, rhs_imm);
3910 }
3911 __ Sltu(dst, lhs, rhs_reg);
3912 }
3913 return (cond == kCondAE);
3914
3915 case kCondBE:
3916 case kCondA:
3917 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3918 // Simulate lhs <= rhs via lhs < rhs + 1.
3919 // Note that this only works if rhs + 1 does not overflow
3920 // to 0, hence the check above.
3921 // Sltiu sign-extends its 16-bit immediate operand before
3922 // the comparison and thus lets us compare directly with
3923 // unsigned values in the ranges [0, 0x7fff] and
3924 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3925 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3926 return (cond == kCondA);
3927 } else {
3928 if (use_imm) {
3929 rhs_reg = TMP;
3930 __ LoadConst64(rhs_reg, rhs_imm);
3931 }
3932 __ Sltu(dst, rhs_reg, lhs);
3933 return (cond == kCondBE);
3934 }
3935 }
3936}
3937
Alexey Frunze299a9392015-12-08 16:08:02 -08003938void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3939 bool is64bit,
3940 LocationSummary* locations,
3941 Mips64Label* label) {
3942 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3943 Location rhs_location = locations->InAt(1);
3944 GpuRegister rhs_reg = ZERO;
3945 int64_t rhs_imm = 0;
3946 bool use_imm = rhs_location.IsConstant();
3947 if (use_imm) {
3948 if (is64bit) {
3949 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3950 } else {
3951 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3952 }
3953 } else {
3954 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3955 }
3956
3957 if (use_imm && rhs_imm == 0) {
3958 switch (cond) {
3959 case kCondEQ:
3960 case kCondBE: // <= 0 if zero
3961 __ Beqzc(lhs, label);
3962 break;
3963 case kCondNE:
3964 case kCondA: // > 0 if non-zero
3965 __ Bnezc(lhs, label);
3966 break;
3967 case kCondLT:
3968 __ Bltzc(lhs, label);
3969 break;
3970 case kCondGE:
3971 __ Bgezc(lhs, label);
3972 break;
3973 case kCondLE:
3974 __ Blezc(lhs, label);
3975 break;
3976 case kCondGT:
3977 __ Bgtzc(lhs, label);
3978 break;
3979 case kCondB: // always false
3980 break;
3981 case kCondAE: // always true
3982 __ Bc(label);
3983 break;
3984 }
3985 } else {
3986 if (use_imm) {
3987 rhs_reg = TMP;
3988 __ LoadConst64(rhs_reg, rhs_imm);
3989 }
3990 switch (cond) {
3991 case kCondEQ:
3992 __ Beqc(lhs, rhs_reg, label);
3993 break;
3994 case kCondNE:
3995 __ Bnec(lhs, rhs_reg, label);
3996 break;
3997 case kCondLT:
3998 __ Bltc(lhs, rhs_reg, label);
3999 break;
4000 case kCondGE:
4001 __ Bgec(lhs, rhs_reg, label);
4002 break;
4003 case kCondLE:
4004 __ Bgec(rhs_reg, lhs, label);
4005 break;
4006 case kCondGT:
4007 __ Bltc(rhs_reg, lhs, label);
4008 break;
4009 case kCondB:
4010 __ Bltuc(lhs, rhs_reg, label);
4011 break;
4012 case kCondAE:
4013 __ Bgeuc(lhs, rhs_reg, label);
4014 break;
4015 case kCondBE:
4016 __ Bgeuc(rhs_reg, lhs, label);
4017 break;
4018 case kCondA:
4019 __ Bltuc(rhs_reg, lhs, label);
4020 break;
4021 }
4022 }
4023}
4024
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004025void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
4026 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004027 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004028 LocationSummary* locations) {
4029 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4030 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4031 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004032 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004033 switch (cond) {
4034 case kCondEQ:
4035 __ CmpEqS(FTMP, lhs, rhs);
4036 __ Mfc1(dst, FTMP);
4037 __ Andi(dst, dst, 1);
4038 break;
4039 case kCondNE:
4040 __ CmpEqS(FTMP, lhs, rhs);
4041 __ Mfc1(dst, FTMP);
4042 __ Addiu(dst, dst, 1);
4043 break;
4044 case kCondLT:
4045 if (gt_bias) {
4046 __ CmpLtS(FTMP, lhs, rhs);
4047 } else {
4048 __ CmpUltS(FTMP, lhs, rhs);
4049 }
4050 __ Mfc1(dst, FTMP);
4051 __ Andi(dst, dst, 1);
4052 break;
4053 case kCondLE:
4054 if (gt_bias) {
4055 __ CmpLeS(FTMP, lhs, rhs);
4056 } else {
4057 __ CmpUleS(FTMP, lhs, rhs);
4058 }
4059 __ Mfc1(dst, FTMP);
4060 __ Andi(dst, dst, 1);
4061 break;
4062 case kCondGT:
4063 if (gt_bias) {
4064 __ CmpUltS(FTMP, rhs, lhs);
4065 } else {
4066 __ CmpLtS(FTMP, rhs, lhs);
4067 }
4068 __ Mfc1(dst, FTMP);
4069 __ Andi(dst, dst, 1);
4070 break;
4071 case kCondGE:
4072 if (gt_bias) {
4073 __ CmpUleS(FTMP, rhs, lhs);
4074 } else {
4075 __ CmpLeS(FTMP, rhs, lhs);
4076 }
4077 __ Mfc1(dst, FTMP);
4078 __ Andi(dst, dst, 1);
4079 break;
4080 default:
4081 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4082 UNREACHABLE();
4083 }
4084 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004086 switch (cond) {
4087 case kCondEQ:
4088 __ CmpEqD(FTMP, lhs, rhs);
4089 __ Mfc1(dst, FTMP);
4090 __ Andi(dst, dst, 1);
4091 break;
4092 case kCondNE:
4093 __ CmpEqD(FTMP, lhs, rhs);
4094 __ Mfc1(dst, FTMP);
4095 __ Addiu(dst, dst, 1);
4096 break;
4097 case kCondLT:
4098 if (gt_bias) {
4099 __ CmpLtD(FTMP, lhs, rhs);
4100 } else {
4101 __ CmpUltD(FTMP, lhs, rhs);
4102 }
4103 __ Mfc1(dst, FTMP);
4104 __ Andi(dst, dst, 1);
4105 break;
4106 case kCondLE:
4107 if (gt_bias) {
4108 __ CmpLeD(FTMP, lhs, rhs);
4109 } else {
4110 __ CmpUleD(FTMP, lhs, rhs);
4111 }
4112 __ Mfc1(dst, FTMP);
4113 __ Andi(dst, dst, 1);
4114 break;
4115 case kCondGT:
4116 if (gt_bias) {
4117 __ CmpUltD(FTMP, rhs, lhs);
4118 } else {
4119 __ CmpLtD(FTMP, rhs, lhs);
4120 }
4121 __ Mfc1(dst, FTMP);
4122 __ Andi(dst, dst, 1);
4123 break;
4124 case kCondGE:
4125 if (gt_bias) {
4126 __ CmpUleD(FTMP, rhs, lhs);
4127 } else {
4128 __ CmpLeD(FTMP, rhs, lhs);
4129 }
4130 __ Mfc1(dst, FTMP);
4131 __ Andi(dst, dst, 1);
4132 break;
4133 default:
4134 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4135 UNREACHABLE();
4136 }
4137 }
4138}
4139
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004140bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4141 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004142 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004143 LocationSummary* input_locations,
4144 FpuRegister dst) {
4145 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4146 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004147 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004148 switch (cond) {
4149 case kCondEQ:
4150 __ CmpEqS(dst, lhs, rhs);
4151 return false;
4152 case kCondNE:
4153 __ CmpEqS(dst, lhs, rhs);
4154 return true;
4155 case kCondLT:
4156 if (gt_bias) {
4157 __ CmpLtS(dst, lhs, rhs);
4158 } else {
4159 __ CmpUltS(dst, lhs, rhs);
4160 }
4161 return false;
4162 case kCondLE:
4163 if (gt_bias) {
4164 __ CmpLeS(dst, lhs, rhs);
4165 } else {
4166 __ CmpUleS(dst, lhs, rhs);
4167 }
4168 return false;
4169 case kCondGT:
4170 if (gt_bias) {
4171 __ CmpUltS(dst, rhs, lhs);
4172 } else {
4173 __ CmpLtS(dst, rhs, lhs);
4174 }
4175 return false;
4176 case kCondGE:
4177 if (gt_bias) {
4178 __ CmpUleS(dst, rhs, lhs);
4179 } else {
4180 __ CmpLeS(dst, rhs, lhs);
4181 }
4182 return false;
4183 default:
4184 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4185 UNREACHABLE();
4186 }
4187 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004188 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004189 switch (cond) {
4190 case kCondEQ:
4191 __ CmpEqD(dst, lhs, rhs);
4192 return false;
4193 case kCondNE:
4194 __ CmpEqD(dst, lhs, rhs);
4195 return true;
4196 case kCondLT:
4197 if (gt_bias) {
4198 __ CmpLtD(dst, lhs, rhs);
4199 } else {
4200 __ CmpUltD(dst, lhs, rhs);
4201 }
4202 return false;
4203 case kCondLE:
4204 if (gt_bias) {
4205 __ CmpLeD(dst, lhs, rhs);
4206 } else {
4207 __ CmpUleD(dst, lhs, rhs);
4208 }
4209 return false;
4210 case kCondGT:
4211 if (gt_bias) {
4212 __ CmpUltD(dst, rhs, lhs);
4213 } else {
4214 __ CmpLtD(dst, rhs, lhs);
4215 }
4216 return false;
4217 case kCondGE:
4218 if (gt_bias) {
4219 __ CmpUleD(dst, rhs, lhs);
4220 } else {
4221 __ CmpLeD(dst, rhs, lhs);
4222 }
4223 return false;
4224 default:
4225 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4226 UNREACHABLE();
4227 }
4228 }
4229}
4230
Alexey Frunze299a9392015-12-08 16:08:02 -08004231void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4232 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004233 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004234 LocationSummary* locations,
4235 Mips64Label* label) {
4236 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4237 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004238 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004239 switch (cond) {
4240 case kCondEQ:
4241 __ CmpEqS(FTMP, lhs, rhs);
4242 __ Bc1nez(FTMP, label);
4243 break;
4244 case kCondNE:
4245 __ CmpEqS(FTMP, lhs, rhs);
4246 __ Bc1eqz(FTMP, label);
4247 break;
4248 case kCondLT:
4249 if (gt_bias) {
4250 __ CmpLtS(FTMP, lhs, rhs);
4251 } else {
4252 __ CmpUltS(FTMP, lhs, rhs);
4253 }
4254 __ Bc1nez(FTMP, label);
4255 break;
4256 case kCondLE:
4257 if (gt_bias) {
4258 __ CmpLeS(FTMP, lhs, rhs);
4259 } else {
4260 __ CmpUleS(FTMP, lhs, rhs);
4261 }
4262 __ Bc1nez(FTMP, label);
4263 break;
4264 case kCondGT:
4265 if (gt_bias) {
4266 __ CmpUltS(FTMP, rhs, lhs);
4267 } else {
4268 __ CmpLtS(FTMP, rhs, lhs);
4269 }
4270 __ Bc1nez(FTMP, label);
4271 break;
4272 case kCondGE:
4273 if (gt_bias) {
4274 __ CmpUleS(FTMP, rhs, lhs);
4275 } else {
4276 __ CmpLeS(FTMP, rhs, lhs);
4277 }
4278 __ Bc1nez(FTMP, label);
4279 break;
4280 default:
4281 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004282 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004283 }
4284 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004286 switch (cond) {
4287 case kCondEQ:
4288 __ CmpEqD(FTMP, lhs, rhs);
4289 __ Bc1nez(FTMP, label);
4290 break;
4291 case kCondNE:
4292 __ CmpEqD(FTMP, lhs, rhs);
4293 __ Bc1eqz(FTMP, label);
4294 break;
4295 case kCondLT:
4296 if (gt_bias) {
4297 __ CmpLtD(FTMP, lhs, rhs);
4298 } else {
4299 __ CmpUltD(FTMP, lhs, rhs);
4300 }
4301 __ Bc1nez(FTMP, label);
4302 break;
4303 case kCondLE:
4304 if (gt_bias) {
4305 __ CmpLeD(FTMP, lhs, rhs);
4306 } else {
4307 __ CmpUleD(FTMP, lhs, rhs);
4308 }
4309 __ Bc1nez(FTMP, label);
4310 break;
4311 case kCondGT:
4312 if (gt_bias) {
4313 __ CmpUltD(FTMP, rhs, lhs);
4314 } else {
4315 __ CmpLtD(FTMP, rhs, lhs);
4316 }
4317 __ Bc1nez(FTMP, label);
4318 break;
4319 case kCondGE:
4320 if (gt_bias) {
4321 __ CmpUleD(FTMP, rhs, lhs);
4322 } else {
4323 __ CmpLeD(FTMP, rhs, lhs);
4324 }
4325 __ Bc1nez(FTMP, label);
4326 break;
4327 default:
4328 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004329 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004330 }
4331 }
4332}
4333
Alexey Frunze4dda3372015-06-01 18:31:49 -07004334void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004335 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004336 Mips64Label* true_target,
4337 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004338 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004339
David Brazdil0debae72015-11-12 18:37:00 +00004340 if (true_target == nullptr && false_target == nullptr) {
4341 // Nothing to do. The code always falls through.
4342 return;
4343 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004344 // Constant condition, statically compared against "true" (integer value 1).
4345 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004346 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004347 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004348 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004349 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004350 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004351 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004352 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004353 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004354 }
David Brazdil0debae72015-11-12 18:37:00 +00004355 return;
4356 }
4357
4358 // The following code generates these patterns:
4359 // (1) true_target == nullptr && false_target != nullptr
4360 // - opposite condition true => branch to false_target
4361 // (2) true_target != nullptr && false_target == nullptr
4362 // - condition true => branch to true_target
4363 // (3) true_target != nullptr && false_target != nullptr
4364 // - condition true => branch to true_target
4365 // - branch to false_target
4366 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004367 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004368 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004369 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004370 if (true_target == nullptr) {
4371 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4372 } else {
4373 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4374 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004375 } else {
4376 // The condition instruction has not been materialized, use its inputs as
4377 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004378 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004379 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004380 LocationSummary* locations = cond->GetLocations();
4381 IfCondition if_cond = condition->GetCondition();
4382 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004383
David Brazdil0debae72015-11-12 18:37:00 +00004384 if (true_target == nullptr) {
4385 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004386 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004387 }
4388
Alexey Frunze299a9392015-12-08 16:08:02 -08004389 switch (type) {
4390 default:
4391 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4392 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004393 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004394 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4395 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004396 case DataType::Type::kFloat32:
4397 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004398 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4399 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004400 }
4401 }
David Brazdil0debae72015-11-12 18:37:00 +00004402
4403 // If neither branch falls through (case 3), the conditional branch to `true_target`
4404 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4405 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004406 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004407 }
4408}
4409
4410void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004411 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004412 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004413 locations->SetInAt(0, Location::RequiresRegister());
4414 }
4415}
4416
4417void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004418 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4419 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004420 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004421 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004422 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004423 nullptr : codegen_->GetLabelOf(false_successor);
4424 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004425}
4426
4427void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004428 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004429 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004430 InvokeRuntimeCallingConvention calling_convention;
4431 RegisterSet caller_saves = RegisterSet::Empty();
4432 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4433 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004434 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004435 locations->SetInAt(0, Location::RequiresRegister());
4436 }
4437}
4438
4439void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004440 SlowPathCodeMIPS64* slow_path =
4441 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004442 GenerateTestAndBranch(deoptimize,
4443 /* condition_input_index */ 0,
4444 slow_path->GetEntryLabel(),
4445 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004446}
4447
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004448// This function returns true if a conditional move can be generated for HSelect.
4449// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4450// branches and regular moves.
4451//
4452// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4453//
4454// While determining feasibility of a conditional move and setting inputs/outputs
4455// are two distinct tasks, this function does both because they share quite a bit
4456// of common logic.
4457static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4458 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4459 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4460 HCondition* condition = cond->AsCondition();
4461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004462 DataType::Type cond_type =
4463 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4464 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004465
4466 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4467 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4468 bool is_true_value_zero_constant =
4469 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4470 bool is_false_value_zero_constant =
4471 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4472
4473 bool can_move_conditionally = false;
4474 bool use_const_for_false_in = false;
4475 bool use_const_for_true_in = false;
4476
4477 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004478 if (!DataType::IsFloatingPointType(cond_type)) {
4479 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004480 // Moving int/long on int/long condition.
4481 if (is_true_value_zero_constant) {
4482 // seleqz out_reg, false_reg, cond_reg
4483 can_move_conditionally = true;
4484 use_const_for_true_in = true;
4485 } else if (is_false_value_zero_constant) {
4486 // selnez out_reg, true_reg, cond_reg
4487 can_move_conditionally = true;
4488 use_const_for_false_in = true;
4489 } else if (materialized) {
4490 // Not materializing unmaterialized int conditions
4491 // to keep the instruction count low.
4492 // selnez AT, true_reg, cond_reg
4493 // seleqz TMP, false_reg, cond_reg
4494 // or out_reg, AT, TMP
4495 can_move_conditionally = true;
4496 }
4497 } else {
4498 // Moving float/double on int/long condition.
4499 if (materialized) {
4500 // Not materializing unmaterialized int conditions
4501 // to keep the instruction count low.
4502 can_move_conditionally = true;
4503 if (is_true_value_zero_constant) {
4504 // sltu TMP, ZERO, cond_reg
4505 // mtc1 TMP, temp_cond_reg
4506 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4507 use_const_for_true_in = true;
4508 } else if (is_false_value_zero_constant) {
4509 // sltu TMP, ZERO, cond_reg
4510 // mtc1 TMP, temp_cond_reg
4511 // selnez.fmt out_reg, true_reg, temp_cond_reg
4512 use_const_for_false_in = true;
4513 } else {
4514 // sltu TMP, ZERO, cond_reg
4515 // mtc1 TMP, temp_cond_reg
4516 // sel.fmt temp_cond_reg, false_reg, true_reg
4517 // mov.fmt out_reg, temp_cond_reg
4518 }
4519 }
4520 }
4521 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004522 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004523 // Moving int/long on float/double condition.
4524 can_move_conditionally = true;
4525 if (is_true_value_zero_constant) {
4526 // mfc1 TMP, temp_cond_reg
4527 // seleqz out_reg, false_reg, TMP
4528 use_const_for_true_in = true;
4529 } else if (is_false_value_zero_constant) {
4530 // mfc1 TMP, temp_cond_reg
4531 // selnez out_reg, true_reg, TMP
4532 use_const_for_false_in = true;
4533 } else {
4534 // mfc1 TMP, temp_cond_reg
4535 // selnez AT, true_reg, TMP
4536 // seleqz TMP, false_reg, TMP
4537 // or out_reg, AT, TMP
4538 }
4539 } else {
4540 // Moving float/double on float/double condition.
4541 can_move_conditionally = true;
4542 if (is_true_value_zero_constant) {
4543 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4544 use_const_for_true_in = true;
4545 } else if (is_false_value_zero_constant) {
4546 // selnez.fmt out_reg, true_reg, temp_cond_reg
4547 use_const_for_false_in = true;
4548 } else {
4549 // sel.fmt temp_cond_reg, false_reg, true_reg
4550 // mov.fmt out_reg, temp_cond_reg
4551 }
4552 }
4553 }
4554 }
4555
4556 if (can_move_conditionally) {
4557 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4558 } else {
4559 DCHECK(!use_const_for_false_in);
4560 DCHECK(!use_const_for_true_in);
4561 }
4562
4563 if (locations_to_set != nullptr) {
4564 if (use_const_for_false_in) {
4565 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4566 } else {
4567 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004568 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004569 ? Location::RequiresFpuRegister()
4570 : Location::RequiresRegister());
4571 }
4572 if (use_const_for_true_in) {
4573 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4574 } else {
4575 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004576 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004577 ? Location::RequiresFpuRegister()
4578 : Location::RequiresRegister());
4579 }
4580 if (materialized) {
4581 locations_to_set->SetInAt(2, Location::RequiresRegister());
4582 }
4583
4584 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004585 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004586 ? Location::RequiresFpuRegister()
4587 : Location::RequiresRegister());
4588 } else {
4589 locations_to_set->SetOut(Location::SameAsFirstInput());
4590 }
4591 }
4592
4593 return can_move_conditionally;
4594}
4595
4596
4597void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4598 LocationSummary* locations = select->GetLocations();
4599 Location dst = locations->Out();
4600 Location false_src = locations->InAt(0);
4601 Location true_src = locations->InAt(1);
4602 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4603 GpuRegister cond_reg = TMP;
4604 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004605 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004606 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004607 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004608
4609 if (IsBooleanValueOrMaterializedCondition(cond)) {
4610 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4611 } else {
4612 HCondition* condition = cond->AsCondition();
4613 LocationSummary* cond_locations = cond->GetLocations();
4614 IfCondition if_cond = condition->GetCondition();
4615 cond_type = condition->InputAt(0)->GetType();
4616 switch (cond_type) {
4617 default:
4618 cond_inverted = MaterializeIntLongCompare(if_cond,
4619 /* is64bit */ false,
4620 cond_locations,
4621 cond_reg);
4622 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004623 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004624 cond_inverted = MaterializeIntLongCompare(if_cond,
4625 /* is64bit */ true,
4626 cond_locations,
4627 cond_reg);
4628 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004629 case DataType::Type::kFloat32:
4630 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004631 cond_inverted = MaterializeFpCompare(if_cond,
4632 condition->IsGtBias(),
4633 cond_type,
4634 cond_locations,
4635 fcond_reg);
4636 break;
4637 }
4638 }
4639
4640 if (true_src.IsConstant()) {
4641 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4642 }
4643 if (false_src.IsConstant()) {
4644 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4645 }
4646
4647 switch (dst_type) {
4648 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004649 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004650 __ Mfc1(cond_reg, fcond_reg);
4651 }
4652 if (true_src.IsConstant()) {
4653 if (cond_inverted) {
4654 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4655 } else {
4656 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4657 }
4658 } else if (false_src.IsConstant()) {
4659 if (cond_inverted) {
4660 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4661 } else {
4662 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4663 }
4664 } else {
4665 DCHECK_NE(cond_reg, AT);
4666 if (cond_inverted) {
4667 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4668 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4669 } else {
4670 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4671 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4672 }
4673 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4674 }
4675 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004676 case DataType::Type::kFloat32: {
4677 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004678 // sel*.fmt tests bit 0 of the condition register, account for that.
4679 __ Sltu(TMP, ZERO, cond_reg);
4680 __ Mtc1(TMP, fcond_reg);
4681 }
4682 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4683 if (true_src.IsConstant()) {
4684 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4685 if (cond_inverted) {
4686 __ SelnezS(dst_reg, src_reg, fcond_reg);
4687 } else {
4688 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4689 }
4690 } else if (false_src.IsConstant()) {
4691 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4692 if (cond_inverted) {
4693 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4694 } else {
4695 __ SelnezS(dst_reg, src_reg, fcond_reg);
4696 }
4697 } else {
4698 if (cond_inverted) {
4699 __ SelS(fcond_reg,
4700 true_src.AsFpuRegister<FpuRegister>(),
4701 false_src.AsFpuRegister<FpuRegister>());
4702 } else {
4703 __ SelS(fcond_reg,
4704 false_src.AsFpuRegister<FpuRegister>(),
4705 true_src.AsFpuRegister<FpuRegister>());
4706 }
4707 __ MovS(dst_reg, fcond_reg);
4708 }
4709 break;
4710 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004711 case DataType::Type::kFloat64: {
4712 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004713 // sel*.fmt tests bit 0 of the condition register, account for that.
4714 __ Sltu(TMP, ZERO, cond_reg);
4715 __ Mtc1(TMP, fcond_reg);
4716 }
4717 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4718 if (true_src.IsConstant()) {
4719 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4720 if (cond_inverted) {
4721 __ SelnezD(dst_reg, src_reg, fcond_reg);
4722 } else {
4723 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4724 }
4725 } else if (false_src.IsConstant()) {
4726 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4727 if (cond_inverted) {
4728 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4729 } else {
4730 __ SelnezD(dst_reg, src_reg, fcond_reg);
4731 }
4732 } else {
4733 if (cond_inverted) {
4734 __ SelD(fcond_reg,
4735 true_src.AsFpuRegister<FpuRegister>(),
4736 false_src.AsFpuRegister<FpuRegister>());
4737 } else {
4738 __ SelD(fcond_reg,
4739 false_src.AsFpuRegister<FpuRegister>(),
4740 true_src.AsFpuRegister<FpuRegister>());
4741 }
4742 __ MovD(dst_reg, fcond_reg);
4743 }
4744 break;
4745 }
4746 }
4747}
4748
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004749void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004750 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004751 LocationSummary(flag, LocationSummary::kNoCall);
4752 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004753}
4754
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004755void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4756 __ LoadFromOffset(kLoadWord,
4757 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4758 SP,
4759 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004760}
4761
David Brazdil74eb1b22015-12-14 11:44:01 +00004762void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004763 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004764 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004765}
4766
4767void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004768 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4769 GenConditionalMove(select);
4770 } else {
4771 LocationSummary* locations = select->GetLocations();
4772 Mips64Label false_target;
4773 GenerateTestAndBranch(select,
4774 /* condition_input_index */ 2,
4775 /* true_target */ nullptr,
4776 &false_target);
4777 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4778 __ Bind(&false_target);
4779 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004780}
4781
David Srbecky0cf44932015-12-09 14:09:59 +00004782void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004783 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004784}
4785
David Srbeckyd28f4a02016-03-14 17:14:24 +00004786void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4787 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004788}
4789
4790void CodeGeneratorMIPS64::GenerateNop() {
4791 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004792}
4793
Alexey Frunze4dda3372015-06-01 18:31:49 -07004794void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004795 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004797 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004799 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004800 instruction,
4801 object_field_get_with_read_barrier
4802 ? LocationSummary::kCallOnSlowPath
4803 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004804 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4805 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4806 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004807 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004808 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004809 locations->SetOut(Location::RequiresFpuRegister());
4810 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004811 // The output overlaps in the case of an object field get with
4812 // read barriers enabled: we do not want the move to overwrite the
4813 // object's location, as we need it to emit the read barrier.
4814 locations->SetOut(Location::RequiresRegister(),
4815 object_field_get_with_read_barrier
4816 ? Location::kOutputOverlap
4817 : Location::kNoOutputOverlap);
4818 }
4819 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4820 // We need a temporary register for the read barrier marking slow
4821 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004822 if (!kBakerReadBarrierThunksEnableForFields) {
4823 locations->AddTemp(Location::RequiresRegister());
4824 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004825 }
4826}
4827
4828void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4829 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004830 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4831 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004832 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004833 Location obj_loc = locations->InAt(0);
4834 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4835 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004836 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004837 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004838 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004839 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4840
Alexey Frunze4dda3372015-06-01 18:31:49 -07004841 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004842 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004843 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004844 load_type = kLoadUnsignedByte;
4845 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004846 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004847 load_type = kLoadSignedByte;
4848 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004849 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004850 load_type = kLoadUnsignedHalfword;
4851 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004852 case DataType::Type::kInt16:
4853 load_type = kLoadSignedHalfword;
4854 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004855 case DataType::Type::kInt32:
4856 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004857 load_type = kLoadWord;
4858 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004859 case DataType::Type::kInt64:
4860 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004861 load_type = kLoadDoubleword;
4862 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004863 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004864 load_type = kLoadUnsignedWord;
4865 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004866 case DataType::Type::kUint32:
4867 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004868 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004869 LOG(FATAL) << "Unreachable type " << type;
4870 UNREACHABLE();
4871 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004872 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004873 DCHECK(dst_loc.IsRegister());
4874 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004875 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004876 // /* HeapReference<Object> */ dst = *(obj + offset)
4877 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004878 Location temp_loc =
4879 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004880 // Note that a potential implicit null check is handled in this
4881 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4882 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4883 dst_loc,
4884 obj,
4885 offset,
4886 temp_loc,
4887 /* needs_null_check */ true);
4888 if (is_volatile) {
4889 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4890 }
4891 } else {
4892 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4893 if (is_volatile) {
4894 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4895 }
4896 // If read barriers are enabled, emit read barriers other than
4897 // Baker's using a slow path (and also unpoison the loaded
4898 // reference, if heap poisoning is enabled).
4899 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4900 }
4901 } else {
4902 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4903 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004904 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004905 DCHECK(dst_loc.IsFpuRegister());
4906 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004907 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004908 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004909
Alexey Frunze15958152017-02-09 19:08:30 -08004910 // Memory barriers, in the case of references, are handled in the
4911 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004912 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004913 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004914 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004915}
4916
4917void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4918 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4919 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004920 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004921 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004922 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004923 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004924 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004925 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004926 }
4927}
4928
4929void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004930 const FieldInfo& field_info,
4931 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004932 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004933 LocationSummary* locations = instruction->GetLocations();
4934 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004935 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004936 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004937 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004938 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4939 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004940 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4941
Alexey Frunze4dda3372015-06-01 18:31:49 -07004942 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004943 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004944 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004946 store_type = kStoreByte;
4947 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004948 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004949 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004950 store_type = kStoreHalfword;
4951 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004952 case DataType::Type::kInt32:
4953 case DataType::Type::kFloat32:
4954 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004955 store_type = kStoreWord;
4956 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004957 case DataType::Type::kInt64:
4958 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004959 store_type = kStoreDoubleword;
4960 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004961 case DataType::Type::kUint32:
4962 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004963 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004964 LOG(FATAL) << "Unreachable type " << type;
4965 UNREACHABLE();
4966 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004967
Alexey Frunze15958152017-02-09 19:08:30 -08004968 if (is_volatile) {
4969 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4970 }
4971
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004972 if (value_location.IsConstant()) {
4973 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4974 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4975 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004977 DCHECK(value_location.IsRegister());
4978 GpuRegister src = value_location.AsRegister<GpuRegister>();
4979 if (kPoisonHeapReferences && needs_write_barrier) {
4980 // Note that in the case where `value` is a null reference,
4981 // we do not enter this block, as a null reference does not
4982 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004983 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004984 __ PoisonHeapReference(TMP, src);
4985 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4986 } else {
4987 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4988 }
4989 } else {
4990 DCHECK(value_location.IsFpuRegister());
4991 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4992 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4993 }
4994 }
Alexey Frunze15958152017-02-09 19:08:30 -08004995
Alexey Frunzec061de12017-02-14 13:27:23 -08004996 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004997 DCHECK(value_location.IsRegister());
4998 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004999 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005000 }
Alexey Frunze15958152017-02-09 19:08:30 -08005001
5002 if (is_volatile) {
5003 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5004 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005005}
5006
5007void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5008 HandleFieldGet(instruction, instruction->GetFieldInfo());
5009}
5010
5011void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5012 HandleFieldGet(instruction, instruction->GetFieldInfo());
5013}
5014
5015void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5016 HandleFieldSet(instruction, instruction->GetFieldInfo());
5017}
5018
5019void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005020 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005021}
5022
Alexey Frunze15958152017-02-09 19:08:30 -08005023void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
5024 HInstruction* instruction,
5025 Location out,
5026 uint32_t offset,
5027 Location maybe_temp,
5028 ReadBarrierOption read_barrier_option) {
5029 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5030 if (read_barrier_option == kWithReadBarrier) {
5031 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005032 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
5033 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5034 }
Alexey Frunze15958152017-02-09 19:08:30 -08005035 if (kUseBakerReadBarrier) {
5036 // Load with fast path based Baker's read barrier.
5037 // /* HeapReference<Object> */ out = *(out + offset)
5038 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5039 out,
5040 out_reg,
5041 offset,
5042 maybe_temp,
5043 /* needs_null_check */ false);
5044 } else {
5045 // Load with slow path based read barrier.
5046 // Save the value of `out` into `maybe_temp` before overwriting it
5047 // in the following move operation, as we will need it for the
5048 // read barrier below.
5049 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
5050 // /* HeapReference<Object> */ out = *(out + offset)
5051 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5052 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5053 }
5054 } else {
5055 // Plain load with no read barrier.
5056 // /* HeapReference<Object> */ out = *(out + offset)
5057 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5058 __ MaybeUnpoisonHeapReference(out_reg);
5059 }
5060}
5061
5062void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
5063 HInstruction* instruction,
5064 Location out,
5065 Location obj,
5066 uint32_t offset,
5067 Location maybe_temp,
5068 ReadBarrierOption read_barrier_option) {
5069 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5070 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
5071 if (read_barrier_option == kWithReadBarrier) {
5072 CHECK(kEmitCompilerReadBarrier);
5073 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005074 if (!kBakerReadBarrierThunksEnableForFields) {
5075 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5076 }
Alexey Frunze15958152017-02-09 19:08:30 -08005077 // Load with fast path based Baker's read barrier.
5078 // /* HeapReference<Object> */ out = *(obj + offset)
5079 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5080 out,
5081 obj_reg,
5082 offset,
5083 maybe_temp,
5084 /* needs_null_check */ false);
5085 } else {
5086 // Load with slow path based read barrier.
5087 // /* HeapReference<Object> */ out = *(obj + offset)
5088 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5089 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5090 }
5091 } else {
5092 // Plain load with no read barrier.
5093 // /* HeapReference<Object> */ out = *(obj + offset)
5094 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5095 __ MaybeUnpoisonHeapReference(out_reg);
5096 }
5097}
5098
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005099static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5100 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5101 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5102 return reg - V0;
5103 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5104 return 13 + (reg - S2);
5105 } else if (reg == S8) { // One more.
5106 return 19;
5107 }
5108 LOG(FATAL) << "Unexpected register " << reg;
5109 UNREACHABLE();
5110}
5111
5112static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5113 int num = GetBakerMarkThunkNumber(reg) +
5114 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5115 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5116}
5117
5118static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5119 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5120 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5121}
5122
5123void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5124 Location root,
5125 GpuRegister obj,
5126 uint32_t offset,
5127 ReadBarrierOption read_barrier_option,
5128 Mips64Label* label_low) {
5129 if (label_low != nullptr) {
5130 DCHECK_EQ(offset, 0x5678u);
5131 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005132 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005133 if (read_barrier_option == kWithReadBarrier) {
5134 DCHECK(kEmitCompilerReadBarrier);
5135 if (kUseBakerReadBarrier) {
5136 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5137 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005138 if (kBakerReadBarrierThunksEnableForGcRoots) {
5139 // Note that we do not actually check the value of `GetIsGcMarking()`
5140 // to decide whether to mark the loaded GC root or not. Instead, we
5141 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5142 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5143 // vice versa.
5144 //
5145 // We use thunks for the slow path. That thunk checks the reference
5146 // and jumps to the entrypoint if needed.
5147 //
5148 // temp = Thread::Current()->pReadBarrierMarkReg00
5149 // // AKA &art_quick_read_barrier_mark_introspection.
5150 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5151 // if (temp != nullptr) {
5152 // temp = &gc_root_thunk<root_reg>
5153 // root = temp(root)
5154 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005155
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005156 const int32_t entry_point_offset =
5157 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5158 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5159 int16_t offset_low = Low16Bits(offset);
5160 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5161 // extension in lwu.
5162 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5163 GpuRegister base = short_offset ? obj : TMP;
5164 // Loading the entrypoint does not require a load acquire since it is only changed when
5165 // threads are suspended or running a checkpoint.
5166 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5167 if (!short_offset) {
5168 DCHECK(!label_low);
5169 __ Daui(base, obj, offset_high);
5170 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005171 Mips64Label skip_call;
5172 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005173 if (label_low != nullptr) {
5174 DCHECK(short_offset);
5175 __ Bind(label_low);
5176 }
5177 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5178 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5179 // in delay slot.
5180 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005181 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005182 } else {
5183 // Note that we do not actually check the value of `GetIsGcMarking()`
5184 // to decide whether to mark the loaded GC root or not. Instead, we
5185 // load into `temp` (T9) the read barrier mark entry point corresponding
5186 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5187 // is false, and vice versa.
5188 //
5189 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5190 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5191 // if (temp != null) {
5192 // root = temp(root)
5193 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005194
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005195 if (label_low != nullptr) {
5196 __ Bind(label_low);
5197 }
5198 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5199 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5200 static_assert(
5201 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5202 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5203 "have different sizes.");
5204 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5205 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5206 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005207
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005208 // Slow path marking the GC root `root`.
5209 Location temp = Location::RegisterLocation(T9);
5210 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005211 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005212 instruction,
5213 root,
5214 /*entrypoint*/ temp);
5215 codegen_->AddSlowPath(slow_path);
5216
5217 const int32_t entry_point_offset =
5218 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5219 // Loading the entrypoint does not require a load acquire since it is only changed when
5220 // threads are suspended or running a checkpoint.
5221 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5222 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5223 __ Bind(slow_path->GetExitLabel());
5224 }
Alexey Frunze15958152017-02-09 19:08:30 -08005225 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005226 if (label_low != nullptr) {
5227 __ Bind(label_low);
5228 }
Alexey Frunze15958152017-02-09 19:08:30 -08005229 // GC root loaded through a slow path for read barriers other
5230 // than Baker's.
5231 // /* GcRoot<mirror::Object>* */ root = obj + offset
5232 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5233 // /* mirror::Object* */ root = root->Read()
5234 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5235 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005236 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005237 if (label_low != nullptr) {
5238 __ Bind(label_low);
5239 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005240 // Plain GC root load with no read barrier.
5241 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5242 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5243 // Note that GC roots are not affected by heap poisoning, thus we
5244 // do not have to unpoison `root_reg` here.
5245 }
5246}
5247
Alexey Frunze15958152017-02-09 19:08:30 -08005248void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5249 Location ref,
5250 GpuRegister obj,
5251 uint32_t offset,
5252 Location temp,
5253 bool needs_null_check) {
5254 DCHECK(kEmitCompilerReadBarrier);
5255 DCHECK(kUseBakerReadBarrier);
5256
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005257 if (kBakerReadBarrierThunksEnableForFields) {
5258 // Note that we do not actually check the value of `GetIsGcMarking()`
5259 // to decide whether to mark the loaded reference or not. Instead, we
5260 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5261 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5262 // vice versa.
5263 //
5264 // We use thunks for the slow path. That thunk checks the reference
5265 // and jumps to the entrypoint if needed. If the holder is not gray,
5266 // it issues a load-load memory barrier and returns to the original
5267 // reference load.
5268 //
5269 // temp = Thread::Current()->pReadBarrierMarkReg00
5270 // // AKA &art_quick_read_barrier_mark_introspection.
5271 // if (temp != nullptr) {
5272 // temp = &field_array_thunk<holder_reg>
5273 // temp()
5274 // }
5275 // not_gray_return_address:
5276 // // If the offset is too large to fit into the lw instruction, we
5277 // // use an adjusted base register (TMP) here. This register
5278 // // receives bits 16 ... 31 of the offset before the thunk invocation
5279 // // and the thunk benefits from it.
5280 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5281 // gray_return_address:
5282
5283 DCHECK(temp.IsInvalid());
5284 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5285 const int32_t entry_point_offset =
5286 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5287 // There may have or may have not been a null check if the field offset is smaller than
5288 // the page size.
5289 // There must've been a null check in case it's actually a load from an array.
5290 // We will, however, perform an explicit null check in the thunk as it's easier to
5291 // do it than not.
5292 if (instruction->IsArrayGet()) {
5293 DCHECK(!needs_null_check);
5294 }
5295 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5296 // Loading the entrypoint does not require a load acquire since it is only changed when
5297 // threads are suspended or running a checkpoint.
5298 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5299 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005300 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005301 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005302 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005303 __ Nop(); // In forbidden slot.
5304 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005305 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005306 // /* HeapReference<Object> */ ref = *(obj + offset)
5307 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5308 } else {
5309 int16_t offset_low = Low16Bits(offset);
5310 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005311 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005312 __ Daui(TMP, obj, offset_high); // In delay slot.
5313 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005314 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005315 // /* HeapReference<Object> */ ref = *(obj + offset)
5316 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5317 }
5318 if (needs_null_check) {
5319 MaybeRecordImplicitNullCheck(instruction);
5320 }
5321 __ MaybeUnpoisonHeapReference(ref_reg);
5322 return;
5323 }
5324
Alexey Frunze15958152017-02-09 19:08:30 -08005325 // /* HeapReference<Object> */ ref = *(obj + offset)
5326 Location no_index = Location::NoLocation();
5327 ScaleFactor no_scale_factor = TIMES_1;
5328 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5329 ref,
5330 obj,
5331 offset,
5332 no_index,
5333 no_scale_factor,
5334 temp,
5335 needs_null_check);
5336}
5337
5338void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5339 Location ref,
5340 GpuRegister obj,
5341 uint32_t data_offset,
5342 Location index,
5343 Location temp,
5344 bool needs_null_check) {
5345 DCHECK(kEmitCompilerReadBarrier);
5346 DCHECK(kUseBakerReadBarrier);
5347
5348 static_assert(
5349 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5350 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005351 ScaleFactor scale_factor = TIMES_4;
5352
5353 if (kBakerReadBarrierThunksEnableForArrays) {
5354 // Note that we do not actually check the value of `GetIsGcMarking()`
5355 // to decide whether to mark the loaded reference or not. Instead, we
5356 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5357 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5358 // vice versa.
5359 //
5360 // We use thunks for the slow path. That thunk checks the reference
5361 // and jumps to the entrypoint if needed. If the holder is not gray,
5362 // it issues a load-load memory barrier and returns to the original
5363 // reference load.
5364 //
5365 // temp = Thread::Current()->pReadBarrierMarkReg00
5366 // // AKA &art_quick_read_barrier_mark_introspection.
5367 // if (temp != nullptr) {
5368 // temp = &field_array_thunk<holder_reg>
5369 // temp()
5370 // }
5371 // not_gray_return_address:
5372 // // The element address is pre-calculated in the TMP register before the
5373 // // thunk invocation and the thunk benefits from it.
5374 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5375 // gray_return_address:
5376
5377 DCHECK(temp.IsInvalid());
5378 DCHECK(index.IsValid());
5379 const int32_t entry_point_offset =
5380 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5381 // We will not do the explicit null check in the thunk as some form of a null check
5382 // must've been done earlier.
5383 DCHECK(!needs_null_check);
5384 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5385 // Loading the entrypoint does not require a load acquire since it is only changed when
5386 // threads are suspended or running a checkpoint.
5387 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005388 Mips64Label skip_call;
5389 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005390 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5391 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5392 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5393 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005394 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005395 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5396 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5397 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5398 __ MaybeUnpoisonHeapReference(ref_reg);
5399 return;
5400 }
5401
Alexey Frunze15958152017-02-09 19:08:30 -08005402 // /* HeapReference<Object> */ ref =
5403 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005404 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5405 ref,
5406 obj,
5407 data_offset,
5408 index,
5409 scale_factor,
5410 temp,
5411 needs_null_check);
5412}
5413
5414void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5415 Location ref,
5416 GpuRegister obj,
5417 uint32_t offset,
5418 Location index,
5419 ScaleFactor scale_factor,
5420 Location temp,
5421 bool needs_null_check,
5422 bool always_update_field) {
5423 DCHECK(kEmitCompilerReadBarrier);
5424 DCHECK(kUseBakerReadBarrier);
5425
5426 // In slow path based read barriers, the read barrier call is
5427 // inserted after the original load. However, in fast path based
5428 // Baker's read barriers, we need to perform the load of
5429 // mirror::Object::monitor_ *before* the original reference load.
5430 // This load-load ordering is required by the read barrier.
5431 // The fast path/slow path (for Baker's algorithm) should look like:
5432 //
5433 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5434 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5435 // HeapReference<Object> ref = *src; // Original reference load.
5436 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5437 // if (is_gray) {
5438 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5439 // }
5440 //
5441 // Note: the original implementation in ReadBarrier::Barrier is
5442 // slightly more complex as it performs additional checks that we do
5443 // not do here for performance reasons.
5444
5445 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5446 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5447 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5448
5449 // /* int32_t */ monitor = obj->monitor_
5450 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5451 if (needs_null_check) {
5452 MaybeRecordImplicitNullCheck(instruction);
5453 }
5454 // /* LockWord */ lock_word = LockWord(monitor)
5455 static_assert(sizeof(LockWord) == sizeof(int32_t),
5456 "art::LockWord and int32_t have different sizes.");
5457
5458 __ Sync(0); // Barrier to prevent load-load reordering.
5459
5460 // The actual reference load.
5461 if (index.IsValid()) {
5462 // Load types involving an "index": ArrayGet,
5463 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5464 // intrinsics.
5465 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5466 if (index.IsConstant()) {
5467 size_t computed_offset =
5468 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5469 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5470 } else {
5471 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005472 if (scale_factor == TIMES_1) {
5473 __ Daddu(TMP, index_reg, obj);
5474 } else {
5475 __ Dlsa(TMP, index_reg, obj, scale_factor);
5476 }
Alexey Frunze15958152017-02-09 19:08:30 -08005477 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5478 }
5479 } else {
5480 // /* HeapReference<Object> */ ref = *(obj + offset)
5481 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5482 }
5483
5484 // Object* ref = ref_addr->AsMirrorPtr()
5485 __ MaybeUnpoisonHeapReference(ref_reg);
5486
5487 // Slow path marking the object `ref` when it is gray.
5488 SlowPathCodeMIPS64* slow_path;
5489 if (always_update_field) {
5490 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5491 // of the form `obj + field_offset`, where `obj` is a register and
5492 // `field_offset` is a register. Thus `offset` and `scale_factor`
5493 // above are expected to be null in this code path.
5494 DCHECK_EQ(offset, 0u);
5495 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005496 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005497 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5498 ref,
5499 obj,
5500 /* field_offset */ index,
5501 temp_reg);
5502 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005503 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005504 }
5505 AddSlowPath(slow_path);
5506
5507 // if (rb_state == ReadBarrier::GrayState())
5508 // ref = ReadBarrier::Mark(ref);
5509 // Given the numeric representation, it's enough to check the low bit of the
5510 // rb_state. We do that by shifting the bit into the sign bit (31) and
5511 // performing a branch on less than zero.
5512 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5513 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5514 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5515 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5516 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5517 __ Bind(slow_path->GetExitLabel());
5518}
5519
5520void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5521 Location out,
5522 Location ref,
5523 Location obj,
5524 uint32_t offset,
5525 Location index) {
5526 DCHECK(kEmitCompilerReadBarrier);
5527
5528 // Insert a slow path based read barrier *after* the reference load.
5529 //
5530 // If heap poisoning is enabled, the unpoisoning of the loaded
5531 // reference will be carried out by the runtime within the slow
5532 // path.
5533 //
5534 // Note that `ref` currently does not get unpoisoned (when heap
5535 // poisoning is enabled), which is alright as the `ref` argument is
5536 // not used by the artReadBarrierSlow entry point.
5537 //
5538 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005539 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005540 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5541 AddSlowPath(slow_path);
5542
5543 __ Bc(slow_path->GetEntryLabel());
5544 __ Bind(slow_path->GetExitLabel());
5545}
5546
5547void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5548 Location out,
5549 Location ref,
5550 Location obj,
5551 uint32_t offset,
5552 Location index) {
5553 if (kEmitCompilerReadBarrier) {
5554 // Baker's read barriers shall be handled by the fast path
5555 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5556 DCHECK(!kUseBakerReadBarrier);
5557 // If heap poisoning is enabled, unpoisoning will be taken care of
5558 // by the runtime within the slow path.
5559 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5560 } else if (kPoisonHeapReferences) {
5561 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5562 }
5563}
5564
5565void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5566 Location out,
5567 Location root) {
5568 DCHECK(kEmitCompilerReadBarrier);
5569
5570 // Insert a slow path based read barrier *after* the GC root load.
5571 //
5572 // Note that GC roots are not affected by heap poisoning, so we do
5573 // not need to do anything special for this here.
5574 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005575 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005576 AddSlowPath(slow_path);
5577
5578 __ Bc(slow_path->GetEntryLabel());
5579 __ Bind(slow_path->GetExitLabel());
5580}
5581
Alexey Frunze4dda3372015-06-01 18:31:49 -07005582void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005583 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5584 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005585 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005586 switch (type_check_kind) {
5587 case TypeCheckKind::kExactCheck:
5588 case TypeCheckKind::kAbstractClassCheck:
5589 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005590 case TypeCheckKind::kArrayObjectCheck: {
5591 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5592 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5593 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005594 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005595 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005596 case TypeCheckKind::kArrayCheck:
5597 case TypeCheckKind::kUnresolvedCheck:
5598 case TypeCheckKind::kInterfaceCheck:
5599 call_kind = LocationSummary::kCallOnSlowPath;
5600 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00005601 case TypeCheckKind::kBitstringCheck:
5602 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005603 }
5604
Vladimir Markoca6fff82017-10-03 14:49:14 +01005605 LocationSummary* locations =
5606 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005607 if (baker_read_barrier_slow_path) {
5608 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5609 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005610 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00005611 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
5612 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
5613 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
5614 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
5615 } else {
5616 locations->SetInAt(1, Location::RequiresRegister());
5617 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005618 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005619 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005620 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005621 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005622}
5623
5624void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005625 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005626 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005627 Location obj_loc = locations->InAt(0);
5628 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00005629 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08005630 Location out_loc = locations->Out();
5631 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5632 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5633 DCHECK_LE(num_temps, 1u);
5634 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005635 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5636 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5637 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5638 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005639 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005640 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005641
5642 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005643 // Avoid this check if we know `obj` is not null.
5644 if (instruction->MustDoNullCheck()) {
5645 __ Move(out, ZERO);
5646 __ Beqzc(obj, &done);
5647 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005648
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005649 switch (type_check_kind) {
5650 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005651 ReadBarrierOption read_barrier_option =
5652 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005653 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005654 GenerateReferenceLoadTwoRegisters(instruction,
5655 out_loc,
5656 obj_loc,
5657 class_offset,
5658 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005659 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005660 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00005661 __ Xor(out, out, cls.AsRegister<GpuRegister>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005662 __ Sltiu(out, out, 1);
5663 break;
5664 }
5665
5666 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005667 ReadBarrierOption read_barrier_option =
5668 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005669 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005670 GenerateReferenceLoadTwoRegisters(instruction,
5671 out_loc,
5672 obj_loc,
5673 class_offset,
5674 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005675 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005676 // If the class is abstract, we eagerly fetch the super class of the
5677 // object to avoid doing a comparison we know will fail.
5678 Mips64Label loop;
5679 __ Bind(&loop);
5680 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005681 GenerateReferenceLoadOneRegister(instruction,
5682 out_loc,
5683 super_offset,
5684 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005685 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005686 // If `out` is null, we use it for the result, and jump to `done`.
5687 __ Beqzc(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00005688 __ Bnec(out, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005689 __ LoadConst32(out, 1);
5690 break;
5691 }
5692
5693 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005694 ReadBarrierOption read_barrier_option =
5695 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005696 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005697 GenerateReferenceLoadTwoRegisters(instruction,
5698 out_loc,
5699 obj_loc,
5700 class_offset,
5701 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005702 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005703 // Walk over the class hierarchy to find a match.
5704 Mips64Label loop, success;
5705 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00005706 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005707 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005708 GenerateReferenceLoadOneRegister(instruction,
5709 out_loc,
5710 super_offset,
5711 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005712 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005713 __ Bnezc(out, &loop);
5714 // If `out` is null, we use it for the result, and jump to `done`.
5715 __ Bc(&done);
5716 __ Bind(&success);
5717 __ LoadConst32(out, 1);
5718 break;
5719 }
5720
5721 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005722 ReadBarrierOption read_barrier_option =
5723 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005724 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005725 GenerateReferenceLoadTwoRegisters(instruction,
5726 out_loc,
5727 obj_loc,
5728 class_offset,
5729 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005730 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005731 // Do an exact check.
5732 Mips64Label success;
Vladimir Marko175e7862018-03-27 09:03:13 +00005733 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005734 // Otherwise, we need to check that the object's class is a non-primitive array.
5735 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005736 GenerateReferenceLoadOneRegister(instruction,
5737 out_loc,
5738 component_offset,
5739 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005740 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005741 // If `out` is null, we use it for the result, and jump to `done`.
5742 __ Beqzc(out, &done);
5743 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5744 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5745 __ Sltiu(out, out, 1);
5746 __ Bc(&done);
5747 __ Bind(&success);
5748 __ LoadConst32(out, 1);
5749 break;
5750 }
5751
5752 case TypeCheckKind::kArrayCheck: {
5753 // No read barrier since the slow path will retry upon failure.
5754 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005755 GenerateReferenceLoadTwoRegisters(instruction,
5756 out_loc,
5757 obj_loc,
5758 class_offset,
5759 maybe_temp_loc,
5760 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005761 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005762 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5763 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005764 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00005765 __ Bnec(out, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005766 __ LoadConst32(out, 1);
5767 break;
5768 }
5769
5770 case TypeCheckKind::kUnresolvedCheck:
5771 case TypeCheckKind::kInterfaceCheck: {
5772 // Note that we indeed only call on slow path, but we always go
5773 // into the slow path for the unresolved and interface check
5774 // cases.
5775 //
5776 // We cannot directly call the InstanceofNonTrivial runtime
5777 // entry point without resorting to a type checking slow path
5778 // here (i.e. by calling InvokeRuntime directly), as it would
5779 // require to assign fixed registers for the inputs of this
5780 // HInstanceOf instruction (following the runtime calling
5781 // convention), which might be cluttered by the potential first
5782 // read barrier emission at the beginning of this method.
5783 //
5784 // TODO: Introduce a new runtime entry point taking the object
5785 // to test (instead of its class) as argument, and let it deal
5786 // with the read barrier issues. This will let us refactor this
5787 // case of the `switch` code as it was previously (with a direct
5788 // call to the runtime not using a type checking slow path).
5789 // This should also be beneficial for the other cases above.
5790 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005791 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5792 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005793 codegen_->AddSlowPath(slow_path);
5794 __ Bc(slow_path->GetEntryLabel());
5795 break;
5796 }
Vladimir Marko175e7862018-03-27 09:03:13 +00005797
5798 case TypeCheckKind::kBitstringCheck: {
5799 // /* HeapReference<Class> */ temp = obj->klass_
5800 GenerateReferenceLoadTwoRegisters(instruction,
5801 out_loc,
5802 obj_loc,
5803 class_offset,
5804 maybe_temp_loc,
5805 kWithoutReadBarrier);
5806
5807 GenerateBitstringTypeCheckCompare(instruction, out);
5808 __ Sltiu(out, out, 1);
5809 break;
5810 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005811 }
5812
5813 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005814
5815 if (slow_path != nullptr) {
5816 __ Bind(slow_path->GetExitLabel());
5817 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005818}
5819
5820void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005821 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005822 locations->SetOut(Location::ConstantLocation(constant));
5823}
5824
5825void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5826 // Will be generated at use site.
5827}
5828
5829void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005830 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005831 locations->SetOut(Location::ConstantLocation(constant));
5832}
5833
5834void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5835 // Will be generated at use site.
5836}
5837
Calin Juravle175dc732015-08-25 15:42:32 +01005838void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5839 // The trampoline uses the same calling convention as dex calling conventions,
5840 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5841 // the method_idx.
5842 HandleInvoke(invoke);
5843}
5844
5845void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5846 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5847}
5848
Alexey Frunze4dda3372015-06-01 18:31:49 -07005849void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5850 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5851 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5852}
5853
5854void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5855 HandleInvoke(invoke);
5856 // The register T0 is required to be used for the hidden argument in
5857 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5858 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5859}
5860
5861void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5862 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5863 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005864 Location receiver = invoke->GetLocations()->InAt(0);
5865 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005866 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005867
5868 // Set the hidden argument.
5869 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5870 invoke->GetDexMethodIndex());
5871
5872 // temp = object->GetClass();
5873 if (receiver.IsStackSlot()) {
5874 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5875 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5876 } else {
5877 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5878 }
5879 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005880 // Instead of simply (possibly) unpoisoning `temp` here, we should
5881 // emit a read barrier for the previous class reference load.
5882 // However this is not required in practice, as this is an
5883 // intermediate/temporary reference and because the current
5884 // concurrent copying collector keeps the from-space memory
5885 // intact/accessible until the end of the marking phase (the
5886 // concurrent copying collector may not in the future).
5887 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005888 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5889 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5890 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005891 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005892 // temp = temp->GetImtEntryAt(method_offset);
5893 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5894 // T9 = temp->GetEntryPoint();
5895 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5896 // T9();
5897 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005898 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005899 DCHECK(!codegen_->IsLeafMethod());
5900 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5901}
5902
5903void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005904 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5905 if (intrinsic.TryDispatch(invoke)) {
5906 return;
5907 }
5908
Alexey Frunze4dda3372015-06-01 18:31:49 -07005909 HandleInvoke(invoke);
5910}
5911
5912void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005913 // Explicit clinit checks triggered by static invokes must have been pruned by
5914 // art::PrepareForRegisterAllocation.
5915 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005916
Chris Larsen3039e382015-08-26 07:54:08 -07005917 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5918 if (intrinsic.TryDispatch(invoke)) {
5919 return;
5920 }
5921
Alexey Frunze4dda3372015-06-01 18:31:49 -07005922 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005923}
5924
Orion Hodsonac141392017-01-13 11:53:47 +00005925void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5926 HandleInvoke(invoke);
5927}
5928
5929void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5930 codegen_->GenerateInvokePolymorphicCall(invoke);
5931}
5932
Orion Hodson4c8e12e2018-05-18 08:33:20 +01005933void LocationsBuilderMIPS64::VisitInvokeCustom(HInvokeCustom* invoke) {
5934 HandleInvoke(invoke);
5935}
5936
5937void InstructionCodeGeneratorMIPS64::VisitInvokeCustom(HInvokeCustom* invoke) {
5938 codegen_->GenerateInvokeCustomCall(invoke);
5939}
5940
Chris Larsen3039e382015-08-26 07:54:08 -07005941static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005942 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005943 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5944 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005945 return true;
5946 }
5947 return false;
5948}
5949
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005950HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005951 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005952 bool fallback_load = false;
5953 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005954 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005955 case HLoadString::LoadKind::kBootImageRelRo:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005956 case HLoadString::LoadKind::kBssEntry:
5957 DCHECK(!Runtime::Current()->UseJitCompilation());
5958 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005959 case HLoadString::LoadKind::kJitTableAddress:
5960 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005961 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005962 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005963 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005964 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005965 }
5966 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005967 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005968 }
5969 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005970}
5971
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005972HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5973 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005974 bool fallback_load = false;
5975 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005976 case HLoadClass::LoadKind::kInvalid:
5977 LOG(FATAL) << "UNREACHABLE";
5978 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005979 case HLoadClass::LoadKind::kReferrersClass:
5980 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005981 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005982 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005983 case HLoadClass::LoadKind::kBssEntry:
5984 DCHECK(!Runtime::Current()->UseJitCompilation());
5985 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005986 case HLoadClass::LoadKind::kJitTableAddress:
5987 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005988 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005989 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005990 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005991 break;
5992 }
5993 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005994 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005995 }
5996 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005997}
5998
Vladimir Markodc151b22015-10-15 18:02:30 +01005999HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
6000 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006001 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08006002 // On MIPS64 we support all dispatch types.
6003 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006004}
6005
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006006void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
6007 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006008 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00006009 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08006010 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
6011 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
6012
Alexey Frunze19f6c692016-11-30 19:19:55 -08006013 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006014 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00006015 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006016 uint32_t offset =
6017 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00006018 __ LoadFromOffset(kLoadDoubleword,
6019 temp.AsRegister<GpuRegister>(),
6020 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006021 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00006022 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006023 }
Vladimir Marko58155012015-08-19 12:49:41 +00006024 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006025 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006026 break;
Vladimir Marko65979462017-05-19 17:25:12 +01006027 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
6028 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006029 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006030 NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006031 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006032 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006033 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01006034 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6035 break;
6036 }
Vladimir Marko58155012015-08-19 12:49:41 +00006037 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006038 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
6039 kLoadDoubleword,
6040 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00006041 break;
Vladimir Markob066d432018-01-03 13:14:37 +00006042 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006043 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00006044 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
6045 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
6046 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6047 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
6048 __ Lwu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6049 break;
6050 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006051 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006052 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006053 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006054 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
6055 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
6056 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08006057 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6058 break;
6059 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006060 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
6061 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
6062 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07006063 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006064 }
6065
Alexey Frunze19f6c692016-11-30 19:19:55 -08006066 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00006067 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006068 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00006069 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006070 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6071 // T9 = callee_method->entry_point_from_quick_compiled_code_;
6072 __ LoadFromOffset(kLoadDoubleword,
6073 T9,
6074 callee_method.AsRegister<GpuRegister>(),
6075 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006076 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006077 // T9()
6078 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006079 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00006080 break;
6081 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006082 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
6083
Alexey Frunze4dda3372015-06-01 18:31:49 -07006084 DCHECK(!IsLeafMethod());
6085}
6086
6087void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006088 // Explicit clinit checks triggered by static invokes must have been pruned by
6089 // art::PrepareForRegisterAllocation.
6090 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006091
6092 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6093 return;
6094 }
6095
6096 LocationSummary* locations = invoke->GetLocations();
6097 codegen_->GenerateStaticOrDirectCall(invoke,
6098 locations->HasTemps()
6099 ? locations->GetTemp(0)
6100 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006101}
6102
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006103void CodeGeneratorMIPS64::GenerateVirtualCall(
6104 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006105 // Use the calling convention instead of the location of the receiver, as
6106 // intrinsics may have put the receiver in a different register. In the intrinsics
6107 // slow path, the arguments have been moved to the right place, so here we are
6108 // guaranteed that the receiver is the first register of the calling convention.
6109 InvokeDexCallingConvention calling_convention;
6110 GpuRegister receiver = calling_convention.GetRegisterAt(0);
6111
Alexey Frunze53afca12015-11-05 16:34:23 -08006112 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006113 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6114 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
6115 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006116 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006117
6118 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006119 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08006120 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006121 // Instead of simply (possibly) unpoisoning `temp` here, we should
6122 // emit a read barrier for the previous class reference load.
6123 // However this is not required in practice, as this is an
6124 // intermediate/temporary reference and because the current
6125 // concurrent copying collector keeps the from-space memory
6126 // intact/accessible until the end of the marking phase (the
6127 // concurrent copying collector may not in the future).
6128 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006129 // temp = temp->GetMethodAt(method_offset);
6130 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6131 // T9 = temp->GetEntryPoint();
6132 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6133 // T9();
6134 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006135 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006136 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006137}
6138
6139void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6140 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6141 return;
6142 }
6143
6144 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006145 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006146}
6147
6148void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006149 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006150 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006151 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006152 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6153 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006154 return;
6155 }
Vladimir Marko41559982017-01-06 14:04:23 +00006156 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006157
Alexey Frunze15958152017-02-09 19:08:30 -08006158 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6159 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006160 ? LocationSummary::kCallOnSlowPath
6161 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006162 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006163 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6164 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6165 }
Vladimir Marko41559982017-01-06 14:04:23 +00006166 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006167 locations->SetInAt(0, Location::RequiresRegister());
6168 }
6169 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006170 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6171 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6172 // Rely on the type resolution or initialization and marking to save everything we need.
6173 RegisterSet caller_saves = RegisterSet::Empty();
6174 InvokeRuntimeCallingConvention calling_convention;
6175 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6176 locations->SetCustomSlowPathCallerSaves(caller_saves);
6177 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006178 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006179 }
6180 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006181}
6182
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006183// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6184// move.
6185void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006186 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006187 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006188 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006189 return;
6190 }
Vladimir Marko41559982017-01-06 14:04:23 +00006191 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006192
Vladimir Marko41559982017-01-06 14:04:23 +00006193 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006194 Location out_loc = locations->Out();
6195 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6196 GpuRegister current_method_reg = ZERO;
6197 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006198 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006199 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6200 }
6201
Alexey Frunze15958152017-02-09 19:08:30 -08006202 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6203 ? kWithoutReadBarrier
6204 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006205 bool generate_null_check = false;
6206 switch (load_kind) {
6207 case HLoadClass::LoadKind::kReferrersClass:
6208 DCHECK(!cls->CanCallRuntime());
6209 DCHECK(!cls->MustGenerateClinitCheck());
6210 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6211 GenerateGcRootFieldLoad(cls,
6212 out_loc,
6213 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006214 ArtMethod::DeclaringClassOffset().Int32Value(),
6215 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006216 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006217 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006218 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006219 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006220 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006221 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006222 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006223 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006224 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006225 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6226 break;
6227 }
6228 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006229 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006230 uint32_t address = dchecked_integral_cast<uint32_t>(
6231 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6232 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006233 __ LoadLiteral(out,
6234 kLoadUnsignedWord,
6235 codegen_->DeduplicateBootImageAddressLiteral(address));
6236 break;
6237 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006238 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006239 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006240 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006241 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006242 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006243 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006244 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006245 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6246 __ Lwu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006247 break;
6248 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006249 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006250 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6251 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006252 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6253 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006254 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006255 GenerateGcRootFieldLoad(cls,
6256 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006257 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006258 /* placeholder */ 0x5678,
6259 read_barrier_option,
6260 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006261 generate_null_check = true;
6262 break;
6263 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006264 case HLoadClass::LoadKind::kJitTableAddress:
6265 __ LoadLiteral(out,
6266 kLoadUnsignedWord,
6267 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6268 cls->GetTypeIndex(),
6269 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006270 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006271 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006272 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006273 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006274 LOG(FATAL) << "UNREACHABLE";
6275 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006276 }
6277
6278 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6279 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006280 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006281 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006282 codegen_->AddSlowPath(slow_path);
6283 if (generate_null_check) {
6284 __ Beqzc(out, slow_path->GetEntryLabel());
6285 }
6286 if (cls->MustGenerateClinitCheck()) {
6287 GenerateClassInitializationCheck(slow_path, out);
6288 } else {
6289 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006290 }
6291 }
6292}
6293
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006294void LocationsBuilderMIPS64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6295 InvokeRuntimeCallingConvention calling_convention;
6296 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6297 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
6298}
6299
6300void InstructionCodeGeneratorMIPS64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6301 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6302}
6303
Orion Hodson18259d72018-04-12 11:18:23 +01006304void LocationsBuilderMIPS64::VisitLoadMethodType(HLoadMethodType* load) {
6305 InvokeRuntimeCallingConvention calling_convention;
6306 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6307 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
6308}
6309
6310void InstructionCodeGeneratorMIPS64::VisitLoadMethodType(HLoadMethodType* load) {
6311 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6312}
6313
David Brazdilcb1c0552015-08-04 16:22:25 +01006314static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006315 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006316}
6317
Alexey Frunze4dda3372015-06-01 18:31:49 -07006318void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6319 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006320 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006321 locations->SetOut(Location::RequiresRegister());
6322}
6323
6324void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6325 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006326 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6327}
6328
6329void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006330 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006331}
6332
6333void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6334 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006335}
6336
Alexey Frunze4dda3372015-06-01 18:31:49 -07006337void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006338 HLoadString::LoadKind load_kind = load->GetLoadKind();
6339 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006340 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006341 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006342 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006343 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006344 } else {
6345 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006346 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6347 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6348 // Rely on the pResolveString and marking to save everything we need.
6349 RegisterSet caller_saves = RegisterSet::Empty();
6350 InvokeRuntimeCallingConvention calling_convention;
6351 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6352 locations->SetCustomSlowPathCallerSaves(caller_saves);
6353 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006354 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006355 }
6356 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006357 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006358}
6359
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006360// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6361// move.
6362void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006363 HLoadString::LoadKind load_kind = load->GetLoadKind();
6364 LocationSummary* locations = load->GetLocations();
6365 Location out_loc = locations->Out();
6366 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6367
6368 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006369 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6370 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006371 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006372 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006373 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006374 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006375 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006376 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006377 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006378 }
6379 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006380 uint32_t address = dchecked_integral_cast<uint32_t>(
6381 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6382 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006383 __ LoadLiteral(out,
6384 kLoadUnsignedWord,
6385 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006386 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006387 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006388 case HLoadString::LoadKind::kBootImageRelRo: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006389 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006390 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006391 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006392 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006393 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006394 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006395 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6396 __ Lwu(out, AT, /* placeholder */ 0x5678);
6397 return;
6398 }
6399 case HLoadString::LoadKind::kBssEntry: {
6400 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6401 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6402 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6403 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6404 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006405 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006406 GenerateGcRootFieldLoad(load,
6407 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006408 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006409 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006410 kCompilerReadBarrierOption,
6411 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006412 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006413 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006414 codegen_->AddSlowPath(slow_path);
6415 __ Beqzc(out, slow_path->GetEntryLabel());
6416 __ Bind(slow_path->GetExitLabel());
6417 return;
6418 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006419 case HLoadString::LoadKind::kJitTableAddress:
6420 __ LoadLiteral(out,
6421 kLoadUnsignedWord,
6422 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6423 load->GetStringIndex(),
6424 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006425 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006426 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006427 default:
6428 break;
6429 }
6430
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006431 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006432 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006433 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006434 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006435 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6436 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6437 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006438}
6439
Alexey Frunze4dda3372015-06-01 18:31:49 -07006440void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006441 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006442 locations->SetOut(Location::ConstantLocation(constant));
6443}
6444
6445void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6446 // Will be generated at use site.
6447}
6448
6449void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006450 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6451 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006452 InvokeRuntimeCallingConvention calling_convention;
6453 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6454}
6455
6456void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006457 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006458 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006459 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006460 if (instruction->IsEnter()) {
6461 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6462 } else {
6463 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6464 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006465}
6466
6467void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6468 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006469 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006470 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006471 case DataType::Type::kInt32:
6472 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006473 locations->SetInAt(0, Location::RequiresRegister());
6474 locations->SetInAt(1, Location::RequiresRegister());
6475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6476 break;
6477
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006478 case DataType::Type::kFloat32:
6479 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006480 locations->SetInAt(0, Location::RequiresFpuRegister());
6481 locations->SetInAt(1, Location::RequiresFpuRegister());
6482 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6483 break;
6484
6485 default:
6486 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6487 }
6488}
6489
6490void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006491 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006492 LocationSummary* locations = instruction->GetLocations();
6493
6494 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006495 case DataType::Type::kInt32:
6496 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006497 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6498 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6499 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006500 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006501 __ MulR6(dst, lhs, rhs);
6502 else
6503 __ Dmul(dst, lhs, rhs);
6504 break;
6505 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006506 case DataType::Type::kFloat32:
6507 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006508 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6509 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6510 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006511 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006512 __ MulS(dst, lhs, rhs);
6513 else
6514 __ MulD(dst, lhs, rhs);
6515 break;
6516 }
6517 default:
6518 LOG(FATAL) << "Unexpected mul type " << type;
6519 }
6520}
6521
6522void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6523 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006524 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006525 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 case DataType::Type::kInt32:
6527 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006528 locations->SetInAt(0, Location::RequiresRegister());
6529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6530 break;
6531
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006532 case DataType::Type::kFloat32:
6533 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006534 locations->SetInAt(0, Location::RequiresFpuRegister());
6535 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6536 break;
6537
6538 default:
6539 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6540 }
6541}
6542
6543void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006544 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006545 LocationSummary* locations = instruction->GetLocations();
6546
6547 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006548 case DataType::Type::kInt32:
6549 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006550 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6551 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006552 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006553 __ Subu(dst, ZERO, src);
6554 else
6555 __ Dsubu(dst, ZERO, src);
6556 break;
6557 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006558 case DataType::Type::kFloat32:
6559 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006560 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6561 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006562 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006563 __ NegS(dst, src);
6564 else
6565 __ NegD(dst, src);
6566 break;
6567 }
6568 default:
6569 LOG(FATAL) << "Unexpected neg type " << type;
6570 }
6571}
6572
6573void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006574 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6575 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006576 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006577 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006578 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6579 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006580}
6581
6582void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006583 // Note: if heap poisoning is enabled, the entry point takes care
6584 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006585 QuickEntrypointEnum entrypoint =
6586 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6587 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006588 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006589 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006590}
6591
6592void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6594 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006595 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006596 if (instruction->IsStringAlloc()) {
6597 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6598 } else {
6599 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006600 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006601 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006602}
6603
6604void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006605 // Note: if heap poisoning is enabled, the entry point takes care
6606 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006607 if (instruction->IsStringAlloc()) {
6608 // String is allocated through StringFactory. Call NewEmptyString entry point.
6609 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006610 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006611 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006612 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6613 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6614 __ Jalr(T9);
6615 __ Nop();
6616 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6617 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006618 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006619 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006620 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006621}
6622
6623void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006624 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006625 locations->SetInAt(0, Location::RequiresRegister());
6626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6627}
6628
6629void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006630 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006631 LocationSummary* locations = instruction->GetLocations();
6632
6633 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006634 case DataType::Type::kInt32:
6635 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006636 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6637 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6638 __ Nor(dst, src, ZERO);
6639 break;
6640 }
6641
6642 default:
6643 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6644 }
6645}
6646
6647void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006648 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006649 locations->SetInAt(0, Location::RequiresRegister());
6650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6651}
6652
6653void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6654 LocationSummary* locations = instruction->GetLocations();
6655 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6656 locations->InAt(0).AsRegister<GpuRegister>(),
6657 1);
6658}
6659
6660void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006661 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6662 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006663}
6664
Calin Juravle2ae48182016-03-16 14:05:09 +00006665void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6666 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006667 return;
6668 }
6669 Location obj = instruction->GetLocations()->InAt(0);
6670
6671 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006672 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006673}
6674
Calin Juravle2ae48182016-03-16 14:05:09 +00006675void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006676 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006677 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006678 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006679
6680 Location obj = instruction->GetLocations()->InAt(0);
6681
6682 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6683}
6684
6685void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006686 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006687}
6688
6689void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6690 HandleBinaryOp(instruction);
6691}
6692
6693void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6694 HandleBinaryOp(instruction);
6695}
6696
6697void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6698 LOG(FATAL) << "Unreachable";
6699}
6700
6701void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006702 if (instruction->GetNext()->IsSuspendCheck() &&
6703 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6704 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6705 // The back edge will generate the suspend check.
6706 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6707 }
6708
Alexey Frunze4dda3372015-06-01 18:31:49 -07006709 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6710}
6711
6712void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006713 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006714 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6715 if (location.IsStackSlot()) {
6716 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6717 } else if (location.IsDoubleStackSlot()) {
6718 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6719 }
6720 locations->SetOut(location);
6721}
6722
6723void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6724 ATTRIBUTE_UNUSED) {
6725 // Nothing to do, the parameter is already at its location.
6726}
6727
6728void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6729 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006730 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006731 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6732}
6733
6734void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6735 ATTRIBUTE_UNUSED) {
6736 // Nothing to do, the method is already at its location.
6737}
6738
6739void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006740 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006741 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006742 locations->SetInAt(i, Location::Any());
6743 }
6744 locations->SetOut(Location::Any());
6745}
6746
6747void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6748 LOG(FATAL) << "Unreachable";
6749}
6750
6751void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006752 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006753 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006754 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6755 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006756 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006757
6758 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006759 case DataType::Type::kInt32:
6760 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006761 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006762 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6764 break;
6765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006766 case DataType::Type::kFloat32:
6767 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006768 InvokeRuntimeCallingConvention calling_convention;
6769 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6770 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6771 locations->SetOut(calling_convention.GetReturnLocation(type));
6772 break;
6773 }
6774
6775 default:
6776 LOG(FATAL) << "Unexpected rem type " << type;
6777 }
6778}
6779
6780void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006782
6783 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006784 case DataType::Type::kInt32:
6785 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006786 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006787 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006789 case DataType::Type::kFloat32:
6790 case DataType::Type::kFloat64: {
6791 QuickEntrypointEnum entrypoint =
6792 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006793 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006794 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006795 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6796 } else {
6797 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6798 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006799 break;
6800 }
6801 default:
6802 LOG(FATAL) << "Unexpected rem type " << type;
6803 }
6804}
6805
Aart Bik1f8d51b2018-02-15 10:42:37 -08006806static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
6807 LocationSummary* locations = new (allocator) LocationSummary(minmax);
6808 switch (minmax->GetResultType()) {
6809 case DataType::Type::kInt32:
6810 case DataType::Type::kInt64:
6811 locations->SetInAt(0, Location::RequiresRegister());
6812 locations->SetInAt(1, Location::RequiresRegister());
6813 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6814 break;
6815 case DataType::Type::kFloat32:
6816 case DataType::Type::kFloat64:
6817 locations->SetInAt(0, Location::RequiresFpuRegister());
6818 locations->SetInAt(1, Location::RequiresFpuRegister());
6819 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6820 break;
6821 default:
6822 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
6823 }
6824}
6825
Aart Bik351df3e2018-03-07 11:54:57 -08006826void InstructionCodeGeneratorMIPS64::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08006827 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6828 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
6829 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6830
6831 if (lhs == rhs) {
6832 if (out != lhs) {
6833 __ Move(out, lhs);
6834 }
6835 } else {
6836 // Some architectures, such as ARM and MIPS (prior to r6), have a
6837 // conditional move instruction which only changes the target
6838 // (output) register if the condition is true (MIPS prior to r6 had
6839 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
6840 // change the target (output) register. If the condition is true the
6841 // output register gets the contents of the "rs" register; otherwise,
6842 // the output register is set to zero. One consequence of this is
6843 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
6844 // needs to use a pair of SELEQZ/SELNEZ instructions. After
6845 // executing this pair of instructions one of the output registers
6846 // from the pair will necessarily contain zero. Then the code ORs the
6847 // output registers from the SELEQZ/SELNEZ instructions to get the
6848 // final result.
6849 //
6850 // The initial test to see if the output register is same as the
6851 // first input register is needed to make sure that value in the
6852 // first input register isn't clobbered before we've finished
6853 // computing the output value. The logic in the corresponding else
6854 // clause performs the same task but makes sure the second input
6855 // register isn't clobbered in the event that it's the same register
6856 // as the output register; the else clause also handles the case
6857 // where the output register is distinct from both the first, and the
6858 // second input registers.
6859 if (out == lhs) {
6860 __ Slt(AT, rhs, lhs);
6861 if (is_min) {
6862 __ Seleqz(out, lhs, AT);
6863 __ Selnez(AT, rhs, AT);
6864 } else {
6865 __ Selnez(out, lhs, AT);
6866 __ Seleqz(AT, rhs, AT);
6867 }
6868 } else {
6869 __ Slt(AT, lhs, rhs);
6870 if (is_min) {
6871 __ Seleqz(out, rhs, AT);
6872 __ Selnez(AT, lhs, AT);
6873 } else {
6874 __ Selnez(out, rhs, AT);
6875 __ Seleqz(AT, lhs, AT);
6876 }
6877 }
6878 __ Or(out, out, AT);
6879 }
6880}
6881
6882void InstructionCodeGeneratorMIPS64::GenerateMinMaxFP(LocationSummary* locations,
6883 bool is_min,
6884 DataType::Type type) {
6885 FpuRegister a = locations->InAt(0).AsFpuRegister<FpuRegister>();
6886 FpuRegister b = locations->InAt(1).AsFpuRegister<FpuRegister>();
6887 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6888
6889 Mips64Label noNaNs;
6890 Mips64Label done;
6891 FpuRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
6892
6893 // When Java computes min/max it prefers a NaN to a number; the
6894 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
6895 // the inputs is a NaN and the other is a valid number, the MIPS
6896 // instruction will return the number; Java wants the NaN value
6897 // returned. This is why there is extra logic preceding the use of
6898 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
6899 // NaN, return the NaN, otherwise return the min/max.
6900 if (type == DataType::Type::kFloat64) {
6901 __ CmpUnD(FTMP, a, b);
6902 __ Bc1eqz(FTMP, &noNaNs);
6903
6904 // One of the inputs is a NaN
6905 __ CmpEqD(ftmp, a, a);
6906 // If a == a then b is the NaN, otherwise a is the NaN.
6907 __ SelD(ftmp, a, b);
6908
6909 if (ftmp != out) {
6910 __ MovD(out, ftmp);
6911 }
6912
6913 __ Bc(&done);
6914
6915 __ Bind(&noNaNs);
6916
6917 if (is_min) {
6918 __ MinD(out, a, b);
6919 } else {
6920 __ MaxD(out, a, b);
6921 }
6922 } else {
6923 DCHECK_EQ(type, DataType::Type::kFloat32);
6924 __ CmpUnS(FTMP, a, b);
6925 __ Bc1eqz(FTMP, &noNaNs);
6926
6927 // One of the inputs is a NaN
6928 __ CmpEqS(ftmp, a, a);
6929 // If a == a then b is the NaN, otherwise a is the NaN.
6930 __ SelS(ftmp, a, b);
6931
6932 if (ftmp != out) {
6933 __ MovS(out, ftmp);
6934 }
6935
6936 __ Bc(&done);
6937
6938 __ Bind(&noNaNs);
6939
6940 if (is_min) {
6941 __ MinS(out, a, b);
6942 } else {
6943 __ MaxS(out, a, b);
6944 }
6945 }
6946
6947 __ Bind(&done);
6948}
6949
Aart Bik351df3e2018-03-07 11:54:57 -08006950void InstructionCodeGeneratorMIPS64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
6951 DataType::Type type = minmax->GetResultType();
6952 switch (type) {
6953 case DataType::Type::kInt32:
6954 case DataType::Type::kInt64:
6955 GenerateMinMaxInt(minmax->GetLocations(), is_min);
6956 break;
6957 case DataType::Type::kFloat32:
6958 case DataType::Type::kFloat64:
6959 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
6960 break;
6961 default:
6962 LOG(FATAL) << "Unexpected type for HMinMax " << type;
6963 }
6964}
6965
Aart Bik1f8d51b2018-02-15 10:42:37 -08006966void LocationsBuilderMIPS64::VisitMin(HMin* min) {
6967 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
6968}
6969
6970void InstructionCodeGeneratorMIPS64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08006971 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006972}
6973
6974void LocationsBuilderMIPS64::VisitMax(HMax* max) {
6975 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
6976}
6977
6978void InstructionCodeGeneratorMIPS64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08006979 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006980}
6981
Aart Bik3dad3412018-02-28 12:01:46 -08006982void LocationsBuilderMIPS64::VisitAbs(HAbs* abs) {
6983 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6984 switch (abs->GetResultType()) {
6985 case DataType::Type::kInt32:
6986 case DataType::Type::kInt64:
6987 locations->SetInAt(0, Location::RequiresRegister());
6988 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6989 break;
6990 case DataType::Type::kFloat32:
6991 case DataType::Type::kFloat64:
6992 locations->SetInAt(0, Location::RequiresFpuRegister());
6993 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6994 break;
6995 default:
6996 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
6997 }
6998}
6999
7000void InstructionCodeGeneratorMIPS64::VisitAbs(HAbs* abs) {
7001 LocationSummary* locations = abs->GetLocations();
7002 switch (abs->GetResultType()) {
7003 case DataType::Type::kInt32: {
7004 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
7005 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
7006 __ Sra(AT, in, 31);
7007 __ Xor(out, in, AT);
7008 __ Subu(out, out, AT);
7009 break;
7010 }
7011 case DataType::Type::kInt64: {
7012 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
7013 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
7014 __ Dsra32(AT, in, 31);
7015 __ Xor(out, in, AT);
7016 __ Dsubu(out, out, AT);
7017 break;
7018 }
7019 case DataType::Type::kFloat32: {
7020 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
7021 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
7022 __ AbsS(out, in);
7023 break;
7024 }
7025 case DataType::Type::kFloat64: {
7026 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
7027 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
7028 __ AbsD(out, in);
7029 break;
7030 }
7031 default:
7032 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
7033 }
7034}
7035
Igor Murashkind01745e2017-04-05 16:40:31 -07007036void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
7037 constructor_fence->SetLocations(nullptr);
7038}
7039
7040void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
7041 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
7042 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
7043}
7044
Alexey Frunze4dda3372015-06-01 18:31:49 -07007045void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7046 memory_barrier->SetLocations(nullptr);
7047}
7048
7049void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7050 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7051}
7052
7053void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007054 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007055 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007056 locations->SetInAt(0, Mips64ReturnLocation(return_type));
7057}
7058
7059void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7060 codegen_->GenerateFrameExit();
7061}
7062
7063void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
7064 ret->SetLocations(nullptr);
7065}
7066
7067void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7068 codegen_->GenerateFrameExit();
7069}
7070
Alexey Frunze92d90602015-12-18 18:16:36 -08007071void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
7072 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007073}
7074
Alexey Frunze92d90602015-12-18 18:16:36 -08007075void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
7076 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007077}
7078
Alexey Frunze4dda3372015-06-01 18:31:49 -07007079void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
7080 HandleShift(shl);
7081}
7082
7083void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
7084 HandleShift(shl);
7085}
7086
7087void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
7088 HandleShift(shr);
7089}
7090
7091void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
7092 HandleShift(shr);
7093}
7094
Alexey Frunze4dda3372015-06-01 18:31:49 -07007095void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
7096 HandleBinaryOp(instruction);
7097}
7098
7099void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
7100 HandleBinaryOp(instruction);
7101}
7102
7103void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7104 HandleFieldGet(instruction, instruction->GetFieldInfo());
7105}
7106
7107void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7108 HandleFieldGet(instruction, instruction->GetFieldInfo());
7109}
7110
7111void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7112 HandleFieldSet(instruction, instruction->GetFieldInfo());
7113}
7114
7115void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01007116 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007117}
7118
Calin Juravlee460d1d2015-09-29 04:52:17 +01007119void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
7120 HUnresolvedInstanceFieldGet* instruction) {
7121 FieldAccessCallingConventionMIPS64 calling_convention;
7122 codegen_->CreateUnresolvedFieldLocationSummary(
7123 instruction, instruction->GetFieldType(), calling_convention);
7124}
7125
7126void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
7127 HUnresolvedInstanceFieldGet* instruction) {
7128 FieldAccessCallingConventionMIPS64 calling_convention;
7129 codegen_->GenerateUnresolvedFieldAccess(instruction,
7130 instruction->GetFieldType(),
7131 instruction->GetFieldIndex(),
7132 instruction->GetDexPc(),
7133 calling_convention);
7134}
7135
7136void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
7137 HUnresolvedInstanceFieldSet* instruction) {
7138 FieldAccessCallingConventionMIPS64 calling_convention;
7139 codegen_->CreateUnresolvedFieldLocationSummary(
7140 instruction, instruction->GetFieldType(), calling_convention);
7141}
7142
7143void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
7144 HUnresolvedInstanceFieldSet* instruction) {
7145 FieldAccessCallingConventionMIPS64 calling_convention;
7146 codegen_->GenerateUnresolvedFieldAccess(instruction,
7147 instruction->GetFieldType(),
7148 instruction->GetFieldIndex(),
7149 instruction->GetDexPc(),
7150 calling_convention);
7151}
7152
7153void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
7154 HUnresolvedStaticFieldGet* instruction) {
7155 FieldAccessCallingConventionMIPS64 calling_convention;
7156 codegen_->CreateUnresolvedFieldLocationSummary(
7157 instruction, instruction->GetFieldType(), calling_convention);
7158}
7159
7160void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
7161 HUnresolvedStaticFieldGet* instruction) {
7162 FieldAccessCallingConventionMIPS64 calling_convention;
7163 codegen_->GenerateUnresolvedFieldAccess(instruction,
7164 instruction->GetFieldType(),
7165 instruction->GetFieldIndex(),
7166 instruction->GetDexPc(),
7167 calling_convention);
7168}
7169
7170void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
7171 HUnresolvedStaticFieldSet* instruction) {
7172 FieldAccessCallingConventionMIPS64 calling_convention;
7173 codegen_->CreateUnresolvedFieldLocationSummary(
7174 instruction, instruction->GetFieldType(), calling_convention);
7175}
7176
7177void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
7178 HUnresolvedStaticFieldSet* instruction) {
7179 FieldAccessCallingConventionMIPS64 calling_convention;
7180 codegen_->GenerateUnresolvedFieldAccess(instruction,
7181 instruction->GetFieldType(),
7182 instruction->GetFieldIndex(),
7183 instruction->GetDexPc(),
7184 calling_convention);
7185}
7186
Alexey Frunze4dda3372015-06-01 18:31:49 -07007187void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007188 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7189 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02007190 // In suspend check slow path, usually there are no caller-save registers at all.
7191 // If SIMD instructions are present, however, we force spilling all live SIMD
7192 // registers in full width (since the runtime only saves/restores lower part).
7193 locations->SetCustomSlowPathCallerSaves(
7194 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007195}
7196
7197void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
7198 HBasicBlock* block = instruction->GetBlock();
7199 if (block->GetLoopInformation() != nullptr) {
7200 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7201 // The back edge will generate the suspend check.
7202 return;
7203 }
7204 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7205 // The goto will generate the suspend check.
7206 return;
7207 }
7208 GenerateSuspendCheck(instruction, nullptr);
7209}
7210
Alexey Frunze4dda3372015-06-01 18:31:49 -07007211void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007212 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7213 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007214 InvokeRuntimeCallingConvention calling_convention;
7215 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7216}
7217
7218void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01007219 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007220 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7221}
7222
7223void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007224 DataType::Type input_type = conversion->GetInputType();
7225 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007226 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7227 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007228
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007229 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
7230 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007231 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
7232 }
7233
Vladimir Markoca6fff82017-10-03 14:49:14 +01007234 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007236 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007237 locations->SetInAt(0, Location::RequiresFpuRegister());
7238 } else {
7239 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007240 }
7241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007242 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007243 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007244 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007246 }
7247}
7248
7249void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
7250 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007251 DataType::Type result_type = conversion->GetResultType();
7252 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007253
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007254 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7255 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007257 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007258 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7259 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
7260
7261 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007262 case DataType::Type::kUint8:
7263 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007264 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007265 case DataType::Type::kInt8:
7266 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007267 // Type conversion from long to types narrower than int is a result of code
7268 // transformations. To avoid unpredictable results for SEB and SEH, we first
7269 // need to sign-extend the low 32-bit value into bits 32 through 63.
7270 __ Sll(dst, src, 0);
7271 __ Seb(dst, dst);
7272 } else {
7273 __ Seb(dst, src);
7274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007275 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007276 case DataType::Type::kUint16:
7277 __ Andi(dst, src, 0xFFFF);
7278 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007279 case DataType::Type::kInt16:
7280 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007281 // Type conversion from long to types narrower than int is a result of code
7282 // transformations. To avoid unpredictable results for SEB and SEH, we first
7283 // need to sign-extend the low 32-bit value into bits 32 through 63.
7284 __ Sll(dst, src, 0);
7285 __ Seh(dst, dst);
7286 } else {
7287 __ Seh(dst, src);
7288 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007289 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007290 case DataType::Type::kInt32:
7291 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007292 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
7293 // conversions, except when the input and output registers are the same and we are not
7294 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007295 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007296 __ Sll(dst, src, 0);
7297 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007298 break;
7299
7300 default:
7301 LOG(FATAL) << "Unexpected type conversion from " << input_type
7302 << " to " << result_type;
7303 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007304 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007305 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7306 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007307 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007308 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007309 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007310 __ Cvtsl(dst, FTMP);
7311 } else {
7312 __ Cvtdl(dst, FTMP);
7313 }
7314 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007315 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007316 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007317 __ Cvtsw(dst, FTMP);
7318 } else {
7319 __ Cvtdw(dst, FTMP);
7320 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007321 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007322 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
7323 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007324 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7325 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007326
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007327 if (result_type == DataType::Type::kInt64) {
7328 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007329 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007330 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007331 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007332 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007333 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007334 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007335 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007336 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007337 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007338 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007339 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007340 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007341 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007342 } else if (DataType::IsFloatingPointType(result_type) &&
7343 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007344 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7345 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007346 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007347 __ Cvtsd(dst, src);
7348 } else {
7349 __ Cvtds(dst, src);
7350 }
7351 } else {
7352 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7353 << " to " << result_type;
7354 }
7355}
7356
7357void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
7358 HandleShift(ushr);
7359}
7360
7361void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
7362 HandleShift(ushr);
7363}
7364
7365void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7366 HandleBinaryOp(instruction);
7367}
7368
7369void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7370 HandleBinaryOp(instruction);
7371}
7372
7373void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7374 // Nothing to do, this should be removed during prepare for register allocator.
7375 LOG(FATAL) << "Unreachable";
7376}
7377
7378void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7379 // Nothing to do, this should be removed during prepare for register allocator.
7380 LOG(FATAL) << "Unreachable";
7381}
7382
7383void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007384 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007385}
7386
7387void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007388 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007389}
7390
7391void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007392 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007393}
7394
7395void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007396 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007397}
7398
7399void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007400 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007401}
7402
7403void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007404 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007405}
7406
7407void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007408 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007409}
7410
7411void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007412 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007413}
7414
7415void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007416 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007417}
7418
7419void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007420 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007421}
7422
7423void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007424 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007425}
7426
7427void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007428 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007429}
7430
Aart Bike9f37602015-10-09 11:15:55 -07007431void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007432 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007433}
7434
7435void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007436 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007437}
7438
7439void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007440 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007441}
7442
7443void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007444 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007445}
7446
7447void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007448 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007449}
7450
7451void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007452 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007453}
7454
7455void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007456 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007457}
7458
7459void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007460 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007461}
7462
Mark Mendellfe57faa2015-09-18 09:26:15 -04007463// Simple implementation of packed switch - generate cascaded compare/jumps.
7464void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7465 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007466 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007467 locations->SetInAt(0, Location::RequiresRegister());
7468}
7469
Alexey Frunze0960ac52016-12-20 17:24:59 -08007470void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7471 int32_t lower_bound,
7472 uint32_t num_entries,
7473 HBasicBlock* switch_block,
7474 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007475 // Create a set of compare/jumps.
7476 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007477 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007478 // Jump to default if index is negative
7479 // Note: We don't check the case that index is positive while value < lower_bound, because in
7480 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7481 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7482
Alexey Frunze0960ac52016-12-20 17:24:59 -08007483 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007484 // Jump to successors[0] if value == lower_bound.
7485 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7486 int32_t last_index = 0;
7487 for (; num_entries - last_index > 2; last_index += 2) {
7488 __ Addiu(temp_reg, temp_reg, -2);
7489 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7490 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7491 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7492 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7493 }
7494 if (num_entries - last_index == 2) {
7495 // The last missing case_value.
7496 __ Addiu(temp_reg, temp_reg, -1);
7497 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007498 }
7499
7500 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007501 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007502 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007503 }
7504}
7505
Alexey Frunze0960ac52016-12-20 17:24:59 -08007506void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7507 int32_t lower_bound,
7508 uint32_t num_entries,
7509 HBasicBlock* switch_block,
7510 HBasicBlock* default_block) {
7511 // Create a jump table.
7512 std::vector<Mips64Label*> labels(num_entries);
7513 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7514 for (uint32_t i = 0; i < num_entries; i++) {
7515 labels[i] = codegen_->GetLabelOf(successors[i]);
7516 }
7517 JumpTable* table = __ CreateJumpTable(std::move(labels));
7518
7519 // Is the value in range?
7520 __ Addiu32(TMP, value_reg, -lower_bound);
7521 __ LoadConst32(AT, num_entries);
7522 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7523
7524 // We are in the range of the table.
7525 // Load the target address from the jump table, indexing by the value.
7526 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007527 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007528 __ Lw(TMP, TMP, 0);
7529 // Compute the absolute target address by adding the table start address
7530 // (the table contains offsets to targets relative to its start).
7531 __ Daddu(TMP, TMP, AT);
7532 // And jump.
7533 __ Jr(TMP);
7534 __ Nop();
7535}
7536
7537void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7538 int32_t lower_bound = switch_instr->GetStartValue();
7539 uint32_t num_entries = switch_instr->GetNumEntries();
7540 LocationSummary* locations = switch_instr->GetLocations();
7541 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7542 HBasicBlock* switch_block = switch_instr->GetBlock();
7543 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7544
7545 if (num_entries > kPackedSwitchJumpTableThreshold) {
7546 GenTableBasedPackedSwitch(value_reg,
7547 lower_bound,
7548 num_entries,
7549 switch_block,
7550 default_block);
7551 } else {
7552 GenPackedSwitchWithCompares(value_reg,
7553 lower_bound,
7554 num_entries,
7555 switch_block,
7556 default_block);
7557 }
7558}
7559
Chris Larsenc9905a62017-03-13 17:06:18 -07007560void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7561 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007562 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007563 locations->SetInAt(0, Location::RequiresRegister());
7564 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007565}
7566
Chris Larsenc9905a62017-03-13 17:06:18 -07007567void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7568 LocationSummary* locations = instruction->GetLocations();
7569 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7570 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7571 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7572 __ LoadFromOffset(kLoadDoubleword,
7573 locations->Out().AsRegister<GpuRegister>(),
7574 locations->InAt(0).AsRegister<GpuRegister>(),
7575 method_offset);
7576 } else {
7577 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7578 instruction->GetIndex(), kMips64PointerSize));
7579 __ LoadFromOffset(kLoadDoubleword,
7580 locations->Out().AsRegister<GpuRegister>(),
7581 locations->InAt(0).AsRegister<GpuRegister>(),
7582 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7583 __ LoadFromOffset(kLoadDoubleword,
7584 locations->Out().AsRegister<GpuRegister>(),
7585 locations->Out().AsRegister<GpuRegister>(),
7586 method_offset);
7587 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007588}
7589
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007590void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7591 ATTRIBUTE_UNUSED) {
7592 LOG(FATAL) << "Unreachable";
7593}
7594
7595void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7596 ATTRIBUTE_UNUSED) {
7597 LOG(FATAL) << "Unreachable";
7598}
7599
Alexey Frunze4dda3372015-06-01 18:31:49 -07007600} // namespace mips64
7601} // namespace art