blob: a45c68e0ac50bf44d0fdf1d29ac5b9907d81397e [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 Markod8dbc8d2017-09-20 13:37:47 +01001512void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001513 DCHECK(linker_patches->empty());
1514 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001515 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001516 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001517 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001518 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001519 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001520 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001521 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001522 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001523 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001524 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001525 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001526 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001527 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001528 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001529 } else {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001530 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001531 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001532 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001533 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001534 boot_image_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001535 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001536 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1537 method_bss_entry_patches_, linker_patches);
1538 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1539 type_bss_entry_patches_, linker_patches);
1540 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1541 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001542 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001543}
1544
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001545CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001546 MethodReference target_method,
1547 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001548 return NewPcRelativePatch(
1549 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001550}
1551
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001552CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001553 MethodReference target_method,
1554 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001555 return NewPcRelativePatch(
1556 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001557}
1558
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001559CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001560 const DexFile& dex_file,
1561 dex::TypeIndex type_index,
1562 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001563 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001564}
1565
Vladimir Marko1998cd02017-01-13 13:02:58 +00001566CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001567 const DexFile& dex_file,
1568 dex::TypeIndex type_index,
1569 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001570 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001571}
1572
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001573CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001574 const DexFile& dex_file,
1575 dex::StringIndex string_index,
1576 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001577 return NewPcRelativePatch(
1578 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001579}
1580
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001581CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1582 const DexFile& dex_file,
1583 dex::StringIndex string_index,
1584 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001585 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001586}
1587
Alexey Frunze19f6c692016-11-30 19:19:55 -08001588CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001589 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001590 uint32_t offset_or_index,
1591 const PcRelativePatchInfo* info_high,
1592 ArenaDeque<PcRelativePatchInfo>* patches) {
1593 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001594 return &patches->back();
1595}
1596
Alexey Frunzef63f5692016-12-13 17:43:11 -08001597Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1598 return map->GetOrCreate(
1599 value,
1600 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1601}
1602
Alexey Frunze19f6c692016-11-30 19:19:55 -08001603Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1604 return uint64_literals_.GetOrCreate(
1605 value,
1606 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1607}
1608
Alexey Frunzef63f5692016-12-13 17:43:11 -08001609Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001610 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001611}
1612
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001613void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1614 GpuRegister out,
1615 PcRelativePatchInfo* info_low) {
1616 DCHECK(!info_high->patch_info_high);
1617 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001618 // Add the high half of a 32-bit offset to PC.
1619 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001620 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001621 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001622 if (info_low != nullptr) {
1623 DCHECK_EQ(info_low->patch_info_high, info_high);
1624 __ Bind(&info_low->label);
1625 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001626}
1627
Alexey Frunze627c1a02017-01-30 19:28:14 -08001628Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1629 dex::StringIndex string_index,
1630 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001631 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001632 return jit_string_patches_.GetOrCreate(
1633 StringReference(&dex_file, string_index),
1634 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1635}
1636
1637Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1638 dex::TypeIndex type_index,
1639 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001640 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001641 return jit_class_patches_.GetOrCreate(
1642 TypeReference(&dex_file, type_index),
1643 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1644}
1645
1646void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1647 const uint8_t* roots_data,
1648 const Literal* literal,
1649 uint64_t index_in_table) const {
1650 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1651 uintptr_t address =
1652 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1653 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1654}
1655
1656void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1657 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001658 const StringReference& string_reference = entry.first;
1659 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001660 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001661 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001662 }
1663 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001664 const TypeReference& type_reference = entry.first;
1665 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001666 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001667 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001668 }
1669}
1670
David Brazdil58282f42016-01-14 12:45:10 +00001671void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001672 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1673 blocked_core_registers_[ZERO] = true;
1674 blocked_core_registers_[K0] = true;
1675 blocked_core_registers_[K1] = true;
1676 blocked_core_registers_[GP] = true;
1677 blocked_core_registers_[SP] = true;
1678 blocked_core_registers_[RA] = true;
1679
Lazar Trsicd9672662015-09-03 17:33:01 +02001680 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1681 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001682 blocked_core_registers_[AT] = true;
1683 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001684 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685 blocked_fpu_registers_[FTMP] = true;
1686
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001687 if (GetInstructionSetFeatures().HasMsa()) {
1688 // To be used just for MSA instructions.
1689 blocked_fpu_registers_[FTMP2] = true;
1690 }
1691
Alexey Frunze4dda3372015-06-01 18:31:49 -07001692 // Reserve suspend and thread registers.
1693 blocked_core_registers_[S0] = true;
1694 blocked_core_registers_[TR] = true;
1695
1696 // Reserve T9 for function calls
1697 blocked_core_registers_[T9] = true;
1698
Goran Jakovljevic782be112016-06-21 12:39:04 +02001699 if (GetGraph()->IsDebuggable()) {
1700 // Stubs do not save callee-save floating point registers. If the graph
1701 // is debuggable, we need to deal with these registers differently. For
1702 // now, just block them.
1703 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1704 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1705 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 }
1707}
1708
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1710 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001711 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001712}
1713
1714size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1715 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001716 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001717}
1718
1719size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001720 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1721 FpuRegister(reg_id),
1722 SP,
1723 stack_index);
1724 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001725}
1726
1727size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001728 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1729 FpuRegister(reg_id),
1730 SP,
1731 stack_index);
1732 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001733}
1734
1735void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001736 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001737}
1738
1739void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001740 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001741}
1742
Calin Juravle175dc732015-08-25 15:42:32 +01001743void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001744 HInstruction* instruction,
1745 uint32_t dex_pc,
1746 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001747 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001748 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001749 if (EntrypointRequiresStackMap(entrypoint)) {
1750 RecordPcInfo(instruction, dex_pc, slow_path);
1751 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001752}
1753
Alexey Frunze15958152017-02-09 19:08:30 -08001754void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1755 HInstruction* instruction,
1756 SlowPathCode* slow_path) {
1757 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1758 GenerateInvokeRuntime(entry_point_offset);
1759}
1760
1761void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1762 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1763 __ Jalr(T9);
1764 __ Nop();
1765}
1766
Alexey Frunze4dda3372015-06-01 18:31:49 -07001767void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1768 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001769 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1770 const size_t status_byte_offset =
1771 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1772 constexpr uint32_t shifted_initialized_value =
1773 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1774
1775 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1776 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001777 __ Bltuc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001778 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1779 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001780 __ Bind(slow_path->GetExitLabel());
1781}
1782
1783void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1784 __ Sync(0); // only stype 0 is supported
1785}
1786
1787void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1788 HBasicBlock* successor) {
1789 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001790 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1791
1792 if (slow_path == nullptr) {
1793 slow_path =
1794 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1795 instruction->SetSlowPath(slow_path);
1796 codegen_->AddSlowPath(slow_path);
1797 if (successor != nullptr) {
1798 DCHECK(successor->IsLoopHeader());
1799 }
1800 } else {
1801 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1802 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803
1804 __ LoadFromOffset(kLoadUnsignedHalfword,
1805 TMP,
1806 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001807 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001808 if (successor == nullptr) {
1809 __ Bnezc(TMP, slow_path->GetEntryLabel());
1810 __ Bind(slow_path->GetReturnLabel());
1811 } else {
1812 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001813 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001814 // slow_path will return to GetLabelOf(successor).
1815 }
1816}
1817
1818InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1819 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001820 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001821 assembler_(codegen->GetAssembler()),
1822 codegen_(codegen) {}
1823
1824void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1825 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001826 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001827 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001828 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 case DataType::Type::kInt32:
1830 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001831 locations->SetInAt(0, Location::RequiresRegister());
1832 HInstruction* right = instruction->InputAt(1);
1833 bool can_use_imm = false;
1834 if (right->IsConstant()) {
1835 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1836 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1837 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001838 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001839 DCHECK(instruction->IsAdd() || instruction->IsSub());
1840 bool single_use = right->GetUses().HasExactlyOneElement();
1841 if (instruction->IsSub()) {
1842 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1843 imm = -imm;
1844 }
1845 }
1846 if (type == DataType::Type::kInt32) {
1847 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1848 } else {
1849 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1850 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001851 }
1852 }
1853 if (can_use_imm)
1854 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1855 else
1856 locations->SetInAt(1, Location::RequiresRegister());
1857 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1858 }
1859 break;
1860
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 case DataType::Type::kFloat32:
1862 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001863 locations->SetInAt(0, Location::RequiresFpuRegister());
1864 locations->SetInAt(1, Location::RequiresFpuRegister());
1865 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1866 break;
1867
1868 default:
1869 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1870 }
1871}
1872
1873void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001874 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001875 LocationSummary* locations = instruction->GetLocations();
1876
1877 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001878 case DataType::Type::kInt32:
1879 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1881 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1882 Location rhs_location = locations->InAt(1);
1883
1884 GpuRegister rhs_reg = ZERO;
1885 int64_t rhs_imm = 0;
1886 bool use_imm = rhs_location.IsConstant();
1887 if (use_imm) {
1888 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1889 } else {
1890 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1891 }
1892
1893 if (instruction->IsAnd()) {
1894 if (use_imm)
1895 __ Andi(dst, lhs, rhs_imm);
1896 else
1897 __ And(dst, lhs, rhs_reg);
1898 } else if (instruction->IsOr()) {
1899 if (use_imm)
1900 __ Ori(dst, lhs, rhs_imm);
1901 else
1902 __ Or(dst, lhs, rhs_reg);
1903 } else if (instruction->IsXor()) {
1904 if (use_imm)
1905 __ Xori(dst, lhs, rhs_imm);
1906 else
1907 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001908 } else if (instruction->IsAdd() || instruction->IsSub()) {
1909 if (instruction->IsSub()) {
1910 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001911 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001912 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001913 if (use_imm) {
1914 if (IsInt<16>(rhs_imm)) {
1915 __ Addiu(dst, lhs, rhs_imm);
1916 } else {
1917 int16_t rhs_imm_high = High16Bits(rhs_imm);
1918 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1919 if (rhs_imm_low < 0) {
1920 rhs_imm_high += 1;
1921 }
1922 __ Aui(dst, lhs, rhs_imm_high);
1923 if (rhs_imm_low != 0) {
1924 __ Addiu(dst, dst, rhs_imm_low);
1925 }
1926 }
1927 } else {
1928 if (instruction->IsAdd()) {
1929 __ Addu(dst, lhs, rhs_reg);
1930 } else {
1931 DCHECK(instruction->IsSub());
1932 __ Subu(dst, lhs, rhs_reg);
1933 }
1934 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001935 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001936 if (use_imm) {
1937 if (IsInt<16>(rhs_imm)) {
1938 __ Daddiu(dst, lhs, rhs_imm);
1939 } else if (IsInt<32>(rhs_imm)) {
1940 int16_t rhs_imm_high = High16Bits(rhs_imm);
1941 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1942 bool overflow_hi16 = false;
1943 if (rhs_imm_low < 0) {
1944 rhs_imm_high += 1;
1945 overflow_hi16 = (rhs_imm_high == -32768);
1946 }
1947 __ Daui(dst, lhs, rhs_imm_high);
1948 if (rhs_imm_low != 0) {
1949 __ Daddiu(dst, dst, rhs_imm_low);
1950 }
1951 if (overflow_hi16) {
1952 __ Dahi(dst, 1);
1953 }
1954 } else {
1955 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1956 if (rhs_imm_low < 0) {
1957 rhs_imm += (INT64_C(1) << 16);
1958 }
1959 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
1960 if (rhs_imm_upper < 0) {
1961 rhs_imm += (INT64_C(1) << 32);
1962 }
1963 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
1964 if (rhs_imm_high < 0) {
1965 rhs_imm += (INT64_C(1) << 48);
1966 }
1967 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
1968 GpuRegister tmp = lhs;
1969 if (rhs_imm_low != 0) {
1970 __ Daddiu(dst, tmp, rhs_imm_low);
1971 tmp = dst;
1972 }
1973 // Dahi and Dati must use the same input and output register, so we have to initialize
1974 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
1975 // Daui(dst, lhs, 0).
1976 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
1977 __ Daui(dst, tmp, rhs_imm_upper);
1978 }
1979 if (rhs_imm_high != 0) {
1980 __ Dahi(dst, rhs_imm_high);
1981 }
1982 if (rhs_imm_top != 0) {
1983 __ Dati(dst, rhs_imm_top);
1984 }
1985 }
1986 } else if (instruction->IsAdd()) {
1987 __ Daddu(dst, lhs, rhs_reg);
1988 } else {
1989 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001990 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001991 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001992 }
1993 }
1994 break;
1995 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996 case DataType::Type::kFloat32:
1997 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001998 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1999 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2000 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2001 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002003 __ AddS(dst, lhs, rhs);
2004 else
2005 __ AddD(dst, lhs, rhs);
2006 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002008 __ SubS(dst, lhs, rhs);
2009 else
2010 __ SubD(dst, lhs, rhs);
2011 } else {
2012 LOG(FATAL) << "Unexpected floating-point binary operation";
2013 }
2014 break;
2015 }
2016 default:
2017 LOG(FATAL) << "Unexpected binary operation type " << type;
2018 }
2019}
2020
2021void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002022 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002023
Vladimir Markoca6fff82017-10-03 14:49:14 +01002024 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 case DataType::Type::kInt32:
2028 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002029 locations->SetInAt(0, Location::RequiresRegister());
2030 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032 break;
2033 }
2034 default:
2035 LOG(FATAL) << "Unexpected shift type " << type;
2036 }
2037}
2038
2039void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002040 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002041 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043
2044 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 case DataType::Type::kInt32:
2046 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002047 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2048 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2049 Location rhs_location = locations->InAt(1);
2050
2051 GpuRegister rhs_reg = ZERO;
2052 int64_t rhs_imm = 0;
2053 bool use_imm = rhs_location.IsConstant();
2054 if (use_imm) {
2055 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2056 } else {
2057 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2058 }
2059
2060 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002061 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063
Alexey Frunze92d90602015-12-18 18:16:36 -08002064 if (shift_value == 0) {
2065 if (dst != lhs) {
2066 __ Move(dst, lhs);
2067 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002069 if (instr->IsShl()) {
2070 __ Sll(dst, lhs, shift_value);
2071 } else if (instr->IsShr()) {
2072 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002073 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002075 } else {
2076 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002077 }
2078 } else {
2079 if (shift_value < 32) {
2080 if (instr->IsShl()) {
2081 __ Dsll(dst, lhs, shift_value);
2082 } else if (instr->IsShr()) {
2083 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002084 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002085 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002086 } else {
2087 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002088 }
2089 } else {
2090 shift_value -= 32;
2091 if (instr->IsShl()) {
2092 __ Dsll32(dst, lhs, shift_value);
2093 } else if (instr->IsShr()) {
2094 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002095 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002096 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002097 } else {
2098 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002099 }
2100 }
2101 }
2102 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002103 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002104 if (instr->IsShl()) {
2105 __ Sllv(dst, lhs, rhs_reg);
2106 } else if (instr->IsShr()) {
2107 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002108 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002109 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002110 } else {
2111 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002112 }
2113 } else {
2114 if (instr->IsShl()) {
2115 __ Dsllv(dst, lhs, rhs_reg);
2116 } else if (instr->IsShr()) {
2117 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002118 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002120 } else {
2121 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122 }
2123 }
2124 }
2125 break;
2126 }
2127 default:
2128 LOG(FATAL) << "Unexpected shift operation type " << type;
2129 }
2130}
2131
2132void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2133 HandleBinaryOp(instruction);
2134}
2135
2136void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2137 HandleBinaryOp(instruction);
2138}
2139
2140void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2141 HandleBinaryOp(instruction);
2142}
2143
2144void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2145 HandleBinaryOp(instruction);
2146}
2147
2148void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002150 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002151 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002153 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2154 object_array_get_with_read_barrier
2155 ? LocationSummary::kCallOnSlowPath
2156 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002157 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2158 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002160 locations->SetInAt(0, Location::RequiresRegister());
2161 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002162 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002163 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2164 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002165 // The output overlaps in the case of an object array get with
2166 // read barriers enabled: we do not want the move to overwrite the
2167 // array's location, as we need it to emit the read barrier.
2168 locations->SetOut(Location::RequiresRegister(),
2169 object_array_get_with_read_barrier
2170 ? Location::kOutputOverlap
2171 : Location::kNoOutputOverlap);
2172 }
2173 // We need a temporary register for the read barrier marking slow
2174 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2175 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002176 bool temp_needed = instruction->GetIndex()->IsConstant()
2177 ? !kBakerReadBarrierThunksEnableForFields
2178 : !kBakerReadBarrierThunksEnableForArrays;
2179 if (temp_needed) {
2180 locations->AddTemp(Location::RequiresRegister());
2181 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002182 }
2183}
2184
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002185static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2186 auto null_checker = [codegen, instruction]() {
2187 codegen->MaybeRecordImplicitNullCheck(instruction);
2188 };
2189 return null_checker;
2190}
2191
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2193 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002194 Location obj_loc = locations->InAt(0);
2195 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2196 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002197 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002198 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002199 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002202 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2203 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002204 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002205 case DataType::Type::kBool:
2206 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002207 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002208 if (index.IsConstant()) {
2209 size_t offset =
2210 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002211 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002212 } else {
2213 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002214 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215 }
2216 break;
2217 }
2218
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002219 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002220 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 if (index.IsConstant()) {
2222 size_t offset =
2223 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002224 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002225 } else {
2226 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002227 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002228 }
2229 break;
2230 }
2231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002232 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002233 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002234 if (maybe_compressed_char_at) {
2235 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002236 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002237 __ Dext(TMP, TMP, 0, 1);
2238 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2239 "Expecting 0=compressed, 1=uncompressed");
2240 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002241 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002242 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2243 if (maybe_compressed_char_at) {
2244 Mips64Label uncompressed_load, done;
2245 __ Bnezc(TMP, &uncompressed_load);
2246 __ LoadFromOffset(kLoadUnsignedByte,
2247 out,
2248 obj,
2249 data_offset + (const_index << TIMES_1));
2250 __ Bc(&done);
2251 __ Bind(&uncompressed_load);
2252 __ LoadFromOffset(kLoadUnsignedHalfword,
2253 out,
2254 obj,
2255 data_offset + (const_index << TIMES_2));
2256 __ Bind(&done);
2257 } else {
2258 __ LoadFromOffset(kLoadUnsignedHalfword,
2259 out,
2260 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002261 data_offset + (const_index << TIMES_2),
2262 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002263 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002264 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002265 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2266 if (maybe_compressed_char_at) {
2267 Mips64Label uncompressed_load, done;
2268 __ Bnezc(TMP, &uncompressed_load);
2269 __ Daddu(TMP, obj, index_reg);
2270 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2271 __ Bc(&done);
2272 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002273 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002274 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2275 __ Bind(&done);
2276 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002277 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002278 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002279 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002280 }
2281 break;
2282 }
2283
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002284 case DataType::Type::kInt16: {
2285 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2286 if (index.IsConstant()) {
2287 size_t offset =
2288 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2289 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2290 } else {
2291 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2292 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2293 }
2294 break;
2295 }
2296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002297 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002298 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002299 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 LoadOperandType load_type =
2301 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002302 if (index.IsConstant()) {
2303 size_t offset =
2304 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002305 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002306 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002307 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002308 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002309 }
2310 break;
2311 }
2312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002314 static_assert(
2315 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2316 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2317 // /* HeapReference<Object> */ out =
2318 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2319 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002320 bool temp_needed = index.IsConstant()
2321 ? !kBakerReadBarrierThunksEnableForFields
2322 : !kBakerReadBarrierThunksEnableForArrays;
2323 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002324 // Note that a potential implicit null check is handled in this
2325 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002326 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2327 if (index.IsConstant()) {
2328 // Array load with a constant index can be treated as a field load.
2329 size_t offset =
2330 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2331 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2332 out_loc,
2333 obj,
2334 offset,
2335 temp,
2336 /* needs_null_check */ false);
2337 } else {
2338 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2339 out_loc,
2340 obj,
2341 data_offset,
2342 index,
2343 temp,
2344 /* needs_null_check */ false);
2345 }
Alexey Frunze15958152017-02-09 19:08:30 -08002346 } else {
2347 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2348 if (index.IsConstant()) {
2349 size_t offset =
2350 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2351 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2352 // If read barriers are enabled, emit read barriers other than
2353 // Baker's using a slow path (and also unpoison the loaded
2354 // reference, if heap poisoning is enabled).
2355 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2356 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002357 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002358 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2359 // If read barriers are enabled, emit read barriers other than
2360 // Baker's using a slow path (and also unpoison the loaded
2361 // reference, if heap poisoning is enabled).
2362 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2363 out_loc,
2364 out_loc,
2365 obj_loc,
2366 data_offset,
2367 index);
2368 }
2369 }
2370 break;
2371 }
2372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002374 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002375 if (index.IsConstant()) {
2376 size_t offset =
2377 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002378 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002379 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002380 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002381 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002382 }
2383 break;
2384 }
2385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002387 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002388 if (index.IsConstant()) {
2389 size_t offset =
2390 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002391 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002392 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002393 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002394 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002395 }
2396 break;
2397 }
2398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002400 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002401 if (index.IsConstant()) {
2402 size_t offset =
2403 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002404 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002405 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002406 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002407 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002408 }
2409 break;
2410 }
2411
Aart Bik66c158e2018-01-31 12:55:04 -08002412 case DataType::Type::kUint32:
2413 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002414 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002415 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2416 UNREACHABLE();
2417 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002418}
2419
2420void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002421 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002422 locations->SetInAt(0, Location::RequiresRegister());
2423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2424}
2425
2426void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2427 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002428 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002429 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2430 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2431 __ LoadFromOffset(kLoadWord, out, obj, offset);
2432 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002433 // Mask out compression flag from String's array length.
2434 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2435 __ Srl(out, out, 1u);
2436 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002437}
2438
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002439Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2440 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2441 ? Location::ConstantLocation(instruction->AsConstant())
2442 : Location::RequiresRegister();
2443}
2444
2445Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2446 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2447 // We can store a non-zero float or double constant without first loading it into the FPU,
2448 // but we should only prefer this if the constant has a single use.
2449 if (instruction->IsConstant() &&
2450 (instruction->AsConstant()->IsZeroBitPattern() ||
2451 instruction->GetUses().HasExactlyOneElement())) {
2452 return Location::ConstantLocation(instruction->AsConstant());
2453 // Otherwise fall through and require an FPU register for the constant.
2454 }
2455 return Location::RequiresFpuRegister();
2456}
2457
Alexey Frunze4dda3372015-06-01 18:31:49 -07002458void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002459 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002460
2461 bool needs_write_barrier =
2462 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2463 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2464
Vladimir Markoca6fff82017-10-03 14:49:14 +01002465 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002466 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002467 may_need_runtime_call_for_type_check ?
2468 LocationSummary::kCallOnSlowPath :
2469 LocationSummary::kNoCall);
2470
2471 locations->SetInAt(0, Location::RequiresRegister());
2472 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002474 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002475 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002476 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2477 }
2478 if (needs_write_barrier) {
2479 // Temporary register for the write barrier.
2480 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481 }
2482}
2483
2484void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2485 LocationSummary* locations = instruction->GetLocations();
2486 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2487 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002488 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002489 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002490 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002491 bool needs_write_barrier =
2492 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002493 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002494 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002495
2496 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002497 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002498 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002499 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002500 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002501 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002502 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002503 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002504 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2505 }
2506 if (value_location.IsConstant()) {
2507 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2508 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2509 } else {
2510 GpuRegister value = value_location.AsRegister<GpuRegister>();
2511 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002512 }
2513 break;
2514 }
2515
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002516 case DataType::Type::kUint16:
2517 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002519 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002520 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002521 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002522 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002523 }
2524 if (value_location.IsConstant()) {
2525 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2526 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2527 } else {
2528 GpuRegister value = value_location.AsRegister<GpuRegister>();
2529 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002530 }
2531 break;
2532 }
2533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002535 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2536 if (index.IsConstant()) {
2537 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2538 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002539 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002540 }
2541 if (value_location.IsConstant()) {
2542 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2543 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2544 } else {
2545 GpuRegister value = value_location.AsRegister<GpuRegister>();
2546 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2547 }
2548 break;
2549 }
2550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002552 if (value_location.IsConstant()) {
2553 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002554 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002555 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002556 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002557 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002558 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002559 }
Alexey Frunze15958152017-02-09 19:08:30 -08002560 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2561 DCHECK_EQ(value, 0);
2562 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2563 DCHECK(!needs_write_barrier);
2564 DCHECK(!may_need_runtime_call_for_type_check);
2565 break;
2566 }
2567
2568 DCHECK(needs_write_barrier);
2569 GpuRegister value = value_location.AsRegister<GpuRegister>();
2570 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2571 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2572 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2573 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2574 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2575 Mips64Label done;
2576 SlowPathCodeMIPS64* slow_path = nullptr;
2577
2578 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002579 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002580 codegen_->AddSlowPath(slow_path);
2581 if (instruction->GetValueCanBeNull()) {
2582 Mips64Label non_zero;
2583 __ Bnezc(value, &non_zero);
2584 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2585 if (index.IsConstant()) {
2586 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002587 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002588 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002589 }
Alexey Frunze15958152017-02-09 19:08:30 -08002590 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2591 __ Bc(&done);
2592 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002593 }
Alexey Frunze15958152017-02-09 19:08:30 -08002594
2595 // Note that when read barriers are enabled, the type checks
2596 // are performed without read barriers. This is fine, even in
2597 // the case where a class object is in the from-space after
2598 // the flip, as a comparison involving such a type would not
2599 // produce a false positive; it may of course produce a false
2600 // negative, in which case we would take the ArraySet slow
2601 // path.
2602
2603 // /* HeapReference<Class> */ temp1 = obj->klass_
2604 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2605 __ MaybeUnpoisonHeapReference(temp1);
2606
2607 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2608 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2609 // /* HeapReference<Class> */ temp2 = value->klass_
2610 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2611 // If heap poisoning is enabled, no need to unpoison `temp1`
2612 // nor `temp2`, as we are comparing two poisoned references.
2613
2614 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2615 Mips64Label do_put;
2616 __ Beqc(temp1, temp2, &do_put);
2617 // If heap poisoning is enabled, the `temp1` reference has
2618 // not been unpoisoned yet; unpoison it now.
2619 __ MaybeUnpoisonHeapReference(temp1);
2620
2621 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2622 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2623 // If heap poisoning is enabled, no need to unpoison
2624 // `temp1`, as we are comparing against null below.
2625 __ Bnezc(temp1, slow_path->GetEntryLabel());
2626 __ Bind(&do_put);
2627 } else {
2628 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2629 }
2630 }
2631
2632 GpuRegister source = value;
2633 if (kPoisonHeapReferences) {
2634 // Note that in the case where `value` is a null reference,
2635 // we do not enter this block, as a null reference does not
2636 // need poisoning.
2637 __ Move(temp1, value);
2638 __ PoisonHeapReference(temp1);
2639 source = temp1;
2640 }
2641
2642 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2643 if (index.IsConstant()) {
2644 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002645 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002646 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002647 }
2648 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2649
2650 if (!may_need_runtime_call_for_type_check) {
2651 codegen_->MaybeRecordImplicitNullCheck(instruction);
2652 }
2653
2654 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2655
2656 if (done.IsLinked()) {
2657 __ Bind(&done);
2658 }
2659
2660 if (slow_path != nullptr) {
2661 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002662 }
2663 break;
2664 }
2665
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002667 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002668 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002669 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002670 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002671 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002672 }
2673 if (value_location.IsConstant()) {
2674 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2675 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2676 } else {
2677 GpuRegister value = value_location.AsRegister<GpuRegister>();
2678 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002679 }
2680 break;
2681 }
2682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002684 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002685 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002686 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);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002689 }
2690 if (value_location.IsConstant()) {
2691 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2692 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2693 } else {
2694 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2695 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002696 }
2697 break;
2698 }
2699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002701 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002702 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002703 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002704 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002705 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002706 }
2707 if (value_location.IsConstant()) {
2708 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2709 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2710 } else {
2711 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2712 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002713 }
2714 break;
2715 }
2716
Aart Bik66c158e2018-01-31 12:55:04 -08002717 case DataType::Type::kUint32:
2718 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002720 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2721 UNREACHABLE();
2722 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002723}
2724
2725void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002726 RegisterSet caller_saves = RegisterSet::Empty();
2727 InvokeRuntimeCallingConvention calling_convention;
2728 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2729 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2730 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002731
2732 HInstruction* index = instruction->InputAt(0);
2733 HInstruction* length = instruction->InputAt(1);
2734
2735 bool const_index = false;
2736 bool const_length = false;
2737
2738 if (index->IsConstant()) {
2739 if (length->IsConstant()) {
2740 const_index = true;
2741 const_length = true;
2742 } else {
2743 int32_t index_value = index->AsIntConstant()->GetValue();
2744 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2745 const_index = true;
2746 }
2747 }
2748 } else if (length->IsConstant()) {
2749 int32_t length_value = length->AsIntConstant()->GetValue();
2750 if (IsUint<15>(length_value)) {
2751 const_length = true;
2752 }
2753 }
2754
2755 locations->SetInAt(0, const_index
2756 ? Location::ConstantLocation(index->AsConstant())
2757 : Location::RequiresRegister());
2758 locations->SetInAt(1, const_length
2759 ? Location::ConstantLocation(length->AsConstant())
2760 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002761}
2762
2763void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2764 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002765 Location index_loc = locations->InAt(0);
2766 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002767
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002768 if (length_loc.IsConstant()) {
2769 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2770 if (index_loc.IsConstant()) {
2771 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2772 if (index < 0 || index >= length) {
2773 BoundsCheckSlowPathMIPS64* slow_path =
2774 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2775 codegen_->AddSlowPath(slow_path);
2776 __ Bc(slow_path->GetEntryLabel());
2777 } else {
2778 // Nothing to be done.
2779 }
2780 return;
2781 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002782
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002783 BoundsCheckSlowPathMIPS64* slow_path =
2784 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2785 codegen_->AddSlowPath(slow_path);
2786 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2787 if (length == 0) {
2788 __ Bc(slow_path->GetEntryLabel());
2789 } else if (length == 1) {
2790 __ Bnezc(index, slow_path->GetEntryLabel());
2791 } else {
2792 DCHECK(IsUint<15>(length)) << length;
2793 __ Sltiu(TMP, index, length);
2794 __ Beqzc(TMP, slow_path->GetEntryLabel());
2795 }
2796 } else {
2797 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2798 BoundsCheckSlowPathMIPS64* slow_path =
2799 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2800 codegen_->AddSlowPath(slow_path);
2801 if (index_loc.IsConstant()) {
2802 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2803 if (index < 0) {
2804 __ Bc(slow_path->GetEntryLabel());
2805 } else if (index == 0) {
2806 __ Blezc(length, slow_path->GetEntryLabel());
2807 } else {
2808 DCHECK(IsInt<16>(index + 1)) << index;
2809 __ Sltiu(TMP, length, index + 1);
2810 __ Bnezc(TMP, slow_path->GetEntryLabel());
2811 }
2812 } else {
2813 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2814 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2815 }
2816 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002817}
2818
Alexey Frunze15958152017-02-09 19:08:30 -08002819// Temp is used for read barrier.
2820static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2821 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002822 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002823 (kUseBakerReadBarrier ||
2824 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2825 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2826 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2827 return 1;
2828 }
2829 return 0;
2830}
2831
2832// Extra temp is used for read barrier.
2833static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2834 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2835}
2836
Alexey Frunze4dda3372015-06-01 18:31:49 -07002837void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002838 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002839 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002840 LocationSummary* locations =
2841 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002842 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002843 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002844 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845}
2846
2847void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002848 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002849 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002850 Location obj_loc = locations->InAt(0);
2851 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002852 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002853 Location temp_loc = locations->GetTemp(0);
2854 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2855 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2856 DCHECK_LE(num_temps, 2u);
2857 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002858 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2859 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2860 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2861 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2862 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2863 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2864 const uint32_t object_array_data_offset =
2865 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2866 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002867
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002868 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002869 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002870 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2871 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002872 codegen_->AddSlowPath(slow_path);
2873
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002874 // Avoid this check if we know `obj` is not null.
2875 if (instruction->MustDoNullCheck()) {
2876 __ Beqzc(obj, &done);
2877 }
2878
2879 switch (type_check_kind) {
2880 case TypeCheckKind::kExactCheck:
2881 case TypeCheckKind::kArrayCheck: {
2882 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002883 GenerateReferenceLoadTwoRegisters(instruction,
2884 temp_loc,
2885 obj_loc,
2886 class_offset,
2887 maybe_temp2_loc,
2888 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002889 // Jump to slow path for throwing the exception or doing a
2890 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002891 __ Bnec(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002892 break;
2893 }
2894
2895 case TypeCheckKind::kAbstractClassCheck: {
2896 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002897 GenerateReferenceLoadTwoRegisters(instruction,
2898 temp_loc,
2899 obj_loc,
2900 class_offset,
2901 maybe_temp2_loc,
2902 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002903 // If the class is abstract, we eagerly fetch the super class of the
2904 // object to avoid doing a comparison we know will fail.
2905 Mips64Label loop;
2906 __ Bind(&loop);
2907 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002908 GenerateReferenceLoadOneRegister(instruction,
2909 temp_loc,
2910 super_offset,
2911 maybe_temp2_loc,
2912 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002913 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2914 // exception.
2915 __ Beqzc(temp, slow_path->GetEntryLabel());
2916 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002917 __ Bnec(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002918 break;
2919 }
2920
2921 case TypeCheckKind::kClassHierarchyCheck: {
2922 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002923 GenerateReferenceLoadTwoRegisters(instruction,
2924 temp_loc,
2925 obj_loc,
2926 class_offset,
2927 maybe_temp2_loc,
2928 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002929 // Walk over the class hierarchy to find a match.
2930 Mips64Label loop;
2931 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002932 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002933 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002934 GenerateReferenceLoadOneRegister(instruction,
2935 temp_loc,
2936 super_offset,
2937 maybe_temp2_loc,
2938 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002939 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2940 // exception. Otherwise, jump to the beginning of the loop.
2941 __ Bnezc(temp, &loop);
2942 __ Bc(slow_path->GetEntryLabel());
2943 break;
2944 }
2945
2946 case TypeCheckKind::kArrayObjectCheck: {
2947 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002948 GenerateReferenceLoadTwoRegisters(instruction,
2949 temp_loc,
2950 obj_loc,
2951 class_offset,
2952 maybe_temp2_loc,
2953 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002954 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002955 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002956 // Otherwise, we need to check that the object's class is a non-primitive array.
2957 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002958 GenerateReferenceLoadOneRegister(instruction,
2959 temp_loc,
2960 component_offset,
2961 maybe_temp2_loc,
2962 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002963 // If the component type is null, jump to the slow path to throw the exception.
2964 __ Beqzc(temp, slow_path->GetEntryLabel());
2965 // Otherwise, the object is indeed an array, further check that this component
2966 // type is not a primitive type.
2967 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2968 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2969 __ Bnezc(temp, slow_path->GetEntryLabel());
2970 break;
2971 }
2972
2973 case TypeCheckKind::kUnresolvedCheck:
2974 // We always go into the type check slow path for the unresolved check case.
2975 // We cannot directly call the CheckCast runtime entry point
2976 // without resorting to a type checking slow path here (i.e. by
2977 // calling InvokeRuntime directly), as it would require to
2978 // assign fixed registers for the inputs of this HInstanceOf
2979 // instruction (following the runtime calling convention), which
2980 // might be cluttered by the potential first read barrier
2981 // emission at the beginning of this method.
2982 __ Bc(slow_path->GetEntryLabel());
2983 break;
2984
2985 case TypeCheckKind::kInterfaceCheck: {
2986 // Avoid read barriers to improve performance of the fast path. We can not get false
2987 // positives by doing this.
2988 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002989 GenerateReferenceLoadTwoRegisters(instruction,
2990 temp_loc,
2991 obj_loc,
2992 class_offset,
2993 maybe_temp2_loc,
2994 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002995 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002996 GenerateReferenceLoadTwoRegisters(instruction,
2997 temp_loc,
2998 temp_loc,
2999 iftable_offset,
3000 maybe_temp2_loc,
3001 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003002 // Iftable is never null.
3003 __ Lw(TMP, temp, array_length_offset);
3004 // Loop through the iftable and check if any class matches.
3005 Mips64Label loop;
3006 __ Bind(&loop);
3007 __ Beqzc(TMP, slow_path->GetEntryLabel());
3008 __ Lwu(AT, temp, object_array_data_offset);
3009 __ MaybeUnpoisonHeapReference(AT);
3010 // Go to next interface.
3011 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3012 __ Addiu(TMP, TMP, -2);
3013 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003014 __ Bnec(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003015 break;
3016 }
3017 }
3018
3019 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003020 __ Bind(slow_path->GetExitLabel());
3021}
3022
3023void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3024 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003025 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003026 locations->SetInAt(0, Location::RequiresRegister());
3027 if (check->HasUses()) {
3028 locations->SetOut(Location::SameAsFirstInput());
3029 }
3030}
3031
3032void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3033 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003034 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003035 check->GetLoadClass(),
3036 check,
3037 check->GetDexPc(),
3038 true);
3039 codegen_->AddSlowPath(slow_path);
3040 GenerateClassInitializationCheck(slow_path,
3041 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3042}
3043
3044void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003046
Vladimir Markoca6fff82017-10-03 14:49:14 +01003047 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003048
3049 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003050 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003051 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003052 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003054 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003055 case DataType::Type::kInt32:
3056 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003057 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003058 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3060 break;
3061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kFloat32:
3063 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003064 locations->SetInAt(0, Location::RequiresFpuRegister());
3065 locations->SetInAt(1, Location::RequiresFpuRegister());
3066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003067 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003068
3069 default:
3070 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3071 }
3072}
3073
3074void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3075 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003076 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003078
3079 // 0 if: left == right
3080 // 1 if: left > right
3081 // -1 if: left < right
3082 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003083 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003084 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003087 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003088 case DataType::Type::kInt32:
3089 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003090 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003091 Location rhs_location = locations->InAt(1);
3092 bool use_imm = rhs_location.IsConstant();
3093 GpuRegister rhs = ZERO;
3094 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003095 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003096 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3097 if (value != 0) {
3098 rhs = AT;
3099 __ LoadConst64(rhs, value);
3100 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003101 } else {
3102 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3103 if (value != 0) {
3104 rhs = AT;
3105 __ LoadConst32(rhs, value);
3106 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003107 }
3108 } else {
3109 rhs = rhs_location.AsRegister<GpuRegister>();
3110 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003111 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003112 __ Slt(res, rhs, lhs);
3113 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003114 break;
3115 }
3116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003117 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003118 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3119 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3120 Mips64Label done;
3121 __ CmpEqS(FTMP, lhs, rhs);
3122 __ LoadConst32(res, 0);
3123 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003124 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003125 __ CmpLtS(FTMP, lhs, rhs);
3126 __ LoadConst32(res, -1);
3127 __ Bc1nez(FTMP, &done);
3128 __ LoadConst32(res, 1);
3129 } else {
3130 __ CmpLtS(FTMP, rhs, lhs);
3131 __ LoadConst32(res, 1);
3132 __ Bc1nez(FTMP, &done);
3133 __ LoadConst32(res, -1);
3134 }
3135 __ Bind(&done);
3136 break;
3137 }
3138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003140 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3141 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3142 Mips64Label done;
3143 __ CmpEqD(FTMP, lhs, rhs);
3144 __ LoadConst32(res, 0);
3145 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003146 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003147 __ CmpLtD(FTMP, lhs, rhs);
3148 __ LoadConst32(res, -1);
3149 __ Bc1nez(FTMP, &done);
3150 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003151 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003152 __ CmpLtD(FTMP, rhs, lhs);
3153 __ LoadConst32(res, 1);
3154 __ Bc1nez(FTMP, &done);
3155 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003156 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003157 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003158 break;
3159 }
3160
3161 default:
3162 LOG(FATAL) << "Unimplemented compare type " << in_type;
3163 }
3164}
3165
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003166void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003167 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003168 switch (instruction->InputAt(0)->GetType()) {
3169 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003170 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003171 locations->SetInAt(0, Location::RequiresRegister());
3172 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3173 break;
3174
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003175 case DataType::Type::kFloat32:
3176 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003177 locations->SetInAt(0, Location::RequiresFpuRegister());
3178 locations->SetInAt(1, Location::RequiresFpuRegister());
3179 break;
3180 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003181 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3183 }
3184}
3185
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003186void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003187 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003188 return;
3189 }
3190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003192 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003193 switch (type) {
3194 default:
3195 // Integer case.
3196 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3197 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003198 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003199 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3200 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003201 case DataType::Type::kFloat32:
3202 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003203 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3204 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003205 }
3206}
3207
Alexey Frunzec857c742015-09-23 15:12:39 -07003208void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3209 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003211
3212 LocationSummary* locations = instruction->GetLocations();
3213 Location second = locations->InAt(1);
3214 DCHECK(second.IsConstant());
3215
3216 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3217 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3218 int64_t imm = Int64FromConstant(second.GetConstant());
3219 DCHECK(imm == 1 || imm == -1);
3220
3221 if (instruction->IsRem()) {
3222 __ Move(out, ZERO);
3223 } else {
3224 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003226 __ Subu(out, ZERO, dividend);
3227 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003229 __ Dsubu(out, ZERO, dividend);
3230 }
3231 } else if (out != dividend) {
3232 __ Move(out, dividend);
3233 }
3234 }
3235}
3236
3237void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3238 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003240
3241 LocationSummary* locations = instruction->GetLocations();
3242 Location second = locations->InAt(1);
3243 DCHECK(second.IsConstant());
3244
3245 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3246 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3247 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003248 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003249 int ctz_imm = CTZ(abs_imm);
3250
3251 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003252 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003253 if (ctz_imm == 1) {
3254 // Fast path for division by +/-2, which is very common.
3255 __ Srl(TMP, dividend, 31);
3256 } else {
3257 __ Sra(TMP, dividend, 31);
3258 __ Srl(TMP, TMP, 32 - ctz_imm);
3259 }
3260 __ Addu(out, dividend, TMP);
3261 __ Sra(out, out, ctz_imm);
3262 if (imm < 0) {
3263 __ Subu(out, ZERO, out);
3264 }
3265 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003266 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003267 if (ctz_imm == 1) {
3268 // Fast path for division by +/-2, which is very common.
3269 __ Dsrl32(TMP, dividend, 31);
3270 } else {
3271 __ Dsra32(TMP, dividend, 31);
3272 if (ctz_imm > 32) {
3273 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3274 } else {
3275 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3276 }
3277 }
3278 __ Daddu(out, dividend, TMP);
3279 if (ctz_imm < 32) {
3280 __ Dsra(out, out, ctz_imm);
3281 } else {
3282 __ Dsra32(out, out, ctz_imm - 32);
3283 }
3284 if (imm < 0) {
3285 __ Dsubu(out, ZERO, out);
3286 }
3287 }
3288 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003289 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003290 if (ctz_imm == 1) {
3291 // Fast path for modulo +/-2, which is very common.
3292 __ Sra(TMP, dividend, 31);
3293 __ Subu(out, dividend, TMP);
3294 __ Andi(out, out, 1);
3295 __ Addu(out, out, TMP);
3296 } else {
3297 __ Sra(TMP, dividend, 31);
3298 __ Srl(TMP, TMP, 32 - ctz_imm);
3299 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003300 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003301 __ Subu(out, out, TMP);
3302 }
3303 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003304 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003305 if (ctz_imm == 1) {
3306 // Fast path for modulo +/-2, which is very common.
3307 __ Dsra32(TMP, dividend, 31);
3308 __ Dsubu(out, dividend, TMP);
3309 __ Andi(out, out, 1);
3310 __ Daddu(out, out, TMP);
3311 } else {
3312 __ Dsra32(TMP, dividend, 31);
3313 if (ctz_imm > 32) {
3314 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3315 } else {
3316 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3317 }
3318 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003319 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003320 __ Dsubu(out, out, TMP);
3321 }
3322 }
3323 }
3324}
3325
3326void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3327 DCHECK(instruction->IsDiv() || instruction->IsRem());
3328
3329 LocationSummary* locations = instruction->GetLocations();
3330 Location second = locations->InAt(1);
3331 DCHECK(second.IsConstant());
3332
3333 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3334 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3335 int64_t imm = Int64FromConstant(second.GetConstant());
3336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003337 DataType::Type type = instruction->GetResultType();
3338 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003339
3340 int64_t magic;
3341 int shift;
3342 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003343 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003344 &magic,
3345 &shift);
3346
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003347 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003348 __ LoadConst32(TMP, magic);
3349 __ MuhR6(TMP, dividend, TMP);
3350
3351 if (imm > 0 && magic < 0) {
3352 __ Addu(TMP, TMP, dividend);
3353 } else if (imm < 0 && magic > 0) {
3354 __ Subu(TMP, TMP, dividend);
3355 }
3356
3357 if (shift != 0) {
3358 __ Sra(TMP, TMP, shift);
3359 }
3360
3361 if (instruction->IsDiv()) {
3362 __ Sra(out, TMP, 31);
3363 __ Subu(out, TMP, out);
3364 } else {
3365 __ Sra(AT, TMP, 31);
3366 __ Subu(AT, TMP, AT);
3367 __ LoadConst32(TMP, imm);
3368 __ MulR6(TMP, AT, TMP);
3369 __ Subu(out, dividend, TMP);
3370 }
3371 } else {
3372 __ LoadConst64(TMP, magic);
3373 __ Dmuh(TMP, dividend, TMP);
3374
3375 if (imm > 0 && magic < 0) {
3376 __ Daddu(TMP, TMP, dividend);
3377 } else if (imm < 0 && magic > 0) {
3378 __ Dsubu(TMP, TMP, dividend);
3379 }
3380
3381 if (shift >= 32) {
3382 __ Dsra32(TMP, TMP, shift - 32);
3383 } else if (shift > 0) {
3384 __ Dsra(TMP, TMP, shift);
3385 }
3386
3387 if (instruction->IsDiv()) {
3388 __ Dsra32(out, TMP, 31);
3389 __ Dsubu(out, TMP, out);
3390 } else {
3391 __ Dsra32(AT, TMP, 31);
3392 __ Dsubu(AT, TMP, AT);
3393 __ LoadConst64(TMP, imm);
3394 __ Dmul(TMP, AT, TMP);
3395 __ Dsubu(out, dividend, TMP);
3396 }
3397 }
3398}
3399
3400void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3401 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 DataType::Type type = instruction->GetResultType();
3403 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003404
3405 LocationSummary* locations = instruction->GetLocations();
3406 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3407 Location second = locations->InAt(1);
3408
3409 if (second.IsConstant()) {
3410 int64_t imm = Int64FromConstant(second.GetConstant());
3411 if (imm == 0) {
3412 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3413 } else if (imm == 1 || imm == -1) {
3414 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003415 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003416 DivRemByPowerOfTwo(instruction);
3417 } else {
3418 DCHECK(imm <= -2 || imm >= 2);
3419 GenerateDivRemWithAnyConstant(instruction);
3420 }
3421 } else {
3422 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3423 GpuRegister divisor = second.AsRegister<GpuRegister>();
3424 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003425 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003426 __ DivR6(out, dividend, divisor);
3427 else
3428 __ Ddiv(out, dividend, divisor);
3429 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003430 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003431 __ ModR6(out, dividend, divisor);
3432 else
3433 __ Dmod(out, dividend, divisor);
3434 }
3435 }
3436}
3437
Alexey Frunze4dda3372015-06-01 18:31:49 -07003438void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3439 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003440 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003441 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003442 case DataType::Type::kInt32:
3443 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003444 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003445 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3447 break;
3448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003449 case DataType::Type::kFloat32:
3450 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003451 locations->SetInAt(0, Location::RequiresFpuRegister());
3452 locations->SetInAt(1, Location::RequiresFpuRegister());
3453 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3454 break;
3455
3456 default:
3457 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3458 }
3459}
3460
3461void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003463 LocationSummary* locations = instruction->GetLocations();
3464
3465 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 case DataType::Type::kInt32:
3467 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003468 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003469 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003470 case DataType::Type::kFloat32:
3471 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3473 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3474 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003475 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003476 __ DivS(dst, lhs, rhs);
3477 else
3478 __ DivD(dst, lhs, rhs);
3479 break;
3480 }
3481 default:
3482 LOG(FATAL) << "Unexpected div type " << type;
3483 }
3484}
3485
3486void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003487 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003488 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003489}
3490
3491void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3492 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003493 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003494 codegen_->AddSlowPath(slow_path);
3495 Location value = instruction->GetLocations()->InAt(0);
3496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003497 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003499 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003500 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003501 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003502 }
3503
3504 if (value.IsConstant()) {
3505 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3506 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003507 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003508 } else {
3509 // A division by a non-null constant is valid. We don't need to perform
3510 // any check, so simply fall through.
3511 }
3512 } else {
3513 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3514 }
3515}
3516
3517void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3518 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003519 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003520 locations->SetOut(Location::ConstantLocation(constant));
3521}
3522
3523void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3524 // Will be generated at use site.
3525}
3526
3527void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3528 exit->SetLocations(nullptr);
3529}
3530
3531void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3532}
3533
3534void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3535 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003536 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003537 locations->SetOut(Location::ConstantLocation(constant));
3538}
3539
3540void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3541 // Will be generated at use site.
3542}
3543
David Brazdilfc6a86a2015-06-26 10:33:45 +00003544void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003545 if (successor->IsExitBlock()) {
3546 DCHECK(got->GetPrevious()->AlwaysThrows());
3547 return; // no code needed
3548 }
3549
Alexey Frunze4dda3372015-06-01 18:31:49 -07003550 HBasicBlock* block = got->GetBlock();
3551 HInstruction* previous = got->GetPrevious();
3552 HLoopInformation* info = block->GetLoopInformation();
3553
3554 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01003555 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3556 __ Ld(AT, SP, kCurrentMethodStackOffset);
3557 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3558 __ Addiu(TMP, TMP, 1);
3559 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3560 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3562 return;
3563 }
3564 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3565 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3566 }
3567 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003568 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003569 }
3570}
3571
David Brazdilfc6a86a2015-06-26 10:33:45 +00003572void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3573 got->SetLocations(nullptr);
3574}
3575
3576void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3577 HandleGoto(got, got->GetSuccessor());
3578}
3579
3580void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3581 try_boundary->SetLocations(nullptr);
3582}
3583
3584void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3585 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3586 if (!successor->IsExitBlock()) {
3587 HandleGoto(try_boundary, successor);
3588 }
3589}
3590
Alexey Frunze299a9392015-12-08 16:08:02 -08003591void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3592 bool is64bit,
3593 LocationSummary* locations) {
3594 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3595 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3596 Location rhs_location = locations->InAt(1);
3597 GpuRegister rhs_reg = ZERO;
3598 int64_t rhs_imm = 0;
3599 bool use_imm = rhs_location.IsConstant();
3600 if (use_imm) {
3601 if (is64bit) {
3602 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3603 } else {
3604 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3605 }
3606 } else {
3607 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3608 }
3609 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3610
3611 switch (cond) {
3612 case kCondEQ:
3613 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003614 if (use_imm && IsInt<16>(-rhs_imm)) {
3615 if (rhs_imm == 0) {
3616 if (cond == kCondEQ) {
3617 __ Sltiu(dst, lhs, 1);
3618 } else {
3619 __ Sltu(dst, ZERO, lhs);
3620 }
3621 } else {
3622 if (is64bit) {
3623 __ Daddiu(dst, lhs, -rhs_imm);
3624 } else {
3625 __ Addiu(dst, lhs, -rhs_imm);
3626 }
3627 if (cond == kCondEQ) {
3628 __ Sltiu(dst, dst, 1);
3629 } else {
3630 __ Sltu(dst, ZERO, dst);
3631 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003632 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003633 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003634 if (use_imm && IsUint<16>(rhs_imm)) {
3635 __ Xori(dst, lhs, rhs_imm);
3636 } else {
3637 if (use_imm) {
3638 rhs_reg = TMP;
3639 __ LoadConst64(rhs_reg, rhs_imm);
3640 }
3641 __ Xor(dst, lhs, rhs_reg);
3642 }
3643 if (cond == kCondEQ) {
3644 __ Sltiu(dst, dst, 1);
3645 } else {
3646 __ Sltu(dst, ZERO, dst);
3647 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003648 }
3649 break;
3650
3651 case kCondLT:
3652 case kCondGE:
3653 if (use_imm && IsInt<16>(rhs_imm)) {
3654 __ Slti(dst, lhs, rhs_imm);
3655 } else {
3656 if (use_imm) {
3657 rhs_reg = TMP;
3658 __ LoadConst64(rhs_reg, rhs_imm);
3659 }
3660 __ Slt(dst, lhs, rhs_reg);
3661 }
3662 if (cond == kCondGE) {
3663 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3664 // only the slt instruction but no sge.
3665 __ Xori(dst, dst, 1);
3666 }
3667 break;
3668
3669 case kCondLE:
3670 case kCondGT:
3671 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3672 // Simulate lhs <= rhs via lhs < rhs + 1.
3673 __ Slti(dst, lhs, rhs_imm_plus_one);
3674 if (cond == kCondGT) {
3675 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3676 // only the slti instruction but no sgti.
3677 __ Xori(dst, dst, 1);
3678 }
3679 } else {
3680 if (use_imm) {
3681 rhs_reg = TMP;
3682 __ LoadConst64(rhs_reg, rhs_imm);
3683 }
3684 __ Slt(dst, rhs_reg, lhs);
3685 if (cond == kCondLE) {
3686 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3687 // only the slt instruction but no sle.
3688 __ Xori(dst, dst, 1);
3689 }
3690 }
3691 break;
3692
3693 case kCondB:
3694 case kCondAE:
3695 if (use_imm && IsInt<16>(rhs_imm)) {
3696 // Sltiu sign-extends its 16-bit immediate operand before
3697 // the comparison and thus lets us compare directly with
3698 // unsigned values in the ranges [0, 0x7fff] and
3699 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3700 __ Sltiu(dst, lhs, rhs_imm);
3701 } else {
3702 if (use_imm) {
3703 rhs_reg = TMP;
3704 __ LoadConst64(rhs_reg, rhs_imm);
3705 }
3706 __ Sltu(dst, lhs, rhs_reg);
3707 }
3708 if (cond == kCondAE) {
3709 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3710 // only the sltu instruction but no sgeu.
3711 __ Xori(dst, dst, 1);
3712 }
3713 break;
3714
3715 case kCondBE:
3716 case kCondA:
3717 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3718 // Simulate lhs <= rhs via lhs < rhs + 1.
3719 // Note that this only works if rhs + 1 does not overflow
3720 // to 0, hence the check above.
3721 // Sltiu sign-extends its 16-bit immediate operand before
3722 // the comparison and thus lets us compare directly with
3723 // unsigned values in the ranges [0, 0x7fff] and
3724 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3725 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3726 if (cond == kCondA) {
3727 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3728 // only the sltiu instruction but no sgtiu.
3729 __ Xori(dst, dst, 1);
3730 }
3731 } else {
3732 if (use_imm) {
3733 rhs_reg = TMP;
3734 __ LoadConst64(rhs_reg, rhs_imm);
3735 }
3736 __ Sltu(dst, rhs_reg, lhs);
3737 if (cond == kCondBE) {
3738 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3739 // only the sltu instruction but no sleu.
3740 __ Xori(dst, dst, 1);
3741 }
3742 }
3743 break;
3744 }
3745}
3746
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003747bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3748 bool is64bit,
3749 LocationSummary* input_locations,
3750 GpuRegister dst) {
3751 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3752 Location rhs_location = input_locations->InAt(1);
3753 GpuRegister rhs_reg = ZERO;
3754 int64_t rhs_imm = 0;
3755 bool use_imm = rhs_location.IsConstant();
3756 if (use_imm) {
3757 if (is64bit) {
3758 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3759 } else {
3760 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3761 }
3762 } else {
3763 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3764 }
3765 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3766
3767 switch (cond) {
3768 case kCondEQ:
3769 case kCondNE:
3770 if (use_imm && IsInt<16>(-rhs_imm)) {
3771 if (is64bit) {
3772 __ Daddiu(dst, lhs, -rhs_imm);
3773 } else {
3774 __ Addiu(dst, lhs, -rhs_imm);
3775 }
3776 } else if (use_imm && IsUint<16>(rhs_imm)) {
3777 __ Xori(dst, lhs, rhs_imm);
3778 } else {
3779 if (use_imm) {
3780 rhs_reg = TMP;
3781 __ LoadConst64(rhs_reg, rhs_imm);
3782 }
3783 __ Xor(dst, lhs, rhs_reg);
3784 }
3785 return (cond == kCondEQ);
3786
3787 case kCondLT:
3788 case kCondGE:
3789 if (use_imm && IsInt<16>(rhs_imm)) {
3790 __ Slti(dst, lhs, rhs_imm);
3791 } else {
3792 if (use_imm) {
3793 rhs_reg = TMP;
3794 __ LoadConst64(rhs_reg, rhs_imm);
3795 }
3796 __ Slt(dst, lhs, rhs_reg);
3797 }
3798 return (cond == kCondGE);
3799
3800 case kCondLE:
3801 case kCondGT:
3802 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3803 // Simulate lhs <= rhs via lhs < rhs + 1.
3804 __ Slti(dst, lhs, rhs_imm_plus_one);
3805 return (cond == kCondGT);
3806 } else {
3807 if (use_imm) {
3808 rhs_reg = TMP;
3809 __ LoadConst64(rhs_reg, rhs_imm);
3810 }
3811 __ Slt(dst, rhs_reg, lhs);
3812 return (cond == kCondLE);
3813 }
3814
3815 case kCondB:
3816 case kCondAE:
3817 if (use_imm && IsInt<16>(rhs_imm)) {
3818 // Sltiu sign-extends its 16-bit immediate operand before
3819 // the comparison and thus lets us compare directly with
3820 // unsigned values in the ranges [0, 0x7fff] and
3821 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3822 __ Sltiu(dst, lhs, rhs_imm);
3823 } else {
3824 if (use_imm) {
3825 rhs_reg = TMP;
3826 __ LoadConst64(rhs_reg, rhs_imm);
3827 }
3828 __ Sltu(dst, lhs, rhs_reg);
3829 }
3830 return (cond == kCondAE);
3831
3832 case kCondBE:
3833 case kCondA:
3834 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3835 // Simulate lhs <= rhs via lhs < rhs + 1.
3836 // Note that this only works if rhs + 1 does not overflow
3837 // to 0, hence the check above.
3838 // Sltiu sign-extends its 16-bit immediate operand before
3839 // the comparison and thus lets us compare directly with
3840 // unsigned values in the ranges [0, 0x7fff] and
3841 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3842 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3843 return (cond == kCondA);
3844 } else {
3845 if (use_imm) {
3846 rhs_reg = TMP;
3847 __ LoadConst64(rhs_reg, rhs_imm);
3848 }
3849 __ Sltu(dst, rhs_reg, lhs);
3850 return (cond == kCondBE);
3851 }
3852 }
3853}
3854
Alexey Frunze299a9392015-12-08 16:08:02 -08003855void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3856 bool is64bit,
3857 LocationSummary* locations,
3858 Mips64Label* label) {
3859 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3860 Location rhs_location = locations->InAt(1);
3861 GpuRegister rhs_reg = ZERO;
3862 int64_t rhs_imm = 0;
3863 bool use_imm = rhs_location.IsConstant();
3864 if (use_imm) {
3865 if (is64bit) {
3866 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3867 } else {
3868 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3869 }
3870 } else {
3871 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3872 }
3873
3874 if (use_imm && rhs_imm == 0) {
3875 switch (cond) {
3876 case kCondEQ:
3877 case kCondBE: // <= 0 if zero
3878 __ Beqzc(lhs, label);
3879 break;
3880 case kCondNE:
3881 case kCondA: // > 0 if non-zero
3882 __ Bnezc(lhs, label);
3883 break;
3884 case kCondLT:
3885 __ Bltzc(lhs, label);
3886 break;
3887 case kCondGE:
3888 __ Bgezc(lhs, label);
3889 break;
3890 case kCondLE:
3891 __ Blezc(lhs, label);
3892 break;
3893 case kCondGT:
3894 __ Bgtzc(lhs, label);
3895 break;
3896 case kCondB: // always false
3897 break;
3898 case kCondAE: // always true
3899 __ Bc(label);
3900 break;
3901 }
3902 } else {
3903 if (use_imm) {
3904 rhs_reg = TMP;
3905 __ LoadConst64(rhs_reg, rhs_imm);
3906 }
3907 switch (cond) {
3908 case kCondEQ:
3909 __ Beqc(lhs, rhs_reg, label);
3910 break;
3911 case kCondNE:
3912 __ Bnec(lhs, rhs_reg, label);
3913 break;
3914 case kCondLT:
3915 __ Bltc(lhs, rhs_reg, label);
3916 break;
3917 case kCondGE:
3918 __ Bgec(lhs, rhs_reg, label);
3919 break;
3920 case kCondLE:
3921 __ Bgec(rhs_reg, lhs, label);
3922 break;
3923 case kCondGT:
3924 __ Bltc(rhs_reg, lhs, label);
3925 break;
3926 case kCondB:
3927 __ Bltuc(lhs, rhs_reg, label);
3928 break;
3929 case kCondAE:
3930 __ Bgeuc(lhs, rhs_reg, label);
3931 break;
3932 case kCondBE:
3933 __ Bgeuc(rhs_reg, lhs, label);
3934 break;
3935 case kCondA:
3936 __ Bltuc(rhs_reg, lhs, label);
3937 break;
3938 }
3939 }
3940}
3941
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003942void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3943 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003944 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003945 LocationSummary* locations) {
3946 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3947 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3948 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003949 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003950 switch (cond) {
3951 case kCondEQ:
3952 __ CmpEqS(FTMP, lhs, rhs);
3953 __ Mfc1(dst, FTMP);
3954 __ Andi(dst, dst, 1);
3955 break;
3956 case kCondNE:
3957 __ CmpEqS(FTMP, lhs, rhs);
3958 __ Mfc1(dst, FTMP);
3959 __ Addiu(dst, dst, 1);
3960 break;
3961 case kCondLT:
3962 if (gt_bias) {
3963 __ CmpLtS(FTMP, lhs, rhs);
3964 } else {
3965 __ CmpUltS(FTMP, lhs, rhs);
3966 }
3967 __ Mfc1(dst, FTMP);
3968 __ Andi(dst, dst, 1);
3969 break;
3970 case kCondLE:
3971 if (gt_bias) {
3972 __ CmpLeS(FTMP, lhs, rhs);
3973 } else {
3974 __ CmpUleS(FTMP, lhs, rhs);
3975 }
3976 __ Mfc1(dst, FTMP);
3977 __ Andi(dst, dst, 1);
3978 break;
3979 case kCondGT:
3980 if (gt_bias) {
3981 __ CmpUltS(FTMP, rhs, lhs);
3982 } else {
3983 __ CmpLtS(FTMP, rhs, lhs);
3984 }
3985 __ Mfc1(dst, FTMP);
3986 __ Andi(dst, dst, 1);
3987 break;
3988 case kCondGE:
3989 if (gt_bias) {
3990 __ CmpUleS(FTMP, rhs, lhs);
3991 } else {
3992 __ CmpLeS(FTMP, rhs, lhs);
3993 }
3994 __ Mfc1(dst, FTMP);
3995 __ Andi(dst, dst, 1);
3996 break;
3997 default:
3998 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3999 UNREACHABLE();
4000 }
4001 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004003 switch (cond) {
4004 case kCondEQ:
4005 __ CmpEqD(FTMP, lhs, rhs);
4006 __ Mfc1(dst, FTMP);
4007 __ Andi(dst, dst, 1);
4008 break;
4009 case kCondNE:
4010 __ CmpEqD(FTMP, lhs, rhs);
4011 __ Mfc1(dst, FTMP);
4012 __ Addiu(dst, dst, 1);
4013 break;
4014 case kCondLT:
4015 if (gt_bias) {
4016 __ CmpLtD(FTMP, lhs, rhs);
4017 } else {
4018 __ CmpUltD(FTMP, lhs, rhs);
4019 }
4020 __ Mfc1(dst, FTMP);
4021 __ Andi(dst, dst, 1);
4022 break;
4023 case kCondLE:
4024 if (gt_bias) {
4025 __ CmpLeD(FTMP, lhs, rhs);
4026 } else {
4027 __ CmpUleD(FTMP, lhs, rhs);
4028 }
4029 __ Mfc1(dst, FTMP);
4030 __ Andi(dst, dst, 1);
4031 break;
4032 case kCondGT:
4033 if (gt_bias) {
4034 __ CmpUltD(FTMP, rhs, lhs);
4035 } else {
4036 __ CmpLtD(FTMP, rhs, lhs);
4037 }
4038 __ Mfc1(dst, FTMP);
4039 __ Andi(dst, dst, 1);
4040 break;
4041 case kCondGE:
4042 if (gt_bias) {
4043 __ CmpUleD(FTMP, rhs, lhs);
4044 } else {
4045 __ CmpLeD(FTMP, rhs, lhs);
4046 }
4047 __ Mfc1(dst, FTMP);
4048 __ Andi(dst, dst, 1);
4049 break;
4050 default:
4051 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4052 UNREACHABLE();
4053 }
4054 }
4055}
4056
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004057bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4058 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004059 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004060 LocationSummary* input_locations,
4061 FpuRegister dst) {
4062 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4063 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004064 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004065 switch (cond) {
4066 case kCondEQ:
4067 __ CmpEqS(dst, lhs, rhs);
4068 return false;
4069 case kCondNE:
4070 __ CmpEqS(dst, lhs, rhs);
4071 return true;
4072 case kCondLT:
4073 if (gt_bias) {
4074 __ CmpLtS(dst, lhs, rhs);
4075 } else {
4076 __ CmpUltS(dst, lhs, rhs);
4077 }
4078 return false;
4079 case kCondLE:
4080 if (gt_bias) {
4081 __ CmpLeS(dst, lhs, rhs);
4082 } else {
4083 __ CmpUleS(dst, lhs, rhs);
4084 }
4085 return false;
4086 case kCondGT:
4087 if (gt_bias) {
4088 __ CmpUltS(dst, rhs, lhs);
4089 } else {
4090 __ CmpLtS(dst, rhs, lhs);
4091 }
4092 return false;
4093 case kCondGE:
4094 if (gt_bias) {
4095 __ CmpUleS(dst, rhs, lhs);
4096 } else {
4097 __ CmpLeS(dst, rhs, lhs);
4098 }
4099 return false;
4100 default:
4101 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4102 UNREACHABLE();
4103 }
4104 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004105 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004106 switch (cond) {
4107 case kCondEQ:
4108 __ CmpEqD(dst, lhs, rhs);
4109 return false;
4110 case kCondNE:
4111 __ CmpEqD(dst, lhs, rhs);
4112 return true;
4113 case kCondLT:
4114 if (gt_bias) {
4115 __ CmpLtD(dst, lhs, rhs);
4116 } else {
4117 __ CmpUltD(dst, lhs, rhs);
4118 }
4119 return false;
4120 case kCondLE:
4121 if (gt_bias) {
4122 __ CmpLeD(dst, lhs, rhs);
4123 } else {
4124 __ CmpUleD(dst, lhs, rhs);
4125 }
4126 return false;
4127 case kCondGT:
4128 if (gt_bias) {
4129 __ CmpUltD(dst, rhs, lhs);
4130 } else {
4131 __ CmpLtD(dst, rhs, lhs);
4132 }
4133 return false;
4134 case kCondGE:
4135 if (gt_bias) {
4136 __ CmpUleD(dst, rhs, lhs);
4137 } else {
4138 __ CmpLeD(dst, rhs, lhs);
4139 }
4140 return false;
4141 default:
4142 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4143 UNREACHABLE();
4144 }
4145 }
4146}
4147
Alexey Frunze299a9392015-12-08 16:08:02 -08004148void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4149 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004150 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004151 LocationSummary* locations,
4152 Mips64Label* label) {
4153 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4154 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004155 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004156 switch (cond) {
4157 case kCondEQ:
4158 __ CmpEqS(FTMP, lhs, rhs);
4159 __ Bc1nez(FTMP, label);
4160 break;
4161 case kCondNE:
4162 __ CmpEqS(FTMP, lhs, rhs);
4163 __ Bc1eqz(FTMP, label);
4164 break;
4165 case kCondLT:
4166 if (gt_bias) {
4167 __ CmpLtS(FTMP, lhs, rhs);
4168 } else {
4169 __ CmpUltS(FTMP, lhs, rhs);
4170 }
4171 __ Bc1nez(FTMP, label);
4172 break;
4173 case kCondLE:
4174 if (gt_bias) {
4175 __ CmpLeS(FTMP, lhs, rhs);
4176 } else {
4177 __ CmpUleS(FTMP, lhs, rhs);
4178 }
4179 __ Bc1nez(FTMP, label);
4180 break;
4181 case kCondGT:
4182 if (gt_bias) {
4183 __ CmpUltS(FTMP, rhs, lhs);
4184 } else {
4185 __ CmpLtS(FTMP, rhs, lhs);
4186 }
4187 __ Bc1nez(FTMP, label);
4188 break;
4189 case kCondGE:
4190 if (gt_bias) {
4191 __ CmpUleS(FTMP, rhs, lhs);
4192 } else {
4193 __ CmpLeS(FTMP, rhs, lhs);
4194 }
4195 __ Bc1nez(FTMP, label);
4196 break;
4197 default:
4198 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004199 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004200 }
4201 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004202 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004203 switch (cond) {
4204 case kCondEQ:
4205 __ CmpEqD(FTMP, lhs, rhs);
4206 __ Bc1nez(FTMP, label);
4207 break;
4208 case kCondNE:
4209 __ CmpEqD(FTMP, lhs, rhs);
4210 __ Bc1eqz(FTMP, label);
4211 break;
4212 case kCondLT:
4213 if (gt_bias) {
4214 __ CmpLtD(FTMP, lhs, rhs);
4215 } else {
4216 __ CmpUltD(FTMP, lhs, rhs);
4217 }
4218 __ Bc1nez(FTMP, label);
4219 break;
4220 case kCondLE:
4221 if (gt_bias) {
4222 __ CmpLeD(FTMP, lhs, rhs);
4223 } else {
4224 __ CmpUleD(FTMP, lhs, rhs);
4225 }
4226 __ Bc1nez(FTMP, label);
4227 break;
4228 case kCondGT:
4229 if (gt_bias) {
4230 __ CmpUltD(FTMP, rhs, lhs);
4231 } else {
4232 __ CmpLtD(FTMP, rhs, lhs);
4233 }
4234 __ Bc1nez(FTMP, label);
4235 break;
4236 case kCondGE:
4237 if (gt_bias) {
4238 __ CmpUleD(FTMP, rhs, lhs);
4239 } else {
4240 __ CmpLeD(FTMP, rhs, lhs);
4241 }
4242 __ Bc1nez(FTMP, label);
4243 break;
4244 default:
4245 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004246 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004247 }
4248 }
4249}
4250
Alexey Frunze4dda3372015-06-01 18:31:49 -07004251void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004252 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004253 Mips64Label* true_target,
4254 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004255 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004256
David Brazdil0debae72015-11-12 18:37:00 +00004257 if (true_target == nullptr && false_target == nullptr) {
4258 // Nothing to do. The code always falls through.
4259 return;
4260 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004261 // Constant condition, statically compared against "true" (integer value 1).
4262 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004263 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004264 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004265 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004266 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004267 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004268 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004269 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004270 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004271 }
David Brazdil0debae72015-11-12 18:37:00 +00004272 return;
4273 }
4274
4275 // The following code generates these patterns:
4276 // (1) true_target == nullptr && false_target != nullptr
4277 // - opposite condition true => branch to false_target
4278 // (2) true_target != nullptr && false_target == nullptr
4279 // - condition true => branch to true_target
4280 // (3) true_target != nullptr && false_target != nullptr
4281 // - condition true => branch to true_target
4282 // - branch to false_target
4283 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004284 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004285 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004286 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004287 if (true_target == nullptr) {
4288 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4289 } else {
4290 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4291 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004292 } else {
4293 // The condition instruction has not been materialized, use its inputs as
4294 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004295 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004296 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004297 LocationSummary* locations = cond->GetLocations();
4298 IfCondition if_cond = condition->GetCondition();
4299 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004300
David Brazdil0debae72015-11-12 18:37:00 +00004301 if (true_target == nullptr) {
4302 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004303 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004304 }
4305
Alexey Frunze299a9392015-12-08 16:08:02 -08004306 switch (type) {
4307 default:
4308 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4309 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004310 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004311 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4312 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004313 case DataType::Type::kFloat32:
4314 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004315 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4316 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004317 }
4318 }
David Brazdil0debae72015-11-12 18:37:00 +00004319
4320 // If neither branch falls through (case 3), the conditional branch to `true_target`
4321 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4322 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004323 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004324 }
4325}
4326
4327void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004328 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004329 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004330 locations->SetInAt(0, Location::RequiresRegister());
4331 }
4332}
4333
4334void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004335 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4336 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004337 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004338 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004339 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004340 nullptr : codegen_->GetLabelOf(false_successor);
4341 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004342}
4343
4344void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004345 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004346 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004347 InvokeRuntimeCallingConvention calling_convention;
4348 RegisterSet caller_saves = RegisterSet::Empty();
4349 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4350 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004351 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004352 locations->SetInAt(0, Location::RequiresRegister());
4353 }
4354}
4355
4356void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004357 SlowPathCodeMIPS64* slow_path =
4358 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004359 GenerateTestAndBranch(deoptimize,
4360 /* condition_input_index */ 0,
4361 slow_path->GetEntryLabel(),
4362 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004363}
4364
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004365// This function returns true if a conditional move can be generated for HSelect.
4366// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4367// branches and regular moves.
4368//
4369// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4370//
4371// While determining feasibility of a conditional move and setting inputs/outputs
4372// are two distinct tasks, this function does both because they share quite a bit
4373// of common logic.
4374static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4375 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4376 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4377 HCondition* condition = cond->AsCondition();
4378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004379 DataType::Type cond_type =
4380 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4381 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004382
4383 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4384 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4385 bool is_true_value_zero_constant =
4386 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4387 bool is_false_value_zero_constant =
4388 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4389
4390 bool can_move_conditionally = false;
4391 bool use_const_for_false_in = false;
4392 bool use_const_for_true_in = false;
4393
4394 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004395 if (!DataType::IsFloatingPointType(cond_type)) {
4396 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004397 // Moving int/long on int/long condition.
4398 if (is_true_value_zero_constant) {
4399 // seleqz out_reg, false_reg, cond_reg
4400 can_move_conditionally = true;
4401 use_const_for_true_in = true;
4402 } else if (is_false_value_zero_constant) {
4403 // selnez out_reg, true_reg, cond_reg
4404 can_move_conditionally = true;
4405 use_const_for_false_in = true;
4406 } else if (materialized) {
4407 // Not materializing unmaterialized int conditions
4408 // to keep the instruction count low.
4409 // selnez AT, true_reg, cond_reg
4410 // seleqz TMP, false_reg, cond_reg
4411 // or out_reg, AT, TMP
4412 can_move_conditionally = true;
4413 }
4414 } else {
4415 // Moving float/double on int/long condition.
4416 if (materialized) {
4417 // Not materializing unmaterialized int conditions
4418 // to keep the instruction count low.
4419 can_move_conditionally = true;
4420 if (is_true_value_zero_constant) {
4421 // sltu TMP, ZERO, cond_reg
4422 // mtc1 TMP, temp_cond_reg
4423 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4424 use_const_for_true_in = true;
4425 } else if (is_false_value_zero_constant) {
4426 // sltu TMP, ZERO, cond_reg
4427 // mtc1 TMP, temp_cond_reg
4428 // selnez.fmt out_reg, true_reg, temp_cond_reg
4429 use_const_for_false_in = true;
4430 } else {
4431 // sltu TMP, ZERO, cond_reg
4432 // mtc1 TMP, temp_cond_reg
4433 // sel.fmt temp_cond_reg, false_reg, true_reg
4434 // mov.fmt out_reg, temp_cond_reg
4435 }
4436 }
4437 }
4438 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004439 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004440 // Moving int/long on float/double condition.
4441 can_move_conditionally = true;
4442 if (is_true_value_zero_constant) {
4443 // mfc1 TMP, temp_cond_reg
4444 // seleqz out_reg, false_reg, TMP
4445 use_const_for_true_in = true;
4446 } else if (is_false_value_zero_constant) {
4447 // mfc1 TMP, temp_cond_reg
4448 // selnez out_reg, true_reg, TMP
4449 use_const_for_false_in = true;
4450 } else {
4451 // mfc1 TMP, temp_cond_reg
4452 // selnez AT, true_reg, TMP
4453 // seleqz TMP, false_reg, TMP
4454 // or out_reg, AT, TMP
4455 }
4456 } else {
4457 // Moving float/double on float/double condition.
4458 can_move_conditionally = true;
4459 if (is_true_value_zero_constant) {
4460 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4461 use_const_for_true_in = true;
4462 } else if (is_false_value_zero_constant) {
4463 // selnez.fmt out_reg, true_reg, temp_cond_reg
4464 use_const_for_false_in = true;
4465 } else {
4466 // sel.fmt temp_cond_reg, false_reg, true_reg
4467 // mov.fmt out_reg, temp_cond_reg
4468 }
4469 }
4470 }
4471 }
4472
4473 if (can_move_conditionally) {
4474 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4475 } else {
4476 DCHECK(!use_const_for_false_in);
4477 DCHECK(!use_const_for_true_in);
4478 }
4479
4480 if (locations_to_set != nullptr) {
4481 if (use_const_for_false_in) {
4482 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4483 } else {
4484 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004485 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004486 ? Location::RequiresFpuRegister()
4487 : Location::RequiresRegister());
4488 }
4489 if (use_const_for_true_in) {
4490 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4491 } else {
4492 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004493 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004494 ? Location::RequiresFpuRegister()
4495 : Location::RequiresRegister());
4496 }
4497 if (materialized) {
4498 locations_to_set->SetInAt(2, Location::RequiresRegister());
4499 }
4500
4501 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004502 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004503 ? Location::RequiresFpuRegister()
4504 : Location::RequiresRegister());
4505 } else {
4506 locations_to_set->SetOut(Location::SameAsFirstInput());
4507 }
4508 }
4509
4510 return can_move_conditionally;
4511}
4512
4513
4514void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4515 LocationSummary* locations = select->GetLocations();
4516 Location dst = locations->Out();
4517 Location false_src = locations->InAt(0);
4518 Location true_src = locations->InAt(1);
4519 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4520 GpuRegister cond_reg = TMP;
4521 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004522 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004523 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004524 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004525
4526 if (IsBooleanValueOrMaterializedCondition(cond)) {
4527 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4528 } else {
4529 HCondition* condition = cond->AsCondition();
4530 LocationSummary* cond_locations = cond->GetLocations();
4531 IfCondition if_cond = condition->GetCondition();
4532 cond_type = condition->InputAt(0)->GetType();
4533 switch (cond_type) {
4534 default:
4535 cond_inverted = MaterializeIntLongCompare(if_cond,
4536 /* is64bit */ false,
4537 cond_locations,
4538 cond_reg);
4539 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004540 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004541 cond_inverted = MaterializeIntLongCompare(if_cond,
4542 /* is64bit */ true,
4543 cond_locations,
4544 cond_reg);
4545 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004546 case DataType::Type::kFloat32:
4547 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004548 cond_inverted = MaterializeFpCompare(if_cond,
4549 condition->IsGtBias(),
4550 cond_type,
4551 cond_locations,
4552 fcond_reg);
4553 break;
4554 }
4555 }
4556
4557 if (true_src.IsConstant()) {
4558 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4559 }
4560 if (false_src.IsConstant()) {
4561 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4562 }
4563
4564 switch (dst_type) {
4565 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004566 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004567 __ Mfc1(cond_reg, fcond_reg);
4568 }
4569 if (true_src.IsConstant()) {
4570 if (cond_inverted) {
4571 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4572 } else {
4573 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4574 }
4575 } else if (false_src.IsConstant()) {
4576 if (cond_inverted) {
4577 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4578 } else {
4579 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4580 }
4581 } else {
4582 DCHECK_NE(cond_reg, AT);
4583 if (cond_inverted) {
4584 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4585 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4586 } else {
4587 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4588 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4589 }
4590 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4591 }
4592 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004593 case DataType::Type::kFloat32: {
4594 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004595 // sel*.fmt tests bit 0 of the condition register, account for that.
4596 __ Sltu(TMP, ZERO, cond_reg);
4597 __ Mtc1(TMP, fcond_reg);
4598 }
4599 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4600 if (true_src.IsConstant()) {
4601 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4602 if (cond_inverted) {
4603 __ SelnezS(dst_reg, src_reg, fcond_reg);
4604 } else {
4605 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4606 }
4607 } else if (false_src.IsConstant()) {
4608 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4609 if (cond_inverted) {
4610 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4611 } else {
4612 __ SelnezS(dst_reg, src_reg, fcond_reg);
4613 }
4614 } else {
4615 if (cond_inverted) {
4616 __ SelS(fcond_reg,
4617 true_src.AsFpuRegister<FpuRegister>(),
4618 false_src.AsFpuRegister<FpuRegister>());
4619 } else {
4620 __ SelS(fcond_reg,
4621 false_src.AsFpuRegister<FpuRegister>(),
4622 true_src.AsFpuRegister<FpuRegister>());
4623 }
4624 __ MovS(dst_reg, fcond_reg);
4625 }
4626 break;
4627 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004628 case DataType::Type::kFloat64: {
4629 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004630 // sel*.fmt tests bit 0 of the condition register, account for that.
4631 __ Sltu(TMP, ZERO, cond_reg);
4632 __ Mtc1(TMP, fcond_reg);
4633 }
4634 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4635 if (true_src.IsConstant()) {
4636 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4637 if (cond_inverted) {
4638 __ SelnezD(dst_reg, src_reg, fcond_reg);
4639 } else {
4640 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4641 }
4642 } else if (false_src.IsConstant()) {
4643 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4644 if (cond_inverted) {
4645 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4646 } else {
4647 __ SelnezD(dst_reg, src_reg, fcond_reg);
4648 }
4649 } else {
4650 if (cond_inverted) {
4651 __ SelD(fcond_reg,
4652 true_src.AsFpuRegister<FpuRegister>(),
4653 false_src.AsFpuRegister<FpuRegister>());
4654 } else {
4655 __ SelD(fcond_reg,
4656 false_src.AsFpuRegister<FpuRegister>(),
4657 true_src.AsFpuRegister<FpuRegister>());
4658 }
4659 __ MovD(dst_reg, fcond_reg);
4660 }
4661 break;
4662 }
4663 }
4664}
4665
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004666void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004667 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004668 LocationSummary(flag, LocationSummary::kNoCall);
4669 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004670}
4671
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004672void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4673 __ LoadFromOffset(kLoadWord,
4674 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4675 SP,
4676 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004677}
4678
David Brazdil74eb1b22015-12-14 11:44:01 +00004679void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004680 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004681 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004682}
4683
4684void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004685 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4686 GenConditionalMove(select);
4687 } else {
4688 LocationSummary* locations = select->GetLocations();
4689 Mips64Label false_target;
4690 GenerateTestAndBranch(select,
4691 /* condition_input_index */ 2,
4692 /* true_target */ nullptr,
4693 &false_target);
4694 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4695 __ Bind(&false_target);
4696 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004697}
4698
David Srbecky0cf44932015-12-09 14:09:59 +00004699void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004700 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004701}
4702
David Srbeckyd28f4a02016-03-14 17:14:24 +00004703void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4704 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004705}
4706
4707void CodeGeneratorMIPS64::GenerateNop() {
4708 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004709}
4710
Alexey Frunze4dda3372015-06-01 18:31:49 -07004711void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004712 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004713 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004714 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004715 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004716 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004717 instruction,
4718 object_field_get_with_read_barrier
4719 ? LocationSummary::kCallOnSlowPath
4720 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004721 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4722 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4723 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004724 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004726 locations->SetOut(Location::RequiresFpuRegister());
4727 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004728 // The output overlaps in the case of an object field get with
4729 // read barriers enabled: we do not want the move to overwrite the
4730 // object's location, as we need it to emit the read barrier.
4731 locations->SetOut(Location::RequiresRegister(),
4732 object_field_get_with_read_barrier
4733 ? Location::kOutputOverlap
4734 : Location::kNoOutputOverlap);
4735 }
4736 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4737 // We need a temporary register for the read barrier marking slow
4738 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004739 if (!kBakerReadBarrierThunksEnableForFields) {
4740 locations->AddTemp(Location::RequiresRegister());
4741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004742 }
4743}
4744
4745void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4746 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004747 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4748 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004749 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004750 Location obj_loc = locations->InAt(0);
4751 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4752 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004753 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004754 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004755 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004756 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4757
Alexey Frunze4dda3372015-06-01 18:31:49 -07004758 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004759 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004760 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004761 load_type = kLoadUnsignedByte;
4762 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004763 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004764 load_type = kLoadSignedByte;
4765 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004766 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004767 load_type = kLoadUnsignedHalfword;
4768 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004769 case DataType::Type::kInt16:
4770 load_type = kLoadSignedHalfword;
4771 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004772 case DataType::Type::kInt32:
4773 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004774 load_type = kLoadWord;
4775 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004776 case DataType::Type::kInt64:
4777 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004778 load_type = kLoadDoubleword;
4779 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004781 load_type = kLoadUnsignedWord;
4782 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004783 case DataType::Type::kUint32:
4784 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004785 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004786 LOG(FATAL) << "Unreachable type " << type;
4787 UNREACHABLE();
4788 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004790 DCHECK(dst_loc.IsRegister());
4791 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004793 // /* HeapReference<Object> */ dst = *(obj + offset)
4794 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004795 Location temp_loc =
4796 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004797 // Note that a potential implicit null check is handled in this
4798 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4799 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4800 dst_loc,
4801 obj,
4802 offset,
4803 temp_loc,
4804 /* needs_null_check */ true);
4805 if (is_volatile) {
4806 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4807 }
4808 } else {
4809 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4810 if (is_volatile) {
4811 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4812 }
4813 // If read barriers are enabled, emit read barriers other than
4814 // Baker's using a slow path (and also unpoison the loaded
4815 // reference, if heap poisoning is enabled).
4816 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4817 }
4818 } else {
4819 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4820 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004821 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004822 DCHECK(dst_loc.IsFpuRegister());
4823 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004824 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004825 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004826
Alexey Frunze15958152017-02-09 19:08:30 -08004827 // Memory barriers, in the case of references, are handled in the
4828 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004829 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004830 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004831 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004832}
4833
4834void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4835 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4836 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004837 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004839 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004840 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004841 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004842 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004843 }
4844}
4845
4846void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004847 const FieldInfo& field_info,
4848 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004849 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004850 LocationSummary* locations = instruction->GetLocations();
4851 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004852 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004853 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004854 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004855 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4856 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004857 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4858
Alexey Frunze4dda3372015-06-01 18:31:49 -07004859 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004861 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004862 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004863 store_type = kStoreByte;
4864 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004866 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004867 store_type = kStoreHalfword;
4868 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 case DataType::Type::kInt32:
4870 case DataType::Type::kFloat32:
4871 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004872 store_type = kStoreWord;
4873 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004874 case DataType::Type::kInt64:
4875 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004876 store_type = kStoreDoubleword;
4877 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004878 case DataType::Type::kUint32:
4879 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004880 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004881 LOG(FATAL) << "Unreachable type " << type;
4882 UNREACHABLE();
4883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004884
Alexey Frunze15958152017-02-09 19:08:30 -08004885 if (is_volatile) {
4886 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4887 }
4888
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004889 if (value_location.IsConstant()) {
4890 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4891 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4892 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004893 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004894 DCHECK(value_location.IsRegister());
4895 GpuRegister src = value_location.AsRegister<GpuRegister>();
4896 if (kPoisonHeapReferences && needs_write_barrier) {
4897 // Note that in the case where `value` is a null reference,
4898 // we do not enter this block, as a null reference does not
4899 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004900 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004901 __ PoisonHeapReference(TMP, src);
4902 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4903 } else {
4904 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4905 }
4906 } else {
4907 DCHECK(value_location.IsFpuRegister());
4908 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4909 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4910 }
4911 }
Alexey Frunze15958152017-02-09 19:08:30 -08004912
Alexey Frunzec061de12017-02-14 13:27:23 -08004913 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004914 DCHECK(value_location.IsRegister());
4915 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004916 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004917 }
Alexey Frunze15958152017-02-09 19:08:30 -08004918
4919 if (is_volatile) {
4920 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4921 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004922}
4923
4924void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4925 HandleFieldGet(instruction, instruction->GetFieldInfo());
4926}
4927
4928void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4929 HandleFieldGet(instruction, instruction->GetFieldInfo());
4930}
4931
4932void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4933 HandleFieldSet(instruction, instruction->GetFieldInfo());
4934}
4935
4936void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004937 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004938}
4939
Alexey Frunze15958152017-02-09 19:08:30 -08004940void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4941 HInstruction* instruction,
4942 Location out,
4943 uint32_t offset,
4944 Location maybe_temp,
4945 ReadBarrierOption read_barrier_option) {
4946 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4947 if (read_barrier_option == kWithReadBarrier) {
4948 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004949 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4950 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4951 }
Alexey Frunze15958152017-02-09 19:08:30 -08004952 if (kUseBakerReadBarrier) {
4953 // Load with fast path based Baker's read barrier.
4954 // /* HeapReference<Object> */ out = *(out + offset)
4955 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4956 out,
4957 out_reg,
4958 offset,
4959 maybe_temp,
4960 /* needs_null_check */ false);
4961 } else {
4962 // Load with slow path based read barrier.
4963 // Save the value of `out` into `maybe_temp` before overwriting it
4964 // in the following move operation, as we will need it for the
4965 // read barrier below.
4966 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4967 // /* HeapReference<Object> */ out = *(out + offset)
4968 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4969 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4970 }
4971 } else {
4972 // Plain load with no read barrier.
4973 // /* HeapReference<Object> */ out = *(out + offset)
4974 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4975 __ MaybeUnpoisonHeapReference(out_reg);
4976 }
4977}
4978
4979void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4980 HInstruction* instruction,
4981 Location out,
4982 Location obj,
4983 uint32_t offset,
4984 Location maybe_temp,
4985 ReadBarrierOption read_barrier_option) {
4986 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4987 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4988 if (read_barrier_option == kWithReadBarrier) {
4989 CHECK(kEmitCompilerReadBarrier);
4990 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004991 if (!kBakerReadBarrierThunksEnableForFields) {
4992 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4993 }
Alexey Frunze15958152017-02-09 19:08:30 -08004994 // Load with fast path based Baker's read barrier.
4995 // /* HeapReference<Object> */ out = *(obj + offset)
4996 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4997 out,
4998 obj_reg,
4999 offset,
5000 maybe_temp,
5001 /* needs_null_check */ false);
5002 } else {
5003 // Load with slow path based read barrier.
5004 // /* HeapReference<Object> */ out = *(obj + offset)
5005 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5006 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5007 }
5008 } else {
5009 // Plain load with no read barrier.
5010 // /* HeapReference<Object> */ out = *(obj + offset)
5011 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5012 __ MaybeUnpoisonHeapReference(out_reg);
5013 }
5014}
5015
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005016static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5017 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5018 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5019 return reg - V0;
5020 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5021 return 13 + (reg - S2);
5022 } else if (reg == S8) { // One more.
5023 return 19;
5024 }
5025 LOG(FATAL) << "Unexpected register " << reg;
5026 UNREACHABLE();
5027}
5028
5029static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5030 int num = GetBakerMarkThunkNumber(reg) +
5031 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5032 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5033}
5034
5035static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5036 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5037 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5038}
5039
5040void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5041 Location root,
5042 GpuRegister obj,
5043 uint32_t offset,
5044 ReadBarrierOption read_barrier_option,
5045 Mips64Label* label_low) {
5046 if (label_low != nullptr) {
5047 DCHECK_EQ(offset, 0x5678u);
5048 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005049 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005050 if (read_barrier_option == kWithReadBarrier) {
5051 DCHECK(kEmitCompilerReadBarrier);
5052 if (kUseBakerReadBarrier) {
5053 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5054 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005055 if (kBakerReadBarrierThunksEnableForGcRoots) {
5056 // Note that we do not actually check the value of `GetIsGcMarking()`
5057 // to decide whether to mark the loaded GC root or not. Instead, we
5058 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5059 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5060 // vice versa.
5061 //
5062 // We use thunks for the slow path. That thunk checks the reference
5063 // and jumps to the entrypoint if needed.
5064 //
5065 // temp = Thread::Current()->pReadBarrierMarkReg00
5066 // // AKA &art_quick_read_barrier_mark_introspection.
5067 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5068 // if (temp != nullptr) {
5069 // temp = &gc_root_thunk<root_reg>
5070 // root = temp(root)
5071 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005072
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005073 const int32_t entry_point_offset =
5074 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5075 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5076 int16_t offset_low = Low16Bits(offset);
5077 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5078 // extension in lwu.
5079 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5080 GpuRegister base = short_offset ? obj : TMP;
5081 // Loading the entrypoint does not require a load acquire since it is only changed when
5082 // threads are suspended or running a checkpoint.
5083 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5084 if (!short_offset) {
5085 DCHECK(!label_low);
5086 __ Daui(base, obj, offset_high);
5087 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005088 Mips64Label skip_call;
5089 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005090 if (label_low != nullptr) {
5091 DCHECK(short_offset);
5092 __ Bind(label_low);
5093 }
5094 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5095 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5096 // in delay slot.
5097 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005098 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005099 } else {
5100 // Note that we do not actually check the value of `GetIsGcMarking()`
5101 // to decide whether to mark the loaded GC root or not. Instead, we
5102 // load into `temp` (T9) the read barrier mark entry point corresponding
5103 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5104 // is false, and vice versa.
5105 //
5106 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5107 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5108 // if (temp != null) {
5109 // root = temp(root)
5110 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005111
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005112 if (label_low != nullptr) {
5113 __ Bind(label_low);
5114 }
5115 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5116 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5117 static_assert(
5118 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5119 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5120 "have different sizes.");
5121 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5122 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5123 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005124
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005125 // Slow path marking the GC root `root`.
5126 Location temp = Location::RegisterLocation(T9);
5127 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005128 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005129 instruction,
5130 root,
5131 /*entrypoint*/ temp);
5132 codegen_->AddSlowPath(slow_path);
5133
5134 const int32_t entry_point_offset =
5135 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5136 // Loading the entrypoint does not require a load acquire since it is only changed when
5137 // threads are suspended or running a checkpoint.
5138 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5139 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5140 __ Bind(slow_path->GetExitLabel());
5141 }
Alexey Frunze15958152017-02-09 19:08:30 -08005142 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005143 if (label_low != nullptr) {
5144 __ Bind(label_low);
5145 }
Alexey Frunze15958152017-02-09 19:08:30 -08005146 // GC root loaded through a slow path for read barriers other
5147 // than Baker's.
5148 // /* GcRoot<mirror::Object>* */ root = obj + offset
5149 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5150 // /* mirror::Object* */ root = root->Read()
5151 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5152 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005153 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005154 if (label_low != nullptr) {
5155 __ Bind(label_low);
5156 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005157 // Plain GC root load with no read barrier.
5158 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5159 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5160 // Note that GC roots are not affected by heap poisoning, thus we
5161 // do not have to unpoison `root_reg` here.
5162 }
5163}
5164
Alexey Frunze15958152017-02-09 19:08:30 -08005165void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5166 Location ref,
5167 GpuRegister obj,
5168 uint32_t offset,
5169 Location temp,
5170 bool needs_null_check) {
5171 DCHECK(kEmitCompilerReadBarrier);
5172 DCHECK(kUseBakerReadBarrier);
5173
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005174 if (kBakerReadBarrierThunksEnableForFields) {
5175 // Note that we do not actually check the value of `GetIsGcMarking()`
5176 // to decide whether to mark the loaded reference or not. Instead, we
5177 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5178 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5179 // vice versa.
5180 //
5181 // We use thunks for the slow path. That thunk checks the reference
5182 // and jumps to the entrypoint if needed. If the holder is not gray,
5183 // it issues a load-load memory barrier and returns to the original
5184 // reference load.
5185 //
5186 // temp = Thread::Current()->pReadBarrierMarkReg00
5187 // // AKA &art_quick_read_barrier_mark_introspection.
5188 // if (temp != nullptr) {
5189 // temp = &field_array_thunk<holder_reg>
5190 // temp()
5191 // }
5192 // not_gray_return_address:
5193 // // If the offset is too large to fit into the lw instruction, we
5194 // // use an adjusted base register (TMP) here. This register
5195 // // receives bits 16 ... 31 of the offset before the thunk invocation
5196 // // and the thunk benefits from it.
5197 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5198 // gray_return_address:
5199
5200 DCHECK(temp.IsInvalid());
5201 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5202 const int32_t entry_point_offset =
5203 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5204 // There may have or may have not been a null check if the field offset is smaller than
5205 // the page size.
5206 // There must've been a null check in case it's actually a load from an array.
5207 // We will, however, perform an explicit null check in the thunk as it's easier to
5208 // do it than not.
5209 if (instruction->IsArrayGet()) {
5210 DCHECK(!needs_null_check);
5211 }
5212 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5213 // Loading the entrypoint does not require a load acquire since it is only changed when
5214 // threads are suspended or running a checkpoint.
5215 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5216 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005217 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005218 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005219 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005220 __ Nop(); // In forbidden slot.
5221 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005222 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005223 // /* HeapReference<Object> */ ref = *(obj + offset)
5224 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5225 } else {
5226 int16_t offset_low = Low16Bits(offset);
5227 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005228 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005229 __ Daui(TMP, obj, offset_high); // In delay slot.
5230 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005231 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005232 // /* HeapReference<Object> */ ref = *(obj + offset)
5233 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5234 }
5235 if (needs_null_check) {
5236 MaybeRecordImplicitNullCheck(instruction);
5237 }
5238 __ MaybeUnpoisonHeapReference(ref_reg);
5239 return;
5240 }
5241
Alexey Frunze15958152017-02-09 19:08:30 -08005242 // /* HeapReference<Object> */ ref = *(obj + offset)
5243 Location no_index = Location::NoLocation();
5244 ScaleFactor no_scale_factor = TIMES_1;
5245 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5246 ref,
5247 obj,
5248 offset,
5249 no_index,
5250 no_scale_factor,
5251 temp,
5252 needs_null_check);
5253}
5254
5255void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5256 Location ref,
5257 GpuRegister obj,
5258 uint32_t data_offset,
5259 Location index,
5260 Location temp,
5261 bool needs_null_check) {
5262 DCHECK(kEmitCompilerReadBarrier);
5263 DCHECK(kUseBakerReadBarrier);
5264
5265 static_assert(
5266 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5267 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005268 ScaleFactor scale_factor = TIMES_4;
5269
5270 if (kBakerReadBarrierThunksEnableForArrays) {
5271 // Note that we do not actually check the value of `GetIsGcMarking()`
5272 // to decide whether to mark the loaded reference or not. Instead, we
5273 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5274 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5275 // vice versa.
5276 //
5277 // We use thunks for the slow path. That thunk checks the reference
5278 // and jumps to the entrypoint if needed. If the holder is not gray,
5279 // it issues a load-load memory barrier and returns to the original
5280 // reference load.
5281 //
5282 // temp = Thread::Current()->pReadBarrierMarkReg00
5283 // // AKA &art_quick_read_barrier_mark_introspection.
5284 // if (temp != nullptr) {
5285 // temp = &field_array_thunk<holder_reg>
5286 // temp()
5287 // }
5288 // not_gray_return_address:
5289 // // The element address is pre-calculated in the TMP register before the
5290 // // thunk invocation and the thunk benefits from it.
5291 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5292 // gray_return_address:
5293
5294 DCHECK(temp.IsInvalid());
5295 DCHECK(index.IsValid());
5296 const int32_t entry_point_offset =
5297 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5298 // We will not do the explicit null check in the thunk as some form of a null check
5299 // must've been done earlier.
5300 DCHECK(!needs_null_check);
5301 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5302 // Loading the entrypoint does not require a load acquire since it is only changed when
5303 // threads are suspended or running a checkpoint.
5304 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005305 Mips64Label skip_call;
5306 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005307 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5308 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5309 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5310 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005311 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005312 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5313 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5314 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5315 __ MaybeUnpoisonHeapReference(ref_reg);
5316 return;
5317 }
5318
Alexey Frunze15958152017-02-09 19:08:30 -08005319 // /* HeapReference<Object> */ ref =
5320 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005321 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5322 ref,
5323 obj,
5324 data_offset,
5325 index,
5326 scale_factor,
5327 temp,
5328 needs_null_check);
5329}
5330
5331void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5332 Location ref,
5333 GpuRegister obj,
5334 uint32_t offset,
5335 Location index,
5336 ScaleFactor scale_factor,
5337 Location temp,
5338 bool needs_null_check,
5339 bool always_update_field) {
5340 DCHECK(kEmitCompilerReadBarrier);
5341 DCHECK(kUseBakerReadBarrier);
5342
5343 // In slow path based read barriers, the read barrier call is
5344 // inserted after the original load. However, in fast path based
5345 // Baker's read barriers, we need to perform the load of
5346 // mirror::Object::monitor_ *before* the original reference load.
5347 // This load-load ordering is required by the read barrier.
5348 // The fast path/slow path (for Baker's algorithm) should look like:
5349 //
5350 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5351 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5352 // HeapReference<Object> ref = *src; // Original reference load.
5353 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5354 // if (is_gray) {
5355 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5356 // }
5357 //
5358 // Note: the original implementation in ReadBarrier::Barrier is
5359 // slightly more complex as it performs additional checks that we do
5360 // not do here for performance reasons.
5361
5362 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5363 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5364 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5365
5366 // /* int32_t */ monitor = obj->monitor_
5367 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5368 if (needs_null_check) {
5369 MaybeRecordImplicitNullCheck(instruction);
5370 }
5371 // /* LockWord */ lock_word = LockWord(monitor)
5372 static_assert(sizeof(LockWord) == sizeof(int32_t),
5373 "art::LockWord and int32_t have different sizes.");
5374
5375 __ Sync(0); // Barrier to prevent load-load reordering.
5376
5377 // The actual reference load.
5378 if (index.IsValid()) {
5379 // Load types involving an "index": ArrayGet,
5380 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5381 // intrinsics.
5382 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5383 if (index.IsConstant()) {
5384 size_t computed_offset =
5385 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5386 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5387 } else {
5388 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005389 if (scale_factor == TIMES_1) {
5390 __ Daddu(TMP, index_reg, obj);
5391 } else {
5392 __ Dlsa(TMP, index_reg, obj, scale_factor);
5393 }
Alexey Frunze15958152017-02-09 19:08:30 -08005394 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5395 }
5396 } else {
5397 // /* HeapReference<Object> */ ref = *(obj + offset)
5398 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5399 }
5400
5401 // Object* ref = ref_addr->AsMirrorPtr()
5402 __ MaybeUnpoisonHeapReference(ref_reg);
5403
5404 // Slow path marking the object `ref` when it is gray.
5405 SlowPathCodeMIPS64* slow_path;
5406 if (always_update_field) {
5407 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5408 // of the form `obj + field_offset`, where `obj` is a register and
5409 // `field_offset` is a register. Thus `offset` and `scale_factor`
5410 // above are expected to be null in this code path.
5411 DCHECK_EQ(offset, 0u);
5412 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005413 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005414 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5415 ref,
5416 obj,
5417 /* field_offset */ index,
5418 temp_reg);
5419 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005420 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005421 }
5422 AddSlowPath(slow_path);
5423
5424 // if (rb_state == ReadBarrier::GrayState())
5425 // ref = ReadBarrier::Mark(ref);
5426 // Given the numeric representation, it's enough to check the low bit of the
5427 // rb_state. We do that by shifting the bit into the sign bit (31) and
5428 // performing a branch on less than zero.
5429 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5430 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5431 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5432 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5433 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5434 __ Bind(slow_path->GetExitLabel());
5435}
5436
5437void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5438 Location out,
5439 Location ref,
5440 Location obj,
5441 uint32_t offset,
5442 Location index) {
5443 DCHECK(kEmitCompilerReadBarrier);
5444
5445 // Insert a slow path based read barrier *after* the reference load.
5446 //
5447 // If heap poisoning is enabled, the unpoisoning of the loaded
5448 // reference will be carried out by the runtime within the slow
5449 // path.
5450 //
5451 // Note that `ref` currently does not get unpoisoned (when heap
5452 // poisoning is enabled), which is alright as the `ref` argument is
5453 // not used by the artReadBarrierSlow entry point.
5454 //
5455 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005456 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005457 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5458 AddSlowPath(slow_path);
5459
5460 __ Bc(slow_path->GetEntryLabel());
5461 __ Bind(slow_path->GetExitLabel());
5462}
5463
5464void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5465 Location out,
5466 Location ref,
5467 Location obj,
5468 uint32_t offset,
5469 Location index) {
5470 if (kEmitCompilerReadBarrier) {
5471 // Baker's read barriers shall be handled by the fast path
5472 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5473 DCHECK(!kUseBakerReadBarrier);
5474 // If heap poisoning is enabled, unpoisoning will be taken care of
5475 // by the runtime within the slow path.
5476 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5477 } else if (kPoisonHeapReferences) {
5478 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5479 }
5480}
5481
5482void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5483 Location out,
5484 Location root) {
5485 DCHECK(kEmitCompilerReadBarrier);
5486
5487 // Insert a slow path based read barrier *after* the GC root load.
5488 //
5489 // Note that GC roots are not affected by heap poisoning, so we do
5490 // not need to do anything special for this here.
5491 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005492 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005493 AddSlowPath(slow_path);
5494
5495 __ Bc(slow_path->GetEntryLabel());
5496 __ Bind(slow_path->GetExitLabel());
5497}
5498
Alexey Frunze4dda3372015-06-01 18:31:49 -07005499void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005500 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5501 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005502 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005503 switch (type_check_kind) {
5504 case TypeCheckKind::kExactCheck:
5505 case TypeCheckKind::kAbstractClassCheck:
5506 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005507 case TypeCheckKind::kArrayObjectCheck: {
5508 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5509 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5510 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005511 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005512 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005513 case TypeCheckKind::kArrayCheck:
5514 case TypeCheckKind::kUnresolvedCheck:
5515 case TypeCheckKind::kInterfaceCheck:
5516 call_kind = LocationSummary::kCallOnSlowPath;
5517 break;
5518 }
5519
Vladimir Markoca6fff82017-10-03 14:49:14 +01005520 LocationSummary* locations =
5521 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005522 if (baker_read_barrier_slow_path) {
5523 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5524 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005525 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005526 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005527 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005528 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005529 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005530 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005531}
5532
5533void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005534 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005535 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005536 Location obj_loc = locations->InAt(0);
5537 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005538 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005539 Location out_loc = locations->Out();
5540 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5541 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5542 DCHECK_LE(num_temps, 1u);
5543 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005544 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5545 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5546 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5547 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005548 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005549 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005550
5551 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005552 // Avoid this check if we know `obj` is not null.
5553 if (instruction->MustDoNullCheck()) {
5554 __ Move(out, ZERO);
5555 __ Beqzc(obj, &done);
5556 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005557
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005558 switch (type_check_kind) {
5559 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005560 ReadBarrierOption read_barrier_option =
5561 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005562 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005563 GenerateReferenceLoadTwoRegisters(instruction,
5564 out_loc,
5565 obj_loc,
5566 class_offset,
5567 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005568 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005569 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005570 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005571 __ Sltiu(out, out, 1);
5572 break;
5573 }
5574
5575 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005576 ReadBarrierOption read_barrier_option =
5577 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005578 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005579 GenerateReferenceLoadTwoRegisters(instruction,
5580 out_loc,
5581 obj_loc,
5582 class_offset,
5583 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005584 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005585 // If the class is abstract, we eagerly fetch the super class of the
5586 // object to avoid doing a comparison we know will fail.
5587 Mips64Label loop;
5588 __ Bind(&loop);
5589 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005590 GenerateReferenceLoadOneRegister(instruction,
5591 out_loc,
5592 super_offset,
5593 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005594 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005595 // If `out` is null, we use it for the result, and jump to `done`.
5596 __ Beqzc(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005597 __ Bnec(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005598 __ LoadConst32(out, 1);
5599 break;
5600 }
5601
5602 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005603 ReadBarrierOption read_barrier_option =
5604 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005605 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005606 GenerateReferenceLoadTwoRegisters(instruction,
5607 out_loc,
5608 obj_loc,
5609 class_offset,
5610 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005611 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005612 // Walk over the class hierarchy to find a match.
5613 Mips64Label loop, success;
5614 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005615 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005616 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005617 GenerateReferenceLoadOneRegister(instruction,
5618 out_loc,
5619 super_offset,
5620 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005621 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005622 __ Bnezc(out, &loop);
5623 // If `out` is null, we use it for the result, and jump to `done`.
5624 __ Bc(&done);
5625 __ Bind(&success);
5626 __ LoadConst32(out, 1);
5627 break;
5628 }
5629
5630 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005631 ReadBarrierOption read_barrier_option =
5632 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005633 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005634 GenerateReferenceLoadTwoRegisters(instruction,
5635 out_loc,
5636 obj_loc,
5637 class_offset,
5638 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005639 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005640 // Do an exact check.
5641 Mips64Label success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005642 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005643 // Otherwise, we need to check that the object's class is a non-primitive array.
5644 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005645 GenerateReferenceLoadOneRegister(instruction,
5646 out_loc,
5647 component_offset,
5648 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005649 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005650 // If `out` is null, we use it for the result, and jump to `done`.
5651 __ Beqzc(out, &done);
5652 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5653 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5654 __ Sltiu(out, out, 1);
5655 __ Bc(&done);
5656 __ Bind(&success);
5657 __ LoadConst32(out, 1);
5658 break;
5659 }
5660
5661 case TypeCheckKind::kArrayCheck: {
5662 // No read barrier since the slow path will retry upon failure.
5663 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005664 GenerateReferenceLoadTwoRegisters(instruction,
5665 out_loc,
5666 obj_loc,
5667 class_offset,
5668 maybe_temp_loc,
5669 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005670 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005671 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5672 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005673 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005674 __ Bnec(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005675 __ LoadConst32(out, 1);
5676 break;
5677 }
5678
5679 case TypeCheckKind::kUnresolvedCheck:
5680 case TypeCheckKind::kInterfaceCheck: {
5681 // Note that we indeed only call on slow path, but we always go
5682 // into the slow path for the unresolved and interface check
5683 // cases.
5684 //
5685 // We cannot directly call the InstanceofNonTrivial runtime
5686 // entry point without resorting to a type checking slow path
5687 // here (i.e. by calling InvokeRuntime directly), as it would
5688 // require to assign fixed registers for the inputs of this
5689 // HInstanceOf instruction (following the runtime calling
5690 // convention), which might be cluttered by the potential first
5691 // read barrier emission at the beginning of this method.
5692 //
5693 // TODO: Introduce a new runtime entry point taking the object
5694 // to test (instead of its class) as argument, and let it deal
5695 // with the read barrier issues. This will let us refactor this
5696 // case of the `switch` code as it was previously (with a direct
5697 // call to the runtime not using a type checking slow path).
5698 // This should also be beneficial for the other cases above.
5699 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005700 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5701 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005702 codegen_->AddSlowPath(slow_path);
5703 __ Bc(slow_path->GetEntryLabel());
5704 break;
5705 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005706 }
5707
5708 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005709
5710 if (slow_path != nullptr) {
5711 __ Bind(slow_path->GetExitLabel());
5712 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005713}
5714
5715void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005716 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005717 locations->SetOut(Location::ConstantLocation(constant));
5718}
5719
5720void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5721 // Will be generated at use site.
5722}
5723
5724void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005725 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005726 locations->SetOut(Location::ConstantLocation(constant));
5727}
5728
5729void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5730 // Will be generated at use site.
5731}
5732
Calin Juravle175dc732015-08-25 15:42:32 +01005733void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5734 // The trampoline uses the same calling convention as dex calling conventions,
5735 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5736 // the method_idx.
5737 HandleInvoke(invoke);
5738}
5739
5740void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5741 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5742}
5743
Alexey Frunze4dda3372015-06-01 18:31:49 -07005744void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5745 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5746 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5747}
5748
5749void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5750 HandleInvoke(invoke);
5751 // The register T0 is required to be used for the hidden argument in
5752 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5753 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5754}
5755
5756void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5757 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5758 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005759 Location receiver = invoke->GetLocations()->InAt(0);
5760 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005761 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005762
5763 // Set the hidden argument.
5764 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5765 invoke->GetDexMethodIndex());
5766
5767 // temp = object->GetClass();
5768 if (receiver.IsStackSlot()) {
5769 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5770 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5771 } else {
5772 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5773 }
5774 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005775 // Instead of simply (possibly) unpoisoning `temp` here, we should
5776 // emit a read barrier for the previous class reference load.
5777 // However this is not required in practice, as this is an
5778 // intermediate/temporary reference and because the current
5779 // concurrent copying collector keeps the from-space memory
5780 // intact/accessible until the end of the marking phase (the
5781 // concurrent copying collector may not in the future).
5782 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005783 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5784 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5785 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005786 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005787 // temp = temp->GetImtEntryAt(method_offset);
5788 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5789 // T9 = temp->GetEntryPoint();
5790 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5791 // T9();
5792 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005793 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005794 DCHECK(!codegen_->IsLeafMethod());
5795 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5796}
5797
5798void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005799 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5800 if (intrinsic.TryDispatch(invoke)) {
5801 return;
5802 }
5803
Alexey Frunze4dda3372015-06-01 18:31:49 -07005804 HandleInvoke(invoke);
5805}
5806
5807void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005808 // Explicit clinit checks triggered by static invokes must have been pruned by
5809 // art::PrepareForRegisterAllocation.
5810 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005811
Chris Larsen3039e382015-08-26 07:54:08 -07005812 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5813 if (intrinsic.TryDispatch(invoke)) {
5814 return;
5815 }
5816
Alexey Frunze4dda3372015-06-01 18:31:49 -07005817 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005818}
5819
Orion Hodsonac141392017-01-13 11:53:47 +00005820void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5821 HandleInvoke(invoke);
5822}
5823
5824void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5825 codegen_->GenerateInvokePolymorphicCall(invoke);
5826}
5827
Chris Larsen3039e382015-08-26 07:54:08 -07005828static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005829 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005830 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5831 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005832 return true;
5833 }
5834 return false;
5835}
5836
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005837HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005838 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005839 bool fallback_load = false;
5840 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005841 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005842 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005843 case HLoadString::LoadKind::kBssEntry:
5844 DCHECK(!Runtime::Current()->UseJitCompilation());
5845 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005846 case HLoadString::LoadKind::kJitTableAddress:
5847 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005848 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005849 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005850 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005851 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005852 }
5853 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005854 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005855 }
5856 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005857}
5858
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5860 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005861 bool fallback_load = false;
5862 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005863 case HLoadClass::LoadKind::kInvalid:
5864 LOG(FATAL) << "UNREACHABLE";
5865 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005866 case HLoadClass::LoadKind::kReferrersClass:
5867 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005868 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005869 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005870 case HLoadClass::LoadKind::kBssEntry:
5871 DCHECK(!Runtime::Current()->UseJitCompilation());
5872 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005873 case HLoadClass::LoadKind::kJitTableAddress:
5874 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005875 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005876 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005877 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005878 break;
5879 }
5880 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005881 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005882 }
5883 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005884}
5885
Vladimir Markodc151b22015-10-15 18:02:30 +01005886HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5887 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005888 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005889 // On MIPS64 we support all dispatch types.
5890 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005891}
5892
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005893void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5894 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005895 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005896 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005897 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5898 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5899
Alexey Frunze19f6c692016-11-30 19:19:55 -08005900 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005901 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005902 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005903 uint32_t offset =
5904 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005905 __ LoadFromOffset(kLoadDoubleword,
5906 temp.AsRegister<GpuRegister>(),
5907 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005908 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005909 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005910 }
Vladimir Marko58155012015-08-19 12:49:41 +00005911 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005912 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005913 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005914 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5915 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005916 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005917 NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005918 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005919 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005920 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005921 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5922 break;
5923 }
Vladimir Marko58155012015-08-19 12:49:41 +00005924 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005925 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5926 kLoadDoubleword,
5927 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005928 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005929 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005930 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005931 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005932 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5933 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5934 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005935 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5936 break;
5937 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005938 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5939 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5940 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005941 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005942 }
5943
Alexey Frunze19f6c692016-11-30 19:19:55 -08005944 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005945 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005946 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005947 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005948 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5949 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5950 __ LoadFromOffset(kLoadDoubleword,
5951 T9,
5952 callee_method.AsRegister<GpuRegister>(),
5953 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005954 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005955 // T9()
5956 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005957 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005958 break;
5959 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005960 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5961
Alexey Frunze4dda3372015-06-01 18:31:49 -07005962 DCHECK(!IsLeafMethod());
5963}
5964
5965void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005966 // Explicit clinit checks triggered by static invokes must have been pruned by
5967 // art::PrepareForRegisterAllocation.
5968 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005969
5970 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5971 return;
5972 }
5973
5974 LocationSummary* locations = invoke->GetLocations();
5975 codegen_->GenerateStaticOrDirectCall(invoke,
5976 locations->HasTemps()
5977 ? locations->GetTemp(0)
5978 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005979}
5980
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005981void CodeGeneratorMIPS64::GenerateVirtualCall(
5982 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005983 // Use the calling convention instead of the location of the receiver, as
5984 // intrinsics may have put the receiver in a different register. In the intrinsics
5985 // slow path, the arguments have been moved to the right place, so here we are
5986 // guaranteed that the receiver is the first register of the calling convention.
5987 InvokeDexCallingConvention calling_convention;
5988 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5989
Alexey Frunze53afca12015-11-05 16:34:23 -08005990 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005991 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5992 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5993 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005994 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005995
5996 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005997 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005998 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005999 // Instead of simply (possibly) unpoisoning `temp` here, we should
6000 // emit a read barrier for the previous class reference load.
6001 // However this is not required in practice, as this is an
6002 // intermediate/temporary reference and because the current
6003 // concurrent copying collector keeps the from-space memory
6004 // intact/accessible until the end of the marking phase (the
6005 // concurrent copying collector may not in the future).
6006 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006007 // temp = temp->GetMethodAt(method_offset);
6008 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6009 // T9 = temp->GetEntryPoint();
6010 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6011 // T9();
6012 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006013 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006014 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006015}
6016
6017void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6018 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6019 return;
6020 }
6021
6022 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006023 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006024}
6025
6026void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006027 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006028 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006029 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006030 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6031 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006032 return;
6033 }
Vladimir Marko41559982017-01-06 14:04:23 +00006034 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006035
Alexey Frunze15958152017-02-09 19:08:30 -08006036 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6037 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006038 ? LocationSummary::kCallOnSlowPath
6039 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006040 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006041 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6042 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6043 }
Vladimir Marko41559982017-01-06 14:04:23 +00006044 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006045 locations->SetInAt(0, Location::RequiresRegister());
6046 }
6047 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006048 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6049 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6050 // Rely on the type resolution or initialization and marking to save everything we need.
6051 RegisterSet caller_saves = RegisterSet::Empty();
6052 InvokeRuntimeCallingConvention calling_convention;
6053 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6054 locations->SetCustomSlowPathCallerSaves(caller_saves);
6055 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006056 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006057 }
6058 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006059}
6060
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006061// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6062// move.
6063void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006064 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006065 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006066 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006067 return;
6068 }
Vladimir Marko41559982017-01-06 14:04:23 +00006069 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006070
Vladimir Marko41559982017-01-06 14:04:23 +00006071 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006072 Location out_loc = locations->Out();
6073 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6074 GpuRegister current_method_reg = ZERO;
6075 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006076 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006077 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6078 }
6079
Alexey Frunze15958152017-02-09 19:08:30 -08006080 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6081 ? kWithoutReadBarrier
6082 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006083 bool generate_null_check = false;
6084 switch (load_kind) {
6085 case HLoadClass::LoadKind::kReferrersClass:
6086 DCHECK(!cls->CanCallRuntime());
6087 DCHECK(!cls->MustGenerateClinitCheck());
6088 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6089 GenerateGcRootFieldLoad(cls,
6090 out_loc,
6091 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006092 ArtMethod::DeclaringClassOffset().Int32Value(),
6093 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006094 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006095 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006096 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006097 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006098 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006099 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006100 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006101 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006102 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006103 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6104 break;
6105 }
6106 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006107 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006108 uint32_t address = dchecked_integral_cast<uint32_t>(
6109 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6110 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006111 __ LoadLiteral(out,
6112 kLoadUnsignedWord,
6113 codegen_->DeduplicateBootImageAddressLiteral(address));
6114 break;
6115 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006116 case HLoadClass::LoadKind::kBootImageClassTable: {
6117 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6118 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006119 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006120 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006121 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006122 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6123 __ Lwu(out, AT, /* placeholder */ 0x5678);
6124 // Extract the reference from the slot data, i.e. clear the hash bits.
6125 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6126 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6127 if (masked_hash != 0) {
6128 __ Daddiu(out, out, -masked_hash);
6129 }
6130 break;
6131 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006132 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006133 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6134 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006135 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6136 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006137 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006138 GenerateGcRootFieldLoad(cls,
6139 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006140 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006141 /* placeholder */ 0x5678,
6142 read_barrier_option,
6143 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006144 generate_null_check = true;
6145 break;
6146 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006147 case HLoadClass::LoadKind::kJitTableAddress:
6148 __ LoadLiteral(out,
6149 kLoadUnsignedWord,
6150 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6151 cls->GetTypeIndex(),
6152 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006153 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006154 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006155 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006156 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006157 LOG(FATAL) << "UNREACHABLE";
6158 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006159 }
6160
6161 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6162 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006163 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006164 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006165 codegen_->AddSlowPath(slow_path);
6166 if (generate_null_check) {
6167 __ Beqzc(out, slow_path->GetEntryLabel());
6168 }
6169 if (cls->MustGenerateClinitCheck()) {
6170 GenerateClassInitializationCheck(slow_path, out);
6171 } else {
6172 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006173 }
6174 }
6175}
6176
David Brazdilcb1c0552015-08-04 16:22:25 +01006177static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006178 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006179}
6180
Alexey Frunze4dda3372015-06-01 18:31:49 -07006181void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6182 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006183 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006184 locations->SetOut(Location::RequiresRegister());
6185}
6186
6187void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6188 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006189 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6190}
6191
6192void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006193 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006194}
6195
6196void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6197 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006198}
6199
Alexey Frunze4dda3372015-06-01 18:31:49 -07006200void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006201 HLoadString::LoadKind load_kind = load->GetLoadKind();
6202 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006203 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006204 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006205 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006206 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006207 } else {
6208 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006209 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6210 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6211 // Rely on the pResolveString and marking to save everything we need.
6212 RegisterSet caller_saves = RegisterSet::Empty();
6213 InvokeRuntimeCallingConvention calling_convention;
6214 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6215 locations->SetCustomSlowPathCallerSaves(caller_saves);
6216 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006217 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006218 }
6219 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006220 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006221}
6222
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006223// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6224// move.
6225void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006226 HLoadString::LoadKind load_kind = load->GetLoadKind();
6227 LocationSummary* locations = load->GetLocations();
6228 Location out_loc = locations->Out();
6229 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6230
6231 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006232 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6233 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006234 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006235 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006236 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006237 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006238 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006239 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006240 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006241 }
6242 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006243 uint32_t address = dchecked_integral_cast<uint32_t>(
6244 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6245 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006246 __ LoadLiteral(out,
6247 kLoadUnsignedWord,
6248 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006249 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006250 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006251 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006252 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006253 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006254 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006255 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006256 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006257 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6258 __ Lwu(out, AT, /* placeholder */ 0x5678);
6259 return;
6260 }
6261 case HLoadString::LoadKind::kBssEntry: {
6262 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6263 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6264 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6265 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6266 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006267 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006268 GenerateGcRootFieldLoad(load,
6269 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006270 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006271 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006272 kCompilerReadBarrierOption,
6273 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006274 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006275 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006276 codegen_->AddSlowPath(slow_path);
6277 __ Beqzc(out, slow_path->GetEntryLabel());
6278 __ Bind(slow_path->GetExitLabel());
6279 return;
6280 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006281 case HLoadString::LoadKind::kJitTableAddress:
6282 __ LoadLiteral(out,
6283 kLoadUnsignedWord,
6284 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6285 load->GetStringIndex(),
6286 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006287 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006288 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006289 default:
6290 break;
6291 }
6292
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006293 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006294 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006295 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006296 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006297 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6298 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6299 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006300}
6301
Alexey Frunze4dda3372015-06-01 18:31:49 -07006302void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006303 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006304 locations->SetOut(Location::ConstantLocation(constant));
6305}
6306
6307void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6308 // Will be generated at use site.
6309}
6310
6311void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006312 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6313 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006314 InvokeRuntimeCallingConvention calling_convention;
6315 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6316}
6317
6318void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006319 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006320 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006321 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006322 if (instruction->IsEnter()) {
6323 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6324 } else {
6325 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6326 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006327}
6328
6329void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6330 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006331 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006332 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006333 case DataType::Type::kInt32:
6334 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006335 locations->SetInAt(0, Location::RequiresRegister());
6336 locations->SetInAt(1, Location::RequiresRegister());
6337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6338 break;
6339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340 case DataType::Type::kFloat32:
6341 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006342 locations->SetInAt(0, Location::RequiresFpuRegister());
6343 locations->SetInAt(1, Location::RequiresFpuRegister());
6344 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6345 break;
6346
6347 default:
6348 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6349 }
6350}
6351
6352void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006353 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006354 LocationSummary* locations = instruction->GetLocations();
6355
6356 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006357 case DataType::Type::kInt32:
6358 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006359 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6360 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6361 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006363 __ MulR6(dst, lhs, rhs);
6364 else
6365 __ Dmul(dst, lhs, rhs);
6366 break;
6367 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 case DataType::Type::kFloat32:
6369 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006370 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6371 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6372 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006374 __ MulS(dst, lhs, rhs);
6375 else
6376 __ MulD(dst, lhs, rhs);
6377 break;
6378 }
6379 default:
6380 LOG(FATAL) << "Unexpected mul type " << type;
6381 }
6382}
6383
6384void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6385 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006386 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006387 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006388 case DataType::Type::kInt32:
6389 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006390 locations->SetInAt(0, Location::RequiresRegister());
6391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6392 break;
6393
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006394 case DataType::Type::kFloat32:
6395 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006396 locations->SetInAt(0, Location::RequiresFpuRegister());
6397 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6398 break;
6399
6400 default:
6401 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6402 }
6403}
6404
6405void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006406 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006407 LocationSummary* locations = instruction->GetLocations();
6408
6409 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006410 case DataType::Type::kInt32:
6411 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006412 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6413 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006415 __ Subu(dst, ZERO, src);
6416 else
6417 __ Dsubu(dst, ZERO, src);
6418 break;
6419 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006420 case DataType::Type::kFloat32:
6421 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006422 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6423 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006424 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006425 __ NegS(dst, src);
6426 else
6427 __ NegD(dst, src);
6428 break;
6429 }
6430 default:
6431 LOG(FATAL) << "Unexpected neg type " << type;
6432 }
6433}
6434
6435void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006436 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6437 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006438 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006440 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6441 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006442}
6443
6444void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006445 // Note: if heap poisoning is enabled, the entry point takes care
6446 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006447 QuickEntrypointEnum entrypoint =
6448 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6449 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006450 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006451 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006452}
6453
6454void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006455 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6456 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006457 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006458 if (instruction->IsStringAlloc()) {
6459 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6460 } else {
6461 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006462 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006463 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006464}
6465
6466void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006467 // Note: if heap poisoning is enabled, the entry point takes care
6468 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006469 if (instruction->IsStringAlloc()) {
6470 // String is allocated through StringFactory. Call NewEmptyString entry point.
6471 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006472 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006473 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006474 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6475 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6476 __ Jalr(T9);
6477 __ Nop();
6478 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6479 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006480 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006481 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006482 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006483}
6484
6485void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006486 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006487 locations->SetInAt(0, Location::RequiresRegister());
6488 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6489}
6490
6491void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006492 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006493 LocationSummary* locations = instruction->GetLocations();
6494
6495 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006496 case DataType::Type::kInt32:
6497 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006498 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6499 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6500 __ Nor(dst, src, ZERO);
6501 break;
6502 }
6503
6504 default:
6505 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6506 }
6507}
6508
6509void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006510 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006511 locations->SetInAt(0, Location::RequiresRegister());
6512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6513}
6514
6515void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6516 LocationSummary* locations = instruction->GetLocations();
6517 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6518 locations->InAt(0).AsRegister<GpuRegister>(),
6519 1);
6520}
6521
6522void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006523 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6524 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006525}
6526
Calin Juravle2ae48182016-03-16 14:05:09 +00006527void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6528 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006529 return;
6530 }
6531 Location obj = instruction->GetLocations()->InAt(0);
6532
6533 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006534 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006535}
6536
Calin Juravle2ae48182016-03-16 14:05:09 +00006537void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006538 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006539 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006540 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006541
6542 Location obj = instruction->GetLocations()->InAt(0);
6543
6544 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6545}
6546
6547void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006548 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006549}
6550
6551void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6552 HandleBinaryOp(instruction);
6553}
6554
6555void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6556 HandleBinaryOp(instruction);
6557}
6558
6559void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6560 LOG(FATAL) << "Unreachable";
6561}
6562
6563void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006564 if (instruction->GetNext()->IsSuspendCheck() &&
6565 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6566 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6567 // The back edge will generate the suspend check.
6568 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6569 }
6570
Alexey Frunze4dda3372015-06-01 18:31:49 -07006571 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6572}
6573
6574void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006575 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006576 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6577 if (location.IsStackSlot()) {
6578 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6579 } else if (location.IsDoubleStackSlot()) {
6580 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6581 }
6582 locations->SetOut(location);
6583}
6584
6585void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6586 ATTRIBUTE_UNUSED) {
6587 // Nothing to do, the parameter is already at its location.
6588}
6589
6590void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6591 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006592 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006593 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6594}
6595
6596void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6597 ATTRIBUTE_UNUSED) {
6598 // Nothing to do, the method is already at its location.
6599}
6600
6601void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006602 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006603 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006604 locations->SetInAt(i, Location::Any());
6605 }
6606 locations->SetOut(Location::Any());
6607}
6608
6609void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6610 LOG(FATAL) << "Unreachable";
6611}
6612
6613void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006614 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006615 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006616 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6617 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006618 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006619
6620 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006621 case DataType::Type::kInt32:
6622 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006623 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006624 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6626 break;
6627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006628 case DataType::Type::kFloat32:
6629 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006630 InvokeRuntimeCallingConvention calling_convention;
6631 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6632 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6633 locations->SetOut(calling_convention.GetReturnLocation(type));
6634 break;
6635 }
6636
6637 default:
6638 LOG(FATAL) << "Unexpected rem type " << type;
6639 }
6640}
6641
6642void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006643 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006644
6645 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006646 case DataType::Type::kInt32:
6647 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006648 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006649 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006651 case DataType::Type::kFloat32:
6652 case DataType::Type::kFloat64: {
6653 QuickEntrypointEnum entrypoint =
6654 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006655 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006656 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006657 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6658 } else {
6659 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6660 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006661 break;
6662 }
6663 default:
6664 LOG(FATAL) << "Unexpected rem type " << type;
6665 }
6666}
6667
Aart Bik3dad3412018-02-28 12:01:46 -08006668void LocationsBuilderMIPS64::VisitAbs(HAbs* abs) {
6669 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
6670 switch (abs->GetResultType()) {
6671 case DataType::Type::kInt32:
6672 case DataType::Type::kInt64:
6673 locations->SetInAt(0, Location::RequiresRegister());
6674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6675 break;
6676 case DataType::Type::kFloat32:
6677 case DataType::Type::kFloat64:
6678 locations->SetInAt(0, Location::RequiresFpuRegister());
6679 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6680 break;
6681 default:
6682 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
6683 }
6684}
6685
6686void InstructionCodeGeneratorMIPS64::VisitAbs(HAbs* abs) {
6687 LocationSummary* locations = abs->GetLocations();
6688 switch (abs->GetResultType()) {
6689 case DataType::Type::kInt32: {
6690 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
6691 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6692 __ Sra(AT, in, 31);
6693 __ Xor(out, in, AT);
6694 __ Subu(out, out, AT);
6695 break;
6696 }
6697 case DataType::Type::kInt64: {
6698 GpuRegister in = locations->InAt(0).AsRegister<GpuRegister>();
6699 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
6700 __ Dsra32(AT, in, 31);
6701 __ Xor(out, in, AT);
6702 __ Dsubu(out, out, AT);
6703 break;
6704 }
6705 case DataType::Type::kFloat32: {
6706 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
6707 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6708 __ AbsS(out, in);
6709 break;
6710 }
6711 case DataType::Type::kFloat64: {
6712 FpuRegister in = locations->InAt(0).AsFpuRegister<FpuRegister>();
6713 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
6714 __ AbsD(out, in);
6715 break;
6716 }
6717 default:
6718 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
6719 }
6720}
6721
Igor Murashkind01745e2017-04-05 16:40:31 -07006722void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6723 constructor_fence->SetLocations(nullptr);
6724}
6725
6726void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6727 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6728 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6729}
6730
Alexey Frunze4dda3372015-06-01 18:31:49 -07006731void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6732 memory_barrier->SetLocations(nullptr);
6733}
6734
6735void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6736 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6737}
6738
6739void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006740 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006741 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006742 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6743}
6744
6745void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6746 codegen_->GenerateFrameExit();
6747}
6748
6749void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6750 ret->SetLocations(nullptr);
6751}
6752
6753void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6754 codegen_->GenerateFrameExit();
6755}
6756
Alexey Frunze92d90602015-12-18 18:16:36 -08006757void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6758 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006759}
6760
Alexey Frunze92d90602015-12-18 18:16:36 -08006761void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6762 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006763}
6764
Alexey Frunze4dda3372015-06-01 18:31:49 -07006765void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6766 HandleShift(shl);
6767}
6768
6769void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6770 HandleShift(shl);
6771}
6772
6773void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6774 HandleShift(shr);
6775}
6776
6777void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6778 HandleShift(shr);
6779}
6780
Alexey Frunze4dda3372015-06-01 18:31:49 -07006781void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6782 HandleBinaryOp(instruction);
6783}
6784
6785void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6786 HandleBinaryOp(instruction);
6787}
6788
6789void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6790 HandleFieldGet(instruction, instruction->GetFieldInfo());
6791}
6792
6793void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6794 HandleFieldGet(instruction, instruction->GetFieldInfo());
6795}
6796
6797void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6798 HandleFieldSet(instruction, instruction->GetFieldInfo());
6799}
6800
6801void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006802 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006803}
6804
Calin Juravlee460d1d2015-09-29 04:52:17 +01006805void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6806 HUnresolvedInstanceFieldGet* instruction) {
6807 FieldAccessCallingConventionMIPS64 calling_convention;
6808 codegen_->CreateUnresolvedFieldLocationSummary(
6809 instruction, instruction->GetFieldType(), calling_convention);
6810}
6811
6812void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6813 HUnresolvedInstanceFieldGet* instruction) {
6814 FieldAccessCallingConventionMIPS64 calling_convention;
6815 codegen_->GenerateUnresolvedFieldAccess(instruction,
6816 instruction->GetFieldType(),
6817 instruction->GetFieldIndex(),
6818 instruction->GetDexPc(),
6819 calling_convention);
6820}
6821
6822void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6823 HUnresolvedInstanceFieldSet* instruction) {
6824 FieldAccessCallingConventionMIPS64 calling_convention;
6825 codegen_->CreateUnresolvedFieldLocationSummary(
6826 instruction, instruction->GetFieldType(), calling_convention);
6827}
6828
6829void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6830 HUnresolvedInstanceFieldSet* instruction) {
6831 FieldAccessCallingConventionMIPS64 calling_convention;
6832 codegen_->GenerateUnresolvedFieldAccess(instruction,
6833 instruction->GetFieldType(),
6834 instruction->GetFieldIndex(),
6835 instruction->GetDexPc(),
6836 calling_convention);
6837}
6838
6839void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6840 HUnresolvedStaticFieldGet* instruction) {
6841 FieldAccessCallingConventionMIPS64 calling_convention;
6842 codegen_->CreateUnresolvedFieldLocationSummary(
6843 instruction, instruction->GetFieldType(), calling_convention);
6844}
6845
6846void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6847 HUnresolvedStaticFieldGet* instruction) {
6848 FieldAccessCallingConventionMIPS64 calling_convention;
6849 codegen_->GenerateUnresolvedFieldAccess(instruction,
6850 instruction->GetFieldType(),
6851 instruction->GetFieldIndex(),
6852 instruction->GetDexPc(),
6853 calling_convention);
6854}
6855
6856void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6857 HUnresolvedStaticFieldSet* instruction) {
6858 FieldAccessCallingConventionMIPS64 calling_convention;
6859 codegen_->CreateUnresolvedFieldLocationSummary(
6860 instruction, instruction->GetFieldType(), calling_convention);
6861}
6862
6863void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6864 HUnresolvedStaticFieldSet* instruction) {
6865 FieldAccessCallingConventionMIPS64 calling_convention;
6866 codegen_->GenerateUnresolvedFieldAccess(instruction,
6867 instruction->GetFieldType(),
6868 instruction->GetFieldIndex(),
6869 instruction->GetDexPc(),
6870 calling_convention);
6871}
6872
Alexey Frunze4dda3372015-06-01 18:31:49 -07006873void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006874 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6875 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006876 // In suspend check slow path, usually there are no caller-save registers at all.
6877 // If SIMD instructions are present, however, we force spilling all live SIMD
6878 // registers in full width (since the runtime only saves/restores lower part).
6879 locations->SetCustomSlowPathCallerSaves(
6880 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006881}
6882
6883void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6884 HBasicBlock* block = instruction->GetBlock();
6885 if (block->GetLoopInformation() != nullptr) {
6886 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6887 // The back edge will generate the suspend check.
6888 return;
6889 }
6890 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6891 // The goto will generate the suspend check.
6892 return;
6893 }
6894 GenerateSuspendCheck(instruction, nullptr);
6895}
6896
Alexey Frunze4dda3372015-06-01 18:31:49 -07006897void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006898 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6899 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006900 InvokeRuntimeCallingConvention calling_convention;
6901 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6902}
6903
6904void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006905 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006906 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6907}
6908
6909void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006910 DataType::Type input_type = conversion->GetInputType();
6911 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006912 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6913 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006915 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6916 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006917 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6918 }
6919
Vladimir Markoca6fff82017-10-03 14:49:14 +01006920 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006922 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006923 locations->SetInAt(0, Location::RequiresFpuRegister());
6924 } else {
6925 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006926 }
6927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006928 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006929 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006930 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006932 }
6933}
6934
6935void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6936 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006937 DataType::Type result_type = conversion->GetResultType();
6938 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006939
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006940 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6941 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006943 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006944 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6945 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6946
6947 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006948 case DataType::Type::kUint8:
6949 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006950 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006951 case DataType::Type::kInt8:
6952 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006953 // Type conversion from long to types narrower than int is a result of code
6954 // transformations. To avoid unpredictable results for SEB and SEH, we first
6955 // need to sign-extend the low 32-bit value into bits 32 through 63.
6956 __ Sll(dst, src, 0);
6957 __ Seb(dst, dst);
6958 } else {
6959 __ Seb(dst, src);
6960 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006961 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006962 case DataType::Type::kUint16:
6963 __ Andi(dst, src, 0xFFFF);
6964 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006965 case DataType::Type::kInt16:
6966 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006967 // Type conversion from long to types narrower than int is a result of code
6968 // transformations. To avoid unpredictable results for SEB and SEH, we first
6969 // need to sign-extend the low 32-bit value into bits 32 through 63.
6970 __ Sll(dst, src, 0);
6971 __ Seh(dst, dst);
6972 } else {
6973 __ Seh(dst, src);
6974 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006975 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006976 case DataType::Type::kInt32:
6977 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006978 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6979 // conversions, except when the input and output registers are the same and we are not
6980 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006981 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006982 __ Sll(dst, src, 0);
6983 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006984 break;
6985
6986 default:
6987 LOG(FATAL) << "Unexpected type conversion from " << input_type
6988 << " to " << result_type;
6989 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006990 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006991 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6992 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006993 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006994 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006995 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006996 __ Cvtsl(dst, FTMP);
6997 } else {
6998 __ Cvtdl(dst, FTMP);
6999 }
7000 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007001 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007002 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007003 __ Cvtsw(dst, FTMP);
7004 } else {
7005 __ Cvtdw(dst, FTMP);
7006 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007007 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007008 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
7009 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007010 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7011 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007013 if (result_type == DataType::Type::kInt64) {
7014 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007015 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007016 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007017 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007018 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007019 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007020 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007021 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007022 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007023 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007024 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007025 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007026 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007027 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007028 } else if (DataType::IsFloatingPointType(result_type) &&
7029 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007030 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7031 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007032 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007033 __ Cvtsd(dst, src);
7034 } else {
7035 __ Cvtds(dst, src);
7036 }
7037 } else {
7038 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7039 << " to " << result_type;
7040 }
7041}
7042
7043void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
7044 HandleShift(ushr);
7045}
7046
7047void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
7048 HandleShift(ushr);
7049}
7050
7051void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7052 HandleBinaryOp(instruction);
7053}
7054
7055void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7056 HandleBinaryOp(instruction);
7057}
7058
7059void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7060 // Nothing to do, this should be removed during prepare for register allocator.
7061 LOG(FATAL) << "Unreachable";
7062}
7063
7064void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7065 // Nothing to do, this should be removed during prepare for register allocator.
7066 LOG(FATAL) << "Unreachable";
7067}
7068
7069void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007070 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007071}
7072
7073void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007074 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007075}
7076
7077void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007078 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007079}
7080
7081void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007082 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007083}
7084
7085void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007086 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007087}
7088
7089void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007090 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007091}
7092
7093void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007094 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007095}
7096
7097void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007098 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007099}
7100
7101void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007102 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007103}
7104
7105void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007106 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007107}
7108
7109void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007110 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007111}
7112
7113void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007114 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007115}
7116
Aart Bike9f37602015-10-09 11:15:55 -07007117void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007118 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007119}
7120
7121void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007122 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007123}
7124
7125void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007126 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007127}
7128
7129void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007130 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007131}
7132
7133void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007134 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007135}
7136
7137void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007138 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007139}
7140
7141void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007142 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007143}
7144
7145void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007146 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007147}
7148
Mark Mendellfe57faa2015-09-18 09:26:15 -04007149// Simple implementation of packed switch - generate cascaded compare/jumps.
7150void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7151 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007152 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007153 locations->SetInAt(0, Location::RequiresRegister());
7154}
7155
Alexey Frunze0960ac52016-12-20 17:24:59 -08007156void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7157 int32_t lower_bound,
7158 uint32_t num_entries,
7159 HBasicBlock* switch_block,
7160 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007161 // Create a set of compare/jumps.
7162 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007163 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007164 // Jump to default if index is negative
7165 // Note: We don't check the case that index is positive while value < lower_bound, because in
7166 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7167 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7168
Alexey Frunze0960ac52016-12-20 17:24:59 -08007169 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007170 // Jump to successors[0] if value == lower_bound.
7171 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7172 int32_t last_index = 0;
7173 for (; num_entries - last_index > 2; last_index += 2) {
7174 __ Addiu(temp_reg, temp_reg, -2);
7175 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7176 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7177 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7178 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7179 }
7180 if (num_entries - last_index == 2) {
7181 // The last missing case_value.
7182 __ Addiu(temp_reg, temp_reg, -1);
7183 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007184 }
7185
7186 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007187 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007188 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007189 }
7190}
7191
Alexey Frunze0960ac52016-12-20 17:24:59 -08007192void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7193 int32_t lower_bound,
7194 uint32_t num_entries,
7195 HBasicBlock* switch_block,
7196 HBasicBlock* default_block) {
7197 // Create a jump table.
7198 std::vector<Mips64Label*> labels(num_entries);
7199 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7200 for (uint32_t i = 0; i < num_entries; i++) {
7201 labels[i] = codegen_->GetLabelOf(successors[i]);
7202 }
7203 JumpTable* table = __ CreateJumpTable(std::move(labels));
7204
7205 // Is the value in range?
7206 __ Addiu32(TMP, value_reg, -lower_bound);
7207 __ LoadConst32(AT, num_entries);
7208 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7209
7210 // We are in the range of the table.
7211 // Load the target address from the jump table, indexing by the value.
7212 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007213 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007214 __ Lw(TMP, TMP, 0);
7215 // Compute the absolute target address by adding the table start address
7216 // (the table contains offsets to targets relative to its start).
7217 __ Daddu(TMP, TMP, AT);
7218 // And jump.
7219 __ Jr(TMP);
7220 __ Nop();
7221}
7222
7223void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7224 int32_t lower_bound = switch_instr->GetStartValue();
7225 uint32_t num_entries = switch_instr->GetNumEntries();
7226 LocationSummary* locations = switch_instr->GetLocations();
7227 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7228 HBasicBlock* switch_block = switch_instr->GetBlock();
7229 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7230
7231 if (num_entries > kPackedSwitchJumpTableThreshold) {
7232 GenTableBasedPackedSwitch(value_reg,
7233 lower_bound,
7234 num_entries,
7235 switch_block,
7236 default_block);
7237 } else {
7238 GenPackedSwitchWithCompares(value_reg,
7239 lower_bound,
7240 num_entries,
7241 switch_block,
7242 default_block);
7243 }
7244}
7245
Chris Larsenc9905a62017-03-13 17:06:18 -07007246void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7247 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007248 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007249 locations->SetInAt(0, Location::RequiresRegister());
7250 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007251}
7252
Chris Larsenc9905a62017-03-13 17:06:18 -07007253void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7254 LocationSummary* locations = instruction->GetLocations();
7255 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7256 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7257 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7258 __ LoadFromOffset(kLoadDoubleword,
7259 locations->Out().AsRegister<GpuRegister>(),
7260 locations->InAt(0).AsRegister<GpuRegister>(),
7261 method_offset);
7262 } else {
7263 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7264 instruction->GetIndex(), kMips64PointerSize));
7265 __ LoadFromOffset(kLoadDoubleword,
7266 locations->Out().AsRegister<GpuRegister>(),
7267 locations->InAt(0).AsRegister<GpuRegister>(),
7268 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7269 __ LoadFromOffset(kLoadDoubleword,
7270 locations->Out().AsRegister<GpuRegister>(),
7271 locations->Out().AsRegister<GpuRegister>(),
7272 method_offset);
7273 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007274}
7275
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007276void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7277 ATTRIBUTE_UNUSED) {
7278 LOG(FATAL) << "Unreachable";
7279}
7280
7281void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7282 ATTRIBUTE_UNUSED) {
7283 LOG(FATAL) << "Unreachable";
7284}
7285
Alexey Frunze4dda3372015-06-01 18:31:49 -07007286} // namespace mips64
7287} // namespace art