blob: 5b07b55cbbbc7d766c52c6b9276fd50c36dddef0 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips64/asm_support_mips64.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070020#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070022#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080023#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070028#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070029#include "intrinsics_mips64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "mirror/array-inl.h"
32#include "mirror/class-inl.h"
33#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010034#include "stack_map_stream.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070035#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070037#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070038#include "utils/stack_checks.h"
39
40namespace art {
41namespace mips64 {
42
43static constexpr int kCurrentMethodStackOffset = 0;
44static constexpr GpuRegister kMethodRegisterArgument = A0;
45
Alexey Frunze4147fcc2017-06-17 19:57:27 -070046// Flags controlling the use of thunks for Baker read barriers.
47constexpr bool kBakerReadBarrierThunksEnableForFields = true;
48constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
49constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
50
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051Location Mips64ReturnLocation(DataType::Type return_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070052 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010054 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 case DataType::Type::kInt8:
56 case DataType::Type::kUint16:
57 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080058 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010059 case DataType::Type::kInt32:
60 case DataType::Type::kReference:
Aart Bik66c158e2018-01-31 12:55:04 -080061 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070063 return Location::RegisterLocation(V0);
64
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010065 case DataType::Type::kFloat32:
66 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070067 return Location::FpuRegisterLocation(F0);
68
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070070 return Location();
71 }
72 UNREACHABLE();
73}
74
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010075Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070076 return Mips64ReturnLocation(type);
77}
78
79Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
80 return Location::RegisterLocation(kMethodRegisterArgument);
81}
82
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070084 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070086 LOG(FATAL) << "Unexpected parameter type " << type;
87 }
88
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010089 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070090 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
91 next_location = Location::FpuRegisterLocation(
92 calling_convention.GetFpuRegisterAt(float_index_++));
93 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010094 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070095 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
96 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
97 float_index_++;
98 } else {
99 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100100 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
101 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700102 }
103
104 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100105 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700106
Alexey Frunze4dda3372015-06-01 18:31:49 -0700107 return next_location;
108}
109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100110Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111 return Mips64ReturnLocation(type);
112}
113
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100114// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
115#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700116#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117
118class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
119 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000120 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121
122 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
125 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000126 if (instruction_->CanThrowIntoCatchBlock()) {
127 // Live registers will be restored in the catch block if caught.
128 SaveLiveRegisters(codegen, instruction_->GetLocations());
129 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 // We're moving two locations to locations that could overlap, so we need a parallel
131 // move resolver.
132 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100133 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700134 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100135 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100136 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700137 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100138 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100139 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
140 ? kQuickThrowStringBounds
141 : kQuickThrowArrayBounds;
142 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100143 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700144 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
145 }
146
Alexandre Rames8158f282015-08-07 10:26:17 +0100147 bool IsFatal() const OVERRIDE { return true; }
148
Roland Levillain46648892015-06-19 16:07:18 +0100149 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
150
Alexey Frunze4dda3372015-06-01 18:31:49 -0700151 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700152 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
153};
154
155class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
156 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700157 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
158 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700159
160 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
161 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
162 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100163 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700164 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
165 }
166
Alexandre Rames8158f282015-08-07 10:26:17 +0100167 bool IsFatal() const OVERRIDE { return true; }
168
Roland Levillain46648892015-06-19 16:07:18 +0100169 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
170
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
173};
174
175class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
176 public:
177 LoadClassSlowPathMIPS64(HLoadClass* cls,
178 HInstruction* at,
179 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000180 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700181 : SlowPathCodeMIPS64(at),
182 cls_(cls),
183 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000184 do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700185 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
186 }
187
188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000189 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700190 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700191 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700192 InvokeRuntimeCallingConvention calling_convention;
193 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700194 __ Bind(GetEntryLabel());
195 SaveLiveRegisters(codegen, locations);
196
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000197 dex::TypeIndex type_index = cls_->GetTypeIndex();
198 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100199 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
200 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000201 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700202 if (do_clinit_) {
203 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
204 } else {
205 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
206 }
207
208 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700209 if (out.IsValid()) {
210 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100211 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700212 mips64_codegen->MoveLocation(out,
213 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
214 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700215 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700216 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700217
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700218 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700219 }
220
Roland Levillain46648892015-06-19 16:07:18 +0100221 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
222
Alexey Frunze4dda3372015-06-01 18:31:49 -0700223 private:
224 // The class this slow path will load.
225 HLoadClass* const cls_;
226
Alexey Frunze4dda3372015-06-01 18:31:49 -0700227 // The dex PC of `at_`.
228 const uint32_t dex_pc_;
229
230 // Whether to initialize the class.
231 const bool do_clinit_;
232
233 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
234};
235
236class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
237 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000238 explicit LoadStringSlowPathMIPS64(HLoadString* instruction)
239 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700240
241 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700242 DCHECK(instruction_->IsLoadString());
243 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700244 LocationSummary* locations = instruction_->GetLocations();
245 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000246 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700248 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700249 __ Bind(GetEntryLabel());
250 SaveLiveRegisters(codegen, locations);
251
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000252 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100253 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700254 instruction_,
255 instruction_->GetDexPc(),
256 this);
257 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700258
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100259 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700261 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700262 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700263 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800264
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700265 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700266 }
267
Roland Levillain46648892015-06-19 16:07:18 +0100268 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
269
Alexey Frunze4dda3372015-06-01 18:31:49 -0700270 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700271 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
272};
273
274class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
275 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000276 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700277
278 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
279 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
280 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000281 if (instruction_->CanThrowIntoCatchBlock()) {
282 // Live registers will be restored in the catch block if caught.
283 SaveLiveRegisters(codegen, instruction_->GetLocations());
284 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100285 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700286 instruction_,
287 instruction_->GetDexPc(),
288 this);
289 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
290 }
291
Alexandre Rames8158f282015-08-07 10:26:17 +0100292 bool IsFatal() const OVERRIDE { return true; }
293
Roland Levillain46648892015-06-19 16:07:18 +0100294 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
295
Alexey Frunze4dda3372015-06-01 18:31:49 -0700296 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700297 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
298};
299
300class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
301 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100302 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000303 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700304
305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200306 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700307 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
308 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200309 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100310 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200312 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700316 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700317 }
318 }
319
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700320 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700321 DCHECK(successor_ == nullptr);
322 return &return_label_;
323 }
324
Roland Levillain46648892015-06-19 16:07:18 +0100325 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
326
Chris Larsena2045912017-11-02 12:39:54 -0700327 HBasicBlock* GetSuccessor() const {
328 return successor_;
329 }
330
Alexey Frunze4dda3372015-06-01 18:31:49 -0700331 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332 // If not null, the block to branch to after the suspend check.
333 HBasicBlock* const successor_;
334
335 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700336 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700337
338 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
339};
340
341class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
342 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800343 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
344 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800348
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700350 DCHECK(instruction_->IsCheckCast()
351 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
352 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
353
354 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800355 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800356 SaveLiveRegisters(codegen, locations);
357 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358
359 // We're moving two locations to locations that could overlap, so we need a parallel
360 // move resolver.
361 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800362 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100364 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800365 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100367 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100369 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800370 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100371 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
373 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 } else {
375 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800376 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
377 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700378 }
379
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800380 if (!is_fatal_) {
381 RestoreLiveRegisters(codegen, locations);
382 __ Bc(GetExitLabel());
383 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700384 }
385
Roland Levillain46648892015-06-19 16:07:18 +0100386 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
387
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800388 bool IsFatal() const OVERRIDE { return is_fatal_; }
389
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800391 const bool is_fatal_;
392
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
394};
395
396class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
397 public:
Aart Bik42249c32016-01-07 15:33:50 -0800398 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000399 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800402 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100404 LocationSummary* locations = instruction_->GetLocations();
405 SaveLiveRegisters(codegen, locations);
406 InvokeRuntimeCallingConvention calling_convention;
407 __ LoadConst32(calling_convention.GetRegisterAt(0),
408 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100409 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100410 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 }
412
Roland Levillain46648892015-06-19 16:07:18 +0100413 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
414
Alexey Frunze4dda3372015-06-01 18:31:49 -0700415 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700416 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
417};
418
Alexey Frunze15958152017-02-09 19:08:30 -0800419class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
420 public:
421 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
422
423 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
424 LocationSummary* locations = instruction_->GetLocations();
425 __ Bind(GetEntryLabel());
426 SaveLiveRegisters(codegen, locations);
427
428 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100429 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800430 parallel_move.AddMove(
431 locations->InAt(0),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(1),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800439 nullptr);
440 parallel_move.AddMove(
441 locations->InAt(2),
442 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100443 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800444 nullptr);
445 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
446
447 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
448 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
449 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
450 RestoreLiveRegisters(codegen, locations);
451 __ Bc(GetExitLabel());
452 }
453
454 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
455
456 private:
457 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
458};
459
460// Slow path marking an object reference `ref` during a read
461// barrier. The field `obj.field` in the object `obj` holding this
462// reference does not get updated by this slow path after marking (see
463// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
464//
465// This means that after the execution of this slow path, `ref` will
466// always be up-to-date, but `obj.field` may not; i.e., after the
467// flip, `ref` will be a to-space reference, but `obj.field` will
468// probably still be a from-space reference (unless it gets updated by
469// another thread, or if another thread installed another object
470// reference (different from `ref`) in `obj.field`).
471//
472// If `entrypoint` is a valid location it is assumed to already be
473// holding the entrypoint. The case where the entrypoint is passed in
474// is for the GcRoot read barrier.
475class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
476 public:
477 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
478 Location ref,
479 Location entrypoint = Location::NoLocation())
480 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
481 DCHECK(kEmitCompilerReadBarrier);
482 }
483
484 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
485
486 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
487 LocationSummary* locations = instruction_->GetLocations();
488 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
489 DCHECK(locations->CanCall());
490 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
491 DCHECK(instruction_->IsInstanceFieldGet() ||
492 instruction_->IsStaticFieldGet() ||
493 instruction_->IsArrayGet() ||
494 instruction_->IsArraySet() ||
495 instruction_->IsLoadClass() ||
496 instruction_->IsLoadString() ||
497 instruction_->IsInstanceOf() ||
498 instruction_->IsCheckCast() ||
499 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
500 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
501 << "Unexpected instruction in read barrier marking slow path: "
502 << instruction_->DebugName();
503
504 __ Bind(GetEntryLabel());
505 // No need to save live registers; it's taken care of by the
506 // entrypoint. Also, there is no need to update the stack mask,
507 // as this runtime call will not trigger a garbage collection.
508 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
509 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
510 (S2 <= ref_reg && ref_reg <= S7) ||
511 (ref_reg == S8)) << ref_reg;
512 // "Compact" slow path, saving two moves.
513 //
514 // Instead of using the standard runtime calling convention (input
515 // and output in A0 and V0 respectively):
516 //
517 // A0 <- ref
518 // V0 <- ReadBarrierMark(A0)
519 // ref <- V0
520 //
521 // we just use rX (the register containing `ref`) as input and output
522 // of a dedicated entrypoint:
523 //
524 // rX <- ReadBarrierMarkRegX(rX)
525 //
526 if (entrypoint_.IsValid()) {
527 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
528 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
529 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
530 __ Nop();
531 } else {
532 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100533 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800534 // This runtime call does not require a stack map.
535 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
536 instruction_,
537 this);
538 }
539 __ Bc(GetExitLabel());
540 }
541
542 private:
543 // The location (register) of the marked object reference.
544 const Location ref_;
545
546 // The location of the entrypoint if already loaded.
547 const Location entrypoint_;
548
549 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
550};
551
552// Slow path marking an object reference `ref` during a read barrier,
553// and if needed, atomically updating the field `obj.field` in the
554// object `obj` holding this reference after marking (contrary to
555// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
556// `obj.field`).
557//
558// This means that after the execution of this slow path, both `ref`
559// and `obj.field` will be up-to-date; i.e., after the flip, both will
560// hold the same to-space reference (unless another thread installed
561// another object reference (different from `ref`) in `obj.field`).
562class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
563 public:
564 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
565 Location ref,
566 GpuRegister obj,
567 Location field_offset,
568 GpuRegister temp1)
569 : SlowPathCodeMIPS64(instruction),
570 ref_(ref),
571 obj_(obj),
572 field_offset_(field_offset),
573 temp1_(temp1) {
574 DCHECK(kEmitCompilerReadBarrier);
575 }
576
577 const char* GetDescription() const OVERRIDE {
578 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
579 }
580
581 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
582 LocationSummary* locations = instruction_->GetLocations();
583 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
584 DCHECK(locations->CanCall());
585 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
586 // This slow path is only used by the UnsafeCASObject intrinsic.
587 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
588 << "Unexpected instruction in read barrier marking and field updating slow path: "
589 << instruction_->DebugName();
590 DCHECK(instruction_->GetLocations()->Intrinsified());
591 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
592 DCHECK(field_offset_.IsRegister()) << field_offset_;
593
594 __ Bind(GetEntryLabel());
595
596 // Save the old reference.
597 // Note that we cannot use AT or TMP to save the old reference, as those
598 // are used by the code that follows, but we need the old reference after
599 // the call to the ReadBarrierMarkRegX entry point.
600 DCHECK_NE(temp1_, AT);
601 DCHECK_NE(temp1_, TMP);
602 __ Move(temp1_, ref_reg);
603
604 // No need to save live registers; it's taken care of by the
605 // entrypoint. Also, there is no need to update the stack mask,
606 // as this runtime call will not trigger a garbage collection.
607 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
608 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
609 (S2 <= ref_reg && ref_reg <= S7) ||
610 (ref_reg == S8)) << ref_reg;
611 // "Compact" slow path, saving two moves.
612 //
613 // Instead of using the standard runtime calling convention (input
614 // and output in A0 and V0 respectively):
615 //
616 // A0 <- ref
617 // V0 <- ReadBarrierMark(A0)
618 // ref <- V0
619 //
620 // we just use rX (the register containing `ref`) as input and output
621 // of a dedicated entrypoint:
622 //
623 // rX <- ReadBarrierMarkRegX(rX)
624 //
625 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100626 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800627 // This runtime call does not require a stack map.
628 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
629 instruction_,
630 this);
631
632 // If the new reference is different from the old reference,
633 // update the field in the holder (`*(obj_ + field_offset_)`).
634 //
635 // Note that this field could also hold a different object, if
636 // another thread had concurrently changed it. In that case, the
637 // the compare-and-set (CAS) loop below would abort, leaving the
638 // field as-is.
639 Mips64Label done;
640 __ Beqc(temp1_, ref_reg, &done);
641
642 // Update the the holder's field atomically. This may fail if
643 // mutator updates before us, but it's OK. This is achieved
644 // using a strong compare-and-set (CAS) operation with relaxed
645 // memory synchronization ordering, where the expected value is
646 // the old reference and the desired value is the new reference.
647
648 // Convenience aliases.
649 GpuRegister base = obj_;
650 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
651 GpuRegister expected = temp1_;
652 GpuRegister value = ref_reg;
653 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
654 GpuRegister tmp = AT; // Value in memory.
655
656 __ Daddu(tmp_ptr, base, offset);
657
658 if (kPoisonHeapReferences) {
659 __ PoisonHeapReference(expected);
660 // Do not poison `value` if it is the same register as
661 // `expected`, which has just been poisoned.
662 if (value != expected) {
663 __ PoisonHeapReference(value);
664 }
665 }
666
667 // do {
668 // tmp = [r_ptr] - expected;
669 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
670
671 Mips64Label loop_head, exit_loop;
672 __ Bind(&loop_head);
673 __ Ll(tmp, tmp_ptr);
674 // The LL instruction sign-extends the 32-bit value, but
675 // 32-bit references must be zero-extended. Zero-extend `tmp`.
676 __ Dext(tmp, tmp, 0, 32);
677 __ Bnec(tmp, expected, &exit_loop);
678 __ Move(tmp, value);
679 __ Sc(tmp, tmp_ptr);
680 __ Beqzc(tmp, &loop_head);
681 __ Bind(&exit_loop);
682
683 if (kPoisonHeapReferences) {
684 __ UnpoisonHeapReference(expected);
685 // Do not unpoison `value` if it is the same register as
686 // `expected`, which has just been unpoisoned.
687 if (value != expected) {
688 __ UnpoisonHeapReference(value);
689 }
690 }
691
692 __ Bind(&done);
693 __ Bc(GetExitLabel());
694 }
695
696 private:
697 // The location (register) of the marked object reference.
698 const Location ref_;
699 // The register containing the object holding the marked object reference field.
700 const GpuRegister obj_;
701 // The location of the offset of the marked reference field within `obj_`.
702 Location field_offset_;
703
704 const GpuRegister temp1_;
705
706 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
707};
708
709// Slow path generating a read barrier for a heap reference.
710class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
711 public:
712 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
713 Location out,
714 Location ref,
715 Location obj,
716 uint32_t offset,
717 Location index)
718 : SlowPathCodeMIPS64(instruction),
719 out_(out),
720 ref_(ref),
721 obj_(obj),
722 offset_(offset),
723 index_(index) {
724 DCHECK(kEmitCompilerReadBarrier);
725 // If `obj` is equal to `out` or `ref`, it means the initial object
726 // has been overwritten by (or after) the heap object reference load
727 // to be instrumented, e.g.:
728 //
729 // __ LoadFromOffset(kLoadWord, out, out, offset);
730 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
731 //
732 // In that case, we have lost the information about the original
733 // object, and the emitted read barrier cannot work properly.
734 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
735 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
736 }
737
738 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
739 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
740 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100741 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800742 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
743 DCHECK(locations->CanCall());
744 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
745 DCHECK(instruction_->IsInstanceFieldGet() ||
746 instruction_->IsStaticFieldGet() ||
747 instruction_->IsArrayGet() ||
748 instruction_->IsInstanceOf() ||
749 instruction_->IsCheckCast() ||
750 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
751 << "Unexpected instruction in read barrier for heap reference slow path: "
752 << instruction_->DebugName();
753
754 __ Bind(GetEntryLabel());
755 SaveLiveRegisters(codegen, locations);
756
757 // We may have to change the index's value, but as `index_` is a
758 // constant member (like other "inputs" of this slow path),
759 // introduce a copy of it, `index`.
760 Location index = index_;
761 if (index_.IsValid()) {
762 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
763 if (instruction_->IsArrayGet()) {
764 // Compute the actual memory offset and store it in `index`.
765 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
766 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
767 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
768 // We are about to change the value of `index_reg` (see the
769 // calls to art::mips64::Mips64Assembler::Sll and
770 // art::mips64::MipsAssembler::Addiu32 below), but it has
771 // not been saved by the previous call to
772 // art::SlowPathCode::SaveLiveRegisters, as it is a
773 // callee-save register --
774 // art::SlowPathCode::SaveLiveRegisters does not consider
775 // callee-save registers, as it has been designed with the
776 // assumption that callee-save registers are supposed to be
777 // handled by the called function. So, as a callee-save
778 // register, `index_reg` _would_ eventually be saved onto
779 // the stack, but it would be too late: we would have
780 // changed its value earlier. Therefore, we manually save
781 // it here into another freely available register,
782 // `free_reg`, chosen of course among the caller-save
783 // registers (as a callee-save `free_reg` register would
784 // exhibit the same problem).
785 //
786 // Note we could have requested a temporary register from
787 // the register allocator instead; but we prefer not to, as
788 // this is a slow path, and we know we can find a
789 // caller-save register that is available.
790 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
791 __ Move(free_reg, index_reg);
792 index_reg = free_reg;
793 index = Location::RegisterLocation(index_reg);
794 } else {
795 // The initial register stored in `index_` has already been
796 // saved in the call to art::SlowPathCode::SaveLiveRegisters
797 // (as it is not a callee-save register), so we can freely
798 // use it.
799 }
800 // Shifting the index value contained in `index_reg` by the scale
801 // factor (2) cannot overflow in practice, as the runtime is
802 // unable to allocate object arrays with a size larger than
803 // 2^26 - 1 (that is, 2^28 - 4 bytes).
804 __ Sll(index_reg, index_reg, TIMES_4);
805 static_assert(
806 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
807 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
808 __ Addiu32(index_reg, index_reg, offset_);
809 } else {
810 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
811 // intrinsics, `index_` is not shifted by a scale factor of 2
812 // (as in the case of ArrayGet), as it is actually an offset
813 // to an object field within an object.
814 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
815 DCHECK(instruction_->GetLocations()->Intrinsified());
816 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
817 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
818 << instruction_->AsInvoke()->GetIntrinsic();
819 DCHECK_EQ(offset_, 0U);
820 DCHECK(index_.IsRegister());
821 }
822 }
823
824 // We're moving two or three locations to locations that could
825 // overlap, so we need a parallel move resolver.
826 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100827 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800828 parallel_move.AddMove(ref_,
829 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100830 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800831 nullptr);
832 parallel_move.AddMove(obj_,
833 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100834 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800835 nullptr);
836 if (index.IsValid()) {
837 parallel_move.AddMove(index,
838 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800840 nullptr);
841 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
842 } else {
843 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
844 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
845 }
846 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
847 instruction_,
848 instruction_->GetDexPc(),
849 this);
850 CheckEntrypointTypes<
851 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
852 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
853
854 RestoreLiveRegisters(codegen, locations);
855 __ Bc(GetExitLabel());
856 }
857
858 const char* GetDescription() const OVERRIDE {
859 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
860 }
861
862 private:
863 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
864 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
865 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
866 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
867 if (i != ref &&
868 i != obj &&
869 !codegen->IsCoreCalleeSaveRegister(i) &&
870 !codegen->IsBlockedCoreRegister(i)) {
871 return static_cast<GpuRegister>(i);
872 }
873 }
874 // We shall never fail to find a free caller-save register, as
875 // there are more than two core caller-save registers on MIPS64
876 // (meaning it is possible to find one which is different from
877 // `ref` and `obj`).
878 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
879 LOG(FATAL) << "Could not find a free caller-save register";
880 UNREACHABLE();
881 }
882
883 const Location out_;
884 const Location ref_;
885 const Location obj_;
886 const uint32_t offset_;
887 // An additional location containing an index to an array.
888 // Only used for HArrayGet and the UnsafeGetObject &
889 // UnsafeGetObjectVolatile intrinsics.
890 const Location index_;
891
892 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
893};
894
895// Slow path generating a read barrier for a GC root.
896class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
897 public:
898 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
899 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
900 DCHECK(kEmitCompilerReadBarrier);
901 }
902
903 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
904 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800906 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
907 DCHECK(locations->CanCall());
908 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
909 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
910 << "Unexpected instruction in read barrier for GC root slow path: "
911 << instruction_->DebugName();
912
913 __ Bind(GetEntryLabel());
914 SaveLiveRegisters(codegen, locations);
915
916 InvokeRuntimeCallingConvention calling_convention;
917 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
918 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
919 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100920 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800921 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
922 instruction_,
923 instruction_->GetDexPc(),
924 this);
925 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
926 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
927
928 RestoreLiveRegisters(codegen, locations);
929 __ Bc(GetExitLabel());
930 }
931
932 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
933
934 private:
935 const Location out_;
936 const Location root_;
937
938 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
939};
940
Alexey Frunze4dda3372015-06-01 18:31:49 -0700941CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
942 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100943 const CompilerOptions& compiler_options,
944 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700945 : CodeGenerator(graph,
946 kNumberOfGpuRegisters,
947 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000948 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700949 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
950 arraysize(kCoreCalleeSaves)),
951 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
952 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100953 compiler_options,
954 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100955 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956 location_builder_(graph, this),
957 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100958 move_resolver_(graph->GetAllocator(), this),
959 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800960 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800961 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800963 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100964 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000965 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100966 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000967 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100968 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000969 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800971 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800973 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100974 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700975 // Save RA (containing the return address) to mimic Quick.
976 AddAllocatedRegister(Location::RegisterLocation(RA));
977}
978
979#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100980// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
981#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700982#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700983
984void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700985 // Ensure that we fix up branches.
986 __ FinalizeCode();
987
988 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100989 StackMapStream* stack_map_stream = GetStackMapStream();
990 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800991 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +0000992 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips64);
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
Alexey Frunze627c1a02017-01-30 19:28:14 -08001642Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1643 dex::StringIndex string_index,
1644 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001645 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001646 return jit_string_patches_.GetOrCreate(
1647 StringReference(&dex_file, string_index),
1648 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1649}
1650
1651Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1652 dex::TypeIndex type_index,
1653 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001654 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001655 return jit_class_patches_.GetOrCreate(
1656 TypeReference(&dex_file, type_index),
1657 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1658}
1659
1660void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1661 const uint8_t* roots_data,
1662 const Literal* literal,
1663 uint64_t index_in_table) const {
1664 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1665 uintptr_t address =
1666 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1667 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1668}
1669
1670void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1671 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001672 const StringReference& string_reference = entry.first;
1673 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001674 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001675 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001676 }
1677 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001678 const TypeReference& type_reference = entry.first;
1679 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001680 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001681 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001682 }
1683}
1684
David Brazdil58282f42016-01-14 12:45:10 +00001685void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001686 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1687 blocked_core_registers_[ZERO] = true;
1688 blocked_core_registers_[K0] = true;
1689 blocked_core_registers_[K1] = true;
1690 blocked_core_registers_[GP] = true;
1691 blocked_core_registers_[SP] = true;
1692 blocked_core_registers_[RA] = true;
1693
Lazar Trsicd9672662015-09-03 17:33:01 +02001694 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1695 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001696 blocked_core_registers_[AT] = true;
1697 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001698 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001699 blocked_fpu_registers_[FTMP] = true;
1700
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001701 if (GetInstructionSetFeatures().HasMsa()) {
1702 // To be used just for MSA instructions.
1703 blocked_fpu_registers_[FTMP2] = true;
1704 }
1705
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 // Reserve suspend and thread registers.
1707 blocked_core_registers_[S0] = true;
1708 blocked_core_registers_[TR] = true;
1709
1710 // Reserve T9 for function calls
1711 blocked_core_registers_[T9] = true;
1712
Goran Jakovljevic782be112016-06-21 12:39:04 +02001713 if (GetGraph()->IsDebuggable()) {
1714 // Stubs do not save callee-save floating point registers. If the graph
1715 // is debuggable, we need to deal with these registers differently. For
1716 // now, just block them.
1717 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1718 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1719 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001720 }
1721}
1722
Alexey Frunze4dda3372015-06-01 18:31:49 -07001723size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1724 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001725 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001726}
1727
1728size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1729 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001730 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731}
1732
1733size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001734 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1735 FpuRegister(reg_id),
1736 SP,
1737 stack_index);
1738 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001739}
1740
1741size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001742 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1743 FpuRegister(reg_id),
1744 SP,
1745 stack_index);
1746 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001747}
1748
1749void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001750 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001751}
1752
1753void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001754 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001755}
1756
Calin Juravle175dc732015-08-25 15:42:32 +01001757void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001758 HInstruction* instruction,
1759 uint32_t dex_pc,
1760 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001761 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001762 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001763 if (EntrypointRequiresStackMap(entrypoint)) {
1764 RecordPcInfo(instruction, dex_pc, slow_path);
1765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766}
1767
Alexey Frunze15958152017-02-09 19:08:30 -08001768void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1769 HInstruction* instruction,
1770 SlowPathCode* slow_path) {
1771 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1772 GenerateInvokeRuntime(entry_point_offset);
1773}
1774
1775void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1776 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1777 __ Jalr(T9);
1778 __ Nop();
1779}
1780
Alexey Frunze4dda3372015-06-01 18:31:49 -07001781void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1782 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001783 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1784 const size_t status_byte_offset =
1785 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1786 constexpr uint32_t shifted_initialized_value =
1787 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1788
1789 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001790 __ Sltiu(TMP, TMP, shifted_initialized_value);
1791 __ Bnezc(TMP, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001792 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1793 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001794 __ Bind(slow_path->GetExitLabel());
1795}
1796
Vladimir Marko175e7862018-03-27 09:03:13 +00001797void InstructionCodeGeneratorMIPS64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1798 GpuRegister temp) {
1799 uint32_t path_to_root = check->GetBitstringPathToRoot();
1800 uint32_t mask = check->GetBitstringMask();
1801 DCHECK(IsPowerOfTwo(mask + 1));
1802 size_t mask_bits = WhichPowerOf2(mask + 1);
1803
1804 if (mask_bits == 16u) {
1805 // Load only the bitstring part of the status word.
1806 __ LoadFromOffset(
1807 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1808 // Compare the bitstring bits using XOR.
1809 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1810 } else {
1811 // /* uint32_t */ temp = temp->status_
1812 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1813 // Compare the bitstring bits using XOR.
1814 if (IsUint<16>(path_to_root)) {
1815 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1816 } else {
1817 __ LoadConst32(TMP, path_to_root);
1818 __ Xor(temp, temp, TMP);
1819 }
1820 // Shift out bits that do not contribute to the comparison.
1821 __ Sll(temp, temp, 32 - mask_bits);
1822 }
1823}
1824
Alexey Frunze4dda3372015-06-01 18:31:49 -07001825void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1826 __ Sync(0); // only stype 0 is supported
1827}
1828
1829void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1830 HBasicBlock* successor) {
1831 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001832 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1833
1834 if (slow_path == nullptr) {
1835 slow_path =
1836 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1837 instruction->SetSlowPath(slow_path);
1838 codegen_->AddSlowPath(slow_path);
1839 if (successor != nullptr) {
1840 DCHECK(successor->IsLoopHeader());
1841 }
1842 } else {
1843 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1844 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001845
1846 __ LoadFromOffset(kLoadUnsignedHalfword,
1847 TMP,
1848 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001849 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001850 if (successor == nullptr) {
1851 __ Bnezc(TMP, slow_path->GetEntryLabel());
1852 __ Bind(slow_path->GetReturnLabel());
1853 } else {
1854 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001855 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001856 // slow_path will return to GetLabelOf(successor).
1857 }
1858}
1859
1860InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1861 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001862 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001863 assembler_(codegen->GetAssembler()),
1864 codegen_(codegen) {}
1865
1866void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1867 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001870 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001871 case DataType::Type::kInt32:
1872 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001873 locations->SetInAt(0, Location::RequiresRegister());
1874 HInstruction* right = instruction->InputAt(1);
1875 bool can_use_imm = false;
1876 if (right->IsConstant()) {
1877 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1878 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1879 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001881 DCHECK(instruction->IsAdd() || instruction->IsSub());
1882 bool single_use = right->GetUses().HasExactlyOneElement();
1883 if (instruction->IsSub()) {
1884 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1885 imm = -imm;
1886 }
1887 }
1888 if (type == DataType::Type::kInt32) {
1889 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1890 } else {
1891 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1892 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001893 }
1894 }
1895 if (can_use_imm)
1896 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1897 else
1898 locations->SetInAt(1, Location::RequiresRegister());
1899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1900 }
1901 break;
1902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001903 case DataType::Type::kFloat32:
1904 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001905 locations->SetInAt(0, Location::RequiresFpuRegister());
1906 locations->SetInAt(1, Location::RequiresFpuRegister());
1907 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1912 }
1913}
1914
1915void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001916 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001917 LocationSummary* locations = instruction->GetLocations();
1918
1919 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001920 case DataType::Type::kInt32:
1921 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001922 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1923 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1924 Location rhs_location = locations->InAt(1);
1925
1926 GpuRegister rhs_reg = ZERO;
1927 int64_t rhs_imm = 0;
1928 bool use_imm = rhs_location.IsConstant();
1929 if (use_imm) {
1930 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1931 } else {
1932 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1933 }
1934
1935 if (instruction->IsAnd()) {
1936 if (use_imm)
1937 __ Andi(dst, lhs, rhs_imm);
1938 else
1939 __ And(dst, lhs, rhs_reg);
1940 } else if (instruction->IsOr()) {
1941 if (use_imm)
1942 __ Ori(dst, lhs, rhs_imm);
1943 else
1944 __ Or(dst, lhs, rhs_reg);
1945 } else if (instruction->IsXor()) {
1946 if (use_imm)
1947 __ Xori(dst, lhs, rhs_imm);
1948 else
1949 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001950 } else if (instruction->IsAdd() || instruction->IsSub()) {
1951 if (instruction->IsSub()) {
1952 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001953 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001954 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001955 if (use_imm) {
1956 if (IsInt<16>(rhs_imm)) {
1957 __ Addiu(dst, lhs, rhs_imm);
1958 } else {
1959 int16_t rhs_imm_high = High16Bits(rhs_imm);
1960 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1961 if (rhs_imm_low < 0) {
1962 rhs_imm_high += 1;
1963 }
1964 __ Aui(dst, lhs, rhs_imm_high);
1965 if (rhs_imm_low != 0) {
1966 __ Addiu(dst, dst, rhs_imm_low);
1967 }
1968 }
1969 } else {
1970 if (instruction->IsAdd()) {
1971 __ Addu(dst, lhs, rhs_reg);
1972 } else {
1973 DCHECK(instruction->IsSub());
1974 __ Subu(dst, lhs, rhs_reg);
1975 }
1976 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001977 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001978 if (use_imm) {
1979 if (IsInt<16>(rhs_imm)) {
1980 __ Daddiu(dst, lhs, rhs_imm);
1981 } else if (IsInt<32>(rhs_imm)) {
1982 int16_t rhs_imm_high = High16Bits(rhs_imm);
1983 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1984 bool overflow_hi16 = false;
1985 if (rhs_imm_low < 0) {
1986 rhs_imm_high += 1;
1987 overflow_hi16 = (rhs_imm_high == -32768);
1988 }
1989 __ Daui(dst, lhs, rhs_imm_high);
1990 if (rhs_imm_low != 0) {
1991 __ Daddiu(dst, dst, rhs_imm_low);
1992 }
1993 if (overflow_hi16) {
1994 __ Dahi(dst, 1);
1995 }
1996 } else {
1997 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1998 if (rhs_imm_low < 0) {
1999 rhs_imm += (INT64_C(1) << 16);
2000 }
2001 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
2002 if (rhs_imm_upper < 0) {
2003 rhs_imm += (INT64_C(1) << 32);
2004 }
2005 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
2006 if (rhs_imm_high < 0) {
2007 rhs_imm += (INT64_C(1) << 48);
2008 }
2009 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
2010 GpuRegister tmp = lhs;
2011 if (rhs_imm_low != 0) {
2012 __ Daddiu(dst, tmp, rhs_imm_low);
2013 tmp = dst;
2014 }
2015 // Dahi and Dati must use the same input and output register, so we have to initialize
2016 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
2017 // Daui(dst, lhs, 0).
2018 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
2019 __ Daui(dst, tmp, rhs_imm_upper);
2020 }
2021 if (rhs_imm_high != 0) {
2022 __ Dahi(dst, rhs_imm_high);
2023 }
2024 if (rhs_imm_top != 0) {
2025 __ Dati(dst, rhs_imm_top);
2026 }
2027 }
2028 } else if (instruction->IsAdd()) {
2029 __ Daddu(dst, lhs, rhs_reg);
2030 } else {
2031 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002033 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002034 }
2035 }
2036 break;
2037 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002038 case DataType::Type::kFloat32:
2039 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2041 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2042 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2043 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002045 __ AddS(dst, lhs, rhs);
2046 else
2047 __ AddD(dst, lhs, rhs);
2048 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 __ SubS(dst, lhs, rhs);
2051 else
2052 __ SubD(dst, lhs, rhs);
2053 } else {
2054 LOG(FATAL) << "Unexpected floating-point binary operation";
2055 }
2056 break;
2057 }
2058 default:
2059 LOG(FATAL) << "Unexpected binary operation type " << type;
2060 }
2061}
2062
2063void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002064 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002065
Vladimir Markoca6fff82017-10-03 14:49:14 +01002066 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002068 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 case DataType::Type::kInt32:
2070 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002071 locations->SetInAt(0, Location::RequiresRegister());
2072 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 break;
2075 }
2076 default:
2077 LOG(FATAL) << "Unexpected shift type " << type;
2078 }
2079}
2080
2081void InstructionCodeGeneratorMIPS64::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 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002085
2086 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 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2090 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2091 Location rhs_location = locations->InAt(1);
2092
2093 GpuRegister rhs_reg = ZERO;
2094 int64_t rhs_imm = 0;
2095 bool use_imm = rhs_location.IsConstant();
2096 if (use_imm) {
2097 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2098 } else {
2099 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2100 }
2101
2102 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002103 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002105
Alexey Frunze92d90602015-12-18 18:16:36 -08002106 if (shift_value == 0) {
2107 if (dst != lhs) {
2108 __ Move(dst, lhs);
2109 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002110 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 if (instr->IsShl()) {
2112 __ Sll(dst, lhs, shift_value);
2113 } else if (instr->IsShr()) {
2114 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002115 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002116 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002117 } else {
2118 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 }
2120 } else {
2121 if (shift_value < 32) {
2122 if (instr->IsShl()) {
2123 __ Dsll(dst, lhs, shift_value);
2124 } else if (instr->IsShr()) {
2125 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002126 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002127 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002128 } else {
2129 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002130 }
2131 } else {
2132 shift_value -= 32;
2133 if (instr->IsShl()) {
2134 __ Dsll32(dst, lhs, shift_value);
2135 } else if (instr->IsShr()) {
2136 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002137 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002138 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002139 } else {
2140 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002141 }
2142 }
2143 }
2144 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002145 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002146 if (instr->IsShl()) {
2147 __ Sllv(dst, lhs, rhs_reg);
2148 } else if (instr->IsShr()) {
2149 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002150 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002151 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002152 } else {
2153 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002154 }
2155 } else {
2156 if (instr->IsShl()) {
2157 __ Dsllv(dst, lhs, rhs_reg);
2158 } else if (instr->IsShr()) {
2159 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002160 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002161 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002162 } else {
2163 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002164 }
2165 }
2166 }
2167 break;
2168 }
2169 default:
2170 LOG(FATAL) << "Unexpected shift operation type " << type;
2171 }
2172}
2173
2174void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2175 HandleBinaryOp(instruction);
2176}
2177
2178void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2179 HandleBinaryOp(instruction);
2180}
2181
2182void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2183 HandleBinaryOp(instruction);
2184}
2185
2186void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2187 HandleBinaryOp(instruction);
2188}
2189
2190void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002191 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002192 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002193 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002194 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002195 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2196 object_array_get_with_read_barrier
2197 ? LocationSummary::kCallOnSlowPath
2198 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002199 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2200 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2201 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002202 locations->SetInAt(0, Location::RequiresRegister());
2203 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002205 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2206 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002207 // The output overlaps in the case of an object array get with
2208 // read barriers enabled: we do not want the move to overwrite the
2209 // array's location, as we need it to emit the read barrier.
2210 locations->SetOut(Location::RequiresRegister(),
2211 object_array_get_with_read_barrier
2212 ? Location::kOutputOverlap
2213 : Location::kNoOutputOverlap);
2214 }
2215 // We need a temporary register for the read barrier marking slow
2216 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2217 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002218 bool temp_needed = instruction->GetIndex()->IsConstant()
2219 ? !kBakerReadBarrierThunksEnableForFields
2220 : !kBakerReadBarrierThunksEnableForArrays;
2221 if (temp_needed) {
2222 locations->AddTemp(Location::RequiresRegister());
2223 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002224 }
2225}
2226
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002227static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2228 auto null_checker = [codegen, instruction]() {
2229 codegen->MaybeRecordImplicitNullCheck(instruction);
2230 };
2231 return null_checker;
2232}
2233
Alexey Frunze4dda3372015-06-01 18:31:49 -07002234void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2235 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002236 Location obj_loc = locations->InAt(0);
2237 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2238 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002239 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002240 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002241 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002244 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2245 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002246 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002247 case DataType::Type::kBool:
2248 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002249 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002250 if (index.IsConstant()) {
2251 size_t offset =
2252 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002253 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002254 } else {
2255 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002256 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002257 }
2258 break;
2259 }
2260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002261 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002262 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002263 if (index.IsConstant()) {
2264 size_t offset =
2265 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002266 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 } else {
2268 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002269 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002270 }
2271 break;
2272 }
2273
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002275 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002276 if (maybe_compressed_char_at) {
2277 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002278 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002279 __ Dext(TMP, TMP, 0, 1);
2280 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2281 "Expecting 0=compressed, 1=uncompressed");
2282 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002283 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002284 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2285 if (maybe_compressed_char_at) {
2286 Mips64Label uncompressed_load, done;
2287 __ Bnezc(TMP, &uncompressed_load);
2288 __ LoadFromOffset(kLoadUnsignedByte,
2289 out,
2290 obj,
2291 data_offset + (const_index << TIMES_1));
2292 __ Bc(&done);
2293 __ Bind(&uncompressed_load);
2294 __ LoadFromOffset(kLoadUnsignedHalfword,
2295 out,
2296 obj,
2297 data_offset + (const_index << TIMES_2));
2298 __ Bind(&done);
2299 } else {
2300 __ LoadFromOffset(kLoadUnsignedHalfword,
2301 out,
2302 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002303 data_offset + (const_index << TIMES_2),
2304 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002305 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002306 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002307 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2308 if (maybe_compressed_char_at) {
2309 Mips64Label uncompressed_load, done;
2310 __ Bnezc(TMP, &uncompressed_load);
2311 __ Daddu(TMP, obj, index_reg);
2312 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2313 __ Bc(&done);
2314 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002315 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002316 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2317 __ Bind(&done);
2318 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002319 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002320 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002321 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002322 }
2323 break;
2324 }
2325
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002326 case DataType::Type::kInt16: {
2327 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2328 if (index.IsConstant()) {
2329 size_t offset =
2330 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2331 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2332 } else {
2333 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2334 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2335 }
2336 break;
2337 }
2338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002339 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002340 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002341 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 LoadOperandType load_type =
2343 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002344 if (index.IsConstant()) {
2345 size_t offset =
2346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002347 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002348 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002349 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002350 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002351 }
2352 break;
2353 }
2354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002356 static_assert(
2357 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2358 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2359 // /* HeapReference<Object> */ out =
2360 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2361 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002362 bool temp_needed = index.IsConstant()
2363 ? !kBakerReadBarrierThunksEnableForFields
2364 : !kBakerReadBarrierThunksEnableForArrays;
2365 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002366 // Note that a potential implicit null check is handled in this
2367 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002368 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2369 if (index.IsConstant()) {
2370 // Array load with a constant index can be treated as a field load.
2371 size_t offset =
2372 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2373 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2374 out_loc,
2375 obj,
2376 offset,
2377 temp,
2378 /* needs_null_check */ false);
2379 } else {
2380 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2381 out_loc,
2382 obj,
2383 data_offset,
2384 index,
2385 temp,
2386 /* needs_null_check */ false);
2387 }
Alexey Frunze15958152017-02-09 19:08:30 -08002388 } else {
2389 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2390 if (index.IsConstant()) {
2391 size_t offset =
2392 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2393 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2394 // If read barriers are enabled, emit read barriers other than
2395 // Baker's using a slow path (and also unpoison the loaded
2396 // reference, if heap poisoning is enabled).
2397 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2398 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002399 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002400 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2401 // If read barriers are enabled, emit read barriers other than
2402 // Baker's using a slow path (and also unpoison the loaded
2403 // reference, if heap poisoning is enabled).
2404 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2405 out_loc,
2406 out_loc,
2407 obj_loc,
2408 data_offset,
2409 index);
2410 }
2411 }
2412 break;
2413 }
2414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002416 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002417 if (index.IsConstant()) {
2418 size_t offset =
2419 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002420 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002421 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002422 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002423 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002424 }
2425 break;
2426 }
2427
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002428 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002429 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002430 if (index.IsConstant()) {
2431 size_t offset =
2432 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002433 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002434 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002435 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002436 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002437 }
2438 break;
2439 }
2440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002442 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002443 if (index.IsConstant()) {
2444 size_t offset =
2445 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002446 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002447 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002448 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002449 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002450 }
2451 break;
2452 }
2453
Aart Bik66c158e2018-01-31 12:55:04 -08002454 case DataType::Type::kUint32:
2455 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002457 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2458 UNREACHABLE();
2459 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002460}
2461
2462void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002463 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002464 locations->SetInAt(0, Location::RequiresRegister());
2465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2466}
2467
2468void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2469 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002470 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002471 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2472 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2473 __ LoadFromOffset(kLoadWord, out, obj, offset);
2474 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002475 // Mask out compression flag from String's array length.
2476 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2477 __ Srl(out, out, 1u);
2478 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002479}
2480
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002481Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2482 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2483 ? Location::ConstantLocation(instruction->AsConstant())
2484 : Location::RequiresRegister();
2485}
2486
2487Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2488 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2489 // We can store a non-zero float or double constant without first loading it into the FPU,
2490 // but we should only prefer this if the constant has a single use.
2491 if (instruction->IsConstant() &&
2492 (instruction->AsConstant()->IsZeroBitPattern() ||
2493 instruction->GetUses().HasExactlyOneElement())) {
2494 return Location::ConstantLocation(instruction->AsConstant());
2495 // Otherwise fall through and require an FPU register for the constant.
2496 }
2497 return Location::RequiresFpuRegister();
2498}
2499
Alexey Frunze4dda3372015-06-01 18:31:49 -07002500void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002501 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002502
2503 bool needs_write_barrier =
2504 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2505 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2506
Vladimir Markoca6fff82017-10-03 14:49:14 +01002507 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002508 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002509 may_need_runtime_call_for_type_check ?
2510 LocationSummary::kCallOnSlowPath :
2511 LocationSummary::kNoCall);
2512
2513 locations->SetInAt(0, Location::RequiresRegister());
2514 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002516 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002518 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2519 }
2520 if (needs_write_barrier) {
2521 // Temporary register for the write barrier.
2522 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002523 }
2524}
2525
2526void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2527 LocationSummary* locations = instruction->GetLocations();
2528 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2529 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002530 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002532 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002533 bool needs_write_barrier =
2534 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002535 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002536 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002537
2538 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002539 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002540 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002543 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002544 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002545 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002546 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2547 }
2548 if (value_location.IsConstant()) {
2549 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2550 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2551 } else {
2552 GpuRegister value = value_location.AsRegister<GpuRegister>();
2553 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002554 }
2555 break;
2556 }
2557
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002558 case DataType::Type::kUint16:
2559 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002564 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002565 }
2566 if (value_location.IsConstant()) {
2567 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2568 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2569 } else {
2570 GpuRegister value = value_location.AsRegister<GpuRegister>();
2571 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002572 }
2573 break;
2574 }
2575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2578 if (index.IsConstant()) {
2579 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2580 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002581 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002582 }
2583 if (value_location.IsConstant()) {
2584 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2585 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2586 } else {
2587 GpuRegister value = value_location.AsRegister<GpuRegister>();
2588 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2589 }
2590 break;
2591 }
2592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002594 if (value_location.IsConstant()) {
2595 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002596 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002597 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002598 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002599 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002600 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002601 }
Alexey Frunze15958152017-02-09 19:08:30 -08002602 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2603 DCHECK_EQ(value, 0);
2604 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2605 DCHECK(!needs_write_barrier);
2606 DCHECK(!may_need_runtime_call_for_type_check);
2607 break;
2608 }
2609
2610 DCHECK(needs_write_barrier);
2611 GpuRegister value = value_location.AsRegister<GpuRegister>();
2612 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2613 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2614 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2615 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2616 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2617 Mips64Label done;
2618 SlowPathCodeMIPS64* slow_path = nullptr;
2619
2620 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002621 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002622 codegen_->AddSlowPath(slow_path);
2623 if (instruction->GetValueCanBeNull()) {
2624 Mips64Label non_zero;
2625 __ Bnezc(value, &non_zero);
2626 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2627 if (index.IsConstant()) {
2628 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002629 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002630 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002631 }
Alexey Frunze15958152017-02-09 19:08:30 -08002632 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2633 __ Bc(&done);
2634 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002635 }
Alexey Frunze15958152017-02-09 19:08:30 -08002636
2637 // Note that when read barriers are enabled, the type checks
2638 // are performed without read barriers. This is fine, even in
2639 // the case where a class object is in the from-space after
2640 // the flip, as a comparison involving such a type would not
2641 // produce a false positive; it may of course produce a false
2642 // negative, in which case we would take the ArraySet slow
2643 // path.
2644
2645 // /* HeapReference<Class> */ temp1 = obj->klass_
2646 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2647 __ MaybeUnpoisonHeapReference(temp1);
2648
2649 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2650 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2651 // /* HeapReference<Class> */ temp2 = value->klass_
2652 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2653 // If heap poisoning is enabled, no need to unpoison `temp1`
2654 // nor `temp2`, as we are comparing two poisoned references.
2655
2656 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2657 Mips64Label do_put;
2658 __ Beqc(temp1, temp2, &do_put);
2659 // If heap poisoning is enabled, the `temp1` reference has
2660 // not been unpoisoned yet; unpoison it now.
2661 __ MaybeUnpoisonHeapReference(temp1);
2662
2663 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2664 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2665 // If heap poisoning is enabled, no need to unpoison
2666 // `temp1`, as we are comparing against null below.
2667 __ Bnezc(temp1, slow_path->GetEntryLabel());
2668 __ Bind(&do_put);
2669 } else {
2670 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2671 }
2672 }
2673
2674 GpuRegister source = value;
2675 if (kPoisonHeapReferences) {
2676 // Note that in the case where `value` is a null reference,
2677 // we do not enter this block, as a null reference does not
2678 // need poisoning.
2679 __ Move(temp1, value);
2680 __ PoisonHeapReference(temp1);
2681 source = temp1;
2682 }
2683
2684 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2685 if (index.IsConstant()) {
2686 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002687 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002688 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002689 }
2690 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2691
2692 if (!may_need_runtime_call_for_type_check) {
2693 codegen_->MaybeRecordImplicitNullCheck(instruction);
2694 }
2695
2696 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2697
2698 if (done.IsLinked()) {
2699 __ Bind(&done);
2700 }
2701
2702 if (slow_path != nullptr) {
2703 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002704 }
2705 break;
2706 }
2707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002709 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002710 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002711 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002712 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002713 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002714 }
2715 if (value_location.IsConstant()) {
2716 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2717 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2718 } else {
2719 GpuRegister value = value_location.AsRegister<GpuRegister>();
2720 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002721 }
2722 break;
2723 }
2724
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002726 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002727 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002728 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002729 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002730 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002731 }
2732 if (value_location.IsConstant()) {
2733 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2734 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2735 } else {
2736 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2737 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002738 }
2739 break;
2740 }
2741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002743 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002744 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002745 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002747 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002748 }
2749 if (value_location.IsConstant()) {
2750 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2751 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2752 } else {
2753 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2754 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002755 }
2756 break;
2757 }
2758
Aart Bik66c158e2018-01-31 12:55:04 -08002759 case DataType::Type::kUint32:
2760 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002762 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2763 UNREACHABLE();
2764 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002765}
2766
2767void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002768 RegisterSet caller_saves = RegisterSet::Empty();
2769 InvokeRuntimeCallingConvention calling_convention;
2770 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2771 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2772 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002773
2774 HInstruction* index = instruction->InputAt(0);
2775 HInstruction* length = instruction->InputAt(1);
2776
2777 bool const_index = false;
2778 bool const_length = false;
2779
2780 if (index->IsConstant()) {
2781 if (length->IsConstant()) {
2782 const_index = true;
2783 const_length = true;
2784 } else {
2785 int32_t index_value = index->AsIntConstant()->GetValue();
2786 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2787 const_index = true;
2788 }
2789 }
2790 } else if (length->IsConstant()) {
2791 int32_t length_value = length->AsIntConstant()->GetValue();
2792 if (IsUint<15>(length_value)) {
2793 const_length = true;
2794 }
2795 }
2796
2797 locations->SetInAt(0, const_index
2798 ? Location::ConstantLocation(index->AsConstant())
2799 : Location::RequiresRegister());
2800 locations->SetInAt(1, const_length
2801 ? Location::ConstantLocation(length->AsConstant())
2802 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002803}
2804
2805void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2806 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002807 Location index_loc = locations->InAt(0);
2808 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002809
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002810 if (length_loc.IsConstant()) {
2811 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2812 if (index_loc.IsConstant()) {
2813 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2814 if (index < 0 || index >= length) {
2815 BoundsCheckSlowPathMIPS64* slow_path =
2816 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2817 codegen_->AddSlowPath(slow_path);
2818 __ Bc(slow_path->GetEntryLabel());
2819 } else {
2820 // Nothing to be done.
2821 }
2822 return;
2823 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002824
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002825 BoundsCheckSlowPathMIPS64* slow_path =
2826 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2827 codegen_->AddSlowPath(slow_path);
2828 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2829 if (length == 0) {
2830 __ Bc(slow_path->GetEntryLabel());
2831 } else if (length == 1) {
2832 __ Bnezc(index, slow_path->GetEntryLabel());
2833 } else {
2834 DCHECK(IsUint<15>(length)) << length;
2835 __ Sltiu(TMP, index, length);
2836 __ Beqzc(TMP, slow_path->GetEntryLabel());
2837 }
2838 } else {
2839 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2840 BoundsCheckSlowPathMIPS64* slow_path =
2841 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2842 codegen_->AddSlowPath(slow_path);
2843 if (index_loc.IsConstant()) {
2844 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2845 if (index < 0) {
2846 __ Bc(slow_path->GetEntryLabel());
2847 } else if (index == 0) {
2848 __ Blezc(length, slow_path->GetEntryLabel());
2849 } else {
2850 DCHECK(IsInt<16>(index + 1)) << index;
2851 __ Sltiu(TMP, length, index + 1);
2852 __ Bnezc(TMP, slow_path->GetEntryLabel());
2853 }
2854 } else {
2855 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2856 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2857 }
2858 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002859}
2860
Alexey Frunze15958152017-02-09 19:08:30 -08002861// Temp is used for read barrier.
2862static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2863 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002864 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002865 (kUseBakerReadBarrier ||
2866 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2867 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2868 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2869 return 1;
2870 }
2871 return 0;
2872}
2873
2874// Extra temp is used for read barrier.
2875static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2876 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2877}
2878
Alexey Frunze4dda3372015-06-01 18:31:49 -07002879void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002880 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002881 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002882 LocationSummary* locations =
2883 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002884 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00002885 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
2886 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2887 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2888 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
2889 } else {
2890 locations->SetInAt(1, Location::RequiresRegister());
2891 }
Alexey Frunze15958152017-02-09 19:08:30 -08002892 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002893}
2894
2895void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002896 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002897 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002898 Location obj_loc = locations->InAt(0);
2899 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00002900 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08002901 Location temp_loc = locations->GetTemp(0);
2902 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2903 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2904 DCHECK_LE(num_temps, 2u);
2905 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002906 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2907 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2908 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2909 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2910 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2911 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2912 const uint32_t object_array_data_offset =
2913 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2914 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002915
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002916 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002917 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002918 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2919 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002920 codegen_->AddSlowPath(slow_path);
2921
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002922 // Avoid this check if we know `obj` is not null.
2923 if (instruction->MustDoNullCheck()) {
2924 __ Beqzc(obj, &done);
2925 }
2926
2927 switch (type_check_kind) {
2928 case TypeCheckKind::kExactCheck:
2929 case TypeCheckKind::kArrayCheck: {
2930 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002931 GenerateReferenceLoadTwoRegisters(instruction,
2932 temp_loc,
2933 obj_loc,
2934 class_offset,
2935 maybe_temp2_loc,
2936 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002937 // Jump to slow path for throwing the exception or doing a
2938 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00002939 __ Bnec(temp, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002940 break;
2941 }
2942
2943 case TypeCheckKind::kAbstractClassCheck: {
2944 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002945 GenerateReferenceLoadTwoRegisters(instruction,
2946 temp_loc,
2947 obj_loc,
2948 class_offset,
2949 maybe_temp2_loc,
2950 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002951 // If the class is abstract, we eagerly fetch the super class of the
2952 // object to avoid doing a comparison we know will fail.
2953 Mips64Label loop;
2954 __ Bind(&loop);
2955 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002956 GenerateReferenceLoadOneRegister(instruction,
2957 temp_loc,
2958 super_offset,
2959 maybe_temp2_loc,
2960 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002961 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2962 // exception.
2963 __ Beqzc(temp, slow_path->GetEntryLabel());
2964 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00002965 __ Bnec(temp, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002966 break;
2967 }
2968
2969 case TypeCheckKind::kClassHierarchyCheck: {
2970 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002971 GenerateReferenceLoadTwoRegisters(instruction,
2972 temp_loc,
2973 obj_loc,
2974 class_offset,
2975 maybe_temp2_loc,
2976 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002977 // Walk over the class hierarchy to find a match.
2978 Mips64Label loop;
2979 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00002980 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002981 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002982 GenerateReferenceLoadOneRegister(instruction,
2983 temp_loc,
2984 super_offset,
2985 maybe_temp2_loc,
2986 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002987 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2988 // exception. Otherwise, jump to the beginning of the loop.
2989 __ Bnezc(temp, &loop);
2990 __ Bc(slow_path->GetEntryLabel());
2991 break;
2992 }
2993
2994 case TypeCheckKind::kArrayObjectCheck: {
2995 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002996 GenerateReferenceLoadTwoRegisters(instruction,
2997 temp_loc,
2998 obj_loc,
2999 class_offset,
3000 maybe_temp2_loc,
3001 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003002 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003003 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003004 // Otherwise, we need to check that the object's class is a non-primitive array.
3005 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003006 GenerateReferenceLoadOneRegister(instruction,
3007 temp_loc,
3008 component_offset,
3009 maybe_temp2_loc,
3010 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003011 // If the component type is null, jump to the slow path to throw the exception.
3012 __ Beqzc(temp, slow_path->GetEntryLabel());
3013 // Otherwise, the object is indeed an array, further check that this component
3014 // type is not a primitive type.
3015 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3016 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3017 __ Bnezc(temp, slow_path->GetEntryLabel());
3018 break;
3019 }
3020
3021 case TypeCheckKind::kUnresolvedCheck:
3022 // We always go into the type check slow path for the unresolved check case.
3023 // We cannot directly call the CheckCast runtime entry point
3024 // without resorting to a type checking slow path here (i.e. by
3025 // calling InvokeRuntime directly), as it would require to
3026 // assign fixed registers for the inputs of this HInstanceOf
3027 // instruction (following the runtime calling convention), which
3028 // might be cluttered by the potential first read barrier
3029 // emission at the beginning of this method.
3030 __ Bc(slow_path->GetEntryLabel());
3031 break;
3032
3033 case TypeCheckKind::kInterfaceCheck: {
3034 // Avoid read barriers to improve performance of the fast path. We can not get false
3035 // positives by doing this.
3036 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003037 GenerateReferenceLoadTwoRegisters(instruction,
3038 temp_loc,
3039 obj_loc,
3040 class_offset,
3041 maybe_temp2_loc,
3042 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003043 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003044 GenerateReferenceLoadTwoRegisters(instruction,
3045 temp_loc,
3046 temp_loc,
3047 iftable_offset,
3048 maybe_temp2_loc,
3049 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003050 // Iftable is never null.
3051 __ Lw(TMP, temp, array_length_offset);
3052 // Loop through the iftable and check if any class matches.
3053 Mips64Label loop;
3054 __ Bind(&loop);
3055 __ Beqzc(TMP, slow_path->GetEntryLabel());
3056 __ Lwu(AT, temp, object_array_data_offset);
3057 __ MaybeUnpoisonHeapReference(AT);
3058 // Go to next interface.
3059 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3060 __ Addiu(TMP, TMP, -2);
3061 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003062 __ Bnec(AT, cls.AsRegister<GpuRegister>(), &loop);
3063 break;
3064 }
3065
3066 case TypeCheckKind::kBitstringCheck: {
3067 // /* HeapReference<Class> */ temp = obj->klass_
3068 GenerateReferenceLoadTwoRegisters(instruction,
3069 temp_loc,
3070 obj_loc,
3071 class_offset,
3072 maybe_temp2_loc,
3073 kWithoutReadBarrier);
3074
3075 GenerateBitstringTypeCheckCompare(instruction, temp);
3076 __ Bnezc(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003077 break;
3078 }
3079 }
3080
3081 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003082 __ Bind(slow_path->GetExitLabel());
3083}
3084
3085void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3086 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003087 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003088 locations->SetInAt(0, Location::RequiresRegister());
3089 if (check->HasUses()) {
3090 locations->SetOut(Location::SameAsFirstInput());
3091 }
3092}
3093
3094void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3095 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003096 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097 check->GetLoadClass(),
3098 check,
3099 check->GetDexPc(),
3100 true);
3101 codegen_->AddSlowPath(slow_path);
3102 GenerateClassInitializationCheck(slow_path,
3103 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3104}
3105
3106void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003107 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003108
Vladimir Markoca6fff82017-10-03 14:49:14 +01003109 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003110
3111 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003113 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003114 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003115 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003116 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003117 case DataType::Type::kInt32:
3118 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003119 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003120 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3122 break;
3123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003124 case DataType::Type::kFloat32:
3125 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003126 locations->SetInAt(0, Location::RequiresFpuRegister());
3127 locations->SetInAt(1, Location::RequiresFpuRegister());
3128 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003129 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003130
3131 default:
3132 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3133 }
3134}
3135
3136void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3137 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003138 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003140
3141 // 0 if: left == right
3142 // 1 if: left > right
3143 // -1 if: left < right
3144 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003146 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003149 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 case DataType::Type::kInt32:
3151 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003152 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003153 Location rhs_location = locations->InAt(1);
3154 bool use_imm = rhs_location.IsConstant();
3155 GpuRegister rhs = ZERO;
3156 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003157 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003158 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3159 if (value != 0) {
3160 rhs = AT;
3161 __ LoadConst64(rhs, value);
3162 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003163 } else {
3164 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3165 if (value != 0) {
3166 rhs = AT;
3167 __ LoadConst32(rhs, value);
3168 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003169 }
3170 } else {
3171 rhs = rhs_location.AsRegister<GpuRegister>();
3172 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003173 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003174 __ Slt(res, rhs, lhs);
3175 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176 break;
3177 }
3178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003180 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3181 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3182 Mips64Label done;
3183 __ CmpEqS(FTMP, lhs, rhs);
3184 __ LoadConst32(res, 0);
3185 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003186 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003187 __ CmpLtS(FTMP, lhs, rhs);
3188 __ LoadConst32(res, -1);
3189 __ Bc1nez(FTMP, &done);
3190 __ LoadConst32(res, 1);
3191 } else {
3192 __ CmpLtS(FTMP, rhs, lhs);
3193 __ LoadConst32(res, 1);
3194 __ Bc1nez(FTMP, &done);
3195 __ LoadConst32(res, -1);
3196 }
3197 __ Bind(&done);
3198 break;
3199 }
3200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003201 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003202 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3203 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3204 Mips64Label done;
3205 __ CmpEqD(FTMP, lhs, rhs);
3206 __ LoadConst32(res, 0);
3207 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003208 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003209 __ CmpLtD(FTMP, lhs, rhs);
3210 __ LoadConst32(res, -1);
3211 __ Bc1nez(FTMP, &done);
3212 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003213 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003214 __ CmpLtD(FTMP, rhs, lhs);
3215 __ LoadConst32(res, 1);
3216 __ Bc1nez(FTMP, &done);
3217 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003218 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003219 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003220 break;
3221 }
3222
3223 default:
3224 LOG(FATAL) << "Unimplemented compare type " << in_type;
3225 }
3226}
3227
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003228void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003229 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003230 switch (instruction->InputAt(0)->GetType()) {
3231 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003232 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003233 locations->SetInAt(0, Location::RequiresRegister());
3234 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3235 break;
3236
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003237 case DataType::Type::kFloat32:
3238 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003239 locations->SetInAt(0, Location::RequiresFpuRegister());
3240 locations->SetInAt(1, Location::RequiresFpuRegister());
3241 break;
3242 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003243 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003244 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3245 }
3246}
3247
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003248void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003249 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003250 return;
3251 }
3252
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003254 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003255 switch (type) {
3256 default:
3257 // Integer case.
3258 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3259 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003260 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003261 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3262 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 case DataType::Type::kFloat32:
3264 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003265 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3266 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003267 }
3268}
3269
Alexey Frunzec857c742015-09-23 15:12:39 -07003270void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3271 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003272 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003273
3274 LocationSummary* locations = instruction->GetLocations();
3275 Location second = locations->InAt(1);
3276 DCHECK(second.IsConstant());
3277
3278 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3279 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3280 int64_t imm = Int64FromConstant(second.GetConstant());
3281 DCHECK(imm == 1 || imm == -1);
3282
3283 if (instruction->IsRem()) {
3284 __ Move(out, ZERO);
3285 } else {
3286 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003288 __ Subu(out, ZERO, dividend);
3289 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003291 __ Dsubu(out, ZERO, dividend);
3292 }
3293 } else if (out != dividend) {
3294 __ Move(out, dividend);
3295 }
3296 }
3297}
3298
3299void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3300 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003301 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003302
3303 LocationSummary* locations = instruction->GetLocations();
3304 Location second = locations->InAt(1);
3305 DCHECK(second.IsConstant());
3306
3307 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3308 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3309 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003310 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003311 int ctz_imm = CTZ(abs_imm);
3312
3313 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003315 if (ctz_imm == 1) {
3316 // Fast path for division by +/-2, which is very common.
3317 __ Srl(TMP, dividend, 31);
3318 } else {
3319 __ Sra(TMP, dividend, 31);
3320 __ Srl(TMP, TMP, 32 - ctz_imm);
3321 }
3322 __ Addu(out, dividend, TMP);
3323 __ Sra(out, out, ctz_imm);
3324 if (imm < 0) {
3325 __ Subu(out, ZERO, out);
3326 }
3327 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003328 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003329 if (ctz_imm == 1) {
3330 // Fast path for division by +/-2, which is very common.
3331 __ Dsrl32(TMP, dividend, 31);
3332 } else {
3333 __ Dsra32(TMP, dividend, 31);
3334 if (ctz_imm > 32) {
3335 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3336 } else {
3337 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3338 }
3339 }
3340 __ Daddu(out, dividend, TMP);
3341 if (ctz_imm < 32) {
3342 __ Dsra(out, out, ctz_imm);
3343 } else {
3344 __ Dsra32(out, out, ctz_imm - 32);
3345 }
3346 if (imm < 0) {
3347 __ Dsubu(out, ZERO, out);
3348 }
3349 }
3350 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003351 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003352 if (ctz_imm == 1) {
3353 // Fast path for modulo +/-2, which is very common.
3354 __ Sra(TMP, dividend, 31);
3355 __ Subu(out, dividend, TMP);
3356 __ Andi(out, out, 1);
3357 __ Addu(out, out, TMP);
3358 } else {
3359 __ Sra(TMP, dividend, 31);
3360 __ Srl(TMP, TMP, 32 - ctz_imm);
3361 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003362 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003363 __ Subu(out, out, TMP);
3364 }
3365 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003366 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003367 if (ctz_imm == 1) {
3368 // Fast path for modulo +/-2, which is very common.
3369 __ Dsra32(TMP, dividend, 31);
3370 __ Dsubu(out, dividend, TMP);
3371 __ Andi(out, out, 1);
3372 __ Daddu(out, out, TMP);
3373 } else {
3374 __ Dsra32(TMP, dividend, 31);
3375 if (ctz_imm > 32) {
3376 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3377 } else {
3378 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3379 }
3380 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003381 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003382 __ Dsubu(out, out, TMP);
3383 }
3384 }
3385 }
3386}
3387
3388void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3389 DCHECK(instruction->IsDiv() || instruction->IsRem());
3390
3391 LocationSummary* locations = instruction->GetLocations();
3392 Location second = locations->InAt(1);
3393 DCHECK(second.IsConstant());
3394
3395 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3396 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3397 int64_t imm = Int64FromConstant(second.GetConstant());
3398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003399 DataType::Type type = instruction->GetResultType();
3400 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003401
3402 int64_t magic;
3403 int shift;
3404 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003405 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003406 &magic,
3407 &shift);
3408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003409 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003410 __ LoadConst32(TMP, magic);
3411 __ MuhR6(TMP, dividend, TMP);
3412
3413 if (imm > 0 && magic < 0) {
3414 __ Addu(TMP, TMP, dividend);
3415 } else if (imm < 0 && magic > 0) {
3416 __ Subu(TMP, TMP, dividend);
3417 }
3418
3419 if (shift != 0) {
3420 __ Sra(TMP, TMP, shift);
3421 }
3422
3423 if (instruction->IsDiv()) {
3424 __ Sra(out, TMP, 31);
3425 __ Subu(out, TMP, out);
3426 } else {
3427 __ Sra(AT, TMP, 31);
3428 __ Subu(AT, TMP, AT);
3429 __ LoadConst32(TMP, imm);
3430 __ MulR6(TMP, AT, TMP);
3431 __ Subu(out, dividend, TMP);
3432 }
3433 } else {
3434 __ LoadConst64(TMP, magic);
3435 __ Dmuh(TMP, dividend, TMP);
3436
3437 if (imm > 0 && magic < 0) {
3438 __ Daddu(TMP, TMP, dividend);
3439 } else if (imm < 0 && magic > 0) {
3440 __ Dsubu(TMP, TMP, dividend);
3441 }
3442
3443 if (shift >= 32) {
3444 __ Dsra32(TMP, TMP, shift - 32);
3445 } else if (shift > 0) {
3446 __ Dsra(TMP, TMP, shift);
3447 }
3448
3449 if (instruction->IsDiv()) {
3450 __ Dsra32(out, TMP, 31);
3451 __ Dsubu(out, TMP, out);
3452 } else {
3453 __ Dsra32(AT, TMP, 31);
3454 __ Dsubu(AT, TMP, AT);
3455 __ LoadConst64(TMP, imm);
3456 __ Dmul(TMP, AT, TMP);
3457 __ Dsubu(out, dividend, TMP);
3458 }
3459 }
3460}
3461
3462void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3463 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003464 DataType::Type type = instruction->GetResultType();
3465 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003466
3467 LocationSummary* locations = instruction->GetLocations();
3468 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3469 Location second = locations->InAt(1);
3470
3471 if (second.IsConstant()) {
3472 int64_t imm = Int64FromConstant(second.GetConstant());
3473 if (imm == 0) {
3474 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3475 } else if (imm == 1 || imm == -1) {
3476 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003477 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003478 DivRemByPowerOfTwo(instruction);
3479 } else {
3480 DCHECK(imm <= -2 || imm >= 2);
3481 GenerateDivRemWithAnyConstant(instruction);
3482 }
3483 } else {
3484 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3485 GpuRegister divisor = second.AsRegister<GpuRegister>();
3486 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003487 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003488 __ DivR6(out, dividend, divisor);
3489 else
3490 __ Ddiv(out, dividend, divisor);
3491 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003492 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003493 __ ModR6(out, dividend, divisor);
3494 else
3495 __ Dmod(out, dividend, divisor);
3496 }
3497 }
3498}
3499
Alexey Frunze4dda3372015-06-01 18:31:49 -07003500void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3501 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003502 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003503 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003504 case DataType::Type::kInt32:
3505 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003506 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003507 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3509 break;
3510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003511 case DataType::Type::kFloat32:
3512 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003513 locations->SetInAt(0, Location::RequiresFpuRegister());
3514 locations->SetInAt(1, Location::RequiresFpuRegister());
3515 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3516 break;
3517
3518 default:
3519 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3520 }
3521}
3522
3523void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003524 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003525 LocationSummary* locations = instruction->GetLocations();
3526
3527 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 case DataType::Type::kInt32:
3529 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003530 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003531 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003532 case DataType::Type::kFloat32:
3533 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003534 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3535 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3536 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003537 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003538 __ DivS(dst, lhs, rhs);
3539 else
3540 __ DivD(dst, lhs, rhs);
3541 break;
3542 }
3543 default:
3544 LOG(FATAL) << "Unexpected div type " << type;
3545 }
3546}
3547
3548void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003549 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003550 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003551}
3552
3553void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3554 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003555 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003556 codegen_->AddSlowPath(slow_path);
3557 Location value = instruction->GetLocations()->InAt(0);
3558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003559 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003560
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003561 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003562 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003563 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003564 }
3565
3566 if (value.IsConstant()) {
3567 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3568 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003569 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003570 } else {
3571 // A division by a non-null constant is valid. We don't need to perform
3572 // any check, so simply fall through.
3573 }
3574 } else {
3575 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3576 }
3577}
3578
3579void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3580 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003581 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003582 locations->SetOut(Location::ConstantLocation(constant));
3583}
3584
3585void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3586 // Will be generated at use site.
3587}
3588
3589void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3590 exit->SetLocations(nullptr);
3591}
3592
3593void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3594}
3595
3596void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3597 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003598 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003599 locations->SetOut(Location::ConstantLocation(constant));
3600}
3601
3602void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3603 // Will be generated at use site.
3604}
3605
David Brazdilfc6a86a2015-06-26 10:33:45 +00003606void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003607 if (successor->IsExitBlock()) {
3608 DCHECK(got->GetPrevious()->AlwaysThrows());
3609 return; // no code needed
3610 }
3611
Alexey Frunze4dda3372015-06-01 18:31:49 -07003612 HBasicBlock* block = got->GetBlock();
3613 HInstruction* previous = got->GetPrevious();
3614 HLoopInformation* info = block->GetLoopInformation();
3615
3616 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01003617 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3618 __ Ld(AT, SP, kCurrentMethodStackOffset);
3619 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3620 __ Addiu(TMP, TMP, 1);
3621 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3622 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003623 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3624 return;
3625 }
3626 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3627 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3628 }
3629 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003630 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003631 }
3632}
3633
David Brazdilfc6a86a2015-06-26 10:33:45 +00003634void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3635 got->SetLocations(nullptr);
3636}
3637
3638void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3639 HandleGoto(got, got->GetSuccessor());
3640}
3641
3642void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3643 try_boundary->SetLocations(nullptr);
3644}
3645
3646void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3647 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3648 if (!successor->IsExitBlock()) {
3649 HandleGoto(try_boundary, successor);
3650 }
3651}
3652
Alexey Frunze299a9392015-12-08 16:08:02 -08003653void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3654 bool is64bit,
3655 LocationSummary* locations) {
3656 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3657 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3658 Location rhs_location = locations->InAt(1);
3659 GpuRegister rhs_reg = ZERO;
3660 int64_t rhs_imm = 0;
3661 bool use_imm = rhs_location.IsConstant();
3662 if (use_imm) {
3663 if (is64bit) {
3664 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3665 } else {
3666 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3667 }
3668 } else {
3669 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3670 }
3671 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3672
3673 switch (cond) {
3674 case kCondEQ:
3675 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003676 if (use_imm && IsInt<16>(-rhs_imm)) {
3677 if (rhs_imm == 0) {
3678 if (cond == kCondEQ) {
3679 __ Sltiu(dst, lhs, 1);
3680 } else {
3681 __ Sltu(dst, ZERO, lhs);
3682 }
3683 } else {
3684 if (is64bit) {
3685 __ Daddiu(dst, lhs, -rhs_imm);
3686 } else {
3687 __ Addiu(dst, lhs, -rhs_imm);
3688 }
3689 if (cond == kCondEQ) {
3690 __ Sltiu(dst, dst, 1);
3691 } else {
3692 __ Sltu(dst, ZERO, dst);
3693 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003694 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003695 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003696 if (use_imm && IsUint<16>(rhs_imm)) {
3697 __ Xori(dst, lhs, rhs_imm);
3698 } else {
3699 if (use_imm) {
3700 rhs_reg = TMP;
3701 __ LoadConst64(rhs_reg, rhs_imm);
3702 }
3703 __ Xor(dst, lhs, rhs_reg);
3704 }
3705 if (cond == kCondEQ) {
3706 __ Sltiu(dst, dst, 1);
3707 } else {
3708 __ Sltu(dst, ZERO, dst);
3709 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003710 }
3711 break;
3712
3713 case kCondLT:
3714 case kCondGE:
3715 if (use_imm && IsInt<16>(rhs_imm)) {
3716 __ Slti(dst, lhs, rhs_imm);
3717 } else {
3718 if (use_imm) {
3719 rhs_reg = TMP;
3720 __ LoadConst64(rhs_reg, rhs_imm);
3721 }
3722 __ Slt(dst, lhs, rhs_reg);
3723 }
3724 if (cond == kCondGE) {
3725 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3726 // only the slt instruction but no sge.
3727 __ Xori(dst, dst, 1);
3728 }
3729 break;
3730
3731 case kCondLE:
3732 case kCondGT:
3733 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3734 // Simulate lhs <= rhs via lhs < rhs + 1.
3735 __ Slti(dst, lhs, rhs_imm_plus_one);
3736 if (cond == kCondGT) {
3737 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3738 // only the slti instruction but no sgti.
3739 __ Xori(dst, dst, 1);
3740 }
3741 } else {
3742 if (use_imm) {
3743 rhs_reg = TMP;
3744 __ LoadConst64(rhs_reg, rhs_imm);
3745 }
3746 __ Slt(dst, rhs_reg, lhs);
3747 if (cond == kCondLE) {
3748 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3749 // only the slt instruction but no sle.
3750 __ Xori(dst, dst, 1);
3751 }
3752 }
3753 break;
3754
3755 case kCondB:
3756 case kCondAE:
3757 if (use_imm && IsInt<16>(rhs_imm)) {
3758 // Sltiu sign-extends its 16-bit immediate operand before
3759 // the comparison and thus lets us compare directly with
3760 // unsigned values in the ranges [0, 0x7fff] and
3761 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3762 __ Sltiu(dst, lhs, rhs_imm);
3763 } else {
3764 if (use_imm) {
3765 rhs_reg = TMP;
3766 __ LoadConst64(rhs_reg, rhs_imm);
3767 }
3768 __ Sltu(dst, lhs, rhs_reg);
3769 }
3770 if (cond == kCondAE) {
3771 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3772 // only the sltu instruction but no sgeu.
3773 __ Xori(dst, dst, 1);
3774 }
3775 break;
3776
3777 case kCondBE:
3778 case kCondA:
3779 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3780 // Simulate lhs <= rhs via lhs < rhs + 1.
3781 // Note that this only works if rhs + 1 does not overflow
3782 // to 0, hence the check above.
3783 // Sltiu sign-extends its 16-bit immediate operand before
3784 // the comparison and thus lets us compare directly with
3785 // unsigned values in the ranges [0, 0x7fff] and
3786 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3787 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3788 if (cond == kCondA) {
3789 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3790 // only the sltiu instruction but no sgtiu.
3791 __ Xori(dst, dst, 1);
3792 }
3793 } else {
3794 if (use_imm) {
3795 rhs_reg = TMP;
3796 __ LoadConst64(rhs_reg, rhs_imm);
3797 }
3798 __ Sltu(dst, rhs_reg, lhs);
3799 if (cond == kCondBE) {
3800 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3801 // only the sltu instruction but no sleu.
3802 __ Xori(dst, dst, 1);
3803 }
3804 }
3805 break;
3806 }
3807}
3808
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003809bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3810 bool is64bit,
3811 LocationSummary* input_locations,
3812 GpuRegister dst) {
3813 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3814 Location rhs_location = input_locations->InAt(1);
3815 GpuRegister rhs_reg = ZERO;
3816 int64_t rhs_imm = 0;
3817 bool use_imm = rhs_location.IsConstant();
3818 if (use_imm) {
3819 if (is64bit) {
3820 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3821 } else {
3822 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3823 }
3824 } else {
3825 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3826 }
3827 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3828
3829 switch (cond) {
3830 case kCondEQ:
3831 case kCondNE:
3832 if (use_imm && IsInt<16>(-rhs_imm)) {
3833 if (is64bit) {
3834 __ Daddiu(dst, lhs, -rhs_imm);
3835 } else {
3836 __ Addiu(dst, lhs, -rhs_imm);
3837 }
3838 } else if (use_imm && IsUint<16>(rhs_imm)) {
3839 __ Xori(dst, lhs, rhs_imm);
3840 } else {
3841 if (use_imm) {
3842 rhs_reg = TMP;
3843 __ LoadConst64(rhs_reg, rhs_imm);
3844 }
3845 __ Xor(dst, lhs, rhs_reg);
3846 }
3847 return (cond == kCondEQ);
3848
3849 case kCondLT:
3850 case kCondGE:
3851 if (use_imm && IsInt<16>(rhs_imm)) {
3852 __ Slti(dst, lhs, rhs_imm);
3853 } else {
3854 if (use_imm) {
3855 rhs_reg = TMP;
3856 __ LoadConst64(rhs_reg, rhs_imm);
3857 }
3858 __ Slt(dst, lhs, rhs_reg);
3859 }
3860 return (cond == kCondGE);
3861
3862 case kCondLE:
3863 case kCondGT:
3864 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3865 // Simulate lhs <= rhs via lhs < rhs + 1.
3866 __ Slti(dst, lhs, rhs_imm_plus_one);
3867 return (cond == kCondGT);
3868 } else {
3869 if (use_imm) {
3870 rhs_reg = TMP;
3871 __ LoadConst64(rhs_reg, rhs_imm);
3872 }
3873 __ Slt(dst, rhs_reg, lhs);
3874 return (cond == kCondLE);
3875 }
3876
3877 case kCondB:
3878 case kCondAE:
3879 if (use_imm && IsInt<16>(rhs_imm)) {
3880 // Sltiu sign-extends its 16-bit immediate operand before
3881 // the comparison and thus lets us compare directly with
3882 // unsigned values in the ranges [0, 0x7fff] and
3883 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3884 __ Sltiu(dst, lhs, rhs_imm);
3885 } else {
3886 if (use_imm) {
3887 rhs_reg = TMP;
3888 __ LoadConst64(rhs_reg, rhs_imm);
3889 }
3890 __ Sltu(dst, lhs, rhs_reg);
3891 }
3892 return (cond == kCondAE);
3893
3894 case kCondBE:
3895 case kCondA:
3896 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3897 // Simulate lhs <= rhs via lhs < rhs + 1.
3898 // Note that this only works if rhs + 1 does not overflow
3899 // to 0, hence the check above.
3900 // Sltiu sign-extends its 16-bit immediate operand before
3901 // the comparison and thus lets us compare directly with
3902 // unsigned values in the ranges [0, 0x7fff] and
3903 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3904 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3905 return (cond == kCondA);
3906 } else {
3907 if (use_imm) {
3908 rhs_reg = TMP;
3909 __ LoadConst64(rhs_reg, rhs_imm);
3910 }
3911 __ Sltu(dst, rhs_reg, lhs);
3912 return (cond == kCondBE);
3913 }
3914 }
3915}
3916
Alexey Frunze299a9392015-12-08 16:08:02 -08003917void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3918 bool is64bit,
3919 LocationSummary* locations,
3920 Mips64Label* label) {
3921 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3922 Location rhs_location = locations->InAt(1);
3923 GpuRegister rhs_reg = ZERO;
3924 int64_t rhs_imm = 0;
3925 bool use_imm = rhs_location.IsConstant();
3926 if (use_imm) {
3927 if (is64bit) {
3928 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3929 } else {
3930 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3931 }
3932 } else {
3933 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3934 }
3935
3936 if (use_imm && rhs_imm == 0) {
3937 switch (cond) {
3938 case kCondEQ:
3939 case kCondBE: // <= 0 if zero
3940 __ Beqzc(lhs, label);
3941 break;
3942 case kCondNE:
3943 case kCondA: // > 0 if non-zero
3944 __ Bnezc(lhs, label);
3945 break;
3946 case kCondLT:
3947 __ Bltzc(lhs, label);
3948 break;
3949 case kCondGE:
3950 __ Bgezc(lhs, label);
3951 break;
3952 case kCondLE:
3953 __ Blezc(lhs, label);
3954 break;
3955 case kCondGT:
3956 __ Bgtzc(lhs, label);
3957 break;
3958 case kCondB: // always false
3959 break;
3960 case kCondAE: // always true
3961 __ Bc(label);
3962 break;
3963 }
3964 } else {
3965 if (use_imm) {
3966 rhs_reg = TMP;
3967 __ LoadConst64(rhs_reg, rhs_imm);
3968 }
3969 switch (cond) {
3970 case kCondEQ:
3971 __ Beqc(lhs, rhs_reg, label);
3972 break;
3973 case kCondNE:
3974 __ Bnec(lhs, rhs_reg, label);
3975 break;
3976 case kCondLT:
3977 __ Bltc(lhs, rhs_reg, label);
3978 break;
3979 case kCondGE:
3980 __ Bgec(lhs, rhs_reg, label);
3981 break;
3982 case kCondLE:
3983 __ Bgec(rhs_reg, lhs, label);
3984 break;
3985 case kCondGT:
3986 __ Bltc(rhs_reg, lhs, label);
3987 break;
3988 case kCondB:
3989 __ Bltuc(lhs, rhs_reg, label);
3990 break;
3991 case kCondAE:
3992 __ Bgeuc(lhs, rhs_reg, label);
3993 break;
3994 case kCondBE:
3995 __ Bgeuc(rhs_reg, lhs, label);
3996 break;
3997 case kCondA:
3998 __ Bltuc(rhs_reg, lhs, label);
3999 break;
4000 }
4001 }
4002}
4003
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004004void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
4005 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004006 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004007 LocationSummary* locations) {
4008 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4009 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4010 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004011 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004012 switch (cond) {
4013 case kCondEQ:
4014 __ CmpEqS(FTMP, lhs, rhs);
4015 __ Mfc1(dst, FTMP);
4016 __ Andi(dst, dst, 1);
4017 break;
4018 case kCondNE:
4019 __ CmpEqS(FTMP, lhs, rhs);
4020 __ Mfc1(dst, FTMP);
4021 __ Addiu(dst, dst, 1);
4022 break;
4023 case kCondLT:
4024 if (gt_bias) {
4025 __ CmpLtS(FTMP, lhs, rhs);
4026 } else {
4027 __ CmpUltS(FTMP, lhs, rhs);
4028 }
4029 __ Mfc1(dst, FTMP);
4030 __ Andi(dst, dst, 1);
4031 break;
4032 case kCondLE:
4033 if (gt_bias) {
4034 __ CmpLeS(FTMP, lhs, rhs);
4035 } else {
4036 __ CmpUleS(FTMP, lhs, rhs);
4037 }
4038 __ Mfc1(dst, FTMP);
4039 __ Andi(dst, dst, 1);
4040 break;
4041 case kCondGT:
4042 if (gt_bias) {
4043 __ CmpUltS(FTMP, rhs, lhs);
4044 } else {
4045 __ CmpLtS(FTMP, rhs, lhs);
4046 }
4047 __ Mfc1(dst, FTMP);
4048 __ Andi(dst, dst, 1);
4049 break;
4050 case kCondGE:
4051 if (gt_bias) {
4052 __ CmpUleS(FTMP, rhs, lhs);
4053 } else {
4054 __ CmpLeS(FTMP, rhs, lhs);
4055 }
4056 __ Mfc1(dst, FTMP);
4057 __ Andi(dst, dst, 1);
4058 break;
4059 default:
4060 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4061 UNREACHABLE();
4062 }
4063 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004064 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004065 switch (cond) {
4066 case kCondEQ:
4067 __ CmpEqD(FTMP, lhs, rhs);
4068 __ Mfc1(dst, FTMP);
4069 __ Andi(dst, dst, 1);
4070 break;
4071 case kCondNE:
4072 __ CmpEqD(FTMP, lhs, rhs);
4073 __ Mfc1(dst, FTMP);
4074 __ Addiu(dst, dst, 1);
4075 break;
4076 case kCondLT:
4077 if (gt_bias) {
4078 __ CmpLtD(FTMP, lhs, rhs);
4079 } else {
4080 __ CmpUltD(FTMP, lhs, rhs);
4081 }
4082 __ Mfc1(dst, FTMP);
4083 __ Andi(dst, dst, 1);
4084 break;
4085 case kCondLE:
4086 if (gt_bias) {
4087 __ CmpLeD(FTMP, lhs, rhs);
4088 } else {
4089 __ CmpUleD(FTMP, lhs, rhs);
4090 }
4091 __ Mfc1(dst, FTMP);
4092 __ Andi(dst, dst, 1);
4093 break;
4094 case kCondGT:
4095 if (gt_bias) {
4096 __ CmpUltD(FTMP, rhs, lhs);
4097 } else {
4098 __ CmpLtD(FTMP, rhs, lhs);
4099 }
4100 __ Mfc1(dst, FTMP);
4101 __ Andi(dst, dst, 1);
4102 break;
4103 case kCondGE:
4104 if (gt_bias) {
4105 __ CmpUleD(FTMP, rhs, lhs);
4106 } else {
4107 __ CmpLeD(FTMP, rhs, lhs);
4108 }
4109 __ Mfc1(dst, FTMP);
4110 __ Andi(dst, dst, 1);
4111 break;
4112 default:
4113 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4114 UNREACHABLE();
4115 }
4116 }
4117}
4118
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004119bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4120 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004122 LocationSummary* input_locations,
4123 FpuRegister dst) {
4124 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4125 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004126 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004127 switch (cond) {
4128 case kCondEQ:
4129 __ CmpEqS(dst, lhs, rhs);
4130 return false;
4131 case kCondNE:
4132 __ CmpEqS(dst, lhs, rhs);
4133 return true;
4134 case kCondLT:
4135 if (gt_bias) {
4136 __ CmpLtS(dst, lhs, rhs);
4137 } else {
4138 __ CmpUltS(dst, lhs, rhs);
4139 }
4140 return false;
4141 case kCondLE:
4142 if (gt_bias) {
4143 __ CmpLeS(dst, lhs, rhs);
4144 } else {
4145 __ CmpUleS(dst, lhs, rhs);
4146 }
4147 return false;
4148 case kCondGT:
4149 if (gt_bias) {
4150 __ CmpUltS(dst, rhs, lhs);
4151 } else {
4152 __ CmpLtS(dst, rhs, lhs);
4153 }
4154 return false;
4155 case kCondGE:
4156 if (gt_bias) {
4157 __ CmpUleS(dst, rhs, lhs);
4158 } else {
4159 __ CmpLeS(dst, rhs, lhs);
4160 }
4161 return false;
4162 default:
4163 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4164 UNREACHABLE();
4165 }
4166 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004167 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004168 switch (cond) {
4169 case kCondEQ:
4170 __ CmpEqD(dst, lhs, rhs);
4171 return false;
4172 case kCondNE:
4173 __ CmpEqD(dst, lhs, rhs);
4174 return true;
4175 case kCondLT:
4176 if (gt_bias) {
4177 __ CmpLtD(dst, lhs, rhs);
4178 } else {
4179 __ CmpUltD(dst, lhs, rhs);
4180 }
4181 return false;
4182 case kCondLE:
4183 if (gt_bias) {
4184 __ CmpLeD(dst, lhs, rhs);
4185 } else {
4186 __ CmpUleD(dst, lhs, rhs);
4187 }
4188 return false;
4189 case kCondGT:
4190 if (gt_bias) {
4191 __ CmpUltD(dst, rhs, lhs);
4192 } else {
4193 __ CmpLtD(dst, rhs, lhs);
4194 }
4195 return false;
4196 case kCondGE:
4197 if (gt_bias) {
4198 __ CmpUleD(dst, rhs, lhs);
4199 } else {
4200 __ CmpLeD(dst, rhs, lhs);
4201 }
4202 return false;
4203 default:
4204 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4205 UNREACHABLE();
4206 }
4207 }
4208}
4209
Alexey Frunze299a9392015-12-08 16:08:02 -08004210void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4211 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004213 LocationSummary* locations,
4214 Mips64Label* label) {
4215 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4216 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004217 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004218 switch (cond) {
4219 case kCondEQ:
4220 __ CmpEqS(FTMP, lhs, rhs);
4221 __ Bc1nez(FTMP, label);
4222 break;
4223 case kCondNE:
4224 __ CmpEqS(FTMP, lhs, rhs);
4225 __ Bc1eqz(FTMP, label);
4226 break;
4227 case kCondLT:
4228 if (gt_bias) {
4229 __ CmpLtS(FTMP, lhs, rhs);
4230 } else {
4231 __ CmpUltS(FTMP, lhs, rhs);
4232 }
4233 __ Bc1nez(FTMP, label);
4234 break;
4235 case kCondLE:
4236 if (gt_bias) {
4237 __ CmpLeS(FTMP, lhs, rhs);
4238 } else {
4239 __ CmpUleS(FTMP, lhs, rhs);
4240 }
4241 __ Bc1nez(FTMP, label);
4242 break;
4243 case kCondGT:
4244 if (gt_bias) {
4245 __ CmpUltS(FTMP, rhs, lhs);
4246 } else {
4247 __ CmpLtS(FTMP, rhs, lhs);
4248 }
4249 __ Bc1nez(FTMP, label);
4250 break;
4251 case kCondGE:
4252 if (gt_bias) {
4253 __ CmpUleS(FTMP, rhs, lhs);
4254 } else {
4255 __ CmpLeS(FTMP, rhs, lhs);
4256 }
4257 __ Bc1nez(FTMP, label);
4258 break;
4259 default:
4260 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004261 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004262 }
4263 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004264 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004265 switch (cond) {
4266 case kCondEQ:
4267 __ CmpEqD(FTMP, lhs, rhs);
4268 __ Bc1nez(FTMP, label);
4269 break;
4270 case kCondNE:
4271 __ CmpEqD(FTMP, lhs, rhs);
4272 __ Bc1eqz(FTMP, label);
4273 break;
4274 case kCondLT:
4275 if (gt_bias) {
4276 __ CmpLtD(FTMP, lhs, rhs);
4277 } else {
4278 __ CmpUltD(FTMP, lhs, rhs);
4279 }
4280 __ Bc1nez(FTMP, label);
4281 break;
4282 case kCondLE:
4283 if (gt_bias) {
4284 __ CmpLeD(FTMP, lhs, rhs);
4285 } else {
4286 __ CmpUleD(FTMP, lhs, rhs);
4287 }
4288 __ Bc1nez(FTMP, label);
4289 break;
4290 case kCondGT:
4291 if (gt_bias) {
4292 __ CmpUltD(FTMP, rhs, lhs);
4293 } else {
4294 __ CmpLtD(FTMP, rhs, lhs);
4295 }
4296 __ Bc1nez(FTMP, label);
4297 break;
4298 case kCondGE:
4299 if (gt_bias) {
4300 __ CmpUleD(FTMP, rhs, lhs);
4301 } else {
4302 __ CmpLeD(FTMP, rhs, lhs);
4303 }
4304 __ Bc1nez(FTMP, label);
4305 break;
4306 default:
4307 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004308 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004309 }
4310 }
4311}
4312
Alexey Frunze4dda3372015-06-01 18:31:49 -07004313void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004314 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004315 Mips64Label* true_target,
4316 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004317 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004318
David Brazdil0debae72015-11-12 18:37:00 +00004319 if (true_target == nullptr && false_target == nullptr) {
4320 // Nothing to do. The code always falls through.
4321 return;
4322 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004323 // Constant condition, statically compared against "true" (integer value 1).
4324 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004325 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004326 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004327 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004328 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004329 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004330 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004331 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004332 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004333 }
David Brazdil0debae72015-11-12 18:37:00 +00004334 return;
4335 }
4336
4337 // The following code generates these patterns:
4338 // (1) true_target == nullptr && false_target != nullptr
4339 // - opposite condition true => branch to false_target
4340 // (2) true_target != nullptr && false_target == nullptr
4341 // - condition true => branch to true_target
4342 // (3) true_target != nullptr && false_target != nullptr
4343 // - condition true => branch to true_target
4344 // - branch to false_target
4345 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004346 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004347 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004348 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004349 if (true_target == nullptr) {
4350 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4351 } else {
4352 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4353 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004354 } else {
4355 // The condition instruction has not been materialized, use its inputs as
4356 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004357 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004358 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004359 LocationSummary* locations = cond->GetLocations();
4360 IfCondition if_cond = condition->GetCondition();
4361 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004362
David Brazdil0debae72015-11-12 18:37:00 +00004363 if (true_target == nullptr) {
4364 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004365 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004366 }
4367
Alexey Frunze299a9392015-12-08 16:08:02 -08004368 switch (type) {
4369 default:
4370 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4371 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004372 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004373 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4374 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004375 case DataType::Type::kFloat32:
4376 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004377 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4378 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004379 }
4380 }
David Brazdil0debae72015-11-12 18:37:00 +00004381
4382 // If neither branch falls through (case 3), the conditional branch to `true_target`
4383 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4384 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004385 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004386 }
4387}
4388
4389void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004390 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004391 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004392 locations->SetInAt(0, Location::RequiresRegister());
4393 }
4394}
4395
4396void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004397 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4398 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004399 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004400 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004401 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004402 nullptr : codegen_->GetLabelOf(false_successor);
4403 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004404}
4405
4406void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004407 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004408 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004409 InvokeRuntimeCallingConvention calling_convention;
4410 RegisterSet caller_saves = RegisterSet::Empty();
4411 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4412 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004413 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004414 locations->SetInAt(0, Location::RequiresRegister());
4415 }
4416}
4417
4418void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004419 SlowPathCodeMIPS64* slow_path =
4420 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004421 GenerateTestAndBranch(deoptimize,
4422 /* condition_input_index */ 0,
4423 slow_path->GetEntryLabel(),
4424 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004425}
4426
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004427// This function returns true if a conditional move can be generated for HSelect.
4428// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4429// branches and regular moves.
4430//
4431// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4432//
4433// While determining feasibility of a conditional move and setting inputs/outputs
4434// are two distinct tasks, this function does both because they share quite a bit
4435// of common logic.
4436static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4437 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4438 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4439 HCondition* condition = cond->AsCondition();
4440
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004441 DataType::Type cond_type =
4442 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4443 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004444
4445 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4446 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4447 bool is_true_value_zero_constant =
4448 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4449 bool is_false_value_zero_constant =
4450 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4451
4452 bool can_move_conditionally = false;
4453 bool use_const_for_false_in = false;
4454 bool use_const_for_true_in = false;
4455
4456 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004457 if (!DataType::IsFloatingPointType(cond_type)) {
4458 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004459 // Moving int/long on int/long condition.
4460 if (is_true_value_zero_constant) {
4461 // seleqz out_reg, false_reg, cond_reg
4462 can_move_conditionally = true;
4463 use_const_for_true_in = true;
4464 } else if (is_false_value_zero_constant) {
4465 // selnez out_reg, true_reg, cond_reg
4466 can_move_conditionally = true;
4467 use_const_for_false_in = true;
4468 } else if (materialized) {
4469 // Not materializing unmaterialized int conditions
4470 // to keep the instruction count low.
4471 // selnez AT, true_reg, cond_reg
4472 // seleqz TMP, false_reg, cond_reg
4473 // or out_reg, AT, TMP
4474 can_move_conditionally = true;
4475 }
4476 } else {
4477 // Moving float/double on int/long condition.
4478 if (materialized) {
4479 // Not materializing unmaterialized int conditions
4480 // to keep the instruction count low.
4481 can_move_conditionally = true;
4482 if (is_true_value_zero_constant) {
4483 // sltu TMP, ZERO, cond_reg
4484 // mtc1 TMP, temp_cond_reg
4485 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4486 use_const_for_true_in = true;
4487 } else if (is_false_value_zero_constant) {
4488 // sltu TMP, ZERO, cond_reg
4489 // mtc1 TMP, temp_cond_reg
4490 // selnez.fmt out_reg, true_reg, temp_cond_reg
4491 use_const_for_false_in = true;
4492 } else {
4493 // sltu TMP, ZERO, cond_reg
4494 // mtc1 TMP, temp_cond_reg
4495 // sel.fmt temp_cond_reg, false_reg, true_reg
4496 // mov.fmt out_reg, temp_cond_reg
4497 }
4498 }
4499 }
4500 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004501 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004502 // Moving int/long on float/double condition.
4503 can_move_conditionally = true;
4504 if (is_true_value_zero_constant) {
4505 // mfc1 TMP, temp_cond_reg
4506 // seleqz out_reg, false_reg, TMP
4507 use_const_for_true_in = true;
4508 } else if (is_false_value_zero_constant) {
4509 // mfc1 TMP, temp_cond_reg
4510 // selnez out_reg, true_reg, TMP
4511 use_const_for_false_in = true;
4512 } else {
4513 // mfc1 TMP, temp_cond_reg
4514 // selnez AT, true_reg, TMP
4515 // seleqz TMP, false_reg, TMP
4516 // or out_reg, AT, TMP
4517 }
4518 } else {
4519 // Moving float/double on float/double condition.
4520 can_move_conditionally = true;
4521 if (is_true_value_zero_constant) {
4522 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4523 use_const_for_true_in = true;
4524 } else if (is_false_value_zero_constant) {
4525 // selnez.fmt out_reg, true_reg, temp_cond_reg
4526 use_const_for_false_in = true;
4527 } else {
4528 // sel.fmt temp_cond_reg, false_reg, true_reg
4529 // mov.fmt out_reg, temp_cond_reg
4530 }
4531 }
4532 }
4533 }
4534
4535 if (can_move_conditionally) {
4536 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4537 } else {
4538 DCHECK(!use_const_for_false_in);
4539 DCHECK(!use_const_for_true_in);
4540 }
4541
4542 if (locations_to_set != nullptr) {
4543 if (use_const_for_false_in) {
4544 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4545 } else {
4546 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004547 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004548 ? Location::RequiresFpuRegister()
4549 : Location::RequiresRegister());
4550 }
4551 if (use_const_for_true_in) {
4552 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4553 } else {
4554 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004555 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004556 ? Location::RequiresFpuRegister()
4557 : Location::RequiresRegister());
4558 }
4559 if (materialized) {
4560 locations_to_set->SetInAt(2, Location::RequiresRegister());
4561 }
4562
4563 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004564 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004565 ? Location::RequiresFpuRegister()
4566 : Location::RequiresRegister());
4567 } else {
4568 locations_to_set->SetOut(Location::SameAsFirstInput());
4569 }
4570 }
4571
4572 return can_move_conditionally;
4573}
4574
4575
4576void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4577 LocationSummary* locations = select->GetLocations();
4578 Location dst = locations->Out();
4579 Location false_src = locations->InAt(0);
4580 Location true_src = locations->InAt(1);
4581 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4582 GpuRegister cond_reg = TMP;
4583 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004584 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004585 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004586 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004587
4588 if (IsBooleanValueOrMaterializedCondition(cond)) {
4589 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4590 } else {
4591 HCondition* condition = cond->AsCondition();
4592 LocationSummary* cond_locations = cond->GetLocations();
4593 IfCondition if_cond = condition->GetCondition();
4594 cond_type = condition->InputAt(0)->GetType();
4595 switch (cond_type) {
4596 default:
4597 cond_inverted = MaterializeIntLongCompare(if_cond,
4598 /* is64bit */ false,
4599 cond_locations,
4600 cond_reg);
4601 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004602 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004603 cond_inverted = MaterializeIntLongCompare(if_cond,
4604 /* is64bit */ true,
4605 cond_locations,
4606 cond_reg);
4607 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004608 case DataType::Type::kFloat32:
4609 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004610 cond_inverted = MaterializeFpCompare(if_cond,
4611 condition->IsGtBias(),
4612 cond_type,
4613 cond_locations,
4614 fcond_reg);
4615 break;
4616 }
4617 }
4618
4619 if (true_src.IsConstant()) {
4620 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4621 }
4622 if (false_src.IsConstant()) {
4623 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4624 }
4625
4626 switch (dst_type) {
4627 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004628 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004629 __ Mfc1(cond_reg, fcond_reg);
4630 }
4631 if (true_src.IsConstant()) {
4632 if (cond_inverted) {
4633 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4634 } else {
4635 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4636 }
4637 } else if (false_src.IsConstant()) {
4638 if (cond_inverted) {
4639 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4640 } else {
4641 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4642 }
4643 } else {
4644 DCHECK_NE(cond_reg, AT);
4645 if (cond_inverted) {
4646 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4647 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4648 } else {
4649 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4650 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4651 }
4652 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4653 }
4654 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004655 case DataType::Type::kFloat32: {
4656 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004657 // sel*.fmt tests bit 0 of the condition register, account for that.
4658 __ Sltu(TMP, ZERO, cond_reg);
4659 __ Mtc1(TMP, fcond_reg);
4660 }
4661 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4662 if (true_src.IsConstant()) {
4663 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4664 if (cond_inverted) {
4665 __ SelnezS(dst_reg, src_reg, fcond_reg);
4666 } else {
4667 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4668 }
4669 } else if (false_src.IsConstant()) {
4670 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4671 if (cond_inverted) {
4672 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4673 } else {
4674 __ SelnezS(dst_reg, src_reg, fcond_reg);
4675 }
4676 } else {
4677 if (cond_inverted) {
4678 __ SelS(fcond_reg,
4679 true_src.AsFpuRegister<FpuRegister>(),
4680 false_src.AsFpuRegister<FpuRegister>());
4681 } else {
4682 __ SelS(fcond_reg,
4683 false_src.AsFpuRegister<FpuRegister>(),
4684 true_src.AsFpuRegister<FpuRegister>());
4685 }
4686 __ MovS(dst_reg, fcond_reg);
4687 }
4688 break;
4689 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004690 case DataType::Type::kFloat64: {
4691 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004692 // sel*.fmt tests bit 0 of the condition register, account for that.
4693 __ Sltu(TMP, ZERO, cond_reg);
4694 __ Mtc1(TMP, fcond_reg);
4695 }
4696 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4697 if (true_src.IsConstant()) {
4698 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4699 if (cond_inverted) {
4700 __ SelnezD(dst_reg, src_reg, fcond_reg);
4701 } else {
4702 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4703 }
4704 } else if (false_src.IsConstant()) {
4705 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4706 if (cond_inverted) {
4707 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4708 } else {
4709 __ SelnezD(dst_reg, src_reg, fcond_reg);
4710 }
4711 } else {
4712 if (cond_inverted) {
4713 __ SelD(fcond_reg,
4714 true_src.AsFpuRegister<FpuRegister>(),
4715 false_src.AsFpuRegister<FpuRegister>());
4716 } else {
4717 __ SelD(fcond_reg,
4718 false_src.AsFpuRegister<FpuRegister>(),
4719 true_src.AsFpuRegister<FpuRegister>());
4720 }
4721 __ MovD(dst_reg, fcond_reg);
4722 }
4723 break;
4724 }
4725 }
4726}
4727
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004728void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004729 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004730 LocationSummary(flag, LocationSummary::kNoCall);
4731 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004732}
4733
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004734void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4735 __ LoadFromOffset(kLoadWord,
4736 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4737 SP,
4738 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004739}
4740
David Brazdil74eb1b22015-12-14 11:44:01 +00004741void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004742 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004743 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004744}
4745
4746void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004747 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4748 GenConditionalMove(select);
4749 } else {
4750 LocationSummary* locations = select->GetLocations();
4751 Mips64Label false_target;
4752 GenerateTestAndBranch(select,
4753 /* condition_input_index */ 2,
4754 /* true_target */ nullptr,
4755 &false_target);
4756 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4757 __ Bind(&false_target);
4758 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004759}
4760
David Srbecky0cf44932015-12-09 14:09:59 +00004761void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004762 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004763}
4764
David Srbeckyd28f4a02016-03-14 17:14:24 +00004765void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4766 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004767}
4768
4769void CodeGeneratorMIPS64::GenerateNop() {
4770 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004771}
4772
Alexey Frunze4dda3372015-06-01 18:31:49 -07004773void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004774 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004775 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004776 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004777 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004778 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004779 instruction,
4780 object_field_get_with_read_barrier
4781 ? LocationSummary::kCallOnSlowPath
4782 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004783 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4784 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4785 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004786 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004787 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004788 locations->SetOut(Location::RequiresFpuRegister());
4789 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004790 // The output overlaps in the case of an object field get with
4791 // read barriers enabled: we do not want the move to overwrite the
4792 // object's location, as we need it to emit the read barrier.
4793 locations->SetOut(Location::RequiresRegister(),
4794 object_field_get_with_read_barrier
4795 ? Location::kOutputOverlap
4796 : Location::kNoOutputOverlap);
4797 }
4798 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4799 // We need a temporary register for the read barrier marking slow
4800 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004801 if (!kBakerReadBarrierThunksEnableForFields) {
4802 locations->AddTemp(Location::RequiresRegister());
4803 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004804 }
4805}
4806
4807void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4808 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004809 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4810 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004811 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004812 Location obj_loc = locations->InAt(0);
4813 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4814 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004815 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004816 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004817 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004818 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4819
Alexey Frunze4dda3372015-06-01 18:31:49 -07004820 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004821 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004822 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004823 load_type = kLoadUnsignedByte;
4824 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004825 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004826 load_type = kLoadSignedByte;
4827 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004828 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004829 load_type = kLoadUnsignedHalfword;
4830 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004831 case DataType::Type::kInt16:
4832 load_type = kLoadSignedHalfword;
4833 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 case DataType::Type::kInt32:
4835 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004836 load_type = kLoadWord;
4837 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004838 case DataType::Type::kInt64:
4839 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004840 load_type = kLoadDoubleword;
4841 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004842 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004843 load_type = kLoadUnsignedWord;
4844 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004845 case DataType::Type::kUint32:
4846 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004847 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004848 LOG(FATAL) << "Unreachable type " << type;
4849 UNREACHABLE();
4850 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004851 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004852 DCHECK(dst_loc.IsRegister());
4853 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004854 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004855 // /* HeapReference<Object> */ dst = *(obj + offset)
4856 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004857 Location temp_loc =
4858 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004859 // Note that a potential implicit null check is handled in this
4860 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4861 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4862 dst_loc,
4863 obj,
4864 offset,
4865 temp_loc,
4866 /* needs_null_check */ true);
4867 if (is_volatile) {
4868 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4869 }
4870 } else {
4871 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4872 if (is_volatile) {
4873 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4874 }
4875 // If read barriers are enabled, emit read barriers other than
4876 // Baker's using a slow path (and also unpoison the loaded
4877 // reference, if heap poisoning is enabled).
4878 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4879 }
4880 } else {
4881 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4882 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004883 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004884 DCHECK(dst_loc.IsFpuRegister());
4885 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004886 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004887 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004888
Alexey Frunze15958152017-02-09 19:08:30 -08004889 // Memory barriers, in the case of references, are handled in the
4890 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004891 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004892 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004893 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004894}
4895
4896void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4897 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4898 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004899 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004900 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004901 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004902 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004903 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004904 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004905 }
4906}
4907
4908void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004909 const FieldInfo& field_info,
4910 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004911 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004912 LocationSummary* locations = instruction->GetLocations();
4913 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004914 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004915 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004916 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004917 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4918 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004919 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4920
Alexey Frunze4dda3372015-06-01 18:31:49 -07004921 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004922 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004923 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004925 store_type = kStoreByte;
4926 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004927 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004928 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004929 store_type = kStoreHalfword;
4930 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004931 case DataType::Type::kInt32:
4932 case DataType::Type::kFloat32:
4933 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004934 store_type = kStoreWord;
4935 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004936 case DataType::Type::kInt64:
4937 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004938 store_type = kStoreDoubleword;
4939 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004940 case DataType::Type::kUint32:
4941 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004942 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004943 LOG(FATAL) << "Unreachable type " << type;
4944 UNREACHABLE();
4945 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004946
Alexey Frunze15958152017-02-09 19:08:30 -08004947 if (is_volatile) {
4948 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4949 }
4950
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004951 if (value_location.IsConstant()) {
4952 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4953 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4954 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004956 DCHECK(value_location.IsRegister());
4957 GpuRegister src = value_location.AsRegister<GpuRegister>();
4958 if (kPoisonHeapReferences && needs_write_barrier) {
4959 // Note that in the case where `value` is a null reference,
4960 // we do not enter this block, as a null reference does not
4961 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004962 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004963 __ PoisonHeapReference(TMP, src);
4964 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4965 } else {
4966 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4967 }
4968 } else {
4969 DCHECK(value_location.IsFpuRegister());
4970 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4971 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4972 }
4973 }
Alexey Frunze15958152017-02-09 19:08:30 -08004974
Alexey Frunzec061de12017-02-14 13:27:23 -08004975 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004976 DCHECK(value_location.IsRegister());
4977 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004978 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004979 }
Alexey Frunze15958152017-02-09 19:08:30 -08004980
4981 if (is_volatile) {
4982 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4983 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004984}
4985
4986void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4987 HandleFieldGet(instruction, instruction->GetFieldInfo());
4988}
4989
4990void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4991 HandleFieldGet(instruction, instruction->GetFieldInfo());
4992}
4993
4994void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4995 HandleFieldSet(instruction, instruction->GetFieldInfo());
4996}
4997
4998void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004999 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005000}
5001
Alexey Frunze15958152017-02-09 19:08:30 -08005002void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
5003 HInstruction* instruction,
5004 Location out,
5005 uint32_t offset,
5006 Location maybe_temp,
5007 ReadBarrierOption read_barrier_option) {
5008 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5009 if (read_barrier_option == kWithReadBarrier) {
5010 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005011 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
5012 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5013 }
Alexey Frunze15958152017-02-09 19:08:30 -08005014 if (kUseBakerReadBarrier) {
5015 // Load with fast path based Baker's read barrier.
5016 // /* HeapReference<Object> */ out = *(out + offset)
5017 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5018 out,
5019 out_reg,
5020 offset,
5021 maybe_temp,
5022 /* needs_null_check */ false);
5023 } else {
5024 // Load with slow path based read barrier.
5025 // Save the value of `out` into `maybe_temp` before overwriting it
5026 // in the following move operation, as we will need it for the
5027 // read barrier below.
5028 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
5029 // /* HeapReference<Object> */ out = *(out + offset)
5030 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5031 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5032 }
5033 } else {
5034 // Plain load with no read barrier.
5035 // /* HeapReference<Object> */ out = *(out + offset)
5036 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5037 __ MaybeUnpoisonHeapReference(out_reg);
5038 }
5039}
5040
5041void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
5042 HInstruction* instruction,
5043 Location out,
5044 Location obj,
5045 uint32_t offset,
5046 Location maybe_temp,
5047 ReadBarrierOption read_barrier_option) {
5048 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5049 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
5050 if (read_barrier_option == kWithReadBarrier) {
5051 CHECK(kEmitCompilerReadBarrier);
5052 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005053 if (!kBakerReadBarrierThunksEnableForFields) {
5054 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5055 }
Alexey Frunze15958152017-02-09 19:08:30 -08005056 // Load with fast path based Baker's read barrier.
5057 // /* HeapReference<Object> */ out = *(obj + offset)
5058 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5059 out,
5060 obj_reg,
5061 offset,
5062 maybe_temp,
5063 /* needs_null_check */ false);
5064 } else {
5065 // Load with slow path based read barrier.
5066 // /* HeapReference<Object> */ out = *(obj + offset)
5067 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5068 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5069 }
5070 } else {
5071 // Plain load with no read barrier.
5072 // /* HeapReference<Object> */ out = *(obj + offset)
5073 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5074 __ MaybeUnpoisonHeapReference(out_reg);
5075 }
5076}
5077
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005078static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5079 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5080 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5081 return reg - V0;
5082 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5083 return 13 + (reg - S2);
5084 } else if (reg == S8) { // One more.
5085 return 19;
5086 }
5087 LOG(FATAL) << "Unexpected register " << reg;
5088 UNREACHABLE();
5089}
5090
5091static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5092 int num = GetBakerMarkThunkNumber(reg) +
5093 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5094 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5095}
5096
5097static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5098 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5099 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5100}
5101
5102void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5103 Location root,
5104 GpuRegister obj,
5105 uint32_t offset,
5106 ReadBarrierOption read_barrier_option,
5107 Mips64Label* label_low) {
5108 if (label_low != nullptr) {
5109 DCHECK_EQ(offset, 0x5678u);
5110 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005111 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005112 if (read_barrier_option == kWithReadBarrier) {
5113 DCHECK(kEmitCompilerReadBarrier);
5114 if (kUseBakerReadBarrier) {
5115 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5116 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005117 if (kBakerReadBarrierThunksEnableForGcRoots) {
5118 // Note that we do not actually check the value of `GetIsGcMarking()`
5119 // to decide whether to mark the loaded GC root or not. Instead, we
5120 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5121 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5122 // vice versa.
5123 //
5124 // We use thunks for the slow path. That thunk checks the reference
5125 // and jumps to the entrypoint if needed.
5126 //
5127 // temp = Thread::Current()->pReadBarrierMarkReg00
5128 // // AKA &art_quick_read_barrier_mark_introspection.
5129 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5130 // if (temp != nullptr) {
5131 // temp = &gc_root_thunk<root_reg>
5132 // root = temp(root)
5133 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005134
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005135 const int32_t entry_point_offset =
5136 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5137 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5138 int16_t offset_low = Low16Bits(offset);
5139 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5140 // extension in lwu.
5141 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5142 GpuRegister base = short_offset ? obj : TMP;
5143 // Loading the entrypoint does not require a load acquire since it is only changed when
5144 // threads are suspended or running a checkpoint.
5145 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5146 if (!short_offset) {
5147 DCHECK(!label_low);
5148 __ Daui(base, obj, offset_high);
5149 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005150 Mips64Label skip_call;
5151 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005152 if (label_low != nullptr) {
5153 DCHECK(short_offset);
5154 __ Bind(label_low);
5155 }
5156 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5157 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5158 // in delay slot.
5159 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005160 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005161 } else {
5162 // Note that we do not actually check the value of `GetIsGcMarking()`
5163 // to decide whether to mark the loaded GC root or not. Instead, we
5164 // load into `temp` (T9) the read barrier mark entry point corresponding
5165 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5166 // is false, and vice versa.
5167 //
5168 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5169 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5170 // if (temp != null) {
5171 // root = temp(root)
5172 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005173
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005174 if (label_low != nullptr) {
5175 __ Bind(label_low);
5176 }
5177 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5178 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5179 static_assert(
5180 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5181 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5182 "have different sizes.");
5183 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5184 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5185 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005186
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005187 // Slow path marking the GC root `root`.
5188 Location temp = Location::RegisterLocation(T9);
5189 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005190 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005191 instruction,
5192 root,
5193 /*entrypoint*/ temp);
5194 codegen_->AddSlowPath(slow_path);
5195
5196 const int32_t entry_point_offset =
5197 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5198 // Loading the entrypoint does not require a load acquire since it is only changed when
5199 // threads are suspended or running a checkpoint.
5200 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5201 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5202 __ Bind(slow_path->GetExitLabel());
5203 }
Alexey Frunze15958152017-02-09 19:08:30 -08005204 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005205 if (label_low != nullptr) {
5206 __ Bind(label_low);
5207 }
Alexey Frunze15958152017-02-09 19:08:30 -08005208 // GC root loaded through a slow path for read barriers other
5209 // than Baker's.
5210 // /* GcRoot<mirror::Object>* */ root = obj + offset
5211 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5212 // /* mirror::Object* */ root = root->Read()
5213 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5214 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005215 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005216 if (label_low != nullptr) {
5217 __ Bind(label_low);
5218 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005219 // Plain GC root load with no read barrier.
5220 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5221 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5222 // Note that GC roots are not affected by heap poisoning, thus we
5223 // do not have to unpoison `root_reg` here.
5224 }
5225}
5226
Alexey Frunze15958152017-02-09 19:08:30 -08005227void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5228 Location ref,
5229 GpuRegister obj,
5230 uint32_t offset,
5231 Location temp,
5232 bool needs_null_check) {
5233 DCHECK(kEmitCompilerReadBarrier);
5234 DCHECK(kUseBakerReadBarrier);
5235
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005236 if (kBakerReadBarrierThunksEnableForFields) {
5237 // Note that we do not actually check the value of `GetIsGcMarking()`
5238 // to decide whether to mark the loaded reference or not. Instead, we
5239 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5240 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5241 // vice versa.
5242 //
5243 // We use thunks for the slow path. That thunk checks the reference
5244 // and jumps to the entrypoint if needed. If the holder is not gray,
5245 // it issues a load-load memory barrier and returns to the original
5246 // reference load.
5247 //
5248 // temp = Thread::Current()->pReadBarrierMarkReg00
5249 // // AKA &art_quick_read_barrier_mark_introspection.
5250 // if (temp != nullptr) {
5251 // temp = &field_array_thunk<holder_reg>
5252 // temp()
5253 // }
5254 // not_gray_return_address:
5255 // // If the offset is too large to fit into the lw instruction, we
5256 // // use an adjusted base register (TMP) here. This register
5257 // // receives bits 16 ... 31 of the offset before the thunk invocation
5258 // // and the thunk benefits from it.
5259 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5260 // gray_return_address:
5261
5262 DCHECK(temp.IsInvalid());
5263 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5264 const int32_t entry_point_offset =
5265 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5266 // There may have or may have not been a null check if the field offset is smaller than
5267 // the page size.
5268 // There must've been a null check in case it's actually a load from an array.
5269 // We will, however, perform an explicit null check in the thunk as it's easier to
5270 // do it than not.
5271 if (instruction->IsArrayGet()) {
5272 DCHECK(!needs_null_check);
5273 }
5274 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5275 // Loading the entrypoint does not require a load acquire since it is only changed when
5276 // threads are suspended or running a checkpoint.
5277 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5278 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005279 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005280 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005281 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005282 __ Nop(); // In forbidden slot.
5283 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005284 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005285 // /* HeapReference<Object> */ ref = *(obj + offset)
5286 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5287 } else {
5288 int16_t offset_low = Low16Bits(offset);
5289 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005290 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005291 __ Daui(TMP, obj, offset_high); // In delay slot.
5292 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005293 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005294 // /* HeapReference<Object> */ ref = *(obj + offset)
5295 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5296 }
5297 if (needs_null_check) {
5298 MaybeRecordImplicitNullCheck(instruction);
5299 }
5300 __ MaybeUnpoisonHeapReference(ref_reg);
5301 return;
5302 }
5303
Alexey Frunze15958152017-02-09 19:08:30 -08005304 // /* HeapReference<Object> */ ref = *(obj + offset)
5305 Location no_index = Location::NoLocation();
5306 ScaleFactor no_scale_factor = TIMES_1;
5307 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5308 ref,
5309 obj,
5310 offset,
5311 no_index,
5312 no_scale_factor,
5313 temp,
5314 needs_null_check);
5315}
5316
5317void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5318 Location ref,
5319 GpuRegister obj,
5320 uint32_t data_offset,
5321 Location index,
5322 Location temp,
5323 bool needs_null_check) {
5324 DCHECK(kEmitCompilerReadBarrier);
5325 DCHECK(kUseBakerReadBarrier);
5326
5327 static_assert(
5328 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5329 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005330 ScaleFactor scale_factor = TIMES_4;
5331
5332 if (kBakerReadBarrierThunksEnableForArrays) {
5333 // Note that we do not actually check the value of `GetIsGcMarking()`
5334 // to decide whether to mark the loaded reference or not. Instead, we
5335 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5336 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5337 // vice versa.
5338 //
5339 // We use thunks for the slow path. That thunk checks the reference
5340 // and jumps to the entrypoint if needed. If the holder is not gray,
5341 // it issues a load-load memory barrier and returns to the original
5342 // reference load.
5343 //
5344 // temp = Thread::Current()->pReadBarrierMarkReg00
5345 // // AKA &art_quick_read_barrier_mark_introspection.
5346 // if (temp != nullptr) {
5347 // temp = &field_array_thunk<holder_reg>
5348 // temp()
5349 // }
5350 // not_gray_return_address:
5351 // // The element address is pre-calculated in the TMP register before the
5352 // // thunk invocation and the thunk benefits from it.
5353 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5354 // gray_return_address:
5355
5356 DCHECK(temp.IsInvalid());
5357 DCHECK(index.IsValid());
5358 const int32_t entry_point_offset =
5359 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5360 // We will not do the explicit null check in the thunk as some form of a null check
5361 // must've been done earlier.
5362 DCHECK(!needs_null_check);
5363 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5364 // Loading the entrypoint does not require a load acquire since it is only changed when
5365 // threads are suspended or running a checkpoint.
5366 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005367 Mips64Label skip_call;
5368 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005369 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5370 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5371 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5372 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005373 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005374 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5375 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5376 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5377 __ MaybeUnpoisonHeapReference(ref_reg);
5378 return;
5379 }
5380
Alexey Frunze15958152017-02-09 19:08:30 -08005381 // /* HeapReference<Object> */ ref =
5382 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005383 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5384 ref,
5385 obj,
5386 data_offset,
5387 index,
5388 scale_factor,
5389 temp,
5390 needs_null_check);
5391}
5392
5393void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5394 Location ref,
5395 GpuRegister obj,
5396 uint32_t offset,
5397 Location index,
5398 ScaleFactor scale_factor,
5399 Location temp,
5400 bool needs_null_check,
5401 bool always_update_field) {
5402 DCHECK(kEmitCompilerReadBarrier);
5403 DCHECK(kUseBakerReadBarrier);
5404
5405 // In slow path based read barriers, the read barrier call is
5406 // inserted after the original load. However, in fast path based
5407 // Baker's read barriers, we need to perform the load of
5408 // mirror::Object::monitor_ *before* the original reference load.
5409 // This load-load ordering is required by the read barrier.
5410 // The fast path/slow path (for Baker's algorithm) should look like:
5411 //
5412 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5413 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5414 // HeapReference<Object> ref = *src; // Original reference load.
5415 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5416 // if (is_gray) {
5417 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5418 // }
5419 //
5420 // Note: the original implementation in ReadBarrier::Barrier is
5421 // slightly more complex as it performs additional checks that we do
5422 // not do here for performance reasons.
5423
5424 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5425 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5426 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5427
5428 // /* int32_t */ monitor = obj->monitor_
5429 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5430 if (needs_null_check) {
5431 MaybeRecordImplicitNullCheck(instruction);
5432 }
5433 // /* LockWord */ lock_word = LockWord(monitor)
5434 static_assert(sizeof(LockWord) == sizeof(int32_t),
5435 "art::LockWord and int32_t have different sizes.");
5436
5437 __ Sync(0); // Barrier to prevent load-load reordering.
5438
5439 // The actual reference load.
5440 if (index.IsValid()) {
5441 // Load types involving an "index": ArrayGet,
5442 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5443 // intrinsics.
5444 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5445 if (index.IsConstant()) {
5446 size_t computed_offset =
5447 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5448 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5449 } else {
5450 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005451 if (scale_factor == TIMES_1) {
5452 __ Daddu(TMP, index_reg, obj);
5453 } else {
5454 __ Dlsa(TMP, index_reg, obj, scale_factor);
5455 }
Alexey Frunze15958152017-02-09 19:08:30 -08005456 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5457 }
5458 } else {
5459 // /* HeapReference<Object> */ ref = *(obj + offset)
5460 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5461 }
5462
5463 // Object* ref = ref_addr->AsMirrorPtr()
5464 __ MaybeUnpoisonHeapReference(ref_reg);
5465
5466 // Slow path marking the object `ref` when it is gray.
5467 SlowPathCodeMIPS64* slow_path;
5468 if (always_update_field) {
5469 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5470 // of the form `obj + field_offset`, where `obj` is a register and
5471 // `field_offset` is a register. Thus `offset` and `scale_factor`
5472 // above are expected to be null in this code path.
5473 DCHECK_EQ(offset, 0u);
5474 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005475 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005476 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5477 ref,
5478 obj,
5479 /* field_offset */ index,
5480 temp_reg);
5481 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005482 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005483 }
5484 AddSlowPath(slow_path);
5485
5486 // if (rb_state == ReadBarrier::GrayState())
5487 // ref = ReadBarrier::Mark(ref);
5488 // Given the numeric representation, it's enough to check the low bit of the
5489 // rb_state. We do that by shifting the bit into the sign bit (31) and
5490 // performing a branch on less than zero.
5491 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5492 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5493 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5494 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5495 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5496 __ Bind(slow_path->GetExitLabel());
5497}
5498
5499void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5500 Location out,
5501 Location ref,
5502 Location obj,
5503 uint32_t offset,
5504 Location index) {
5505 DCHECK(kEmitCompilerReadBarrier);
5506
5507 // Insert a slow path based read barrier *after* the reference load.
5508 //
5509 // If heap poisoning is enabled, the unpoisoning of the loaded
5510 // reference will be carried out by the runtime within the slow
5511 // path.
5512 //
5513 // Note that `ref` currently does not get unpoisoned (when heap
5514 // poisoning is enabled), which is alright as the `ref` argument is
5515 // not used by the artReadBarrierSlow entry point.
5516 //
5517 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005518 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005519 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5520 AddSlowPath(slow_path);
5521
5522 __ Bc(slow_path->GetEntryLabel());
5523 __ Bind(slow_path->GetExitLabel());
5524}
5525
5526void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5527 Location out,
5528 Location ref,
5529 Location obj,
5530 uint32_t offset,
5531 Location index) {
5532 if (kEmitCompilerReadBarrier) {
5533 // Baker's read barriers shall be handled by the fast path
5534 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5535 DCHECK(!kUseBakerReadBarrier);
5536 // If heap poisoning is enabled, unpoisoning will be taken care of
5537 // by the runtime within the slow path.
5538 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5539 } else if (kPoisonHeapReferences) {
5540 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5541 }
5542}
5543
5544void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5545 Location out,
5546 Location root) {
5547 DCHECK(kEmitCompilerReadBarrier);
5548
5549 // Insert a slow path based read barrier *after* the GC root load.
5550 //
5551 // Note that GC roots are not affected by heap poisoning, so we do
5552 // not need to do anything special for this here.
5553 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005554 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005555 AddSlowPath(slow_path);
5556
5557 __ Bc(slow_path->GetEntryLabel());
5558 __ Bind(slow_path->GetExitLabel());
5559}
5560
Alexey Frunze4dda3372015-06-01 18:31:49 -07005561void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005562 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5563 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005564 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005565 switch (type_check_kind) {
5566 case TypeCheckKind::kExactCheck:
5567 case TypeCheckKind::kAbstractClassCheck:
5568 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005569 case TypeCheckKind::kArrayObjectCheck: {
5570 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5571 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5572 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005573 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005574 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005575 case TypeCheckKind::kArrayCheck:
5576 case TypeCheckKind::kUnresolvedCheck:
5577 case TypeCheckKind::kInterfaceCheck:
5578 call_kind = LocationSummary::kCallOnSlowPath;
5579 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00005580 case TypeCheckKind::kBitstringCheck:
5581 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005582 }
5583
Vladimir Markoca6fff82017-10-03 14:49:14 +01005584 LocationSummary* locations =
5585 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005586 if (baker_read_barrier_slow_path) {
5587 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5588 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005589 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00005590 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
5591 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
5592 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
5593 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
5594 } else {
5595 locations->SetInAt(1, Location::RequiresRegister());
5596 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005597 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005598 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005599 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005600 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005601}
5602
5603void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005604 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005605 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005606 Location obj_loc = locations->InAt(0);
5607 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Marko175e7862018-03-27 09:03:13 +00005608 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08005609 Location out_loc = locations->Out();
5610 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5611 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5612 DCHECK_LE(num_temps, 1u);
5613 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005614 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5615 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5616 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5617 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005618 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005619 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005620
5621 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005622 // Avoid this check if we know `obj` is not null.
5623 if (instruction->MustDoNullCheck()) {
5624 __ Move(out, ZERO);
5625 __ Beqzc(obj, &done);
5626 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005627
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005628 switch (type_check_kind) {
5629 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005630 ReadBarrierOption read_barrier_option =
5631 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005632 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005633 GenerateReferenceLoadTwoRegisters(instruction,
5634 out_loc,
5635 obj_loc,
5636 class_offset,
5637 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005638 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005639 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00005640 __ Xor(out, out, cls.AsRegister<GpuRegister>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005641 __ Sltiu(out, out, 1);
5642 break;
5643 }
5644
5645 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005646 ReadBarrierOption read_barrier_option =
5647 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005648 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005649 GenerateReferenceLoadTwoRegisters(instruction,
5650 out_loc,
5651 obj_loc,
5652 class_offset,
5653 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005654 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005655 // If the class is abstract, we eagerly fetch the super class of the
5656 // object to avoid doing a comparison we know will fail.
5657 Mips64Label loop;
5658 __ Bind(&loop);
5659 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005660 GenerateReferenceLoadOneRegister(instruction,
5661 out_loc,
5662 super_offset,
5663 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005664 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005665 // If `out` is null, we use it for the result, and jump to `done`.
5666 __ Beqzc(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00005667 __ Bnec(out, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005668 __ LoadConst32(out, 1);
5669 break;
5670 }
5671
5672 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005673 ReadBarrierOption read_barrier_option =
5674 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005675 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005676 GenerateReferenceLoadTwoRegisters(instruction,
5677 out_loc,
5678 obj_loc,
5679 class_offset,
5680 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005681 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005682 // Walk over the class hierarchy to find a match.
5683 Mips64Label loop, success;
5684 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00005685 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005686 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005687 GenerateReferenceLoadOneRegister(instruction,
5688 out_loc,
5689 super_offset,
5690 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005691 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005692 __ Bnezc(out, &loop);
5693 // If `out` is null, we use it for the result, and jump to `done`.
5694 __ Bc(&done);
5695 __ Bind(&success);
5696 __ LoadConst32(out, 1);
5697 break;
5698 }
5699
5700 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005701 ReadBarrierOption read_barrier_option =
5702 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005703 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005704 GenerateReferenceLoadTwoRegisters(instruction,
5705 out_loc,
5706 obj_loc,
5707 class_offset,
5708 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005709 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005710 // Do an exact check.
5711 Mips64Label success;
Vladimir Marko175e7862018-03-27 09:03:13 +00005712 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005713 // Otherwise, we need to check that the object's class is a non-primitive array.
5714 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005715 GenerateReferenceLoadOneRegister(instruction,
5716 out_loc,
5717 component_offset,
5718 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005719 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005720 // If `out` is null, we use it for the result, and jump to `done`.
5721 __ Beqzc(out, &done);
5722 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5723 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5724 __ Sltiu(out, out, 1);
5725 __ Bc(&done);
5726 __ Bind(&success);
5727 __ LoadConst32(out, 1);
5728 break;
5729 }
5730
5731 case TypeCheckKind::kArrayCheck: {
5732 // No read barrier since the slow path will retry upon failure.
5733 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005734 GenerateReferenceLoadTwoRegisters(instruction,
5735 out_loc,
5736 obj_loc,
5737 class_offset,
5738 maybe_temp_loc,
5739 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005740 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005741 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5742 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005743 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00005744 __ Bnec(out, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005745 __ LoadConst32(out, 1);
5746 break;
5747 }
5748
5749 case TypeCheckKind::kUnresolvedCheck:
5750 case TypeCheckKind::kInterfaceCheck: {
5751 // Note that we indeed only call on slow path, but we always go
5752 // into the slow path for the unresolved and interface check
5753 // cases.
5754 //
5755 // We cannot directly call the InstanceofNonTrivial runtime
5756 // entry point without resorting to a type checking slow path
5757 // here (i.e. by calling InvokeRuntime directly), as it would
5758 // require to assign fixed registers for the inputs of this
5759 // HInstanceOf instruction (following the runtime calling
5760 // convention), which might be cluttered by the potential first
5761 // read barrier emission at the beginning of this method.
5762 //
5763 // TODO: Introduce a new runtime entry point taking the object
5764 // to test (instead of its class) as argument, and let it deal
5765 // with the read barrier issues. This will let us refactor this
5766 // case of the `switch` code as it was previously (with a direct
5767 // call to the runtime not using a type checking slow path).
5768 // This should also be beneficial for the other cases above.
5769 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005770 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5771 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005772 codegen_->AddSlowPath(slow_path);
5773 __ Bc(slow_path->GetEntryLabel());
5774 break;
5775 }
Vladimir Marko175e7862018-03-27 09:03:13 +00005776
5777 case TypeCheckKind::kBitstringCheck: {
5778 // /* HeapReference<Class> */ temp = obj->klass_
5779 GenerateReferenceLoadTwoRegisters(instruction,
5780 out_loc,
5781 obj_loc,
5782 class_offset,
5783 maybe_temp_loc,
5784 kWithoutReadBarrier);
5785
5786 GenerateBitstringTypeCheckCompare(instruction, out);
5787 __ Sltiu(out, out, 1);
5788 break;
5789 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005790 }
5791
5792 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005793
5794 if (slow_path != nullptr) {
5795 __ Bind(slow_path->GetExitLabel());
5796 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005797}
5798
5799void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005800 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005801 locations->SetOut(Location::ConstantLocation(constant));
5802}
5803
5804void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5805 // Will be generated at use site.
5806}
5807
5808void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005809 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005810 locations->SetOut(Location::ConstantLocation(constant));
5811}
5812
5813void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5814 // Will be generated at use site.
5815}
5816
Calin Juravle175dc732015-08-25 15:42:32 +01005817void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5818 // The trampoline uses the same calling convention as dex calling conventions,
5819 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5820 // the method_idx.
5821 HandleInvoke(invoke);
5822}
5823
5824void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5825 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5826}
5827
Alexey Frunze4dda3372015-06-01 18:31:49 -07005828void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5829 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5830 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5831}
5832
5833void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5834 HandleInvoke(invoke);
5835 // The register T0 is required to be used for the hidden argument in
5836 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5837 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5838}
5839
5840void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5841 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5842 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005843 Location receiver = invoke->GetLocations()->InAt(0);
5844 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005845 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005846
5847 // Set the hidden argument.
5848 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5849 invoke->GetDexMethodIndex());
5850
5851 // temp = object->GetClass();
5852 if (receiver.IsStackSlot()) {
5853 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5854 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5855 } else {
5856 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5857 }
5858 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005859 // Instead of simply (possibly) unpoisoning `temp` here, we should
5860 // emit a read barrier for the previous class reference load.
5861 // However this is not required in practice, as this is an
5862 // intermediate/temporary reference and because the current
5863 // concurrent copying collector keeps the from-space memory
5864 // intact/accessible until the end of the marking phase (the
5865 // concurrent copying collector may not in the future).
5866 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005867 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5868 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5869 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005870 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005871 // temp = temp->GetImtEntryAt(method_offset);
5872 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5873 // T9 = temp->GetEntryPoint();
5874 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5875 // T9();
5876 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005877 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005878 DCHECK(!codegen_->IsLeafMethod());
5879 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5880}
5881
5882void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005883 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5884 if (intrinsic.TryDispatch(invoke)) {
5885 return;
5886 }
5887
Alexey Frunze4dda3372015-06-01 18:31:49 -07005888 HandleInvoke(invoke);
5889}
5890
5891void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005892 // Explicit clinit checks triggered by static invokes must have been pruned by
5893 // art::PrepareForRegisterAllocation.
5894 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005895
Chris Larsen3039e382015-08-26 07:54:08 -07005896 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5897 if (intrinsic.TryDispatch(invoke)) {
5898 return;
5899 }
5900
Alexey Frunze4dda3372015-06-01 18:31:49 -07005901 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005902}
5903
Orion Hodsonac141392017-01-13 11:53:47 +00005904void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5905 HandleInvoke(invoke);
5906}
5907
5908void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5909 codegen_->GenerateInvokePolymorphicCall(invoke);
5910}
5911
Chris Larsen3039e382015-08-26 07:54:08 -07005912static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005913 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005914 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5915 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005916 return true;
5917 }
5918 return false;
5919}
5920
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005922 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005923 bool fallback_load = false;
5924 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005925 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005926 case HLoadString::LoadKind::kBootImageRelRo:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005927 case HLoadString::LoadKind::kBssEntry:
5928 DCHECK(!Runtime::Current()->UseJitCompilation());
5929 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005930 case HLoadString::LoadKind::kJitTableAddress:
5931 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005932 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005933 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005934 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005935 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005936 }
5937 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005938 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005939 }
5940 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005941}
5942
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005943HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5944 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005945 bool fallback_load = false;
5946 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005947 case HLoadClass::LoadKind::kInvalid:
5948 LOG(FATAL) << "UNREACHABLE";
5949 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005950 case HLoadClass::LoadKind::kReferrersClass:
5951 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005952 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005953 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005954 case HLoadClass::LoadKind::kBssEntry:
5955 DCHECK(!Runtime::Current()->UseJitCompilation());
5956 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005957 case HLoadClass::LoadKind::kJitTableAddress:
5958 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005959 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005960 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005961 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005962 break;
5963 }
5964 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005965 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005966 }
5967 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005968}
5969
Vladimir Markodc151b22015-10-15 18:02:30 +01005970HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5971 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005972 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005973 // On MIPS64 we support all dispatch types.
5974 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005975}
5976
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005977void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5978 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005979 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005980 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005981 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5982 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5983
Alexey Frunze19f6c692016-11-30 19:19:55 -08005984 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005985 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005986 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005987 uint32_t offset =
5988 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005989 __ LoadFromOffset(kLoadDoubleword,
5990 temp.AsRegister<GpuRegister>(),
5991 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005992 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005993 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005994 }
Vladimir Marko58155012015-08-19 12:49:41 +00005995 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005996 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005997 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005998 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5999 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006000 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006001 NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006002 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006003 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006004 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01006005 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6006 break;
6007 }
Vladimir Marko58155012015-08-19 12:49:41 +00006008 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006009 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
6010 kLoadDoubleword,
6011 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00006012 break;
Vladimir Markob066d432018-01-03 13:14:37 +00006013 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006014 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00006015 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
6016 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
6017 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6018 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
6019 __ Lwu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6020 break;
6021 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006022 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006023 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006024 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006025 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
6026 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
6027 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08006028 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6029 break;
6030 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006031 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
6032 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
6033 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07006034 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006035 }
6036
Alexey Frunze19f6c692016-11-30 19:19:55 -08006037 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00006038 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006039 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00006040 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006041 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6042 // T9 = callee_method->entry_point_from_quick_compiled_code_;
6043 __ LoadFromOffset(kLoadDoubleword,
6044 T9,
6045 callee_method.AsRegister<GpuRegister>(),
6046 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006047 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006048 // T9()
6049 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006050 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00006051 break;
6052 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006053 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
6054
Alexey Frunze4dda3372015-06-01 18:31:49 -07006055 DCHECK(!IsLeafMethod());
6056}
6057
6058void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006059 // Explicit clinit checks triggered by static invokes must have been pruned by
6060 // art::PrepareForRegisterAllocation.
6061 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006062
6063 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6064 return;
6065 }
6066
6067 LocationSummary* locations = invoke->GetLocations();
6068 codegen_->GenerateStaticOrDirectCall(invoke,
6069 locations->HasTemps()
6070 ? locations->GetTemp(0)
6071 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006072}
6073
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006074void CodeGeneratorMIPS64::GenerateVirtualCall(
6075 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006076 // Use the calling convention instead of the location of the receiver, as
6077 // intrinsics may have put the receiver in a different register. In the intrinsics
6078 // slow path, the arguments have been moved to the right place, so here we are
6079 // guaranteed that the receiver is the first register of the calling convention.
6080 InvokeDexCallingConvention calling_convention;
6081 GpuRegister receiver = calling_convention.GetRegisterAt(0);
6082
Alexey Frunze53afca12015-11-05 16:34:23 -08006083 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006084 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6085 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
6086 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006087 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006088
6089 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006090 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08006091 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006092 // Instead of simply (possibly) unpoisoning `temp` here, we should
6093 // emit a read barrier for the previous class reference load.
6094 // However this is not required in practice, as this is an
6095 // intermediate/temporary reference and because the current
6096 // concurrent copying collector keeps the from-space memory
6097 // intact/accessible until the end of the marking phase (the
6098 // concurrent copying collector may not in the future).
6099 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006100 // temp = temp->GetMethodAt(method_offset);
6101 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6102 // T9 = temp->GetEntryPoint();
6103 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6104 // T9();
6105 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006106 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006107 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006108}
6109
6110void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6111 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6112 return;
6113 }
6114
6115 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006116 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006117}
6118
6119void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006120 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006121 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006122 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006123 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6124 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006125 return;
6126 }
Vladimir Marko41559982017-01-06 14:04:23 +00006127 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006128
Alexey Frunze15958152017-02-09 19:08:30 -08006129 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6130 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006131 ? LocationSummary::kCallOnSlowPath
6132 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006133 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006134 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6135 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6136 }
Vladimir Marko41559982017-01-06 14:04:23 +00006137 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006138 locations->SetInAt(0, Location::RequiresRegister());
6139 }
6140 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006141 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6142 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6143 // Rely on the type resolution or initialization and marking to save everything we need.
6144 RegisterSet caller_saves = RegisterSet::Empty();
6145 InvokeRuntimeCallingConvention calling_convention;
6146 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6147 locations->SetCustomSlowPathCallerSaves(caller_saves);
6148 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006149 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006150 }
6151 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006152}
6153
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006154// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6155// move.
6156void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006157 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006158 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006159 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006160 return;
6161 }
Vladimir Marko41559982017-01-06 14:04:23 +00006162 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006163
Vladimir Marko41559982017-01-06 14:04:23 +00006164 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006165 Location out_loc = locations->Out();
6166 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6167 GpuRegister current_method_reg = ZERO;
6168 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006169 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006170 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6171 }
6172
Alexey Frunze15958152017-02-09 19:08:30 -08006173 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6174 ? kWithoutReadBarrier
6175 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006176 bool generate_null_check = false;
6177 switch (load_kind) {
6178 case HLoadClass::LoadKind::kReferrersClass:
6179 DCHECK(!cls->CanCallRuntime());
6180 DCHECK(!cls->MustGenerateClinitCheck());
6181 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6182 GenerateGcRootFieldLoad(cls,
6183 out_loc,
6184 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006185 ArtMethod::DeclaringClassOffset().Int32Value(),
6186 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006187 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006188 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006189 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006190 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006191 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006192 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006193 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006194 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006195 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006196 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6197 break;
6198 }
6199 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006200 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006201 uint32_t address = dchecked_integral_cast<uint32_t>(
6202 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6203 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006204 __ LoadLiteral(out,
6205 kLoadUnsignedWord,
6206 codegen_->DeduplicateBootImageAddressLiteral(address));
6207 break;
6208 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006209 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006210 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006211 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006212 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006213 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006214 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006215 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006216 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6217 __ Lwu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006218 break;
6219 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006220 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006221 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6222 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006223 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6224 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006225 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006226 GenerateGcRootFieldLoad(cls,
6227 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006228 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006229 /* placeholder */ 0x5678,
6230 read_barrier_option,
6231 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006232 generate_null_check = true;
6233 break;
6234 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006235 case HLoadClass::LoadKind::kJitTableAddress:
6236 __ LoadLiteral(out,
6237 kLoadUnsignedWord,
6238 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6239 cls->GetTypeIndex(),
6240 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006241 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006242 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006243 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006244 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006245 LOG(FATAL) << "UNREACHABLE";
6246 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006247 }
6248
6249 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6250 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006251 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006252 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006253 codegen_->AddSlowPath(slow_path);
6254 if (generate_null_check) {
6255 __ Beqzc(out, slow_path->GetEntryLabel());
6256 }
6257 if (cls->MustGenerateClinitCheck()) {
6258 GenerateClassInitializationCheck(slow_path, out);
6259 } else {
6260 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006261 }
6262 }
6263}
6264
David Brazdilcb1c0552015-08-04 16:22:25 +01006265static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006266 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006267}
6268
Alexey Frunze4dda3372015-06-01 18:31:49 -07006269void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6270 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006271 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006272 locations->SetOut(Location::RequiresRegister());
6273}
6274
6275void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6276 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006277 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6278}
6279
6280void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006281 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006282}
6283
6284void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6285 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006286}
6287
Alexey Frunze4dda3372015-06-01 18:31:49 -07006288void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006289 HLoadString::LoadKind load_kind = load->GetLoadKind();
6290 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006291 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006292 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006293 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006294 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006295 } else {
6296 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006297 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6298 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6299 // Rely on the pResolveString and marking to save everything we need.
6300 RegisterSet caller_saves = RegisterSet::Empty();
6301 InvokeRuntimeCallingConvention calling_convention;
6302 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6303 locations->SetCustomSlowPathCallerSaves(caller_saves);
6304 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006305 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006306 }
6307 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006308 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006309}
6310
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006311// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6312// move.
6313void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006314 HLoadString::LoadKind load_kind = load->GetLoadKind();
6315 LocationSummary* locations = load->GetLocations();
6316 Location out_loc = locations->Out();
6317 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6318
6319 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006320 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6321 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006322 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006323 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006324 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006325 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006326 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006327 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006328 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006329 }
6330 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006331 uint32_t address = dchecked_integral_cast<uint32_t>(
6332 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6333 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006334 __ LoadLiteral(out,
6335 kLoadUnsignedWord,
6336 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006337 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006338 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006339 case HLoadString::LoadKind::kBootImageRelRo: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006340 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006341 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006342 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006343 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006344 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006345 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006346 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6347 __ Lwu(out, AT, /* placeholder */ 0x5678);
6348 return;
6349 }
6350 case HLoadString::LoadKind::kBssEntry: {
6351 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6352 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6353 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6354 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6355 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006356 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006357 GenerateGcRootFieldLoad(load,
6358 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006359 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006360 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006361 kCompilerReadBarrierOption,
6362 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006363 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006364 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006365 codegen_->AddSlowPath(slow_path);
6366 __ Beqzc(out, slow_path->GetEntryLabel());
6367 __ Bind(slow_path->GetExitLabel());
6368 return;
6369 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006370 case HLoadString::LoadKind::kJitTableAddress:
6371 __ LoadLiteral(out,
6372 kLoadUnsignedWord,
6373 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6374 load->GetStringIndex(),
6375 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006376 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006377 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006378 default:
6379 break;
6380 }
6381
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006382 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006383 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006384 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006385 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006386 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6387 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6388 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006389}
6390
Alexey Frunze4dda3372015-06-01 18:31:49 -07006391void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006392 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006393 locations->SetOut(Location::ConstantLocation(constant));
6394}
6395
6396void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6397 // Will be generated at use site.
6398}
6399
6400void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006401 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6402 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006403 InvokeRuntimeCallingConvention calling_convention;
6404 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6405}
6406
6407void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006408 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006409 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006410 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006411 if (instruction->IsEnter()) {
6412 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6413 } else {
6414 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6415 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006416}
6417
6418void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6419 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006420 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006421 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006422 case DataType::Type::kInt32:
6423 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006424 locations->SetInAt(0, Location::RequiresRegister());
6425 locations->SetInAt(1, Location::RequiresRegister());
6426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6427 break;
6428
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006429 case DataType::Type::kFloat32:
6430 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006431 locations->SetInAt(0, Location::RequiresFpuRegister());
6432 locations->SetInAt(1, Location::RequiresFpuRegister());
6433 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6434 break;
6435
6436 default:
6437 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6438 }
6439}
6440
6441void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006443 LocationSummary* locations = instruction->GetLocations();
6444
6445 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006446 case DataType::Type::kInt32:
6447 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006448 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6449 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6450 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006451 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006452 __ MulR6(dst, lhs, rhs);
6453 else
6454 __ Dmul(dst, lhs, rhs);
6455 break;
6456 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006457 case DataType::Type::kFloat32:
6458 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006459 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6460 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6461 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006462 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006463 __ MulS(dst, lhs, rhs);
6464 else
6465 __ MulD(dst, lhs, rhs);
6466 break;
6467 }
6468 default:
6469 LOG(FATAL) << "Unexpected mul type " << type;
6470 }
6471}
6472
6473void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6474 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006475 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006476 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006477 case DataType::Type::kInt32:
6478 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006479 locations->SetInAt(0, Location::RequiresRegister());
6480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6481 break;
6482
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006483 case DataType::Type::kFloat32:
6484 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006485 locations->SetInAt(0, Location::RequiresFpuRegister());
6486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6487 break;
6488
6489 default:
6490 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6491 }
6492}
6493
6494void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006495 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006496 LocationSummary* locations = instruction->GetLocations();
6497
6498 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 case DataType::Type::kInt32:
6500 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006501 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6502 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006503 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006504 __ Subu(dst, ZERO, src);
6505 else
6506 __ Dsubu(dst, ZERO, src);
6507 break;
6508 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 case DataType::Type::kFloat32:
6510 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006511 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6512 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006513 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006514 __ NegS(dst, src);
6515 else
6516 __ NegD(dst, src);
6517 break;
6518 }
6519 default:
6520 LOG(FATAL) << "Unexpected neg type " << type;
6521 }
6522}
6523
6524void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006525 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6526 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006527 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006528 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006529 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6530 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006531}
6532
6533void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006534 // Note: if heap poisoning is enabled, the entry point takes care
6535 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006536 QuickEntrypointEnum entrypoint =
6537 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6538 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006539 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006540 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006541}
6542
6543void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006544 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6545 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006546 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006547 if (instruction->IsStringAlloc()) {
6548 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6549 } else {
6550 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006551 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006552 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006553}
6554
6555void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006556 // Note: if heap poisoning is enabled, the entry point takes care
6557 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006558 if (instruction->IsStringAlloc()) {
6559 // String is allocated through StringFactory. Call NewEmptyString entry point.
6560 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006561 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006562 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006563 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6564 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6565 __ Jalr(T9);
6566 __ Nop();
6567 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6568 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006569 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006570 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006571 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006572}
6573
6574void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006575 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006576 locations->SetInAt(0, Location::RequiresRegister());
6577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6578}
6579
6580void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006581 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006582 LocationSummary* locations = instruction->GetLocations();
6583
6584 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006585 case DataType::Type::kInt32:
6586 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006587 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6588 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6589 __ Nor(dst, src, ZERO);
6590 break;
6591 }
6592
6593 default:
6594 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6595 }
6596}
6597
6598void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006599 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006600 locations->SetInAt(0, Location::RequiresRegister());
6601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6602}
6603
6604void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6605 LocationSummary* locations = instruction->GetLocations();
6606 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6607 locations->InAt(0).AsRegister<GpuRegister>(),
6608 1);
6609}
6610
6611void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006612 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6613 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006614}
6615
Calin Juravle2ae48182016-03-16 14:05:09 +00006616void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6617 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006618 return;
6619 }
6620 Location obj = instruction->GetLocations()->InAt(0);
6621
6622 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006623 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006624}
6625
Calin Juravle2ae48182016-03-16 14:05:09 +00006626void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006627 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006628 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006629 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006630
6631 Location obj = instruction->GetLocations()->InAt(0);
6632
6633 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6634}
6635
6636void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006637 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006638}
6639
6640void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6641 HandleBinaryOp(instruction);
6642}
6643
6644void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6645 HandleBinaryOp(instruction);
6646}
6647
6648void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6649 LOG(FATAL) << "Unreachable";
6650}
6651
6652void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006653 if (instruction->GetNext()->IsSuspendCheck() &&
6654 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6655 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6656 // The back edge will generate the suspend check.
6657 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6658 }
6659
Alexey Frunze4dda3372015-06-01 18:31:49 -07006660 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6661}
6662
6663void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006664 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006665 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6666 if (location.IsStackSlot()) {
6667 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6668 } else if (location.IsDoubleStackSlot()) {
6669 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6670 }
6671 locations->SetOut(location);
6672}
6673
6674void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6675 ATTRIBUTE_UNUSED) {
6676 // Nothing to do, the parameter is already at its location.
6677}
6678
6679void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6680 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006681 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006682 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6683}
6684
6685void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6686 ATTRIBUTE_UNUSED) {
6687 // Nothing to do, the method is already at its location.
6688}
6689
6690void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006692 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006693 locations->SetInAt(i, Location::Any());
6694 }
6695 locations->SetOut(Location::Any());
6696}
6697
6698void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6699 LOG(FATAL) << "Unreachable";
6700}
6701
6702void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006703 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006704 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006705 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6706 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006708
6709 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006710 case DataType::Type::kInt32:
6711 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006712 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006713 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6715 break;
6716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006717 case DataType::Type::kFloat32:
6718 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006719 InvokeRuntimeCallingConvention calling_convention;
6720 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6721 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6722 locations->SetOut(calling_convention.GetReturnLocation(type));
6723 break;
6724 }
6725
6726 default:
6727 LOG(FATAL) << "Unexpected rem type " << type;
6728 }
6729}
6730
6731void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006732 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006733
6734 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006735 case DataType::Type::kInt32:
6736 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006737 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006738 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006740 case DataType::Type::kFloat32:
6741 case DataType::Type::kFloat64: {
6742 QuickEntrypointEnum entrypoint =
6743 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006744 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006745 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006746 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6747 } else {
6748 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6749 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006750 break;
6751 }
6752 default:
6753 LOG(FATAL) << "Unexpected rem type " << type;
6754 }
6755}
6756
Aart Bik1f8d51b2018-02-15 10:42:37 -08006757static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
6758 LocationSummary* locations = new (allocator) LocationSummary(minmax);
6759 switch (minmax->GetResultType()) {
6760 case DataType::Type::kInt32:
6761 case DataType::Type::kInt64:
6762 locations->SetInAt(0, Location::RequiresRegister());
6763 locations->SetInAt(1, Location::RequiresRegister());
6764 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6765 break;
6766 case DataType::Type::kFloat32:
6767 case DataType::Type::kFloat64:
6768 locations->SetInAt(0, Location::RequiresFpuRegister());
6769 locations->SetInAt(1, Location::RequiresFpuRegister());
6770 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6771 break;
6772 default:
6773 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
6774 }
6775}
6776
Aart Bik351df3e2018-03-07 11:54:57 -08006777void InstructionCodeGeneratorMIPS64::GenerateMinMaxInt(LocationSummary* locations, bool is_min) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08006778 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6779 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
6780 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6781
6782 if (lhs == rhs) {
6783 if (out != lhs) {
6784 __ Move(out, lhs);
6785 }
6786 } else {
6787 // Some architectures, such as ARM and MIPS (prior to r6), have a
6788 // conditional move instruction which only changes the target
6789 // (output) register if the condition is true (MIPS prior to r6 had
6790 // MOVF, MOVT, and MOVZ). The SELEQZ and SELNEZ instructions always
6791 // change the target (output) register. If the condition is true the
6792 // output register gets the contents of the "rs" register; otherwise,
6793 // the output register is set to zero. One consequence of this is
6794 // that to implement something like "rd = c==0 ? rs : rt" MIPS64r6
6795 // needs to use a pair of SELEQZ/SELNEZ instructions. After
6796 // executing this pair of instructions one of the output registers
6797 // from the pair will necessarily contain zero. Then the code ORs the
6798 // output registers from the SELEQZ/SELNEZ instructions to get the
6799 // final result.
6800 //
6801 // The initial test to see if the output register is same as the
6802 // first input register is needed to make sure that value in the
6803 // first input register isn't clobbered before we've finished
6804 // computing the output value. The logic in the corresponding else
6805 // clause performs the same task but makes sure the second input
6806 // register isn't clobbered in the event that it's the same register
6807 // as the output register; the else clause also handles the case
6808 // where the output register is distinct from both the first, and the
6809 // second input registers.
6810 if (out == lhs) {
6811 __ Slt(AT, rhs, lhs);
6812 if (is_min) {
6813 __ Seleqz(out, lhs, AT);
6814 __ Selnez(AT, rhs, AT);
6815 } else {
6816 __ Selnez(out, lhs, AT);
6817 __ Seleqz(AT, rhs, AT);
6818 }
6819 } else {
6820 __ Slt(AT, lhs, rhs);
6821 if (is_min) {
6822 __ Seleqz(out, rhs, AT);
6823 __ Selnez(AT, lhs, AT);
6824 } else {
6825 __ Selnez(out, rhs, AT);
6826 __ Seleqz(AT, lhs, AT);
6827 }
6828 }
6829 __ Or(out, out, AT);
6830 }
6831}
6832
6833void InstructionCodeGeneratorMIPS64::GenerateMinMaxFP(LocationSummary* locations,
6834 bool is_min,
6835 DataType::Type type) {
6836 FpuRegister a = locations->InAt(0).AsFpuRegister<FpuRegister>();
6837 FpuRegister b = locations->InAt(1).AsFpuRegister<FpuRegister>();
6838 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6839
6840 Mips64Label noNaNs;
6841 Mips64Label done;
6842 FpuRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
6843
6844 // When Java computes min/max it prefers a NaN to a number; the
6845 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
6846 // the inputs is a NaN and the other is a valid number, the MIPS
6847 // instruction will return the number; Java wants the NaN value
6848 // returned. This is why there is extra logic preceding the use of
6849 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
6850 // NaN, return the NaN, otherwise return the min/max.
6851 if (type == DataType::Type::kFloat64) {
6852 __ CmpUnD(FTMP, a, b);
6853 __ Bc1eqz(FTMP, &noNaNs);
6854
6855 // One of the inputs is a NaN
6856 __ CmpEqD(ftmp, a, a);
6857 // If a == a then b is the NaN, otherwise a is the NaN.
6858 __ SelD(ftmp, a, b);
6859
6860 if (ftmp != out) {
6861 __ MovD(out, ftmp);
6862 }
6863
6864 __ Bc(&done);
6865
6866 __ Bind(&noNaNs);
6867
6868 if (is_min) {
6869 __ MinD(out, a, b);
6870 } else {
6871 __ MaxD(out, a, b);
6872 }
6873 } else {
6874 DCHECK_EQ(type, DataType::Type::kFloat32);
6875 __ CmpUnS(FTMP, a, b);
6876 __ Bc1eqz(FTMP, &noNaNs);
6877
6878 // One of the inputs is a NaN
6879 __ CmpEqS(ftmp, a, a);
6880 // If a == a then b is the NaN, otherwise a is the NaN.
6881 __ SelS(ftmp, a, b);
6882
6883 if (ftmp != out) {
6884 __ MovS(out, ftmp);
6885 }
6886
6887 __ Bc(&done);
6888
6889 __ Bind(&noNaNs);
6890
6891 if (is_min) {
6892 __ MinS(out, a, b);
6893 } else {
6894 __ MaxS(out, a, b);
6895 }
6896 }
6897
6898 __ Bind(&done);
6899}
6900
Aart Bik351df3e2018-03-07 11:54:57 -08006901void InstructionCodeGeneratorMIPS64::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
6902 DataType::Type type = minmax->GetResultType();
6903 switch (type) {
6904 case DataType::Type::kInt32:
6905 case DataType::Type::kInt64:
6906 GenerateMinMaxInt(minmax->GetLocations(), is_min);
6907 break;
6908 case DataType::Type::kFloat32:
6909 case DataType::Type::kFloat64:
6910 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
6911 break;
6912 default:
6913 LOG(FATAL) << "Unexpected type for HMinMax " << type;
6914 }
6915}
6916
Aart Bik1f8d51b2018-02-15 10:42:37 -08006917void LocationsBuilderMIPS64::VisitMin(HMin* min) {
6918 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
6919}
6920
6921void InstructionCodeGeneratorMIPS64::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08006922 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006923}
6924
6925void LocationsBuilderMIPS64::VisitMax(HMax* max) {
6926 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
6927}
6928
6929void InstructionCodeGeneratorMIPS64::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08006930 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08006931}
6932
Aart Bik3dad3412018-02-28 12:01:46 -08006933void LocationsBuilderMIPS64::VisitAbs(HAbs* abs) {
6934 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6935 switch (abs->GetResultType()) {
6936 case DataType::Type::kInt32:
6937 case DataType::Type::kInt64:
6938 locations->SetInAt(0, Location::RequiresRegister());
6939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6940 break;
6941 case DataType::Type::kFloat32:
6942 case DataType::Type::kFloat64:
6943 locations->SetInAt(0, Location::RequiresFpuRegister());
6944 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6945 break;
6946 default:
6947 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
6948 }
6949}
6950
6951void InstructionCodeGeneratorMIPS64::VisitAbs(HAbs* abs) {
6952 LocationSummary* locations = abs->GetLocations();
6953 switch (abs->GetResultType()) {
6954 case DataType::Type::kInt32: {
6955 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
6956 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6957 __ Sra(AT, in, 31);
6958 __ Xor(out, in, AT);
6959 __ Subu(out, out, AT);
6960 break;
6961 }
6962 case DataType::Type::kInt64: {
6963 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
6964 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6965 __ Dsra32(AT, in, 31);
6966 __ Xor(out, in, AT);
6967 __ Dsubu(out, out, AT);
6968 break;
6969 }
6970 case DataType::Type::kFloat32: {
6971 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
6972 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6973 __ AbsS(out, in);
6974 break;
6975 }
6976 case DataType::Type::kFloat64: {
6977 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
6978 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6979 __ AbsD(out, in);
6980 break;
6981 }
6982 default:
6983 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
6984 }
6985}
6986
Igor Murashkind01745e2017-04-05 16:40:31 -07006987void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6988 constructor_fence->SetLocations(nullptr);
6989}
6990
6991void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6992 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6993 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6994}
6995
Alexey Frunze4dda3372015-06-01 18:31:49 -07006996void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6997 memory_barrier->SetLocations(nullptr);
6998}
6999
7000void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
7001 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
7002}
7003
7004void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007005 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007006 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007007 locations->SetInAt(0, Mips64ReturnLocation(return_type));
7008}
7009
7010void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
7011 codegen_->GenerateFrameExit();
7012}
7013
7014void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
7015 ret->SetLocations(nullptr);
7016}
7017
7018void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
7019 codegen_->GenerateFrameExit();
7020}
7021
Alexey Frunze92d90602015-12-18 18:16:36 -08007022void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
7023 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007024}
7025
Alexey Frunze92d90602015-12-18 18:16:36 -08007026void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
7027 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00007028}
7029
Alexey Frunze4dda3372015-06-01 18:31:49 -07007030void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
7031 HandleShift(shl);
7032}
7033
7034void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
7035 HandleShift(shl);
7036}
7037
7038void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
7039 HandleShift(shr);
7040}
7041
7042void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
7043 HandleShift(shr);
7044}
7045
Alexey Frunze4dda3372015-06-01 18:31:49 -07007046void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
7047 HandleBinaryOp(instruction);
7048}
7049
7050void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
7051 HandleBinaryOp(instruction);
7052}
7053
7054void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7055 HandleFieldGet(instruction, instruction->GetFieldInfo());
7056}
7057
7058void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
7059 HandleFieldGet(instruction, instruction->GetFieldInfo());
7060}
7061
7062void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
7063 HandleFieldSet(instruction, instruction->GetFieldInfo());
7064}
7065
7066void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01007067 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007068}
7069
Calin Juravlee460d1d2015-09-29 04:52:17 +01007070void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
7071 HUnresolvedInstanceFieldGet* instruction) {
7072 FieldAccessCallingConventionMIPS64 calling_convention;
7073 codegen_->CreateUnresolvedFieldLocationSummary(
7074 instruction, instruction->GetFieldType(), calling_convention);
7075}
7076
7077void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
7078 HUnresolvedInstanceFieldGet* instruction) {
7079 FieldAccessCallingConventionMIPS64 calling_convention;
7080 codegen_->GenerateUnresolvedFieldAccess(instruction,
7081 instruction->GetFieldType(),
7082 instruction->GetFieldIndex(),
7083 instruction->GetDexPc(),
7084 calling_convention);
7085}
7086
7087void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
7088 HUnresolvedInstanceFieldSet* instruction) {
7089 FieldAccessCallingConventionMIPS64 calling_convention;
7090 codegen_->CreateUnresolvedFieldLocationSummary(
7091 instruction, instruction->GetFieldType(), calling_convention);
7092}
7093
7094void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
7095 HUnresolvedInstanceFieldSet* instruction) {
7096 FieldAccessCallingConventionMIPS64 calling_convention;
7097 codegen_->GenerateUnresolvedFieldAccess(instruction,
7098 instruction->GetFieldType(),
7099 instruction->GetFieldIndex(),
7100 instruction->GetDexPc(),
7101 calling_convention);
7102}
7103
7104void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
7105 HUnresolvedStaticFieldGet* instruction) {
7106 FieldAccessCallingConventionMIPS64 calling_convention;
7107 codegen_->CreateUnresolvedFieldLocationSummary(
7108 instruction, instruction->GetFieldType(), calling_convention);
7109}
7110
7111void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
7112 HUnresolvedStaticFieldGet* instruction) {
7113 FieldAccessCallingConventionMIPS64 calling_convention;
7114 codegen_->GenerateUnresolvedFieldAccess(instruction,
7115 instruction->GetFieldType(),
7116 instruction->GetFieldIndex(),
7117 instruction->GetDexPc(),
7118 calling_convention);
7119}
7120
7121void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
7122 HUnresolvedStaticFieldSet* instruction) {
7123 FieldAccessCallingConventionMIPS64 calling_convention;
7124 codegen_->CreateUnresolvedFieldLocationSummary(
7125 instruction, instruction->GetFieldType(), calling_convention);
7126}
7127
7128void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
7129 HUnresolvedStaticFieldSet* instruction) {
7130 FieldAccessCallingConventionMIPS64 calling_convention;
7131 codegen_->GenerateUnresolvedFieldAccess(instruction,
7132 instruction->GetFieldType(),
7133 instruction->GetFieldIndex(),
7134 instruction->GetDexPc(),
7135 calling_convention);
7136}
7137
Alexey Frunze4dda3372015-06-01 18:31:49 -07007138void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007139 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7140 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02007141 // In suspend check slow path, usually there are no caller-save registers at all.
7142 // If SIMD instructions are present, however, we force spilling all live SIMD
7143 // registers in full width (since the runtime only saves/restores lower part).
7144 locations->SetCustomSlowPathCallerSaves(
7145 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007146}
7147
7148void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
7149 HBasicBlock* block = instruction->GetBlock();
7150 if (block->GetLoopInformation() != nullptr) {
7151 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
7152 // The back edge will generate the suspend check.
7153 return;
7154 }
7155 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
7156 // The goto will generate the suspend check.
7157 return;
7158 }
7159 GenerateSuspendCheck(instruction, nullptr);
7160}
7161
Alexey Frunze4dda3372015-06-01 18:31:49 -07007162void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007163 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7164 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007165 InvokeRuntimeCallingConvention calling_convention;
7166 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7167}
7168
7169void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01007170 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007171 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
7172}
7173
7174void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007175 DataType::Type input_type = conversion->GetInputType();
7176 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007177 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7178 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007179
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007180 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
7181 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007182 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
7183 }
7184
Vladimir Markoca6fff82017-10-03 14:49:14 +01007185 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007187 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007188 locations->SetInAt(0, Location::RequiresFpuRegister());
7189 } else {
7190 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07007191 }
7192
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007193 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007194 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007195 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007197 }
7198}
7199
7200void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
7201 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007202 DataType::Type result_type = conversion->GetResultType();
7203 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07007204
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007205 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
7206 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07007207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007208 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007209 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7210 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
7211
7212 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007213 case DataType::Type::kUint8:
7214 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007215 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007216 case DataType::Type::kInt8:
7217 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007218 // Type conversion from long to types narrower than int is a result of code
7219 // transformations. To avoid unpredictable results for SEB and SEH, we first
7220 // need to sign-extend the low 32-bit value into bits 32 through 63.
7221 __ Sll(dst, src, 0);
7222 __ Seb(dst, dst);
7223 } else {
7224 __ Seb(dst, src);
7225 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007226 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01007227 case DataType::Type::kUint16:
7228 __ Andi(dst, src, 0xFFFF);
7229 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007230 case DataType::Type::kInt16:
7231 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00007232 // Type conversion from long to types narrower than int is a result of code
7233 // transformations. To avoid unpredictable results for SEB and SEH, we first
7234 // need to sign-extend the low 32-bit value into bits 32 through 63.
7235 __ Sll(dst, src, 0);
7236 __ Seh(dst, dst);
7237 } else {
7238 __ Seh(dst, src);
7239 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007240 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007241 case DataType::Type::kInt32:
7242 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007243 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
7244 // conversions, except when the input and output registers are the same and we are not
7245 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007246 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007247 __ Sll(dst, src, 0);
7248 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007249 break;
7250
7251 default:
7252 LOG(FATAL) << "Unexpected type conversion from " << input_type
7253 << " to " << result_type;
7254 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007255 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007256 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7257 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007258 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007259 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007260 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007261 __ Cvtsl(dst, FTMP);
7262 } else {
7263 __ Cvtdl(dst, FTMP);
7264 }
7265 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007266 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007267 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007268 __ Cvtsw(dst, FTMP);
7269 } else {
7270 __ Cvtdw(dst, FTMP);
7271 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007272 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007273 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
7274 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007275 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7276 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007277
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007278 if (result_type == DataType::Type::kInt64) {
7279 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007280 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007281 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007282 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007283 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007284 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007285 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007286 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007287 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007288 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007289 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007290 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007291 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007292 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007293 } else if (DataType::IsFloatingPointType(result_type) &&
7294 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007295 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7296 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007297 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007298 __ Cvtsd(dst, src);
7299 } else {
7300 __ Cvtds(dst, src);
7301 }
7302 } else {
7303 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7304 << " to " << result_type;
7305 }
7306}
7307
7308void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
7309 HandleShift(ushr);
7310}
7311
7312void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
7313 HandleShift(ushr);
7314}
7315
7316void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7317 HandleBinaryOp(instruction);
7318}
7319
7320void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7321 HandleBinaryOp(instruction);
7322}
7323
7324void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7325 // Nothing to do, this should be removed during prepare for register allocator.
7326 LOG(FATAL) << "Unreachable";
7327}
7328
7329void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7330 // Nothing to do, this should be removed during prepare for register allocator.
7331 LOG(FATAL) << "Unreachable";
7332}
7333
7334void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007335 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007336}
7337
7338void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007339 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007340}
7341
7342void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007343 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007344}
7345
7346void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007347 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007348}
7349
7350void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007351 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007352}
7353
7354void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007355 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007356}
7357
7358void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007359 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007360}
7361
7362void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007363 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007364}
7365
7366void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007367 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007368}
7369
7370void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007371 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007372}
7373
7374void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007375 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007376}
7377
7378void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007379 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007380}
7381
Aart Bike9f37602015-10-09 11:15:55 -07007382void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007383 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007384}
7385
7386void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007387 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007388}
7389
7390void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007391 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007392}
7393
7394void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007395 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007396}
7397
7398void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007399 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007400}
7401
7402void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007403 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007404}
7405
7406void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007407 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007408}
7409
7410void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007411 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007412}
7413
Mark Mendellfe57faa2015-09-18 09:26:15 -04007414// Simple implementation of packed switch - generate cascaded compare/jumps.
7415void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7416 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007417 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007418 locations->SetInAt(0, Location::RequiresRegister());
7419}
7420
Alexey Frunze0960ac52016-12-20 17:24:59 -08007421void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7422 int32_t lower_bound,
7423 uint32_t num_entries,
7424 HBasicBlock* switch_block,
7425 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007426 // Create a set of compare/jumps.
7427 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007428 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007429 // Jump to default if index is negative
7430 // Note: We don't check the case that index is positive while value < lower_bound, because in
7431 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7432 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7433
Alexey Frunze0960ac52016-12-20 17:24:59 -08007434 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007435 // Jump to successors[0] if value == lower_bound.
7436 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7437 int32_t last_index = 0;
7438 for (; num_entries - last_index > 2; last_index += 2) {
7439 __ Addiu(temp_reg, temp_reg, -2);
7440 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7441 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7442 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7443 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7444 }
7445 if (num_entries - last_index == 2) {
7446 // The last missing case_value.
7447 __ Addiu(temp_reg, temp_reg, -1);
7448 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007449 }
7450
7451 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007452 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007453 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007454 }
7455}
7456
Alexey Frunze0960ac52016-12-20 17:24:59 -08007457void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7458 int32_t lower_bound,
7459 uint32_t num_entries,
7460 HBasicBlock* switch_block,
7461 HBasicBlock* default_block) {
7462 // Create a jump table.
7463 std::vector<Mips64Label*> labels(num_entries);
7464 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7465 for (uint32_t i = 0; i < num_entries; i++) {
7466 labels[i] = codegen_->GetLabelOf(successors[i]);
7467 }
7468 JumpTable* table = __ CreateJumpTable(std::move(labels));
7469
7470 // Is the value in range?
7471 __ Addiu32(TMP, value_reg, -lower_bound);
7472 __ LoadConst32(AT, num_entries);
7473 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7474
7475 // We are in the range of the table.
7476 // Load the target address from the jump table, indexing by the value.
7477 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007478 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007479 __ Lw(TMP, TMP, 0);
7480 // Compute the absolute target address by adding the table start address
7481 // (the table contains offsets to targets relative to its start).
7482 __ Daddu(TMP, TMP, AT);
7483 // And jump.
7484 __ Jr(TMP);
7485 __ Nop();
7486}
7487
7488void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7489 int32_t lower_bound = switch_instr->GetStartValue();
7490 uint32_t num_entries = switch_instr->GetNumEntries();
7491 LocationSummary* locations = switch_instr->GetLocations();
7492 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7493 HBasicBlock* switch_block = switch_instr->GetBlock();
7494 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7495
7496 if (num_entries > kPackedSwitchJumpTableThreshold) {
7497 GenTableBasedPackedSwitch(value_reg,
7498 lower_bound,
7499 num_entries,
7500 switch_block,
7501 default_block);
7502 } else {
7503 GenPackedSwitchWithCompares(value_reg,
7504 lower_bound,
7505 num_entries,
7506 switch_block,
7507 default_block);
7508 }
7509}
7510
Chris Larsenc9905a62017-03-13 17:06:18 -07007511void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7512 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007513 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007514 locations->SetInAt(0, Location::RequiresRegister());
7515 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007516}
7517
Chris Larsenc9905a62017-03-13 17:06:18 -07007518void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7519 LocationSummary* locations = instruction->GetLocations();
7520 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7521 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7522 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7523 __ LoadFromOffset(kLoadDoubleword,
7524 locations->Out().AsRegister<GpuRegister>(),
7525 locations->InAt(0).AsRegister<GpuRegister>(),
7526 method_offset);
7527 } else {
7528 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7529 instruction->GetIndex(), kMips64PointerSize));
7530 __ LoadFromOffset(kLoadDoubleword,
7531 locations->Out().AsRegister<GpuRegister>(),
7532 locations->InAt(0).AsRegister<GpuRegister>(),
7533 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7534 __ LoadFromOffset(kLoadDoubleword,
7535 locations->Out().AsRegister<GpuRegister>(),
7536 locations->Out().AsRegister<GpuRegister>(),
7537 method_offset);
7538 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007539}
7540
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007541void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7542 ATTRIBUTE_UNUSED) {
7543 LOG(FATAL) << "Unreachable";
7544}
7545
7546void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7547 ATTRIBUTE_UNUSED) {
7548 LOG(FATAL) << "Unreachable";
7549}
7550
Alexey Frunze4dda3372015-06-01 18:31:49 -07007551} // namespace mips64
7552} // namespace art