blob: f1bb5c15eb21b7f7e95f943092d56910c1f296d8 [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)),
965 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
966 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
967 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
968 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
969 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
970 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) {
1502 const DexFile& dex_file = info.target_dex_file;
1503 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);
1508 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 Marko65979462017-05-19 17:25:12 +01001515 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001516 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001517 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001518 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001519 pc_relative_string_patches_.size() +
1520 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>(
1524 pc_relative_method_patches_, linker_patches);
1525 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1526 pc_relative_type_patches_, linker_patches);
1527 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1528 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001529 } else {
1530 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001531 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1532 pc_relative_type_patches_, linker_patches);
1533 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1534 pc_relative_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 Marko65979462017-05-19 17:25:12 +01001545CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001546 MethodReference target_method,
1547 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001548 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001549 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001550 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001551 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001552}
1553
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001554CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001555 MethodReference target_method,
1556 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001557 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001558 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001559 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001560 &method_bss_entry_patches_);
1561}
1562
Alexey Frunzef63f5692016-12-13 17:43:11 -08001563CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001564 const DexFile& dex_file,
1565 dex::TypeIndex type_index,
1566 const PcRelativePatchInfo* info_high) {
1567 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001568}
1569
Vladimir Marko1998cd02017-01-13 13:02:58 +00001570CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001571 const DexFile& dex_file,
1572 dex::TypeIndex type_index,
1573 const PcRelativePatchInfo* info_high) {
1574 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001575}
1576
Vladimir Marko65979462017-05-19 17:25:12 +01001577CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001578 const DexFile& dex_file,
1579 dex::StringIndex string_index,
1580 const PcRelativePatchInfo* info_high) {
1581 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001582}
1583
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001584CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1585 const DexFile& dex_file,
1586 dex::StringIndex string_index,
1587 const PcRelativePatchInfo* info_high) {
1588 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1589}
1590
Alexey Frunze19f6c692016-11-30 19:19:55 -08001591CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001592 const DexFile& dex_file,
1593 uint32_t offset_or_index,
1594 const PcRelativePatchInfo* info_high,
1595 ArenaDeque<PcRelativePatchInfo>* patches) {
1596 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001597 return &patches->back();
1598}
1599
Alexey Frunzef63f5692016-12-13 17:43:11 -08001600Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1601 return map->GetOrCreate(
1602 value,
1603 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1604}
1605
Alexey Frunze19f6c692016-11-30 19:19:55 -08001606Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1607 return uint64_literals_.GetOrCreate(
1608 value,
1609 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1610}
1611
Alexey Frunzef63f5692016-12-13 17:43:11 -08001612Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001613 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001614}
1615
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001616void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1617 GpuRegister out,
1618 PcRelativePatchInfo* info_low) {
1619 DCHECK(!info_high->patch_info_high);
1620 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001621 // Add the high half of a 32-bit offset to PC.
1622 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001623 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001624 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001625 if (info_low != nullptr) {
1626 DCHECK_EQ(info_low->patch_info_high, info_high);
1627 __ Bind(&info_low->label);
1628 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001629}
1630
Alexey Frunze627c1a02017-01-30 19:28:14 -08001631Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1632 dex::StringIndex string_index,
1633 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001634 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001635 return jit_string_patches_.GetOrCreate(
1636 StringReference(&dex_file, string_index),
1637 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1638}
1639
1640Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1641 dex::TypeIndex type_index,
1642 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001643 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001644 return jit_class_patches_.GetOrCreate(
1645 TypeReference(&dex_file, type_index),
1646 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1647}
1648
1649void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1650 const uint8_t* roots_data,
1651 const Literal* literal,
1652 uint64_t index_in_table) const {
1653 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1654 uintptr_t address =
1655 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1656 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1657}
1658
1659void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1660 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001661 const StringReference& string_reference = entry.first;
1662 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001663 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001664 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001665 }
1666 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001667 const TypeReference& type_reference = entry.first;
1668 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001669 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001670 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001671 }
1672}
1673
David Brazdil58282f42016-01-14 12:45:10 +00001674void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001675 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1676 blocked_core_registers_[ZERO] = true;
1677 blocked_core_registers_[K0] = true;
1678 blocked_core_registers_[K1] = true;
1679 blocked_core_registers_[GP] = true;
1680 blocked_core_registers_[SP] = true;
1681 blocked_core_registers_[RA] = true;
1682
Lazar Trsicd9672662015-09-03 17:33:01 +02001683 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1684 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685 blocked_core_registers_[AT] = true;
1686 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001687 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001688 blocked_fpu_registers_[FTMP] = true;
1689
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001690 if (GetInstructionSetFeatures().HasMsa()) {
1691 // To be used just for MSA instructions.
1692 blocked_fpu_registers_[FTMP2] = true;
1693 }
1694
Alexey Frunze4dda3372015-06-01 18:31:49 -07001695 // Reserve suspend and thread registers.
1696 blocked_core_registers_[S0] = true;
1697 blocked_core_registers_[TR] = true;
1698
1699 // Reserve T9 for function calls
1700 blocked_core_registers_[T9] = true;
1701
Goran Jakovljevic782be112016-06-21 12:39:04 +02001702 if (GetGraph()->IsDebuggable()) {
1703 // Stubs do not save callee-save floating point registers. If the graph
1704 // is debuggable, we need to deal with these registers differently. For
1705 // now, just block them.
1706 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1707 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1708 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709 }
1710}
1711
Alexey Frunze4dda3372015-06-01 18:31:49 -07001712size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1713 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001714 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001715}
1716
1717size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1718 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001719 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001720}
1721
1722size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001723 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1724 FpuRegister(reg_id),
1725 SP,
1726 stack_index);
1727 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728}
1729
1730size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001731 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1732 FpuRegister(reg_id),
1733 SP,
1734 stack_index);
1735 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001736}
1737
1738void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001739 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740}
1741
1742void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001743 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001744}
1745
Calin Juravle175dc732015-08-25 15:42:32 +01001746void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001747 HInstruction* instruction,
1748 uint32_t dex_pc,
1749 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001750 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001751 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001752 if (EntrypointRequiresStackMap(entrypoint)) {
1753 RecordPcInfo(instruction, dex_pc, slow_path);
1754 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001755}
1756
Alexey Frunze15958152017-02-09 19:08:30 -08001757void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1758 HInstruction* instruction,
1759 SlowPathCode* slow_path) {
1760 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1761 GenerateInvokeRuntime(entry_point_offset);
1762}
1763
1764void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1765 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1766 __ Jalr(T9);
1767 __ Nop();
1768}
1769
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1771 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001772 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1773 const size_t status_byte_offset =
1774 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1775 constexpr uint32_t shifted_initialized_value =
1776 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1777
1778 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1779 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001780 __ Bltuc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001781 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1782 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001783 __ Bind(slow_path->GetExitLabel());
1784}
1785
1786void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1787 __ Sync(0); // only stype 0 is supported
1788}
1789
1790void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1791 HBasicBlock* successor) {
1792 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001793 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1794
1795 if (slow_path == nullptr) {
1796 slow_path =
1797 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1798 instruction->SetSlowPath(slow_path);
1799 codegen_->AddSlowPath(slow_path);
1800 if (successor != nullptr) {
1801 DCHECK(successor->IsLoopHeader());
1802 }
1803 } else {
1804 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1805 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806
1807 __ LoadFromOffset(kLoadUnsignedHalfword,
1808 TMP,
1809 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001810 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811 if (successor == nullptr) {
1812 __ Bnezc(TMP, slow_path->GetEntryLabel());
1813 __ Bind(slow_path->GetReturnLabel());
1814 } else {
1815 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001816 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001817 // slow_path will return to GetLabelOf(successor).
1818 }
1819}
1820
1821InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1822 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001823 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001824 assembler_(codegen->GetAssembler()),
1825 codegen_(codegen) {}
1826
1827void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1828 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001829 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001831 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001832 case DataType::Type::kInt32:
1833 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001834 locations->SetInAt(0, Location::RequiresRegister());
1835 HInstruction* right = instruction->InputAt(1);
1836 bool can_use_imm = false;
1837 if (right->IsConstant()) {
1838 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1839 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1840 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001841 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001842 DCHECK(instruction->IsAdd() || instruction->IsSub());
1843 bool single_use = right->GetUses().HasExactlyOneElement();
1844 if (instruction->IsSub()) {
1845 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1846 imm = -imm;
1847 }
1848 }
1849 if (type == DataType::Type::kInt32) {
1850 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1851 } else {
1852 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1853 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001854 }
1855 }
1856 if (can_use_imm)
1857 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1858 else
1859 locations->SetInAt(1, Location::RequiresRegister());
1860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1861 }
1862 break;
1863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 case DataType::Type::kFloat32:
1865 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001866 locations->SetInAt(0, Location::RequiresFpuRegister());
1867 locations->SetInAt(1, Location::RequiresFpuRegister());
1868 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1869 break;
1870
1871 default:
1872 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1873 }
1874}
1875
1876void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001877 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001878 LocationSummary* locations = instruction->GetLocations();
1879
1880 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001881 case DataType::Type::kInt32:
1882 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001883 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1884 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1885 Location rhs_location = locations->InAt(1);
1886
1887 GpuRegister rhs_reg = ZERO;
1888 int64_t rhs_imm = 0;
1889 bool use_imm = rhs_location.IsConstant();
1890 if (use_imm) {
1891 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1892 } else {
1893 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1894 }
1895
1896 if (instruction->IsAnd()) {
1897 if (use_imm)
1898 __ Andi(dst, lhs, rhs_imm);
1899 else
1900 __ And(dst, lhs, rhs_reg);
1901 } else if (instruction->IsOr()) {
1902 if (use_imm)
1903 __ Ori(dst, lhs, rhs_imm);
1904 else
1905 __ Or(dst, lhs, rhs_reg);
1906 } else if (instruction->IsXor()) {
1907 if (use_imm)
1908 __ Xori(dst, lhs, rhs_imm);
1909 else
1910 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001911 } else if (instruction->IsAdd() || instruction->IsSub()) {
1912 if (instruction->IsSub()) {
1913 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001914 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001915 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001916 if (use_imm) {
1917 if (IsInt<16>(rhs_imm)) {
1918 __ Addiu(dst, lhs, rhs_imm);
1919 } else {
1920 int16_t rhs_imm_high = High16Bits(rhs_imm);
1921 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1922 if (rhs_imm_low < 0) {
1923 rhs_imm_high += 1;
1924 }
1925 __ Aui(dst, lhs, rhs_imm_high);
1926 if (rhs_imm_low != 0) {
1927 __ Addiu(dst, dst, rhs_imm_low);
1928 }
1929 }
1930 } else {
1931 if (instruction->IsAdd()) {
1932 __ Addu(dst, lhs, rhs_reg);
1933 } else {
1934 DCHECK(instruction->IsSub());
1935 __ Subu(dst, lhs, rhs_reg);
1936 }
1937 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001938 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001939 if (use_imm) {
1940 if (IsInt<16>(rhs_imm)) {
1941 __ Daddiu(dst, lhs, rhs_imm);
1942 } else if (IsInt<32>(rhs_imm)) {
1943 int16_t rhs_imm_high = High16Bits(rhs_imm);
1944 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1945 bool overflow_hi16 = false;
1946 if (rhs_imm_low < 0) {
1947 rhs_imm_high += 1;
1948 overflow_hi16 = (rhs_imm_high == -32768);
1949 }
1950 __ Daui(dst, lhs, rhs_imm_high);
1951 if (rhs_imm_low != 0) {
1952 __ Daddiu(dst, dst, rhs_imm_low);
1953 }
1954 if (overflow_hi16) {
1955 __ Dahi(dst, 1);
1956 }
1957 } else {
1958 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1959 if (rhs_imm_low < 0) {
1960 rhs_imm += (INT64_C(1) << 16);
1961 }
1962 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
1963 if (rhs_imm_upper < 0) {
1964 rhs_imm += (INT64_C(1) << 32);
1965 }
1966 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
1967 if (rhs_imm_high < 0) {
1968 rhs_imm += (INT64_C(1) << 48);
1969 }
1970 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
1971 GpuRegister tmp = lhs;
1972 if (rhs_imm_low != 0) {
1973 __ Daddiu(dst, tmp, rhs_imm_low);
1974 tmp = dst;
1975 }
1976 // Dahi and Dati must use the same input and output register, so we have to initialize
1977 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
1978 // Daui(dst, lhs, 0).
1979 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
1980 __ Daui(dst, tmp, rhs_imm_upper);
1981 }
1982 if (rhs_imm_high != 0) {
1983 __ Dahi(dst, rhs_imm_high);
1984 }
1985 if (rhs_imm_top != 0) {
1986 __ Dati(dst, rhs_imm_top);
1987 }
1988 }
1989 } else if (instruction->IsAdd()) {
1990 __ Daddu(dst, lhs, rhs_reg);
1991 } else {
1992 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001993 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001994 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001995 }
1996 }
1997 break;
1998 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001999 case DataType::Type::kFloat32:
2000 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002001 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2002 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2003 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2004 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002006 __ AddS(dst, lhs, rhs);
2007 else
2008 __ AddD(dst, lhs, rhs);
2009 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002011 __ SubS(dst, lhs, rhs);
2012 else
2013 __ SubD(dst, lhs, rhs);
2014 } else {
2015 LOG(FATAL) << "Unexpected floating-point binary operation";
2016 }
2017 break;
2018 }
2019 default:
2020 LOG(FATAL) << "Unexpected binary operation type " << type;
2021 }
2022}
2023
2024void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002025 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026
Vladimir Markoca6fff82017-10-03 14:49:14 +01002027 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002028 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002029 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 case DataType::Type::kInt32:
2031 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032 locations->SetInAt(0, Location::RequiresRegister());
2033 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002035 break;
2036 }
2037 default:
2038 LOG(FATAL) << "Unexpected shift type " << type;
2039 }
2040}
2041
2042void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002043 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002044 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002046
2047 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 case DataType::Type::kInt32:
2049 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2051 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2052 Location rhs_location = locations->InAt(1);
2053
2054 GpuRegister rhs_reg = ZERO;
2055 int64_t rhs_imm = 0;
2056 bool use_imm = rhs_location.IsConstant();
2057 if (use_imm) {
2058 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2059 } else {
2060 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2061 }
2062
2063 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002064 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002066
Alexey Frunze92d90602015-12-18 18:16:36 -08002067 if (shift_value == 0) {
2068 if (dst != lhs) {
2069 __ Move(dst, lhs);
2070 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002072 if (instr->IsShl()) {
2073 __ Sll(dst, lhs, shift_value);
2074 } else if (instr->IsShr()) {
2075 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002076 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002077 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002078 } else {
2079 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002080 }
2081 } else {
2082 if (shift_value < 32) {
2083 if (instr->IsShl()) {
2084 __ Dsll(dst, lhs, shift_value);
2085 } else if (instr->IsShr()) {
2086 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002087 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002088 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002089 } else {
2090 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002091 }
2092 } else {
2093 shift_value -= 32;
2094 if (instr->IsShl()) {
2095 __ Dsll32(dst, lhs, shift_value);
2096 } else if (instr->IsShr()) {
2097 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002098 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002099 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002100 } else {
2101 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002102 }
2103 }
2104 }
2105 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002107 if (instr->IsShl()) {
2108 __ Sllv(dst, lhs, rhs_reg);
2109 } else if (instr->IsShr()) {
2110 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002111 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002112 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002113 } else {
2114 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002115 }
2116 } else {
2117 if (instr->IsShl()) {
2118 __ Dsllv(dst, lhs, rhs_reg);
2119 } else if (instr->IsShr()) {
2120 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002121 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002123 } else {
2124 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002125 }
2126 }
2127 }
2128 break;
2129 }
2130 default:
2131 LOG(FATAL) << "Unexpected shift operation type " << type;
2132 }
2133}
2134
2135void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2136 HandleBinaryOp(instruction);
2137}
2138
2139void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2140 HandleBinaryOp(instruction);
2141}
2142
2143void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2144 HandleBinaryOp(instruction);
2145}
2146
2147void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2148 HandleBinaryOp(instruction);
2149}
2150
2151void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002152 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002153 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002155 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002156 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2157 object_array_get_with_read_barrier
2158 ? LocationSummary::kCallOnSlowPath
2159 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002160 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2161 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2162 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002163 locations->SetInAt(0, Location::RequiresRegister());
2164 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002165 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2167 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002168 // The output overlaps in the case of an object array get with
2169 // read barriers enabled: we do not want the move to overwrite the
2170 // array's location, as we need it to emit the read barrier.
2171 locations->SetOut(Location::RequiresRegister(),
2172 object_array_get_with_read_barrier
2173 ? Location::kOutputOverlap
2174 : Location::kNoOutputOverlap);
2175 }
2176 // We need a temporary register for the read barrier marking slow
2177 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2178 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002179 bool temp_needed = instruction->GetIndex()->IsConstant()
2180 ? !kBakerReadBarrierThunksEnableForFields
2181 : !kBakerReadBarrierThunksEnableForArrays;
2182 if (temp_needed) {
2183 locations->AddTemp(Location::RequiresRegister());
2184 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002185 }
2186}
2187
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002188static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2189 auto null_checker = [codegen, instruction]() {
2190 codegen->MaybeRecordImplicitNullCheck(instruction);
2191 };
2192 return null_checker;
2193}
2194
Alexey Frunze4dda3372015-06-01 18:31:49 -07002195void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2196 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002197 Location obj_loc = locations->InAt(0);
2198 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2199 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002201 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002202 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002205 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2206 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002207 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002208 case DataType::Type::kBool:
2209 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002210 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002211 if (index.IsConstant()) {
2212 size_t offset =
2213 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002214 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215 } else {
2216 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002217 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002218 }
2219 break;
2220 }
2221
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002223 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002224 if (index.IsConstant()) {
2225 size_t offset =
2226 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002227 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002228 } else {
2229 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002230 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002231 }
2232 break;
2233 }
2234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002235 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002236 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002237 if (maybe_compressed_char_at) {
2238 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002239 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002240 __ Dext(TMP, TMP, 0, 1);
2241 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2242 "Expecting 0=compressed, 1=uncompressed");
2243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002244 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002245 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2246 if (maybe_compressed_char_at) {
2247 Mips64Label uncompressed_load, done;
2248 __ Bnezc(TMP, &uncompressed_load);
2249 __ LoadFromOffset(kLoadUnsignedByte,
2250 out,
2251 obj,
2252 data_offset + (const_index << TIMES_1));
2253 __ Bc(&done);
2254 __ Bind(&uncompressed_load);
2255 __ LoadFromOffset(kLoadUnsignedHalfword,
2256 out,
2257 obj,
2258 data_offset + (const_index << TIMES_2));
2259 __ Bind(&done);
2260 } else {
2261 __ LoadFromOffset(kLoadUnsignedHalfword,
2262 out,
2263 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002264 data_offset + (const_index << TIMES_2),
2265 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002266 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002268 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2269 if (maybe_compressed_char_at) {
2270 Mips64Label uncompressed_load, done;
2271 __ Bnezc(TMP, &uncompressed_load);
2272 __ Daddu(TMP, obj, index_reg);
2273 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2274 __ Bc(&done);
2275 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002276 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002277 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2278 __ Bind(&done);
2279 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002280 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002281 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002282 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002283 }
2284 break;
2285 }
2286
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002287 case DataType::Type::kInt16: {
2288 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2289 if (index.IsConstant()) {
2290 size_t offset =
2291 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2292 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2293 } else {
2294 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2295 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2296 }
2297 break;
2298 }
2299
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002301 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002302 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 LoadOperandType load_type =
2304 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002305 if (index.IsConstant()) {
2306 size_t offset =
2307 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002308 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002309 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002310 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002311 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002312 }
2313 break;
2314 }
2315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002316 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002317 static_assert(
2318 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2319 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2320 // /* HeapReference<Object> */ out =
2321 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2322 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002323 bool temp_needed = index.IsConstant()
2324 ? !kBakerReadBarrierThunksEnableForFields
2325 : !kBakerReadBarrierThunksEnableForArrays;
2326 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002327 // Note that a potential implicit null check is handled in this
2328 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002329 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2330 if (index.IsConstant()) {
2331 // Array load with a constant index can be treated as a field load.
2332 size_t offset =
2333 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2334 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2335 out_loc,
2336 obj,
2337 offset,
2338 temp,
2339 /* needs_null_check */ false);
2340 } else {
2341 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2342 out_loc,
2343 obj,
2344 data_offset,
2345 index,
2346 temp,
2347 /* needs_null_check */ false);
2348 }
Alexey Frunze15958152017-02-09 19:08:30 -08002349 } else {
2350 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2351 if (index.IsConstant()) {
2352 size_t offset =
2353 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2354 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2355 // If read barriers are enabled, emit read barriers other than
2356 // Baker's using a slow path (and also unpoison the loaded
2357 // reference, if heap poisoning is enabled).
2358 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2359 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002360 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002361 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2362 // If read barriers are enabled, emit read barriers other than
2363 // Baker's using a slow path (and also unpoison the loaded
2364 // reference, if heap poisoning is enabled).
2365 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2366 out_loc,
2367 out_loc,
2368 obj_loc,
2369 data_offset,
2370 index);
2371 }
2372 }
2373 break;
2374 }
2375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002377 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002378 if (index.IsConstant()) {
2379 size_t offset =
2380 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002381 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002382 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002383 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002384 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002385 }
2386 break;
2387 }
2388
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002389 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002390 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002391 if (index.IsConstant()) {
2392 size_t offset =
2393 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002394 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002395 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002396 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002397 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002398 }
2399 break;
2400 }
2401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002402 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002403 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002404 if (index.IsConstant()) {
2405 size_t offset =
2406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002407 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002408 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002409 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002410 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002411 }
2412 break;
2413 }
2414
Aart Bik66c158e2018-01-31 12:55:04 -08002415 case DataType::Type::kUint32:
2416 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002418 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2419 UNREACHABLE();
2420 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002421}
2422
2423void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002424 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002425 locations->SetInAt(0, Location::RequiresRegister());
2426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2427}
2428
2429void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2430 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002431 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002432 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2433 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2434 __ LoadFromOffset(kLoadWord, out, obj, offset);
2435 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002436 // Mask out compression flag from String's array length.
2437 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2438 __ Srl(out, out, 1u);
2439 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002440}
2441
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002442Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2443 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2444 ? Location::ConstantLocation(instruction->AsConstant())
2445 : Location::RequiresRegister();
2446}
2447
2448Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2449 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2450 // We can store a non-zero float or double constant without first loading it into the FPU,
2451 // but we should only prefer this if the constant has a single use.
2452 if (instruction->IsConstant() &&
2453 (instruction->AsConstant()->IsZeroBitPattern() ||
2454 instruction->GetUses().HasExactlyOneElement())) {
2455 return Location::ConstantLocation(instruction->AsConstant());
2456 // Otherwise fall through and require an FPU register for the constant.
2457 }
2458 return Location::RequiresFpuRegister();
2459}
2460
Alexey Frunze4dda3372015-06-01 18:31:49 -07002461void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002463
2464 bool needs_write_barrier =
2465 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2466 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2467
Vladimir Markoca6fff82017-10-03 14:49:14 +01002468 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002469 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002470 may_need_runtime_call_for_type_check ?
2471 LocationSummary::kCallOnSlowPath :
2472 LocationSummary::kNoCall);
2473
2474 locations->SetInAt(0, Location::RequiresRegister());
2475 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002477 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002478 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002479 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2480 }
2481 if (needs_write_barrier) {
2482 // Temporary register for the write barrier.
2483 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002484 }
2485}
2486
2487void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2488 LocationSummary* locations = instruction->GetLocations();
2489 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2490 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002491 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002492 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002493 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002494 bool needs_write_barrier =
2495 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002496 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002497 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002498
2499 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002501 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002502 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002503 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002504 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002505 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002506 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002507 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2508 }
2509 if (value_location.IsConstant()) {
2510 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2511 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2512 } else {
2513 GpuRegister value = value_location.AsRegister<GpuRegister>();
2514 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002515 }
2516 break;
2517 }
2518
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002519 case DataType::Type::kUint16:
2520 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002522 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002523 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002524 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002525 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002526 }
2527 if (value_location.IsConstant()) {
2528 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2529 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2530 } else {
2531 GpuRegister value = value_location.AsRegister<GpuRegister>();
2532 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002533 }
2534 break;
2535 }
2536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002538 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2539 if (index.IsConstant()) {
2540 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2541 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002542 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002543 }
2544 if (value_location.IsConstant()) {
2545 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2546 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2547 } else {
2548 GpuRegister value = value_location.AsRegister<GpuRegister>();
2549 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2550 }
2551 break;
2552 }
2553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002555 if (value_location.IsConstant()) {
2556 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002557 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002558 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002559 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002561 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002562 }
Alexey Frunze15958152017-02-09 19:08:30 -08002563 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2564 DCHECK_EQ(value, 0);
2565 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2566 DCHECK(!needs_write_barrier);
2567 DCHECK(!may_need_runtime_call_for_type_check);
2568 break;
2569 }
2570
2571 DCHECK(needs_write_barrier);
2572 GpuRegister value = value_location.AsRegister<GpuRegister>();
2573 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2574 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2575 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2576 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2577 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2578 Mips64Label done;
2579 SlowPathCodeMIPS64* slow_path = nullptr;
2580
2581 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002582 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002583 codegen_->AddSlowPath(slow_path);
2584 if (instruction->GetValueCanBeNull()) {
2585 Mips64Label non_zero;
2586 __ Bnezc(value, &non_zero);
2587 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2588 if (index.IsConstant()) {
2589 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002590 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002591 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002592 }
Alexey Frunze15958152017-02-09 19:08:30 -08002593 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2594 __ Bc(&done);
2595 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002596 }
Alexey Frunze15958152017-02-09 19:08:30 -08002597
2598 // Note that when read barriers are enabled, the type checks
2599 // are performed without read barriers. This is fine, even in
2600 // the case where a class object is in the from-space after
2601 // the flip, as a comparison involving such a type would not
2602 // produce a false positive; it may of course produce a false
2603 // negative, in which case we would take the ArraySet slow
2604 // path.
2605
2606 // /* HeapReference<Class> */ temp1 = obj->klass_
2607 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2608 __ MaybeUnpoisonHeapReference(temp1);
2609
2610 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2611 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2612 // /* HeapReference<Class> */ temp2 = value->klass_
2613 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2614 // If heap poisoning is enabled, no need to unpoison `temp1`
2615 // nor `temp2`, as we are comparing two poisoned references.
2616
2617 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2618 Mips64Label do_put;
2619 __ Beqc(temp1, temp2, &do_put);
2620 // If heap poisoning is enabled, the `temp1` reference has
2621 // not been unpoisoned yet; unpoison it now.
2622 __ MaybeUnpoisonHeapReference(temp1);
2623
2624 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2625 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2626 // If heap poisoning is enabled, no need to unpoison
2627 // `temp1`, as we are comparing against null below.
2628 __ Bnezc(temp1, slow_path->GetEntryLabel());
2629 __ Bind(&do_put);
2630 } else {
2631 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2632 }
2633 }
2634
2635 GpuRegister source = value;
2636 if (kPoisonHeapReferences) {
2637 // Note that in the case where `value` is a null reference,
2638 // we do not enter this block, as a null reference does not
2639 // need poisoning.
2640 __ Move(temp1, value);
2641 __ PoisonHeapReference(temp1);
2642 source = temp1;
2643 }
2644
2645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2646 if (index.IsConstant()) {
2647 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002648 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002649 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002650 }
2651 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2652
2653 if (!may_need_runtime_call_for_type_check) {
2654 codegen_->MaybeRecordImplicitNullCheck(instruction);
2655 }
2656
2657 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2658
2659 if (done.IsLinked()) {
2660 __ Bind(&done);
2661 }
2662
2663 if (slow_path != nullptr) {
2664 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002665 }
2666 break;
2667 }
2668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002670 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002671 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002672 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002673 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002674 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002675 }
2676 if (value_location.IsConstant()) {
2677 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2678 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2679 } else {
2680 GpuRegister value = value_location.AsRegister<GpuRegister>();
2681 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002682 }
2683 break;
2684 }
2685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002686 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002687 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002688 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002689 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002690 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002691 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002692 }
2693 if (value_location.IsConstant()) {
2694 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2695 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2696 } else {
2697 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2698 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002699 }
2700 break;
2701 }
2702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002704 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002705 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002706 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002707 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002708 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002709 }
2710 if (value_location.IsConstant()) {
2711 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2712 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2713 } else {
2714 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2715 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002716 }
2717 break;
2718 }
2719
Aart Bik66c158e2018-01-31 12:55:04 -08002720 case DataType::Type::kUint32:
2721 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002723 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2724 UNREACHABLE();
2725 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002726}
2727
2728void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002729 RegisterSet caller_saves = RegisterSet::Empty();
2730 InvokeRuntimeCallingConvention calling_convention;
2731 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2732 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2733 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002734
2735 HInstruction* index = instruction->InputAt(0);
2736 HInstruction* length = instruction->InputAt(1);
2737
2738 bool const_index = false;
2739 bool const_length = false;
2740
2741 if (index->IsConstant()) {
2742 if (length->IsConstant()) {
2743 const_index = true;
2744 const_length = true;
2745 } else {
2746 int32_t index_value = index->AsIntConstant()->GetValue();
2747 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2748 const_index = true;
2749 }
2750 }
2751 } else if (length->IsConstant()) {
2752 int32_t length_value = length->AsIntConstant()->GetValue();
2753 if (IsUint<15>(length_value)) {
2754 const_length = true;
2755 }
2756 }
2757
2758 locations->SetInAt(0, const_index
2759 ? Location::ConstantLocation(index->AsConstant())
2760 : Location::RequiresRegister());
2761 locations->SetInAt(1, const_length
2762 ? Location::ConstantLocation(length->AsConstant())
2763 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002764}
2765
2766void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2767 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002768 Location index_loc = locations->InAt(0);
2769 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002770
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002771 if (length_loc.IsConstant()) {
2772 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2773 if (index_loc.IsConstant()) {
2774 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2775 if (index < 0 || index >= length) {
2776 BoundsCheckSlowPathMIPS64* slow_path =
2777 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2778 codegen_->AddSlowPath(slow_path);
2779 __ Bc(slow_path->GetEntryLabel());
2780 } else {
2781 // Nothing to be done.
2782 }
2783 return;
2784 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002785
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002786 BoundsCheckSlowPathMIPS64* slow_path =
2787 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2788 codegen_->AddSlowPath(slow_path);
2789 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2790 if (length == 0) {
2791 __ Bc(slow_path->GetEntryLabel());
2792 } else if (length == 1) {
2793 __ Bnezc(index, slow_path->GetEntryLabel());
2794 } else {
2795 DCHECK(IsUint<15>(length)) << length;
2796 __ Sltiu(TMP, index, length);
2797 __ Beqzc(TMP, slow_path->GetEntryLabel());
2798 }
2799 } else {
2800 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2801 BoundsCheckSlowPathMIPS64* slow_path =
2802 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2803 codegen_->AddSlowPath(slow_path);
2804 if (index_loc.IsConstant()) {
2805 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2806 if (index < 0) {
2807 __ Bc(slow_path->GetEntryLabel());
2808 } else if (index == 0) {
2809 __ Blezc(length, slow_path->GetEntryLabel());
2810 } else {
2811 DCHECK(IsInt<16>(index + 1)) << index;
2812 __ Sltiu(TMP, length, index + 1);
2813 __ Bnezc(TMP, slow_path->GetEntryLabel());
2814 }
2815 } else {
2816 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2817 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2818 }
2819 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002820}
2821
Alexey Frunze15958152017-02-09 19:08:30 -08002822// Temp is used for read barrier.
2823static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2824 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002825 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002826 (kUseBakerReadBarrier ||
2827 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2828 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2829 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2830 return 1;
2831 }
2832 return 0;
2833}
2834
2835// Extra temp is used for read barrier.
2836static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2837 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2838}
2839
Alexey Frunze4dda3372015-06-01 18:31:49 -07002840void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002841 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002842 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002843 LocationSummary* locations =
2844 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002846 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002847 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002848}
2849
2850void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002851 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002852 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002853 Location obj_loc = locations->InAt(0);
2854 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002855 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002856 Location temp_loc = locations->GetTemp(0);
2857 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2858 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2859 DCHECK_LE(num_temps, 2u);
2860 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002861 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2862 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2863 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2864 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2865 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2866 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2867 const uint32_t object_array_data_offset =
2868 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2869 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002870
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002871 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002872 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002873 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2874 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002875 codegen_->AddSlowPath(slow_path);
2876
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002877 // Avoid this check if we know `obj` is not null.
2878 if (instruction->MustDoNullCheck()) {
2879 __ Beqzc(obj, &done);
2880 }
2881
2882 switch (type_check_kind) {
2883 case TypeCheckKind::kExactCheck:
2884 case TypeCheckKind::kArrayCheck: {
2885 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002886 GenerateReferenceLoadTwoRegisters(instruction,
2887 temp_loc,
2888 obj_loc,
2889 class_offset,
2890 maybe_temp2_loc,
2891 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002892 // Jump to slow path for throwing the exception or doing a
2893 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002894 __ Bnec(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002895 break;
2896 }
2897
2898 case TypeCheckKind::kAbstractClassCheck: {
2899 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002900 GenerateReferenceLoadTwoRegisters(instruction,
2901 temp_loc,
2902 obj_loc,
2903 class_offset,
2904 maybe_temp2_loc,
2905 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002906 // If the class is abstract, we eagerly fetch the super class of the
2907 // object to avoid doing a comparison we know will fail.
2908 Mips64Label loop;
2909 __ Bind(&loop);
2910 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002911 GenerateReferenceLoadOneRegister(instruction,
2912 temp_loc,
2913 super_offset,
2914 maybe_temp2_loc,
2915 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002916 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2917 // exception.
2918 __ Beqzc(temp, slow_path->GetEntryLabel());
2919 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002920 __ Bnec(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002921 break;
2922 }
2923
2924 case TypeCheckKind::kClassHierarchyCheck: {
2925 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002926 GenerateReferenceLoadTwoRegisters(instruction,
2927 temp_loc,
2928 obj_loc,
2929 class_offset,
2930 maybe_temp2_loc,
2931 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002932 // Walk over the class hierarchy to find a match.
2933 Mips64Label loop;
2934 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002935 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002936 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002937 GenerateReferenceLoadOneRegister(instruction,
2938 temp_loc,
2939 super_offset,
2940 maybe_temp2_loc,
2941 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002942 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2943 // exception. Otherwise, jump to the beginning of the loop.
2944 __ Bnezc(temp, &loop);
2945 __ Bc(slow_path->GetEntryLabel());
2946 break;
2947 }
2948
2949 case TypeCheckKind::kArrayObjectCheck: {
2950 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002951 GenerateReferenceLoadTwoRegisters(instruction,
2952 temp_loc,
2953 obj_loc,
2954 class_offset,
2955 maybe_temp2_loc,
2956 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002957 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002958 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002959 // Otherwise, we need to check that the object's class is a non-primitive array.
2960 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002961 GenerateReferenceLoadOneRegister(instruction,
2962 temp_loc,
2963 component_offset,
2964 maybe_temp2_loc,
2965 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002966 // If the component type is null, jump to the slow path to throw the exception.
2967 __ Beqzc(temp, slow_path->GetEntryLabel());
2968 // Otherwise, the object is indeed an array, further check that this component
2969 // type is not a primitive type.
2970 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2971 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2972 __ Bnezc(temp, slow_path->GetEntryLabel());
2973 break;
2974 }
2975
2976 case TypeCheckKind::kUnresolvedCheck:
2977 // We always go into the type check slow path for the unresolved check case.
2978 // We cannot directly call the CheckCast runtime entry point
2979 // without resorting to a type checking slow path here (i.e. by
2980 // calling InvokeRuntime directly), as it would require to
2981 // assign fixed registers for the inputs of this HInstanceOf
2982 // instruction (following the runtime calling convention), which
2983 // might be cluttered by the potential first read barrier
2984 // emission at the beginning of this method.
2985 __ Bc(slow_path->GetEntryLabel());
2986 break;
2987
2988 case TypeCheckKind::kInterfaceCheck: {
2989 // Avoid read barriers to improve performance of the fast path. We can not get false
2990 // positives by doing this.
2991 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002992 GenerateReferenceLoadTwoRegisters(instruction,
2993 temp_loc,
2994 obj_loc,
2995 class_offset,
2996 maybe_temp2_loc,
2997 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002998 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002999 GenerateReferenceLoadTwoRegisters(instruction,
3000 temp_loc,
3001 temp_loc,
3002 iftable_offset,
3003 maybe_temp2_loc,
3004 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003005 // Iftable is never null.
3006 __ Lw(TMP, temp, array_length_offset);
3007 // Loop through the iftable and check if any class matches.
3008 Mips64Label loop;
3009 __ Bind(&loop);
3010 __ Beqzc(TMP, slow_path->GetEntryLabel());
3011 __ Lwu(AT, temp, object_array_data_offset);
3012 __ MaybeUnpoisonHeapReference(AT);
3013 // Go to next interface.
3014 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3015 __ Addiu(TMP, TMP, -2);
3016 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003017 __ Bnec(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003018 break;
3019 }
3020 }
3021
3022 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003023 __ Bind(slow_path->GetExitLabel());
3024}
3025
3026void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3027 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003028 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003029 locations->SetInAt(0, Location::RequiresRegister());
3030 if (check->HasUses()) {
3031 locations->SetOut(Location::SameAsFirstInput());
3032 }
3033}
3034
3035void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3036 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003037 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003038 check->GetLoadClass(),
3039 check,
3040 check->GetDexPc(),
3041 true);
3042 codegen_->AddSlowPath(slow_path);
3043 GenerateClassInitializationCheck(slow_path,
3044 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3045}
3046
3047void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003048 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003049
Vladimir Markoca6fff82017-10-03 14:49:14 +01003050 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003051
3052 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003054 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003055 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003056 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003057 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003058 case DataType::Type::kInt32:
3059 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003060 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003061 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3063 break;
3064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003065 case DataType::Type::kFloat32:
3066 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003067 locations->SetInAt(0, Location::RequiresFpuRegister());
3068 locations->SetInAt(1, Location::RequiresFpuRegister());
3069 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003070 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071
3072 default:
3073 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3074 }
3075}
3076
3077void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3078 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003079 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003081
3082 // 0 if: left == right
3083 // 1 if: left > right
3084 // -1 if: left < right
3085 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003087 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003088 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003089 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003090 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 case DataType::Type::kInt32:
3092 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003093 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003094 Location rhs_location = locations->InAt(1);
3095 bool use_imm = rhs_location.IsConstant();
3096 GpuRegister rhs = ZERO;
3097 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003098 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003099 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3100 if (value != 0) {
3101 rhs = AT;
3102 __ LoadConst64(rhs, value);
3103 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003104 } else {
3105 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3106 if (value != 0) {
3107 rhs = AT;
3108 __ LoadConst32(rhs, value);
3109 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003110 }
3111 } else {
3112 rhs = rhs_location.AsRegister<GpuRegister>();
3113 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003114 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003115 __ Slt(res, rhs, lhs);
3116 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003117 break;
3118 }
3119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003121 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3122 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3123 Mips64Label done;
3124 __ CmpEqS(FTMP, lhs, rhs);
3125 __ LoadConst32(res, 0);
3126 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003127 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003128 __ CmpLtS(FTMP, lhs, rhs);
3129 __ LoadConst32(res, -1);
3130 __ Bc1nez(FTMP, &done);
3131 __ LoadConst32(res, 1);
3132 } else {
3133 __ CmpLtS(FTMP, rhs, lhs);
3134 __ LoadConst32(res, 1);
3135 __ Bc1nez(FTMP, &done);
3136 __ LoadConst32(res, -1);
3137 }
3138 __ Bind(&done);
3139 break;
3140 }
3141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003142 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003143 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3144 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3145 Mips64Label done;
3146 __ CmpEqD(FTMP, lhs, rhs);
3147 __ LoadConst32(res, 0);
3148 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003149 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003150 __ CmpLtD(FTMP, lhs, rhs);
3151 __ LoadConst32(res, -1);
3152 __ Bc1nez(FTMP, &done);
3153 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003154 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003155 __ CmpLtD(FTMP, rhs, lhs);
3156 __ LoadConst32(res, 1);
3157 __ Bc1nez(FTMP, &done);
3158 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003159 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003160 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003161 break;
3162 }
3163
3164 default:
3165 LOG(FATAL) << "Unimplemented compare type " << in_type;
3166 }
3167}
3168
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003169void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003170 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003171 switch (instruction->InputAt(0)->GetType()) {
3172 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003173 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003174 locations->SetInAt(0, Location::RequiresRegister());
3175 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3176 break;
3177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003178 case DataType::Type::kFloat32:
3179 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003180 locations->SetInAt(0, Location::RequiresFpuRegister());
3181 locations->SetInAt(1, Location::RequiresFpuRegister());
3182 break;
3183 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003184 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003185 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3186 }
3187}
3188
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003189void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003190 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003191 return;
3192 }
3193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003194 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003195 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003196 switch (type) {
3197 default:
3198 // Integer case.
3199 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3200 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003201 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003202 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3203 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003204 case DataType::Type::kFloat32:
3205 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003206 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3207 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003208 }
3209}
3210
Alexey Frunzec857c742015-09-23 15:12:39 -07003211void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3212 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003213 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003214
3215 LocationSummary* locations = instruction->GetLocations();
3216 Location second = locations->InAt(1);
3217 DCHECK(second.IsConstant());
3218
3219 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3220 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3221 int64_t imm = Int64FromConstant(second.GetConstant());
3222 DCHECK(imm == 1 || imm == -1);
3223
3224 if (instruction->IsRem()) {
3225 __ Move(out, ZERO);
3226 } else {
3227 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003229 __ Subu(out, ZERO, dividend);
3230 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003232 __ Dsubu(out, ZERO, dividend);
3233 }
3234 } else if (out != dividend) {
3235 __ Move(out, dividend);
3236 }
3237 }
3238}
3239
3240void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3241 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003242 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003243
3244 LocationSummary* locations = instruction->GetLocations();
3245 Location second = locations->InAt(1);
3246 DCHECK(second.IsConstant());
3247
3248 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3249 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3250 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003251 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003252 int ctz_imm = CTZ(abs_imm);
3253
3254 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003255 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003256 if (ctz_imm == 1) {
3257 // Fast path for division by +/-2, which is very common.
3258 __ Srl(TMP, dividend, 31);
3259 } else {
3260 __ Sra(TMP, dividend, 31);
3261 __ Srl(TMP, TMP, 32 - ctz_imm);
3262 }
3263 __ Addu(out, dividend, TMP);
3264 __ Sra(out, out, ctz_imm);
3265 if (imm < 0) {
3266 __ Subu(out, ZERO, out);
3267 }
3268 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003269 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003270 if (ctz_imm == 1) {
3271 // Fast path for division by +/-2, which is very common.
3272 __ Dsrl32(TMP, dividend, 31);
3273 } else {
3274 __ Dsra32(TMP, dividend, 31);
3275 if (ctz_imm > 32) {
3276 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3277 } else {
3278 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3279 }
3280 }
3281 __ Daddu(out, dividend, TMP);
3282 if (ctz_imm < 32) {
3283 __ Dsra(out, out, ctz_imm);
3284 } else {
3285 __ Dsra32(out, out, ctz_imm - 32);
3286 }
3287 if (imm < 0) {
3288 __ Dsubu(out, ZERO, out);
3289 }
3290 }
3291 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003292 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003293 if (ctz_imm == 1) {
3294 // Fast path for modulo +/-2, which is very common.
3295 __ Sra(TMP, dividend, 31);
3296 __ Subu(out, dividend, TMP);
3297 __ Andi(out, out, 1);
3298 __ Addu(out, out, TMP);
3299 } else {
3300 __ Sra(TMP, dividend, 31);
3301 __ Srl(TMP, TMP, 32 - ctz_imm);
3302 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003303 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003304 __ Subu(out, out, TMP);
3305 }
3306 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003307 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003308 if (ctz_imm == 1) {
3309 // Fast path for modulo +/-2, which is very common.
3310 __ Dsra32(TMP, dividend, 31);
3311 __ Dsubu(out, dividend, TMP);
3312 __ Andi(out, out, 1);
3313 __ Daddu(out, out, TMP);
3314 } else {
3315 __ Dsra32(TMP, dividend, 31);
3316 if (ctz_imm > 32) {
3317 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3318 } else {
3319 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3320 }
3321 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003322 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003323 __ Dsubu(out, out, TMP);
3324 }
3325 }
3326 }
3327}
3328
3329void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3330 DCHECK(instruction->IsDiv() || instruction->IsRem());
3331
3332 LocationSummary* locations = instruction->GetLocations();
3333 Location second = locations->InAt(1);
3334 DCHECK(second.IsConstant());
3335
3336 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3337 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3338 int64_t imm = Int64FromConstant(second.GetConstant());
3339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003340 DataType::Type type = instruction->GetResultType();
3341 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003342
3343 int64_t magic;
3344 int shift;
3345 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003346 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003347 &magic,
3348 &shift);
3349
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003350 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003351 __ LoadConst32(TMP, magic);
3352 __ MuhR6(TMP, dividend, TMP);
3353
3354 if (imm > 0 && magic < 0) {
3355 __ Addu(TMP, TMP, dividend);
3356 } else if (imm < 0 && magic > 0) {
3357 __ Subu(TMP, TMP, dividend);
3358 }
3359
3360 if (shift != 0) {
3361 __ Sra(TMP, TMP, shift);
3362 }
3363
3364 if (instruction->IsDiv()) {
3365 __ Sra(out, TMP, 31);
3366 __ Subu(out, TMP, out);
3367 } else {
3368 __ Sra(AT, TMP, 31);
3369 __ Subu(AT, TMP, AT);
3370 __ LoadConst32(TMP, imm);
3371 __ MulR6(TMP, AT, TMP);
3372 __ Subu(out, dividend, TMP);
3373 }
3374 } else {
3375 __ LoadConst64(TMP, magic);
3376 __ Dmuh(TMP, dividend, TMP);
3377
3378 if (imm > 0 && magic < 0) {
3379 __ Daddu(TMP, TMP, dividend);
3380 } else if (imm < 0 && magic > 0) {
3381 __ Dsubu(TMP, TMP, dividend);
3382 }
3383
3384 if (shift >= 32) {
3385 __ Dsra32(TMP, TMP, shift - 32);
3386 } else if (shift > 0) {
3387 __ Dsra(TMP, TMP, shift);
3388 }
3389
3390 if (instruction->IsDiv()) {
3391 __ Dsra32(out, TMP, 31);
3392 __ Dsubu(out, TMP, out);
3393 } else {
3394 __ Dsra32(AT, TMP, 31);
3395 __ Dsubu(AT, TMP, AT);
3396 __ LoadConst64(TMP, imm);
3397 __ Dmul(TMP, AT, TMP);
3398 __ Dsubu(out, dividend, TMP);
3399 }
3400 }
3401}
3402
3403void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3404 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003405 DataType::Type type = instruction->GetResultType();
3406 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003407
3408 LocationSummary* locations = instruction->GetLocations();
3409 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3410 Location second = locations->InAt(1);
3411
3412 if (second.IsConstant()) {
3413 int64_t imm = Int64FromConstant(second.GetConstant());
3414 if (imm == 0) {
3415 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3416 } else if (imm == 1 || imm == -1) {
3417 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003418 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003419 DivRemByPowerOfTwo(instruction);
3420 } else {
3421 DCHECK(imm <= -2 || imm >= 2);
3422 GenerateDivRemWithAnyConstant(instruction);
3423 }
3424 } else {
3425 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3426 GpuRegister divisor = second.AsRegister<GpuRegister>();
3427 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003428 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003429 __ DivR6(out, dividend, divisor);
3430 else
3431 __ Ddiv(out, dividend, divisor);
3432 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003433 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003434 __ ModR6(out, dividend, divisor);
3435 else
3436 __ Dmod(out, dividend, divisor);
3437 }
3438 }
3439}
3440
Alexey Frunze4dda3372015-06-01 18:31:49 -07003441void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3442 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003443 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003444 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003445 case DataType::Type::kInt32:
3446 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003447 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003448 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3450 break;
3451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003452 case DataType::Type::kFloat32:
3453 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003454 locations->SetInAt(0, Location::RequiresFpuRegister());
3455 locations->SetInAt(1, Location::RequiresFpuRegister());
3456 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3457 break;
3458
3459 default:
3460 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3461 }
3462}
3463
3464void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003465 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003466 LocationSummary* locations = instruction->GetLocations();
3467
3468 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003469 case DataType::Type::kInt32:
3470 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003471 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003473 case DataType::Type::kFloat32:
3474 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003475 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3476 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3477 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003478 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003479 __ DivS(dst, lhs, rhs);
3480 else
3481 __ DivD(dst, lhs, rhs);
3482 break;
3483 }
3484 default:
3485 LOG(FATAL) << "Unexpected div type " << type;
3486 }
3487}
3488
3489void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003490 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003491 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003492}
3493
3494void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3495 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003496 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003497 codegen_->AddSlowPath(slow_path);
3498 Location value = instruction->GetLocations()->InAt(0);
3499
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003503 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003504 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003505 }
3506
3507 if (value.IsConstant()) {
3508 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3509 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003510 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003511 } else {
3512 // A division by a non-null constant is valid. We don't need to perform
3513 // any check, so simply fall through.
3514 }
3515 } else {
3516 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3517 }
3518}
3519
3520void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3521 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003522 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003523 locations->SetOut(Location::ConstantLocation(constant));
3524}
3525
3526void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3527 // Will be generated at use site.
3528}
3529
3530void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3531 exit->SetLocations(nullptr);
3532}
3533
3534void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3535}
3536
3537void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3538 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003539 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003540 locations->SetOut(Location::ConstantLocation(constant));
3541}
3542
3543void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3544 // Will be generated at use site.
3545}
3546
David Brazdilfc6a86a2015-06-26 10:33:45 +00003547void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003548 if (successor->IsExitBlock()) {
3549 DCHECK(got->GetPrevious()->AlwaysThrows());
3550 return; // no code needed
3551 }
3552
Alexey Frunze4dda3372015-06-01 18:31:49 -07003553 HBasicBlock* block = got->GetBlock();
3554 HInstruction* previous = got->GetPrevious();
3555 HLoopInformation* info = block->GetLoopInformation();
3556
3557 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01003558 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
3559 __ Ld(AT, SP, kCurrentMethodStackOffset);
3560 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3561 __ Addiu(TMP, TMP, 1);
3562 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
3563 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003564 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3565 return;
3566 }
3567 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3568 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3569 }
3570 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003571 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003572 }
3573}
3574
David Brazdilfc6a86a2015-06-26 10:33:45 +00003575void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3576 got->SetLocations(nullptr);
3577}
3578
3579void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3580 HandleGoto(got, got->GetSuccessor());
3581}
3582
3583void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3584 try_boundary->SetLocations(nullptr);
3585}
3586
3587void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3588 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3589 if (!successor->IsExitBlock()) {
3590 HandleGoto(try_boundary, successor);
3591 }
3592}
3593
Alexey Frunze299a9392015-12-08 16:08:02 -08003594void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3595 bool is64bit,
3596 LocationSummary* locations) {
3597 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3598 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3599 Location rhs_location = locations->InAt(1);
3600 GpuRegister rhs_reg = ZERO;
3601 int64_t rhs_imm = 0;
3602 bool use_imm = rhs_location.IsConstant();
3603 if (use_imm) {
3604 if (is64bit) {
3605 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3606 } else {
3607 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3608 }
3609 } else {
3610 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3611 }
3612 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3613
3614 switch (cond) {
3615 case kCondEQ:
3616 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003617 if (use_imm && IsInt<16>(-rhs_imm)) {
3618 if (rhs_imm == 0) {
3619 if (cond == kCondEQ) {
3620 __ Sltiu(dst, lhs, 1);
3621 } else {
3622 __ Sltu(dst, ZERO, lhs);
3623 }
3624 } else {
3625 if (is64bit) {
3626 __ Daddiu(dst, lhs, -rhs_imm);
3627 } else {
3628 __ Addiu(dst, lhs, -rhs_imm);
3629 }
3630 if (cond == kCondEQ) {
3631 __ Sltiu(dst, dst, 1);
3632 } else {
3633 __ Sltu(dst, ZERO, dst);
3634 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003635 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003636 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003637 if (use_imm && IsUint<16>(rhs_imm)) {
3638 __ Xori(dst, lhs, rhs_imm);
3639 } else {
3640 if (use_imm) {
3641 rhs_reg = TMP;
3642 __ LoadConst64(rhs_reg, rhs_imm);
3643 }
3644 __ Xor(dst, lhs, rhs_reg);
3645 }
3646 if (cond == kCondEQ) {
3647 __ Sltiu(dst, dst, 1);
3648 } else {
3649 __ Sltu(dst, ZERO, dst);
3650 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003651 }
3652 break;
3653
3654 case kCondLT:
3655 case kCondGE:
3656 if (use_imm && IsInt<16>(rhs_imm)) {
3657 __ Slti(dst, lhs, rhs_imm);
3658 } else {
3659 if (use_imm) {
3660 rhs_reg = TMP;
3661 __ LoadConst64(rhs_reg, rhs_imm);
3662 }
3663 __ Slt(dst, lhs, rhs_reg);
3664 }
3665 if (cond == kCondGE) {
3666 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3667 // only the slt instruction but no sge.
3668 __ Xori(dst, dst, 1);
3669 }
3670 break;
3671
3672 case kCondLE:
3673 case kCondGT:
3674 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3675 // Simulate lhs <= rhs via lhs < rhs + 1.
3676 __ Slti(dst, lhs, rhs_imm_plus_one);
3677 if (cond == kCondGT) {
3678 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3679 // only the slti instruction but no sgti.
3680 __ Xori(dst, dst, 1);
3681 }
3682 } else {
3683 if (use_imm) {
3684 rhs_reg = TMP;
3685 __ LoadConst64(rhs_reg, rhs_imm);
3686 }
3687 __ Slt(dst, rhs_reg, lhs);
3688 if (cond == kCondLE) {
3689 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3690 // only the slt instruction but no sle.
3691 __ Xori(dst, dst, 1);
3692 }
3693 }
3694 break;
3695
3696 case kCondB:
3697 case kCondAE:
3698 if (use_imm && IsInt<16>(rhs_imm)) {
3699 // Sltiu sign-extends its 16-bit immediate operand before
3700 // the comparison and thus lets us compare directly with
3701 // unsigned values in the ranges [0, 0x7fff] and
3702 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3703 __ Sltiu(dst, lhs, rhs_imm);
3704 } else {
3705 if (use_imm) {
3706 rhs_reg = TMP;
3707 __ LoadConst64(rhs_reg, rhs_imm);
3708 }
3709 __ Sltu(dst, lhs, rhs_reg);
3710 }
3711 if (cond == kCondAE) {
3712 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3713 // only the sltu instruction but no sgeu.
3714 __ Xori(dst, dst, 1);
3715 }
3716 break;
3717
3718 case kCondBE:
3719 case kCondA:
3720 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3721 // Simulate lhs <= rhs via lhs < rhs + 1.
3722 // Note that this only works if rhs + 1 does not overflow
3723 // to 0, hence the check above.
3724 // Sltiu sign-extends its 16-bit immediate operand before
3725 // the comparison and thus lets us compare directly with
3726 // unsigned values in the ranges [0, 0x7fff] and
3727 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3728 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3729 if (cond == kCondA) {
3730 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3731 // only the sltiu instruction but no sgtiu.
3732 __ Xori(dst, dst, 1);
3733 }
3734 } else {
3735 if (use_imm) {
3736 rhs_reg = TMP;
3737 __ LoadConst64(rhs_reg, rhs_imm);
3738 }
3739 __ Sltu(dst, rhs_reg, lhs);
3740 if (cond == kCondBE) {
3741 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3742 // only the sltu instruction but no sleu.
3743 __ Xori(dst, dst, 1);
3744 }
3745 }
3746 break;
3747 }
3748}
3749
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003750bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3751 bool is64bit,
3752 LocationSummary* input_locations,
3753 GpuRegister dst) {
3754 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3755 Location rhs_location = input_locations->InAt(1);
3756 GpuRegister rhs_reg = ZERO;
3757 int64_t rhs_imm = 0;
3758 bool use_imm = rhs_location.IsConstant();
3759 if (use_imm) {
3760 if (is64bit) {
3761 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3762 } else {
3763 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3764 }
3765 } else {
3766 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3767 }
3768 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3769
3770 switch (cond) {
3771 case kCondEQ:
3772 case kCondNE:
3773 if (use_imm && IsInt<16>(-rhs_imm)) {
3774 if (is64bit) {
3775 __ Daddiu(dst, lhs, -rhs_imm);
3776 } else {
3777 __ Addiu(dst, lhs, -rhs_imm);
3778 }
3779 } else if (use_imm && IsUint<16>(rhs_imm)) {
3780 __ Xori(dst, lhs, rhs_imm);
3781 } else {
3782 if (use_imm) {
3783 rhs_reg = TMP;
3784 __ LoadConst64(rhs_reg, rhs_imm);
3785 }
3786 __ Xor(dst, lhs, rhs_reg);
3787 }
3788 return (cond == kCondEQ);
3789
3790 case kCondLT:
3791 case kCondGE:
3792 if (use_imm && IsInt<16>(rhs_imm)) {
3793 __ Slti(dst, lhs, rhs_imm);
3794 } else {
3795 if (use_imm) {
3796 rhs_reg = TMP;
3797 __ LoadConst64(rhs_reg, rhs_imm);
3798 }
3799 __ Slt(dst, lhs, rhs_reg);
3800 }
3801 return (cond == kCondGE);
3802
3803 case kCondLE:
3804 case kCondGT:
3805 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3806 // Simulate lhs <= rhs via lhs < rhs + 1.
3807 __ Slti(dst, lhs, rhs_imm_plus_one);
3808 return (cond == kCondGT);
3809 } else {
3810 if (use_imm) {
3811 rhs_reg = TMP;
3812 __ LoadConst64(rhs_reg, rhs_imm);
3813 }
3814 __ Slt(dst, rhs_reg, lhs);
3815 return (cond == kCondLE);
3816 }
3817
3818 case kCondB:
3819 case kCondAE:
3820 if (use_imm && IsInt<16>(rhs_imm)) {
3821 // Sltiu sign-extends its 16-bit immediate operand before
3822 // the comparison and thus lets us compare directly with
3823 // unsigned values in the ranges [0, 0x7fff] and
3824 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3825 __ Sltiu(dst, lhs, rhs_imm);
3826 } else {
3827 if (use_imm) {
3828 rhs_reg = TMP;
3829 __ LoadConst64(rhs_reg, rhs_imm);
3830 }
3831 __ Sltu(dst, lhs, rhs_reg);
3832 }
3833 return (cond == kCondAE);
3834
3835 case kCondBE:
3836 case kCondA:
3837 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3838 // Simulate lhs <= rhs via lhs < rhs + 1.
3839 // Note that this only works if rhs + 1 does not overflow
3840 // to 0, hence the check above.
3841 // Sltiu sign-extends its 16-bit immediate operand before
3842 // the comparison and thus lets us compare directly with
3843 // unsigned values in the ranges [0, 0x7fff] and
3844 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3845 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3846 return (cond == kCondA);
3847 } else {
3848 if (use_imm) {
3849 rhs_reg = TMP;
3850 __ LoadConst64(rhs_reg, rhs_imm);
3851 }
3852 __ Sltu(dst, rhs_reg, lhs);
3853 return (cond == kCondBE);
3854 }
3855 }
3856}
3857
Alexey Frunze299a9392015-12-08 16:08:02 -08003858void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3859 bool is64bit,
3860 LocationSummary* locations,
3861 Mips64Label* label) {
3862 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3863 Location rhs_location = locations->InAt(1);
3864 GpuRegister rhs_reg = ZERO;
3865 int64_t rhs_imm = 0;
3866 bool use_imm = rhs_location.IsConstant();
3867 if (use_imm) {
3868 if (is64bit) {
3869 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3870 } else {
3871 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3872 }
3873 } else {
3874 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3875 }
3876
3877 if (use_imm && rhs_imm == 0) {
3878 switch (cond) {
3879 case kCondEQ:
3880 case kCondBE: // <= 0 if zero
3881 __ Beqzc(lhs, label);
3882 break;
3883 case kCondNE:
3884 case kCondA: // > 0 if non-zero
3885 __ Bnezc(lhs, label);
3886 break;
3887 case kCondLT:
3888 __ Bltzc(lhs, label);
3889 break;
3890 case kCondGE:
3891 __ Bgezc(lhs, label);
3892 break;
3893 case kCondLE:
3894 __ Blezc(lhs, label);
3895 break;
3896 case kCondGT:
3897 __ Bgtzc(lhs, label);
3898 break;
3899 case kCondB: // always false
3900 break;
3901 case kCondAE: // always true
3902 __ Bc(label);
3903 break;
3904 }
3905 } else {
3906 if (use_imm) {
3907 rhs_reg = TMP;
3908 __ LoadConst64(rhs_reg, rhs_imm);
3909 }
3910 switch (cond) {
3911 case kCondEQ:
3912 __ Beqc(lhs, rhs_reg, label);
3913 break;
3914 case kCondNE:
3915 __ Bnec(lhs, rhs_reg, label);
3916 break;
3917 case kCondLT:
3918 __ Bltc(lhs, rhs_reg, label);
3919 break;
3920 case kCondGE:
3921 __ Bgec(lhs, rhs_reg, label);
3922 break;
3923 case kCondLE:
3924 __ Bgec(rhs_reg, lhs, label);
3925 break;
3926 case kCondGT:
3927 __ Bltc(rhs_reg, lhs, label);
3928 break;
3929 case kCondB:
3930 __ Bltuc(lhs, rhs_reg, label);
3931 break;
3932 case kCondAE:
3933 __ Bgeuc(lhs, rhs_reg, label);
3934 break;
3935 case kCondBE:
3936 __ Bgeuc(rhs_reg, lhs, label);
3937 break;
3938 case kCondA:
3939 __ Bltuc(rhs_reg, lhs, label);
3940 break;
3941 }
3942 }
3943}
3944
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003945void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3946 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003947 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003948 LocationSummary* locations) {
3949 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3950 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3951 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003952 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003953 switch (cond) {
3954 case kCondEQ:
3955 __ CmpEqS(FTMP, lhs, rhs);
3956 __ Mfc1(dst, FTMP);
3957 __ Andi(dst, dst, 1);
3958 break;
3959 case kCondNE:
3960 __ CmpEqS(FTMP, lhs, rhs);
3961 __ Mfc1(dst, FTMP);
3962 __ Addiu(dst, dst, 1);
3963 break;
3964 case kCondLT:
3965 if (gt_bias) {
3966 __ CmpLtS(FTMP, lhs, rhs);
3967 } else {
3968 __ CmpUltS(FTMP, lhs, rhs);
3969 }
3970 __ Mfc1(dst, FTMP);
3971 __ Andi(dst, dst, 1);
3972 break;
3973 case kCondLE:
3974 if (gt_bias) {
3975 __ CmpLeS(FTMP, lhs, rhs);
3976 } else {
3977 __ CmpUleS(FTMP, lhs, rhs);
3978 }
3979 __ Mfc1(dst, FTMP);
3980 __ Andi(dst, dst, 1);
3981 break;
3982 case kCondGT:
3983 if (gt_bias) {
3984 __ CmpUltS(FTMP, rhs, lhs);
3985 } else {
3986 __ CmpLtS(FTMP, rhs, lhs);
3987 }
3988 __ Mfc1(dst, FTMP);
3989 __ Andi(dst, dst, 1);
3990 break;
3991 case kCondGE:
3992 if (gt_bias) {
3993 __ CmpUleS(FTMP, rhs, lhs);
3994 } else {
3995 __ CmpLeS(FTMP, rhs, lhs);
3996 }
3997 __ Mfc1(dst, FTMP);
3998 __ Andi(dst, dst, 1);
3999 break;
4000 default:
4001 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4002 UNREACHABLE();
4003 }
4004 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004005 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004006 switch (cond) {
4007 case kCondEQ:
4008 __ CmpEqD(FTMP, lhs, rhs);
4009 __ Mfc1(dst, FTMP);
4010 __ Andi(dst, dst, 1);
4011 break;
4012 case kCondNE:
4013 __ CmpEqD(FTMP, lhs, rhs);
4014 __ Mfc1(dst, FTMP);
4015 __ Addiu(dst, dst, 1);
4016 break;
4017 case kCondLT:
4018 if (gt_bias) {
4019 __ CmpLtD(FTMP, lhs, rhs);
4020 } else {
4021 __ CmpUltD(FTMP, lhs, rhs);
4022 }
4023 __ Mfc1(dst, FTMP);
4024 __ Andi(dst, dst, 1);
4025 break;
4026 case kCondLE:
4027 if (gt_bias) {
4028 __ CmpLeD(FTMP, lhs, rhs);
4029 } else {
4030 __ CmpUleD(FTMP, lhs, rhs);
4031 }
4032 __ Mfc1(dst, FTMP);
4033 __ Andi(dst, dst, 1);
4034 break;
4035 case kCondGT:
4036 if (gt_bias) {
4037 __ CmpUltD(FTMP, rhs, lhs);
4038 } else {
4039 __ CmpLtD(FTMP, rhs, lhs);
4040 }
4041 __ Mfc1(dst, FTMP);
4042 __ Andi(dst, dst, 1);
4043 break;
4044 case kCondGE:
4045 if (gt_bias) {
4046 __ CmpUleD(FTMP, rhs, lhs);
4047 } else {
4048 __ CmpLeD(FTMP, rhs, lhs);
4049 }
4050 __ Mfc1(dst, FTMP);
4051 __ Andi(dst, dst, 1);
4052 break;
4053 default:
4054 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4055 UNREACHABLE();
4056 }
4057 }
4058}
4059
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004060bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4061 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004062 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004063 LocationSummary* input_locations,
4064 FpuRegister dst) {
4065 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4066 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004067 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004068 switch (cond) {
4069 case kCondEQ:
4070 __ CmpEqS(dst, lhs, rhs);
4071 return false;
4072 case kCondNE:
4073 __ CmpEqS(dst, lhs, rhs);
4074 return true;
4075 case kCondLT:
4076 if (gt_bias) {
4077 __ CmpLtS(dst, lhs, rhs);
4078 } else {
4079 __ CmpUltS(dst, lhs, rhs);
4080 }
4081 return false;
4082 case kCondLE:
4083 if (gt_bias) {
4084 __ CmpLeS(dst, lhs, rhs);
4085 } else {
4086 __ CmpUleS(dst, lhs, rhs);
4087 }
4088 return false;
4089 case kCondGT:
4090 if (gt_bias) {
4091 __ CmpUltS(dst, rhs, lhs);
4092 } else {
4093 __ CmpLtS(dst, rhs, lhs);
4094 }
4095 return false;
4096 case kCondGE:
4097 if (gt_bias) {
4098 __ CmpUleS(dst, rhs, lhs);
4099 } else {
4100 __ CmpLeS(dst, rhs, lhs);
4101 }
4102 return false;
4103 default:
4104 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4105 UNREACHABLE();
4106 }
4107 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004108 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004109 switch (cond) {
4110 case kCondEQ:
4111 __ CmpEqD(dst, lhs, rhs);
4112 return false;
4113 case kCondNE:
4114 __ CmpEqD(dst, lhs, rhs);
4115 return true;
4116 case kCondLT:
4117 if (gt_bias) {
4118 __ CmpLtD(dst, lhs, rhs);
4119 } else {
4120 __ CmpUltD(dst, lhs, rhs);
4121 }
4122 return false;
4123 case kCondLE:
4124 if (gt_bias) {
4125 __ CmpLeD(dst, lhs, rhs);
4126 } else {
4127 __ CmpUleD(dst, lhs, rhs);
4128 }
4129 return false;
4130 case kCondGT:
4131 if (gt_bias) {
4132 __ CmpUltD(dst, rhs, lhs);
4133 } else {
4134 __ CmpLtD(dst, rhs, lhs);
4135 }
4136 return false;
4137 case kCondGE:
4138 if (gt_bias) {
4139 __ CmpUleD(dst, rhs, lhs);
4140 } else {
4141 __ CmpLeD(dst, rhs, lhs);
4142 }
4143 return false;
4144 default:
4145 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4146 UNREACHABLE();
4147 }
4148 }
4149}
4150
Alexey Frunze299a9392015-12-08 16:08:02 -08004151void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4152 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004153 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004154 LocationSummary* locations,
4155 Mips64Label* label) {
4156 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4157 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004158 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004159 switch (cond) {
4160 case kCondEQ:
4161 __ CmpEqS(FTMP, lhs, rhs);
4162 __ Bc1nez(FTMP, label);
4163 break;
4164 case kCondNE:
4165 __ CmpEqS(FTMP, lhs, rhs);
4166 __ Bc1eqz(FTMP, label);
4167 break;
4168 case kCondLT:
4169 if (gt_bias) {
4170 __ CmpLtS(FTMP, lhs, rhs);
4171 } else {
4172 __ CmpUltS(FTMP, lhs, rhs);
4173 }
4174 __ Bc1nez(FTMP, label);
4175 break;
4176 case kCondLE:
4177 if (gt_bias) {
4178 __ CmpLeS(FTMP, lhs, rhs);
4179 } else {
4180 __ CmpUleS(FTMP, lhs, rhs);
4181 }
4182 __ Bc1nez(FTMP, label);
4183 break;
4184 case kCondGT:
4185 if (gt_bias) {
4186 __ CmpUltS(FTMP, rhs, lhs);
4187 } else {
4188 __ CmpLtS(FTMP, rhs, lhs);
4189 }
4190 __ Bc1nez(FTMP, label);
4191 break;
4192 case kCondGE:
4193 if (gt_bias) {
4194 __ CmpUleS(FTMP, rhs, lhs);
4195 } else {
4196 __ CmpLeS(FTMP, rhs, lhs);
4197 }
4198 __ Bc1nez(FTMP, label);
4199 break;
4200 default:
4201 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004202 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004203 }
4204 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004206 switch (cond) {
4207 case kCondEQ:
4208 __ CmpEqD(FTMP, lhs, rhs);
4209 __ Bc1nez(FTMP, label);
4210 break;
4211 case kCondNE:
4212 __ CmpEqD(FTMP, lhs, rhs);
4213 __ Bc1eqz(FTMP, label);
4214 break;
4215 case kCondLT:
4216 if (gt_bias) {
4217 __ CmpLtD(FTMP, lhs, rhs);
4218 } else {
4219 __ CmpUltD(FTMP, lhs, rhs);
4220 }
4221 __ Bc1nez(FTMP, label);
4222 break;
4223 case kCondLE:
4224 if (gt_bias) {
4225 __ CmpLeD(FTMP, lhs, rhs);
4226 } else {
4227 __ CmpUleD(FTMP, lhs, rhs);
4228 }
4229 __ Bc1nez(FTMP, label);
4230 break;
4231 case kCondGT:
4232 if (gt_bias) {
4233 __ CmpUltD(FTMP, rhs, lhs);
4234 } else {
4235 __ CmpLtD(FTMP, rhs, lhs);
4236 }
4237 __ Bc1nez(FTMP, label);
4238 break;
4239 case kCondGE:
4240 if (gt_bias) {
4241 __ CmpUleD(FTMP, rhs, lhs);
4242 } else {
4243 __ CmpLeD(FTMP, rhs, lhs);
4244 }
4245 __ Bc1nez(FTMP, label);
4246 break;
4247 default:
4248 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004249 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004250 }
4251 }
4252}
4253
Alexey Frunze4dda3372015-06-01 18:31:49 -07004254void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004255 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004256 Mips64Label* true_target,
4257 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004258 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004259
David Brazdil0debae72015-11-12 18:37:00 +00004260 if (true_target == nullptr && false_target == nullptr) {
4261 // Nothing to do. The code always falls through.
4262 return;
4263 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004264 // Constant condition, statically compared against "true" (integer value 1).
4265 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004266 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004267 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004268 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004269 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004270 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004271 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004272 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004273 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004274 }
David Brazdil0debae72015-11-12 18:37:00 +00004275 return;
4276 }
4277
4278 // The following code generates these patterns:
4279 // (1) true_target == nullptr && false_target != nullptr
4280 // - opposite condition true => branch to false_target
4281 // (2) true_target != nullptr && false_target == nullptr
4282 // - condition true => branch to true_target
4283 // (3) true_target != nullptr && false_target != nullptr
4284 // - condition true => branch to true_target
4285 // - branch to false_target
4286 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004287 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004288 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004289 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004290 if (true_target == nullptr) {
4291 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4292 } else {
4293 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4294 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004295 } else {
4296 // The condition instruction has not been materialized, use its inputs as
4297 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004298 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004299 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004300 LocationSummary* locations = cond->GetLocations();
4301 IfCondition if_cond = condition->GetCondition();
4302 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004303
David Brazdil0debae72015-11-12 18:37:00 +00004304 if (true_target == nullptr) {
4305 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004306 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004307 }
4308
Alexey Frunze299a9392015-12-08 16:08:02 -08004309 switch (type) {
4310 default:
4311 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4312 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004313 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004314 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4315 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004316 case DataType::Type::kFloat32:
4317 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004318 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4319 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004320 }
4321 }
David Brazdil0debae72015-11-12 18:37:00 +00004322
4323 // If neither branch falls through (case 3), the conditional branch to `true_target`
4324 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4325 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004326 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004327 }
4328}
4329
4330void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004331 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004332 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004333 locations->SetInAt(0, Location::RequiresRegister());
4334 }
4335}
4336
4337void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004338 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4339 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004340 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004341 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004342 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004343 nullptr : codegen_->GetLabelOf(false_successor);
4344 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004345}
4346
4347void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004348 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004349 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004350 InvokeRuntimeCallingConvention calling_convention;
4351 RegisterSet caller_saves = RegisterSet::Empty();
4352 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4353 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004354 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004355 locations->SetInAt(0, Location::RequiresRegister());
4356 }
4357}
4358
4359void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004360 SlowPathCodeMIPS64* slow_path =
4361 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004362 GenerateTestAndBranch(deoptimize,
4363 /* condition_input_index */ 0,
4364 slow_path->GetEntryLabel(),
4365 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004366}
4367
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004368// This function returns true if a conditional move can be generated for HSelect.
4369// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4370// branches and regular moves.
4371//
4372// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4373//
4374// While determining feasibility of a conditional move and setting inputs/outputs
4375// are two distinct tasks, this function does both because they share quite a bit
4376// of common logic.
4377static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4378 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4379 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4380 HCondition* condition = cond->AsCondition();
4381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004382 DataType::Type cond_type =
4383 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4384 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004385
4386 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4387 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4388 bool is_true_value_zero_constant =
4389 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4390 bool is_false_value_zero_constant =
4391 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4392
4393 bool can_move_conditionally = false;
4394 bool use_const_for_false_in = false;
4395 bool use_const_for_true_in = false;
4396
4397 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004398 if (!DataType::IsFloatingPointType(cond_type)) {
4399 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004400 // Moving int/long on int/long condition.
4401 if (is_true_value_zero_constant) {
4402 // seleqz out_reg, false_reg, cond_reg
4403 can_move_conditionally = true;
4404 use_const_for_true_in = true;
4405 } else if (is_false_value_zero_constant) {
4406 // selnez out_reg, true_reg, cond_reg
4407 can_move_conditionally = true;
4408 use_const_for_false_in = true;
4409 } else if (materialized) {
4410 // Not materializing unmaterialized int conditions
4411 // to keep the instruction count low.
4412 // selnez AT, true_reg, cond_reg
4413 // seleqz TMP, false_reg, cond_reg
4414 // or out_reg, AT, TMP
4415 can_move_conditionally = true;
4416 }
4417 } else {
4418 // Moving float/double on int/long condition.
4419 if (materialized) {
4420 // Not materializing unmaterialized int conditions
4421 // to keep the instruction count low.
4422 can_move_conditionally = true;
4423 if (is_true_value_zero_constant) {
4424 // sltu TMP, ZERO, cond_reg
4425 // mtc1 TMP, temp_cond_reg
4426 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4427 use_const_for_true_in = true;
4428 } else if (is_false_value_zero_constant) {
4429 // sltu TMP, ZERO, cond_reg
4430 // mtc1 TMP, temp_cond_reg
4431 // selnez.fmt out_reg, true_reg, temp_cond_reg
4432 use_const_for_false_in = true;
4433 } else {
4434 // sltu TMP, ZERO, cond_reg
4435 // mtc1 TMP, temp_cond_reg
4436 // sel.fmt temp_cond_reg, false_reg, true_reg
4437 // mov.fmt out_reg, temp_cond_reg
4438 }
4439 }
4440 }
4441 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004442 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004443 // Moving int/long on float/double condition.
4444 can_move_conditionally = true;
4445 if (is_true_value_zero_constant) {
4446 // mfc1 TMP, temp_cond_reg
4447 // seleqz out_reg, false_reg, TMP
4448 use_const_for_true_in = true;
4449 } else if (is_false_value_zero_constant) {
4450 // mfc1 TMP, temp_cond_reg
4451 // selnez out_reg, true_reg, TMP
4452 use_const_for_false_in = true;
4453 } else {
4454 // mfc1 TMP, temp_cond_reg
4455 // selnez AT, true_reg, TMP
4456 // seleqz TMP, false_reg, TMP
4457 // or out_reg, AT, TMP
4458 }
4459 } else {
4460 // Moving float/double on float/double condition.
4461 can_move_conditionally = true;
4462 if (is_true_value_zero_constant) {
4463 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4464 use_const_for_true_in = true;
4465 } else if (is_false_value_zero_constant) {
4466 // selnez.fmt out_reg, true_reg, temp_cond_reg
4467 use_const_for_false_in = true;
4468 } else {
4469 // sel.fmt temp_cond_reg, false_reg, true_reg
4470 // mov.fmt out_reg, temp_cond_reg
4471 }
4472 }
4473 }
4474 }
4475
4476 if (can_move_conditionally) {
4477 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4478 } else {
4479 DCHECK(!use_const_for_false_in);
4480 DCHECK(!use_const_for_true_in);
4481 }
4482
4483 if (locations_to_set != nullptr) {
4484 if (use_const_for_false_in) {
4485 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4486 } else {
4487 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004488 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004489 ? Location::RequiresFpuRegister()
4490 : Location::RequiresRegister());
4491 }
4492 if (use_const_for_true_in) {
4493 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4494 } else {
4495 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004496 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004497 ? Location::RequiresFpuRegister()
4498 : Location::RequiresRegister());
4499 }
4500 if (materialized) {
4501 locations_to_set->SetInAt(2, Location::RequiresRegister());
4502 }
4503
4504 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004505 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004506 ? Location::RequiresFpuRegister()
4507 : Location::RequiresRegister());
4508 } else {
4509 locations_to_set->SetOut(Location::SameAsFirstInput());
4510 }
4511 }
4512
4513 return can_move_conditionally;
4514}
4515
4516
4517void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4518 LocationSummary* locations = select->GetLocations();
4519 Location dst = locations->Out();
4520 Location false_src = locations->InAt(0);
4521 Location true_src = locations->InAt(1);
4522 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4523 GpuRegister cond_reg = TMP;
4524 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004525 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004526 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004527 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004528
4529 if (IsBooleanValueOrMaterializedCondition(cond)) {
4530 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4531 } else {
4532 HCondition* condition = cond->AsCondition();
4533 LocationSummary* cond_locations = cond->GetLocations();
4534 IfCondition if_cond = condition->GetCondition();
4535 cond_type = condition->InputAt(0)->GetType();
4536 switch (cond_type) {
4537 default:
4538 cond_inverted = MaterializeIntLongCompare(if_cond,
4539 /* is64bit */ false,
4540 cond_locations,
4541 cond_reg);
4542 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004543 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004544 cond_inverted = MaterializeIntLongCompare(if_cond,
4545 /* is64bit */ true,
4546 cond_locations,
4547 cond_reg);
4548 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004549 case DataType::Type::kFloat32:
4550 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004551 cond_inverted = MaterializeFpCompare(if_cond,
4552 condition->IsGtBias(),
4553 cond_type,
4554 cond_locations,
4555 fcond_reg);
4556 break;
4557 }
4558 }
4559
4560 if (true_src.IsConstant()) {
4561 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4562 }
4563 if (false_src.IsConstant()) {
4564 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4565 }
4566
4567 switch (dst_type) {
4568 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004570 __ Mfc1(cond_reg, fcond_reg);
4571 }
4572 if (true_src.IsConstant()) {
4573 if (cond_inverted) {
4574 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4575 } else {
4576 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4577 }
4578 } else if (false_src.IsConstant()) {
4579 if (cond_inverted) {
4580 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4581 } else {
4582 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4583 }
4584 } else {
4585 DCHECK_NE(cond_reg, AT);
4586 if (cond_inverted) {
4587 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4588 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4589 } else {
4590 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4591 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4592 }
4593 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4594 }
4595 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004596 case DataType::Type::kFloat32: {
4597 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004598 // sel*.fmt tests bit 0 of the condition register, account for that.
4599 __ Sltu(TMP, ZERO, cond_reg);
4600 __ Mtc1(TMP, fcond_reg);
4601 }
4602 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4603 if (true_src.IsConstant()) {
4604 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4605 if (cond_inverted) {
4606 __ SelnezS(dst_reg, src_reg, fcond_reg);
4607 } else {
4608 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4609 }
4610 } else if (false_src.IsConstant()) {
4611 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4612 if (cond_inverted) {
4613 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4614 } else {
4615 __ SelnezS(dst_reg, src_reg, fcond_reg);
4616 }
4617 } else {
4618 if (cond_inverted) {
4619 __ SelS(fcond_reg,
4620 true_src.AsFpuRegister<FpuRegister>(),
4621 false_src.AsFpuRegister<FpuRegister>());
4622 } else {
4623 __ SelS(fcond_reg,
4624 false_src.AsFpuRegister<FpuRegister>(),
4625 true_src.AsFpuRegister<FpuRegister>());
4626 }
4627 __ MovS(dst_reg, fcond_reg);
4628 }
4629 break;
4630 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004631 case DataType::Type::kFloat64: {
4632 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004633 // sel*.fmt tests bit 0 of the condition register, account for that.
4634 __ Sltu(TMP, ZERO, cond_reg);
4635 __ Mtc1(TMP, fcond_reg);
4636 }
4637 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4638 if (true_src.IsConstant()) {
4639 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4640 if (cond_inverted) {
4641 __ SelnezD(dst_reg, src_reg, fcond_reg);
4642 } else {
4643 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4644 }
4645 } else if (false_src.IsConstant()) {
4646 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4647 if (cond_inverted) {
4648 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4649 } else {
4650 __ SelnezD(dst_reg, src_reg, fcond_reg);
4651 }
4652 } else {
4653 if (cond_inverted) {
4654 __ SelD(fcond_reg,
4655 true_src.AsFpuRegister<FpuRegister>(),
4656 false_src.AsFpuRegister<FpuRegister>());
4657 } else {
4658 __ SelD(fcond_reg,
4659 false_src.AsFpuRegister<FpuRegister>(),
4660 true_src.AsFpuRegister<FpuRegister>());
4661 }
4662 __ MovD(dst_reg, fcond_reg);
4663 }
4664 break;
4665 }
4666 }
4667}
4668
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004669void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004670 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004671 LocationSummary(flag, LocationSummary::kNoCall);
4672 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004673}
4674
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004675void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4676 __ LoadFromOffset(kLoadWord,
4677 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4678 SP,
4679 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004680}
4681
David Brazdil74eb1b22015-12-14 11:44:01 +00004682void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004683 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004684 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004685}
4686
4687void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004688 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4689 GenConditionalMove(select);
4690 } else {
4691 LocationSummary* locations = select->GetLocations();
4692 Mips64Label false_target;
4693 GenerateTestAndBranch(select,
4694 /* condition_input_index */ 2,
4695 /* true_target */ nullptr,
4696 &false_target);
4697 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4698 __ Bind(&false_target);
4699 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004700}
4701
David Srbecky0cf44932015-12-09 14:09:59 +00004702void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004703 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004704}
4705
David Srbeckyd28f4a02016-03-14 17:14:24 +00004706void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4707 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004708}
4709
4710void CodeGeneratorMIPS64::GenerateNop() {
4711 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004712}
4713
Alexey Frunze4dda3372015-06-01 18:31:49 -07004714void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004715 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004716 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004717 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004718 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004719 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004720 instruction,
4721 object_field_get_with_read_barrier
4722 ? LocationSummary::kCallOnSlowPath
4723 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004724 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4725 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4726 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004727 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004728 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004729 locations->SetOut(Location::RequiresFpuRegister());
4730 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004731 // The output overlaps in the case of an object field get with
4732 // read barriers enabled: we do not want the move to overwrite the
4733 // object's location, as we need it to emit the read barrier.
4734 locations->SetOut(Location::RequiresRegister(),
4735 object_field_get_with_read_barrier
4736 ? Location::kOutputOverlap
4737 : Location::kNoOutputOverlap);
4738 }
4739 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4740 // We need a temporary register for the read barrier marking slow
4741 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004742 if (!kBakerReadBarrierThunksEnableForFields) {
4743 locations->AddTemp(Location::RequiresRegister());
4744 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004745 }
4746}
4747
4748void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4749 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004750 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4751 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004752 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004753 Location obj_loc = locations->InAt(0);
4754 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4755 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004756 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004757 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004758 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004759 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4760
Alexey Frunze4dda3372015-06-01 18:31:49 -07004761 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004762 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004763 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004764 load_type = kLoadUnsignedByte;
4765 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004766 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004767 load_type = kLoadSignedByte;
4768 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004769 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004770 load_type = kLoadUnsignedHalfword;
4771 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004772 case DataType::Type::kInt16:
4773 load_type = kLoadSignedHalfword;
4774 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004775 case DataType::Type::kInt32:
4776 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004777 load_type = kLoadWord;
4778 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004779 case DataType::Type::kInt64:
4780 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004781 load_type = kLoadDoubleword;
4782 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004784 load_type = kLoadUnsignedWord;
4785 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004786 case DataType::Type::kUint32:
4787 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004788 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004789 LOG(FATAL) << "Unreachable type " << type;
4790 UNREACHABLE();
4791 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004793 DCHECK(dst_loc.IsRegister());
4794 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004796 // /* HeapReference<Object> */ dst = *(obj + offset)
4797 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004798 Location temp_loc =
4799 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004800 // Note that a potential implicit null check is handled in this
4801 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4802 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4803 dst_loc,
4804 obj,
4805 offset,
4806 temp_loc,
4807 /* needs_null_check */ true);
4808 if (is_volatile) {
4809 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4810 }
4811 } else {
4812 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4813 if (is_volatile) {
4814 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4815 }
4816 // If read barriers are enabled, emit read barriers other than
4817 // Baker's using a slow path (and also unpoison the loaded
4818 // reference, if heap poisoning is enabled).
4819 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4820 }
4821 } else {
4822 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4823 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004824 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004825 DCHECK(dst_loc.IsFpuRegister());
4826 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004827 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004828 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004829
Alexey Frunze15958152017-02-09 19:08:30 -08004830 // Memory barriers, in the case of references, are handled in the
4831 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004833 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004834 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004835}
4836
4837void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4838 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4839 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004840 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004841 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004842 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004843 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004844 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004845 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004846 }
4847}
4848
4849void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004850 const FieldInfo& field_info,
4851 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004853 LocationSummary* locations = instruction->GetLocations();
4854 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004855 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004856 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004857 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004858 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4859 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004860 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4861
Alexey Frunze4dda3372015-06-01 18:31:49 -07004862 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004863 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004864 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004866 store_type = kStoreByte;
4867 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004868 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004869 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004870 store_type = kStoreHalfword;
4871 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004872 case DataType::Type::kInt32:
4873 case DataType::Type::kFloat32:
4874 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004875 store_type = kStoreWord;
4876 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004877 case DataType::Type::kInt64:
4878 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004879 store_type = kStoreDoubleword;
4880 break;
Aart Bik66c158e2018-01-31 12:55:04 -08004881 case DataType::Type::kUint32:
4882 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004884 LOG(FATAL) << "Unreachable type " << type;
4885 UNREACHABLE();
4886 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004887
Alexey Frunze15958152017-02-09 19:08:30 -08004888 if (is_volatile) {
4889 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4890 }
4891
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004892 if (value_location.IsConstant()) {
4893 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4894 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4895 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004896 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004897 DCHECK(value_location.IsRegister());
4898 GpuRegister src = value_location.AsRegister<GpuRegister>();
4899 if (kPoisonHeapReferences && needs_write_barrier) {
4900 // Note that in the case where `value` is a null reference,
4901 // we do not enter this block, as a null reference does not
4902 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004903 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004904 __ PoisonHeapReference(TMP, src);
4905 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4906 } else {
4907 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4908 }
4909 } else {
4910 DCHECK(value_location.IsFpuRegister());
4911 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4912 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4913 }
4914 }
Alexey Frunze15958152017-02-09 19:08:30 -08004915
Alexey Frunzec061de12017-02-14 13:27:23 -08004916 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004917 DCHECK(value_location.IsRegister());
4918 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004919 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004920 }
Alexey Frunze15958152017-02-09 19:08:30 -08004921
4922 if (is_volatile) {
4923 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4924 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004925}
4926
4927void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4928 HandleFieldGet(instruction, instruction->GetFieldInfo());
4929}
4930
4931void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4932 HandleFieldGet(instruction, instruction->GetFieldInfo());
4933}
4934
4935void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4936 HandleFieldSet(instruction, instruction->GetFieldInfo());
4937}
4938
4939void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004940 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004941}
4942
Alexey Frunze15958152017-02-09 19:08:30 -08004943void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4944 HInstruction* instruction,
4945 Location out,
4946 uint32_t offset,
4947 Location maybe_temp,
4948 ReadBarrierOption read_barrier_option) {
4949 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4950 if (read_barrier_option == kWithReadBarrier) {
4951 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004952 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4953 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4954 }
Alexey Frunze15958152017-02-09 19:08:30 -08004955 if (kUseBakerReadBarrier) {
4956 // Load with fast path based Baker's read barrier.
4957 // /* HeapReference<Object> */ out = *(out + offset)
4958 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4959 out,
4960 out_reg,
4961 offset,
4962 maybe_temp,
4963 /* needs_null_check */ false);
4964 } else {
4965 // Load with slow path based read barrier.
4966 // Save the value of `out` into `maybe_temp` before overwriting it
4967 // in the following move operation, as we will need it for the
4968 // read barrier below.
4969 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4970 // /* HeapReference<Object> */ out = *(out + offset)
4971 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4972 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4973 }
4974 } else {
4975 // Plain load with no read barrier.
4976 // /* HeapReference<Object> */ out = *(out + offset)
4977 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4978 __ MaybeUnpoisonHeapReference(out_reg);
4979 }
4980}
4981
4982void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4983 HInstruction* instruction,
4984 Location out,
4985 Location obj,
4986 uint32_t offset,
4987 Location maybe_temp,
4988 ReadBarrierOption read_barrier_option) {
4989 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4990 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4991 if (read_barrier_option == kWithReadBarrier) {
4992 CHECK(kEmitCompilerReadBarrier);
4993 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004994 if (!kBakerReadBarrierThunksEnableForFields) {
4995 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4996 }
Alexey Frunze15958152017-02-09 19:08:30 -08004997 // Load with fast path based Baker's read barrier.
4998 // /* HeapReference<Object> */ out = *(obj + offset)
4999 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5000 out,
5001 obj_reg,
5002 offset,
5003 maybe_temp,
5004 /* needs_null_check */ false);
5005 } else {
5006 // Load with slow path based read barrier.
5007 // /* HeapReference<Object> */ out = *(obj + offset)
5008 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5009 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5010 }
5011 } else {
5012 // Plain load with no read barrier.
5013 // /* HeapReference<Object> */ out = *(obj + offset)
5014 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5015 __ MaybeUnpoisonHeapReference(out_reg);
5016 }
5017}
5018
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005019static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5020 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5021 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5022 return reg - V0;
5023 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5024 return 13 + (reg - S2);
5025 } else if (reg == S8) { // One more.
5026 return 19;
5027 }
5028 LOG(FATAL) << "Unexpected register " << reg;
5029 UNREACHABLE();
5030}
5031
5032static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5033 int num = GetBakerMarkThunkNumber(reg) +
5034 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5035 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5036}
5037
5038static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5039 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5040 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5041}
5042
5043void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5044 Location root,
5045 GpuRegister obj,
5046 uint32_t offset,
5047 ReadBarrierOption read_barrier_option,
5048 Mips64Label* label_low) {
5049 if (label_low != nullptr) {
5050 DCHECK_EQ(offset, 0x5678u);
5051 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005052 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005053 if (read_barrier_option == kWithReadBarrier) {
5054 DCHECK(kEmitCompilerReadBarrier);
5055 if (kUseBakerReadBarrier) {
5056 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5057 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005058 if (kBakerReadBarrierThunksEnableForGcRoots) {
5059 // Note that we do not actually check the value of `GetIsGcMarking()`
5060 // to decide whether to mark the loaded GC root or not. Instead, we
5061 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5062 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5063 // vice versa.
5064 //
5065 // We use thunks for the slow path. That thunk checks the reference
5066 // and jumps to the entrypoint if needed.
5067 //
5068 // temp = Thread::Current()->pReadBarrierMarkReg00
5069 // // AKA &art_quick_read_barrier_mark_introspection.
5070 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5071 // if (temp != nullptr) {
5072 // temp = &gc_root_thunk<root_reg>
5073 // root = temp(root)
5074 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005075
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005076 const int32_t entry_point_offset =
5077 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5078 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5079 int16_t offset_low = Low16Bits(offset);
5080 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5081 // extension in lwu.
5082 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5083 GpuRegister base = short_offset ? obj : TMP;
5084 // Loading the entrypoint does not require a load acquire since it is only changed when
5085 // threads are suspended or running a checkpoint.
5086 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5087 if (!short_offset) {
5088 DCHECK(!label_low);
5089 __ Daui(base, obj, offset_high);
5090 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005091 Mips64Label skip_call;
5092 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005093 if (label_low != nullptr) {
5094 DCHECK(short_offset);
5095 __ Bind(label_low);
5096 }
5097 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5098 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5099 // in delay slot.
5100 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005101 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005102 } else {
5103 // Note that we do not actually check the value of `GetIsGcMarking()`
5104 // to decide whether to mark the loaded GC root or not. Instead, we
5105 // load into `temp` (T9) the read barrier mark entry point corresponding
5106 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5107 // is false, and vice versa.
5108 //
5109 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5110 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5111 // if (temp != null) {
5112 // root = temp(root)
5113 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005114
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005115 if (label_low != nullptr) {
5116 __ Bind(label_low);
5117 }
5118 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5119 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5120 static_assert(
5121 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5122 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5123 "have different sizes.");
5124 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5125 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5126 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005127
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005128 // Slow path marking the GC root `root`.
5129 Location temp = Location::RegisterLocation(T9);
5130 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005131 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005132 instruction,
5133 root,
5134 /*entrypoint*/ temp);
5135 codegen_->AddSlowPath(slow_path);
5136
5137 const int32_t entry_point_offset =
5138 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5139 // Loading the entrypoint does not require a load acquire since it is only changed when
5140 // threads are suspended or running a checkpoint.
5141 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5142 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5143 __ Bind(slow_path->GetExitLabel());
5144 }
Alexey Frunze15958152017-02-09 19:08:30 -08005145 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005146 if (label_low != nullptr) {
5147 __ Bind(label_low);
5148 }
Alexey Frunze15958152017-02-09 19:08:30 -08005149 // GC root loaded through a slow path for read barriers other
5150 // than Baker's.
5151 // /* GcRoot<mirror::Object>* */ root = obj + offset
5152 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5153 // /* mirror::Object* */ root = root->Read()
5154 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5155 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005156 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005157 if (label_low != nullptr) {
5158 __ Bind(label_low);
5159 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005160 // Plain GC root load with no read barrier.
5161 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5162 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5163 // Note that GC roots are not affected by heap poisoning, thus we
5164 // do not have to unpoison `root_reg` here.
5165 }
5166}
5167
Alexey Frunze15958152017-02-09 19:08:30 -08005168void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5169 Location ref,
5170 GpuRegister obj,
5171 uint32_t offset,
5172 Location temp,
5173 bool needs_null_check) {
5174 DCHECK(kEmitCompilerReadBarrier);
5175 DCHECK(kUseBakerReadBarrier);
5176
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005177 if (kBakerReadBarrierThunksEnableForFields) {
5178 // Note that we do not actually check the value of `GetIsGcMarking()`
5179 // to decide whether to mark the loaded reference or not. Instead, we
5180 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5181 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5182 // vice versa.
5183 //
5184 // We use thunks for the slow path. That thunk checks the reference
5185 // and jumps to the entrypoint if needed. If the holder is not gray,
5186 // it issues a load-load memory barrier and returns to the original
5187 // reference load.
5188 //
5189 // temp = Thread::Current()->pReadBarrierMarkReg00
5190 // // AKA &art_quick_read_barrier_mark_introspection.
5191 // if (temp != nullptr) {
5192 // temp = &field_array_thunk<holder_reg>
5193 // temp()
5194 // }
5195 // not_gray_return_address:
5196 // // If the offset is too large to fit into the lw instruction, we
5197 // // use an adjusted base register (TMP) here. This register
5198 // // receives bits 16 ... 31 of the offset before the thunk invocation
5199 // // and the thunk benefits from it.
5200 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5201 // gray_return_address:
5202
5203 DCHECK(temp.IsInvalid());
5204 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5205 const int32_t entry_point_offset =
5206 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5207 // There may have or may have not been a null check if the field offset is smaller than
5208 // the page size.
5209 // There must've been a null check in case it's actually a load from an array.
5210 // We will, however, perform an explicit null check in the thunk as it's easier to
5211 // do it than not.
5212 if (instruction->IsArrayGet()) {
5213 DCHECK(!needs_null_check);
5214 }
5215 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5216 // Loading the entrypoint does not require a load acquire since it is only changed when
5217 // threads are suspended or running a checkpoint.
5218 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5219 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005220 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005221 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005222 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005223 __ Nop(); // In forbidden slot.
5224 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005225 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005226 // /* HeapReference<Object> */ ref = *(obj + offset)
5227 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5228 } else {
5229 int16_t offset_low = Low16Bits(offset);
5230 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005231 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005232 __ Daui(TMP, obj, offset_high); // In delay slot.
5233 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005234 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005235 // /* HeapReference<Object> */ ref = *(obj + offset)
5236 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5237 }
5238 if (needs_null_check) {
5239 MaybeRecordImplicitNullCheck(instruction);
5240 }
5241 __ MaybeUnpoisonHeapReference(ref_reg);
5242 return;
5243 }
5244
Alexey Frunze15958152017-02-09 19:08:30 -08005245 // /* HeapReference<Object> */ ref = *(obj + offset)
5246 Location no_index = Location::NoLocation();
5247 ScaleFactor no_scale_factor = TIMES_1;
5248 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5249 ref,
5250 obj,
5251 offset,
5252 no_index,
5253 no_scale_factor,
5254 temp,
5255 needs_null_check);
5256}
5257
5258void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5259 Location ref,
5260 GpuRegister obj,
5261 uint32_t data_offset,
5262 Location index,
5263 Location temp,
5264 bool needs_null_check) {
5265 DCHECK(kEmitCompilerReadBarrier);
5266 DCHECK(kUseBakerReadBarrier);
5267
5268 static_assert(
5269 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5270 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005271 ScaleFactor scale_factor = TIMES_4;
5272
5273 if (kBakerReadBarrierThunksEnableForArrays) {
5274 // Note that we do not actually check the value of `GetIsGcMarking()`
5275 // to decide whether to mark the loaded reference or not. Instead, we
5276 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5277 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5278 // vice versa.
5279 //
5280 // We use thunks for the slow path. That thunk checks the reference
5281 // and jumps to the entrypoint if needed. If the holder is not gray,
5282 // it issues a load-load memory barrier and returns to the original
5283 // reference load.
5284 //
5285 // temp = Thread::Current()->pReadBarrierMarkReg00
5286 // // AKA &art_quick_read_barrier_mark_introspection.
5287 // if (temp != nullptr) {
5288 // temp = &field_array_thunk<holder_reg>
5289 // temp()
5290 // }
5291 // not_gray_return_address:
5292 // // The element address is pre-calculated in the TMP register before the
5293 // // thunk invocation and the thunk benefits from it.
5294 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5295 // gray_return_address:
5296
5297 DCHECK(temp.IsInvalid());
5298 DCHECK(index.IsValid());
5299 const int32_t entry_point_offset =
5300 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5301 // We will not do the explicit null check in the thunk as some form of a null check
5302 // must've been done earlier.
5303 DCHECK(!needs_null_check);
5304 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5305 // Loading the entrypoint does not require a load acquire since it is only changed when
5306 // threads are suspended or running a checkpoint.
5307 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005308 Mips64Label skip_call;
5309 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005310 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5311 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5312 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5313 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005314 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005315 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5316 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5317 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5318 __ MaybeUnpoisonHeapReference(ref_reg);
5319 return;
5320 }
5321
Alexey Frunze15958152017-02-09 19:08:30 -08005322 // /* HeapReference<Object> */ ref =
5323 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005324 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5325 ref,
5326 obj,
5327 data_offset,
5328 index,
5329 scale_factor,
5330 temp,
5331 needs_null_check);
5332}
5333
5334void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5335 Location ref,
5336 GpuRegister obj,
5337 uint32_t offset,
5338 Location index,
5339 ScaleFactor scale_factor,
5340 Location temp,
5341 bool needs_null_check,
5342 bool always_update_field) {
5343 DCHECK(kEmitCompilerReadBarrier);
5344 DCHECK(kUseBakerReadBarrier);
5345
5346 // In slow path based read barriers, the read barrier call is
5347 // inserted after the original load. However, in fast path based
5348 // Baker's read barriers, we need to perform the load of
5349 // mirror::Object::monitor_ *before* the original reference load.
5350 // This load-load ordering is required by the read barrier.
5351 // The fast path/slow path (for Baker's algorithm) should look like:
5352 //
5353 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5354 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5355 // HeapReference<Object> ref = *src; // Original reference load.
5356 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5357 // if (is_gray) {
5358 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5359 // }
5360 //
5361 // Note: the original implementation in ReadBarrier::Barrier is
5362 // slightly more complex as it performs additional checks that we do
5363 // not do here for performance reasons.
5364
5365 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5366 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5367 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5368
5369 // /* int32_t */ monitor = obj->monitor_
5370 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5371 if (needs_null_check) {
5372 MaybeRecordImplicitNullCheck(instruction);
5373 }
5374 // /* LockWord */ lock_word = LockWord(monitor)
5375 static_assert(sizeof(LockWord) == sizeof(int32_t),
5376 "art::LockWord and int32_t have different sizes.");
5377
5378 __ Sync(0); // Barrier to prevent load-load reordering.
5379
5380 // The actual reference load.
5381 if (index.IsValid()) {
5382 // Load types involving an "index": ArrayGet,
5383 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5384 // intrinsics.
5385 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5386 if (index.IsConstant()) {
5387 size_t computed_offset =
5388 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5389 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5390 } else {
5391 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005392 if (scale_factor == TIMES_1) {
5393 __ Daddu(TMP, index_reg, obj);
5394 } else {
5395 __ Dlsa(TMP, index_reg, obj, scale_factor);
5396 }
Alexey Frunze15958152017-02-09 19:08:30 -08005397 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5398 }
5399 } else {
5400 // /* HeapReference<Object> */ ref = *(obj + offset)
5401 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5402 }
5403
5404 // Object* ref = ref_addr->AsMirrorPtr()
5405 __ MaybeUnpoisonHeapReference(ref_reg);
5406
5407 // Slow path marking the object `ref` when it is gray.
5408 SlowPathCodeMIPS64* slow_path;
5409 if (always_update_field) {
5410 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5411 // of the form `obj + field_offset`, where `obj` is a register and
5412 // `field_offset` is a register. Thus `offset` and `scale_factor`
5413 // above are expected to be null in this code path.
5414 DCHECK_EQ(offset, 0u);
5415 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005416 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005417 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5418 ref,
5419 obj,
5420 /* field_offset */ index,
5421 temp_reg);
5422 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005423 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005424 }
5425 AddSlowPath(slow_path);
5426
5427 // if (rb_state == ReadBarrier::GrayState())
5428 // ref = ReadBarrier::Mark(ref);
5429 // Given the numeric representation, it's enough to check the low bit of the
5430 // rb_state. We do that by shifting the bit into the sign bit (31) and
5431 // performing a branch on less than zero.
5432 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5433 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5434 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5435 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5436 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5437 __ Bind(slow_path->GetExitLabel());
5438}
5439
5440void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5441 Location out,
5442 Location ref,
5443 Location obj,
5444 uint32_t offset,
5445 Location index) {
5446 DCHECK(kEmitCompilerReadBarrier);
5447
5448 // Insert a slow path based read barrier *after* the reference load.
5449 //
5450 // If heap poisoning is enabled, the unpoisoning of the loaded
5451 // reference will be carried out by the runtime within the slow
5452 // path.
5453 //
5454 // Note that `ref` currently does not get unpoisoned (when heap
5455 // poisoning is enabled), which is alright as the `ref` argument is
5456 // not used by the artReadBarrierSlow entry point.
5457 //
5458 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005459 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005460 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5461 AddSlowPath(slow_path);
5462
5463 __ Bc(slow_path->GetEntryLabel());
5464 __ Bind(slow_path->GetExitLabel());
5465}
5466
5467void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5468 Location out,
5469 Location ref,
5470 Location obj,
5471 uint32_t offset,
5472 Location index) {
5473 if (kEmitCompilerReadBarrier) {
5474 // Baker's read barriers shall be handled by the fast path
5475 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5476 DCHECK(!kUseBakerReadBarrier);
5477 // If heap poisoning is enabled, unpoisoning will be taken care of
5478 // by the runtime within the slow path.
5479 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5480 } else if (kPoisonHeapReferences) {
5481 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5482 }
5483}
5484
5485void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5486 Location out,
5487 Location root) {
5488 DCHECK(kEmitCompilerReadBarrier);
5489
5490 // Insert a slow path based read barrier *after* the GC root load.
5491 //
5492 // Note that GC roots are not affected by heap poisoning, so we do
5493 // not need to do anything special for this here.
5494 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005495 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005496 AddSlowPath(slow_path);
5497
5498 __ Bc(slow_path->GetEntryLabel());
5499 __ Bind(slow_path->GetExitLabel());
5500}
5501
Alexey Frunze4dda3372015-06-01 18:31:49 -07005502void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005503 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5504 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005505 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005506 switch (type_check_kind) {
5507 case TypeCheckKind::kExactCheck:
5508 case TypeCheckKind::kAbstractClassCheck:
5509 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005510 case TypeCheckKind::kArrayObjectCheck: {
5511 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5512 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5513 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005514 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005515 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005516 case TypeCheckKind::kArrayCheck:
5517 case TypeCheckKind::kUnresolvedCheck:
5518 case TypeCheckKind::kInterfaceCheck:
5519 call_kind = LocationSummary::kCallOnSlowPath;
5520 break;
5521 }
5522
Vladimir Markoca6fff82017-10-03 14:49:14 +01005523 LocationSummary* locations =
5524 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005525 if (baker_read_barrier_slow_path) {
5526 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5527 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005528 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005529 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005530 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005531 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005532 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005533 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005534}
5535
5536void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005537 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005538 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005539 Location obj_loc = locations->InAt(0);
5540 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005541 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005542 Location out_loc = locations->Out();
5543 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5544 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5545 DCHECK_LE(num_temps, 1u);
5546 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005547 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5548 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5549 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5550 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005551 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005552 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005553
5554 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005555 // Avoid this check if we know `obj` is not null.
5556 if (instruction->MustDoNullCheck()) {
5557 __ Move(out, ZERO);
5558 __ Beqzc(obj, &done);
5559 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005560
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005561 switch (type_check_kind) {
5562 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005563 ReadBarrierOption read_barrier_option =
5564 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005565 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005566 GenerateReferenceLoadTwoRegisters(instruction,
5567 out_loc,
5568 obj_loc,
5569 class_offset,
5570 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005571 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005572 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005573 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005574 __ Sltiu(out, out, 1);
5575 break;
5576 }
5577
5578 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005579 ReadBarrierOption read_barrier_option =
5580 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005581 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005582 GenerateReferenceLoadTwoRegisters(instruction,
5583 out_loc,
5584 obj_loc,
5585 class_offset,
5586 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005587 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005588 // If the class is abstract, we eagerly fetch the super class of the
5589 // object to avoid doing a comparison we know will fail.
5590 Mips64Label loop;
5591 __ Bind(&loop);
5592 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005593 GenerateReferenceLoadOneRegister(instruction,
5594 out_loc,
5595 super_offset,
5596 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005597 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005598 // If `out` is null, we use it for the result, and jump to `done`.
5599 __ Beqzc(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005600 __ Bnec(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005601 __ LoadConst32(out, 1);
5602 break;
5603 }
5604
5605 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005606 ReadBarrierOption read_barrier_option =
5607 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005608 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005609 GenerateReferenceLoadTwoRegisters(instruction,
5610 out_loc,
5611 obj_loc,
5612 class_offset,
5613 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005614 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005615 // Walk over the class hierarchy to find a match.
5616 Mips64Label loop, success;
5617 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005618 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005619 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005620 GenerateReferenceLoadOneRegister(instruction,
5621 out_loc,
5622 super_offset,
5623 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005624 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005625 __ Bnezc(out, &loop);
5626 // If `out` is null, we use it for the result, and jump to `done`.
5627 __ Bc(&done);
5628 __ Bind(&success);
5629 __ LoadConst32(out, 1);
5630 break;
5631 }
5632
5633 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005634 ReadBarrierOption read_barrier_option =
5635 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005636 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005637 GenerateReferenceLoadTwoRegisters(instruction,
5638 out_loc,
5639 obj_loc,
5640 class_offset,
5641 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005642 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005643 // Do an exact check.
5644 Mips64Label success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005645 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005646 // Otherwise, we need to check that the object's class is a non-primitive array.
5647 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005648 GenerateReferenceLoadOneRegister(instruction,
5649 out_loc,
5650 component_offset,
5651 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005652 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005653 // If `out` is null, we use it for the result, and jump to `done`.
5654 __ Beqzc(out, &done);
5655 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5656 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5657 __ Sltiu(out, out, 1);
5658 __ Bc(&done);
5659 __ Bind(&success);
5660 __ LoadConst32(out, 1);
5661 break;
5662 }
5663
5664 case TypeCheckKind::kArrayCheck: {
5665 // No read barrier since the slow path will retry upon failure.
5666 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005667 GenerateReferenceLoadTwoRegisters(instruction,
5668 out_loc,
5669 obj_loc,
5670 class_offset,
5671 maybe_temp_loc,
5672 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005673 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005674 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5675 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005676 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005677 __ Bnec(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005678 __ LoadConst32(out, 1);
5679 break;
5680 }
5681
5682 case TypeCheckKind::kUnresolvedCheck:
5683 case TypeCheckKind::kInterfaceCheck: {
5684 // Note that we indeed only call on slow path, but we always go
5685 // into the slow path for the unresolved and interface check
5686 // cases.
5687 //
5688 // We cannot directly call the InstanceofNonTrivial runtime
5689 // entry point without resorting to a type checking slow path
5690 // here (i.e. by calling InvokeRuntime directly), as it would
5691 // require to assign fixed registers for the inputs of this
5692 // HInstanceOf instruction (following the runtime calling
5693 // convention), which might be cluttered by the potential first
5694 // read barrier emission at the beginning of this method.
5695 //
5696 // TODO: Introduce a new runtime entry point taking the object
5697 // to test (instead of its class) as argument, and let it deal
5698 // with the read barrier issues. This will let us refactor this
5699 // case of the `switch` code as it was previously (with a direct
5700 // call to the runtime not using a type checking slow path).
5701 // This should also be beneficial for the other cases above.
5702 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005703 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5704 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005705 codegen_->AddSlowPath(slow_path);
5706 __ Bc(slow_path->GetEntryLabel());
5707 break;
5708 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005709 }
5710
5711 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005712
5713 if (slow_path != nullptr) {
5714 __ Bind(slow_path->GetExitLabel());
5715 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005716}
5717
5718void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005719 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005720 locations->SetOut(Location::ConstantLocation(constant));
5721}
5722
5723void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5724 // Will be generated at use site.
5725}
5726
5727void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005728 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005729 locations->SetOut(Location::ConstantLocation(constant));
5730}
5731
5732void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5733 // Will be generated at use site.
5734}
5735
Calin Juravle175dc732015-08-25 15:42:32 +01005736void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5737 // The trampoline uses the same calling convention as dex calling conventions,
5738 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5739 // the method_idx.
5740 HandleInvoke(invoke);
5741}
5742
5743void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5744 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5745}
5746
Alexey Frunze4dda3372015-06-01 18:31:49 -07005747void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5748 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5749 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5750}
5751
5752void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5753 HandleInvoke(invoke);
5754 // The register T0 is required to be used for the hidden argument in
5755 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5756 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5757}
5758
5759void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5760 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5761 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005762 Location receiver = invoke->GetLocations()->InAt(0);
5763 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005764 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005765
5766 // Set the hidden argument.
5767 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5768 invoke->GetDexMethodIndex());
5769
5770 // temp = object->GetClass();
5771 if (receiver.IsStackSlot()) {
5772 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5773 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5774 } else {
5775 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5776 }
5777 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005778 // Instead of simply (possibly) unpoisoning `temp` here, we should
5779 // emit a read barrier for the previous class reference load.
5780 // However this is not required in practice, as this is an
5781 // intermediate/temporary reference and because the current
5782 // concurrent copying collector keeps the from-space memory
5783 // intact/accessible until the end of the marking phase (the
5784 // concurrent copying collector may not in the future).
5785 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005786 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5787 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5788 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005789 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005790 // temp = temp->GetImtEntryAt(method_offset);
5791 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5792 // T9 = temp->GetEntryPoint();
5793 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5794 // T9();
5795 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005796 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005797 DCHECK(!codegen_->IsLeafMethod());
5798 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5799}
5800
5801void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005802 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5803 if (intrinsic.TryDispatch(invoke)) {
5804 return;
5805 }
5806
Alexey Frunze4dda3372015-06-01 18:31:49 -07005807 HandleInvoke(invoke);
5808}
5809
5810void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005811 // Explicit clinit checks triggered by static invokes must have been pruned by
5812 // art::PrepareForRegisterAllocation.
5813 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005814
Chris Larsen3039e382015-08-26 07:54:08 -07005815 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5816 if (intrinsic.TryDispatch(invoke)) {
5817 return;
5818 }
5819
Alexey Frunze4dda3372015-06-01 18:31:49 -07005820 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005821}
5822
Orion Hodsonac141392017-01-13 11:53:47 +00005823void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5824 HandleInvoke(invoke);
5825}
5826
5827void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5828 codegen_->GenerateInvokePolymorphicCall(invoke);
5829}
5830
Chris Larsen3039e382015-08-26 07:54:08 -07005831static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005832 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005833 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5834 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005835 return true;
5836 }
5837 return false;
5838}
5839
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005840HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005841 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005842 bool fallback_load = false;
5843 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005844 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005845 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005846 case HLoadString::LoadKind::kBssEntry:
5847 DCHECK(!Runtime::Current()->UseJitCompilation());
5848 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005849 case HLoadString::LoadKind::kJitTableAddress:
5850 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005851 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005852 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005853 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005854 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005855 }
5856 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005857 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005858 }
5859 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860}
5861
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005862HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5863 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005864 bool fallback_load = false;
5865 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005866 case HLoadClass::LoadKind::kInvalid:
5867 LOG(FATAL) << "UNREACHABLE";
5868 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005869 case HLoadClass::LoadKind::kReferrersClass:
5870 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005871 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005872 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005873 case HLoadClass::LoadKind::kBssEntry:
5874 DCHECK(!Runtime::Current()->UseJitCompilation());
5875 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005876 case HLoadClass::LoadKind::kJitTableAddress:
5877 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005878 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005879 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005880 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005881 break;
5882 }
5883 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005884 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005885 }
5886 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005887}
5888
Vladimir Markodc151b22015-10-15 18:02:30 +01005889HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5890 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005891 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005892 // On MIPS64 we support all dispatch types.
5893 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005894}
5895
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005896void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5897 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005898 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005899 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005900 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5901 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5902
Alexey Frunze19f6c692016-11-30 19:19:55 -08005903 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005904 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005905 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005906 uint32_t offset =
5907 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005908 __ LoadFromOffset(kLoadDoubleword,
5909 temp.AsRegister<GpuRegister>(),
5910 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005911 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005912 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005913 }
Vladimir Marko58155012015-08-19 12:49:41 +00005914 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005915 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005916 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005917 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5918 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005919 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005920 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005921 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5922 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5923 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005924 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5925 break;
5926 }
Vladimir Marko58155012015-08-19 12:49:41 +00005927 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005928 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5929 kLoadDoubleword,
5930 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005931 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005932 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005933 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005934 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005935 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5936 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5937 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005938 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5939 break;
5940 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005941 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5942 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5943 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005944 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005945 }
5946
Alexey Frunze19f6c692016-11-30 19:19:55 -08005947 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005948 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005949 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005950 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005951 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5952 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5953 __ LoadFromOffset(kLoadDoubleword,
5954 T9,
5955 callee_method.AsRegister<GpuRegister>(),
5956 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005957 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005958 // T9()
5959 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005960 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005961 break;
5962 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005963 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5964
Alexey Frunze4dda3372015-06-01 18:31:49 -07005965 DCHECK(!IsLeafMethod());
5966}
5967
5968void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005969 // Explicit clinit checks triggered by static invokes must have been pruned by
5970 // art::PrepareForRegisterAllocation.
5971 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005972
5973 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5974 return;
5975 }
5976
5977 LocationSummary* locations = invoke->GetLocations();
5978 codegen_->GenerateStaticOrDirectCall(invoke,
5979 locations->HasTemps()
5980 ? locations->GetTemp(0)
5981 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005982}
5983
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005984void CodeGeneratorMIPS64::GenerateVirtualCall(
5985 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005986 // Use the calling convention instead of the location of the receiver, as
5987 // intrinsics may have put the receiver in a different register. In the intrinsics
5988 // slow path, the arguments have been moved to the right place, so here we are
5989 // guaranteed that the receiver is the first register of the calling convention.
5990 InvokeDexCallingConvention calling_convention;
5991 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5992
Alexey Frunze53afca12015-11-05 16:34:23 -08005993 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005994 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5995 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5996 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005997 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005998
5999 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006000 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08006001 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006002 // Instead of simply (possibly) unpoisoning `temp` here, we should
6003 // emit a read barrier for the previous class reference load.
6004 // However this is not required in practice, as this is an
6005 // intermediate/temporary reference and because the current
6006 // concurrent copying collector keeps the from-space memory
6007 // intact/accessible until the end of the marking phase (the
6008 // concurrent copying collector may not in the future).
6009 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006010 // temp = temp->GetMethodAt(method_offset);
6011 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6012 // T9 = temp->GetEntryPoint();
6013 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6014 // T9();
6015 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006016 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006017 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006018}
6019
6020void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6021 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6022 return;
6023 }
6024
6025 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006026 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006027}
6028
6029void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006030 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006031 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006032 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006033 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6034 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006035 return;
6036 }
Vladimir Marko41559982017-01-06 14:04:23 +00006037 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006038
Alexey Frunze15958152017-02-09 19:08:30 -08006039 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6040 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006041 ? LocationSummary::kCallOnSlowPath
6042 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006043 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006044 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6045 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6046 }
Vladimir Marko41559982017-01-06 14:04:23 +00006047 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006048 locations->SetInAt(0, Location::RequiresRegister());
6049 }
6050 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006051 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6052 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6053 // Rely on the type resolution or initialization and marking to save everything we need.
6054 RegisterSet caller_saves = RegisterSet::Empty();
6055 InvokeRuntimeCallingConvention calling_convention;
6056 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6057 locations->SetCustomSlowPathCallerSaves(caller_saves);
6058 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006059 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006060 }
6061 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006062}
6063
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006064// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6065// move.
6066void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006067 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006068 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006069 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006070 return;
6071 }
Vladimir Marko41559982017-01-06 14:04:23 +00006072 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006073
Vladimir Marko41559982017-01-06 14:04:23 +00006074 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006075 Location out_loc = locations->Out();
6076 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6077 GpuRegister current_method_reg = ZERO;
6078 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006079 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006080 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6081 }
6082
Alexey Frunze15958152017-02-09 19:08:30 -08006083 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6084 ? kWithoutReadBarrier
6085 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006086 bool generate_null_check = false;
6087 switch (load_kind) {
6088 case HLoadClass::LoadKind::kReferrersClass:
6089 DCHECK(!cls->CanCallRuntime());
6090 DCHECK(!cls->MustGenerateClinitCheck());
6091 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6092 GenerateGcRootFieldLoad(cls,
6093 out_loc,
6094 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006095 ArtMethod::DeclaringClassOffset().Int32Value(),
6096 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006097 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006098 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006099 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006100 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006101 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006102 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006103 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6104 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6105 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006106 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6107 break;
6108 }
6109 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006110 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006111 uint32_t address = dchecked_integral_cast<uint32_t>(
6112 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6113 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006114 __ LoadLiteral(out,
6115 kLoadUnsignedWord,
6116 codegen_->DeduplicateBootImageAddressLiteral(address));
6117 break;
6118 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006119 case HLoadClass::LoadKind::kBootImageClassTable: {
6120 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6121 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6122 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6123 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6124 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6125 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6126 __ Lwu(out, AT, /* placeholder */ 0x5678);
6127 // Extract the reference from the slot data, i.e. clear the hash bits.
6128 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6129 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6130 if (masked_hash != 0) {
6131 __ Daddiu(out, out, -masked_hash);
6132 }
6133 break;
6134 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006135 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006136 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6137 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006138 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6139 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006140 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006141 GenerateGcRootFieldLoad(cls,
6142 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006143 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006144 /* placeholder */ 0x5678,
6145 read_barrier_option,
6146 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006147 generate_null_check = true;
6148 break;
6149 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006150 case HLoadClass::LoadKind::kJitTableAddress:
6151 __ LoadLiteral(out,
6152 kLoadUnsignedWord,
6153 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6154 cls->GetTypeIndex(),
6155 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006156 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006157 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006158 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006159 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006160 LOG(FATAL) << "UNREACHABLE";
6161 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006162 }
6163
6164 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6165 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006166 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006167 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006168 codegen_->AddSlowPath(slow_path);
6169 if (generate_null_check) {
6170 __ Beqzc(out, slow_path->GetEntryLabel());
6171 }
6172 if (cls->MustGenerateClinitCheck()) {
6173 GenerateClassInitializationCheck(slow_path, out);
6174 } else {
6175 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006176 }
6177 }
6178}
6179
David Brazdilcb1c0552015-08-04 16:22:25 +01006180static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006181 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006182}
6183
Alexey Frunze4dda3372015-06-01 18:31:49 -07006184void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6185 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006186 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006187 locations->SetOut(Location::RequiresRegister());
6188}
6189
6190void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6191 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006192 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6193}
6194
6195void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006196 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006197}
6198
6199void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6200 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006201}
6202
Alexey Frunze4dda3372015-06-01 18:31:49 -07006203void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006204 HLoadString::LoadKind load_kind = load->GetLoadKind();
6205 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006206 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006207 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006208 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006209 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006210 } else {
6211 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006212 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6213 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6214 // Rely on the pResolveString and marking to save everything we need.
6215 RegisterSet caller_saves = RegisterSet::Empty();
6216 InvokeRuntimeCallingConvention calling_convention;
6217 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6218 locations->SetCustomSlowPathCallerSaves(caller_saves);
6219 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006220 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006221 }
6222 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006223 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006224}
6225
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006226// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6227// move.
6228void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006229 HLoadString::LoadKind load_kind = load->GetLoadKind();
6230 LocationSummary* locations = load->GetLocations();
6231 Location out_loc = locations->Out();
6232 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6233
6234 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006235 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6236 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006237 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006238 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006239 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6240 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6241 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006242 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006243 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006244 }
6245 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006246 uint32_t address = dchecked_integral_cast<uint32_t>(
6247 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6248 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006249 __ LoadLiteral(out,
6250 kLoadUnsignedWord,
6251 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006252 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006253 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006254 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006255 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006256 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006257 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006258 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6259 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006260 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6261 __ Lwu(out, AT, /* placeholder */ 0x5678);
6262 return;
6263 }
6264 case HLoadString::LoadKind::kBssEntry: {
6265 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6266 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6267 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6268 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6269 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006270 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006271 GenerateGcRootFieldLoad(load,
6272 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006273 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006274 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006275 kCompilerReadBarrierOption,
6276 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006277 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006278 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006279 codegen_->AddSlowPath(slow_path);
6280 __ Beqzc(out, slow_path->GetEntryLabel());
6281 __ Bind(slow_path->GetExitLabel());
6282 return;
6283 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006284 case HLoadString::LoadKind::kJitTableAddress:
6285 __ LoadLiteral(out,
6286 kLoadUnsignedWord,
6287 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6288 load->GetStringIndex(),
6289 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006290 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006291 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006292 default:
6293 break;
6294 }
6295
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006296 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006297 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006298 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006299 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006300 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6301 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6302 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006303}
6304
Alexey Frunze4dda3372015-06-01 18:31:49 -07006305void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006306 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006307 locations->SetOut(Location::ConstantLocation(constant));
6308}
6309
6310void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6311 // Will be generated at use site.
6312}
6313
6314void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006315 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6316 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006317 InvokeRuntimeCallingConvention calling_convention;
6318 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6319}
6320
6321void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006322 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006323 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006324 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006325 if (instruction->IsEnter()) {
6326 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6327 } else {
6328 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6329 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006330}
6331
6332void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6333 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006334 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006335 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006336 case DataType::Type::kInt32:
6337 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006338 locations->SetInAt(0, Location::RequiresRegister());
6339 locations->SetInAt(1, Location::RequiresRegister());
6340 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6341 break;
6342
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006343 case DataType::Type::kFloat32:
6344 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006345 locations->SetInAt(0, Location::RequiresFpuRegister());
6346 locations->SetInAt(1, Location::RequiresFpuRegister());
6347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6348 break;
6349
6350 default:
6351 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6352 }
6353}
6354
6355void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006356 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006357 LocationSummary* locations = instruction->GetLocations();
6358
6359 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006360 case DataType::Type::kInt32:
6361 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006362 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6363 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6364 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006365 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006366 __ MulR6(dst, lhs, rhs);
6367 else
6368 __ Dmul(dst, lhs, rhs);
6369 break;
6370 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006371 case DataType::Type::kFloat32:
6372 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006373 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6374 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6375 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006376 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006377 __ MulS(dst, lhs, rhs);
6378 else
6379 __ MulD(dst, lhs, rhs);
6380 break;
6381 }
6382 default:
6383 LOG(FATAL) << "Unexpected mul type " << type;
6384 }
6385}
6386
6387void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6388 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006389 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006390 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kInt32:
6392 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006393 locations->SetInAt(0, Location::RequiresRegister());
6394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6395 break;
6396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 case DataType::Type::kFloat32:
6398 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006399 locations->SetInAt(0, Location::RequiresFpuRegister());
6400 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6401 break;
6402
6403 default:
6404 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6405 }
6406}
6407
6408void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006410 LocationSummary* locations = instruction->GetLocations();
6411
6412 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006413 case DataType::Type::kInt32:
6414 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006415 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6416 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006417 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006418 __ Subu(dst, ZERO, src);
6419 else
6420 __ Dsubu(dst, ZERO, src);
6421 break;
6422 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006423 case DataType::Type::kFloat32:
6424 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006425 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6426 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006427 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006428 __ NegS(dst, src);
6429 else
6430 __ NegD(dst, src);
6431 break;
6432 }
6433 default:
6434 LOG(FATAL) << "Unexpected neg type " << type;
6435 }
6436}
6437
6438void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006439 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6440 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006441 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006443 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6444 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006445}
6446
6447void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006448 // Note: if heap poisoning is enabled, the entry point takes care
6449 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006450 QuickEntrypointEnum entrypoint =
6451 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6452 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006453 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006454 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006455}
6456
6457void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006458 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6459 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006460 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006461 if (instruction->IsStringAlloc()) {
6462 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6463 } else {
6464 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006465 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006466 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006467}
6468
6469void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006470 // Note: if heap poisoning is enabled, the entry point takes care
6471 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006472 if (instruction->IsStringAlloc()) {
6473 // String is allocated through StringFactory. Call NewEmptyString entry point.
6474 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006475 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006476 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006477 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6478 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6479 __ Jalr(T9);
6480 __ Nop();
6481 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6482 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006483 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006484 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006485 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006486}
6487
6488void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006489 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006490 locations->SetInAt(0, Location::RequiresRegister());
6491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6492}
6493
6494void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006495 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006496 LocationSummary* locations = instruction->GetLocations();
6497
6498 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 case DataType::Type::kInt32:
6500 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006501 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6502 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6503 __ Nor(dst, src, ZERO);
6504 break;
6505 }
6506
6507 default:
6508 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6509 }
6510}
6511
6512void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006513 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006514 locations->SetInAt(0, Location::RequiresRegister());
6515 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6516}
6517
6518void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6519 LocationSummary* locations = instruction->GetLocations();
6520 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6521 locations->InAt(0).AsRegister<GpuRegister>(),
6522 1);
6523}
6524
6525void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006526 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6527 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006528}
6529
Calin Juravle2ae48182016-03-16 14:05:09 +00006530void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6531 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006532 return;
6533 }
6534 Location obj = instruction->GetLocations()->InAt(0);
6535
6536 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006537 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006538}
6539
Calin Juravle2ae48182016-03-16 14:05:09 +00006540void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006541 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006542 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006543 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006544
6545 Location obj = instruction->GetLocations()->InAt(0);
6546
6547 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6548}
6549
6550void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006551 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006552}
6553
6554void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6555 HandleBinaryOp(instruction);
6556}
6557
6558void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6559 HandleBinaryOp(instruction);
6560}
6561
6562void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6563 LOG(FATAL) << "Unreachable";
6564}
6565
6566void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006567 if (instruction->GetNext()->IsSuspendCheck() &&
6568 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6569 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6570 // The back edge will generate the suspend check.
6571 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6572 }
6573
Alexey Frunze4dda3372015-06-01 18:31:49 -07006574 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6575}
6576
6577void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006578 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006579 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6580 if (location.IsStackSlot()) {
6581 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6582 } else if (location.IsDoubleStackSlot()) {
6583 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6584 }
6585 locations->SetOut(location);
6586}
6587
6588void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6589 ATTRIBUTE_UNUSED) {
6590 // Nothing to do, the parameter is already at its location.
6591}
6592
6593void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6594 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006595 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006596 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6597}
6598
6599void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6600 ATTRIBUTE_UNUSED) {
6601 // Nothing to do, the method is already at its location.
6602}
6603
6604void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006605 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006606 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006607 locations->SetInAt(i, Location::Any());
6608 }
6609 locations->SetOut(Location::Any());
6610}
6611
6612void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6613 LOG(FATAL) << "Unreachable";
6614}
6615
6616void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006617 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006618 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006619 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6620 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006621 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006622
6623 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006624 case DataType::Type::kInt32:
6625 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006626 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006627 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6629 break;
6630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 case DataType::Type::kFloat32:
6632 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006633 InvokeRuntimeCallingConvention calling_convention;
6634 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6635 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6636 locations->SetOut(calling_convention.GetReturnLocation(type));
6637 break;
6638 }
6639
6640 default:
6641 LOG(FATAL) << "Unexpected rem type " << type;
6642 }
6643}
6644
6645void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006646 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006647
6648 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006649 case DataType::Type::kInt32:
6650 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006651 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006652 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006654 case DataType::Type::kFloat32:
6655 case DataType::Type::kFloat64: {
6656 QuickEntrypointEnum entrypoint =
6657 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006658 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006659 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006660 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6661 } else {
6662 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6663 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006664 break;
6665 }
6666 default:
6667 LOG(FATAL) << "Unexpected rem type " << type;
6668 }
6669}
6670
Igor Murashkind01745e2017-04-05 16:40:31 -07006671void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6672 constructor_fence->SetLocations(nullptr);
6673}
6674
6675void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6676 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6677 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6678}
6679
Alexey Frunze4dda3372015-06-01 18:31:49 -07006680void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6681 memory_barrier->SetLocations(nullptr);
6682}
6683
6684void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6685 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6686}
6687
6688void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006689 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006690 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006691 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6692}
6693
6694void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6695 codegen_->GenerateFrameExit();
6696}
6697
6698void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6699 ret->SetLocations(nullptr);
6700}
6701
6702void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6703 codegen_->GenerateFrameExit();
6704}
6705
Alexey Frunze92d90602015-12-18 18:16:36 -08006706void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6707 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006708}
6709
Alexey Frunze92d90602015-12-18 18:16:36 -08006710void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6711 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006712}
6713
Alexey Frunze4dda3372015-06-01 18:31:49 -07006714void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6715 HandleShift(shl);
6716}
6717
6718void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6719 HandleShift(shl);
6720}
6721
6722void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6723 HandleShift(shr);
6724}
6725
6726void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6727 HandleShift(shr);
6728}
6729
Alexey Frunze4dda3372015-06-01 18:31:49 -07006730void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6731 HandleBinaryOp(instruction);
6732}
6733
6734void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6735 HandleBinaryOp(instruction);
6736}
6737
6738void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6739 HandleFieldGet(instruction, instruction->GetFieldInfo());
6740}
6741
6742void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6743 HandleFieldGet(instruction, instruction->GetFieldInfo());
6744}
6745
6746void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6747 HandleFieldSet(instruction, instruction->GetFieldInfo());
6748}
6749
6750void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006751 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006752}
6753
Calin Juravlee460d1d2015-09-29 04:52:17 +01006754void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6755 HUnresolvedInstanceFieldGet* instruction) {
6756 FieldAccessCallingConventionMIPS64 calling_convention;
6757 codegen_->CreateUnresolvedFieldLocationSummary(
6758 instruction, instruction->GetFieldType(), calling_convention);
6759}
6760
6761void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6762 HUnresolvedInstanceFieldGet* instruction) {
6763 FieldAccessCallingConventionMIPS64 calling_convention;
6764 codegen_->GenerateUnresolvedFieldAccess(instruction,
6765 instruction->GetFieldType(),
6766 instruction->GetFieldIndex(),
6767 instruction->GetDexPc(),
6768 calling_convention);
6769}
6770
6771void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6772 HUnresolvedInstanceFieldSet* instruction) {
6773 FieldAccessCallingConventionMIPS64 calling_convention;
6774 codegen_->CreateUnresolvedFieldLocationSummary(
6775 instruction, instruction->GetFieldType(), calling_convention);
6776}
6777
6778void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6779 HUnresolvedInstanceFieldSet* instruction) {
6780 FieldAccessCallingConventionMIPS64 calling_convention;
6781 codegen_->GenerateUnresolvedFieldAccess(instruction,
6782 instruction->GetFieldType(),
6783 instruction->GetFieldIndex(),
6784 instruction->GetDexPc(),
6785 calling_convention);
6786}
6787
6788void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6789 HUnresolvedStaticFieldGet* instruction) {
6790 FieldAccessCallingConventionMIPS64 calling_convention;
6791 codegen_->CreateUnresolvedFieldLocationSummary(
6792 instruction, instruction->GetFieldType(), calling_convention);
6793}
6794
6795void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6796 HUnresolvedStaticFieldGet* instruction) {
6797 FieldAccessCallingConventionMIPS64 calling_convention;
6798 codegen_->GenerateUnresolvedFieldAccess(instruction,
6799 instruction->GetFieldType(),
6800 instruction->GetFieldIndex(),
6801 instruction->GetDexPc(),
6802 calling_convention);
6803}
6804
6805void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6806 HUnresolvedStaticFieldSet* instruction) {
6807 FieldAccessCallingConventionMIPS64 calling_convention;
6808 codegen_->CreateUnresolvedFieldLocationSummary(
6809 instruction, instruction->GetFieldType(), calling_convention);
6810}
6811
6812void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6813 HUnresolvedStaticFieldSet* instruction) {
6814 FieldAccessCallingConventionMIPS64 calling_convention;
6815 codegen_->GenerateUnresolvedFieldAccess(instruction,
6816 instruction->GetFieldType(),
6817 instruction->GetFieldIndex(),
6818 instruction->GetDexPc(),
6819 calling_convention);
6820}
6821
Alexey Frunze4dda3372015-06-01 18:31:49 -07006822void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006823 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6824 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006825 // In suspend check slow path, usually there are no caller-save registers at all.
6826 // If SIMD instructions are present, however, we force spilling all live SIMD
6827 // registers in full width (since the runtime only saves/restores lower part).
6828 locations->SetCustomSlowPathCallerSaves(
6829 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006830}
6831
6832void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6833 HBasicBlock* block = instruction->GetBlock();
6834 if (block->GetLoopInformation() != nullptr) {
6835 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6836 // The back edge will generate the suspend check.
6837 return;
6838 }
6839 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6840 // The goto will generate the suspend check.
6841 return;
6842 }
6843 GenerateSuspendCheck(instruction, nullptr);
6844}
6845
Alexey Frunze4dda3372015-06-01 18:31:49 -07006846void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006847 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6848 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006849 InvokeRuntimeCallingConvention calling_convention;
6850 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6851}
6852
6853void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006854 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006855 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6856}
6857
6858void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006859 DataType::Type input_type = conversion->GetInputType();
6860 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006861 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6862 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006864 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6865 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006866 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6867 }
6868
Vladimir Markoca6fff82017-10-03 14:49:14 +01006869 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006870
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006871 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006872 locations->SetInAt(0, Location::RequiresFpuRegister());
6873 } else {
6874 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006875 }
6876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006877 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006878 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006879 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006881 }
6882}
6883
6884void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6885 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006886 DataType::Type result_type = conversion->GetResultType();
6887 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006888
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006889 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6890 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006892 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006893 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6894 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6895
6896 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006897 case DataType::Type::kUint8:
6898 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006899 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006900 case DataType::Type::kInt8:
6901 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006902 // Type conversion from long to types narrower than int is a result of code
6903 // transformations. To avoid unpredictable results for SEB and SEH, we first
6904 // need to sign-extend the low 32-bit value into bits 32 through 63.
6905 __ Sll(dst, src, 0);
6906 __ Seb(dst, dst);
6907 } else {
6908 __ Seb(dst, src);
6909 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006910 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006911 case DataType::Type::kUint16:
6912 __ Andi(dst, src, 0xFFFF);
6913 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006914 case DataType::Type::kInt16:
6915 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006916 // Type conversion from long to types narrower than int is a result of code
6917 // transformations. To avoid unpredictable results for SEB and SEH, we first
6918 // need to sign-extend the low 32-bit value into bits 32 through 63.
6919 __ Sll(dst, src, 0);
6920 __ Seh(dst, dst);
6921 } else {
6922 __ Seh(dst, src);
6923 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006924 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006925 case DataType::Type::kInt32:
6926 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006927 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6928 // conversions, except when the input and output registers are the same and we are not
6929 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006930 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006931 __ Sll(dst, src, 0);
6932 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006933 break;
6934
6935 default:
6936 LOG(FATAL) << "Unexpected type conversion from " << input_type
6937 << " to " << result_type;
6938 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006939 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006940 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6941 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006942 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006943 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006944 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006945 __ Cvtsl(dst, FTMP);
6946 } else {
6947 __ Cvtdl(dst, FTMP);
6948 }
6949 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006950 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006951 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006952 __ Cvtsw(dst, FTMP);
6953 } else {
6954 __ Cvtdw(dst, FTMP);
6955 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006956 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006957 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6958 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006959 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6960 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006962 if (result_type == DataType::Type::kInt64) {
6963 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006964 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006965 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006966 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006967 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006968 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006969 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006970 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006971 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006972 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006973 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006974 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006975 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006976 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006977 } else if (DataType::IsFloatingPointType(result_type) &&
6978 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006979 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6980 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006981 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006982 __ Cvtsd(dst, src);
6983 } else {
6984 __ Cvtds(dst, src);
6985 }
6986 } else {
6987 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6988 << " to " << result_type;
6989 }
6990}
6991
6992void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6993 HandleShift(ushr);
6994}
6995
6996void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6997 HandleShift(ushr);
6998}
6999
7000void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7001 HandleBinaryOp(instruction);
7002}
7003
7004void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7005 HandleBinaryOp(instruction);
7006}
7007
7008void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7009 // Nothing to do, this should be removed during prepare for register allocator.
7010 LOG(FATAL) << "Unreachable";
7011}
7012
7013void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7014 // Nothing to do, this should be removed during prepare for register allocator.
7015 LOG(FATAL) << "Unreachable";
7016}
7017
7018void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007019 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007020}
7021
7022void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007023 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007024}
7025
7026void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007027 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007028}
7029
7030void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007031 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007032}
7033
7034void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007035 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007036}
7037
7038void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007039 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007040}
7041
7042void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007043 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007044}
7045
7046void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007047 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007048}
7049
7050void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007051 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007052}
7053
7054void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007055 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007056}
7057
7058void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007059 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007060}
7061
7062void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007063 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007064}
7065
Aart Bike9f37602015-10-09 11:15:55 -07007066void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007067 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007068}
7069
7070void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007071 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007072}
7073
7074void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007075 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007076}
7077
7078void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007079 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007080}
7081
7082void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007083 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007084}
7085
7086void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007087 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007088}
7089
7090void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007091 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007092}
7093
7094void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007095 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007096}
7097
Mark Mendellfe57faa2015-09-18 09:26:15 -04007098// Simple implementation of packed switch - generate cascaded compare/jumps.
7099void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7100 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007101 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007102 locations->SetInAt(0, Location::RequiresRegister());
7103}
7104
Alexey Frunze0960ac52016-12-20 17:24:59 -08007105void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7106 int32_t lower_bound,
7107 uint32_t num_entries,
7108 HBasicBlock* switch_block,
7109 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007110 // Create a set of compare/jumps.
7111 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007112 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007113 // Jump to default if index is negative
7114 // Note: We don't check the case that index is positive while value < lower_bound, because in
7115 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7116 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7117
Alexey Frunze0960ac52016-12-20 17:24:59 -08007118 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007119 // Jump to successors[0] if value == lower_bound.
7120 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7121 int32_t last_index = 0;
7122 for (; num_entries - last_index > 2; last_index += 2) {
7123 __ Addiu(temp_reg, temp_reg, -2);
7124 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7125 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7126 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7127 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7128 }
7129 if (num_entries - last_index == 2) {
7130 // The last missing case_value.
7131 __ Addiu(temp_reg, temp_reg, -1);
7132 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007133 }
7134
7135 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007136 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007137 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007138 }
7139}
7140
Alexey Frunze0960ac52016-12-20 17:24:59 -08007141void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7142 int32_t lower_bound,
7143 uint32_t num_entries,
7144 HBasicBlock* switch_block,
7145 HBasicBlock* default_block) {
7146 // Create a jump table.
7147 std::vector<Mips64Label*> labels(num_entries);
7148 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7149 for (uint32_t i = 0; i < num_entries; i++) {
7150 labels[i] = codegen_->GetLabelOf(successors[i]);
7151 }
7152 JumpTable* table = __ CreateJumpTable(std::move(labels));
7153
7154 // Is the value in range?
7155 __ Addiu32(TMP, value_reg, -lower_bound);
7156 __ LoadConst32(AT, num_entries);
7157 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7158
7159 // We are in the range of the table.
7160 // Load the target address from the jump table, indexing by the value.
7161 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007162 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007163 __ Lw(TMP, TMP, 0);
7164 // Compute the absolute target address by adding the table start address
7165 // (the table contains offsets to targets relative to its start).
7166 __ Daddu(TMP, TMP, AT);
7167 // And jump.
7168 __ Jr(TMP);
7169 __ Nop();
7170}
7171
7172void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7173 int32_t lower_bound = switch_instr->GetStartValue();
7174 uint32_t num_entries = switch_instr->GetNumEntries();
7175 LocationSummary* locations = switch_instr->GetLocations();
7176 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7177 HBasicBlock* switch_block = switch_instr->GetBlock();
7178 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7179
7180 if (num_entries > kPackedSwitchJumpTableThreshold) {
7181 GenTableBasedPackedSwitch(value_reg,
7182 lower_bound,
7183 num_entries,
7184 switch_block,
7185 default_block);
7186 } else {
7187 GenPackedSwitchWithCompares(value_reg,
7188 lower_bound,
7189 num_entries,
7190 switch_block,
7191 default_block);
7192 }
7193}
7194
Chris Larsenc9905a62017-03-13 17:06:18 -07007195void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7196 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007197 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007198 locations->SetInAt(0, Location::RequiresRegister());
7199 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007200}
7201
Chris Larsenc9905a62017-03-13 17:06:18 -07007202void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7203 LocationSummary* locations = instruction->GetLocations();
7204 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7205 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7206 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7207 __ LoadFromOffset(kLoadDoubleword,
7208 locations->Out().AsRegister<GpuRegister>(),
7209 locations->InAt(0).AsRegister<GpuRegister>(),
7210 method_offset);
7211 } else {
7212 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7213 instruction->GetIndex(), kMips64PointerSize));
7214 __ LoadFromOffset(kLoadDoubleword,
7215 locations->Out().AsRegister<GpuRegister>(),
7216 locations->InAt(0).AsRegister<GpuRegister>(),
7217 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7218 __ LoadFromOffset(kLoadDoubleword,
7219 locations->Out().AsRegister<GpuRegister>(),
7220 locations->Out().AsRegister<GpuRegister>(),
7221 method_offset);
7222 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007223}
7224
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007225void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7226 ATTRIBUTE_UNUSED) {
7227 LOG(FATAL) << "Unreachable";
7228}
7229
7230void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7231 ATTRIBUTE_UNUSED) {
7232 LOG(FATAL) << "Unreachable";
7233}
7234
Alexey Frunze4dda3372015-06-01 18:31:49 -07007235} // namespace mips64
7236} // namespace art