blob: 9f863640d5cfdc37d64ead58747a4f420b2b1b5b [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,
943 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100944 const CompilerOptions& compiler_options,
945 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700946 : CodeGenerator(graph,
947 kNumberOfGpuRegisters,
948 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000949 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700950 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
951 arraysize(kCoreCalleeSaves)),
952 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
953 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100954 compiler_options,
955 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100956 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700957 location_builder_(graph, this),
958 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100959 move_resolver_(graph->GetAllocator(), this),
960 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800961 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800962 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100963 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800964 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100965 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000966 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100967 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000968 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100969 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000970 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100971 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800972 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100973 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800974 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100975 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976 // Save RA (containing the return address) to mimic Quick.
977 AddAllocatedRegister(Location::RegisterLocation(RA));
978}
979
980#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100981// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
982#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700983#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700984
985void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700986 // Ensure that we fix up branches.
987 __ FinalizeCode();
988
989 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100990 StackMapStream* stack_map_stream = GetStackMapStream();
991 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
David Srbeckyd02b23f2018-05-29 23:27:22 +0100992 uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700993 uint32_t new_position = __ GetAdjustedPosition(old_position);
994 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100995 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700996 }
997
998 // Adjust pc offsets for the disassembly information.
999 if (disasm_info_ != nullptr) {
1000 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1001 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1002 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1003 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1004 it.second.start = __ GetAdjustedPosition(it.second.start);
1005 it.second.end = __ GetAdjustedPosition(it.second.end);
1006 }
1007 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1008 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1009 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1010 }
1011 }
1012
Alexey Frunze4dda3372015-06-01 18:31:49 -07001013 CodeGenerator::Finalize(allocator);
1014}
1015
1016Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1017 return codegen_->GetAssembler();
1018}
1019
1020void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001021 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001022 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1023}
1024
1025void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001026 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001027 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1028}
1029
1030void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1031 // Pop reg
1032 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001033 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001034}
1035
1036void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1037 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001038 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001039 __ Sd(GpuRegister(reg), SP, 0);
1040}
1041
1042void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1043 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1044 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1045 // Allocate a scratch register other than TMP, if available.
1046 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1047 // automatically unspilled when the scratch scope object is destroyed).
1048 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1049 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001050 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001051 __ LoadFromOffset(load_type,
1052 GpuRegister(ensure_scratch.GetRegister()),
1053 SP,
1054 index1 + stack_offset);
1055 __ LoadFromOffset(load_type,
1056 TMP,
1057 SP,
1058 index2 + stack_offset);
1059 __ StoreToOffset(store_type,
1060 GpuRegister(ensure_scratch.GetRegister()),
1061 SP,
1062 index2 + stack_offset);
1063 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1064}
1065
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001066void ParallelMoveResolverMIPS64::ExchangeQuadSlots(int index1, int index2) {
1067 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, index1);
1068 __ LoadFpuFromOffset(kLoadQuadword, FTMP2, SP, index2);
1069 __ StoreFpuToOffset(kStoreQuadword, FTMP, SP, index2);
1070 __ StoreFpuToOffset(kStoreQuadword, FTMP2, SP, index1);
1071}
1072
Alexey Frunze4dda3372015-06-01 18:31:49 -07001073static dwarf::Reg DWARFReg(GpuRegister reg) {
1074 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1075}
1076
David Srbeckyba702002016-02-01 18:15:29 +00001077static dwarf::Reg DWARFReg(FpuRegister reg) {
1078 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1079}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001080
1081void CodeGeneratorMIPS64::GenerateFrameEntry() {
1082 __ Bind(&frame_entry_label_);
1083
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001084 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001085 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1086 __ Addiu(TMP, TMP, 1);
1087 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001088 }
1089
Vladimir Marko33bff252017-11-01 14:35:42 +00001090 bool do_overflow_check =
1091 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001092
1093 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001094 __ LoadFromOffset(
1095 kLoadWord,
1096 ZERO,
1097 SP,
1098 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001099 RecordPcInfo(nullptr, 0);
1100 }
1101
Alexey Frunze4dda3372015-06-01 18:31:49 -07001102 if (HasEmptyFrame()) {
1103 return;
1104 }
1105
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001107 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1108 LOG(FATAL) << "Stack frame larger than "
1109 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001110 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001111
1112 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001113
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001114 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001115 __ IncreaseFrameSize(ofs);
1116
1117 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1118 GpuRegister reg = kCoreCalleeSaves[i];
1119 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001120 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001121 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001122 __ cfi().RelOffset(DWARFReg(reg), ofs);
1123 }
1124 }
1125
1126 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1127 FpuRegister reg = kFpuCalleeSaves[i];
1128 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001129 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001130 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001131 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132 }
1133 }
1134
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001135 // Save the current method if we need it. Note that we do not
1136 // do this in HCurrentMethod, as the instruction might have been removed
1137 // in the SSA graph.
1138 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001139 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001140 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001141
1142 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1143 // Initialize should_deoptimize flag to 0.
1144 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1145 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001146}
1147
1148void CodeGeneratorMIPS64::GenerateFrameExit() {
1149 __ cfi().RememberState();
1150
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001152 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001154 // For better instruction scheduling restore RA before other registers.
1155 uint32_t ofs = GetFrameSize();
1156 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001157 GpuRegister reg = kCoreCalleeSaves[i];
1158 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001159 ofs -= kMips64DoublewordSize;
1160 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001161 __ cfi().Restore(DWARFReg(reg));
1162 }
1163 }
1164
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001165 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1166 FpuRegister reg = kFpuCalleeSaves[i];
1167 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1168 ofs -= kMips64DoublewordSize;
1169 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1170 __ cfi().Restore(DWARFReg(reg));
1171 }
1172 }
1173
1174 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001175 }
1176
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001177 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001178
1179 __ cfi().RestoreState();
1180 __ cfi().DefCFAOffset(GetFrameSize());
1181}
1182
1183void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1184 __ Bind(GetLabelOf(block));
1185}
1186
1187void CodeGeneratorMIPS64::MoveLocation(Location destination,
1188 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001189 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001190 if (source.Equals(destination)) {
1191 return;
1192 }
1193
1194 // A valid move can always be inferred from the destination and source
1195 // locations. When moving from and to a register, the argument type can be
1196 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001197 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001198 DCHECK_EQ(unspecified_type, false);
1199
1200 if (destination.IsRegister() || destination.IsFpuRegister()) {
1201 if (unspecified_type) {
1202 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1203 if (source.IsStackSlot() ||
1204 (src_cst != nullptr && (src_cst->IsIntConstant()
1205 || src_cst->IsFloatConstant()
1206 || src_cst->IsNullConstant()))) {
1207 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001208 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001209 } else {
1210 // If the source is a double stack slot or a 64bit constant, a 64bit
1211 // type is appropriate. Else the source is a register, and since the
1212 // type has not been specified, we chose a 64bit type to force a 64bit
1213 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 }
1216 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1218 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1220 // Move to GPR/FPR from stack
1221 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001222 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001223 __ LoadFpuFromOffset(load_type,
1224 destination.AsFpuRegister<FpuRegister>(),
1225 SP,
1226 source.GetStackIndex());
1227 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001228 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001229 __ LoadFromOffset(load_type,
1230 destination.AsRegister<GpuRegister>(),
1231 SP,
1232 source.GetStackIndex());
1233 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001234 } else if (source.IsSIMDStackSlot()) {
1235 __ LoadFpuFromOffset(kLoadQuadword,
1236 destination.AsFpuRegister<FpuRegister>(),
1237 SP,
1238 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 } else if (source.IsConstant()) {
1240 // Move to GPR/FPR from constant
1241 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001242 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001243 gpr = destination.AsRegister<GpuRegister>();
1244 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001246 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001248 gpr = ZERO;
1249 } else {
1250 __ LoadConst32(gpr, value);
1251 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001252 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001253 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001254 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001255 gpr = ZERO;
1256 } else {
1257 __ LoadConst64(gpr, value);
1258 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001259 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001260 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001261 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001262 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1264 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001265 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001266 if (destination.IsRegister()) {
1267 // Move to GPR from GPR
1268 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1269 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001270 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001271 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1273 } else {
1274 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1275 }
1276 }
1277 } else if (source.IsFpuRegister()) {
1278 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001279 if (GetGraph()->HasSIMD()) {
1280 __ MoveV(VectorRegisterFrom(destination),
1281 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001282 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001283 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001284 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001285 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1286 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001287 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001288 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1289 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291 } else {
1292 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001293 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001294 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1295 } else {
1296 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1297 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001298 }
1299 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001300 } else if (destination.IsSIMDStackSlot()) {
1301 if (source.IsFpuRegister()) {
1302 __ StoreFpuToOffset(kStoreQuadword,
1303 source.AsFpuRegister<FpuRegister>(),
1304 SP,
1305 destination.GetStackIndex());
1306 } else {
1307 DCHECK(source.IsSIMDStackSlot());
1308 __ LoadFpuFromOffset(kLoadQuadword,
1309 FTMP,
1310 SP,
1311 source.GetStackIndex());
1312 __ StoreFpuToOffset(kStoreQuadword,
1313 FTMP,
1314 SP,
1315 destination.GetStackIndex());
1316 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001317 } else { // The destination is not a register. It must be a stack slot.
1318 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1319 if (source.IsRegister() || source.IsFpuRegister()) {
1320 if (unspecified_type) {
1321 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001322 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001323 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001324 dst_type =
1325 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001326 }
1327 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001328 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1329 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001330 // Move to stack from GPR/FPR
1331 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1332 if (source.IsRegister()) {
1333 __ StoreToOffset(store_type,
1334 source.AsRegister<GpuRegister>(),
1335 SP,
1336 destination.GetStackIndex());
1337 } else {
1338 __ StoreFpuToOffset(store_type,
1339 source.AsFpuRegister<FpuRegister>(),
1340 SP,
1341 destination.GetStackIndex());
1342 }
1343 } else if (source.IsConstant()) {
1344 // Move to stack from constant
1345 HConstant* src_cst = source.GetConstant();
1346 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001347 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001348 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001349 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1350 if (value != 0) {
1351 gpr = TMP;
1352 __ LoadConst32(gpr, value);
1353 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001354 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001355 DCHECK(destination.IsDoubleStackSlot());
1356 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1357 if (value != 0) {
1358 gpr = TMP;
1359 __ LoadConst64(gpr, value);
1360 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001361 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001362 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001363 } else {
1364 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1365 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1366 // Move to stack from stack
1367 if (destination.IsStackSlot()) {
1368 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1369 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1370 } else {
1371 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1372 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1373 }
1374 }
1375 }
1376}
1377
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001378void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001379 DCHECK(!loc1.IsConstant());
1380 DCHECK(!loc2.IsConstant());
1381
1382 if (loc1.Equals(loc2)) {
1383 return;
1384 }
1385
1386 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1387 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001388 bool is_simd1 = loc1.IsSIMDStackSlot();
1389 bool is_simd2 = loc2.IsSIMDStackSlot();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001390 bool is_fp_reg1 = loc1.IsFpuRegister();
1391 bool is_fp_reg2 = loc2.IsFpuRegister();
1392
1393 if (loc2.IsRegister() && loc1.IsRegister()) {
1394 // Swap 2 GPRs
1395 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1396 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1397 __ Move(TMP, r2);
1398 __ Move(r2, r1);
1399 __ Move(r1, TMP);
1400 } else if (is_fp_reg2 && is_fp_reg1) {
1401 // Swap 2 FPRs
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001402 if (GetGraph()->HasSIMD()) {
1403 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1404 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1405 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001406 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001407 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1408 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
1409 if (type == DataType::Type::kFloat32) {
1410 __ MovS(FTMP, r1);
1411 __ MovS(r1, r2);
1412 __ MovS(r2, FTMP);
1413 } else {
1414 DCHECK_EQ(type, DataType::Type::kFloat64);
1415 __ MovD(FTMP, r1);
1416 __ MovD(r1, r2);
1417 __ MovD(r2, FTMP);
1418 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001419 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001420 } else if (is_slot1 != is_slot2) {
1421 // Swap GPR/FPR and stack slot
1422 Location reg_loc = is_slot1 ? loc2 : loc1;
1423 Location mem_loc = is_slot1 ? loc1 : loc2;
1424 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1425 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001426 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001427 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1428 if (reg_loc.IsFpuRegister()) {
1429 __ StoreFpuToOffset(store_type,
1430 reg_loc.AsFpuRegister<FpuRegister>(),
1431 SP,
1432 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001433 if (mem_loc.IsStackSlot()) {
1434 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1435 } else {
1436 DCHECK(mem_loc.IsDoubleStackSlot());
1437 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1438 }
1439 } else {
1440 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1441 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1442 }
1443 } else if (is_slot1 && is_slot2) {
1444 move_resolver_.Exchange(loc1.GetStackIndex(),
1445 loc2.GetStackIndex(),
1446 loc1.IsDoubleStackSlot());
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001447 } else if (is_simd1 && is_simd2) {
1448 move_resolver_.ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
1449 } else if ((is_fp_reg1 && is_simd2) || (is_fp_reg2 && is_simd1)) {
1450 Location fp_reg_loc = is_fp_reg1 ? loc1 : loc2;
1451 Location mem_loc = is_fp_reg1 ? loc2 : loc1;
1452 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, mem_loc.GetStackIndex());
1453 __ StoreFpuToOffset(kStoreQuadword,
1454 fp_reg_loc.AsFpuRegister<FpuRegister>(),
1455 SP,
1456 mem_loc.GetStackIndex());
1457 __ MoveV(VectorRegisterFrom(fp_reg_loc), static_cast<VectorRegister>(FTMP));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001458 } else {
1459 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1460 }
1461}
1462
Calin Juravle175dc732015-08-25 15:42:32 +01001463void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1464 DCHECK(location.IsRegister());
1465 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1466}
1467
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1469 if (location.IsRegister()) {
1470 locations->AddTemp(location);
1471 } else {
1472 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1473 }
1474}
1475
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001476void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1477 GpuRegister value,
1478 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001479 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001480 GpuRegister card = AT;
1481 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001482 if (value_can_be_null) {
1483 __ Beqzc(value, &done);
1484 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001485 __ LoadFromOffset(kLoadDoubleword,
1486 card,
1487 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001488 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001489 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1490 __ Daddu(temp, card, temp);
1491 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001492 if (value_can_be_null) {
1493 __ Bind(&done);
1494 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001495}
1496
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001497template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001498inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1499 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001500 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001501 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001502 const DexFile* dex_file = info.target_dex_file;
Alexey Frunze19f6c692016-11-30 19:19:55 -08001503 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001504 DCHECK(info.label.IsBound());
1505 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1506 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1507 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001508 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001509 }
1510}
1511
Vladimir Markob066d432018-01-03 13:14:37 +00001512linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
1513 const DexFile* target_dex_file,
1514 uint32_t pc_insn_offset,
1515 uint32_t boot_image_offset) {
1516 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
1517 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
1518}
1519
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001520void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001521 DCHECK(linker_patches->empty());
1522 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001523 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001524 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001525 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001526 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001527 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001528 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001529 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001530 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001531 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001532 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001533 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001534 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001535 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001536 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001537 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00001538 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
1539 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001540 DCHECK(boot_image_type_patches_.empty());
1541 DCHECK(boot_image_string_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001542 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001543 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1544 method_bss_entry_patches_, linker_patches);
1545 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1546 type_bss_entry_patches_, linker_patches);
1547 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1548 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001549 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001550}
1551
Vladimir Markob066d432018-01-03 13:14:37 +00001552CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageRelRoPatch(
1553 uint32_t boot_image_offset,
1554 const PcRelativePatchInfo* info_high) {
1555 return NewPcRelativePatch(
1556 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1557}
1558
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001559CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001560 MethodReference target_method,
1561 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001562 return NewPcRelativePatch(
1563 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001564}
1565
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001566CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001567 MethodReference target_method,
1568 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001569 return NewPcRelativePatch(
1570 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001571}
1572
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001573CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001574 const DexFile& dex_file,
1575 dex::TypeIndex type_index,
1576 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001577 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001578}
1579
Vladimir Marko1998cd02017-01-13 13:02:58 +00001580CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001581 const DexFile& dex_file,
1582 dex::TypeIndex type_index,
1583 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001584 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001585}
1586
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001587CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001588 const DexFile& dex_file,
1589 dex::StringIndex string_index,
1590 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001591 return NewPcRelativePatch(
1592 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001593}
1594
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001595CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1596 const DexFile& dex_file,
1597 dex::StringIndex string_index,
1598 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001599 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001600}
1601
Alexey Frunze19f6c692016-11-30 19:19:55 -08001602CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001603 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001604 uint32_t offset_or_index,
1605 const PcRelativePatchInfo* info_high,
1606 ArenaDeque<PcRelativePatchInfo>* patches) {
1607 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001608 return &patches->back();
1609}
1610
Alexey Frunzef63f5692016-12-13 17:43:11 -08001611Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1612 return map->GetOrCreate(
1613 value,
1614 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1615}
1616
Alexey Frunze19f6c692016-11-30 19:19:55 -08001617Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1618 return uint64_literals_.GetOrCreate(
1619 value,
1620 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1621}
1622
Alexey Frunzef63f5692016-12-13 17:43:11 -08001623Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001624 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001625}
1626
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001627void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1628 GpuRegister out,
1629 PcRelativePatchInfo* info_low) {
1630 DCHECK(!info_high->patch_info_high);
1631 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001632 // Add the high half of a 32-bit offset to PC.
1633 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001634 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001635 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001636 if (info_low != nullptr) {
1637 DCHECK_EQ(info_low->patch_info_high, info_high);
1638 __ Bind(&info_low->label);
1639 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001640}
1641
Vladimir Markoeebb8212018-06-05 14:57:24 +01001642void CodeGeneratorMIPS64::LoadBootImageAddress(GpuRegister reg, uint32_t boot_image_offset) {
1643 DCHECK(!GetCompilerOptions().IsBootImage());
1644 if (GetCompilerOptions().GetCompilePic()) {
1645 DCHECK(Runtime::Current()->IsAotCompiler());
1646 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
1647 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
1648 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
1649 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
1650 __ Lwu(reg, AT, /* placeholder */ 0x5678);
1651 } else {
1652 gc::Heap* heap = Runtime::Current()->GetHeap();
1653 DCHECK(!heap->GetBootImageSpaces().empty());
1654 uintptr_t address =
1655 reinterpret_cast<uintptr_t>(heap->GetBootImageSpaces()[0]->Begin() + boot_image_offset);
1656 __ LoadLiteral(reg, kLoadDoubleword, DeduplicateBootImageAddressLiteral(address));
1657 }
1658}
1659
Alexey Frunze627c1a02017-01-30 19:28:14 -08001660Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1661 dex::StringIndex string_index,
1662 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001663 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001664 return jit_string_patches_.GetOrCreate(
1665 StringReference(&dex_file, string_index),
1666 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1667}
1668
1669Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1670 dex::TypeIndex type_index,
1671 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001672 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001673 return jit_class_patches_.GetOrCreate(
1674 TypeReference(&dex_file, type_index),
1675 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1676}
1677
1678void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1679 const uint8_t* roots_data,
1680 const Literal* literal,
1681 uint64_t index_in_table) const {
1682 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1683 uintptr_t address =
1684 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1685 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1686}
1687
1688void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1689 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001690 const StringReference& string_reference = entry.first;
1691 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001692 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001693 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001694 }
1695 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001696 const TypeReference& type_reference = entry.first;
1697 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001698 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001699 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001700 }
1701}
1702
David Brazdil58282f42016-01-14 12:45:10 +00001703void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001704 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1705 blocked_core_registers_[ZERO] = true;
1706 blocked_core_registers_[K0] = true;
1707 blocked_core_registers_[K1] = true;
1708 blocked_core_registers_[GP] = true;
1709 blocked_core_registers_[SP] = true;
1710 blocked_core_registers_[RA] = true;
1711
Lazar Trsicd9672662015-09-03 17:33:01 +02001712 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1713 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001714 blocked_core_registers_[AT] = true;
1715 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001716 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001717 blocked_fpu_registers_[FTMP] = true;
1718
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001719 if (GetInstructionSetFeatures().HasMsa()) {
1720 // To be used just for MSA instructions.
1721 blocked_fpu_registers_[FTMP2] = true;
1722 }
1723
Alexey Frunze4dda3372015-06-01 18:31:49 -07001724 // Reserve suspend and thread registers.
1725 blocked_core_registers_[S0] = true;
1726 blocked_core_registers_[TR] = true;
1727
1728 // Reserve T9 for function calls
1729 blocked_core_registers_[T9] = true;
1730
Goran Jakovljevic782be112016-06-21 12:39:04 +02001731 if (GetGraph()->IsDebuggable()) {
1732 // Stubs do not save callee-save floating point registers. If the graph
1733 // is debuggable, we need to deal with these registers differently. For
1734 // now, just block them.
1735 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1736 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1737 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738 }
1739}
1740
Alexey Frunze4dda3372015-06-01 18:31:49 -07001741size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1742 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001743 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001744}
1745
1746size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1747 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001748 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001749}
1750
1751size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001752 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1753 FpuRegister(reg_id),
1754 SP,
1755 stack_index);
1756 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001757}
1758
1759size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001760 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1761 FpuRegister(reg_id),
1762 SP,
1763 stack_index);
1764 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001765}
1766
1767void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001768 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001769}
1770
1771void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001772 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001773}
1774
Calin Juravle175dc732015-08-25 15:42:32 +01001775void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001776 HInstruction* instruction,
1777 uint32_t dex_pc,
1778 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001779 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001780 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001781 if (EntrypointRequiresStackMap(entrypoint)) {
1782 RecordPcInfo(instruction, dex_pc, slow_path);
1783 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784}
1785
Alexey Frunze15958152017-02-09 19:08:30 -08001786void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1787 HInstruction* instruction,
1788 SlowPathCode* slow_path) {
1789 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1790 GenerateInvokeRuntime(entry_point_offset);
1791}
1792
1793void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1794 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1795 __ Jalr(T9);
1796 __ Nop();
1797}
1798
Alexey Frunze4dda3372015-06-01 18:31:49 -07001799void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1800 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001801 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1802 const size_t status_byte_offset =
1803 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1804 constexpr uint32_t shifted_initialized_value =
1805 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1806
1807 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001808 __ Sltiu(TMP, TMP, shifted_initialized_value);
1809 __ Bnezc(TMP, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001810 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1811 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001812 __ Bind(slow_path->GetExitLabel());
1813}
1814
Vladimir Marko175e7862018-03-27 09:03:13 +00001815void InstructionCodeGeneratorMIPS64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1816 GpuRegister temp) {
1817 uint32_t path_to_root = check->GetBitstringPathToRoot();
1818 uint32_t mask = check->GetBitstringMask();
1819 DCHECK(IsPowerOfTwo(mask + 1));
1820 size_t mask_bits = WhichPowerOf2(mask + 1);
1821
1822 if (mask_bits == 16u) {
1823 // Load only the bitstring part of the status word.
1824 __ LoadFromOffset(
1825 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1826 // Compare the bitstring bits using XOR.
1827 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1828 } else {
1829 // /* uint32_t */ temp = temp->status_
1830 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1831 // Compare the bitstring bits using XOR.
1832 if (IsUint<16>(path_to_root)) {
1833 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1834 } else {
1835 __ LoadConst32(TMP, path_to_root);
1836 __ Xor(temp, temp, TMP);
1837 }
1838 // Shift out bits that do not contribute to the comparison.
1839 __ Sll(temp, temp, 32 - mask_bits);
1840 }
1841}
1842
Alexey Frunze4dda3372015-06-01 18:31:49 -07001843void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1844 __ Sync(0); // only stype 0 is supported
1845}
1846
1847void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1848 HBasicBlock* successor) {
1849 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001850 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1851
1852 if (slow_path == nullptr) {
1853 slow_path =
1854 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1855 instruction->SetSlowPath(slow_path);
1856 codegen_->AddSlowPath(slow_path);
1857 if (successor != nullptr) {
1858 DCHECK(successor->IsLoopHeader());
1859 }
1860 } else {
1861 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1862 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001863
1864 __ LoadFromOffset(kLoadUnsignedHalfword,
1865 TMP,
1866 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001867 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001868 if (successor == nullptr) {
1869 __ Bnezc(TMP, slow_path->GetEntryLabel());
1870 __ Bind(slow_path->GetReturnLabel());
1871 } else {
1872 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001873 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001874 // slow_path will return to GetLabelOf(successor).
1875 }
1876}
1877
1878InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1879 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001880 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001881 assembler_(codegen->GetAssembler()),
1882 codegen_(codegen) {}
1883
1884void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1885 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001886 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001887 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001888 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001889 case DataType::Type::kInt32:
1890 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001891 locations->SetInAt(0, Location::RequiresRegister());
1892 HInstruction* right = instruction->InputAt(1);
1893 bool can_use_imm = false;
1894 if (right->IsConstant()) {
1895 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1896 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1897 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001898 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001899 DCHECK(instruction->IsAdd() || instruction->IsSub());
1900 bool single_use = right->GetUses().HasExactlyOneElement();
1901 if (instruction->IsSub()) {
1902 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1903 imm = -imm;
1904 }
1905 }
1906 if (type == DataType::Type::kInt32) {
1907 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1908 } else {
1909 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1910 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001911 }
1912 }
1913 if (can_use_imm)
1914 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1915 else
1916 locations->SetInAt(1, Location::RequiresRegister());
1917 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1918 }
1919 break;
1920
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001921 case DataType::Type::kFloat32:
1922 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001923 locations->SetInAt(0, Location::RequiresFpuRegister());
1924 locations->SetInAt(1, Location::RequiresFpuRegister());
1925 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1926 break;
1927
1928 default:
1929 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1930 }
1931}
1932
1933void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001934 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001935 LocationSummary* locations = instruction->GetLocations();
1936
1937 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 case DataType::Type::kInt32:
1939 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001940 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1941 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1942 Location rhs_location = locations->InAt(1);
1943
1944 GpuRegister rhs_reg = ZERO;
1945 int64_t rhs_imm = 0;
1946 bool use_imm = rhs_location.IsConstant();
1947 if (use_imm) {
1948 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1949 } else {
1950 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1951 }
1952
1953 if (instruction->IsAnd()) {
1954 if (use_imm)
1955 __ Andi(dst, lhs, rhs_imm);
1956 else
1957 __ And(dst, lhs, rhs_reg);
1958 } else if (instruction->IsOr()) {
1959 if (use_imm)
1960 __ Ori(dst, lhs, rhs_imm);
1961 else
1962 __ Or(dst, lhs, rhs_reg);
1963 } else if (instruction->IsXor()) {
1964 if (use_imm)
1965 __ Xori(dst, lhs, rhs_imm);
1966 else
1967 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001968 } else if (instruction->IsAdd() || instruction->IsSub()) {
1969 if (instruction->IsSub()) {
1970 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001971 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001972 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001973 if (use_imm) {
1974 if (IsInt<16>(rhs_imm)) {
1975 __ Addiu(dst, lhs, rhs_imm);
1976 } else {
1977 int16_t rhs_imm_high = High16Bits(rhs_imm);
1978 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1979 if (rhs_imm_low < 0) {
1980 rhs_imm_high += 1;
1981 }
1982 __ Aui(dst, lhs, rhs_imm_high);
1983 if (rhs_imm_low != 0) {
1984 __ Addiu(dst, dst, rhs_imm_low);
1985 }
1986 }
1987 } else {
1988 if (instruction->IsAdd()) {
1989 __ Addu(dst, lhs, rhs_reg);
1990 } else {
1991 DCHECK(instruction->IsSub());
1992 __ Subu(dst, lhs, rhs_reg);
1993 }
1994 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001995 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001996 if (use_imm) {
1997 if (IsInt<16>(rhs_imm)) {
1998 __ Daddiu(dst, lhs, rhs_imm);
1999 } else if (IsInt<32>(rhs_imm)) {
2000 int16_t rhs_imm_high = High16Bits(rhs_imm);
2001 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2002 bool overflow_hi16 = false;
2003 if (rhs_imm_low < 0) {
2004 rhs_imm_high += 1;
2005 overflow_hi16 = (rhs_imm_high == -32768);
2006 }
2007 __ Daui(dst, lhs, rhs_imm_high);
2008 if (rhs_imm_low != 0) {
2009 __ Daddiu(dst, dst, rhs_imm_low);
2010 }
2011 if (overflow_hi16) {
2012 __ Dahi(dst, 1);
2013 }
2014 } else {
2015 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
2016 if (rhs_imm_low < 0) {
2017 rhs_imm += (INT64_C(1) << 16);
2018 }
2019 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
2020 if (rhs_imm_upper < 0) {
2021 rhs_imm += (INT64_C(1) << 32);
2022 }
2023 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
2024 if (rhs_imm_high < 0) {
2025 rhs_imm += (INT64_C(1) << 48);
2026 }
2027 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
2028 GpuRegister tmp = lhs;
2029 if (rhs_imm_low != 0) {
2030 __ Daddiu(dst, tmp, rhs_imm_low);
2031 tmp = dst;
2032 }
2033 // Dahi and Dati must use the same input and output register, so we have to initialize
2034 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
2035 // Daui(dst, lhs, 0).
2036 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
2037 __ Daui(dst, tmp, rhs_imm_upper);
2038 }
2039 if (rhs_imm_high != 0) {
2040 __ Dahi(dst, rhs_imm_high);
2041 }
2042 if (rhs_imm_top != 0) {
2043 __ Dati(dst, rhs_imm_top);
2044 }
2045 }
2046 } else if (instruction->IsAdd()) {
2047 __ Daddu(dst, lhs, rhs_reg);
2048 } else {
2049 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002051 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002052 }
2053 }
2054 break;
2055 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002056 case DataType::Type::kFloat32:
2057 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002058 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2059 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2060 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2061 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063 __ AddS(dst, lhs, rhs);
2064 else
2065 __ AddD(dst, lhs, rhs);
2066 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002068 __ SubS(dst, lhs, rhs);
2069 else
2070 __ SubD(dst, lhs, rhs);
2071 } else {
2072 LOG(FATAL) << "Unexpected floating-point binary operation";
2073 }
2074 break;
2075 }
2076 default:
2077 LOG(FATAL) << "Unexpected binary operation type " << type;
2078 }
2079}
2080
2081void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002082 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002083
Vladimir Markoca6fff82017-10-03 14:49:14 +01002084 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002085 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002086 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002087 case DataType::Type::kInt32:
2088 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002089 locations->SetInAt(0, Location::RequiresRegister());
2090 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002092 break;
2093 }
2094 default:
2095 LOG(FATAL) << "Unexpected shift type " << type;
2096 }
2097}
2098
2099void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002100 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002101 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002103
2104 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002105 case DataType::Type::kInt32:
2106 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002107 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2108 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2109 Location rhs_location = locations->InAt(1);
2110
2111 GpuRegister rhs_reg = ZERO;
2112 int64_t rhs_imm = 0;
2113 bool use_imm = rhs_location.IsConstant();
2114 if (use_imm) {
2115 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2116 } else {
2117 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2118 }
2119
2120 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002121 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002123
Alexey Frunze92d90602015-12-18 18:16:36 -08002124 if (shift_value == 0) {
2125 if (dst != lhs) {
2126 __ Move(dst, lhs);
2127 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002129 if (instr->IsShl()) {
2130 __ Sll(dst, lhs, shift_value);
2131 } else if (instr->IsShr()) {
2132 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002133 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002134 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002135 } else {
2136 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002137 }
2138 } else {
2139 if (shift_value < 32) {
2140 if (instr->IsShl()) {
2141 __ Dsll(dst, lhs, shift_value);
2142 } else if (instr->IsShr()) {
2143 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002144 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002145 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002146 } else {
2147 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002148 }
2149 } else {
2150 shift_value -= 32;
2151 if (instr->IsShl()) {
2152 __ Dsll32(dst, lhs, shift_value);
2153 } else if (instr->IsShr()) {
2154 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002155 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002156 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002157 } else {
2158 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002159 }
2160 }
2161 }
2162 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002164 if (instr->IsShl()) {
2165 __ Sllv(dst, lhs, rhs_reg);
2166 } else if (instr->IsShr()) {
2167 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002168 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002169 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002170 } else {
2171 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002172 }
2173 } else {
2174 if (instr->IsShl()) {
2175 __ Dsllv(dst, lhs, rhs_reg);
2176 } else if (instr->IsShr()) {
2177 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002178 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002180 } else {
2181 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002182 }
2183 }
2184 }
2185 break;
2186 }
2187 default:
2188 LOG(FATAL) << "Unexpected shift operation type " << type;
2189 }
2190}
2191
2192void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2193 HandleBinaryOp(instruction);
2194}
2195
2196void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2197 HandleBinaryOp(instruction);
2198}
2199
2200void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2201 HandleBinaryOp(instruction);
2202}
2203
2204void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2205 HandleBinaryOp(instruction);
2206}
2207
2208void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002210 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002211 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002212 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002213 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2214 object_array_get_with_read_barrier
2215 ? LocationSummary::kCallOnSlowPath
2216 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002217 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2218 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2219 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002220 locations->SetInAt(0, Location::RequiresRegister());
2221 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002223 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2224 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002225 // The output overlaps in the case of an object array get with
2226 // read barriers enabled: we do not want the move to overwrite the
2227 // array's location, as we need it to emit the read barrier.
2228 locations->SetOut(Location::RequiresRegister(),
2229 object_array_get_with_read_barrier
2230 ? Location::kOutputOverlap
2231 : Location::kNoOutputOverlap);
2232 }
2233 // We need a temporary register for the read barrier marking slow
2234 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2235 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002236 bool temp_needed = instruction->GetIndex()->IsConstant()
2237 ? !kBakerReadBarrierThunksEnableForFields
2238 : !kBakerReadBarrierThunksEnableForArrays;
2239 if (temp_needed) {
2240 locations->AddTemp(Location::RequiresRegister());
2241 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002242 }
2243}
2244
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002245static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2246 auto null_checker = [codegen, instruction]() {
2247 codegen->MaybeRecordImplicitNullCheck(instruction);
2248 };
2249 return null_checker;
2250}
2251
Alexey Frunze4dda3372015-06-01 18:31:49 -07002252void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2253 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002254 Location obj_loc = locations->InAt(0);
2255 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2256 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002257 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002258 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002259 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002261 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002262 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2263 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002264 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002265 case DataType::Type::kBool:
2266 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002267 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002268 if (index.IsConstant()) {
2269 size_t offset =
2270 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002271 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002272 } else {
2273 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002274 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002275 }
2276 break;
2277 }
2278
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002280 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002281 if (index.IsConstant()) {
2282 size_t offset =
2283 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002284 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002285 } else {
2286 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002287 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002288 }
2289 break;
2290 }
2291
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002293 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002294 if (maybe_compressed_char_at) {
2295 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002296 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002297 __ Dext(TMP, TMP, 0, 1);
2298 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2299 "Expecting 0=compressed, 1=uncompressed");
2300 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002301 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002302 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2303 if (maybe_compressed_char_at) {
2304 Mips64Label uncompressed_load, done;
2305 __ Bnezc(TMP, &uncompressed_load);
2306 __ LoadFromOffset(kLoadUnsignedByte,
2307 out,
2308 obj,
2309 data_offset + (const_index << TIMES_1));
2310 __ Bc(&done);
2311 __ Bind(&uncompressed_load);
2312 __ LoadFromOffset(kLoadUnsignedHalfword,
2313 out,
2314 obj,
2315 data_offset + (const_index << TIMES_2));
2316 __ Bind(&done);
2317 } else {
2318 __ LoadFromOffset(kLoadUnsignedHalfword,
2319 out,
2320 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002321 data_offset + (const_index << TIMES_2),
2322 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002323 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002324 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002325 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2326 if (maybe_compressed_char_at) {
2327 Mips64Label uncompressed_load, done;
2328 __ Bnezc(TMP, &uncompressed_load);
2329 __ Daddu(TMP, obj, index_reg);
2330 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2331 __ Bc(&done);
2332 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002333 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002334 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2335 __ Bind(&done);
2336 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002337 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002338 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002339 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002340 }
2341 break;
2342 }
2343
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002344 case DataType::Type::kInt16: {
2345 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2346 if (index.IsConstant()) {
2347 size_t offset =
2348 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2349 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2350 } else {
2351 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2352 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2353 }
2354 break;
2355 }
2356
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002358 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002359 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 LoadOperandType load_type =
2361 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002362 if (index.IsConstant()) {
2363 size_t offset =
2364 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002365 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002366 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002367 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002368 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002369 }
2370 break;
2371 }
2372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002374 static_assert(
2375 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2376 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2377 // /* HeapReference<Object> */ out =
2378 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2379 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002380 bool temp_needed = index.IsConstant()
2381 ? !kBakerReadBarrierThunksEnableForFields
2382 : !kBakerReadBarrierThunksEnableForArrays;
2383 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002384 // Note that a potential implicit null check is handled in this
2385 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002386 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2387 if (index.IsConstant()) {
2388 // Array load with a constant index can be treated as a field load.
2389 size_t offset =
2390 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2391 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2392 out_loc,
2393 obj,
2394 offset,
2395 temp,
2396 /* needs_null_check */ false);
2397 } else {
2398 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2399 out_loc,
2400 obj,
2401 data_offset,
2402 index,
2403 temp,
2404 /* needs_null_check */ false);
2405 }
Alexey Frunze15958152017-02-09 19:08:30 -08002406 } else {
2407 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2408 if (index.IsConstant()) {
2409 size_t offset =
2410 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2411 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2412 // If read barriers are enabled, emit read barriers other than
2413 // Baker's using a slow path (and also unpoison the loaded
2414 // reference, if heap poisoning is enabled).
2415 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2416 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002417 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002418 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2419 // If read barriers are enabled, emit read barriers other than
2420 // Baker's using a slow path (and also unpoison the loaded
2421 // reference, if heap poisoning is enabled).
2422 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2423 out_loc,
2424 out_loc,
2425 obj_loc,
2426 data_offset,
2427 index);
2428 }
2429 }
2430 break;
2431 }
2432
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002433 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002434 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002435 if (index.IsConstant()) {
2436 size_t offset =
2437 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002438 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002439 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002440 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002441 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002442 }
2443 break;
2444 }
2445
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002447 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448 if (index.IsConstant()) {
2449 size_t offset =
2450 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002451 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002452 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002453 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002454 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002455 }
2456 break;
2457 }
2458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002459 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002460 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002461 if (index.IsConstant()) {
2462 size_t offset =
2463 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002464 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002465 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002466 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002467 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 }
2469 break;
2470 }
2471
Aart Bik66c158e2018-01-31 12:55:04 -08002472 case DataType::Type::kUint32:
2473 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002474 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002475 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2476 UNREACHABLE();
2477 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002478}
2479
2480void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002481 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002482 locations->SetInAt(0, Location::RequiresRegister());
2483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2484}
2485
2486void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2487 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002488 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002489 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2490 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2491 __ LoadFromOffset(kLoadWord, out, obj, offset);
2492 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002493 // Mask out compression flag from String's array length.
2494 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2495 __ Srl(out, out, 1u);
2496 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002497}
2498
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002499Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2500 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2501 ? Location::ConstantLocation(instruction->AsConstant())
2502 : Location::RequiresRegister();
2503}
2504
2505Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2506 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2507 // We can store a non-zero float or double constant without first loading it into the FPU,
2508 // but we should only prefer this if the constant has a single use.
2509 if (instruction->IsConstant() &&
2510 (instruction->AsConstant()->IsZeroBitPattern() ||
2511 instruction->GetUses().HasExactlyOneElement())) {
2512 return Location::ConstantLocation(instruction->AsConstant());
2513 // Otherwise fall through and require an FPU register for the constant.
2514 }
2515 return Location::RequiresFpuRegister();
2516}
2517
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002519 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002520
2521 bool needs_write_barrier =
2522 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2523 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2524
Vladimir Markoca6fff82017-10-03 14:49:14 +01002525 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002526 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002527 may_need_runtime_call_for_type_check ?
2528 LocationSummary::kCallOnSlowPath :
2529 LocationSummary::kNoCall);
2530
2531 locations->SetInAt(0, Location::RequiresRegister());
2532 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002534 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002535 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002536 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2537 }
2538 if (needs_write_barrier) {
2539 // Temporary register for the write barrier.
2540 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002541 }
2542}
2543
2544void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2545 LocationSummary* locations = instruction->GetLocations();
2546 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2547 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002548 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002550 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002551 bool needs_write_barrier =
2552 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002553 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002554 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002555
2556 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002558 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002561 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002562 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002564 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2565 }
2566 if (value_location.IsConstant()) {
2567 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2568 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2569 } else {
2570 GpuRegister value = value_location.AsRegister<GpuRegister>();
2571 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002572 }
2573 break;
2574 }
2575
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002576 case DataType::Type::kUint16:
2577 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002579 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002580 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002581 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002582 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002583 }
2584 if (value_location.IsConstant()) {
2585 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2586 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2587 } else {
2588 GpuRegister value = value_location.AsRegister<GpuRegister>();
2589 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002590 }
2591 break;
2592 }
2593
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002594 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2596 if (index.IsConstant()) {
2597 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2598 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002599 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002600 }
2601 if (value_location.IsConstant()) {
2602 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2603 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2604 } else {
2605 GpuRegister value = value_location.AsRegister<GpuRegister>();
2606 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2607 }
2608 break;
2609 }
2610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002612 if (value_location.IsConstant()) {
2613 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002614 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002615 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002616 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002617 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002618 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002619 }
Alexey Frunze15958152017-02-09 19:08:30 -08002620 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2621 DCHECK_EQ(value, 0);
2622 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2623 DCHECK(!needs_write_barrier);
2624 DCHECK(!may_need_runtime_call_for_type_check);
2625 break;
2626 }
2627
2628 DCHECK(needs_write_barrier);
2629 GpuRegister value = value_location.AsRegister<GpuRegister>();
2630 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2631 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2632 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2633 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2634 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2635 Mips64Label done;
2636 SlowPathCodeMIPS64* slow_path = nullptr;
2637
2638 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002639 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002640 codegen_->AddSlowPath(slow_path);
2641 if (instruction->GetValueCanBeNull()) {
2642 Mips64Label non_zero;
2643 __ Bnezc(value, &non_zero);
2644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2645 if (index.IsConstant()) {
2646 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002647 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002648 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002649 }
Alexey Frunze15958152017-02-09 19:08:30 -08002650 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2651 __ Bc(&done);
2652 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002653 }
Alexey Frunze15958152017-02-09 19:08:30 -08002654
2655 // Note that when read barriers are enabled, the type checks
2656 // are performed without read barriers. This is fine, even in
2657 // the case where a class object is in the from-space after
2658 // the flip, as a comparison involving such a type would not
2659 // produce a false positive; it may of course produce a false
2660 // negative, in which case we would take the ArraySet slow
2661 // path.
2662
2663 // /* HeapReference<Class> */ temp1 = obj->klass_
2664 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2665 __ MaybeUnpoisonHeapReference(temp1);
2666
2667 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2668 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2669 // /* HeapReference<Class> */ temp2 = value->klass_
2670 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2671 // If heap poisoning is enabled, no need to unpoison `temp1`
2672 // nor `temp2`, as we are comparing two poisoned references.
2673
2674 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2675 Mips64Label do_put;
2676 __ Beqc(temp1, temp2, &do_put);
2677 // If heap poisoning is enabled, the `temp1` reference has
2678 // not been unpoisoned yet; unpoison it now.
2679 __ MaybeUnpoisonHeapReference(temp1);
2680
2681 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2682 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2683 // If heap poisoning is enabled, no need to unpoison
2684 // `temp1`, as we are comparing against null below.
2685 __ Bnezc(temp1, slow_path->GetEntryLabel());
2686 __ Bind(&do_put);
2687 } else {
2688 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2689 }
2690 }
2691
2692 GpuRegister source = value;
2693 if (kPoisonHeapReferences) {
2694 // Note that in the case where `value` is a null reference,
2695 // we do not enter this block, as a null reference does not
2696 // need poisoning.
2697 __ Move(temp1, value);
2698 __ PoisonHeapReference(temp1);
2699 source = temp1;
2700 }
2701
2702 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2703 if (index.IsConstant()) {
2704 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002705 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002706 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002707 }
2708 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2709
2710 if (!may_need_runtime_call_for_type_check) {
2711 codegen_->MaybeRecordImplicitNullCheck(instruction);
2712 }
2713
2714 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2715
2716 if (done.IsLinked()) {
2717 __ Bind(&done);
2718 }
2719
2720 if (slow_path != nullptr) {
2721 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002722 }
2723 break;
2724 }
2725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002727 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002728 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002729 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002730 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002731 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002732 }
2733 if (value_location.IsConstant()) {
2734 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2735 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2736 } else {
2737 GpuRegister value = value_location.AsRegister<GpuRegister>();
2738 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002739 }
2740 break;
2741 }
2742
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002744 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002745 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002746 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002747 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002748 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002749 }
2750 if (value_location.IsConstant()) {
2751 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2752 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2753 } else {
2754 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2755 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002756 }
2757 break;
2758 }
2759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002761 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002762 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002763 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002764 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002765 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002766 }
2767 if (value_location.IsConstant()) {
2768 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2769 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2770 } else {
2771 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2772 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002773 }
2774 break;
2775 }
2776
Aart Bik66c158e2018-01-31 12:55:04 -08002777 case DataType::Type::kUint32:
2778 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002779 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002780 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2781 UNREACHABLE();
2782 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002783}
2784
2785void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002786 RegisterSet caller_saves = RegisterSet::Empty();
2787 InvokeRuntimeCallingConvention calling_convention;
2788 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2789 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2790 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002791
2792 HInstruction* index = instruction->InputAt(0);
2793 HInstruction* length = instruction->InputAt(1);
2794
2795 bool const_index = false;
2796 bool const_length = false;
2797
2798 if (index->IsConstant()) {
2799 if (length->IsConstant()) {
2800 const_index = true;
2801 const_length = true;
2802 } else {
2803 int32_t index_value = index->AsIntConstant()->GetValue();
2804 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2805 const_index = true;
2806 }
2807 }
2808 } else if (length->IsConstant()) {
2809 int32_t length_value = length->AsIntConstant()->GetValue();
2810 if (IsUint<15>(length_value)) {
2811 const_length = true;
2812 }
2813 }
2814
2815 locations->SetInAt(0, const_index
2816 ? Location::ConstantLocation(index->AsConstant())
2817 : Location::RequiresRegister());
2818 locations->SetInAt(1, const_length
2819 ? Location::ConstantLocation(length->AsConstant())
2820 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002821}
2822
2823void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2824 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002825 Location index_loc = locations->InAt(0);
2826 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002827
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002828 if (length_loc.IsConstant()) {
2829 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2830 if (index_loc.IsConstant()) {
2831 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2832 if (index < 0 || index >= length) {
2833 BoundsCheckSlowPathMIPS64* slow_path =
2834 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2835 codegen_->AddSlowPath(slow_path);
2836 __ Bc(slow_path->GetEntryLabel());
2837 } else {
2838 // Nothing to be done.
2839 }
2840 return;
2841 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002842
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002843 BoundsCheckSlowPathMIPS64* slow_path =
2844 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2845 codegen_->AddSlowPath(slow_path);
2846 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2847 if (length == 0) {
2848 __ Bc(slow_path->GetEntryLabel());
2849 } else if (length == 1) {
2850 __ Bnezc(index, slow_path->GetEntryLabel());
2851 } else {
2852 DCHECK(IsUint<15>(length)) << length;
2853 __ Sltiu(TMP, index, length);
2854 __ Beqzc(TMP, slow_path->GetEntryLabel());
2855 }
2856 } else {
2857 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2858 BoundsCheckSlowPathMIPS64* slow_path =
2859 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2860 codegen_->AddSlowPath(slow_path);
2861 if (index_loc.IsConstant()) {
2862 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2863 if (index < 0) {
2864 __ Bc(slow_path->GetEntryLabel());
2865 } else if (index == 0) {
2866 __ Blezc(length, slow_path->GetEntryLabel());
2867 } else {
2868 DCHECK(IsInt<16>(index + 1)) << index;
2869 __ Sltiu(TMP, length, index + 1);
2870 __ Bnezc(TMP, slow_path->GetEntryLabel());
2871 }
2872 } else {
2873 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2874 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2875 }
2876 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002877}
2878
Alexey Frunze15958152017-02-09 19:08:30 -08002879// Temp is used for read barrier.
2880static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2881 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002882 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002883 (kUseBakerReadBarrier ||
2884 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2885 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2886 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2887 return 1;
2888 }
2889 return 0;
2890}
2891
2892// Extra temp is used for read barrier.
2893static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2894 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2895}
2896
Alexey Frunze4dda3372015-06-01 18:31:49 -07002897void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002898 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002899 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002900 LocationSummary* locations =
2901 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002902 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00002903 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
2904 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2905 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2906 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
2907 } else {
2908 locations->SetInAt(1, Location::RequiresRegister());
2909 }
Alexey Frunze15958152017-02-09 19:08:30 -08002910 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002911}
2912
2913void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002914 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002915 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002916 Location obj_loc = locations->InAt(0);
2917 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00002918 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08002919 Location temp_loc = locations->GetTemp(0);
2920 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2921 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2922 DCHECK_LE(num_temps, 2u);
2923 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002924 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2925 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2926 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2927 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2928 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2929 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2930 const uint32_t object_array_data_offset =
2931 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2932 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002933
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002934 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002935 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002936 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2937 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938 codegen_->AddSlowPath(slow_path);
2939
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002940 // Avoid this check if we know `obj` is not null.
2941 if (instruction->MustDoNullCheck()) {
2942 __ Beqzc(obj, &done);
2943 }
2944
2945 switch (type_check_kind) {
2946 case TypeCheckKind::kExactCheck:
2947 case TypeCheckKind::kArrayCheck: {
2948 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002949 GenerateReferenceLoadTwoRegisters(instruction,
2950 temp_loc,
2951 obj_loc,
2952 class_offset,
2953 maybe_temp2_loc,
2954 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002955 // Jump to slow path for throwing the exception or doing a
2956 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00002957 __ Bnec(temp, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002958 break;
2959 }
2960
2961 case TypeCheckKind::kAbstractClassCheck: {
2962 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002963 GenerateReferenceLoadTwoRegisters(instruction,
2964 temp_loc,
2965 obj_loc,
2966 class_offset,
2967 maybe_temp2_loc,
2968 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002969 // If the class is abstract, we eagerly fetch the super class of the
2970 // object to avoid doing a comparison we know will fail.
2971 Mips64Label loop;
2972 __ Bind(&loop);
2973 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002974 GenerateReferenceLoadOneRegister(instruction,
2975 temp_loc,
2976 super_offset,
2977 maybe_temp2_loc,
2978 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002979 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2980 // exception.
2981 __ Beqzc(temp, slow_path->GetEntryLabel());
2982 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00002983 __ Bnec(temp, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002984 break;
2985 }
2986
2987 case TypeCheckKind::kClassHierarchyCheck: {
2988 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002989 GenerateReferenceLoadTwoRegisters(instruction,
2990 temp_loc,
2991 obj_loc,
2992 class_offset,
2993 maybe_temp2_loc,
2994 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002995 // Walk over the class hierarchy to find a match.
2996 Mips64Label loop;
2997 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00002998 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002999 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003000 GenerateReferenceLoadOneRegister(instruction,
3001 temp_loc,
3002 super_offset,
3003 maybe_temp2_loc,
3004 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003005 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3006 // exception. Otherwise, jump to the beginning of the loop.
3007 __ Bnezc(temp, &loop);
3008 __ Bc(slow_path->GetEntryLabel());
3009 break;
3010 }
3011
3012 case TypeCheckKind::kArrayObjectCheck: {
3013 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003014 GenerateReferenceLoadTwoRegisters(instruction,
3015 temp_loc,
3016 obj_loc,
3017 class_offset,
3018 maybe_temp2_loc,
3019 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003020 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003021 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003022 // Otherwise, we need to check that the object's class is a non-primitive array.
3023 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003024 GenerateReferenceLoadOneRegister(instruction,
3025 temp_loc,
3026 component_offset,
3027 maybe_temp2_loc,
3028 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003029 // If the component type is null, jump to the slow path to throw the exception.
3030 __ Beqzc(temp, slow_path->GetEntryLabel());
3031 // Otherwise, the object is indeed an array, further check that this component
3032 // type is not a primitive type.
3033 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3034 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3035 __ Bnezc(temp, slow_path->GetEntryLabel());
3036 break;
3037 }
3038
3039 case TypeCheckKind::kUnresolvedCheck:
3040 // We always go into the type check slow path for the unresolved check case.
3041 // We cannot directly call the CheckCast runtime entry point
3042 // without resorting to a type checking slow path here (i.e. by
3043 // calling InvokeRuntime directly), as it would require to
3044 // assign fixed registers for the inputs of this HInstanceOf
3045 // instruction (following the runtime calling convention), which
3046 // might be cluttered by the potential first read barrier
3047 // emission at the beginning of this method.
3048 __ Bc(slow_path->GetEntryLabel());
3049 break;
3050
3051 case TypeCheckKind::kInterfaceCheck: {
3052 // Avoid read barriers to improve performance of the fast path. We can not get false
3053 // positives by doing this.
3054 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003055 GenerateReferenceLoadTwoRegisters(instruction,
3056 temp_loc,
3057 obj_loc,
3058 class_offset,
3059 maybe_temp2_loc,
3060 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003061 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003062 GenerateReferenceLoadTwoRegisters(instruction,
3063 temp_loc,
3064 temp_loc,
3065 iftable_offset,
3066 maybe_temp2_loc,
3067 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003068 // Iftable is never null.
3069 __ Lw(TMP, temp, array_length_offset);
3070 // Loop through the iftable and check if any class matches.
3071 Mips64Label loop;
3072 __ Bind(&loop);
3073 __ Beqzc(TMP, slow_path->GetEntryLabel());
3074 __ Lwu(AT, temp, object_array_data_offset);
3075 __ MaybeUnpoisonHeapReference(AT);
3076 // Go to next interface.
3077 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3078 __ Addiu(TMP, TMP, -2);
3079 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003080 __ Bnec(AT, cls.AsRegister<GpuRegister>(), &loop);
3081 break;
3082 }
3083
3084 case TypeCheckKind::kBitstringCheck: {
3085 // /* HeapReference<Class> */ temp = obj->klass_
3086 GenerateReferenceLoadTwoRegisters(instruction,
3087 temp_loc,
3088 obj_loc,
3089 class_offset,
3090 maybe_temp2_loc,
3091 kWithoutReadBarrier);
3092
3093 GenerateBitstringTypeCheckCompare(instruction, temp);
3094 __ Bnezc(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003095 break;
3096 }
3097 }
3098
3099 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003100 __ Bind(slow_path->GetExitLabel());
3101}
3102
3103void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3104 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003105 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003106 locations->SetInAt(0, Location::RequiresRegister());
3107 if (check->HasUses()) {
3108 locations->SetOut(Location::SameAsFirstInput());
3109 }
3110}
3111
3112void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3113 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003114 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003115 check->GetLoadClass(),
3116 check,
3117 check->GetDexPc(),
3118 true);
3119 codegen_->AddSlowPath(slow_path);
3120 GenerateClassInitializationCheck(slow_path,
3121 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3122}
3123
3124void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003126
Vladimir Markoca6fff82017-10-03 14:49:14 +01003127 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128
3129 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003130 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003131 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003134 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 case DataType::Type::kInt32:
3136 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003137 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003138 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3140 break;
3141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003142 case DataType::Type::kFloat32:
3143 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003144 locations->SetInAt(0, Location::RequiresFpuRegister());
3145 locations->SetInAt(1, Location::RequiresFpuRegister());
3146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003147 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003148
3149 default:
3150 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3151 }
3152}
3153
3154void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3155 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003156 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003157 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003158
3159 // 0 if: left == right
3160 // 1 if: left > right
3161 // -1 if: left < right
3162 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003164 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003166 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003167 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003168 case DataType::Type::kInt32:
3169 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003170 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003171 Location rhs_location = locations->InAt(1);
3172 bool use_imm = rhs_location.IsConstant();
3173 GpuRegister rhs = ZERO;
3174 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003175 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003176 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3177 if (value != 0) {
3178 rhs = AT;
3179 __ LoadConst64(rhs, value);
3180 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003181 } else {
3182 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3183 if (value != 0) {
3184 rhs = AT;
3185 __ LoadConst32(rhs, value);
3186 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003187 }
3188 } else {
3189 rhs = rhs_location.AsRegister<GpuRegister>();
3190 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003191 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003192 __ Slt(res, rhs, lhs);
3193 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003194 break;
3195 }
3196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003197 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003198 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3199 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3200 Mips64Label done;
3201 __ CmpEqS(FTMP, lhs, rhs);
3202 __ LoadConst32(res, 0);
3203 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003204 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003205 __ CmpLtS(FTMP, lhs, rhs);
3206 __ LoadConst32(res, -1);
3207 __ Bc1nez(FTMP, &done);
3208 __ LoadConst32(res, 1);
3209 } else {
3210 __ CmpLtS(FTMP, rhs, lhs);
3211 __ LoadConst32(res, 1);
3212 __ Bc1nez(FTMP, &done);
3213 __ LoadConst32(res, -1);
3214 }
3215 __ Bind(&done);
3216 break;
3217 }
3218
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003219 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003220 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3221 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3222 Mips64Label done;
3223 __ CmpEqD(FTMP, lhs, rhs);
3224 __ LoadConst32(res, 0);
3225 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003226 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003227 __ CmpLtD(FTMP, lhs, rhs);
3228 __ LoadConst32(res, -1);
3229 __ Bc1nez(FTMP, &done);
3230 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003231 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003232 __ CmpLtD(FTMP, rhs, lhs);
3233 __ LoadConst32(res, 1);
3234 __ Bc1nez(FTMP, &done);
3235 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003236 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003237 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003238 break;
3239 }
3240
3241 default:
3242 LOG(FATAL) << "Unimplemented compare type " << in_type;
3243 }
3244}
3245
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003246void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003247 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003248 switch (instruction->InputAt(0)->GetType()) {
3249 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003250 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003251 locations->SetInAt(0, Location::RequiresRegister());
3252 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3253 break;
3254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003255 case DataType::Type::kFloat32:
3256 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003257 locations->SetInAt(0, Location::RequiresFpuRegister());
3258 locations->SetInAt(1, Location::RequiresFpuRegister());
3259 break;
3260 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003261 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3263 }
3264}
3265
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003266void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003267 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003268 return;
3269 }
3270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003271 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003272 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003273 switch (type) {
3274 default:
3275 // Integer case.
3276 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3277 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003278 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003279 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3280 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003281 case DataType::Type::kFloat32:
3282 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003283 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3284 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003285 }
3286}
3287
Alexey Frunzec857c742015-09-23 15:12:39 -07003288void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3289 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003291
3292 LocationSummary* locations = instruction->GetLocations();
3293 Location second = locations->InAt(1);
3294 DCHECK(second.IsConstant());
3295
3296 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3297 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3298 int64_t imm = Int64FromConstant(second.GetConstant());
3299 DCHECK(imm == 1 || imm == -1);
3300
3301 if (instruction->IsRem()) {
3302 __ Move(out, ZERO);
3303 } else {
3304 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003305 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003306 __ Subu(out, ZERO, dividend);
3307 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003308 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003309 __ Dsubu(out, ZERO, dividend);
3310 }
3311 } else if (out != dividend) {
3312 __ Move(out, dividend);
3313 }
3314 }
3315}
3316
3317void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3318 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003319 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003320
3321 LocationSummary* locations = instruction->GetLocations();
3322 Location second = locations->InAt(1);
3323 DCHECK(second.IsConstant());
3324
3325 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3326 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3327 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003328 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003329 int ctz_imm = CTZ(abs_imm);
3330
3331 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003332 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003333 if (ctz_imm == 1) {
3334 // Fast path for division by +/-2, which is very common.
3335 __ Srl(TMP, dividend, 31);
3336 } else {
3337 __ Sra(TMP, dividend, 31);
3338 __ Srl(TMP, TMP, 32 - ctz_imm);
3339 }
3340 __ Addu(out, dividend, TMP);
3341 __ Sra(out, out, ctz_imm);
3342 if (imm < 0) {
3343 __ Subu(out, ZERO, out);
3344 }
3345 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003346 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003347 if (ctz_imm == 1) {
3348 // Fast path for division by +/-2, which is very common.
3349 __ Dsrl32(TMP, dividend, 31);
3350 } else {
3351 __ Dsra32(TMP, dividend, 31);
3352 if (ctz_imm > 32) {
3353 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3354 } else {
3355 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3356 }
3357 }
3358 __ Daddu(out, dividend, TMP);
3359 if (ctz_imm < 32) {
3360 __ Dsra(out, out, ctz_imm);
3361 } else {
3362 __ Dsra32(out, out, ctz_imm - 32);
3363 }
3364 if (imm < 0) {
3365 __ Dsubu(out, ZERO, out);
3366 }
3367 }
3368 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003369 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003370 if (ctz_imm == 1) {
3371 // Fast path for modulo +/-2, which is very common.
3372 __ Sra(TMP, dividend, 31);
3373 __ Subu(out, dividend, TMP);
3374 __ Andi(out, out, 1);
3375 __ Addu(out, out, TMP);
3376 } else {
3377 __ Sra(TMP, dividend, 31);
3378 __ Srl(TMP, TMP, 32 - ctz_imm);
3379 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003380 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003381 __ Subu(out, out, TMP);
3382 }
3383 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003384 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003385 if (ctz_imm == 1) {
3386 // Fast path for modulo +/-2, which is very common.
3387 __ Dsra32(TMP, dividend, 31);
3388 __ Dsubu(out, dividend, TMP);
3389 __ Andi(out, out, 1);
3390 __ Daddu(out, out, TMP);
3391 } else {
3392 __ Dsra32(TMP, dividend, 31);
3393 if (ctz_imm > 32) {
3394 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3395 } else {
3396 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3397 }
3398 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003399 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003400 __ Dsubu(out, out, TMP);
3401 }
3402 }
3403 }
3404}
3405
3406void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3407 DCHECK(instruction->IsDiv() || instruction->IsRem());
3408
3409 LocationSummary* locations = instruction->GetLocations();
3410 Location second = locations->InAt(1);
3411 DCHECK(second.IsConstant());
3412
3413 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3414 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3415 int64_t imm = Int64FromConstant(second.GetConstant());
3416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 DataType::Type type = instruction->GetResultType();
3418 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003419
3420 int64_t magic;
3421 int shift;
3422 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003423 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003424 &magic,
3425 &shift);
3426
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003427 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003428 __ LoadConst32(TMP, magic);
3429 __ MuhR6(TMP, dividend, TMP);
3430
3431 if (imm > 0 && magic < 0) {
3432 __ Addu(TMP, TMP, dividend);
3433 } else if (imm < 0 && magic > 0) {
3434 __ Subu(TMP, TMP, dividend);
3435 }
3436
3437 if (shift != 0) {
3438 __ Sra(TMP, TMP, shift);
3439 }
3440
3441 if (instruction->IsDiv()) {
3442 __ Sra(out, TMP, 31);
3443 __ Subu(out, TMP, out);
3444 } else {
3445 __ Sra(AT, TMP, 31);
3446 __ Subu(AT, TMP, AT);
3447 __ LoadConst32(TMP, imm);
3448 __ MulR6(TMP, AT, TMP);
3449 __ Subu(out, dividend, TMP);
3450 }
3451 } else {
3452 __ LoadConst64(TMP, magic);
3453 __ Dmuh(TMP, dividend, TMP);
3454
3455 if (imm > 0 && magic < 0) {
3456 __ Daddu(TMP, TMP, dividend);
3457 } else if (imm < 0 && magic > 0) {
3458 __ Dsubu(TMP, TMP, dividend);
3459 }
3460
3461 if (shift >= 32) {
3462 __ Dsra32(TMP, TMP, shift - 32);
3463 } else if (shift > 0) {
3464 __ Dsra(TMP, TMP, shift);
3465 }
3466
3467 if (instruction->IsDiv()) {
3468 __ Dsra32(out, TMP, 31);
3469 __ Dsubu(out, TMP, out);
3470 } else {
3471 __ Dsra32(AT, TMP, 31);
3472 __ Dsubu(AT, TMP, AT);
3473 __ LoadConst64(TMP, imm);
3474 __ Dmul(TMP, AT, TMP);
3475 __ Dsubu(out, dividend, TMP);
3476 }
3477 }
3478}
3479
3480void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3481 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003482 DataType::Type type = instruction->GetResultType();
3483 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003484
3485 LocationSummary* locations = instruction->GetLocations();
3486 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3487 Location second = locations->InAt(1);
3488
3489 if (second.IsConstant()) {
3490 int64_t imm = Int64FromConstant(second.GetConstant());
3491 if (imm == 0) {
3492 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3493 } else if (imm == 1 || imm == -1) {
3494 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003495 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003496 DivRemByPowerOfTwo(instruction);
3497 } else {
3498 DCHECK(imm <= -2 || imm >= 2);
3499 GenerateDivRemWithAnyConstant(instruction);
3500 }
3501 } else {
3502 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3503 GpuRegister divisor = second.AsRegister<GpuRegister>();
3504 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003505 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003506 __ DivR6(out, dividend, divisor);
3507 else
3508 __ Ddiv(out, dividend, divisor);
3509 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003511 __ ModR6(out, dividend, divisor);
3512 else
3513 __ Dmod(out, dividend, divisor);
3514 }
3515 }
3516}
3517
Alexey Frunze4dda3372015-06-01 18:31:49 -07003518void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3519 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003520 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003521 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003522 case DataType::Type::kInt32:
3523 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003524 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003525 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003526 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3527 break;
3528
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003529 case DataType::Type::kFloat32:
3530 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003531 locations->SetInAt(0, Location::RequiresFpuRegister());
3532 locations->SetInAt(1, Location::RequiresFpuRegister());
3533 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3534 break;
3535
3536 default:
3537 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3538 }
3539}
3540
3541void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003542 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003543 LocationSummary* locations = instruction->GetLocations();
3544
3545 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003546 case DataType::Type::kInt32:
3547 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003548 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003549 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003550 case DataType::Type::kFloat32:
3551 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003552 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3553 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3554 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003555 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003556 __ DivS(dst, lhs, rhs);
3557 else
3558 __ DivD(dst, lhs, rhs);
3559 break;
3560 }
3561 default:
3562 LOG(FATAL) << "Unexpected div type " << type;
3563 }
3564}
3565
3566void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003567 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003568 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003569}
3570
3571void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3572 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003573 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003574 codegen_->AddSlowPath(slow_path);
3575 Location value = instruction->GetLocations()->InAt(0);
3576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003577 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003579 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003580 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003581 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003582 }
3583
3584 if (value.IsConstant()) {
3585 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3586 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003587 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 } else {
3589 // A division by a non-null constant is valid. We don't need to perform
3590 // any check, so simply fall through.
3591 }
3592 } else {
3593 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3594 }
3595}
3596
3597void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3598 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003599 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003600 locations->SetOut(Location::ConstantLocation(constant));
3601}
3602
3603void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3604 // Will be generated at use site.
3605}
3606
3607void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3608 exit->SetLocations(nullptr);
3609}
3610
3611void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3612}
3613
3614void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3615 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003616 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003617 locations->SetOut(Location::ConstantLocation(constant));
3618}
3619
3620void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3621 // Will be generated at use site.
3622}
3623
David Brazdilfc6a86a2015-06-26 10:33:45 +00003624void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003625 if (successor->IsExitBlock()) {
3626 DCHECK(got->GetPrevious()->AlwaysThrows());
3627 return; // no code needed
3628 }
3629
Alexey Frunze4dda3372015-06-01 18:31:49 -07003630 HBasicBlock* block = got->GetBlock();
3631 HInstruction* previous = got->GetPrevious();
3632 HLoopInformation* info = block->GetLoopInformation();
3633
3634 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01003635 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3636 __ Ld(AT, SP, kCurrentMethodStackOffset);
3637 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3638 __ Addiu(TMP, TMP, 1);
3639 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3640 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003641 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3642 return;
3643 }
3644 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3645 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3646 }
3647 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003648 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003649 }
3650}
3651
David Brazdilfc6a86a2015-06-26 10:33:45 +00003652void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3653 got->SetLocations(nullptr);
3654}
3655
3656void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3657 HandleGoto(got, got->GetSuccessor());
3658}
3659
3660void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3661 try_boundary->SetLocations(nullptr);
3662}
3663
3664void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3665 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3666 if (!successor->IsExitBlock()) {
3667 HandleGoto(try_boundary, successor);
3668 }
3669}
3670
Alexey Frunze299a9392015-12-08 16:08:02 -08003671void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3672 bool is64bit,
3673 LocationSummary* locations) {
3674 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3675 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3676 Location rhs_location = locations->InAt(1);
3677 GpuRegister rhs_reg = ZERO;
3678 int64_t rhs_imm = 0;
3679 bool use_imm = rhs_location.IsConstant();
3680 if (use_imm) {
3681 if (is64bit) {
3682 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3683 } else {
3684 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3685 }
3686 } else {
3687 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3688 }
3689 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3690
3691 switch (cond) {
3692 case kCondEQ:
3693 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003694 if (use_imm && IsInt<16>(-rhs_imm)) {
3695 if (rhs_imm == 0) {
3696 if (cond == kCondEQ) {
3697 __ Sltiu(dst, lhs, 1);
3698 } else {
3699 __ Sltu(dst, ZERO, lhs);
3700 }
3701 } else {
3702 if (is64bit) {
3703 __ Daddiu(dst, lhs, -rhs_imm);
3704 } else {
3705 __ Addiu(dst, lhs, -rhs_imm);
3706 }
3707 if (cond == kCondEQ) {
3708 __ Sltiu(dst, dst, 1);
3709 } else {
3710 __ Sltu(dst, ZERO, dst);
3711 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003712 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003713 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003714 if (use_imm && IsUint<16>(rhs_imm)) {
3715 __ Xori(dst, lhs, rhs_imm);
3716 } else {
3717 if (use_imm) {
3718 rhs_reg = TMP;
3719 __ LoadConst64(rhs_reg, rhs_imm);
3720 }
3721 __ Xor(dst, lhs, rhs_reg);
3722 }
3723 if (cond == kCondEQ) {
3724 __ Sltiu(dst, dst, 1);
3725 } else {
3726 __ Sltu(dst, ZERO, dst);
3727 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003728 }
3729 break;
3730
3731 case kCondLT:
3732 case kCondGE:
3733 if (use_imm && IsInt<16>(rhs_imm)) {
3734 __ Slti(dst, lhs, rhs_imm);
3735 } else {
3736 if (use_imm) {
3737 rhs_reg = TMP;
3738 __ LoadConst64(rhs_reg, rhs_imm);
3739 }
3740 __ Slt(dst, lhs, rhs_reg);
3741 }
3742 if (cond == kCondGE) {
3743 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3744 // only the slt instruction but no sge.
3745 __ Xori(dst, dst, 1);
3746 }
3747 break;
3748
3749 case kCondLE:
3750 case kCondGT:
3751 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3752 // Simulate lhs <= rhs via lhs < rhs + 1.
3753 __ Slti(dst, lhs, rhs_imm_plus_one);
3754 if (cond == kCondGT) {
3755 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3756 // only the slti instruction but no sgti.
3757 __ Xori(dst, dst, 1);
3758 }
3759 } else {
3760 if (use_imm) {
3761 rhs_reg = TMP;
3762 __ LoadConst64(rhs_reg, rhs_imm);
3763 }
3764 __ Slt(dst, rhs_reg, lhs);
3765 if (cond == kCondLE) {
3766 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3767 // only the slt instruction but no sle.
3768 __ Xori(dst, dst, 1);
3769 }
3770 }
3771 break;
3772
3773 case kCondB:
3774 case kCondAE:
3775 if (use_imm && IsInt<16>(rhs_imm)) {
3776 // Sltiu sign-extends its 16-bit immediate operand before
3777 // the comparison and thus lets us compare directly with
3778 // unsigned values in the ranges [0, 0x7fff] and
3779 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3780 __ Sltiu(dst, lhs, rhs_imm);
3781 } else {
3782 if (use_imm) {
3783 rhs_reg = TMP;
3784 __ LoadConst64(rhs_reg, rhs_imm);
3785 }
3786 __ Sltu(dst, lhs, rhs_reg);
3787 }
3788 if (cond == kCondAE) {
3789 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3790 // only the sltu instruction but no sgeu.
3791 __ Xori(dst, dst, 1);
3792 }
3793 break;
3794
3795 case kCondBE:
3796 case kCondA:
3797 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3798 // Simulate lhs <= rhs via lhs < rhs + 1.
3799 // Note that this only works if rhs + 1 does not overflow
3800 // to 0, hence the check above.
3801 // Sltiu sign-extends its 16-bit immediate operand before
3802 // the comparison and thus lets us compare directly with
3803 // unsigned values in the ranges [0, 0x7fff] and
3804 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3805 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3806 if (cond == kCondA) {
3807 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3808 // only the sltiu instruction but no sgtiu.
3809 __ Xori(dst, dst, 1);
3810 }
3811 } else {
3812 if (use_imm) {
3813 rhs_reg = TMP;
3814 __ LoadConst64(rhs_reg, rhs_imm);
3815 }
3816 __ Sltu(dst, rhs_reg, lhs);
3817 if (cond == kCondBE) {
3818 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3819 // only the sltu instruction but no sleu.
3820 __ Xori(dst, dst, 1);
3821 }
3822 }
3823 break;
3824 }
3825}
3826
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003827bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3828 bool is64bit,
3829 LocationSummary* input_locations,
3830 GpuRegister dst) {
3831 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3832 Location rhs_location = input_locations->InAt(1);
3833 GpuRegister rhs_reg = ZERO;
3834 int64_t rhs_imm = 0;
3835 bool use_imm = rhs_location.IsConstant();
3836 if (use_imm) {
3837 if (is64bit) {
3838 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3839 } else {
3840 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3841 }
3842 } else {
3843 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3844 }
3845 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3846
3847 switch (cond) {
3848 case kCondEQ:
3849 case kCondNE:
3850 if (use_imm && IsInt<16>(-rhs_imm)) {
3851 if (is64bit) {
3852 __ Daddiu(dst, lhs, -rhs_imm);
3853 } else {
3854 __ Addiu(dst, lhs, -rhs_imm);
3855 }
3856 } else if (use_imm && IsUint<16>(rhs_imm)) {
3857 __ Xori(dst, lhs, rhs_imm);
3858 } else {
3859 if (use_imm) {
3860 rhs_reg = TMP;
3861 __ LoadConst64(rhs_reg, rhs_imm);
3862 }
3863 __ Xor(dst, lhs, rhs_reg);
3864 }
3865 return (cond == kCondEQ);
3866
3867 case kCondLT:
3868 case kCondGE:
3869 if (use_imm && IsInt<16>(rhs_imm)) {
3870 __ Slti(dst, lhs, rhs_imm);
3871 } else {
3872 if (use_imm) {
3873 rhs_reg = TMP;
3874 __ LoadConst64(rhs_reg, rhs_imm);
3875 }
3876 __ Slt(dst, lhs, rhs_reg);
3877 }
3878 return (cond == kCondGE);
3879
3880 case kCondLE:
3881 case kCondGT:
3882 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3883 // Simulate lhs <= rhs via lhs < rhs + 1.
3884 __ Slti(dst, lhs, rhs_imm_plus_one);
3885 return (cond == kCondGT);
3886 } else {
3887 if (use_imm) {
3888 rhs_reg = TMP;
3889 __ LoadConst64(rhs_reg, rhs_imm);
3890 }
3891 __ Slt(dst, rhs_reg, lhs);
3892 return (cond == kCondLE);
3893 }
3894
3895 case kCondB:
3896 case kCondAE:
3897 if (use_imm && IsInt<16>(rhs_imm)) {
3898 // Sltiu sign-extends its 16-bit immediate operand before
3899 // the comparison and thus lets us compare directly with
3900 // unsigned values in the ranges [0, 0x7fff] and
3901 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3902 __ Sltiu(dst, lhs, rhs_imm);
3903 } else {
3904 if (use_imm) {
3905 rhs_reg = TMP;
3906 __ LoadConst64(rhs_reg, rhs_imm);
3907 }
3908 __ Sltu(dst, lhs, rhs_reg);
3909 }
3910 return (cond == kCondAE);
3911
3912 case kCondBE:
3913 case kCondA:
3914 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3915 // Simulate lhs <= rhs via lhs < rhs + 1.
3916 // Note that this only works if rhs + 1 does not overflow
3917 // to 0, hence the check above.
3918 // Sltiu sign-extends its 16-bit immediate operand before
3919 // the comparison and thus lets us compare directly with
3920 // unsigned values in the ranges [0, 0x7fff] and
3921 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3922 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3923 return (cond == kCondA);
3924 } else {
3925 if (use_imm) {
3926 rhs_reg = TMP;
3927 __ LoadConst64(rhs_reg, rhs_imm);
3928 }
3929 __ Sltu(dst, rhs_reg, lhs);
3930 return (cond == kCondBE);
3931 }
3932 }
3933}
3934
Alexey Frunze299a9392015-12-08 16:08:02 -08003935void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3936 bool is64bit,
3937 LocationSummary* locations,
3938 Mips64Label* label) {
3939 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3940 Location rhs_location = locations->InAt(1);
3941 GpuRegister rhs_reg = ZERO;
3942 int64_t rhs_imm = 0;
3943 bool use_imm = rhs_location.IsConstant();
3944 if (use_imm) {
3945 if (is64bit) {
3946 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3947 } else {
3948 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3949 }
3950 } else {
3951 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3952 }
3953
3954 if (use_imm && rhs_imm == 0) {
3955 switch (cond) {
3956 case kCondEQ:
3957 case kCondBE: // <= 0 if zero
3958 __ Beqzc(lhs, label);
3959 break;
3960 case kCondNE:
3961 case kCondA: // > 0 if non-zero
3962 __ Bnezc(lhs, label);
3963 break;
3964 case kCondLT:
3965 __ Bltzc(lhs, label);
3966 break;
3967 case kCondGE:
3968 __ Bgezc(lhs, label);
3969 break;
3970 case kCondLE:
3971 __ Blezc(lhs, label);
3972 break;
3973 case kCondGT:
3974 __ Bgtzc(lhs, label);
3975 break;
3976 case kCondB: // always false
3977 break;
3978 case kCondAE: // always true
3979 __ Bc(label);
3980 break;
3981 }
3982 } else {
3983 if (use_imm) {
3984 rhs_reg = TMP;
3985 __ LoadConst64(rhs_reg, rhs_imm);
3986 }
3987 switch (cond) {
3988 case kCondEQ:
3989 __ Beqc(lhs, rhs_reg, label);
3990 break;
3991 case kCondNE:
3992 __ Bnec(lhs, rhs_reg, label);
3993 break;
3994 case kCondLT:
3995 __ Bltc(lhs, rhs_reg, label);
3996 break;
3997 case kCondGE:
3998 __ Bgec(lhs, rhs_reg, label);
3999 break;
4000 case kCondLE:
4001 __ Bgec(rhs_reg, lhs, label);
4002 break;
4003 case kCondGT:
4004 __ Bltc(rhs_reg, lhs, label);
4005 break;
4006 case kCondB:
4007 __ Bltuc(lhs, rhs_reg, label);
4008 break;
4009 case kCondAE:
4010 __ Bgeuc(lhs, rhs_reg, label);
4011 break;
4012 case kCondBE:
4013 __ Bgeuc(rhs_reg, lhs, label);
4014 break;
4015 case kCondA:
4016 __ Bltuc(rhs_reg, lhs, label);
4017 break;
4018 }
4019 }
4020}
4021
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004022void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
4023 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004024 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004025 LocationSummary* locations) {
4026 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4027 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4028 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004029 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004030 switch (cond) {
4031 case kCondEQ:
4032 __ CmpEqS(FTMP, lhs, rhs);
4033 __ Mfc1(dst, FTMP);
4034 __ Andi(dst, dst, 1);
4035 break;
4036 case kCondNE:
4037 __ CmpEqS(FTMP, lhs, rhs);
4038 __ Mfc1(dst, FTMP);
4039 __ Addiu(dst, dst, 1);
4040 break;
4041 case kCondLT:
4042 if (gt_bias) {
4043 __ CmpLtS(FTMP, lhs, rhs);
4044 } else {
4045 __ CmpUltS(FTMP, lhs, rhs);
4046 }
4047 __ Mfc1(dst, FTMP);
4048 __ Andi(dst, dst, 1);
4049 break;
4050 case kCondLE:
4051 if (gt_bias) {
4052 __ CmpLeS(FTMP, lhs, rhs);
4053 } else {
4054 __ CmpUleS(FTMP, lhs, rhs);
4055 }
4056 __ Mfc1(dst, FTMP);
4057 __ Andi(dst, dst, 1);
4058 break;
4059 case kCondGT:
4060 if (gt_bias) {
4061 __ CmpUltS(FTMP, rhs, lhs);
4062 } else {
4063 __ CmpLtS(FTMP, rhs, lhs);
4064 }
4065 __ Mfc1(dst, FTMP);
4066 __ Andi(dst, dst, 1);
4067 break;
4068 case kCondGE:
4069 if (gt_bias) {
4070 __ CmpUleS(FTMP, rhs, lhs);
4071 } else {
4072 __ CmpLeS(FTMP, rhs, lhs);
4073 }
4074 __ Mfc1(dst, FTMP);
4075 __ Andi(dst, dst, 1);
4076 break;
4077 default:
4078 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4079 UNREACHABLE();
4080 }
4081 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004083 switch (cond) {
4084 case kCondEQ:
4085 __ CmpEqD(FTMP, lhs, rhs);
4086 __ Mfc1(dst, FTMP);
4087 __ Andi(dst, dst, 1);
4088 break;
4089 case kCondNE:
4090 __ CmpEqD(FTMP, lhs, rhs);
4091 __ Mfc1(dst, FTMP);
4092 __ Addiu(dst, dst, 1);
4093 break;
4094 case kCondLT:
4095 if (gt_bias) {
4096 __ CmpLtD(FTMP, lhs, rhs);
4097 } else {
4098 __ CmpUltD(FTMP, lhs, rhs);
4099 }
4100 __ Mfc1(dst, FTMP);
4101 __ Andi(dst, dst, 1);
4102 break;
4103 case kCondLE:
4104 if (gt_bias) {
4105 __ CmpLeD(FTMP, lhs, rhs);
4106 } else {
4107 __ CmpUleD(FTMP, lhs, rhs);
4108 }
4109 __ Mfc1(dst, FTMP);
4110 __ Andi(dst, dst, 1);
4111 break;
4112 case kCondGT:
4113 if (gt_bias) {
4114 __ CmpUltD(FTMP, rhs, lhs);
4115 } else {
4116 __ CmpLtD(FTMP, rhs, lhs);
4117 }
4118 __ Mfc1(dst, FTMP);
4119 __ Andi(dst, dst, 1);
4120 break;
4121 case kCondGE:
4122 if (gt_bias) {
4123 __ CmpUleD(FTMP, rhs, lhs);
4124 } else {
4125 __ CmpLeD(FTMP, rhs, lhs);
4126 }
4127 __ Mfc1(dst, FTMP);
4128 __ Andi(dst, dst, 1);
4129 break;
4130 default:
4131 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4132 UNREACHABLE();
4133 }
4134 }
4135}
4136
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004137bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4138 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004139 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004140 LocationSummary* input_locations,
4141 FpuRegister dst) {
4142 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4143 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004144 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004145 switch (cond) {
4146 case kCondEQ:
4147 __ CmpEqS(dst, lhs, rhs);
4148 return false;
4149 case kCondNE:
4150 __ CmpEqS(dst, lhs, rhs);
4151 return true;
4152 case kCondLT:
4153 if (gt_bias) {
4154 __ CmpLtS(dst, lhs, rhs);
4155 } else {
4156 __ CmpUltS(dst, lhs, rhs);
4157 }
4158 return false;
4159 case kCondLE:
4160 if (gt_bias) {
4161 __ CmpLeS(dst, lhs, rhs);
4162 } else {
4163 __ CmpUleS(dst, lhs, rhs);
4164 }
4165 return false;
4166 case kCondGT:
4167 if (gt_bias) {
4168 __ CmpUltS(dst, rhs, lhs);
4169 } else {
4170 __ CmpLtS(dst, rhs, lhs);
4171 }
4172 return false;
4173 case kCondGE:
4174 if (gt_bias) {
4175 __ CmpUleS(dst, rhs, lhs);
4176 } else {
4177 __ CmpLeS(dst, rhs, lhs);
4178 }
4179 return false;
4180 default:
4181 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4182 UNREACHABLE();
4183 }
4184 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004185 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004186 switch (cond) {
4187 case kCondEQ:
4188 __ CmpEqD(dst, lhs, rhs);
4189 return false;
4190 case kCondNE:
4191 __ CmpEqD(dst, lhs, rhs);
4192 return true;
4193 case kCondLT:
4194 if (gt_bias) {
4195 __ CmpLtD(dst, lhs, rhs);
4196 } else {
4197 __ CmpUltD(dst, lhs, rhs);
4198 }
4199 return false;
4200 case kCondLE:
4201 if (gt_bias) {
4202 __ CmpLeD(dst, lhs, rhs);
4203 } else {
4204 __ CmpUleD(dst, lhs, rhs);
4205 }
4206 return false;
4207 case kCondGT:
4208 if (gt_bias) {
4209 __ CmpUltD(dst, rhs, lhs);
4210 } else {
4211 __ CmpLtD(dst, rhs, lhs);
4212 }
4213 return false;
4214 case kCondGE:
4215 if (gt_bias) {
4216 __ CmpUleD(dst, rhs, lhs);
4217 } else {
4218 __ CmpLeD(dst, rhs, lhs);
4219 }
4220 return false;
4221 default:
4222 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4223 UNREACHABLE();
4224 }
4225 }
4226}
4227
Alexey Frunze299a9392015-12-08 16:08:02 -08004228void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4229 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004230 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004231 LocationSummary* locations,
4232 Mips64Label* label) {
4233 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4234 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004235 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004236 switch (cond) {
4237 case kCondEQ:
4238 __ CmpEqS(FTMP, lhs, rhs);
4239 __ Bc1nez(FTMP, label);
4240 break;
4241 case kCondNE:
4242 __ CmpEqS(FTMP, lhs, rhs);
4243 __ Bc1eqz(FTMP, label);
4244 break;
4245 case kCondLT:
4246 if (gt_bias) {
4247 __ CmpLtS(FTMP, lhs, rhs);
4248 } else {
4249 __ CmpUltS(FTMP, lhs, rhs);
4250 }
4251 __ Bc1nez(FTMP, label);
4252 break;
4253 case kCondLE:
4254 if (gt_bias) {
4255 __ CmpLeS(FTMP, lhs, rhs);
4256 } else {
4257 __ CmpUleS(FTMP, lhs, rhs);
4258 }
4259 __ Bc1nez(FTMP, label);
4260 break;
4261 case kCondGT:
4262 if (gt_bias) {
4263 __ CmpUltS(FTMP, rhs, lhs);
4264 } else {
4265 __ CmpLtS(FTMP, rhs, lhs);
4266 }
4267 __ Bc1nez(FTMP, label);
4268 break;
4269 case kCondGE:
4270 if (gt_bias) {
4271 __ CmpUleS(FTMP, rhs, lhs);
4272 } else {
4273 __ CmpLeS(FTMP, rhs, lhs);
4274 }
4275 __ Bc1nez(FTMP, label);
4276 break;
4277 default:
4278 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004279 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004280 }
4281 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004282 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004283 switch (cond) {
4284 case kCondEQ:
4285 __ CmpEqD(FTMP, lhs, rhs);
4286 __ Bc1nez(FTMP, label);
4287 break;
4288 case kCondNE:
4289 __ CmpEqD(FTMP, lhs, rhs);
4290 __ Bc1eqz(FTMP, label);
4291 break;
4292 case kCondLT:
4293 if (gt_bias) {
4294 __ CmpLtD(FTMP, lhs, rhs);
4295 } else {
4296 __ CmpUltD(FTMP, lhs, rhs);
4297 }
4298 __ Bc1nez(FTMP, label);
4299 break;
4300 case kCondLE:
4301 if (gt_bias) {
4302 __ CmpLeD(FTMP, lhs, rhs);
4303 } else {
4304 __ CmpUleD(FTMP, lhs, rhs);
4305 }
4306 __ Bc1nez(FTMP, label);
4307 break;
4308 case kCondGT:
4309 if (gt_bias) {
4310 __ CmpUltD(FTMP, rhs, lhs);
4311 } else {
4312 __ CmpLtD(FTMP, rhs, lhs);
4313 }
4314 __ Bc1nez(FTMP, label);
4315 break;
4316 case kCondGE:
4317 if (gt_bias) {
4318 __ CmpUleD(FTMP, rhs, lhs);
4319 } else {
4320 __ CmpLeD(FTMP, rhs, lhs);
4321 }
4322 __ Bc1nez(FTMP, label);
4323 break;
4324 default:
4325 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004326 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004327 }
4328 }
4329}
4330
Alexey Frunze4dda3372015-06-01 18:31:49 -07004331void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004332 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004333 Mips64Label* true_target,
4334 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004335 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004336
David Brazdil0debae72015-11-12 18:37:00 +00004337 if (true_target == nullptr && false_target == nullptr) {
4338 // Nothing to do. The code always falls through.
4339 return;
4340 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004341 // Constant condition, statically compared against "true" (integer value 1).
4342 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004343 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004344 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004345 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004346 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004347 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004348 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004349 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004350 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004351 }
David Brazdil0debae72015-11-12 18:37:00 +00004352 return;
4353 }
4354
4355 // The following code generates these patterns:
4356 // (1) true_target == nullptr && false_target != nullptr
4357 // - opposite condition true => branch to false_target
4358 // (2) true_target != nullptr && false_target == nullptr
4359 // - condition true => branch to true_target
4360 // (3) true_target != nullptr && false_target != nullptr
4361 // - condition true => branch to true_target
4362 // - branch to false_target
4363 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004364 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004365 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004366 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004367 if (true_target == nullptr) {
4368 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4369 } else {
4370 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4371 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004372 } else {
4373 // The condition instruction has not been materialized, use its inputs as
4374 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004375 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004376 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004377 LocationSummary* locations = cond->GetLocations();
4378 IfCondition if_cond = condition->GetCondition();
4379 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004380
David Brazdil0debae72015-11-12 18:37:00 +00004381 if (true_target == nullptr) {
4382 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004383 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004384 }
4385
Alexey Frunze299a9392015-12-08 16:08:02 -08004386 switch (type) {
4387 default:
4388 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4389 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004390 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004391 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4392 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004393 case DataType::Type::kFloat32:
4394 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004395 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4396 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004397 }
4398 }
David Brazdil0debae72015-11-12 18:37:00 +00004399
4400 // If neither branch falls through (case 3), the conditional branch to `true_target`
4401 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4402 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004403 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004404 }
4405}
4406
4407void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004408 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004409 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004410 locations->SetInAt(0, Location::RequiresRegister());
4411 }
4412}
4413
4414void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004415 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4416 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004417 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004418 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004419 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004420 nullptr : codegen_->GetLabelOf(false_successor);
4421 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004422}
4423
4424void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004425 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004426 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004427 InvokeRuntimeCallingConvention calling_convention;
4428 RegisterSet caller_saves = RegisterSet::Empty();
4429 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4430 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004431 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004432 locations->SetInAt(0, Location::RequiresRegister());
4433 }
4434}
4435
4436void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004437 SlowPathCodeMIPS64* slow_path =
4438 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004439 GenerateTestAndBranch(deoptimize,
4440 /* condition_input_index */ 0,
4441 slow_path->GetEntryLabel(),
4442 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004443}
4444
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004445// This function returns true if a conditional move can be generated for HSelect.
4446// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4447// branches and regular moves.
4448//
4449// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4450//
4451// While determining feasibility of a conditional move and setting inputs/outputs
4452// are two distinct tasks, this function does both because they share quite a bit
4453// of common logic.
4454static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4455 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4456 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4457 HCondition* condition = cond->AsCondition();
4458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004459 DataType::Type cond_type =
4460 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4461 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004462
4463 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4464 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4465 bool is_true_value_zero_constant =
4466 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4467 bool is_false_value_zero_constant =
4468 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4469
4470 bool can_move_conditionally = false;
4471 bool use_const_for_false_in = false;
4472 bool use_const_for_true_in = false;
4473
4474 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004475 if (!DataType::IsFloatingPointType(cond_type)) {
4476 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004477 // Moving int/long on int/long condition.
4478 if (is_true_value_zero_constant) {
4479 // seleqz out_reg, false_reg, cond_reg
4480 can_move_conditionally = true;
4481 use_const_for_true_in = true;
4482 } else if (is_false_value_zero_constant) {
4483 // selnez out_reg, true_reg, cond_reg
4484 can_move_conditionally = true;
4485 use_const_for_false_in = true;
4486 } else if (materialized) {
4487 // Not materializing unmaterialized int conditions
4488 // to keep the instruction count low.
4489 // selnez AT, true_reg, cond_reg
4490 // seleqz TMP, false_reg, cond_reg
4491 // or out_reg, AT, TMP
4492 can_move_conditionally = true;
4493 }
4494 } else {
4495 // Moving float/double on int/long condition.
4496 if (materialized) {
4497 // Not materializing unmaterialized int conditions
4498 // to keep the instruction count low.
4499 can_move_conditionally = true;
4500 if (is_true_value_zero_constant) {
4501 // sltu TMP, ZERO, cond_reg
4502 // mtc1 TMP, temp_cond_reg
4503 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4504 use_const_for_true_in = true;
4505 } else if (is_false_value_zero_constant) {
4506 // sltu TMP, ZERO, cond_reg
4507 // mtc1 TMP, temp_cond_reg
4508 // selnez.fmt out_reg, true_reg, temp_cond_reg
4509 use_const_for_false_in = true;
4510 } else {
4511 // sltu TMP, ZERO, cond_reg
4512 // mtc1 TMP, temp_cond_reg
4513 // sel.fmt temp_cond_reg, false_reg, true_reg
4514 // mov.fmt out_reg, temp_cond_reg
4515 }
4516 }
4517 }
4518 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004519 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004520 // Moving int/long on float/double condition.
4521 can_move_conditionally = true;
4522 if (is_true_value_zero_constant) {
4523 // mfc1 TMP, temp_cond_reg
4524 // seleqz out_reg, false_reg, TMP
4525 use_const_for_true_in = true;
4526 } else if (is_false_value_zero_constant) {
4527 // mfc1 TMP, temp_cond_reg
4528 // selnez out_reg, true_reg, TMP
4529 use_const_for_false_in = true;
4530 } else {
4531 // mfc1 TMP, temp_cond_reg
4532 // selnez AT, true_reg, TMP
4533 // seleqz TMP, false_reg, TMP
4534 // or out_reg, AT, TMP
4535 }
4536 } else {
4537 // Moving float/double on float/double condition.
4538 can_move_conditionally = true;
4539 if (is_true_value_zero_constant) {
4540 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4541 use_const_for_true_in = true;
4542 } else if (is_false_value_zero_constant) {
4543 // selnez.fmt out_reg, true_reg, temp_cond_reg
4544 use_const_for_false_in = true;
4545 } else {
4546 // sel.fmt temp_cond_reg, false_reg, true_reg
4547 // mov.fmt out_reg, temp_cond_reg
4548 }
4549 }
4550 }
4551 }
4552
4553 if (can_move_conditionally) {
4554 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4555 } else {
4556 DCHECK(!use_const_for_false_in);
4557 DCHECK(!use_const_for_true_in);
4558 }
4559
4560 if (locations_to_set != nullptr) {
4561 if (use_const_for_false_in) {
4562 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4563 } else {
4564 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004565 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004566 ? Location::RequiresFpuRegister()
4567 : Location::RequiresRegister());
4568 }
4569 if (use_const_for_true_in) {
4570 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4571 } else {
4572 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004573 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004574 ? Location::RequiresFpuRegister()
4575 : Location::RequiresRegister());
4576 }
4577 if (materialized) {
4578 locations_to_set->SetInAt(2, Location::RequiresRegister());
4579 }
4580
4581 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004582 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004583 ? Location::RequiresFpuRegister()
4584 : Location::RequiresRegister());
4585 } else {
4586 locations_to_set->SetOut(Location::SameAsFirstInput());
4587 }
4588 }
4589
4590 return can_move_conditionally;
4591}
4592
4593
4594void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4595 LocationSummary* locations = select->GetLocations();
4596 Location dst = locations->Out();
4597 Location false_src = locations->InAt(0);
4598 Location true_src = locations->InAt(1);
4599 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4600 GpuRegister cond_reg = TMP;
4601 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004602 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004603 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004605
4606 if (IsBooleanValueOrMaterializedCondition(cond)) {
4607 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4608 } else {
4609 HCondition* condition = cond->AsCondition();
4610 LocationSummary* cond_locations = cond->GetLocations();
4611 IfCondition if_cond = condition->GetCondition();
4612 cond_type = condition->InputAt(0)->GetType();
4613 switch (cond_type) {
4614 default:
4615 cond_inverted = MaterializeIntLongCompare(if_cond,
4616 /* is64bit */ false,
4617 cond_locations,
4618 cond_reg);
4619 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004620 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004621 cond_inverted = MaterializeIntLongCompare(if_cond,
4622 /* is64bit */ true,
4623 cond_locations,
4624 cond_reg);
4625 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004626 case DataType::Type::kFloat32:
4627 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004628 cond_inverted = MaterializeFpCompare(if_cond,
4629 condition->IsGtBias(),
4630 cond_type,
4631 cond_locations,
4632 fcond_reg);
4633 break;
4634 }
4635 }
4636
4637 if (true_src.IsConstant()) {
4638 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4639 }
4640 if (false_src.IsConstant()) {
4641 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4642 }
4643
4644 switch (dst_type) {
4645 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004646 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004647 __ Mfc1(cond_reg, fcond_reg);
4648 }
4649 if (true_src.IsConstant()) {
4650 if (cond_inverted) {
4651 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4652 } else {
4653 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4654 }
4655 } else if (false_src.IsConstant()) {
4656 if (cond_inverted) {
4657 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4658 } else {
4659 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4660 }
4661 } else {
4662 DCHECK_NE(cond_reg, AT);
4663 if (cond_inverted) {
4664 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4665 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4666 } else {
4667 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4668 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4669 }
4670 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4671 }
4672 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004673 case DataType::Type::kFloat32: {
4674 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004675 // sel*.fmt tests bit 0 of the condition register, account for that.
4676 __ Sltu(TMP, ZERO, cond_reg);
4677 __ Mtc1(TMP, fcond_reg);
4678 }
4679 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4680 if (true_src.IsConstant()) {
4681 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4682 if (cond_inverted) {
4683 __ SelnezS(dst_reg, src_reg, fcond_reg);
4684 } else {
4685 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4686 }
4687 } else if (false_src.IsConstant()) {
4688 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4689 if (cond_inverted) {
4690 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4691 } else {
4692 __ SelnezS(dst_reg, src_reg, fcond_reg);
4693 }
4694 } else {
4695 if (cond_inverted) {
4696 __ SelS(fcond_reg,
4697 true_src.AsFpuRegister<FpuRegister>(),
4698 false_src.AsFpuRegister<FpuRegister>());
4699 } else {
4700 __ SelS(fcond_reg,
4701 false_src.AsFpuRegister<FpuRegister>(),
4702 true_src.AsFpuRegister<FpuRegister>());
4703 }
4704 __ MovS(dst_reg, fcond_reg);
4705 }
4706 break;
4707 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004708 case DataType::Type::kFloat64: {
4709 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004710 // sel*.fmt tests bit 0 of the condition register, account for that.
4711 __ Sltu(TMP, ZERO, cond_reg);
4712 __ Mtc1(TMP, fcond_reg);
4713 }
4714 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4715 if (true_src.IsConstant()) {
4716 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4717 if (cond_inverted) {
4718 __ SelnezD(dst_reg, src_reg, fcond_reg);
4719 } else {
4720 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4721 }
4722 } else if (false_src.IsConstant()) {
4723 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4724 if (cond_inverted) {
4725 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4726 } else {
4727 __ SelnezD(dst_reg, src_reg, fcond_reg);
4728 }
4729 } else {
4730 if (cond_inverted) {
4731 __ SelD(fcond_reg,
4732 true_src.AsFpuRegister<FpuRegister>(),
4733 false_src.AsFpuRegister<FpuRegister>());
4734 } else {
4735 __ SelD(fcond_reg,
4736 false_src.AsFpuRegister<FpuRegister>(),
4737 true_src.AsFpuRegister<FpuRegister>());
4738 }
4739 __ MovD(dst_reg, fcond_reg);
4740 }
4741 break;
4742 }
4743 }
4744}
4745
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004746void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004747 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004748 LocationSummary(flag, LocationSummary::kNoCall);
4749 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004750}
4751
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004752void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4753 __ LoadFromOffset(kLoadWord,
4754 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4755 SP,
4756 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004757}
4758
David Brazdil74eb1b22015-12-14 11:44:01 +00004759void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004760 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004761 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004762}
4763
4764void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004765 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4766 GenConditionalMove(select);
4767 } else {
4768 LocationSummary* locations = select->GetLocations();
4769 Mips64Label false_target;
4770 GenerateTestAndBranch(select,
4771 /* condition_input_index */ 2,
4772 /* true_target */ nullptr,
4773 &false_target);
4774 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4775 __ Bind(&false_target);
4776 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004777}
4778
David Srbecky0cf44932015-12-09 14:09:59 +00004779void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004780 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004781}
4782
David Srbeckyd28f4a02016-03-14 17:14:24 +00004783void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4784 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004785}
4786
4787void CodeGeneratorMIPS64::GenerateNop() {
4788 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004789}
4790
Alexey Frunze4dda3372015-06-01 18:31:49 -07004791void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004792 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004793 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004794 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004796 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004797 instruction,
4798 object_field_get_with_read_barrier
4799 ? LocationSummary::kCallOnSlowPath
4800 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004801 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4802 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4803 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004804 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004806 locations->SetOut(Location::RequiresFpuRegister());
4807 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004808 // The output overlaps in the case of an object field get with
4809 // read barriers enabled: we do not want the move to overwrite the
4810 // object's location, as we need it to emit the read barrier.
4811 locations->SetOut(Location::RequiresRegister(),
4812 object_field_get_with_read_barrier
4813 ? Location::kOutputOverlap
4814 : Location::kNoOutputOverlap);
4815 }
4816 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4817 // We need a temporary register for the read barrier marking slow
4818 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004819 if (!kBakerReadBarrierThunksEnableForFields) {
4820 locations->AddTemp(Location::RequiresRegister());
4821 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004822 }
4823}
4824
4825void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4826 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004827 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4828 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004829 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004830 Location obj_loc = locations->InAt(0);
4831 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4832 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004833 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004834 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004835 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004836 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4837
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004839 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004840 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004841 load_type = kLoadUnsignedByte;
4842 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004843 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004844 load_type = kLoadSignedByte;
4845 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004846 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004847 load_type = kLoadUnsignedHalfword;
4848 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004849 case DataType::Type::kInt16:
4850 load_type = kLoadSignedHalfword;
4851 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 case DataType::Type::kInt32:
4853 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004854 load_type = kLoadWord;
4855 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004856 case DataType::Type::kInt64:
4857 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004858 load_type = kLoadDoubleword;
4859 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004861 load_type = kLoadUnsignedWord;
4862 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004863 case DataType::Type::kUint32:
4864 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004866 LOG(FATAL) << "Unreachable type " << type;
4867 UNREACHABLE();
4868 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004870 DCHECK(dst_loc.IsRegister());
4871 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004872 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004873 // /* HeapReference<Object> */ dst = *(obj + offset)
4874 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004875 Location temp_loc =
4876 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004877 // Note that a potential implicit null check is handled in this
4878 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4879 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4880 dst_loc,
4881 obj,
4882 offset,
4883 temp_loc,
4884 /* needs_null_check */ true);
4885 if (is_volatile) {
4886 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4887 }
4888 } else {
4889 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4890 if (is_volatile) {
4891 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4892 }
4893 // If read barriers are enabled, emit read barriers other than
4894 // Baker's using a slow path (and also unpoison the loaded
4895 // reference, if heap poisoning is enabled).
4896 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4897 }
4898 } else {
4899 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4900 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004901 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004902 DCHECK(dst_loc.IsFpuRegister());
4903 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004904 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004905 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004906
Alexey Frunze15958152017-02-09 19:08:30 -08004907 // Memory barriers, in the case of references, are handled in the
4908 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004909 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004910 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004911 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004912}
4913
4914void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4915 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4916 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004917 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004918 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004919 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004920 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004921 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004922 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004923 }
4924}
4925
4926void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004927 const FieldInfo& field_info,
4928 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004929 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004930 LocationSummary* locations = instruction->GetLocations();
4931 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004932 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004933 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004934 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004935 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4936 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004937 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4938
Alexey Frunze4dda3372015-06-01 18:31:49 -07004939 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004940 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004941 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004942 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004943 store_type = kStoreByte;
4944 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004946 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004947 store_type = kStoreHalfword;
4948 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004949 case DataType::Type::kInt32:
4950 case DataType::Type::kFloat32:
4951 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004952 store_type = kStoreWord;
4953 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004954 case DataType::Type::kInt64:
4955 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004956 store_type = kStoreDoubleword;
4957 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004958 case DataType::Type::kUint32:
4959 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004960 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004961 LOG(FATAL) << "Unreachable type " << type;
4962 UNREACHABLE();
4963 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004964
Alexey Frunze15958152017-02-09 19:08:30 -08004965 if (is_volatile) {
4966 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4967 }
4968
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004969 if (value_location.IsConstant()) {
4970 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4971 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4972 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004974 DCHECK(value_location.IsRegister());
4975 GpuRegister src = value_location.AsRegister<GpuRegister>();
4976 if (kPoisonHeapReferences && needs_write_barrier) {
4977 // Note that in the case where `value` is a null reference,
4978 // we do not enter this block, as a null reference does not
4979 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004980 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004981 __ PoisonHeapReference(TMP, src);
4982 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4983 } else {
4984 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4985 }
4986 } else {
4987 DCHECK(value_location.IsFpuRegister());
4988 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4989 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4990 }
4991 }
Alexey Frunze15958152017-02-09 19:08:30 -08004992
Alexey Frunzec061de12017-02-14 13:27:23 -08004993 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004994 DCHECK(value_location.IsRegister());
4995 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004996 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004997 }
Alexey Frunze15958152017-02-09 19:08:30 -08004998
4999 if (is_volatile) {
5000 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
5001 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005002}
5003
5004void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5005 HandleFieldGet(instruction, instruction->GetFieldInfo());
5006}
5007
5008void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5009 HandleFieldGet(instruction, instruction->GetFieldInfo());
5010}
5011
5012void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5013 HandleFieldSet(instruction, instruction->GetFieldInfo());
5014}
5015
5016void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005017 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005018}
5019
Alexey Frunze15958152017-02-09 19:08:30 -08005020void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
5021 HInstruction* instruction,
5022 Location out,
5023 uint32_t offset,
5024 Location maybe_temp,
5025 ReadBarrierOption read_barrier_option) {
5026 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5027 if (read_barrier_option == kWithReadBarrier) {
5028 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005029 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
5030 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5031 }
Alexey Frunze15958152017-02-09 19:08:30 -08005032 if (kUseBakerReadBarrier) {
5033 // Load with fast path based Baker's read barrier.
5034 // /* HeapReference<Object> */ out = *(out + offset)
5035 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5036 out,
5037 out_reg,
5038 offset,
5039 maybe_temp,
5040 /* needs_null_check */ false);
5041 } else {
5042 // Load with slow path based read barrier.
5043 // Save the value of `out` into `maybe_temp` before overwriting it
5044 // in the following move operation, as we will need it for the
5045 // read barrier below.
5046 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
5047 // /* HeapReference<Object> */ out = *(out + offset)
5048 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5049 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5050 }
5051 } else {
5052 // Plain load with no read barrier.
5053 // /* HeapReference<Object> */ out = *(out + offset)
5054 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5055 __ MaybeUnpoisonHeapReference(out_reg);
5056 }
5057}
5058
5059void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
5060 HInstruction* instruction,
5061 Location out,
5062 Location obj,
5063 uint32_t offset,
5064 Location maybe_temp,
5065 ReadBarrierOption read_barrier_option) {
5066 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5067 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
5068 if (read_barrier_option == kWithReadBarrier) {
5069 CHECK(kEmitCompilerReadBarrier);
5070 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005071 if (!kBakerReadBarrierThunksEnableForFields) {
5072 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5073 }
Alexey Frunze15958152017-02-09 19:08:30 -08005074 // Load with fast path based Baker's read barrier.
5075 // /* HeapReference<Object> */ out = *(obj + offset)
5076 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5077 out,
5078 obj_reg,
5079 offset,
5080 maybe_temp,
5081 /* needs_null_check */ false);
5082 } else {
5083 // Load with slow path based read barrier.
5084 // /* HeapReference<Object> */ out = *(obj + offset)
5085 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5086 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5087 }
5088 } else {
5089 // Plain load with no read barrier.
5090 // /* HeapReference<Object> */ out = *(obj + offset)
5091 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5092 __ MaybeUnpoisonHeapReference(out_reg);
5093 }
5094}
5095
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005096static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5097 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5098 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5099 return reg - V0;
5100 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5101 return 13 + (reg - S2);
5102 } else if (reg == S8) { // One more.
5103 return 19;
5104 }
5105 LOG(FATAL) << "Unexpected register " << reg;
5106 UNREACHABLE();
5107}
5108
5109static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5110 int num = GetBakerMarkThunkNumber(reg) +
5111 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5112 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5113}
5114
5115static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5116 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5117 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5118}
5119
5120void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5121 Location root,
5122 GpuRegister obj,
5123 uint32_t offset,
5124 ReadBarrierOption read_barrier_option,
5125 Mips64Label* label_low) {
5126 if (label_low != nullptr) {
5127 DCHECK_EQ(offset, 0x5678u);
5128 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005129 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005130 if (read_barrier_option == kWithReadBarrier) {
5131 DCHECK(kEmitCompilerReadBarrier);
5132 if (kUseBakerReadBarrier) {
5133 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5134 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005135 if (kBakerReadBarrierThunksEnableForGcRoots) {
5136 // Note that we do not actually check the value of `GetIsGcMarking()`
5137 // to decide whether to mark the loaded GC root or not. Instead, we
5138 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5139 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5140 // vice versa.
5141 //
5142 // We use thunks for the slow path. That thunk checks the reference
5143 // and jumps to the entrypoint if needed.
5144 //
5145 // temp = Thread::Current()->pReadBarrierMarkReg00
5146 // // AKA &art_quick_read_barrier_mark_introspection.
5147 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5148 // if (temp != nullptr) {
5149 // temp = &gc_root_thunk<root_reg>
5150 // root = temp(root)
5151 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005152
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005153 const int32_t entry_point_offset =
5154 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5155 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5156 int16_t offset_low = Low16Bits(offset);
5157 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5158 // extension in lwu.
5159 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5160 GpuRegister base = short_offset ? obj : TMP;
5161 // Loading the entrypoint does not require a load acquire since it is only changed when
5162 // threads are suspended or running a checkpoint.
5163 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5164 if (!short_offset) {
5165 DCHECK(!label_low);
5166 __ Daui(base, obj, offset_high);
5167 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005168 Mips64Label skip_call;
5169 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005170 if (label_low != nullptr) {
5171 DCHECK(short_offset);
5172 __ Bind(label_low);
5173 }
5174 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5175 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5176 // in delay slot.
5177 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005178 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005179 } else {
5180 // Note that we do not actually check the value of `GetIsGcMarking()`
5181 // to decide whether to mark the loaded GC root or not. Instead, we
5182 // load into `temp` (T9) the read barrier mark entry point corresponding
5183 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5184 // is false, and vice versa.
5185 //
5186 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5187 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5188 // if (temp != null) {
5189 // root = temp(root)
5190 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005191
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005192 if (label_low != nullptr) {
5193 __ Bind(label_low);
5194 }
5195 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5196 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5197 static_assert(
5198 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5199 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5200 "have different sizes.");
5201 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5202 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5203 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005204
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005205 // Slow path marking the GC root `root`.
5206 Location temp = Location::RegisterLocation(T9);
5207 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005208 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005209 instruction,
5210 root,
5211 /*entrypoint*/ temp);
5212 codegen_->AddSlowPath(slow_path);
5213
5214 const int32_t entry_point_offset =
5215 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5216 // Loading the entrypoint does not require a load acquire since it is only changed when
5217 // threads are suspended or running a checkpoint.
5218 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5219 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5220 __ Bind(slow_path->GetExitLabel());
5221 }
Alexey Frunze15958152017-02-09 19:08:30 -08005222 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005223 if (label_low != nullptr) {
5224 __ Bind(label_low);
5225 }
Alexey Frunze15958152017-02-09 19:08:30 -08005226 // GC root loaded through a slow path for read barriers other
5227 // than Baker's.
5228 // /* GcRoot<mirror::Object>* */ root = obj + offset
5229 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5230 // /* mirror::Object* */ root = root->Read()
5231 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5232 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005233 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005234 if (label_low != nullptr) {
5235 __ Bind(label_low);
5236 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005237 // Plain GC root load with no read barrier.
5238 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5239 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5240 // Note that GC roots are not affected by heap poisoning, thus we
5241 // do not have to unpoison `root_reg` here.
5242 }
5243}
5244
Alexey Frunze15958152017-02-09 19:08:30 -08005245void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5246 Location ref,
5247 GpuRegister obj,
5248 uint32_t offset,
5249 Location temp,
5250 bool needs_null_check) {
5251 DCHECK(kEmitCompilerReadBarrier);
5252 DCHECK(kUseBakerReadBarrier);
5253
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005254 if (kBakerReadBarrierThunksEnableForFields) {
5255 // Note that we do not actually check the value of `GetIsGcMarking()`
5256 // to decide whether to mark the loaded reference or not. Instead, we
5257 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5258 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5259 // vice versa.
5260 //
5261 // We use thunks for the slow path. That thunk checks the reference
5262 // and jumps to the entrypoint if needed. If the holder is not gray,
5263 // it issues a load-load memory barrier and returns to the original
5264 // reference load.
5265 //
5266 // temp = Thread::Current()->pReadBarrierMarkReg00
5267 // // AKA &art_quick_read_barrier_mark_introspection.
5268 // if (temp != nullptr) {
5269 // temp = &field_array_thunk<holder_reg>
5270 // temp()
5271 // }
5272 // not_gray_return_address:
5273 // // If the offset is too large to fit into the lw instruction, we
5274 // // use an adjusted base register (TMP) here. This register
5275 // // receives bits 16 ... 31 of the offset before the thunk invocation
5276 // // and the thunk benefits from it.
5277 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5278 // gray_return_address:
5279
5280 DCHECK(temp.IsInvalid());
5281 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5282 const int32_t entry_point_offset =
5283 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5284 // There may have or may have not been a null check if the field offset is smaller than
5285 // the page size.
5286 // There must've been a null check in case it's actually a load from an array.
5287 // We will, however, perform an explicit null check in the thunk as it's easier to
5288 // do it than not.
5289 if (instruction->IsArrayGet()) {
5290 DCHECK(!needs_null_check);
5291 }
5292 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5293 // Loading the entrypoint does not require a load acquire since it is only changed when
5294 // threads are suspended or running a checkpoint.
5295 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5296 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005297 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005298 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005299 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005300 __ Nop(); // In forbidden slot.
5301 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005302 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005303 // /* HeapReference<Object> */ ref = *(obj + offset)
5304 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5305 } else {
5306 int16_t offset_low = Low16Bits(offset);
5307 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005308 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005309 __ Daui(TMP, obj, offset_high); // In delay slot.
5310 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005311 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005312 // /* HeapReference<Object> */ ref = *(obj + offset)
5313 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5314 }
5315 if (needs_null_check) {
5316 MaybeRecordImplicitNullCheck(instruction);
5317 }
5318 __ MaybeUnpoisonHeapReference(ref_reg);
5319 return;
5320 }
5321
Alexey Frunze15958152017-02-09 19:08:30 -08005322 // /* HeapReference<Object> */ ref = *(obj + offset)
5323 Location no_index = Location::NoLocation();
5324 ScaleFactor no_scale_factor = TIMES_1;
5325 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5326 ref,
5327 obj,
5328 offset,
5329 no_index,
5330 no_scale_factor,
5331 temp,
5332 needs_null_check);
5333}
5334
5335void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5336 Location ref,
5337 GpuRegister obj,
5338 uint32_t data_offset,
5339 Location index,
5340 Location temp,
5341 bool needs_null_check) {
5342 DCHECK(kEmitCompilerReadBarrier);
5343 DCHECK(kUseBakerReadBarrier);
5344
5345 static_assert(
5346 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5347 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005348 ScaleFactor scale_factor = TIMES_4;
5349
5350 if (kBakerReadBarrierThunksEnableForArrays) {
5351 // Note that we do not actually check the value of `GetIsGcMarking()`
5352 // to decide whether to mark the loaded reference or not. Instead, we
5353 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5354 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5355 // vice versa.
5356 //
5357 // We use thunks for the slow path. That thunk checks the reference
5358 // and jumps to the entrypoint if needed. If the holder is not gray,
5359 // it issues a load-load memory barrier and returns to the original
5360 // reference load.
5361 //
5362 // temp = Thread::Current()->pReadBarrierMarkReg00
5363 // // AKA &art_quick_read_barrier_mark_introspection.
5364 // if (temp != nullptr) {
5365 // temp = &field_array_thunk<holder_reg>
5366 // temp()
5367 // }
5368 // not_gray_return_address:
5369 // // The element address is pre-calculated in the TMP register before the
5370 // // thunk invocation and the thunk benefits from it.
5371 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5372 // gray_return_address:
5373
5374 DCHECK(temp.IsInvalid());
5375 DCHECK(index.IsValid());
5376 const int32_t entry_point_offset =
5377 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5378 // We will not do the explicit null check in the thunk as some form of a null check
5379 // must've been done earlier.
5380 DCHECK(!needs_null_check);
5381 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5382 // Loading the entrypoint does not require a load acquire since it is only changed when
5383 // threads are suspended or running a checkpoint.
5384 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005385 Mips64Label skip_call;
5386 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005387 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5388 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5389 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5390 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005391 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005392 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5393 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5394 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5395 __ MaybeUnpoisonHeapReference(ref_reg);
5396 return;
5397 }
5398
Alexey Frunze15958152017-02-09 19:08:30 -08005399 // /* HeapReference<Object> */ ref =
5400 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005401 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5402 ref,
5403 obj,
5404 data_offset,
5405 index,
5406 scale_factor,
5407 temp,
5408 needs_null_check);
5409}
5410
5411void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5412 Location ref,
5413 GpuRegister obj,
5414 uint32_t offset,
5415 Location index,
5416 ScaleFactor scale_factor,
5417 Location temp,
5418 bool needs_null_check,
5419 bool always_update_field) {
5420 DCHECK(kEmitCompilerReadBarrier);
5421 DCHECK(kUseBakerReadBarrier);
5422
5423 // In slow path based read barriers, the read barrier call is
5424 // inserted after the original load. However, in fast path based
5425 // Baker's read barriers, we need to perform the load of
5426 // mirror::Object::monitor_ *before* the original reference load.
5427 // This load-load ordering is required by the read barrier.
5428 // The fast path/slow path (for Baker's algorithm) should look like:
5429 //
5430 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5431 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5432 // HeapReference<Object> ref = *src; // Original reference load.
5433 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5434 // if (is_gray) {
5435 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5436 // }
5437 //
5438 // Note: the original implementation in ReadBarrier::Barrier is
5439 // slightly more complex as it performs additional checks that we do
5440 // not do here for performance reasons.
5441
5442 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5443 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5444 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5445
5446 // /* int32_t */ monitor = obj->monitor_
5447 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5448 if (needs_null_check) {
5449 MaybeRecordImplicitNullCheck(instruction);
5450 }
5451 // /* LockWord */ lock_word = LockWord(monitor)
5452 static_assert(sizeof(LockWord) == sizeof(int32_t),
5453 "art::LockWord and int32_t have different sizes.");
5454
5455 __ Sync(0); // Barrier to prevent load-load reordering.
5456
5457 // The actual reference load.
5458 if (index.IsValid()) {
5459 // Load types involving an "index": ArrayGet,
5460 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5461 // intrinsics.
5462 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5463 if (index.IsConstant()) {
5464 size_t computed_offset =
5465 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5466 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5467 } else {
5468 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005469 if (scale_factor == TIMES_1) {
5470 __ Daddu(TMP, index_reg, obj);
5471 } else {
5472 __ Dlsa(TMP, index_reg, obj, scale_factor);
5473 }
Alexey Frunze15958152017-02-09 19:08:30 -08005474 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5475 }
5476 } else {
5477 // /* HeapReference<Object> */ ref = *(obj + offset)
5478 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5479 }
5480
5481 // Object* ref = ref_addr->AsMirrorPtr()
5482 __ MaybeUnpoisonHeapReference(ref_reg);
5483
5484 // Slow path marking the object `ref` when it is gray.
5485 SlowPathCodeMIPS64* slow_path;
5486 if (always_update_field) {
5487 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5488 // of the form `obj + field_offset`, where `obj` is a register and
5489 // `field_offset` is a register. Thus `offset` and `scale_factor`
5490 // above are expected to be null in this code path.
5491 DCHECK_EQ(offset, 0u);
5492 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005493 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005494 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5495 ref,
5496 obj,
5497 /* field_offset */ index,
5498 temp_reg);
5499 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005500 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005501 }
5502 AddSlowPath(slow_path);
5503
5504 // if (rb_state == ReadBarrier::GrayState())
5505 // ref = ReadBarrier::Mark(ref);
5506 // Given the numeric representation, it's enough to check the low bit of the
5507 // rb_state. We do that by shifting the bit into the sign bit (31) and
5508 // performing a branch on less than zero.
5509 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5510 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5511 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5512 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5513 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5514 __ Bind(slow_path->GetExitLabel());
5515}
5516
5517void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5518 Location out,
5519 Location ref,
5520 Location obj,
5521 uint32_t offset,
5522 Location index) {
5523 DCHECK(kEmitCompilerReadBarrier);
5524
5525 // Insert a slow path based read barrier *after* the reference load.
5526 //
5527 // If heap poisoning is enabled, the unpoisoning of the loaded
5528 // reference will be carried out by the runtime within the slow
5529 // path.
5530 //
5531 // Note that `ref` currently does not get unpoisoned (when heap
5532 // poisoning is enabled), which is alright as the `ref` argument is
5533 // not used by the artReadBarrierSlow entry point.
5534 //
5535 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005536 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005537 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5538 AddSlowPath(slow_path);
5539
5540 __ Bc(slow_path->GetEntryLabel());
5541 __ Bind(slow_path->GetExitLabel());
5542}
5543
5544void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5545 Location out,
5546 Location ref,
5547 Location obj,
5548 uint32_t offset,
5549 Location index) {
5550 if (kEmitCompilerReadBarrier) {
5551 // Baker's read barriers shall be handled by the fast path
5552 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5553 DCHECK(!kUseBakerReadBarrier);
5554 // If heap poisoning is enabled, unpoisoning will be taken care of
5555 // by the runtime within the slow path.
5556 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5557 } else if (kPoisonHeapReferences) {
5558 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5559 }
5560}
5561
5562void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5563 Location out,
5564 Location root) {
5565 DCHECK(kEmitCompilerReadBarrier);
5566
5567 // Insert a slow path based read barrier *after* the GC root load.
5568 //
5569 // Note that GC roots are not affected by heap poisoning, so we do
5570 // not need to do anything special for this here.
5571 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005572 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005573 AddSlowPath(slow_path);
5574
5575 __ Bc(slow_path->GetEntryLabel());
5576 __ Bind(slow_path->GetExitLabel());
5577}
5578
Alexey Frunze4dda3372015-06-01 18:31:49 -07005579void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005580 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5581 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005582 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005583 switch (type_check_kind) {
5584 case TypeCheckKind::kExactCheck:
5585 case TypeCheckKind::kAbstractClassCheck:
5586 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005587 case TypeCheckKind::kArrayObjectCheck: {
5588 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5589 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5590 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005591 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005592 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005593 case TypeCheckKind::kArrayCheck:
5594 case TypeCheckKind::kUnresolvedCheck:
5595 case TypeCheckKind::kInterfaceCheck:
5596 call_kind = LocationSummary::kCallOnSlowPath;
5597 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00005598 case TypeCheckKind::kBitstringCheck:
5599 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005600 }
5601
Vladimir Markoca6fff82017-10-03 14:49:14 +01005602 LocationSummary* locations =
5603 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005604 if (baker_read_barrier_slow_path) {
5605 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5606 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005607 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00005608 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
5609 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
5610 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
5611 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
5612 } else {
5613 locations->SetInAt(1, Location::RequiresRegister());
5614 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005615 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005616 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005617 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005618 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005619}
5620
5621void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005622 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005623 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005624 Location obj_loc = locations->InAt(0);
5625 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00005626 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08005627 Location out_loc = locations->Out();
5628 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5629 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5630 DCHECK_LE(num_temps, 1u);
5631 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005632 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5633 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5634 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5635 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005636 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005637 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005638
5639 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005640 // Avoid this check if we know `obj` is not null.
5641 if (instruction->MustDoNullCheck()) {
5642 __ Move(out, ZERO);
5643 __ Beqzc(obj, &done);
5644 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005645
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005646 switch (type_check_kind) {
5647 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005648 ReadBarrierOption read_barrier_option =
5649 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005650 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005651 GenerateReferenceLoadTwoRegisters(instruction,
5652 out_loc,
5653 obj_loc,
5654 class_offset,
5655 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005656 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005657 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00005658 __ Xor(out, out, cls.AsRegister<GpuRegister>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005659 __ Sltiu(out, out, 1);
5660 break;
5661 }
5662
5663 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005664 ReadBarrierOption read_barrier_option =
5665 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005666 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005667 GenerateReferenceLoadTwoRegisters(instruction,
5668 out_loc,
5669 obj_loc,
5670 class_offset,
5671 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005672 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005673 // If the class is abstract, we eagerly fetch the super class of the
5674 // object to avoid doing a comparison we know will fail.
5675 Mips64Label loop;
5676 __ Bind(&loop);
5677 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005678 GenerateReferenceLoadOneRegister(instruction,
5679 out_loc,
5680 super_offset,
5681 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005682 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005683 // If `out` is null, we use it for the result, and jump to `done`.
5684 __ Beqzc(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00005685 __ Bnec(out, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005686 __ LoadConst32(out, 1);
5687 break;
5688 }
5689
5690 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005691 ReadBarrierOption read_barrier_option =
5692 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005693 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005694 GenerateReferenceLoadTwoRegisters(instruction,
5695 out_loc,
5696 obj_loc,
5697 class_offset,
5698 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005699 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005700 // Walk over the class hierarchy to find a match.
5701 Mips64Label loop, success;
5702 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00005703 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005704 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005705 GenerateReferenceLoadOneRegister(instruction,
5706 out_loc,
5707 super_offset,
5708 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005709 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005710 __ Bnezc(out, &loop);
5711 // If `out` is null, we use it for the result, and jump to `done`.
5712 __ Bc(&done);
5713 __ Bind(&success);
5714 __ LoadConst32(out, 1);
5715 break;
5716 }
5717
5718 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005719 ReadBarrierOption read_barrier_option =
5720 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005721 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005722 GenerateReferenceLoadTwoRegisters(instruction,
5723 out_loc,
5724 obj_loc,
5725 class_offset,
5726 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005727 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005728 // Do an exact check.
5729 Mips64Label success;
Vladimir Marko175e7862018-03-27 09:03:13 +00005730 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005731 // Otherwise, we need to check that the object's class is a non-primitive array.
5732 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005733 GenerateReferenceLoadOneRegister(instruction,
5734 out_loc,
5735 component_offset,
5736 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005737 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005738 // If `out` is null, we use it for the result, and jump to `done`.
5739 __ Beqzc(out, &done);
5740 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5741 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5742 __ Sltiu(out, out, 1);
5743 __ Bc(&done);
5744 __ Bind(&success);
5745 __ LoadConst32(out, 1);
5746 break;
5747 }
5748
5749 case TypeCheckKind::kArrayCheck: {
5750 // No read barrier since the slow path will retry upon failure.
5751 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005752 GenerateReferenceLoadTwoRegisters(instruction,
5753 out_loc,
5754 obj_loc,
5755 class_offset,
5756 maybe_temp_loc,
5757 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005758 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005759 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5760 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005761 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00005762 __ Bnec(out, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005763 __ LoadConst32(out, 1);
5764 break;
5765 }
5766
5767 case TypeCheckKind::kUnresolvedCheck:
5768 case TypeCheckKind::kInterfaceCheck: {
5769 // Note that we indeed only call on slow path, but we always go
5770 // into the slow path for the unresolved and interface check
5771 // cases.
5772 //
5773 // We cannot directly call the InstanceofNonTrivial runtime
5774 // entry point without resorting to a type checking slow path
5775 // here (i.e. by calling InvokeRuntime directly), as it would
5776 // require to assign fixed registers for the inputs of this
5777 // HInstanceOf instruction (following the runtime calling
5778 // convention), which might be cluttered by the potential first
5779 // read barrier emission at the beginning of this method.
5780 //
5781 // TODO: Introduce a new runtime entry point taking the object
5782 // to test (instead of its class) as argument, and let it deal
5783 // with the read barrier issues. This will let us refactor this
5784 // case of the `switch` code as it was previously (with a direct
5785 // call to the runtime not using a type checking slow path).
5786 // This should also be beneficial for the other cases above.
5787 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005788 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5789 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005790 codegen_->AddSlowPath(slow_path);
5791 __ Bc(slow_path->GetEntryLabel());
5792 break;
5793 }
Vladimir Marko175e7862018-03-27 09:03:13 +00005794
5795 case TypeCheckKind::kBitstringCheck: {
5796 // /* HeapReference<Class> */ temp = obj->klass_
5797 GenerateReferenceLoadTwoRegisters(instruction,
5798 out_loc,
5799 obj_loc,
5800 class_offset,
5801 maybe_temp_loc,
5802 kWithoutReadBarrier);
5803
5804 GenerateBitstringTypeCheckCompare(instruction, out);
5805 __ Sltiu(out, out, 1);
5806 break;
5807 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005808 }
5809
5810 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005811
5812 if (slow_path != nullptr) {
5813 __ Bind(slow_path->GetExitLabel());
5814 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005815}
5816
5817void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005818 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005819 locations->SetOut(Location::ConstantLocation(constant));
5820}
5821
5822void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5823 // Will be generated at use site.
5824}
5825
5826void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005827 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005828 locations->SetOut(Location::ConstantLocation(constant));
5829}
5830
5831void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5832 // Will be generated at use site.
5833}
5834
Calin Juravle175dc732015-08-25 15:42:32 +01005835void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5836 // The trampoline uses the same calling convention as dex calling conventions,
5837 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5838 // the method_idx.
5839 HandleInvoke(invoke);
5840}
5841
5842void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5843 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5844}
5845
Alexey Frunze4dda3372015-06-01 18:31:49 -07005846void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5847 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5848 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5849}
5850
5851void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5852 HandleInvoke(invoke);
5853 // The register T0 is required to be used for the hidden argument in
5854 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5855 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5856}
5857
5858void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5859 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5860 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005861 Location receiver = invoke->GetLocations()->InAt(0);
5862 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005863 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005864
5865 // Set the hidden argument.
5866 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5867 invoke->GetDexMethodIndex());
5868
5869 // temp = object->GetClass();
5870 if (receiver.IsStackSlot()) {
5871 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5872 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5873 } else {
5874 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5875 }
5876 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005877 // Instead of simply (possibly) unpoisoning `temp` here, we should
5878 // emit a read barrier for the previous class reference load.
5879 // However this is not required in practice, as this is an
5880 // intermediate/temporary reference and because the current
5881 // concurrent copying collector keeps the from-space memory
5882 // intact/accessible until the end of the marking phase (the
5883 // concurrent copying collector may not in the future).
5884 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005885 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5886 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5887 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005888 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005889 // temp = temp->GetImtEntryAt(method_offset);
5890 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5891 // T9 = temp->GetEntryPoint();
5892 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5893 // T9();
5894 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005895 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005896 DCHECK(!codegen_->IsLeafMethod());
5897 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5898}
5899
5900void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005901 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5902 if (intrinsic.TryDispatch(invoke)) {
5903 return;
5904 }
5905
Alexey Frunze4dda3372015-06-01 18:31:49 -07005906 HandleInvoke(invoke);
5907}
5908
5909void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005910 // Explicit clinit checks triggered by static invokes must have been pruned by
5911 // art::PrepareForRegisterAllocation.
5912 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005913
Chris Larsen3039e382015-08-26 07:54:08 -07005914 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5915 if (intrinsic.TryDispatch(invoke)) {
5916 return;
5917 }
5918
Alexey Frunze4dda3372015-06-01 18:31:49 -07005919 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005920}
5921
Orion Hodsonac141392017-01-13 11:53:47 +00005922void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5923 HandleInvoke(invoke);
5924}
5925
5926void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5927 codegen_->GenerateInvokePolymorphicCall(invoke);
5928}
5929
Orion Hodson4c8e12e2018-05-18 08:33:20 +01005930void LocationsBuilderMIPS64::VisitInvokeCustom(HInvokeCustom* invoke) {
5931 HandleInvoke(invoke);
5932}
5933
5934void InstructionCodeGeneratorMIPS64::VisitInvokeCustom(HInvokeCustom* invoke) {
5935 codegen_->GenerateInvokeCustomCall(invoke);
5936}
5937
Chris Larsen3039e382015-08-26 07:54:08 -07005938static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005939 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005940 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5941 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005942 return true;
5943 }
5944 return false;
5945}
5946
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005947HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005948 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005949 bool fallback_load = false;
5950 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005951 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005952 case HLoadString::LoadKind::kBootImageRelRo:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005953 case HLoadString::LoadKind::kBssEntry:
5954 DCHECK(!Runtime::Current()->UseJitCompilation());
5955 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005956 case HLoadString::LoadKind::kJitTableAddress:
5957 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005958 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005959 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005960 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005961 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005962 }
5963 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005964 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005965 }
5966 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967}
5968
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005969HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5970 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005971 bool fallback_load = false;
5972 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005973 case HLoadClass::LoadKind::kInvalid:
5974 LOG(FATAL) << "UNREACHABLE";
5975 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005976 case HLoadClass::LoadKind::kReferrersClass:
5977 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005978 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005979 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005980 case HLoadClass::LoadKind::kBssEntry:
5981 DCHECK(!Runtime::Current()->UseJitCompilation());
5982 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005983 case HLoadClass::LoadKind::kJitTableAddress:
5984 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005985 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005986 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005987 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005988 break;
5989 }
5990 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005991 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005992 }
5993 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005994}
5995
Vladimir Markodc151b22015-10-15 18:02:30 +01005996HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5997 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005998 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005999 // On MIPS64 we support all dispatch types.
6000 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006001}
6002
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006003void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
6004 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006005 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00006006 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08006007 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
6008 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
6009
Alexey Frunze19f6c692016-11-30 19:19:55 -08006010 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006011 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00006012 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006013 uint32_t offset =
6014 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00006015 __ LoadFromOffset(kLoadDoubleword,
6016 temp.AsRegister<GpuRegister>(),
6017 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006018 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00006019 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006020 }
Vladimir Marko58155012015-08-19 12:49:41 +00006021 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006022 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006023 break;
Vladimir Marko65979462017-05-19 17:25:12 +01006024 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
6025 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006026 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006027 NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006028 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006029 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006030 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01006031 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6032 break;
6033 }
Vladimir Marko58155012015-08-19 12:49:41 +00006034 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006035 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
6036 kLoadDoubleword,
6037 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00006038 break;
Vladimir Markob066d432018-01-03 13:14:37 +00006039 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006040 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00006041 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
6042 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
6043 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6044 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
6045 __ Lwu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6046 break;
6047 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006048 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006049 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006050 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006051 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
6052 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
6053 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08006054 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6055 break;
6056 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006057 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
6058 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
6059 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07006060 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006061 }
6062
Alexey Frunze19f6c692016-11-30 19:19:55 -08006063 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00006064 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006065 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00006066 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006067 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6068 // T9 = callee_method->entry_point_from_quick_compiled_code_;
6069 __ LoadFromOffset(kLoadDoubleword,
6070 T9,
6071 callee_method.AsRegister<GpuRegister>(),
6072 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006073 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006074 // T9()
6075 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006076 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00006077 break;
6078 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006079 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
6080
Alexey Frunze4dda3372015-06-01 18:31:49 -07006081 DCHECK(!IsLeafMethod());
6082}
6083
6084void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006085 // Explicit clinit checks triggered by static invokes must have been pruned by
6086 // art::PrepareForRegisterAllocation.
6087 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006088
6089 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6090 return;
6091 }
6092
6093 LocationSummary* locations = invoke->GetLocations();
6094 codegen_->GenerateStaticOrDirectCall(invoke,
6095 locations->HasTemps()
6096 ? locations->GetTemp(0)
6097 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006098}
6099
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006100void CodeGeneratorMIPS64::GenerateVirtualCall(
6101 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006102 // Use the calling convention instead of the location of the receiver, as
6103 // intrinsics may have put the receiver in a different register. In the intrinsics
6104 // slow path, the arguments have been moved to the right place, so here we are
6105 // guaranteed that the receiver is the first register of the calling convention.
6106 InvokeDexCallingConvention calling_convention;
6107 GpuRegister receiver = calling_convention.GetRegisterAt(0);
6108
Alexey Frunze53afca12015-11-05 16:34:23 -08006109 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006110 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6111 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
6112 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006113 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006114
6115 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006116 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08006117 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006118 // Instead of simply (possibly) unpoisoning `temp` here, we should
6119 // emit a read barrier for the previous class reference load.
6120 // However this is not required in practice, as this is an
6121 // intermediate/temporary reference and because the current
6122 // concurrent copying collector keeps the from-space memory
6123 // intact/accessible until the end of the marking phase (the
6124 // concurrent copying collector may not in the future).
6125 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006126 // temp = temp->GetMethodAt(method_offset);
6127 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6128 // T9 = temp->GetEntryPoint();
6129 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6130 // T9();
6131 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006132 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006133 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006134}
6135
6136void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6137 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6138 return;
6139 }
6140
6141 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006142 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006143}
6144
6145void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006146 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006147 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006148 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006149 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6150 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006151 return;
6152 }
Vladimir Marko41559982017-01-06 14:04:23 +00006153 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006154
Alexey Frunze15958152017-02-09 19:08:30 -08006155 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6156 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006157 ? LocationSummary::kCallOnSlowPath
6158 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006159 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006160 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6161 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6162 }
Vladimir Marko41559982017-01-06 14:04:23 +00006163 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006164 locations->SetInAt(0, Location::RequiresRegister());
6165 }
6166 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006167 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6168 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6169 // Rely on the type resolution or initialization and marking to save everything we need.
6170 RegisterSet caller_saves = RegisterSet::Empty();
6171 InvokeRuntimeCallingConvention calling_convention;
6172 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6173 locations->SetCustomSlowPathCallerSaves(caller_saves);
6174 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006175 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006176 }
6177 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006178}
6179
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006180// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6181// move.
6182void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006183 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006184 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006185 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006186 return;
6187 }
Vladimir Marko41559982017-01-06 14:04:23 +00006188 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006189
Vladimir Marko41559982017-01-06 14:04:23 +00006190 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006191 Location out_loc = locations->Out();
6192 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6193 GpuRegister current_method_reg = ZERO;
6194 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006195 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006196 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6197 }
6198
Alexey Frunze15958152017-02-09 19:08:30 -08006199 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6200 ? kWithoutReadBarrier
6201 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006202 bool generate_null_check = false;
6203 switch (load_kind) {
6204 case HLoadClass::LoadKind::kReferrersClass:
6205 DCHECK(!cls->CanCallRuntime());
6206 DCHECK(!cls->MustGenerateClinitCheck());
6207 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6208 GenerateGcRootFieldLoad(cls,
6209 out_loc,
6210 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006211 ArtMethod::DeclaringClassOffset().Int32Value(),
6212 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006213 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006214 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006215 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006216 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006217 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006218 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006219 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006220 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006221 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006222 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6223 break;
6224 }
6225 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006226 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006227 uint32_t address = dchecked_integral_cast<uint32_t>(
6228 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6229 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006230 __ LoadLiteral(out,
6231 kLoadUnsignedWord,
6232 codegen_->DeduplicateBootImageAddressLiteral(address));
6233 break;
6234 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006235 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006236 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006237 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006238 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006239 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006240 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006241 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006242 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6243 __ Lwu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006244 break;
6245 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006246 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006247 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6248 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006249 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6250 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006251 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006252 GenerateGcRootFieldLoad(cls,
6253 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006254 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006255 /* placeholder */ 0x5678,
6256 read_barrier_option,
6257 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006258 generate_null_check = true;
6259 break;
6260 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006261 case HLoadClass::LoadKind::kJitTableAddress:
6262 __ LoadLiteral(out,
6263 kLoadUnsignedWord,
6264 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6265 cls->GetTypeIndex(),
6266 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006267 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006268 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006269 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006270 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006271 LOG(FATAL) << "UNREACHABLE";
6272 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006273 }
6274
6275 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6276 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006277 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006278 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006279 codegen_->AddSlowPath(slow_path);
6280 if (generate_null_check) {
6281 __ Beqzc(out, slow_path->GetEntryLabel());
6282 }
6283 if (cls->MustGenerateClinitCheck()) {
6284 GenerateClassInitializationCheck(slow_path, out);
6285 } else {
6286 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006287 }
6288 }
6289}
6290
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006291void LocationsBuilderMIPS64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6292 InvokeRuntimeCallingConvention calling_convention;
6293 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6294 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
6295}
6296
6297void InstructionCodeGeneratorMIPS64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6298 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6299}
6300
Orion Hodson18259d72018-04-12 11:18:23 +01006301void LocationsBuilderMIPS64::VisitLoadMethodType(HLoadMethodType* load) {
6302 InvokeRuntimeCallingConvention calling_convention;
6303 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6304 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
6305}
6306
6307void InstructionCodeGeneratorMIPS64::VisitLoadMethodType(HLoadMethodType* load) {
6308 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6309}
6310
David Brazdilcb1c0552015-08-04 16:22:25 +01006311static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006312 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006313}
6314
Alexey Frunze4dda3372015-06-01 18:31:49 -07006315void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6316 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006317 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006318 locations->SetOut(Location::RequiresRegister());
6319}
6320
6321void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6322 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006323 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6324}
6325
6326void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006327 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006328}
6329
6330void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6331 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006332}
6333
Alexey Frunze4dda3372015-06-01 18:31:49 -07006334void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006335 HLoadString::LoadKind load_kind = load->GetLoadKind();
6336 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006337 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006338 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006339 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006340 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006341 } else {
6342 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006343 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6344 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6345 // Rely on the pResolveString and marking to save everything we need.
6346 RegisterSet caller_saves = RegisterSet::Empty();
6347 InvokeRuntimeCallingConvention calling_convention;
6348 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6349 locations->SetCustomSlowPathCallerSaves(caller_saves);
6350 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006351 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006352 }
6353 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006354 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006355}
6356
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006357// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6358// move.
6359void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006360 HLoadString::LoadKind load_kind = load->GetLoadKind();
6361 LocationSummary* locations = load->GetLocations();
6362 Location out_loc = locations->Out();
6363 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6364
6365 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006366 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6367 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006368 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006369 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006370 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006371 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006372 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006373 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006374 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006375 }
6376 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006377 uint32_t address = dchecked_integral_cast<uint32_t>(
6378 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6379 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006380 __ LoadLiteral(out,
6381 kLoadUnsignedWord,
6382 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006383 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006384 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006385 case HLoadString::LoadKind::kBootImageRelRo: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006386 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006387 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006388 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006389 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006390 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006391 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006392 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6393 __ Lwu(out, AT, /* placeholder */ 0x5678);
6394 return;
6395 }
6396 case HLoadString::LoadKind::kBssEntry: {
6397 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6398 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6399 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6400 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6401 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006402 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006403 GenerateGcRootFieldLoad(load,
6404 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006405 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006406 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006407 kCompilerReadBarrierOption,
6408 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006409 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006410 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006411 codegen_->AddSlowPath(slow_path);
6412 __ Beqzc(out, slow_path->GetEntryLabel());
6413 __ Bind(slow_path->GetExitLabel());
6414 return;
6415 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006416 case HLoadString::LoadKind::kJitTableAddress:
6417 __ LoadLiteral(out,
6418 kLoadUnsignedWord,
6419 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6420 load->GetStringIndex(),
6421 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006422 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006423 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006424 default:
6425 break;
6426 }
6427
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006428 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006429 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006430 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006431 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006432 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6433 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6434 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006435}
6436
Alexey Frunze4dda3372015-06-01 18:31:49 -07006437void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006438 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006439 locations->SetOut(Location::ConstantLocation(constant));
6440}
6441
6442void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6443 // Will be generated at use site.
6444}
6445
6446void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006447 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6448 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006449 InvokeRuntimeCallingConvention calling_convention;
6450 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6451}
6452
6453void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006454 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006455 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006456 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006457 if (instruction->IsEnter()) {
6458 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6459 } else {
6460 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6461 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006462}
6463
6464void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6465 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006466 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006467 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006468 case DataType::Type::kInt32:
6469 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006470 locations->SetInAt(0, Location::RequiresRegister());
6471 locations->SetInAt(1, Location::RequiresRegister());
6472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6473 break;
6474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006475 case DataType::Type::kFloat32:
6476 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006477 locations->SetInAt(0, Location::RequiresFpuRegister());
6478 locations->SetInAt(1, Location::RequiresFpuRegister());
6479 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6480 break;
6481
6482 default:
6483 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6484 }
6485}
6486
6487void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006488 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006489 LocationSummary* locations = instruction->GetLocations();
6490
6491 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006492 case DataType::Type::kInt32:
6493 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006494 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6495 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6496 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006497 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006498 __ MulR6(dst, lhs, rhs);
6499 else
6500 __ Dmul(dst, lhs, rhs);
6501 break;
6502 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006503 case DataType::Type::kFloat32:
6504 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006505 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6506 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6507 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006508 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006509 __ MulS(dst, lhs, rhs);
6510 else
6511 __ MulD(dst, lhs, rhs);
6512 break;
6513 }
6514 default:
6515 LOG(FATAL) << "Unexpected mul type " << type;
6516 }
6517}
6518
6519void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6520 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006521 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006522 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006523 case DataType::Type::kInt32:
6524 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006525 locations->SetInAt(0, Location::RequiresRegister());
6526 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6527 break;
6528
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006529 case DataType::Type::kFloat32:
6530 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006531 locations->SetInAt(0, Location::RequiresFpuRegister());
6532 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6533 break;
6534
6535 default:
6536 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6537 }
6538}
6539
6540void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006541 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006542 LocationSummary* locations = instruction->GetLocations();
6543
6544 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 case DataType::Type::kInt32:
6546 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006547 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6548 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006549 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006550 __ Subu(dst, ZERO, src);
6551 else
6552 __ Dsubu(dst, ZERO, src);
6553 break;
6554 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006555 case DataType::Type::kFloat32:
6556 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006557 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6558 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006559 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006560 __ NegS(dst, src);
6561 else
6562 __ NegD(dst, src);
6563 break;
6564 }
6565 default:
6566 LOG(FATAL) << "Unexpected neg type " << type;
6567 }
6568}
6569
6570void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006571 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6572 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006573 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006574 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006575 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6576 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006577}
6578
6579void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006580 // Note: if heap poisoning is enabled, the entry point takes care
6581 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006582 QuickEntrypointEnum entrypoint =
6583 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6584 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006585 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006586 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006587}
6588
6589void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006590 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6591 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006592 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006593 if (instruction->IsStringAlloc()) {
6594 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6595 } else {
6596 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006597 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006598 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006599}
6600
6601void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006602 // Note: if heap poisoning is enabled, the entry point takes care
6603 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006604 if (instruction->IsStringAlloc()) {
6605 // String is allocated through StringFactory. Call NewEmptyString entry point.
6606 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006607 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006608 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006609 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6610 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6611 __ Jalr(T9);
6612 __ Nop();
6613 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6614 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006615 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006616 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006617 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006618}
6619
6620void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006621 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006622 locations->SetInAt(0, Location::RequiresRegister());
6623 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6624}
6625
6626void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006627 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006628 LocationSummary* locations = instruction->GetLocations();
6629
6630 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 case DataType::Type::kInt32:
6632 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006633 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6634 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6635 __ Nor(dst, src, ZERO);
6636 break;
6637 }
6638
6639 default:
6640 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6641 }
6642}
6643
6644void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006645 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006646 locations->SetInAt(0, Location::RequiresRegister());
6647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6648}
6649
6650void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6651 LocationSummary* locations = instruction->GetLocations();
6652 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6653 locations->InAt(0).AsRegister<GpuRegister>(),
6654 1);
6655}
6656
6657void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006658 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6659 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006660}
6661
Calin Juravle2ae48182016-03-16 14:05:09 +00006662void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6663 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006664 return;
6665 }
6666 Location obj = instruction->GetLocations()->InAt(0);
6667
6668 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006669 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006670}
6671
Calin Juravle2ae48182016-03-16 14:05:09 +00006672void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006673 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006674 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006675 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006676
6677 Location obj = instruction->GetLocations()->InAt(0);
6678
6679 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6680}
6681
6682void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006683 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006684}
6685
6686void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6687 HandleBinaryOp(instruction);
6688}
6689
6690void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6691 HandleBinaryOp(instruction);
6692}
6693
6694void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6695 LOG(FATAL) << "Unreachable";
6696}
6697
6698void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006699 if (instruction->GetNext()->IsSuspendCheck() &&
6700 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6701 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6702 // The back edge will generate the suspend check.
6703 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6704 }
6705
Alexey Frunze4dda3372015-06-01 18:31:49 -07006706 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6707}
6708
6709void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006710 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006711 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6712 if (location.IsStackSlot()) {
6713 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6714 } else if (location.IsDoubleStackSlot()) {
6715 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6716 }
6717 locations->SetOut(location);
6718}
6719
6720void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6721 ATTRIBUTE_UNUSED) {
6722 // Nothing to do, the parameter is already at its location.
6723}
6724
6725void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6726 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006727 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006728 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6729}
6730
6731void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6732 ATTRIBUTE_UNUSED) {
6733 // Nothing to do, the method is already at its location.
6734}
6735
6736void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006737 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006738 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006739 locations->SetInAt(i, Location::Any());
6740 }
6741 locations->SetOut(Location::Any());
6742}
6743
6744void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6745 LOG(FATAL) << "Unreachable";
6746}
6747
6748void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006749 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006750 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006751 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6752 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006753 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006754
6755 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006756 case DataType::Type::kInt32:
6757 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006758 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006759 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006760 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6761 break;
6762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006763 case DataType::Type::kFloat32:
6764 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006765 InvokeRuntimeCallingConvention calling_convention;
6766 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6767 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6768 locations->SetOut(calling_convention.GetReturnLocation(type));
6769 break;
6770 }
6771
6772 default:
6773 LOG(FATAL) << "Unexpected rem type " << type;
6774 }
6775}
6776
6777void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006778 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006779
6780 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 case DataType::Type::kInt32:
6782 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006783 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006784 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006786 case DataType::Type::kFloat32:
6787 case DataType::Type::kFloat64: {
6788 QuickEntrypointEnum entrypoint =
6789 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006790 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006791 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006792 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6793 } else {
6794 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6795 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006796 break;
6797 }
6798 default:
6799 LOG(FATAL) << "Unexpected rem type " << type;
6800 }
6801}
6802
Aart Bik1f8d51b2018-02-15 10:42:37 -08006803static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
6804 LocationSummary* locations = new (allocator) LocationSummary(minmax);
6805 switch (minmax->GetResultType()) {
6806 case DataType::Type::kInt32:
6807 case DataType::Type::kInt64:
6808 locations->SetInAt(0, Location::RequiresRegister());
6809 locations->SetInAt(1, Location::RequiresRegister());
6810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6811 break;
6812 case DataType::Type::kFloat32:
6813 case DataType::Type::kFloat64:
6814 locations->SetInAt(0, Location::RequiresFpuRegister());
6815 locations->SetInAt(1, Location::RequiresFpuRegister());
6816 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6817 break;
6818 default:
6819 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
6820 }
6821}
6822
Aart Bik351df3e2018-03-07 11:54:57 -08006823void InstructionCodeGeneratorMIPS64::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08006824 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6825 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
6826 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6827
6828 if (lhs == rhs) {
6829 if (out != lhs) {
6830 __ Move(out, lhs);
6831 }
6832 } else {
6833 // Some architectures, such as ARM and MIPS (prior to r6), have a
6834 // conditional move instruction which only changes the target
6835 // (output) register if the condition is true (MIPS prior to r6 had
6836 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
6837 // change the target (output) register. If the condition is true the
6838 // output register gets the contents of the "rs" register; otherwise,
6839 // the output register is set to zero. One consequence of this is
6840 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
6841 // needs to use a pair of SELEQZ/SELNEZ instructions. After
6842 // executing this pair of instructions one of the output registers
6843 // from the pair will necessarily contain zero. Then the code ORs the
6844 // output registers from the SELEQZ/SELNEZ instructions to get the
6845 // final result.
6846 //
6847 // The initial test to see if the output register is same as the
6848 // first input register is needed to make sure that value in the
6849 // first input register isn't clobbered before we've finished
6850 // computing the output value. The logic in the corresponding else
6851 // clause performs the same task but makes sure the second input
6852 // register isn't clobbered in the event that it's the same register
6853 // as the output register; the else clause also handles the case
6854 // where the output register is distinct from both the first, and the
6855 // second input registers.
6856 if (out == lhs) {
6857 __ Slt(AT, rhs, lhs);
6858 if (is_min) {
6859 __ Seleqz(out, lhs, AT);
6860 __ Selnez(AT, rhs, AT);
6861 } else {
6862 __ Selnez(out, lhs, AT);
6863 __ Seleqz(AT, rhs, AT);
6864 }
6865 } else {
6866 __ Slt(AT, lhs, rhs);
6867 if (is_min) {
6868 __ Seleqz(out, rhs, AT);
6869 __ Selnez(AT, lhs, AT);
6870 } else {
6871 __ Selnez(out, rhs, AT);
6872 __ Seleqz(AT, lhs, AT);
6873 }
6874 }
6875 __ Or(out, out, AT);
6876 }
6877}
6878
6879void InstructionCodeGeneratorMIPS64::GenerateMinMaxFP(LocationSummary* locations,
6880 bool is_min,
6881 DataType::Type type) {
6882 FpuRegister a = locations->InAt(0).AsFpuRegister<FpuRegister>();
6883 FpuRegister b = locations->InAt(1).AsFpuRegister<FpuRegister>();
6884 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6885
6886 Mips64Label noNaNs;
6887 Mips64Label done;
6888 FpuRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
6889
6890 // When Java computes min/max it prefers a NaN to a number; the
6891 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
6892 // the inputs is a NaN and the other is a valid number, the MIPS
6893 // instruction will return the number; Java wants the NaN value
6894 // returned. This is why there is extra logic preceding the use of
6895 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
6896 // NaN, return the NaN, otherwise return the min/max.
6897 if (type == DataType::Type::kFloat64) {
6898 __ CmpUnD(FTMP, a, b);
6899 __ Bc1eqz(FTMP, &noNaNs);
6900
6901 // One of the inputs is a NaN
6902 __ CmpEqD(ftmp, a, a);
6903 // If a == a then b is the NaN, otherwise a is the NaN.
6904 __ SelD(ftmp, a, b);
6905
6906 if (ftmp != out) {
6907 __ MovD(out, ftmp);
6908 }
6909
6910 __ Bc(&done);
6911
6912 __ Bind(&noNaNs);
6913
6914 if (is_min) {
6915 __ MinD(out, a, b);
6916 } else {
6917 __ MaxD(out, a, b);
6918 }
6919 } else {
6920 DCHECK_EQ(type, DataType::Type::kFloat32);
6921 __ CmpUnS(FTMP, a, b);
6922 __ Bc1eqz(FTMP, &noNaNs);
6923
6924 // One of the inputs is a NaN
6925 __ CmpEqS(ftmp, a, a);
6926 // If a == a then b is the NaN, otherwise a is the NaN.
6927 __ SelS(ftmp, a, b);
6928
6929 if (ftmp != out) {
6930 __ MovS(out, ftmp);
6931 }
6932
6933 __ Bc(&done);
6934
6935 __ Bind(&noNaNs);
6936
6937 if (is_min) {
6938 __ MinS(out, a, b);
6939 } else {
6940 __ MaxS(out, a, b);
6941 }
6942 }
6943
6944 __ Bind(&done);
6945}
6946
Aart Bik351df3e2018-03-07 11:54:57 -08006947void InstructionCodeGeneratorMIPS64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
6948 DataType::Type type = minmax->GetResultType();
6949 switch (type) {
6950 case DataType::Type::kInt32:
6951 case DataType::Type::kInt64:
6952 GenerateMinMaxInt(minmax->GetLocations(), is_min);
6953 break;
6954 case DataType::Type::kFloat32:
6955 case DataType::Type::kFloat64:
6956 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
6957 break;
6958 default:
6959 LOG(FATAL) << "Unexpected type for HMinMax " << type;
6960 }
6961}
6962
Aart Bik1f8d51b2018-02-15 10:42:37 -08006963void LocationsBuilderMIPS64::VisitMin(HMin* min) {
6964 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
6965}
6966
6967void InstructionCodeGeneratorMIPS64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08006968 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006969}
6970
6971void LocationsBuilderMIPS64::VisitMax(HMax* max) {
6972 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
6973}
6974
6975void InstructionCodeGeneratorMIPS64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08006976 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006977}
6978
Aart Bik3dad3412018-02-28 12:01:46 -08006979void LocationsBuilderMIPS64::VisitAbs(HAbs* abs) {
6980 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6981 switch (abs->GetResultType()) {
6982 case DataType::Type::kInt32:
6983 case DataType::Type::kInt64:
6984 locations->SetInAt(0, Location::RequiresRegister());
6985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6986 break;
6987 case DataType::Type::kFloat32:
6988 case DataType::Type::kFloat64:
6989 locations->SetInAt(0, Location::RequiresFpuRegister());
6990 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6991 break;
6992 default:
6993 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
6994 }
6995}
6996
6997void InstructionCodeGeneratorMIPS64::VisitAbs(HAbs* abs) {
6998 LocationSummary* locations = abs->GetLocations();
6999 switch (abs->GetResultType()) {
7000 case DataType::Type::kInt32: {
7001 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
7002 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
7003 __ Sra(AT, in, 31);
7004 __ Xor(out, in, AT);
7005 __ Subu(out, out, AT);
7006 break;
7007 }
7008 case DataType::Type::kInt64: {
7009 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
7010 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
7011 __ Dsra32(AT, in, 31);
7012 __ Xor(out, in, AT);
7013 __ Dsubu(out, out, AT);
7014 break;
7015 }
7016 case DataType::Type::kFloat32: {
7017 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
7018 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
7019 __ AbsS(out, in);
7020 break;
7021 }
7022 case DataType::Type::kFloat64: {
7023 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
7024 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
7025 __ AbsD(out, in);
7026 break;
7027 }
7028 default:
7029 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
7030 }
7031}
7032
Igor Murashkind01745e2017-04-05 16:40:31 -07007033void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
7034 constructor_fence->SetLocations(nullptr);
7035}
7036
7037void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
7038 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
7039 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
7040}
7041
Alexey Frunze4dda3372015-06-01 18:31:49 -07007042void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7043 memory_barrier->SetLocations(nullptr);
7044}
7045
7046void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7047 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7048}
7049
7050void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007051 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007052 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007053 locations->SetInAt(0, Mips64ReturnLocation(return_type));
7054}
7055
7056void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7057 codegen_->GenerateFrameExit();
7058}
7059
7060void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
7061 ret->SetLocations(nullptr);
7062}
7063
7064void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7065 codegen_->GenerateFrameExit();
7066}
7067
Alexey Frunze92d90602015-12-18 18:16:36 -08007068void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
7069 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007070}
7071
Alexey Frunze92d90602015-12-18 18:16:36 -08007072void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
7073 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007074}
7075
Alexey Frunze4dda3372015-06-01 18:31:49 -07007076void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
7077 HandleShift(shl);
7078}
7079
7080void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
7081 HandleShift(shl);
7082}
7083
7084void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
7085 HandleShift(shr);
7086}
7087
7088void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
7089 HandleShift(shr);
7090}
7091
Alexey Frunze4dda3372015-06-01 18:31:49 -07007092void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
7093 HandleBinaryOp(instruction);
7094}
7095
7096void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
7097 HandleBinaryOp(instruction);
7098}
7099
7100void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7101 HandleFieldGet(instruction, instruction->GetFieldInfo());
7102}
7103
7104void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7105 HandleFieldGet(instruction, instruction->GetFieldInfo());
7106}
7107
7108void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7109 HandleFieldSet(instruction, instruction->GetFieldInfo());
7110}
7111
7112void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01007113 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007114}
7115
Calin Juravlee460d1d2015-09-29 04:52:17 +01007116void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
7117 HUnresolvedInstanceFieldGet* instruction) {
7118 FieldAccessCallingConventionMIPS64 calling_convention;
7119 codegen_->CreateUnresolvedFieldLocationSummary(
7120 instruction, instruction->GetFieldType(), calling_convention);
7121}
7122
7123void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
7124 HUnresolvedInstanceFieldGet* instruction) {
7125 FieldAccessCallingConventionMIPS64 calling_convention;
7126 codegen_->GenerateUnresolvedFieldAccess(instruction,
7127 instruction->GetFieldType(),
7128 instruction->GetFieldIndex(),
7129 instruction->GetDexPc(),
7130 calling_convention);
7131}
7132
7133void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
7134 HUnresolvedInstanceFieldSet* instruction) {
7135 FieldAccessCallingConventionMIPS64 calling_convention;
7136 codegen_->CreateUnresolvedFieldLocationSummary(
7137 instruction, instruction->GetFieldType(), calling_convention);
7138}
7139
7140void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
7141 HUnresolvedInstanceFieldSet* instruction) {
7142 FieldAccessCallingConventionMIPS64 calling_convention;
7143 codegen_->GenerateUnresolvedFieldAccess(instruction,
7144 instruction->GetFieldType(),
7145 instruction->GetFieldIndex(),
7146 instruction->GetDexPc(),
7147 calling_convention);
7148}
7149
7150void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
7151 HUnresolvedStaticFieldGet* instruction) {
7152 FieldAccessCallingConventionMIPS64 calling_convention;
7153 codegen_->CreateUnresolvedFieldLocationSummary(
7154 instruction, instruction->GetFieldType(), calling_convention);
7155}
7156
7157void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
7158 HUnresolvedStaticFieldGet* instruction) {
7159 FieldAccessCallingConventionMIPS64 calling_convention;
7160 codegen_->GenerateUnresolvedFieldAccess(instruction,
7161 instruction->GetFieldType(),
7162 instruction->GetFieldIndex(),
7163 instruction->GetDexPc(),
7164 calling_convention);
7165}
7166
7167void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
7168 HUnresolvedStaticFieldSet* instruction) {
7169 FieldAccessCallingConventionMIPS64 calling_convention;
7170 codegen_->CreateUnresolvedFieldLocationSummary(
7171 instruction, instruction->GetFieldType(), calling_convention);
7172}
7173
7174void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
7175 HUnresolvedStaticFieldSet* instruction) {
7176 FieldAccessCallingConventionMIPS64 calling_convention;
7177 codegen_->GenerateUnresolvedFieldAccess(instruction,
7178 instruction->GetFieldType(),
7179 instruction->GetFieldIndex(),
7180 instruction->GetDexPc(),
7181 calling_convention);
7182}
7183
Alexey Frunze4dda3372015-06-01 18:31:49 -07007184void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007185 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7186 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02007187 // In suspend check slow path, usually there are no caller-save registers at all.
7188 // If SIMD instructions are present, however, we force spilling all live SIMD
7189 // registers in full width (since the runtime only saves/restores lower part).
7190 locations->SetCustomSlowPathCallerSaves(
7191 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007192}
7193
7194void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
7195 HBasicBlock* block = instruction->GetBlock();
7196 if (block->GetLoopInformation() != nullptr) {
7197 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7198 // The back edge will generate the suspend check.
7199 return;
7200 }
7201 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7202 // The goto will generate the suspend check.
7203 return;
7204 }
7205 GenerateSuspendCheck(instruction, nullptr);
7206}
7207
Alexey Frunze4dda3372015-06-01 18:31:49 -07007208void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007209 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7210 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007211 InvokeRuntimeCallingConvention calling_convention;
7212 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7213}
7214
7215void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01007216 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007217 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7218}
7219
7220void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007221 DataType::Type input_type = conversion->GetInputType();
7222 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007223 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7224 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007226 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
7227 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007228 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
7229 }
7230
Vladimir Markoca6fff82017-10-03 14:49:14 +01007231 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007233 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007234 locations->SetInAt(0, Location::RequiresFpuRegister());
7235 } else {
7236 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007237 }
7238
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007239 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007240 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007241 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007242 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007243 }
7244}
7245
7246void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
7247 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007248 DataType::Type result_type = conversion->GetResultType();
7249 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007250
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007251 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7252 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007254 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007255 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7256 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
7257
7258 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007259 case DataType::Type::kUint8:
7260 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007261 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007262 case DataType::Type::kInt8:
7263 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007264 // Type conversion from long to types narrower than int is a result of code
7265 // transformations. To avoid unpredictable results for SEB and SEH, we first
7266 // need to sign-extend the low 32-bit value into bits 32 through 63.
7267 __ Sll(dst, src, 0);
7268 __ Seb(dst, dst);
7269 } else {
7270 __ Seb(dst, src);
7271 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007272 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007273 case DataType::Type::kUint16:
7274 __ Andi(dst, src, 0xFFFF);
7275 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007276 case DataType::Type::kInt16:
7277 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007278 // Type conversion from long to types narrower than int is a result of code
7279 // transformations. To avoid unpredictable results for SEB and SEH, we first
7280 // need to sign-extend the low 32-bit value into bits 32 through 63.
7281 __ Sll(dst, src, 0);
7282 __ Seh(dst, dst);
7283 } else {
7284 __ Seh(dst, src);
7285 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007286 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007287 case DataType::Type::kInt32:
7288 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007289 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
7290 // conversions, except when the input and output registers are the same and we are not
7291 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007292 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007293 __ Sll(dst, src, 0);
7294 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007295 break;
7296
7297 default:
7298 LOG(FATAL) << "Unexpected type conversion from " << input_type
7299 << " to " << result_type;
7300 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007301 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007302 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7303 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007304 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007305 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007306 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007307 __ Cvtsl(dst, FTMP);
7308 } else {
7309 __ Cvtdl(dst, FTMP);
7310 }
7311 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007312 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007313 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007314 __ Cvtsw(dst, FTMP);
7315 } else {
7316 __ Cvtdw(dst, FTMP);
7317 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007318 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007319 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
7320 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007321 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7322 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007324 if (result_type == DataType::Type::kInt64) {
7325 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007326 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007327 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007328 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007329 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007330 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007331 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007332 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007333 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007334 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007335 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007336 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007337 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007338 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007339 } else if (DataType::IsFloatingPointType(result_type) &&
7340 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007341 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7342 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007343 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007344 __ Cvtsd(dst, src);
7345 } else {
7346 __ Cvtds(dst, src);
7347 }
7348 } else {
7349 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7350 << " to " << result_type;
7351 }
7352}
7353
7354void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
7355 HandleShift(ushr);
7356}
7357
7358void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
7359 HandleShift(ushr);
7360}
7361
7362void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7363 HandleBinaryOp(instruction);
7364}
7365
7366void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7367 HandleBinaryOp(instruction);
7368}
7369
7370void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7371 // Nothing to do, this should be removed during prepare for register allocator.
7372 LOG(FATAL) << "Unreachable";
7373}
7374
7375void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7376 // Nothing to do, this should be removed during prepare for register allocator.
7377 LOG(FATAL) << "Unreachable";
7378}
7379
7380void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007381 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007382}
7383
7384void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007385 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007386}
7387
7388void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007389 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007390}
7391
7392void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007393 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007394}
7395
7396void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007397 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007398}
7399
7400void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007401 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007402}
7403
7404void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007405 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007406}
7407
7408void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007409 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007410}
7411
7412void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007413 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007414}
7415
7416void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007417 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007418}
7419
7420void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007421 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007422}
7423
7424void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007425 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007426}
7427
Aart Bike9f37602015-10-09 11:15:55 -07007428void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007429 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007430}
7431
7432void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007433 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007434}
7435
7436void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007437 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007438}
7439
7440void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007441 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007442}
7443
7444void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007445 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007446}
7447
7448void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007449 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007450}
7451
7452void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007453 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007454}
7455
7456void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007457 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007458}
7459
Mark Mendellfe57faa2015-09-18 09:26:15 -04007460// Simple implementation of packed switch - generate cascaded compare/jumps.
7461void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7462 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007463 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007464 locations->SetInAt(0, Location::RequiresRegister());
7465}
7466
Alexey Frunze0960ac52016-12-20 17:24:59 -08007467void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7468 int32_t lower_bound,
7469 uint32_t num_entries,
7470 HBasicBlock* switch_block,
7471 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007472 // Create a set of compare/jumps.
7473 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007474 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007475 // Jump to default if index is negative
7476 // Note: We don't check the case that index is positive while value < lower_bound, because in
7477 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7478 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7479
Alexey Frunze0960ac52016-12-20 17:24:59 -08007480 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007481 // Jump to successors[0] if value == lower_bound.
7482 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7483 int32_t last_index = 0;
7484 for (; num_entries - last_index > 2; last_index += 2) {
7485 __ Addiu(temp_reg, temp_reg, -2);
7486 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7487 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7488 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7489 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7490 }
7491 if (num_entries - last_index == 2) {
7492 // The last missing case_value.
7493 __ Addiu(temp_reg, temp_reg, -1);
7494 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007495 }
7496
7497 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007498 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007499 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007500 }
7501}
7502
Alexey Frunze0960ac52016-12-20 17:24:59 -08007503void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7504 int32_t lower_bound,
7505 uint32_t num_entries,
7506 HBasicBlock* switch_block,
7507 HBasicBlock* default_block) {
7508 // Create a jump table.
7509 std::vector<Mips64Label*> labels(num_entries);
7510 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7511 for (uint32_t i = 0; i < num_entries; i++) {
7512 labels[i] = codegen_->GetLabelOf(successors[i]);
7513 }
7514 JumpTable* table = __ CreateJumpTable(std::move(labels));
7515
7516 // Is the value in range?
7517 __ Addiu32(TMP, value_reg, -lower_bound);
7518 __ LoadConst32(AT, num_entries);
7519 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7520
7521 // We are in the range of the table.
7522 // Load the target address from the jump table, indexing by the value.
7523 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007524 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007525 __ Lw(TMP, TMP, 0);
7526 // Compute the absolute target address by adding the table start address
7527 // (the table contains offsets to targets relative to its start).
7528 __ Daddu(TMP, TMP, AT);
7529 // And jump.
7530 __ Jr(TMP);
7531 __ Nop();
7532}
7533
7534void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7535 int32_t lower_bound = switch_instr->GetStartValue();
7536 uint32_t num_entries = switch_instr->GetNumEntries();
7537 LocationSummary* locations = switch_instr->GetLocations();
7538 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7539 HBasicBlock* switch_block = switch_instr->GetBlock();
7540 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7541
7542 if (num_entries > kPackedSwitchJumpTableThreshold) {
7543 GenTableBasedPackedSwitch(value_reg,
7544 lower_bound,
7545 num_entries,
7546 switch_block,
7547 default_block);
7548 } else {
7549 GenPackedSwitchWithCompares(value_reg,
7550 lower_bound,
7551 num_entries,
7552 switch_block,
7553 default_block);
7554 }
7555}
7556
Chris Larsenc9905a62017-03-13 17:06:18 -07007557void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7558 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007559 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007560 locations->SetInAt(0, Location::RequiresRegister());
7561 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007562}
7563
Chris Larsenc9905a62017-03-13 17:06:18 -07007564void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7565 LocationSummary* locations = instruction->GetLocations();
7566 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7567 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7568 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7569 __ LoadFromOffset(kLoadDoubleword,
7570 locations->Out().AsRegister<GpuRegister>(),
7571 locations->InAt(0).AsRegister<GpuRegister>(),
7572 method_offset);
7573 } else {
7574 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7575 instruction->GetIndex(), kMips64PointerSize));
7576 __ LoadFromOffset(kLoadDoubleword,
7577 locations->Out().AsRegister<GpuRegister>(),
7578 locations->InAt(0).AsRegister<GpuRegister>(),
7579 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7580 __ LoadFromOffset(kLoadDoubleword,
7581 locations->Out().AsRegister<GpuRegister>(),
7582 locations->Out().AsRegister<GpuRegister>(),
7583 method_offset);
7584 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007585}
7586
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007587void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7588 ATTRIBUTE_UNUSED) {
7589 LOG(FATAL) << "Unreachable";
7590}
7591
7592void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7593 ATTRIBUTE_UNUSED) {
7594 LOG(FATAL) << "Unreachable";
7595}
7596
Alexey Frunze4dda3372015-06-01 18:31:49 -07007597} // namespace mips64
7598} // namespace art