blob: 480b9178d26a968edc1746626ff1155408e39ae2 [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:
58 case DataType::Type::kInt32:
59 case DataType::Type::kReference:
60 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070061 return Location::RegisterLocation(V0);
62
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kFloat32:
64 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070065 return Location::FpuRegisterLocation(F0);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070068 return Location();
69 }
70 UNREACHABLE();
71}
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070074 return Mips64ReturnLocation(type);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
78 return Location::RegisterLocation(kMethodRegisterArgument);
79}
80
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070082 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070084 LOG(FATAL) << "Unexpected parameter type " << type;
85 }
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070088 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
89 next_location = Location::FpuRegisterLocation(
90 calling_convention.GetFpuRegisterAt(float_index_++));
91 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070093 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
94 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
95 float_index_++;
96 } else {
97 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010098 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
99 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700100 }
101
102 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
Alexey Frunze4dda3372015-06-01 18:31:49 -0700105 return next_location;
106}
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700109 return Mips64ReturnLocation(type);
110}
111
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100112// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
113#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700114#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115
116class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
117 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700119
120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100121 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
123 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000124 if (instruction_->CanThrowIntoCatchBlock()) {
125 // Live registers will be restored in the catch block if caught.
126 SaveLiveRegisters(codegen, instruction_->GetLocations());
127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 // We're moving two locations to locations that could overlap, so we need a parallel
129 // move resolver.
130 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100137 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
138 ? kQuickThrowStringBounds
139 : kQuickThrowArrayBounds;
140 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100141 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
143 }
144
Alexandre Rames8158f282015-08-07 10:26:17 +0100145 bool IsFatal() const OVERRIDE { return true; }
146
Roland Levillain46648892015-06-19 16:07:18 +0100147 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
148
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
151};
152
153class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
154 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700155 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
156 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
160 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100161 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
163 }
164
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 bool IsFatal() const OVERRIDE { return true; }
166
Roland Levillain46648892015-06-19 16:07:18 +0100167 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
168
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700170 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
171};
172
173class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
174 public:
175 LoadClassSlowPathMIPS64(HLoadClass* cls,
176 HInstruction* at,
177 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000178 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700179 : SlowPathCodeMIPS64(at),
180 cls_(cls),
181 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000182 do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000187 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700188 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700189 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700190 InvokeRuntimeCallingConvention calling_convention;
191 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 __ Bind(GetEntryLabel());
193 SaveLiveRegisters(codegen, locations);
194
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000195 dex::TypeIndex type_index = cls_->GetTypeIndex();
196 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100197 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
198 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000199 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700200 if (do_clinit_) {
201 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
202 } else {
203 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
204 }
205
206 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700207 if (out.IsValid()) {
208 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100209 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700210 mips64_codegen->MoveLocation(out,
211 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
212 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700215
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700216 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 }
218
Roland Levillain46648892015-06-19 16:07:18 +0100219 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
220
Alexey Frunze4dda3372015-06-01 18:31:49 -0700221 private:
222 // The class this slow path will load.
223 HLoadClass* const cls_;
224
Alexey Frunze4dda3372015-06-01 18:31:49 -0700225 // The dex PC of `at_`.
226 const uint32_t dex_pc_;
227
228 // Whether to initialize the class.
229 const bool do_clinit_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
232};
233
234class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
235 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000236 explicit LoadStringSlowPathMIPS64(HLoadString* instruction)
237 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700240 DCHECK(instruction_->IsLoadString());
241 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700242 LocationSummary* locations = instruction_->GetLocations();
243 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000244 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700246 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000250 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100251 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700252 instruction_,
253 instruction_->GetDexPc(),
254 this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100257 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700258 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800262
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700263 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700264 }
265
Roland Levillain46648892015-06-19 16:07:18 +0100266 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
267
Alexey Frunze4dda3372015-06-01 18:31:49 -0700268 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
270};
271
272class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
273 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000274 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275
276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
277 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
278 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000279 if (instruction_->CanThrowIntoCatchBlock()) {
280 // Live registers will be restored in the catch block if caught.
281 SaveLiveRegisters(codegen, instruction_->GetLocations());
282 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100283 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700284 instruction_,
285 instruction_->GetDexPc(),
286 this);
287 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
288 }
289
Alexandre Rames8158f282015-08-07 10:26:17 +0100290 bool IsFatal() const OVERRIDE { return true; }
291
Roland Levillain46648892015-06-19 16:07:18 +0100292 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
293
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
296};
297
298class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
299 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100300 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000301 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200304 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200307 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100308 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200310 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700312 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 }
316 }
317
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700318 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 DCHECK(successor_ == nullptr);
320 return &return_label_;
321 }
322
Roland Levillain46648892015-06-19 16:07:18 +0100323 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
324
Chris Larsena2045912017-11-02 12:39:54 -0700325 HBasicBlock* GetSuccessor() const {
326 return successor_;
327 }
328
Alexey Frunze4dda3372015-06-01 18:31:49 -0700329 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
342 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700343
344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
345 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800346
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 DCHECK(instruction_->IsCheckCast()
349 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
350 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
351
352 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800353 if (!is_fatal_) {
354 SaveLiveRegisters(codegen, locations);
355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356
357 // We're moving two locations to locations that could overlap, so we need a parallel
358 // move resolver.
359 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100362 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100365 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100367 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800368 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100369 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
371 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 } else {
373 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800374 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
375 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 }
377
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800378 if (!is_fatal_) {
379 RestoreLiveRegisters(codegen, locations);
380 __ Bc(GetExitLabel());
381 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
385
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800386 bool IsFatal() const OVERRIDE { return is_fatal_; }
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800389 const bool is_fatal_;
390
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
392};
393
394class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
395 public:
Aart Bik42249c32016-01-07 15:33:50 -0800396 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000397 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398
399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800400 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100402 LocationSummary* locations = instruction_->GetLocations();
403 SaveLiveRegisters(codegen, locations);
404 InvokeRuntimeCallingConvention calling_convention;
405 __ LoadConst32(calling_convention.GetRegisterAt(0),
406 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100407 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100408 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 }
410
Roland Levillain46648892015-06-19 16:07:18 +0100411 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
412
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
415};
416
Alexey Frunze15958152017-02-09 19:08:30 -0800417class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
418 public:
419 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
420
421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
422 LocationSummary* locations = instruction_->GetLocations();
423 __ Bind(GetEntryLabel());
424 SaveLiveRegisters(codegen, locations);
425
426 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100427 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800428 parallel_move.AddMove(
429 locations->InAt(0),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100431 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800432 nullptr);
433 parallel_move.AddMove(
434 locations->InAt(1),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100436 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(2),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100441 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800442 nullptr);
443 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
444
445 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
446 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
447 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
448 RestoreLiveRegisters(codegen, locations);
449 __ Bc(GetExitLabel());
450 }
451
452 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
453
454 private:
455 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
456};
457
458// Slow path marking an object reference `ref` during a read
459// barrier. The field `obj.field` in the object `obj` holding this
460// reference does not get updated by this slow path after marking (see
461// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
462//
463// This means that after the execution of this slow path, `ref` will
464// always be up-to-date, but `obj.field` may not; i.e., after the
465// flip, `ref` will be a to-space reference, but `obj.field` will
466// probably still be a from-space reference (unless it gets updated by
467// another thread, or if another thread installed another object
468// reference (different from `ref`) in `obj.field`).
469//
470// If `entrypoint` is a valid location it is assumed to already be
471// holding the entrypoint. The case where the entrypoint is passed in
472// is for the GcRoot read barrier.
473class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
474 public:
475 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
476 Location ref,
477 Location entrypoint = Location::NoLocation())
478 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
479 DCHECK(kEmitCompilerReadBarrier);
480 }
481
482 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
483
484 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
485 LocationSummary* locations = instruction_->GetLocations();
486 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
487 DCHECK(locations->CanCall());
488 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
489 DCHECK(instruction_->IsInstanceFieldGet() ||
490 instruction_->IsStaticFieldGet() ||
491 instruction_->IsArrayGet() ||
492 instruction_->IsArraySet() ||
493 instruction_->IsLoadClass() ||
494 instruction_->IsLoadString() ||
495 instruction_->IsInstanceOf() ||
496 instruction_->IsCheckCast() ||
497 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
498 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
499 << "Unexpected instruction in read barrier marking slow path: "
500 << instruction_->DebugName();
501
502 __ Bind(GetEntryLabel());
503 // No need to save live registers; it's taken care of by the
504 // entrypoint. Also, there is no need to update the stack mask,
505 // as this runtime call will not trigger a garbage collection.
506 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
507 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
508 (S2 <= ref_reg && ref_reg <= S7) ||
509 (ref_reg == S8)) << ref_reg;
510 // "Compact" slow path, saving two moves.
511 //
512 // Instead of using the standard runtime calling convention (input
513 // and output in A0 and V0 respectively):
514 //
515 // A0 <- ref
516 // V0 <- ReadBarrierMark(A0)
517 // ref <- V0
518 //
519 // we just use rX (the register containing `ref`) as input and output
520 // of a dedicated entrypoint:
521 //
522 // rX <- ReadBarrierMarkRegX(rX)
523 //
524 if (entrypoint_.IsValid()) {
525 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
526 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
527 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
528 __ Nop();
529 } else {
530 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100531 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800532 // This runtime call does not require a stack map.
533 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
534 instruction_,
535 this);
536 }
537 __ Bc(GetExitLabel());
538 }
539
540 private:
541 // The location (register) of the marked object reference.
542 const Location ref_;
543
544 // The location of the entrypoint if already loaded.
545 const Location entrypoint_;
546
547 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
548};
549
550// Slow path marking an object reference `ref` during a read barrier,
551// and if needed, atomically updating the field `obj.field` in the
552// object `obj` holding this reference after marking (contrary to
553// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
554// `obj.field`).
555//
556// This means that after the execution of this slow path, both `ref`
557// and `obj.field` will be up-to-date; i.e., after the flip, both will
558// hold the same to-space reference (unless another thread installed
559// another object reference (different from `ref`) in `obj.field`).
560class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
561 public:
562 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
563 Location ref,
564 GpuRegister obj,
565 Location field_offset,
566 GpuRegister temp1)
567 : SlowPathCodeMIPS64(instruction),
568 ref_(ref),
569 obj_(obj),
570 field_offset_(field_offset),
571 temp1_(temp1) {
572 DCHECK(kEmitCompilerReadBarrier);
573 }
574
575 const char* GetDescription() const OVERRIDE {
576 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
577 }
578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 LocationSummary* locations = instruction_->GetLocations();
581 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
582 DCHECK(locations->CanCall());
583 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
584 // This slow path is only used by the UnsafeCASObject intrinsic.
585 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
586 << "Unexpected instruction in read barrier marking and field updating slow path: "
587 << instruction_->DebugName();
588 DCHECK(instruction_->GetLocations()->Intrinsified());
589 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
590 DCHECK(field_offset_.IsRegister()) << field_offset_;
591
592 __ Bind(GetEntryLabel());
593
594 // Save the old reference.
595 // Note that we cannot use AT or TMP to save the old reference, as those
596 // are used by the code that follows, but we need the old reference after
597 // the call to the ReadBarrierMarkRegX entry point.
598 DCHECK_NE(temp1_, AT);
599 DCHECK_NE(temp1_, TMP);
600 __ Move(temp1_, ref_reg);
601
602 // No need to save live registers; it's taken care of by the
603 // entrypoint. Also, there is no need to update the stack mask,
604 // as this runtime call will not trigger a garbage collection.
605 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
606 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
607 (S2 <= ref_reg && ref_reg <= S7) ||
608 (ref_reg == S8)) << ref_reg;
609 // "Compact" slow path, saving two moves.
610 //
611 // Instead of using the standard runtime calling convention (input
612 // and output in A0 and V0 respectively):
613 //
614 // A0 <- ref
615 // V0 <- ReadBarrierMark(A0)
616 // ref <- V0
617 //
618 // we just use rX (the register containing `ref`) as input and output
619 // of a dedicated entrypoint:
620 //
621 // rX <- ReadBarrierMarkRegX(rX)
622 //
623 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100624 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800625 // This runtime call does not require a stack map.
626 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
627 instruction_,
628 this);
629
630 // If the new reference is different from the old reference,
631 // update the field in the holder (`*(obj_ + field_offset_)`).
632 //
633 // Note that this field could also hold a different object, if
634 // another thread had concurrently changed it. In that case, the
635 // the compare-and-set (CAS) loop below would abort, leaving the
636 // field as-is.
637 Mips64Label done;
638 __ Beqc(temp1_, ref_reg, &done);
639
640 // Update the the holder's field atomically. This may fail if
641 // mutator updates before us, but it's OK. This is achieved
642 // using a strong compare-and-set (CAS) operation with relaxed
643 // memory synchronization ordering, where the expected value is
644 // the old reference and the desired value is the new reference.
645
646 // Convenience aliases.
647 GpuRegister base = obj_;
648 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
649 GpuRegister expected = temp1_;
650 GpuRegister value = ref_reg;
651 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
652 GpuRegister tmp = AT; // Value in memory.
653
654 __ Daddu(tmp_ptr, base, offset);
655
656 if (kPoisonHeapReferences) {
657 __ PoisonHeapReference(expected);
658 // Do not poison `value` if it is the same register as
659 // `expected`, which has just been poisoned.
660 if (value != expected) {
661 __ PoisonHeapReference(value);
662 }
663 }
664
665 // do {
666 // tmp = [r_ptr] - expected;
667 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
668
669 Mips64Label loop_head, exit_loop;
670 __ Bind(&loop_head);
671 __ Ll(tmp, tmp_ptr);
672 // The LL instruction sign-extends the 32-bit value, but
673 // 32-bit references must be zero-extended. Zero-extend `tmp`.
674 __ Dext(tmp, tmp, 0, 32);
675 __ Bnec(tmp, expected, &exit_loop);
676 __ Move(tmp, value);
677 __ Sc(tmp, tmp_ptr);
678 __ Beqzc(tmp, &loop_head);
679 __ Bind(&exit_loop);
680
681 if (kPoisonHeapReferences) {
682 __ UnpoisonHeapReference(expected);
683 // Do not unpoison `value` if it is the same register as
684 // `expected`, which has just been unpoisoned.
685 if (value != expected) {
686 __ UnpoisonHeapReference(value);
687 }
688 }
689
690 __ Bind(&done);
691 __ Bc(GetExitLabel());
692 }
693
694 private:
695 // The location (register) of the marked object reference.
696 const Location ref_;
697 // The register containing the object holding the marked object reference field.
698 const GpuRegister obj_;
699 // The location of the offset of the marked reference field within `obj_`.
700 Location field_offset_;
701
702 const GpuRegister temp1_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
705};
706
707// Slow path generating a read barrier for a heap reference.
708class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
709 public:
710 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
711 Location out,
712 Location ref,
713 Location obj,
714 uint32_t offset,
715 Location index)
716 : SlowPathCodeMIPS64(instruction),
717 out_(out),
718 ref_(ref),
719 obj_(obj),
720 offset_(offset),
721 index_(index) {
722 DCHECK(kEmitCompilerReadBarrier);
723 // If `obj` is equal to `out` or `ref`, it means the initial object
724 // has been overwritten by (or after) the heap object reference load
725 // to be instrumented, e.g.:
726 //
727 // __ LoadFromOffset(kLoadWord, out, out, offset);
728 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
729 //
730 // In that case, we have lost the information about the original
731 // object, and the emitted read barrier cannot work properly.
732 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
733 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
734 }
735
736 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
737 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
738 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800740 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
741 DCHECK(locations->CanCall());
742 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
743 DCHECK(instruction_->IsInstanceFieldGet() ||
744 instruction_->IsStaticFieldGet() ||
745 instruction_->IsArrayGet() ||
746 instruction_->IsInstanceOf() ||
747 instruction_->IsCheckCast() ||
748 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
749 << "Unexpected instruction in read barrier for heap reference slow path: "
750 << instruction_->DebugName();
751
752 __ Bind(GetEntryLabel());
753 SaveLiveRegisters(codegen, locations);
754
755 // We may have to change the index's value, but as `index_` is a
756 // constant member (like other "inputs" of this slow path),
757 // introduce a copy of it, `index`.
758 Location index = index_;
759 if (index_.IsValid()) {
760 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
761 if (instruction_->IsArrayGet()) {
762 // Compute the actual memory offset and store it in `index`.
763 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
764 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
765 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
766 // We are about to change the value of `index_reg` (see the
767 // calls to art::mips64::Mips64Assembler::Sll and
768 // art::mips64::MipsAssembler::Addiu32 below), but it has
769 // not been saved by the previous call to
770 // art::SlowPathCode::SaveLiveRegisters, as it is a
771 // callee-save register --
772 // art::SlowPathCode::SaveLiveRegisters does not consider
773 // callee-save registers, as it has been designed with the
774 // assumption that callee-save registers are supposed to be
775 // handled by the called function. So, as a callee-save
776 // register, `index_reg` _would_ eventually be saved onto
777 // the stack, but it would be too late: we would have
778 // changed its value earlier. Therefore, we manually save
779 // it here into another freely available register,
780 // `free_reg`, chosen of course among the caller-save
781 // registers (as a callee-save `free_reg` register would
782 // exhibit the same problem).
783 //
784 // Note we could have requested a temporary register from
785 // the register allocator instead; but we prefer not to, as
786 // this is a slow path, and we know we can find a
787 // caller-save register that is available.
788 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
789 __ Move(free_reg, index_reg);
790 index_reg = free_reg;
791 index = Location::RegisterLocation(index_reg);
792 } else {
793 // The initial register stored in `index_` has already been
794 // saved in the call to art::SlowPathCode::SaveLiveRegisters
795 // (as it is not a callee-save register), so we can freely
796 // use it.
797 }
798 // Shifting the index value contained in `index_reg` by the scale
799 // factor (2) cannot overflow in practice, as the runtime is
800 // unable to allocate object arrays with a size larger than
801 // 2^26 - 1 (that is, 2^28 - 4 bytes).
802 __ Sll(index_reg, index_reg, TIMES_4);
803 static_assert(
804 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
805 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
806 __ Addiu32(index_reg, index_reg, offset_);
807 } else {
808 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
809 // intrinsics, `index_` is not shifted by a scale factor of 2
810 // (as in the case of ArrayGet), as it is actually an offset
811 // to an object field within an object.
812 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
813 DCHECK(instruction_->GetLocations()->Intrinsified());
814 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
815 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
816 << instruction_->AsInvoke()->GetIntrinsic();
817 DCHECK_EQ(offset_, 0U);
818 DCHECK(index_.IsRegister());
819 }
820 }
821
822 // We're moving two or three locations to locations that could
823 // overlap, so we need a parallel move resolver.
824 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100825 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800826 parallel_move.AddMove(ref_,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800829 nullptr);
830 parallel_move.AddMove(obj_,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800833 nullptr);
834 if (index.IsValid()) {
835 parallel_move.AddMove(index,
836 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100837 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800838 nullptr);
839 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
840 } else {
841 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
842 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
843 }
844 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
845 instruction_,
846 instruction_->GetDexPc(),
847 this);
848 CheckEntrypointTypes<
849 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
850 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
851
852 RestoreLiveRegisters(codegen, locations);
853 __ Bc(GetExitLabel());
854 }
855
856 const char* GetDescription() const OVERRIDE {
857 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
858 }
859
860 private:
861 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
862 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
863 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
864 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
865 if (i != ref &&
866 i != obj &&
867 !codegen->IsCoreCalleeSaveRegister(i) &&
868 !codegen->IsBlockedCoreRegister(i)) {
869 return static_cast<GpuRegister>(i);
870 }
871 }
872 // We shall never fail to find a free caller-save register, as
873 // there are more than two core caller-save registers on MIPS64
874 // (meaning it is possible to find one which is different from
875 // `ref` and `obj`).
876 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
877 LOG(FATAL) << "Could not find a free caller-save register";
878 UNREACHABLE();
879 }
880
881 const Location out_;
882 const Location ref_;
883 const Location obj_;
884 const uint32_t offset_;
885 // An additional location containing an index to an array.
886 // Only used for HArrayGet and the UnsafeGetObject &
887 // UnsafeGetObjectVolatile intrinsics.
888 const Location index_;
889
890 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
891};
892
893// Slow path generating a read barrier for a GC root.
894class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
895 public:
896 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
897 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
898 DCHECK(kEmitCompilerReadBarrier);
899 }
900
901 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
902 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100903 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800904 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
905 DCHECK(locations->CanCall());
906 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
907 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
908 << "Unexpected instruction in read barrier for GC root slow path: "
909 << instruction_->DebugName();
910
911 __ Bind(GetEntryLabel());
912 SaveLiveRegisters(codegen, locations);
913
914 InvokeRuntimeCallingConvention calling_convention;
915 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
916 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
917 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100918 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800919 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
920 instruction_,
921 instruction_->GetDexPc(),
922 this);
923 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
924 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
925
926 RestoreLiveRegisters(codegen, locations);
927 __ Bc(GetExitLabel());
928 }
929
930 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
931
932 private:
933 const Location out_;
934 const Location root_;
935
936 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
937};
938
Alexey Frunze4dda3372015-06-01 18:31:49 -0700939CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
940 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100941 const CompilerOptions& compiler_options,
942 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943 : CodeGenerator(graph,
944 kNumberOfGpuRegisters,
945 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000946 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
948 arraysize(kCoreCalleeSaves)),
949 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
950 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100951 compiler_options,
952 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100953 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700954 location_builder_(graph, this),
955 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 move_resolver_(graph->GetAllocator(), this),
957 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800958 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800959 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800961 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
963 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
964 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
965 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
966 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
967 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
968 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800969 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800971 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700973 // Save RA (containing the return address) to mimic Quick.
974 AddAllocatedRegister(Location::RegisterLocation(RA));
975}
976
977#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100978// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
979#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700980#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700981
982void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700983 // Ensure that we fix up branches.
984 __ FinalizeCode();
985
986 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100987 StackMapStream* stack_map_stream = GetStackMapStream();
988 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800989 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +0000990 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700991 uint32_t new_position = __ GetAdjustedPosition(old_position);
992 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100993 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700994 }
995
996 // Adjust pc offsets for the disassembly information.
997 if (disasm_info_ != nullptr) {
998 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
999 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1000 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1001 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1002 it.second.start = __ GetAdjustedPosition(it.second.start);
1003 it.second.end = __ GetAdjustedPosition(it.second.end);
1004 }
1005 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1006 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1007 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1008 }
1009 }
1010
Alexey Frunze4dda3372015-06-01 18:31:49 -07001011 CodeGenerator::Finalize(allocator);
1012}
1013
1014Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1015 return codegen_->GetAssembler();
1016}
1017
1018void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001019 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001020 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1021}
1022
1023void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001024 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1026}
1027
1028void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1029 // Pop reg
1030 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001031 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032}
1033
1034void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1035 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001036 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001037 __ Sd(GpuRegister(reg), SP, 0);
1038}
1039
1040void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1041 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1042 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1043 // Allocate a scratch register other than TMP, if available.
1044 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1045 // automatically unspilled when the scratch scope object is destroyed).
1046 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1047 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001048 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001049 __ LoadFromOffset(load_type,
1050 GpuRegister(ensure_scratch.GetRegister()),
1051 SP,
1052 index1 + stack_offset);
1053 __ LoadFromOffset(load_type,
1054 TMP,
1055 SP,
1056 index2 + stack_offset);
1057 __ StoreToOffset(store_type,
1058 GpuRegister(ensure_scratch.GetRegister()),
1059 SP,
1060 index2 + stack_offset);
1061 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1062}
1063
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001064void ParallelMoveResolverMIPS64::ExchangeQuadSlots(int index1, int index2) {
1065 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, index1);
1066 __ LoadFpuFromOffset(kLoadQuadword, FTMP2, SP, index2);
1067 __ StoreFpuToOffset(kStoreQuadword, FTMP, SP, index2);
1068 __ StoreFpuToOffset(kStoreQuadword, FTMP2, SP, index1);
1069}
1070
Alexey Frunze4dda3372015-06-01 18:31:49 -07001071static dwarf::Reg DWARFReg(GpuRegister reg) {
1072 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1073}
1074
David Srbeckyba702002016-02-01 18:15:29 +00001075static dwarf::Reg DWARFReg(FpuRegister reg) {
1076 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1077}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001078
1079void CodeGeneratorMIPS64::GenerateFrameEntry() {
1080 __ Bind(&frame_entry_label_);
1081
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001082 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1083 LOG(WARNING) << "Unimplemented hotness update in mips64 backend";
1084 }
1085
Vladimir Marko33bff252017-11-01 14:35:42 +00001086 bool do_overflow_check =
1087 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001088
1089 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001090 __ LoadFromOffset(
1091 kLoadWord,
1092 ZERO,
1093 SP,
1094 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001095 RecordPcInfo(nullptr, 0);
1096 }
1097
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 if (HasEmptyFrame()) {
1099 return;
1100 }
1101
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001102 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001103 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1104 LOG(FATAL) << "Stack frame larger than "
1105 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001107
1108 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001109
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001110 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001111 __ IncreaseFrameSize(ofs);
1112
1113 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1114 GpuRegister reg = kCoreCalleeSaves[i];
1115 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001116 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001117 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001118 __ cfi().RelOffset(DWARFReg(reg), ofs);
1119 }
1120 }
1121
1122 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1123 FpuRegister reg = kFpuCalleeSaves[i];
1124 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001125 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001126 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001127 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001128 }
1129 }
1130
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001131 // Save the current method if we need it. Note that we do not
1132 // do this in HCurrentMethod, as the instruction might have been removed
1133 // in the SSA graph.
1134 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001135 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001136 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001137
1138 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1139 // Initialize should_deoptimize flag to 0.
1140 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1141 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001142}
1143
1144void CodeGeneratorMIPS64::GenerateFrameExit() {
1145 __ cfi().RememberState();
1146
Alexey Frunze4dda3372015-06-01 18:31:49 -07001147 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001148 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001150 // For better instruction scheduling restore RA before other registers.
1151 uint32_t ofs = GetFrameSize();
1152 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153 GpuRegister reg = kCoreCalleeSaves[i];
1154 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001155 ofs -= kMips64DoublewordSize;
1156 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001157 __ cfi().Restore(DWARFReg(reg));
1158 }
1159 }
1160
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001161 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1162 FpuRegister reg = kFpuCalleeSaves[i];
1163 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1164 ofs -= kMips64DoublewordSize;
1165 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1166 __ cfi().Restore(DWARFReg(reg));
1167 }
1168 }
1169
1170 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001171 }
1172
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001173 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001174
1175 __ cfi().RestoreState();
1176 __ cfi().DefCFAOffset(GetFrameSize());
1177}
1178
1179void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1180 __ Bind(GetLabelOf(block));
1181}
1182
1183void CodeGeneratorMIPS64::MoveLocation(Location destination,
1184 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001185 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186 if (source.Equals(destination)) {
1187 return;
1188 }
1189
1190 // A valid move can always be inferred from the destination and source
1191 // locations. When moving from and to a register, the argument type can be
1192 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001194 DCHECK_EQ(unspecified_type, false);
1195
1196 if (destination.IsRegister() || destination.IsFpuRegister()) {
1197 if (unspecified_type) {
1198 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1199 if (source.IsStackSlot() ||
1200 (src_cst != nullptr && (src_cst->IsIntConstant()
1201 || src_cst->IsFloatConstant()
1202 || src_cst->IsNullConstant()))) {
1203 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001205 } else {
1206 // If the source is a double stack slot or a 64bit constant, a 64bit
1207 // type is appropriate. Else the source is a register, and since the
1208 // type has not been specified, we chose a 64bit type to force a 64bit
1209 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001210 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001211 }
1212 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1214 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1216 // Move to GPR/FPR from stack
1217 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001218 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 __ LoadFpuFromOffset(load_type,
1220 destination.AsFpuRegister<FpuRegister>(),
1221 SP,
1222 source.GetStackIndex());
1223 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001225 __ LoadFromOffset(load_type,
1226 destination.AsRegister<GpuRegister>(),
1227 SP,
1228 source.GetStackIndex());
1229 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001230 } else if (source.IsSIMDStackSlot()) {
1231 __ LoadFpuFromOffset(kLoadQuadword,
1232 destination.AsFpuRegister<FpuRegister>(),
1233 SP,
1234 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001235 } else if (source.IsConstant()) {
1236 // Move to GPR/FPR from constant
1237 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001238 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 gpr = destination.AsRegister<GpuRegister>();
1240 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001241 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001242 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001243 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001244 gpr = ZERO;
1245 } else {
1246 __ LoadConst32(gpr, value);
1247 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001248 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001249 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001250 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001251 gpr = ZERO;
1252 } else {
1253 __ LoadConst64(gpr, value);
1254 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001255 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001256 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001257 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001258 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001259 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1260 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001262 if (destination.IsRegister()) {
1263 // Move to GPR from GPR
1264 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1265 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001267 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1269 } else {
1270 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1271 }
1272 }
1273 } else if (source.IsFpuRegister()) {
1274 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001275 if (GetGraph()->HasSIMD()) {
1276 __ MoveV(VectorRegisterFrom(destination),
1277 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001278 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001279 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001280 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001281 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1282 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001284 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1285 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001286 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287 } else {
1288 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001289 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1291 } else {
1292 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1293 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001294 }
1295 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001296 } else if (destination.IsSIMDStackSlot()) {
1297 if (source.IsFpuRegister()) {
1298 __ StoreFpuToOffset(kStoreQuadword,
1299 source.AsFpuRegister<FpuRegister>(),
1300 SP,
1301 destination.GetStackIndex());
1302 } else {
1303 DCHECK(source.IsSIMDStackSlot());
1304 __ LoadFpuFromOffset(kLoadQuadword,
1305 FTMP,
1306 SP,
1307 source.GetStackIndex());
1308 __ StoreFpuToOffset(kStoreQuadword,
1309 FTMP,
1310 SP,
1311 destination.GetStackIndex());
1312 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001313 } else { // The destination is not a register. It must be a stack slot.
1314 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1315 if (source.IsRegister() || source.IsFpuRegister()) {
1316 if (unspecified_type) {
1317 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001318 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001320 dst_type =
1321 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001322 }
1323 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001324 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1325 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001326 // Move to stack from GPR/FPR
1327 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1328 if (source.IsRegister()) {
1329 __ StoreToOffset(store_type,
1330 source.AsRegister<GpuRegister>(),
1331 SP,
1332 destination.GetStackIndex());
1333 } else {
1334 __ StoreFpuToOffset(store_type,
1335 source.AsFpuRegister<FpuRegister>(),
1336 SP,
1337 destination.GetStackIndex());
1338 }
1339 } else if (source.IsConstant()) {
1340 // Move to stack from constant
1341 HConstant* src_cst = source.GetConstant();
1342 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001343 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001344 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001345 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1346 if (value != 0) {
1347 gpr = TMP;
1348 __ LoadConst32(gpr, value);
1349 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001350 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001351 DCHECK(destination.IsDoubleStackSlot());
1352 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1353 if (value != 0) {
1354 gpr = TMP;
1355 __ LoadConst64(gpr, value);
1356 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001357 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001358 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001359 } else {
1360 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1361 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1362 // Move to stack from stack
1363 if (destination.IsStackSlot()) {
1364 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1365 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1366 } else {
1367 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1368 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1369 }
1370 }
1371 }
1372}
1373
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001374void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001375 DCHECK(!loc1.IsConstant());
1376 DCHECK(!loc2.IsConstant());
1377
1378 if (loc1.Equals(loc2)) {
1379 return;
1380 }
1381
1382 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1383 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001384 bool is_simd1 = loc1.IsSIMDStackSlot();
1385 bool is_simd2 = loc2.IsSIMDStackSlot();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001386 bool is_fp_reg1 = loc1.IsFpuRegister();
1387 bool is_fp_reg2 = loc2.IsFpuRegister();
1388
1389 if (loc2.IsRegister() && loc1.IsRegister()) {
1390 // Swap 2 GPRs
1391 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1392 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1393 __ Move(TMP, r2);
1394 __ Move(r2, r1);
1395 __ Move(r1, TMP);
1396 } else if (is_fp_reg2 && is_fp_reg1) {
1397 // Swap 2 FPRs
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001398 if (GetGraph()->HasSIMD()) {
1399 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1400 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1401 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001402 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001403 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1404 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
1405 if (type == DataType::Type::kFloat32) {
1406 __ MovS(FTMP, r1);
1407 __ MovS(r1, r2);
1408 __ MovS(r2, FTMP);
1409 } else {
1410 DCHECK_EQ(type, DataType::Type::kFloat64);
1411 __ MovD(FTMP, r1);
1412 __ MovD(r1, r2);
1413 __ MovD(r2, FTMP);
1414 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001415 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001416 } else if (is_slot1 != is_slot2) {
1417 // Swap GPR/FPR and stack slot
1418 Location reg_loc = is_slot1 ? loc2 : loc1;
1419 Location mem_loc = is_slot1 ? loc1 : loc2;
1420 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1421 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001422 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001423 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1424 if (reg_loc.IsFpuRegister()) {
1425 __ StoreFpuToOffset(store_type,
1426 reg_loc.AsFpuRegister<FpuRegister>(),
1427 SP,
1428 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001429 if (mem_loc.IsStackSlot()) {
1430 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1431 } else {
1432 DCHECK(mem_loc.IsDoubleStackSlot());
1433 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1434 }
1435 } else {
1436 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1437 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1438 }
1439 } else if (is_slot1 && is_slot2) {
1440 move_resolver_.Exchange(loc1.GetStackIndex(),
1441 loc2.GetStackIndex(),
1442 loc1.IsDoubleStackSlot());
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001443 } else if (is_simd1 && is_simd2) {
1444 move_resolver_.ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
1445 } else if ((is_fp_reg1 && is_simd2) || (is_fp_reg2 && is_simd1)) {
1446 Location fp_reg_loc = is_fp_reg1 ? loc1 : loc2;
1447 Location mem_loc = is_fp_reg1 ? loc2 : loc1;
1448 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, mem_loc.GetStackIndex());
1449 __ StoreFpuToOffset(kStoreQuadword,
1450 fp_reg_loc.AsFpuRegister<FpuRegister>(),
1451 SP,
1452 mem_loc.GetStackIndex());
1453 __ MoveV(VectorRegisterFrom(fp_reg_loc), static_cast<VectorRegister>(FTMP));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001454 } else {
1455 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1456 }
1457}
1458
Calin Juravle175dc732015-08-25 15:42:32 +01001459void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1460 DCHECK(location.IsRegister());
1461 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1462}
1463
Calin Juravlee460d1d2015-09-29 04:52:17 +01001464void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1465 if (location.IsRegister()) {
1466 locations->AddTemp(location);
1467 } else {
1468 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1469 }
1470}
1471
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001472void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1473 GpuRegister value,
1474 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001475 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001476 GpuRegister card = AT;
1477 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001478 if (value_can_be_null) {
1479 __ Beqzc(value, &done);
1480 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001481 __ LoadFromOffset(kLoadDoubleword,
1482 card,
1483 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001484 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001485 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1486 __ Daddu(temp, card, temp);
1487 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001488 if (value_can_be_null) {
1489 __ Bind(&done);
1490 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001491}
1492
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001493template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001494inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1495 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001496 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001497 for (const PcRelativePatchInfo& info : infos) {
1498 const DexFile& dex_file = info.target_dex_file;
1499 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001500 DCHECK(info.label.IsBound());
1501 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1502 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1503 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1504 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001505 }
1506}
1507
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001508void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001509 DCHECK(linker_patches->empty());
1510 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001511 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001512 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001513 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001514 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001515 pc_relative_string_patches_.size() +
1516 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001517 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001518 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001519 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1520 pc_relative_method_patches_, linker_patches);
1521 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1522 pc_relative_type_patches_, linker_patches);
1523 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1524 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001525 } else {
1526 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001527 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1528 pc_relative_type_patches_, linker_patches);
1529 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1530 pc_relative_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001531 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001532 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1533 method_bss_entry_patches_, linker_patches);
1534 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1535 type_bss_entry_patches_, linker_patches);
1536 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1537 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001538 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001539}
1540
Vladimir Marko65979462017-05-19 17:25:12 +01001541CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001542 MethodReference target_method,
1543 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001544 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001545 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001546 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001547 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001548}
1549
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001550CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001551 MethodReference target_method,
1552 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001553 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001554 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001555 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001556 &method_bss_entry_patches_);
1557}
1558
Alexey Frunzef63f5692016-12-13 17:43:11 -08001559CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001560 const DexFile& dex_file,
1561 dex::TypeIndex type_index,
1562 const PcRelativePatchInfo* info_high) {
1563 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001564}
1565
Vladimir Marko1998cd02017-01-13 13:02:58 +00001566CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001567 const DexFile& dex_file,
1568 dex::TypeIndex type_index,
1569 const PcRelativePatchInfo* info_high) {
1570 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001571}
1572
Vladimir Marko65979462017-05-19 17:25:12 +01001573CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001574 const DexFile& dex_file,
1575 dex::StringIndex string_index,
1576 const PcRelativePatchInfo* info_high) {
1577 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001578}
1579
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001580CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1581 const DexFile& dex_file,
1582 dex::StringIndex string_index,
1583 const PcRelativePatchInfo* info_high) {
1584 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1585}
1586
Alexey Frunze19f6c692016-11-30 19:19:55 -08001587CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001588 const DexFile& dex_file,
1589 uint32_t offset_or_index,
1590 const PcRelativePatchInfo* info_high,
1591 ArenaDeque<PcRelativePatchInfo>* patches) {
1592 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001593 return &patches->back();
1594}
1595
Alexey Frunzef63f5692016-12-13 17:43:11 -08001596Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1597 return map->GetOrCreate(
1598 value,
1599 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1600}
1601
Alexey Frunze19f6c692016-11-30 19:19:55 -08001602Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1603 return uint64_literals_.GetOrCreate(
1604 value,
1605 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1606}
1607
Alexey Frunzef63f5692016-12-13 17:43:11 -08001608Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001609 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001610}
1611
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001612void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1613 GpuRegister out,
1614 PcRelativePatchInfo* info_low) {
1615 DCHECK(!info_high->patch_info_high);
1616 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001617 // Add the high half of a 32-bit offset to PC.
1618 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001619 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001620 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001621 if (info_low != nullptr) {
1622 DCHECK_EQ(info_low->patch_info_high, info_high);
1623 __ Bind(&info_low->label);
1624 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001625}
1626
Alexey Frunze627c1a02017-01-30 19:28:14 -08001627Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1628 dex::StringIndex string_index,
1629 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001630 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001631 return jit_string_patches_.GetOrCreate(
1632 StringReference(&dex_file, string_index),
1633 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1634}
1635
1636Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1637 dex::TypeIndex type_index,
1638 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001639 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001640 return jit_class_patches_.GetOrCreate(
1641 TypeReference(&dex_file, type_index),
1642 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1643}
1644
1645void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1646 const uint8_t* roots_data,
1647 const Literal* literal,
1648 uint64_t index_in_table) const {
1649 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1650 uintptr_t address =
1651 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1652 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1653}
1654
1655void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1656 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001657 const StringReference& string_reference = entry.first;
1658 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001659 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001660 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001661 }
1662 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001663 const TypeReference& type_reference = entry.first;
1664 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001665 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001666 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001667 }
1668}
1669
David Brazdil58282f42016-01-14 12:45:10 +00001670void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001671 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1672 blocked_core_registers_[ZERO] = true;
1673 blocked_core_registers_[K0] = true;
1674 blocked_core_registers_[K1] = true;
1675 blocked_core_registers_[GP] = true;
1676 blocked_core_registers_[SP] = true;
1677 blocked_core_registers_[RA] = true;
1678
Lazar Trsicd9672662015-09-03 17:33:01 +02001679 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1680 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681 blocked_core_registers_[AT] = true;
1682 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001683 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001684 blocked_fpu_registers_[FTMP] = true;
1685
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001686 if (GetInstructionSetFeatures().HasMsa()) {
1687 // To be used just for MSA instructions.
1688 blocked_fpu_registers_[FTMP2] = true;
1689 }
1690
Alexey Frunze4dda3372015-06-01 18:31:49 -07001691 // Reserve suspend and thread registers.
1692 blocked_core_registers_[S0] = true;
1693 blocked_core_registers_[TR] = true;
1694
1695 // Reserve T9 for function calls
1696 blocked_core_registers_[T9] = true;
1697
Goran Jakovljevic782be112016-06-21 12:39:04 +02001698 if (GetGraph()->IsDebuggable()) {
1699 // Stubs do not save callee-save floating point registers. If the graph
1700 // is debuggable, we need to deal with these registers differently. For
1701 // now, just block them.
1702 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1703 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1704 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705 }
1706}
1707
Alexey Frunze4dda3372015-06-01 18:31:49 -07001708size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1709 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001710 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001711}
1712
1713size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1714 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001715 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001716}
1717
1718size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001719 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1720 FpuRegister(reg_id),
1721 SP,
1722 stack_index);
1723 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001724}
1725
1726size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001727 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1728 FpuRegister(reg_id),
1729 SP,
1730 stack_index);
1731 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001732}
1733
1734void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001735 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001736}
1737
1738void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001739 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740}
1741
Calin Juravle175dc732015-08-25 15:42:32 +01001742void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001743 HInstruction* instruction,
1744 uint32_t dex_pc,
1745 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001746 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001747 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001748 if (EntrypointRequiresStackMap(entrypoint)) {
1749 RecordPcInfo(instruction, dex_pc, slow_path);
1750 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001751}
1752
Alexey Frunze15958152017-02-09 19:08:30 -08001753void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1754 HInstruction* instruction,
1755 SlowPathCode* slow_path) {
1756 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1757 GenerateInvokeRuntime(entry_point_offset);
1758}
1759
1760void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1761 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1762 __ Jalr(T9);
1763 __ Nop();
1764}
1765
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1767 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001768 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1769 const size_t status_byte_offset =
1770 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1771 constexpr uint32_t shifted_initialized_value =
1772 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1773
1774 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1775 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001776 __ Bltuc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001777 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1778 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 __ Bind(slow_path->GetExitLabel());
1780}
1781
1782void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1783 __ Sync(0); // only stype 0 is supported
1784}
1785
1786void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1787 HBasicBlock* successor) {
1788 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001789 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1790
1791 if (slow_path == nullptr) {
1792 slow_path =
1793 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1794 instruction->SetSlowPath(slow_path);
1795 codegen_->AddSlowPath(slow_path);
1796 if (successor != nullptr) {
1797 DCHECK(successor->IsLoopHeader());
1798 }
1799 } else {
1800 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1801 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001802
1803 __ LoadFromOffset(kLoadUnsignedHalfword,
1804 TMP,
1805 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001806 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001807 if (successor == nullptr) {
1808 __ Bnezc(TMP, slow_path->GetEntryLabel());
1809 __ Bind(slow_path->GetReturnLabel());
1810 } else {
1811 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001812 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001813 // slow_path will return to GetLabelOf(successor).
1814 }
1815}
1816
1817InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1818 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001819 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001820 assembler_(codegen->GetAssembler()),
1821 codegen_(codegen) {}
1822
1823void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1824 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001825 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001827 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001828 case DataType::Type::kInt32:
1829 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001830 locations->SetInAt(0, Location::RequiresRegister());
1831 HInstruction* right = instruction->InputAt(1);
1832 bool can_use_imm = false;
1833 if (right->IsConstant()) {
1834 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1835 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1836 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001837 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001838 DCHECK(instruction->IsAdd() || instruction->IsSub());
1839 bool single_use = right->GetUses().HasExactlyOneElement();
1840 if (instruction->IsSub()) {
1841 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1842 imm = -imm;
1843 }
1844 }
1845 if (type == DataType::Type::kInt32) {
1846 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1847 } else {
1848 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1849 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001850 }
1851 }
1852 if (can_use_imm)
1853 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1854 else
1855 locations->SetInAt(1, Location::RequiresRegister());
1856 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1857 }
1858 break;
1859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 case DataType::Type::kFloat32:
1861 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001862 locations->SetInAt(0, Location::RequiresFpuRegister());
1863 locations->SetInAt(1, Location::RequiresFpuRegister());
1864 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1865 break;
1866
1867 default:
1868 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1869 }
1870}
1871
1872void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001874 LocationSummary* locations = instruction->GetLocations();
1875
1876 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001877 case DataType::Type::kInt32:
1878 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001879 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1880 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1881 Location rhs_location = locations->InAt(1);
1882
1883 GpuRegister rhs_reg = ZERO;
1884 int64_t rhs_imm = 0;
1885 bool use_imm = rhs_location.IsConstant();
1886 if (use_imm) {
1887 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1888 } else {
1889 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1890 }
1891
1892 if (instruction->IsAnd()) {
1893 if (use_imm)
1894 __ Andi(dst, lhs, rhs_imm);
1895 else
1896 __ And(dst, lhs, rhs_reg);
1897 } else if (instruction->IsOr()) {
1898 if (use_imm)
1899 __ Ori(dst, lhs, rhs_imm);
1900 else
1901 __ Or(dst, lhs, rhs_reg);
1902 } else if (instruction->IsXor()) {
1903 if (use_imm)
1904 __ Xori(dst, lhs, rhs_imm);
1905 else
1906 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001907 } else if (instruction->IsAdd() || instruction->IsSub()) {
1908 if (instruction->IsSub()) {
1909 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001910 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001911 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001912 if (use_imm) {
1913 if (IsInt<16>(rhs_imm)) {
1914 __ Addiu(dst, lhs, rhs_imm);
1915 } else {
1916 int16_t rhs_imm_high = High16Bits(rhs_imm);
1917 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1918 if (rhs_imm_low < 0) {
1919 rhs_imm_high += 1;
1920 }
1921 __ Aui(dst, lhs, rhs_imm_high);
1922 if (rhs_imm_low != 0) {
1923 __ Addiu(dst, dst, rhs_imm_low);
1924 }
1925 }
1926 } else {
1927 if (instruction->IsAdd()) {
1928 __ Addu(dst, lhs, rhs_reg);
1929 } else {
1930 DCHECK(instruction->IsSub());
1931 __ Subu(dst, lhs, rhs_reg);
1932 }
1933 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001934 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001935 if (use_imm) {
1936 if (IsInt<16>(rhs_imm)) {
1937 __ Daddiu(dst, lhs, rhs_imm);
1938 } else if (IsInt<32>(rhs_imm)) {
1939 int16_t rhs_imm_high = High16Bits(rhs_imm);
1940 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1941 bool overflow_hi16 = false;
1942 if (rhs_imm_low < 0) {
1943 rhs_imm_high += 1;
1944 overflow_hi16 = (rhs_imm_high == -32768);
1945 }
1946 __ Daui(dst, lhs, rhs_imm_high);
1947 if (rhs_imm_low != 0) {
1948 __ Daddiu(dst, dst, rhs_imm_low);
1949 }
1950 if (overflow_hi16) {
1951 __ Dahi(dst, 1);
1952 }
1953 } else {
1954 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1955 if (rhs_imm_low < 0) {
1956 rhs_imm += (INT64_C(1) << 16);
1957 }
1958 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
1959 if (rhs_imm_upper < 0) {
1960 rhs_imm += (INT64_C(1) << 32);
1961 }
1962 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
1963 if (rhs_imm_high < 0) {
1964 rhs_imm += (INT64_C(1) << 48);
1965 }
1966 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
1967 GpuRegister tmp = lhs;
1968 if (rhs_imm_low != 0) {
1969 __ Daddiu(dst, tmp, rhs_imm_low);
1970 tmp = dst;
1971 }
1972 // Dahi and Dati must use the same input and output register, so we have to initialize
1973 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
1974 // Daui(dst, lhs, 0).
1975 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
1976 __ Daui(dst, tmp, rhs_imm_upper);
1977 }
1978 if (rhs_imm_high != 0) {
1979 __ Dahi(dst, rhs_imm_high);
1980 }
1981 if (rhs_imm_top != 0) {
1982 __ Dati(dst, rhs_imm_top);
1983 }
1984 }
1985 } else if (instruction->IsAdd()) {
1986 __ Daddu(dst, lhs, rhs_reg);
1987 } else {
1988 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001989 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001990 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001991 }
1992 }
1993 break;
1994 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001995 case DataType::Type::kFloat32:
1996 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001997 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1998 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1999 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2000 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002001 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002002 __ AddS(dst, lhs, rhs);
2003 else
2004 __ AddD(dst, lhs, rhs);
2005 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002006 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002007 __ SubS(dst, lhs, rhs);
2008 else
2009 __ SubD(dst, lhs, rhs);
2010 } else {
2011 LOG(FATAL) << "Unexpected floating-point binary operation";
2012 }
2013 break;
2014 }
2015 default:
2016 LOG(FATAL) << "Unexpected binary operation type " << type;
2017 }
2018}
2019
2020void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002021 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002022
Vladimir Markoca6fff82017-10-03 14:49:14 +01002023 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002024 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002025 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002026 case DataType::Type::kInt32:
2027 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002028 locations->SetInAt(0, Location::RequiresRegister());
2029 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002030 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002031 break;
2032 }
2033 default:
2034 LOG(FATAL) << "Unexpected shift type " << type;
2035 }
2036}
2037
2038void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002039 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002042
2043 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kInt32:
2045 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002046 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2047 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2048 Location rhs_location = locations->InAt(1);
2049
2050 GpuRegister rhs_reg = ZERO;
2051 int64_t rhs_imm = 0;
2052 bool use_imm = rhs_location.IsConstant();
2053 if (use_imm) {
2054 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2055 } else {
2056 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2057 }
2058
2059 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002060 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002062
Alexey Frunze92d90602015-12-18 18:16:36 -08002063 if (shift_value == 0) {
2064 if (dst != lhs) {
2065 __ Move(dst, lhs);
2066 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002068 if (instr->IsShl()) {
2069 __ Sll(dst, lhs, shift_value);
2070 } else if (instr->IsShr()) {
2071 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002072 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002073 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002074 } else {
2075 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002076 }
2077 } else {
2078 if (shift_value < 32) {
2079 if (instr->IsShl()) {
2080 __ Dsll(dst, lhs, shift_value);
2081 } else if (instr->IsShr()) {
2082 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002083 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002084 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002085 } else {
2086 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002087 }
2088 } else {
2089 shift_value -= 32;
2090 if (instr->IsShl()) {
2091 __ Dsll32(dst, lhs, shift_value);
2092 } else if (instr->IsShr()) {
2093 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002094 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002095 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002096 } else {
2097 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002098 }
2099 }
2100 }
2101 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002103 if (instr->IsShl()) {
2104 __ Sllv(dst, lhs, rhs_reg);
2105 } else if (instr->IsShr()) {
2106 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002107 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002108 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002109 } else {
2110 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 }
2112 } else {
2113 if (instr->IsShl()) {
2114 __ Dsllv(dst, lhs, rhs_reg);
2115 } else if (instr->IsShr()) {
2116 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002117 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002118 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002119 } else {
2120 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121 }
2122 }
2123 }
2124 break;
2125 }
2126 default:
2127 LOG(FATAL) << "Unexpected shift operation type " << type;
2128 }
2129}
2130
2131void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2132 HandleBinaryOp(instruction);
2133}
2134
2135void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2136 HandleBinaryOp(instruction);
2137}
2138
2139void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2140 HandleBinaryOp(instruction);
2141}
2142
2143void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2144 HandleBinaryOp(instruction);
2145}
2146
2147void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002149 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002151 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002152 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2153 object_array_get_with_read_barrier
2154 ? LocationSummary::kCallOnSlowPath
2155 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002156 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2157 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2158 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002159 locations->SetInAt(0, Location::RequiresRegister());
2160 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002162 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2163 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002164 // The output overlaps in the case of an object array get with
2165 // read barriers enabled: we do not want the move to overwrite the
2166 // array's location, as we need it to emit the read barrier.
2167 locations->SetOut(Location::RequiresRegister(),
2168 object_array_get_with_read_barrier
2169 ? Location::kOutputOverlap
2170 : Location::kNoOutputOverlap);
2171 }
2172 // We need a temporary register for the read barrier marking slow
2173 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2174 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002175 bool temp_needed = instruction->GetIndex()->IsConstant()
2176 ? !kBakerReadBarrierThunksEnableForFields
2177 : !kBakerReadBarrierThunksEnableForArrays;
2178 if (temp_needed) {
2179 locations->AddTemp(Location::RequiresRegister());
2180 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002181 }
2182}
2183
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002184static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2185 auto null_checker = [codegen, instruction]() {
2186 codegen->MaybeRecordImplicitNullCheck(instruction);
2187 };
2188 return null_checker;
2189}
2190
Alexey Frunze4dda3372015-06-01 18:31:49 -07002191void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2192 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002193 Location obj_loc = locations->InAt(0);
2194 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2195 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002196 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002197 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002198 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002200 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002201 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2202 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002204 case DataType::Type::kBool:
2205 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002206 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002207 if (index.IsConstant()) {
2208 size_t offset =
2209 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002210 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002211 } else {
2212 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002213 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002214 }
2215 break;
2216 }
2217
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002218 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002219 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002220 if (index.IsConstant()) {
2221 size_t offset =
2222 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002223 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002224 } else {
2225 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002226 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002227 }
2228 break;
2229 }
2230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002232 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002233 if (maybe_compressed_char_at) {
2234 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002235 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002236 __ Dext(TMP, TMP, 0, 1);
2237 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2238 "Expecting 0=compressed, 1=uncompressed");
2239 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002240 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002241 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2242 if (maybe_compressed_char_at) {
2243 Mips64Label uncompressed_load, done;
2244 __ Bnezc(TMP, &uncompressed_load);
2245 __ LoadFromOffset(kLoadUnsignedByte,
2246 out,
2247 obj,
2248 data_offset + (const_index << TIMES_1));
2249 __ Bc(&done);
2250 __ Bind(&uncompressed_load);
2251 __ LoadFromOffset(kLoadUnsignedHalfword,
2252 out,
2253 obj,
2254 data_offset + (const_index << TIMES_2));
2255 __ Bind(&done);
2256 } else {
2257 __ LoadFromOffset(kLoadUnsignedHalfword,
2258 out,
2259 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002260 data_offset + (const_index << TIMES_2),
2261 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002262 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002263 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002264 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2265 if (maybe_compressed_char_at) {
2266 Mips64Label uncompressed_load, done;
2267 __ Bnezc(TMP, &uncompressed_load);
2268 __ Daddu(TMP, obj, index_reg);
2269 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2270 __ Bc(&done);
2271 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002272 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002273 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2274 __ Bind(&done);
2275 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002276 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002277 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002278 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002279 }
2280 break;
2281 }
2282
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002283 case DataType::Type::kInt16: {
2284 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2285 if (index.IsConstant()) {
2286 size_t offset =
2287 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2288 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2289 } else {
2290 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2291 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2292 }
2293 break;
2294 }
2295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002297 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002298 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 LoadOperandType load_type =
2300 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002301 if (index.IsConstant()) {
2302 size_t offset =
2303 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002304 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002305 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002306 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002307 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002308 }
2309 break;
2310 }
2311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002313 static_assert(
2314 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2315 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2316 // /* HeapReference<Object> */ out =
2317 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2318 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002319 bool temp_needed = index.IsConstant()
2320 ? !kBakerReadBarrierThunksEnableForFields
2321 : !kBakerReadBarrierThunksEnableForArrays;
2322 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002323 // Note that a potential implicit null check is handled in this
2324 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002325 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2326 if (index.IsConstant()) {
2327 // Array load with a constant index can be treated as a field load.
2328 size_t offset =
2329 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2330 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2331 out_loc,
2332 obj,
2333 offset,
2334 temp,
2335 /* needs_null_check */ false);
2336 } else {
2337 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2338 out_loc,
2339 obj,
2340 data_offset,
2341 index,
2342 temp,
2343 /* needs_null_check */ false);
2344 }
Alexey Frunze15958152017-02-09 19:08:30 -08002345 } else {
2346 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2347 if (index.IsConstant()) {
2348 size_t offset =
2349 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2350 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2351 // If read barriers are enabled, emit read barriers other than
2352 // Baker's using a slow path (and also unpoison the loaded
2353 // reference, if heap poisoning is enabled).
2354 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2355 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002356 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002357 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2358 // If read barriers are enabled, emit read barriers other than
2359 // Baker's using a slow path (and also unpoison the loaded
2360 // reference, if heap poisoning is enabled).
2361 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2362 out_loc,
2363 out_loc,
2364 obj_loc,
2365 data_offset,
2366 index);
2367 }
2368 }
2369 break;
2370 }
2371
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002373 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002374 if (index.IsConstant()) {
2375 size_t offset =
2376 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002377 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002378 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002379 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002380 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002381 }
2382 break;
2383 }
2384
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002386 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002387 if (index.IsConstant()) {
2388 size_t offset =
2389 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002390 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002391 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002392 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002393 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002394 }
2395 break;
2396 }
2397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002399 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400 if (index.IsConstant()) {
2401 size_t offset =
2402 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002403 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002404 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002405 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002406 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002407 }
2408 break;
2409 }
2410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002412 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2413 UNREACHABLE();
2414 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002415}
2416
2417void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002418 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002419 locations->SetInAt(0, Location::RequiresRegister());
2420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2421}
2422
2423void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2424 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002425 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002426 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2427 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2428 __ LoadFromOffset(kLoadWord, out, obj, offset);
2429 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002430 // Mask out compression flag from String's array length.
2431 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2432 __ Srl(out, out, 1u);
2433 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002434}
2435
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002436Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2437 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2438 ? Location::ConstantLocation(instruction->AsConstant())
2439 : Location::RequiresRegister();
2440}
2441
2442Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2443 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2444 // We can store a non-zero float or double constant without first loading it into the FPU,
2445 // but we should only prefer this if the constant has a single use.
2446 if (instruction->IsConstant() &&
2447 (instruction->AsConstant()->IsZeroBitPattern() ||
2448 instruction->GetUses().HasExactlyOneElement())) {
2449 return Location::ConstantLocation(instruction->AsConstant());
2450 // Otherwise fall through and require an FPU register for the constant.
2451 }
2452 return Location::RequiresFpuRegister();
2453}
2454
Alexey Frunze4dda3372015-06-01 18:31:49 -07002455void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002457
2458 bool needs_write_barrier =
2459 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2460 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2461
Vladimir Markoca6fff82017-10-03 14:49:14 +01002462 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002463 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002464 may_need_runtime_call_for_type_check ?
2465 LocationSummary::kCallOnSlowPath :
2466 LocationSummary::kNoCall);
2467
2468 locations->SetInAt(0, Location::RequiresRegister());
2469 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002470 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002471 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002472 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002473 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2474 }
2475 if (needs_write_barrier) {
2476 // Temporary register for the write barrier.
2477 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002478 }
2479}
2480
2481void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2482 LocationSummary* locations = instruction->GetLocations();
2483 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2484 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002485 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002487 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002488 bool needs_write_barrier =
2489 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002490 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002491 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002492
2493 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002495 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002498 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002499 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002500 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002501 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2502 }
2503 if (value_location.IsConstant()) {
2504 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2505 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2506 } else {
2507 GpuRegister value = value_location.AsRegister<GpuRegister>();
2508 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002509 }
2510 break;
2511 }
2512
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002513 case DataType::Type::kUint16:
2514 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002516 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002517 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002519 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002520 }
2521 if (value_location.IsConstant()) {
2522 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2523 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2524 } else {
2525 GpuRegister value = value_location.AsRegister<GpuRegister>();
2526 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002527 }
2528 break;
2529 }
2530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002532 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2533 if (index.IsConstant()) {
2534 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2535 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002536 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002537 }
2538 if (value_location.IsConstant()) {
2539 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2540 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2541 } else {
2542 GpuRegister value = value_location.AsRegister<GpuRegister>();
2543 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2544 }
2545 break;
2546 }
2547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002549 if (value_location.IsConstant()) {
2550 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002551 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002552 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002553 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002554 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002555 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002556 }
Alexey Frunze15958152017-02-09 19:08:30 -08002557 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2558 DCHECK_EQ(value, 0);
2559 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2560 DCHECK(!needs_write_barrier);
2561 DCHECK(!may_need_runtime_call_for_type_check);
2562 break;
2563 }
2564
2565 DCHECK(needs_write_barrier);
2566 GpuRegister value = value_location.AsRegister<GpuRegister>();
2567 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2568 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2569 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2570 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2571 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2572 Mips64Label done;
2573 SlowPathCodeMIPS64* slow_path = nullptr;
2574
2575 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002576 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002577 codegen_->AddSlowPath(slow_path);
2578 if (instruction->GetValueCanBeNull()) {
2579 Mips64Label non_zero;
2580 __ Bnezc(value, &non_zero);
2581 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2582 if (index.IsConstant()) {
2583 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002584 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002585 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002586 }
Alexey Frunze15958152017-02-09 19:08:30 -08002587 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2588 __ Bc(&done);
2589 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002590 }
Alexey Frunze15958152017-02-09 19:08:30 -08002591
2592 // Note that when read barriers are enabled, the type checks
2593 // are performed without read barriers. This is fine, even in
2594 // the case where a class object is in the from-space after
2595 // the flip, as a comparison involving such a type would not
2596 // produce a false positive; it may of course produce a false
2597 // negative, in which case we would take the ArraySet slow
2598 // path.
2599
2600 // /* HeapReference<Class> */ temp1 = obj->klass_
2601 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2602 __ MaybeUnpoisonHeapReference(temp1);
2603
2604 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2605 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2606 // /* HeapReference<Class> */ temp2 = value->klass_
2607 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2608 // If heap poisoning is enabled, no need to unpoison `temp1`
2609 // nor `temp2`, as we are comparing two poisoned references.
2610
2611 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2612 Mips64Label do_put;
2613 __ Beqc(temp1, temp2, &do_put);
2614 // If heap poisoning is enabled, the `temp1` reference has
2615 // not been unpoisoned yet; unpoison it now.
2616 __ MaybeUnpoisonHeapReference(temp1);
2617
2618 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2619 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2620 // If heap poisoning is enabled, no need to unpoison
2621 // `temp1`, as we are comparing against null below.
2622 __ Bnezc(temp1, slow_path->GetEntryLabel());
2623 __ Bind(&do_put);
2624 } else {
2625 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2626 }
2627 }
2628
2629 GpuRegister source = value;
2630 if (kPoisonHeapReferences) {
2631 // Note that in the case where `value` is a null reference,
2632 // we do not enter this block, as a null reference does not
2633 // need poisoning.
2634 __ Move(temp1, value);
2635 __ PoisonHeapReference(temp1);
2636 source = temp1;
2637 }
2638
2639 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2640 if (index.IsConstant()) {
2641 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002642 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002643 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002644 }
2645 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2646
2647 if (!may_need_runtime_call_for_type_check) {
2648 codegen_->MaybeRecordImplicitNullCheck(instruction);
2649 }
2650
2651 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2652
2653 if (done.IsLinked()) {
2654 __ Bind(&done);
2655 }
2656
2657 if (slow_path != nullptr) {
2658 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002659 }
2660 break;
2661 }
2662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002663 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002664 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002665 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002666 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002667 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002668 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002669 }
2670 if (value_location.IsConstant()) {
2671 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2672 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2673 } else {
2674 GpuRegister value = value_location.AsRegister<GpuRegister>();
2675 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002676 }
2677 break;
2678 }
2679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002681 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002682 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002683 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002684 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002685 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002686 }
2687 if (value_location.IsConstant()) {
2688 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2689 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2690 } else {
2691 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2692 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002693 }
2694 break;
2695 }
2696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002698 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002699 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002700 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002701 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002702 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002703 }
2704 if (value_location.IsConstant()) {
2705 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2706 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2707 } else {
2708 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2709 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002710 }
2711 break;
2712 }
2713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002715 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2716 UNREACHABLE();
2717 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002718}
2719
2720void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002721 RegisterSet caller_saves = RegisterSet::Empty();
2722 InvokeRuntimeCallingConvention calling_convention;
2723 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2724 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2725 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002726
2727 HInstruction* index = instruction->InputAt(0);
2728 HInstruction* length = instruction->InputAt(1);
2729
2730 bool const_index = false;
2731 bool const_length = false;
2732
2733 if (index->IsConstant()) {
2734 if (length->IsConstant()) {
2735 const_index = true;
2736 const_length = true;
2737 } else {
2738 int32_t index_value = index->AsIntConstant()->GetValue();
2739 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2740 const_index = true;
2741 }
2742 }
2743 } else if (length->IsConstant()) {
2744 int32_t length_value = length->AsIntConstant()->GetValue();
2745 if (IsUint<15>(length_value)) {
2746 const_length = true;
2747 }
2748 }
2749
2750 locations->SetInAt(0, const_index
2751 ? Location::ConstantLocation(index->AsConstant())
2752 : Location::RequiresRegister());
2753 locations->SetInAt(1, const_length
2754 ? Location::ConstantLocation(length->AsConstant())
2755 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002756}
2757
2758void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2759 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002760 Location index_loc = locations->InAt(0);
2761 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002762
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002763 if (length_loc.IsConstant()) {
2764 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2765 if (index_loc.IsConstant()) {
2766 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2767 if (index < 0 || index >= length) {
2768 BoundsCheckSlowPathMIPS64* slow_path =
2769 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2770 codegen_->AddSlowPath(slow_path);
2771 __ Bc(slow_path->GetEntryLabel());
2772 } else {
2773 // Nothing to be done.
2774 }
2775 return;
2776 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002777
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002778 BoundsCheckSlowPathMIPS64* slow_path =
2779 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2780 codegen_->AddSlowPath(slow_path);
2781 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2782 if (length == 0) {
2783 __ Bc(slow_path->GetEntryLabel());
2784 } else if (length == 1) {
2785 __ Bnezc(index, slow_path->GetEntryLabel());
2786 } else {
2787 DCHECK(IsUint<15>(length)) << length;
2788 __ Sltiu(TMP, index, length);
2789 __ Beqzc(TMP, slow_path->GetEntryLabel());
2790 }
2791 } else {
2792 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2793 BoundsCheckSlowPathMIPS64* slow_path =
2794 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2795 codegen_->AddSlowPath(slow_path);
2796 if (index_loc.IsConstant()) {
2797 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2798 if (index < 0) {
2799 __ Bc(slow_path->GetEntryLabel());
2800 } else if (index == 0) {
2801 __ Blezc(length, slow_path->GetEntryLabel());
2802 } else {
2803 DCHECK(IsInt<16>(index + 1)) << index;
2804 __ Sltiu(TMP, length, index + 1);
2805 __ Bnezc(TMP, slow_path->GetEntryLabel());
2806 }
2807 } else {
2808 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2809 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2810 }
2811 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002812}
2813
Alexey Frunze15958152017-02-09 19:08:30 -08002814// Temp is used for read barrier.
2815static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2816 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002817 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002818 (kUseBakerReadBarrier ||
2819 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2820 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2821 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2822 return 1;
2823 }
2824 return 0;
2825}
2826
2827// Extra temp is used for read barrier.
2828static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2829 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2830}
2831
Alexey Frunze4dda3372015-06-01 18:31:49 -07002832void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002833 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2834 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2835
2836 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2837 switch (type_check_kind) {
2838 case TypeCheckKind::kExactCheck:
2839 case TypeCheckKind::kAbstractClassCheck:
2840 case TypeCheckKind::kClassHierarchyCheck:
2841 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002842 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002843 ? LocationSummary::kCallOnSlowPath
2844 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2845 break;
2846 case TypeCheckKind::kArrayCheck:
2847 case TypeCheckKind::kUnresolvedCheck:
2848 case TypeCheckKind::kInterfaceCheck:
2849 call_kind = LocationSummary::kCallOnSlowPath;
2850 break;
2851 }
2852
Vladimir Markoca6fff82017-10-03 14:49:14 +01002853 LocationSummary* locations =
2854 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002855 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002856 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002857 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002858}
2859
2860void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002861 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002862 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002863 Location obj_loc = locations->InAt(0);
2864 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002865 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002866 Location temp_loc = locations->GetTemp(0);
2867 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2868 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2869 DCHECK_LE(num_temps, 2u);
2870 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002871 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2872 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2873 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2874 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2875 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2876 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2877 const uint32_t object_array_data_offset =
2878 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2879 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002880
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002881 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2882 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2883 // read barriers is done for performance and code size reasons.
2884 bool is_type_check_slow_path_fatal = false;
2885 if (!kEmitCompilerReadBarrier) {
2886 is_type_check_slow_path_fatal =
2887 (type_check_kind == TypeCheckKind::kExactCheck ||
2888 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2889 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2890 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2891 !instruction->CanThrowIntoCatchBlock();
2892 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002893 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002894 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2895 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002896 codegen_->AddSlowPath(slow_path);
2897
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002898 // Avoid this check if we know `obj` is not null.
2899 if (instruction->MustDoNullCheck()) {
2900 __ Beqzc(obj, &done);
2901 }
2902
2903 switch (type_check_kind) {
2904 case TypeCheckKind::kExactCheck:
2905 case TypeCheckKind::kArrayCheck: {
2906 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002907 GenerateReferenceLoadTwoRegisters(instruction,
2908 temp_loc,
2909 obj_loc,
2910 class_offset,
2911 maybe_temp2_loc,
2912 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002913 // Jump to slow path for throwing the exception or doing a
2914 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002915 __ Bnec(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002916 break;
2917 }
2918
2919 case TypeCheckKind::kAbstractClassCheck: {
2920 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002921 GenerateReferenceLoadTwoRegisters(instruction,
2922 temp_loc,
2923 obj_loc,
2924 class_offset,
2925 maybe_temp2_loc,
2926 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002927 // If the class is abstract, we eagerly fetch the super class of the
2928 // object to avoid doing a comparison we know will fail.
2929 Mips64Label loop;
2930 __ Bind(&loop);
2931 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002932 GenerateReferenceLoadOneRegister(instruction,
2933 temp_loc,
2934 super_offset,
2935 maybe_temp2_loc,
2936 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002937 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2938 // exception.
2939 __ Beqzc(temp, slow_path->GetEntryLabel());
2940 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002941 __ Bnec(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002942 break;
2943 }
2944
2945 case TypeCheckKind::kClassHierarchyCheck: {
2946 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002947 GenerateReferenceLoadTwoRegisters(instruction,
2948 temp_loc,
2949 obj_loc,
2950 class_offset,
2951 maybe_temp2_loc,
2952 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002953 // Walk over the class hierarchy to find a match.
2954 Mips64Label loop;
2955 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002956 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002957 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002958 GenerateReferenceLoadOneRegister(instruction,
2959 temp_loc,
2960 super_offset,
2961 maybe_temp2_loc,
2962 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002963 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2964 // exception. Otherwise, jump to the beginning of the loop.
2965 __ Bnezc(temp, &loop);
2966 __ Bc(slow_path->GetEntryLabel());
2967 break;
2968 }
2969
2970 case TypeCheckKind::kArrayObjectCheck: {
2971 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002972 GenerateReferenceLoadTwoRegisters(instruction,
2973 temp_loc,
2974 obj_loc,
2975 class_offset,
2976 maybe_temp2_loc,
2977 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002978 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002979 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002980 // Otherwise, we need to check that the object's class is a non-primitive array.
2981 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002982 GenerateReferenceLoadOneRegister(instruction,
2983 temp_loc,
2984 component_offset,
2985 maybe_temp2_loc,
2986 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002987 // If the component type is null, jump to the slow path to throw the exception.
2988 __ Beqzc(temp, slow_path->GetEntryLabel());
2989 // Otherwise, the object is indeed an array, further check that this component
2990 // type is not a primitive type.
2991 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2992 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2993 __ Bnezc(temp, slow_path->GetEntryLabel());
2994 break;
2995 }
2996
2997 case TypeCheckKind::kUnresolvedCheck:
2998 // We always go into the type check slow path for the unresolved check case.
2999 // We cannot directly call the CheckCast runtime entry point
3000 // without resorting to a type checking slow path here (i.e. by
3001 // calling InvokeRuntime directly), as it would require to
3002 // assign fixed registers for the inputs of this HInstanceOf
3003 // instruction (following the runtime calling convention), which
3004 // might be cluttered by the potential first read barrier
3005 // emission at the beginning of this method.
3006 __ Bc(slow_path->GetEntryLabel());
3007 break;
3008
3009 case TypeCheckKind::kInterfaceCheck: {
3010 // Avoid read barriers to improve performance of the fast path. We can not get false
3011 // positives by doing this.
3012 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003013 GenerateReferenceLoadTwoRegisters(instruction,
3014 temp_loc,
3015 obj_loc,
3016 class_offset,
3017 maybe_temp2_loc,
3018 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003019 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003020 GenerateReferenceLoadTwoRegisters(instruction,
3021 temp_loc,
3022 temp_loc,
3023 iftable_offset,
3024 maybe_temp2_loc,
3025 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003026 // Iftable is never null.
3027 __ Lw(TMP, temp, array_length_offset);
3028 // Loop through the iftable and check if any class matches.
3029 Mips64Label loop;
3030 __ Bind(&loop);
3031 __ Beqzc(TMP, slow_path->GetEntryLabel());
3032 __ Lwu(AT, temp, object_array_data_offset);
3033 __ MaybeUnpoisonHeapReference(AT);
3034 // Go to next interface.
3035 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3036 __ Addiu(TMP, TMP, -2);
3037 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003038 __ Bnec(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003039 break;
3040 }
3041 }
3042
3043 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003044 __ Bind(slow_path->GetExitLabel());
3045}
3046
3047void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3048 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003049 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003050 locations->SetInAt(0, Location::RequiresRegister());
3051 if (check->HasUses()) {
3052 locations->SetOut(Location::SameAsFirstInput());
3053 }
3054}
3055
3056void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3057 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003058 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059 check->GetLoadClass(),
3060 check,
3061 check->GetDexPc(),
3062 true);
3063 codegen_->AddSlowPath(slow_path);
3064 GenerateClassInitializationCheck(slow_path,
3065 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3066}
3067
3068void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003069 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003070
Vladimir Markoca6fff82017-10-03 14:49:14 +01003071 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003072
3073 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003075 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003076 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003078 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 case DataType::Type::kInt32:
3080 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003081 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003082 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3084 break;
3085
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 case DataType::Type::kFloat32:
3087 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003088 locations->SetInAt(0, Location::RequiresFpuRegister());
3089 locations->SetInAt(1, Location::RequiresFpuRegister());
3090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003091 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003092
3093 default:
3094 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3095 }
3096}
3097
3098void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3099 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003100 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003101 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003102
3103 // 0 if: left == right
3104 // 1 if: left > right
3105 // -1 if: left < right
3106 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003107 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003108 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003109 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003111 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kInt32:
3113 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003114 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003115 Location rhs_location = locations->InAt(1);
3116 bool use_imm = rhs_location.IsConstant();
3117 GpuRegister rhs = ZERO;
3118 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003119 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003120 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3121 if (value != 0) {
3122 rhs = AT;
3123 __ LoadConst64(rhs, value);
3124 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003125 } else {
3126 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3127 if (value != 0) {
3128 rhs = AT;
3129 __ LoadConst32(rhs, value);
3130 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003131 }
3132 } else {
3133 rhs = rhs_location.AsRegister<GpuRegister>();
3134 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003135 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003136 __ Slt(res, rhs, lhs);
3137 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003138 break;
3139 }
3140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003141 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003142 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3143 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3144 Mips64Label done;
3145 __ CmpEqS(FTMP, lhs, rhs);
3146 __ LoadConst32(res, 0);
3147 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003148 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003149 __ CmpLtS(FTMP, lhs, rhs);
3150 __ LoadConst32(res, -1);
3151 __ Bc1nez(FTMP, &done);
3152 __ LoadConst32(res, 1);
3153 } else {
3154 __ CmpLtS(FTMP, rhs, lhs);
3155 __ LoadConst32(res, 1);
3156 __ Bc1nez(FTMP, &done);
3157 __ LoadConst32(res, -1);
3158 }
3159 __ Bind(&done);
3160 break;
3161 }
3162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003164 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3165 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3166 Mips64Label done;
3167 __ CmpEqD(FTMP, lhs, rhs);
3168 __ LoadConst32(res, 0);
3169 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003170 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003171 __ CmpLtD(FTMP, lhs, rhs);
3172 __ LoadConst32(res, -1);
3173 __ Bc1nez(FTMP, &done);
3174 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003175 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003176 __ CmpLtD(FTMP, rhs, lhs);
3177 __ LoadConst32(res, 1);
3178 __ Bc1nez(FTMP, &done);
3179 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003180 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003181 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003182 break;
3183 }
3184
3185 default:
3186 LOG(FATAL) << "Unimplemented compare type " << in_type;
3187 }
3188}
3189
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003190void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003191 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003192 switch (instruction->InputAt(0)->GetType()) {
3193 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003194 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003195 locations->SetInAt(0, Location::RequiresRegister());
3196 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3197 break;
3198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 case DataType::Type::kFloat32:
3200 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003201 locations->SetInAt(0, Location::RequiresFpuRegister());
3202 locations->SetInAt(1, Location::RequiresFpuRegister());
3203 break;
3204 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003205 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003206 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3207 }
3208}
3209
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003210void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003211 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003212 return;
3213 }
3214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003216 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003217 switch (type) {
3218 default:
3219 // Integer case.
3220 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3221 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003222 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003223 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3224 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kFloat32:
3226 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003227 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3228 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003229 }
3230}
3231
Alexey Frunzec857c742015-09-23 15:12:39 -07003232void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3233 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003234 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003235
3236 LocationSummary* locations = instruction->GetLocations();
3237 Location second = locations->InAt(1);
3238 DCHECK(second.IsConstant());
3239
3240 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3241 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3242 int64_t imm = Int64FromConstant(second.GetConstant());
3243 DCHECK(imm == 1 || imm == -1);
3244
3245 if (instruction->IsRem()) {
3246 __ Move(out, ZERO);
3247 } else {
3248 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003250 __ Subu(out, ZERO, dividend);
3251 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003252 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003253 __ Dsubu(out, ZERO, dividend);
3254 }
3255 } else if (out != dividend) {
3256 __ Move(out, dividend);
3257 }
3258 }
3259}
3260
3261void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3262 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003264
3265 LocationSummary* locations = instruction->GetLocations();
3266 Location second = locations->InAt(1);
3267 DCHECK(second.IsConstant());
3268
3269 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3270 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3271 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003272 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003273 int ctz_imm = CTZ(abs_imm);
3274
3275 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003276 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003277 if (ctz_imm == 1) {
3278 // Fast path for division by +/-2, which is very common.
3279 __ Srl(TMP, dividend, 31);
3280 } else {
3281 __ Sra(TMP, dividend, 31);
3282 __ Srl(TMP, TMP, 32 - ctz_imm);
3283 }
3284 __ Addu(out, dividend, TMP);
3285 __ Sra(out, out, ctz_imm);
3286 if (imm < 0) {
3287 __ Subu(out, ZERO, out);
3288 }
3289 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003290 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003291 if (ctz_imm == 1) {
3292 // Fast path for division by +/-2, which is very common.
3293 __ Dsrl32(TMP, dividend, 31);
3294 } else {
3295 __ Dsra32(TMP, dividend, 31);
3296 if (ctz_imm > 32) {
3297 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3298 } else {
3299 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3300 }
3301 }
3302 __ Daddu(out, dividend, TMP);
3303 if (ctz_imm < 32) {
3304 __ Dsra(out, out, ctz_imm);
3305 } else {
3306 __ Dsra32(out, out, ctz_imm - 32);
3307 }
3308 if (imm < 0) {
3309 __ Dsubu(out, ZERO, out);
3310 }
3311 }
3312 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003313 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003314 if (ctz_imm == 1) {
3315 // Fast path for modulo +/-2, which is very common.
3316 __ Sra(TMP, dividend, 31);
3317 __ Subu(out, dividend, TMP);
3318 __ Andi(out, out, 1);
3319 __ Addu(out, out, TMP);
3320 } else {
3321 __ Sra(TMP, dividend, 31);
3322 __ Srl(TMP, TMP, 32 - ctz_imm);
3323 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003324 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003325 __ Subu(out, out, TMP);
3326 }
3327 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003328 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003329 if (ctz_imm == 1) {
3330 // Fast path for modulo +/-2, which is very common.
3331 __ Dsra32(TMP, dividend, 31);
3332 __ Dsubu(out, dividend, TMP);
3333 __ Andi(out, out, 1);
3334 __ Daddu(out, out, TMP);
3335 } else {
3336 __ Dsra32(TMP, dividend, 31);
3337 if (ctz_imm > 32) {
3338 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3339 } else {
3340 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3341 }
3342 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003343 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003344 __ Dsubu(out, out, TMP);
3345 }
3346 }
3347 }
3348}
3349
3350void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3351 DCHECK(instruction->IsDiv() || instruction->IsRem());
3352
3353 LocationSummary* locations = instruction->GetLocations();
3354 Location second = locations->InAt(1);
3355 DCHECK(second.IsConstant());
3356
3357 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3358 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3359 int64_t imm = Int64FromConstant(second.GetConstant());
3360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003361 DataType::Type type = instruction->GetResultType();
3362 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003363
3364 int64_t magic;
3365 int shift;
3366 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003367 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003368 &magic,
3369 &shift);
3370
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003371 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003372 __ LoadConst32(TMP, magic);
3373 __ MuhR6(TMP, dividend, TMP);
3374
3375 if (imm > 0 && magic < 0) {
3376 __ Addu(TMP, TMP, dividend);
3377 } else if (imm < 0 && magic > 0) {
3378 __ Subu(TMP, TMP, dividend);
3379 }
3380
3381 if (shift != 0) {
3382 __ Sra(TMP, TMP, shift);
3383 }
3384
3385 if (instruction->IsDiv()) {
3386 __ Sra(out, TMP, 31);
3387 __ Subu(out, TMP, out);
3388 } else {
3389 __ Sra(AT, TMP, 31);
3390 __ Subu(AT, TMP, AT);
3391 __ LoadConst32(TMP, imm);
3392 __ MulR6(TMP, AT, TMP);
3393 __ Subu(out, dividend, TMP);
3394 }
3395 } else {
3396 __ LoadConst64(TMP, magic);
3397 __ Dmuh(TMP, dividend, TMP);
3398
3399 if (imm > 0 && magic < 0) {
3400 __ Daddu(TMP, TMP, dividend);
3401 } else if (imm < 0 && magic > 0) {
3402 __ Dsubu(TMP, TMP, dividend);
3403 }
3404
3405 if (shift >= 32) {
3406 __ Dsra32(TMP, TMP, shift - 32);
3407 } else if (shift > 0) {
3408 __ Dsra(TMP, TMP, shift);
3409 }
3410
3411 if (instruction->IsDiv()) {
3412 __ Dsra32(out, TMP, 31);
3413 __ Dsubu(out, TMP, out);
3414 } else {
3415 __ Dsra32(AT, TMP, 31);
3416 __ Dsubu(AT, TMP, AT);
3417 __ LoadConst64(TMP, imm);
3418 __ Dmul(TMP, AT, TMP);
3419 __ Dsubu(out, dividend, TMP);
3420 }
3421 }
3422}
3423
3424void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3425 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003426 DataType::Type type = instruction->GetResultType();
3427 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003428
3429 LocationSummary* locations = instruction->GetLocations();
3430 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3431 Location second = locations->InAt(1);
3432
3433 if (second.IsConstant()) {
3434 int64_t imm = Int64FromConstant(second.GetConstant());
3435 if (imm == 0) {
3436 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3437 } else if (imm == 1 || imm == -1) {
3438 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003439 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003440 DivRemByPowerOfTwo(instruction);
3441 } else {
3442 DCHECK(imm <= -2 || imm >= 2);
3443 GenerateDivRemWithAnyConstant(instruction);
3444 }
3445 } else {
3446 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3447 GpuRegister divisor = second.AsRegister<GpuRegister>();
3448 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003449 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003450 __ DivR6(out, dividend, divisor);
3451 else
3452 __ Ddiv(out, dividend, divisor);
3453 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003454 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003455 __ ModR6(out, dividend, divisor);
3456 else
3457 __ Dmod(out, dividend, divisor);
3458 }
3459 }
3460}
3461
Alexey Frunze4dda3372015-06-01 18:31:49 -07003462void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3463 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003464 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003465 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 case DataType::Type::kInt32:
3467 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003468 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003469 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3471 break;
3472
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 locations->SetInAt(0, Location::RequiresFpuRegister());
3476 locations->SetInAt(1, Location::RequiresFpuRegister());
3477 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3478 break;
3479
3480 default:
3481 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3482 }
3483}
3484
3485void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003487 LocationSummary* locations = instruction->GetLocations();
3488
3489 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 case DataType::Type::kInt32:
3491 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003492 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003493 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003494 case DataType::Type::kFloat32:
3495 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003496 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3497 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3498 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003499 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003500 __ DivS(dst, lhs, rhs);
3501 else
3502 __ DivD(dst, lhs, rhs);
3503 break;
3504 }
3505 default:
3506 LOG(FATAL) << "Unexpected div type " << type;
3507 }
3508}
3509
3510void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003511 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003512 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003513}
3514
3515void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3516 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003517 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003518 codegen_->AddSlowPath(slow_path);
3519 Location value = instruction->GetLocations()->InAt(0);
3520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003524 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003525 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003526 }
3527
3528 if (value.IsConstant()) {
3529 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3530 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003531 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003532 } else {
3533 // A division by a non-null constant is valid. We don't need to perform
3534 // any check, so simply fall through.
3535 }
3536 } else {
3537 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3538 }
3539}
3540
3541void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3542 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003543 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003544 locations->SetOut(Location::ConstantLocation(constant));
3545}
3546
3547void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3548 // Will be generated at use site.
3549}
3550
3551void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3552 exit->SetLocations(nullptr);
3553}
3554
3555void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3556}
3557
3558void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3559 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003560 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561 locations->SetOut(Location::ConstantLocation(constant));
3562}
3563
3564void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3565 // Will be generated at use site.
3566}
3567
David Brazdilfc6a86a2015-06-26 10:33:45 +00003568void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003569 if (successor->IsExitBlock()) {
3570 DCHECK(got->GetPrevious()->AlwaysThrows());
3571 return; // no code needed
3572 }
3573
Alexey Frunze4dda3372015-06-01 18:31:49 -07003574 HBasicBlock* block = got->GetBlock();
3575 HInstruction* previous = got->GetPrevious();
3576 HLoopInformation* info = block->GetLoopInformation();
3577
3578 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003579 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3580 return;
3581 }
3582 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3583 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3584 }
3585 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003586 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587 }
3588}
3589
David Brazdilfc6a86a2015-06-26 10:33:45 +00003590void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3591 got->SetLocations(nullptr);
3592}
3593
3594void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3595 HandleGoto(got, got->GetSuccessor());
3596}
3597
3598void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3599 try_boundary->SetLocations(nullptr);
3600}
3601
3602void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3603 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3604 if (!successor->IsExitBlock()) {
3605 HandleGoto(try_boundary, successor);
3606 }
3607}
3608
Alexey Frunze299a9392015-12-08 16:08:02 -08003609void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3610 bool is64bit,
3611 LocationSummary* locations) {
3612 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3613 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3614 Location rhs_location = locations->InAt(1);
3615 GpuRegister rhs_reg = ZERO;
3616 int64_t rhs_imm = 0;
3617 bool use_imm = rhs_location.IsConstant();
3618 if (use_imm) {
3619 if (is64bit) {
3620 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3621 } else {
3622 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3623 }
3624 } else {
3625 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3626 }
3627 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3628
3629 switch (cond) {
3630 case kCondEQ:
3631 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003632 if (use_imm && IsInt<16>(-rhs_imm)) {
3633 if (rhs_imm == 0) {
3634 if (cond == kCondEQ) {
3635 __ Sltiu(dst, lhs, 1);
3636 } else {
3637 __ Sltu(dst, ZERO, lhs);
3638 }
3639 } else {
3640 if (is64bit) {
3641 __ Daddiu(dst, lhs, -rhs_imm);
3642 } else {
3643 __ Addiu(dst, lhs, -rhs_imm);
3644 }
3645 if (cond == kCondEQ) {
3646 __ Sltiu(dst, dst, 1);
3647 } else {
3648 __ Sltu(dst, ZERO, dst);
3649 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003650 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003651 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003652 if (use_imm && IsUint<16>(rhs_imm)) {
3653 __ Xori(dst, lhs, rhs_imm);
3654 } else {
3655 if (use_imm) {
3656 rhs_reg = TMP;
3657 __ LoadConst64(rhs_reg, rhs_imm);
3658 }
3659 __ Xor(dst, lhs, rhs_reg);
3660 }
3661 if (cond == kCondEQ) {
3662 __ Sltiu(dst, dst, 1);
3663 } else {
3664 __ Sltu(dst, ZERO, dst);
3665 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003666 }
3667 break;
3668
3669 case kCondLT:
3670 case kCondGE:
3671 if (use_imm && IsInt<16>(rhs_imm)) {
3672 __ Slti(dst, lhs, rhs_imm);
3673 } else {
3674 if (use_imm) {
3675 rhs_reg = TMP;
3676 __ LoadConst64(rhs_reg, rhs_imm);
3677 }
3678 __ Slt(dst, lhs, rhs_reg);
3679 }
3680 if (cond == kCondGE) {
3681 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3682 // only the slt instruction but no sge.
3683 __ Xori(dst, dst, 1);
3684 }
3685 break;
3686
3687 case kCondLE:
3688 case kCondGT:
3689 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3690 // Simulate lhs <= rhs via lhs < rhs + 1.
3691 __ Slti(dst, lhs, rhs_imm_plus_one);
3692 if (cond == kCondGT) {
3693 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3694 // only the slti instruction but no sgti.
3695 __ Xori(dst, dst, 1);
3696 }
3697 } else {
3698 if (use_imm) {
3699 rhs_reg = TMP;
3700 __ LoadConst64(rhs_reg, rhs_imm);
3701 }
3702 __ Slt(dst, rhs_reg, lhs);
3703 if (cond == kCondLE) {
3704 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3705 // only the slt instruction but no sle.
3706 __ Xori(dst, dst, 1);
3707 }
3708 }
3709 break;
3710
3711 case kCondB:
3712 case kCondAE:
3713 if (use_imm && IsInt<16>(rhs_imm)) {
3714 // Sltiu sign-extends its 16-bit immediate operand before
3715 // the comparison and thus lets us compare directly with
3716 // unsigned values in the ranges [0, 0x7fff] and
3717 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3718 __ Sltiu(dst, lhs, rhs_imm);
3719 } else {
3720 if (use_imm) {
3721 rhs_reg = TMP;
3722 __ LoadConst64(rhs_reg, rhs_imm);
3723 }
3724 __ Sltu(dst, lhs, rhs_reg);
3725 }
3726 if (cond == kCondAE) {
3727 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3728 // only the sltu instruction but no sgeu.
3729 __ Xori(dst, dst, 1);
3730 }
3731 break;
3732
3733 case kCondBE:
3734 case kCondA:
3735 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3736 // Simulate lhs <= rhs via lhs < rhs + 1.
3737 // Note that this only works if rhs + 1 does not overflow
3738 // to 0, hence the check above.
3739 // Sltiu sign-extends its 16-bit immediate operand before
3740 // the comparison and thus lets us compare directly with
3741 // unsigned values in the ranges [0, 0x7fff] and
3742 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3743 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3744 if (cond == kCondA) {
3745 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3746 // only the sltiu instruction but no sgtiu.
3747 __ Xori(dst, dst, 1);
3748 }
3749 } else {
3750 if (use_imm) {
3751 rhs_reg = TMP;
3752 __ LoadConst64(rhs_reg, rhs_imm);
3753 }
3754 __ Sltu(dst, rhs_reg, lhs);
3755 if (cond == kCondBE) {
3756 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3757 // only the sltu instruction but no sleu.
3758 __ Xori(dst, dst, 1);
3759 }
3760 }
3761 break;
3762 }
3763}
3764
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003765bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3766 bool is64bit,
3767 LocationSummary* input_locations,
3768 GpuRegister dst) {
3769 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3770 Location rhs_location = input_locations->InAt(1);
3771 GpuRegister rhs_reg = ZERO;
3772 int64_t rhs_imm = 0;
3773 bool use_imm = rhs_location.IsConstant();
3774 if (use_imm) {
3775 if (is64bit) {
3776 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3777 } else {
3778 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3779 }
3780 } else {
3781 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3782 }
3783 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3784
3785 switch (cond) {
3786 case kCondEQ:
3787 case kCondNE:
3788 if (use_imm && IsInt<16>(-rhs_imm)) {
3789 if (is64bit) {
3790 __ Daddiu(dst, lhs, -rhs_imm);
3791 } else {
3792 __ Addiu(dst, lhs, -rhs_imm);
3793 }
3794 } else if (use_imm && IsUint<16>(rhs_imm)) {
3795 __ Xori(dst, lhs, rhs_imm);
3796 } else {
3797 if (use_imm) {
3798 rhs_reg = TMP;
3799 __ LoadConst64(rhs_reg, rhs_imm);
3800 }
3801 __ Xor(dst, lhs, rhs_reg);
3802 }
3803 return (cond == kCondEQ);
3804
3805 case kCondLT:
3806 case kCondGE:
3807 if (use_imm && IsInt<16>(rhs_imm)) {
3808 __ Slti(dst, lhs, rhs_imm);
3809 } else {
3810 if (use_imm) {
3811 rhs_reg = TMP;
3812 __ LoadConst64(rhs_reg, rhs_imm);
3813 }
3814 __ Slt(dst, lhs, rhs_reg);
3815 }
3816 return (cond == kCondGE);
3817
3818 case kCondLE:
3819 case kCondGT:
3820 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3821 // Simulate lhs <= rhs via lhs < rhs + 1.
3822 __ Slti(dst, lhs, rhs_imm_plus_one);
3823 return (cond == kCondGT);
3824 } else {
3825 if (use_imm) {
3826 rhs_reg = TMP;
3827 __ LoadConst64(rhs_reg, rhs_imm);
3828 }
3829 __ Slt(dst, rhs_reg, lhs);
3830 return (cond == kCondLE);
3831 }
3832
3833 case kCondB:
3834 case kCondAE:
3835 if (use_imm && IsInt<16>(rhs_imm)) {
3836 // Sltiu sign-extends its 16-bit immediate operand before
3837 // the comparison and thus lets us compare directly with
3838 // unsigned values in the ranges [0, 0x7fff] and
3839 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3840 __ Sltiu(dst, lhs, rhs_imm);
3841 } else {
3842 if (use_imm) {
3843 rhs_reg = TMP;
3844 __ LoadConst64(rhs_reg, rhs_imm);
3845 }
3846 __ Sltu(dst, lhs, rhs_reg);
3847 }
3848 return (cond == kCondAE);
3849
3850 case kCondBE:
3851 case kCondA:
3852 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3853 // Simulate lhs <= rhs via lhs < rhs + 1.
3854 // Note that this only works if rhs + 1 does not overflow
3855 // to 0, hence the check above.
3856 // Sltiu sign-extends its 16-bit immediate operand before
3857 // the comparison and thus lets us compare directly with
3858 // unsigned values in the ranges [0, 0x7fff] and
3859 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3860 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3861 return (cond == kCondA);
3862 } else {
3863 if (use_imm) {
3864 rhs_reg = TMP;
3865 __ LoadConst64(rhs_reg, rhs_imm);
3866 }
3867 __ Sltu(dst, rhs_reg, lhs);
3868 return (cond == kCondBE);
3869 }
3870 }
3871}
3872
Alexey Frunze299a9392015-12-08 16:08:02 -08003873void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3874 bool is64bit,
3875 LocationSummary* locations,
3876 Mips64Label* label) {
3877 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3878 Location rhs_location = locations->InAt(1);
3879 GpuRegister rhs_reg = ZERO;
3880 int64_t rhs_imm = 0;
3881 bool use_imm = rhs_location.IsConstant();
3882 if (use_imm) {
3883 if (is64bit) {
3884 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3885 } else {
3886 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3887 }
3888 } else {
3889 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3890 }
3891
3892 if (use_imm && rhs_imm == 0) {
3893 switch (cond) {
3894 case kCondEQ:
3895 case kCondBE: // <= 0 if zero
3896 __ Beqzc(lhs, label);
3897 break;
3898 case kCondNE:
3899 case kCondA: // > 0 if non-zero
3900 __ Bnezc(lhs, label);
3901 break;
3902 case kCondLT:
3903 __ Bltzc(lhs, label);
3904 break;
3905 case kCondGE:
3906 __ Bgezc(lhs, label);
3907 break;
3908 case kCondLE:
3909 __ Blezc(lhs, label);
3910 break;
3911 case kCondGT:
3912 __ Bgtzc(lhs, label);
3913 break;
3914 case kCondB: // always false
3915 break;
3916 case kCondAE: // always true
3917 __ Bc(label);
3918 break;
3919 }
3920 } else {
3921 if (use_imm) {
3922 rhs_reg = TMP;
3923 __ LoadConst64(rhs_reg, rhs_imm);
3924 }
3925 switch (cond) {
3926 case kCondEQ:
3927 __ Beqc(lhs, rhs_reg, label);
3928 break;
3929 case kCondNE:
3930 __ Bnec(lhs, rhs_reg, label);
3931 break;
3932 case kCondLT:
3933 __ Bltc(lhs, rhs_reg, label);
3934 break;
3935 case kCondGE:
3936 __ Bgec(lhs, rhs_reg, label);
3937 break;
3938 case kCondLE:
3939 __ Bgec(rhs_reg, lhs, label);
3940 break;
3941 case kCondGT:
3942 __ Bltc(rhs_reg, lhs, label);
3943 break;
3944 case kCondB:
3945 __ Bltuc(lhs, rhs_reg, label);
3946 break;
3947 case kCondAE:
3948 __ Bgeuc(lhs, rhs_reg, label);
3949 break;
3950 case kCondBE:
3951 __ Bgeuc(rhs_reg, lhs, label);
3952 break;
3953 case kCondA:
3954 __ Bltuc(rhs_reg, lhs, label);
3955 break;
3956 }
3957 }
3958}
3959
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003960void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3961 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003962 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003963 LocationSummary* locations) {
3964 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3965 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3966 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003967 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003968 switch (cond) {
3969 case kCondEQ:
3970 __ CmpEqS(FTMP, lhs, rhs);
3971 __ Mfc1(dst, FTMP);
3972 __ Andi(dst, dst, 1);
3973 break;
3974 case kCondNE:
3975 __ CmpEqS(FTMP, lhs, rhs);
3976 __ Mfc1(dst, FTMP);
3977 __ Addiu(dst, dst, 1);
3978 break;
3979 case kCondLT:
3980 if (gt_bias) {
3981 __ CmpLtS(FTMP, lhs, rhs);
3982 } else {
3983 __ CmpUltS(FTMP, lhs, rhs);
3984 }
3985 __ Mfc1(dst, FTMP);
3986 __ Andi(dst, dst, 1);
3987 break;
3988 case kCondLE:
3989 if (gt_bias) {
3990 __ CmpLeS(FTMP, lhs, rhs);
3991 } else {
3992 __ CmpUleS(FTMP, lhs, rhs);
3993 }
3994 __ Mfc1(dst, FTMP);
3995 __ Andi(dst, dst, 1);
3996 break;
3997 case kCondGT:
3998 if (gt_bias) {
3999 __ CmpUltS(FTMP, rhs, lhs);
4000 } else {
4001 __ CmpLtS(FTMP, rhs, lhs);
4002 }
4003 __ Mfc1(dst, FTMP);
4004 __ Andi(dst, dst, 1);
4005 break;
4006 case kCondGE:
4007 if (gt_bias) {
4008 __ CmpUleS(FTMP, rhs, lhs);
4009 } else {
4010 __ CmpLeS(FTMP, rhs, lhs);
4011 }
4012 __ Mfc1(dst, FTMP);
4013 __ Andi(dst, dst, 1);
4014 break;
4015 default:
4016 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4017 UNREACHABLE();
4018 }
4019 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004020 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004021 switch (cond) {
4022 case kCondEQ:
4023 __ CmpEqD(FTMP, lhs, rhs);
4024 __ Mfc1(dst, FTMP);
4025 __ Andi(dst, dst, 1);
4026 break;
4027 case kCondNE:
4028 __ CmpEqD(FTMP, lhs, rhs);
4029 __ Mfc1(dst, FTMP);
4030 __ Addiu(dst, dst, 1);
4031 break;
4032 case kCondLT:
4033 if (gt_bias) {
4034 __ CmpLtD(FTMP, lhs, rhs);
4035 } else {
4036 __ CmpUltD(FTMP, lhs, rhs);
4037 }
4038 __ Mfc1(dst, FTMP);
4039 __ Andi(dst, dst, 1);
4040 break;
4041 case kCondLE:
4042 if (gt_bias) {
4043 __ CmpLeD(FTMP, lhs, rhs);
4044 } else {
4045 __ CmpUleD(FTMP, lhs, rhs);
4046 }
4047 __ Mfc1(dst, FTMP);
4048 __ Andi(dst, dst, 1);
4049 break;
4050 case kCondGT:
4051 if (gt_bias) {
4052 __ CmpUltD(FTMP, rhs, lhs);
4053 } else {
4054 __ CmpLtD(FTMP, rhs, lhs);
4055 }
4056 __ Mfc1(dst, FTMP);
4057 __ Andi(dst, dst, 1);
4058 break;
4059 case kCondGE:
4060 if (gt_bias) {
4061 __ CmpUleD(FTMP, rhs, lhs);
4062 } else {
4063 __ CmpLeD(FTMP, rhs, lhs);
4064 }
4065 __ Mfc1(dst, FTMP);
4066 __ Andi(dst, dst, 1);
4067 break;
4068 default:
4069 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4070 UNREACHABLE();
4071 }
4072 }
4073}
4074
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004075bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4076 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004077 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004078 LocationSummary* input_locations,
4079 FpuRegister dst) {
4080 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4081 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004083 switch (cond) {
4084 case kCondEQ:
4085 __ CmpEqS(dst, lhs, rhs);
4086 return false;
4087 case kCondNE:
4088 __ CmpEqS(dst, lhs, rhs);
4089 return true;
4090 case kCondLT:
4091 if (gt_bias) {
4092 __ CmpLtS(dst, lhs, rhs);
4093 } else {
4094 __ CmpUltS(dst, lhs, rhs);
4095 }
4096 return false;
4097 case kCondLE:
4098 if (gt_bias) {
4099 __ CmpLeS(dst, lhs, rhs);
4100 } else {
4101 __ CmpUleS(dst, lhs, rhs);
4102 }
4103 return false;
4104 case kCondGT:
4105 if (gt_bias) {
4106 __ CmpUltS(dst, rhs, lhs);
4107 } else {
4108 __ CmpLtS(dst, rhs, lhs);
4109 }
4110 return false;
4111 case kCondGE:
4112 if (gt_bias) {
4113 __ CmpUleS(dst, rhs, lhs);
4114 } else {
4115 __ CmpLeS(dst, rhs, lhs);
4116 }
4117 return false;
4118 default:
4119 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4120 UNREACHABLE();
4121 }
4122 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004123 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004124 switch (cond) {
4125 case kCondEQ:
4126 __ CmpEqD(dst, lhs, rhs);
4127 return false;
4128 case kCondNE:
4129 __ CmpEqD(dst, lhs, rhs);
4130 return true;
4131 case kCondLT:
4132 if (gt_bias) {
4133 __ CmpLtD(dst, lhs, rhs);
4134 } else {
4135 __ CmpUltD(dst, lhs, rhs);
4136 }
4137 return false;
4138 case kCondLE:
4139 if (gt_bias) {
4140 __ CmpLeD(dst, lhs, rhs);
4141 } else {
4142 __ CmpUleD(dst, lhs, rhs);
4143 }
4144 return false;
4145 case kCondGT:
4146 if (gt_bias) {
4147 __ CmpUltD(dst, rhs, lhs);
4148 } else {
4149 __ CmpLtD(dst, rhs, lhs);
4150 }
4151 return false;
4152 case kCondGE:
4153 if (gt_bias) {
4154 __ CmpUleD(dst, rhs, lhs);
4155 } else {
4156 __ CmpLeD(dst, rhs, lhs);
4157 }
4158 return false;
4159 default:
4160 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4161 UNREACHABLE();
4162 }
4163 }
4164}
4165
Alexey Frunze299a9392015-12-08 16:08:02 -08004166void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4167 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004168 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004169 LocationSummary* locations,
4170 Mips64Label* label) {
4171 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4172 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004173 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004174 switch (cond) {
4175 case kCondEQ:
4176 __ CmpEqS(FTMP, lhs, rhs);
4177 __ Bc1nez(FTMP, label);
4178 break;
4179 case kCondNE:
4180 __ CmpEqS(FTMP, lhs, rhs);
4181 __ Bc1eqz(FTMP, label);
4182 break;
4183 case kCondLT:
4184 if (gt_bias) {
4185 __ CmpLtS(FTMP, lhs, rhs);
4186 } else {
4187 __ CmpUltS(FTMP, lhs, rhs);
4188 }
4189 __ Bc1nez(FTMP, label);
4190 break;
4191 case kCondLE:
4192 if (gt_bias) {
4193 __ CmpLeS(FTMP, lhs, rhs);
4194 } else {
4195 __ CmpUleS(FTMP, lhs, rhs);
4196 }
4197 __ Bc1nez(FTMP, label);
4198 break;
4199 case kCondGT:
4200 if (gt_bias) {
4201 __ CmpUltS(FTMP, rhs, lhs);
4202 } else {
4203 __ CmpLtS(FTMP, rhs, lhs);
4204 }
4205 __ Bc1nez(FTMP, label);
4206 break;
4207 case kCondGE:
4208 if (gt_bias) {
4209 __ CmpUleS(FTMP, rhs, lhs);
4210 } else {
4211 __ CmpLeS(FTMP, rhs, lhs);
4212 }
4213 __ Bc1nez(FTMP, label);
4214 break;
4215 default:
4216 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004217 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004218 }
4219 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004220 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004221 switch (cond) {
4222 case kCondEQ:
4223 __ CmpEqD(FTMP, lhs, rhs);
4224 __ Bc1nez(FTMP, label);
4225 break;
4226 case kCondNE:
4227 __ CmpEqD(FTMP, lhs, rhs);
4228 __ Bc1eqz(FTMP, label);
4229 break;
4230 case kCondLT:
4231 if (gt_bias) {
4232 __ CmpLtD(FTMP, lhs, rhs);
4233 } else {
4234 __ CmpUltD(FTMP, lhs, rhs);
4235 }
4236 __ Bc1nez(FTMP, label);
4237 break;
4238 case kCondLE:
4239 if (gt_bias) {
4240 __ CmpLeD(FTMP, lhs, rhs);
4241 } else {
4242 __ CmpUleD(FTMP, lhs, rhs);
4243 }
4244 __ Bc1nez(FTMP, label);
4245 break;
4246 case kCondGT:
4247 if (gt_bias) {
4248 __ CmpUltD(FTMP, rhs, lhs);
4249 } else {
4250 __ CmpLtD(FTMP, rhs, lhs);
4251 }
4252 __ Bc1nez(FTMP, label);
4253 break;
4254 case kCondGE:
4255 if (gt_bias) {
4256 __ CmpUleD(FTMP, rhs, lhs);
4257 } else {
4258 __ CmpLeD(FTMP, rhs, lhs);
4259 }
4260 __ Bc1nez(FTMP, label);
4261 break;
4262 default:
4263 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004264 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004265 }
4266 }
4267}
4268
Alexey Frunze4dda3372015-06-01 18:31:49 -07004269void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004270 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004271 Mips64Label* true_target,
4272 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004273 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004274
David Brazdil0debae72015-11-12 18:37:00 +00004275 if (true_target == nullptr && false_target == nullptr) {
4276 // Nothing to do. The code always falls through.
4277 return;
4278 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004279 // Constant condition, statically compared against "true" (integer value 1).
4280 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004281 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004282 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004283 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004284 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004285 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004286 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004287 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004288 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004289 }
David Brazdil0debae72015-11-12 18:37:00 +00004290 return;
4291 }
4292
4293 // The following code generates these patterns:
4294 // (1) true_target == nullptr && false_target != nullptr
4295 // - opposite condition true => branch to false_target
4296 // (2) true_target != nullptr && false_target == nullptr
4297 // - condition true => branch to true_target
4298 // (3) true_target != nullptr && false_target != nullptr
4299 // - condition true => branch to true_target
4300 // - branch to false_target
4301 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004302 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004303 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004304 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004305 if (true_target == nullptr) {
4306 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4307 } else {
4308 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4309 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004310 } else {
4311 // The condition instruction has not been materialized, use its inputs as
4312 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004313 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004314 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004315 LocationSummary* locations = cond->GetLocations();
4316 IfCondition if_cond = condition->GetCondition();
4317 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004318
David Brazdil0debae72015-11-12 18:37:00 +00004319 if (true_target == nullptr) {
4320 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004321 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004322 }
4323
Alexey Frunze299a9392015-12-08 16:08:02 -08004324 switch (type) {
4325 default:
4326 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4327 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004328 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004329 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4330 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004331 case DataType::Type::kFloat32:
4332 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004333 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4334 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004335 }
4336 }
David Brazdil0debae72015-11-12 18:37:00 +00004337
4338 // If neither branch falls through (case 3), the conditional branch to `true_target`
4339 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4340 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004341 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004342 }
4343}
4344
4345void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004346 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004347 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004348 locations->SetInAt(0, Location::RequiresRegister());
4349 }
4350}
4351
4352void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004353 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4354 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004355 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004356 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004357 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004358 nullptr : codegen_->GetLabelOf(false_successor);
4359 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004360}
4361
4362void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004363 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004364 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004365 InvokeRuntimeCallingConvention calling_convention;
4366 RegisterSet caller_saves = RegisterSet::Empty();
4367 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4368 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004369 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004370 locations->SetInAt(0, Location::RequiresRegister());
4371 }
4372}
4373
4374void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004375 SlowPathCodeMIPS64* slow_path =
4376 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004377 GenerateTestAndBranch(deoptimize,
4378 /* condition_input_index */ 0,
4379 slow_path->GetEntryLabel(),
4380 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004381}
4382
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004383// This function returns true if a conditional move can be generated for HSelect.
4384// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4385// branches and regular moves.
4386//
4387// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4388//
4389// While determining feasibility of a conditional move and setting inputs/outputs
4390// are two distinct tasks, this function does both because they share quite a bit
4391// of common logic.
4392static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4393 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4394 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4395 HCondition* condition = cond->AsCondition();
4396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004397 DataType::Type cond_type =
4398 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4399 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004400
4401 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4402 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4403 bool is_true_value_zero_constant =
4404 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4405 bool is_false_value_zero_constant =
4406 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4407
4408 bool can_move_conditionally = false;
4409 bool use_const_for_false_in = false;
4410 bool use_const_for_true_in = false;
4411
4412 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004413 if (!DataType::IsFloatingPointType(cond_type)) {
4414 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004415 // Moving int/long on int/long condition.
4416 if (is_true_value_zero_constant) {
4417 // seleqz out_reg, false_reg, cond_reg
4418 can_move_conditionally = true;
4419 use_const_for_true_in = true;
4420 } else if (is_false_value_zero_constant) {
4421 // selnez out_reg, true_reg, cond_reg
4422 can_move_conditionally = true;
4423 use_const_for_false_in = true;
4424 } else if (materialized) {
4425 // Not materializing unmaterialized int conditions
4426 // to keep the instruction count low.
4427 // selnez AT, true_reg, cond_reg
4428 // seleqz TMP, false_reg, cond_reg
4429 // or out_reg, AT, TMP
4430 can_move_conditionally = true;
4431 }
4432 } else {
4433 // Moving float/double on int/long condition.
4434 if (materialized) {
4435 // Not materializing unmaterialized int conditions
4436 // to keep the instruction count low.
4437 can_move_conditionally = true;
4438 if (is_true_value_zero_constant) {
4439 // sltu TMP, ZERO, cond_reg
4440 // mtc1 TMP, temp_cond_reg
4441 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4442 use_const_for_true_in = true;
4443 } else if (is_false_value_zero_constant) {
4444 // sltu TMP, ZERO, cond_reg
4445 // mtc1 TMP, temp_cond_reg
4446 // selnez.fmt out_reg, true_reg, temp_cond_reg
4447 use_const_for_false_in = true;
4448 } else {
4449 // sltu TMP, ZERO, cond_reg
4450 // mtc1 TMP, temp_cond_reg
4451 // sel.fmt temp_cond_reg, false_reg, true_reg
4452 // mov.fmt out_reg, temp_cond_reg
4453 }
4454 }
4455 }
4456 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004457 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004458 // Moving int/long on float/double condition.
4459 can_move_conditionally = true;
4460 if (is_true_value_zero_constant) {
4461 // mfc1 TMP, temp_cond_reg
4462 // seleqz out_reg, false_reg, TMP
4463 use_const_for_true_in = true;
4464 } else if (is_false_value_zero_constant) {
4465 // mfc1 TMP, temp_cond_reg
4466 // selnez out_reg, true_reg, TMP
4467 use_const_for_false_in = true;
4468 } else {
4469 // mfc1 TMP, temp_cond_reg
4470 // selnez AT, true_reg, TMP
4471 // seleqz TMP, false_reg, TMP
4472 // or out_reg, AT, TMP
4473 }
4474 } else {
4475 // Moving float/double on float/double condition.
4476 can_move_conditionally = true;
4477 if (is_true_value_zero_constant) {
4478 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4479 use_const_for_true_in = true;
4480 } else if (is_false_value_zero_constant) {
4481 // selnez.fmt out_reg, true_reg, temp_cond_reg
4482 use_const_for_false_in = true;
4483 } else {
4484 // sel.fmt temp_cond_reg, false_reg, true_reg
4485 // mov.fmt out_reg, temp_cond_reg
4486 }
4487 }
4488 }
4489 }
4490
4491 if (can_move_conditionally) {
4492 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4493 } else {
4494 DCHECK(!use_const_for_false_in);
4495 DCHECK(!use_const_for_true_in);
4496 }
4497
4498 if (locations_to_set != nullptr) {
4499 if (use_const_for_false_in) {
4500 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4501 } else {
4502 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004503 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004504 ? Location::RequiresFpuRegister()
4505 : Location::RequiresRegister());
4506 }
4507 if (use_const_for_true_in) {
4508 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4509 } else {
4510 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004511 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004512 ? Location::RequiresFpuRegister()
4513 : Location::RequiresRegister());
4514 }
4515 if (materialized) {
4516 locations_to_set->SetInAt(2, Location::RequiresRegister());
4517 }
4518
4519 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004520 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004521 ? Location::RequiresFpuRegister()
4522 : Location::RequiresRegister());
4523 } else {
4524 locations_to_set->SetOut(Location::SameAsFirstInput());
4525 }
4526 }
4527
4528 return can_move_conditionally;
4529}
4530
4531
4532void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4533 LocationSummary* locations = select->GetLocations();
4534 Location dst = locations->Out();
4535 Location false_src = locations->InAt(0);
4536 Location true_src = locations->InAt(1);
4537 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4538 GpuRegister cond_reg = TMP;
4539 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004540 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004541 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004542 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004543
4544 if (IsBooleanValueOrMaterializedCondition(cond)) {
4545 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4546 } else {
4547 HCondition* condition = cond->AsCondition();
4548 LocationSummary* cond_locations = cond->GetLocations();
4549 IfCondition if_cond = condition->GetCondition();
4550 cond_type = condition->InputAt(0)->GetType();
4551 switch (cond_type) {
4552 default:
4553 cond_inverted = MaterializeIntLongCompare(if_cond,
4554 /* is64bit */ false,
4555 cond_locations,
4556 cond_reg);
4557 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004558 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004559 cond_inverted = MaterializeIntLongCompare(if_cond,
4560 /* is64bit */ true,
4561 cond_locations,
4562 cond_reg);
4563 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004564 case DataType::Type::kFloat32:
4565 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004566 cond_inverted = MaterializeFpCompare(if_cond,
4567 condition->IsGtBias(),
4568 cond_type,
4569 cond_locations,
4570 fcond_reg);
4571 break;
4572 }
4573 }
4574
4575 if (true_src.IsConstant()) {
4576 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4577 }
4578 if (false_src.IsConstant()) {
4579 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4580 }
4581
4582 switch (dst_type) {
4583 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004584 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004585 __ Mfc1(cond_reg, fcond_reg);
4586 }
4587 if (true_src.IsConstant()) {
4588 if (cond_inverted) {
4589 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4590 } else {
4591 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4592 }
4593 } else if (false_src.IsConstant()) {
4594 if (cond_inverted) {
4595 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4596 } else {
4597 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4598 }
4599 } else {
4600 DCHECK_NE(cond_reg, AT);
4601 if (cond_inverted) {
4602 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4603 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4604 } else {
4605 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4606 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4607 }
4608 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4609 }
4610 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004611 case DataType::Type::kFloat32: {
4612 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004613 // sel*.fmt tests bit 0 of the condition register, account for that.
4614 __ Sltu(TMP, ZERO, cond_reg);
4615 __ Mtc1(TMP, fcond_reg);
4616 }
4617 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4618 if (true_src.IsConstant()) {
4619 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4620 if (cond_inverted) {
4621 __ SelnezS(dst_reg, src_reg, fcond_reg);
4622 } else {
4623 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4624 }
4625 } else if (false_src.IsConstant()) {
4626 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4627 if (cond_inverted) {
4628 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4629 } else {
4630 __ SelnezS(dst_reg, src_reg, fcond_reg);
4631 }
4632 } else {
4633 if (cond_inverted) {
4634 __ SelS(fcond_reg,
4635 true_src.AsFpuRegister<FpuRegister>(),
4636 false_src.AsFpuRegister<FpuRegister>());
4637 } else {
4638 __ SelS(fcond_reg,
4639 false_src.AsFpuRegister<FpuRegister>(),
4640 true_src.AsFpuRegister<FpuRegister>());
4641 }
4642 __ MovS(dst_reg, fcond_reg);
4643 }
4644 break;
4645 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004646 case DataType::Type::kFloat64: {
4647 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004648 // sel*.fmt tests bit 0 of the condition register, account for that.
4649 __ Sltu(TMP, ZERO, cond_reg);
4650 __ Mtc1(TMP, fcond_reg);
4651 }
4652 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4653 if (true_src.IsConstant()) {
4654 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4655 if (cond_inverted) {
4656 __ SelnezD(dst_reg, src_reg, fcond_reg);
4657 } else {
4658 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4659 }
4660 } else if (false_src.IsConstant()) {
4661 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4662 if (cond_inverted) {
4663 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4664 } else {
4665 __ SelnezD(dst_reg, src_reg, fcond_reg);
4666 }
4667 } else {
4668 if (cond_inverted) {
4669 __ SelD(fcond_reg,
4670 true_src.AsFpuRegister<FpuRegister>(),
4671 false_src.AsFpuRegister<FpuRegister>());
4672 } else {
4673 __ SelD(fcond_reg,
4674 false_src.AsFpuRegister<FpuRegister>(),
4675 true_src.AsFpuRegister<FpuRegister>());
4676 }
4677 __ MovD(dst_reg, fcond_reg);
4678 }
4679 break;
4680 }
4681 }
4682}
4683
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004684void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004685 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004686 LocationSummary(flag, LocationSummary::kNoCall);
4687 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004688}
4689
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004690void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4691 __ LoadFromOffset(kLoadWord,
4692 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4693 SP,
4694 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004695}
4696
David Brazdil74eb1b22015-12-14 11:44:01 +00004697void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004698 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004699 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004700}
4701
4702void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004703 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4704 GenConditionalMove(select);
4705 } else {
4706 LocationSummary* locations = select->GetLocations();
4707 Mips64Label false_target;
4708 GenerateTestAndBranch(select,
4709 /* condition_input_index */ 2,
4710 /* true_target */ nullptr,
4711 &false_target);
4712 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4713 __ Bind(&false_target);
4714 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004715}
4716
David Srbecky0cf44932015-12-09 14:09:59 +00004717void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004718 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004719}
4720
David Srbeckyd28f4a02016-03-14 17:14:24 +00004721void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4722 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004723}
4724
4725void CodeGeneratorMIPS64::GenerateNop() {
4726 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004727}
4728
Alexey Frunze4dda3372015-06-01 18:31:49 -07004729void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004730 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004731 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004732 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004733 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004734 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004735 instruction,
4736 object_field_get_with_read_barrier
4737 ? LocationSummary::kCallOnSlowPath
4738 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004739 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4740 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004742 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004743 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004744 locations->SetOut(Location::RequiresFpuRegister());
4745 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004746 // The output overlaps in the case of an object field get with
4747 // read barriers enabled: we do not want the move to overwrite the
4748 // object's location, as we need it to emit the read barrier.
4749 locations->SetOut(Location::RequiresRegister(),
4750 object_field_get_with_read_barrier
4751 ? Location::kOutputOverlap
4752 : Location::kNoOutputOverlap);
4753 }
4754 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4755 // We need a temporary register for the read barrier marking slow
4756 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004757 if (!kBakerReadBarrierThunksEnableForFields) {
4758 locations->AddTemp(Location::RequiresRegister());
4759 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004760 }
4761}
4762
4763void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4764 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004765 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4766 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004767 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004768 Location obj_loc = locations->InAt(0);
4769 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4770 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004771 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004772 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004773 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004774 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4775
Alexey Frunze4dda3372015-06-01 18:31:49 -07004776 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004777 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004778 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004779 load_type = kLoadUnsignedByte;
4780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004781 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004782 load_type = kLoadSignedByte;
4783 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004784 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004785 load_type = kLoadUnsignedHalfword;
4786 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004787 case DataType::Type::kInt16:
4788 load_type = kLoadSignedHalfword;
4789 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004790 case DataType::Type::kInt32:
4791 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004792 load_type = kLoadWord;
4793 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004794 case DataType::Type::kInt64:
4795 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004796 load_type = kLoadDoubleword;
4797 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004799 load_type = kLoadUnsignedWord;
4800 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004801 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004802 LOG(FATAL) << "Unreachable type " << type;
4803 UNREACHABLE();
4804 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004806 DCHECK(dst_loc.IsRegister());
4807 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004808 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004809 // /* HeapReference<Object> */ dst = *(obj + offset)
4810 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004811 Location temp_loc =
4812 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004813 // Note that a potential implicit null check is handled in this
4814 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4815 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4816 dst_loc,
4817 obj,
4818 offset,
4819 temp_loc,
4820 /* needs_null_check */ true);
4821 if (is_volatile) {
4822 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4823 }
4824 } else {
4825 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4826 if (is_volatile) {
4827 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4828 }
4829 // If read barriers are enabled, emit read barriers other than
4830 // Baker's using a slow path (and also unpoison the loaded
4831 // reference, if heap poisoning is enabled).
4832 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4833 }
4834 } else {
4835 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4836 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004837 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004838 DCHECK(dst_loc.IsFpuRegister());
4839 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004840 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004841 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004842
Alexey Frunze15958152017-02-09 19:08:30 -08004843 // Memory barriers, in the case of references, are handled in the
4844 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004845 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004846 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004847 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004848}
4849
4850void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4851 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4852 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004853 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004854 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004855 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004856 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004857 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004858 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004859 }
4860}
4861
4862void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004863 const FieldInfo& field_info,
4864 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004866 LocationSummary* locations = instruction->GetLocations();
4867 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004868 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004869 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004870 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004871 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4872 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004873 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4874
Alexey Frunze4dda3372015-06-01 18:31:49 -07004875 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004876 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004877 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004878 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004879 store_type = kStoreByte;
4880 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004881 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004882 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004883 store_type = kStoreHalfword;
4884 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004885 case DataType::Type::kInt32:
4886 case DataType::Type::kFloat32:
4887 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004888 store_type = kStoreWord;
4889 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004890 case DataType::Type::kInt64:
4891 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004892 store_type = kStoreDoubleword;
4893 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004894 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004895 LOG(FATAL) << "Unreachable type " << type;
4896 UNREACHABLE();
4897 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004898
Alexey Frunze15958152017-02-09 19:08:30 -08004899 if (is_volatile) {
4900 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4901 }
4902
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004903 if (value_location.IsConstant()) {
4904 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4905 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4906 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004907 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004908 DCHECK(value_location.IsRegister());
4909 GpuRegister src = value_location.AsRegister<GpuRegister>();
4910 if (kPoisonHeapReferences && needs_write_barrier) {
4911 // Note that in the case where `value` is a null reference,
4912 // we do not enter this block, as a null reference does not
4913 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004914 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004915 __ PoisonHeapReference(TMP, src);
4916 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4917 } else {
4918 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4919 }
4920 } else {
4921 DCHECK(value_location.IsFpuRegister());
4922 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4923 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4924 }
4925 }
Alexey Frunze15958152017-02-09 19:08:30 -08004926
Alexey Frunzec061de12017-02-14 13:27:23 -08004927 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004928 DCHECK(value_location.IsRegister());
4929 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004930 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004931 }
Alexey Frunze15958152017-02-09 19:08:30 -08004932
4933 if (is_volatile) {
4934 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4935 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004936}
4937
4938void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4939 HandleFieldGet(instruction, instruction->GetFieldInfo());
4940}
4941
4942void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4943 HandleFieldGet(instruction, instruction->GetFieldInfo());
4944}
4945
4946void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4947 HandleFieldSet(instruction, instruction->GetFieldInfo());
4948}
4949
4950void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004951 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004952}
4953
Alexey Frunze15958152017-02-09 19:08:30 -08004954void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4955 HInstruction* instruction,
4956 Location out,
4957 uint32_t offset,
4958 Location maybe_temp,
4959 ReadBarrierOption read_barrier_option) {
4960 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4961 if (read_barrier_option == kWithReadBarrier) {
4962 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004963 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4964 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4965 }
Alexey Frunze15958152017-02-09 19:08:30 -08004966 if (kUseBakerReadBarrier) {
4967 // Load with fast path based Baker's read barrier.
4968 // /* HeapReference<Object> */ out = *(out + offset)
4969 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4970 out,
4971 out_reg,
4972 offset,
4973 maybe_temp,
4974 /* needs_null_check */ false);
4975 } else {
4976 // Load with slow path based read barrier.
4977 // Save the value of `out` into `maybe_temp` before overwriting it
4978 // in the following move operation, as we will need it for the
4979 // read barrier below.
4980 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4981 // /* HeapReference<Object> */ out = *(out + offset)
4982 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4983 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4984 }
4985 } else {
4986 // Plain load with no read barrier.
4987 // /* HeapReference<Object> */ out = *(out + offset)
4988 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4989 __ MaybeUnpoisonHeapReference(out_reg);
4990 }
4991}
4992
4993void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4994 HInstruction* instruction,
4995 Location out,
4996 Location obj,
4997 uint32_t offset,
4998 Location maybe_temp,
4999 ReadBarrierOption read_barrier_option) {
5000 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5001 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
5002 if (read_barrier_option == kWithReadBarrier) {
5003 CHECK(kEmitCompilerReadBarrier);
5004 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005005 if (!kBakerReadBarrierThunksEnableForFields) {
5006 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5007 }
Alexey Frunze15958152017-02-09 19:08:30 -08005008 // Load with fast path based Baker's read barrier.
5009 // /* HeapReference<Object> */ out = *(obj + offset)
5010 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5011 out,
5012 obj_reg,
5013 offset,
5014 maybe_temp,
5015 /* needs_null_check */ false);
5016 } else {
5017 // Load with slow path based read barrier.
5018 // /* HeapReference<Object> */ out = *(obj + offset)
5019 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5020 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5021 }
5022 } else {
5023 // Plain load with no read barrier.
5024 // /* HeapReference<Object> */ out = *(obj + offset)
5025 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5026 __ MaybeUnpoisonHeapReference(out_reg);
5027 }
5028}
5029
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005030static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5031 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5032 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5033 return reg - V0;
5034 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5035 return 13 + (reg - S2);
5036 } else if (reg == S8) { // One more.
5037 return 19;
5038 }
5039 LOG(FATAL) << "Unexpected register " << reg;
5040 UNREACHABLE();
5041}
5042
5043static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5044 int num = GetBakerMarkThunkNumber(reg) +
5045 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5046 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5047}
5048
5049static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5050 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5051 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5052}
5053
5054void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5055 Location root,
5056 GpuRegister obj,
5057 uint32_t offset,
5058 ReadBarrierOption read_barrier_option,
5059 Mips64Label* label_low) {
5060 if (label_low != nullptr) {
5061 DCHECK_EQ(offset, 0x5678u);
5062 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005063 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005064 if (read_barrier_option == kWithReadBarrier) {
5065 DCHECK(kEmitCompilerReadBarrier);
5066 if (kUseBakerReadBarrier) {
5067 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5068 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005069 if (kBakerReadBarrierThunksEnableForGcRoots) {
5070 // Note that we do not actually check the value of `GetIsGcMarking()`
5071 // to decide whether to mark the loaded GC root or not. Instead, we
5072 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5073 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5074 // vice versa.
5075 //
5076 // We use thunks for the slow path. That thunk checks the reference
5077 // and jumps to the entrypoint if needed.
5078 //
5079 // temp = Thread::Current()->pReadBarrierMarkReg00
5080 // // AKA &art_quick_read_barrier_mark_introspection.
5081 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5082 // if (temp != nullptr) {
5083 // temp = &gc_root_thunk<root_reg>
5084 // root = temp(root)
5085 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005086
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005087 const int32_t entry_point_offset =
5088 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5089 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5090 int16_t offset_low = Low16Bits(offset);
5091 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5092 // extension in lwu.
5093 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5094 GpuRegister base = short_offset ? obj : TMP;
5095 // Loading the entrypoint does not require a load acquire since it is only changed when
5096 // threads are suspended or running a checkpoint.
5097 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5098 if (!short_offset) {
5099 DCHECK(!label_low);
5100 __ Daui(base, obj, offset_high);
5101 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005102 Mips64Label skip_call;
5103 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005104 if (label_low != nullptr) {
5105 DCHECK(short_offset);
5106 __ Bind(label_low);
5107 }
5108 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5109 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5110 // in delay slot.
5111 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005112 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005113 } else {
5114 // Note that we do not actually check the value of `GetIsGcMarking()`
5115 // to decide whether to mark the loaded GC root or not. Instead, we
5116 // load into `temp` (T9) the read barrier mark entry point corresponding
5117 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5118 // is false, and vice versa.
5119 //
5120 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5121 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5122 // if (temp != null) {
5123 // root = temp(root)
5124 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005125
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005126 if (label_low != nullptr) {
5127 __ Bind(label_low);
5128 }
5129 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5130 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5131 static_assert(
5132 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5133 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5134 "have different sizes.");
5135 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5136 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5137 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005138
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005139 // Slow path marking the GC root `root`.
5140 Location temp = Location::RegisterLocation(T9);
5141 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005142 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005143 instruction,
5144 root,
5145 /*entrypoint*/ temp);
5146 codegen_->AddSlowPath(slow_path);
5147
5148 const int32_t entry_point_offset =
5149 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5150 // Loading the entrypoint does not require a load acquire since it is only changed when
5151 // threads are suspended or running a checkpoint.
5152 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5153 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5154 __ Bind(slow_path->GetExitLabel());
5155 }
Alexey Frunze15958152017-02-09 19:08:30 -08005156 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005157 if (label_low != nullptr) {
5158 __ Bind(label_low);
5159 }
Alexey Frunze15958152017-02-09 19:08:30 -08005160 // GC root loaded through a slow path for read barriers other
5161 // than Baker's.
5162 // /* GcRoot<mirror::Object>* */ root = obj + offset
5163 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5164 // /* mirror::Object* */ root = root->Read()
5165 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5166 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005167 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005168 if (label_low != nullptr) {
5169 __ Bind(label_low);
5170 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005171 // Plain GC root load with no read barrier.
5172 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5173 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5174 // Note that GC roots are not affected by heap poisoning, thus we
5175 // do not have to unpoison `root_reg` here.
5176 }
5177}
5178
Alexey Frunze15958152017-02-09 19:08:30 -08005179void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5180 Location ref,
5181 GpuRegister obj,
5182 uint32_t offset,
5183 Location temp,
5184 bool needs_null_check) {
5185 DCHECK(kEmitCompilerReadBarrier);
5186 DCHECK(kUseBakerReadBarrier);
5187
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005188 if (kBakerReadBarrierThunksEnableForFields) {
5189 // Note that we do not actually check the value of `GetIsGcMarking()`
5190 // to decide whether to mark the loaded reference or not. Instead, we
5191 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5192 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5193 // vice versa.
5194 //
5195 // We use thunks for the slow path. That thunk checks the reference
5196 // and jumps to the entrypoint if needed. If the holder is not gray,
5197 // it issues a load-load memory barrier and returns to the original
5198 // reference load.
5199 //
5200 // temp = Thread::Current()->pReadBarrierMarkReg00
5201 // // AKA &art_quick_read_barrier_mark_introspection.
5202 // if (temp != nullptr) {
5203 // temp = &field_array_thunk<holder_reg>
5204 // temp()
5205 // }
5206 // not_gray_return_address:
5207 // // If the offset is too large to fit into the lw instruction, we
5208 // // use an adjusted base register (TMP) here. This register
5209 // // receives bits 16 ... 31 of the offset before the thunk invocation
5210 // // and the thunk benefits from it.
5211 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5212 // gray_return_address:
5213
5214 DCHECK(temp.IsInvalid());
5215 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5216 const int32_t entry_point_offset =
5217 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5218 // There may have or may have not been a null check if the field offset is smaller than
5219 // the page size.
5220 // There must've been a null check in case it's actually a load from an array.
5221 // We will, however, perform an explicit null check in the thunk as it's easier to
5222 // do it than not.
5223 if (instruction->IsArrayGet()) {
5224 DCHECK(!needs_null_check);
5225 }
5226 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5227 // Loading the entrypoint does not require a load acquire since it is only changed when
5228 // threads are suspended or running a checkpoint.
5229 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5230 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005231 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005232 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005233 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005234 __ Nop(); // In forbidden slot.
5235 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005236 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005237 // /* HeapReference<Object> */ ref = *(obj + offset)
5238 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5239 } else {
5240 int16_t offset_low = Low16Bits(offset);
5241 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005242 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005243 __ Daui(TMP, obj, offset_high); // In delay slot.
5244 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005245 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005246 // /* HeapReference<Object> */ ref = *(obj + offset)
5247 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5248 }
5249 if (needs_null_check) {
5250 MaybeRecordImplicitNullCheck(instruction);
5251 }
5252 __ MaybeUnpoisonHeapReference(ref_reg);
5253 return;
5254 }
5255
Alexey Frunze15958152017-02-09 19:08:30 -08005256 // /* HeapReference<Object> */ ref = *(obj + offset)
5257 Location no_index = Location::NoLocation();
5258 ScaleFactor no_scale_factor = TIMES_1;
5259 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5260 ref,
5261 obj,
5262 offset,
5263 no_index,
5264 no_scale_factor,
5265 temp,
5266 needs_null_check);
5267}
5268
5269void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5270 Location ref,
5271 GpuRegister obj,
5272 uint32_t data_offset,
5273 Location index,
5274 Location temp,
5275 bool needs_null_check) {
5276 DCHECK(kEmitCompilerReadBarrier);
5277 DCHECK(kUseBakerReadBarrier);
5278
5279 static_assert(
5280 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5281 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005282 ScaleFactor scale_factor = TIMES_4;
5283
5284 if (kBakerReadBarrierThunksEnableForArrays) {
5285 // Note that we do not actually check the value of `GetIsGcMarking()`
5286 // to decide whether to mark the loaded reference or not. Instead, we
5287 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5288 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5289 // vice versa.
5290 //
5291 // We use thunks for the slow path. That thunk checks the reference
5292 // and jumps to the entrypoint if needed. If the holder is not gray,
5293 // it issues a load-load memory barrier and returns to the original
5294 // reference load.
5295 //
5296 // temp = Thread::Current()->pReadBarrierMarkReg00
5297 // // AKA &art_quick_read_barrier_mark_introspection.
5298 // if (temp != nullptr) {
5299 // temp = &field_array_thunk<holder_reg>
5300 // temp()
5301 // }
5302 // not_gray_return_address:
5303 // // The element address is pre-calculated in the TMP register before the
5304 // // thunk invocation and the thunk benefits from it.
5305 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5306 // gray_return_address:
5307
5308 DCHECK(temp.IsInvalid());
5309 DCHECK(index.IsValid());
5310 const int32_t entry_point_offset =
5311 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5312 // We will not do the explicit null check in the thunk as some form of a null check
5313 // must've been done earlier.
5314 DCHECK(!needs_null_check);
5315 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5316 // Loading the entrypoint does not require a load acquire since it is only changed when
5317 // threads are suspended or running a checkpoint.
5318 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005319 Mips64Label skip_call;
5320 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005321 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5322 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5323 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5324 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005325 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005326 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5327 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5328 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5329 __ MaybeUnpoisonHeapReference(ref_reg);
5330 return;
5331 }
5332
Alexey Frunze15958152017-02-09 19:08:30 -08005333 // /* HeapReference<Object> */ ref =
5334 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005335 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5336 ref,
5337 obj,
5338 data_offset,
5339 index,
5340 scale_factor,
5341 temp,
5342 needs_null_check);
5343}
5344
5345void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5346 Location ref,
5347 GpuRegister obj,
5348 uint32_t offset,
5349 Location index,
5350 ScaleFactor scale_factor,
5351 Location temp,
5352 bool needs_null_check,
5353 bool always_update_field) {
5354 DCHECK(kEmitCompilerReadBarrier);
5355 DCHECK(kUseBakerReadBarrier);
5356
5357 // In slow path based read barriers, the read barrier call is
5358 // inserted after the original load. However, in fast path based
5359 // Baker's read barriers, we need to perform the load of
5360 // mirror::Object::monitor_ *before* the original reference load.
5361 // This load-load ordering is required by the read barrier.
5362 // The fast path/slow path (for Baker's algorithm) should look like:
5363 //
5364 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5365 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5366 // HeapReference<Object> ref = *src; // Original reference load.
5367 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5368 // if (is_gray) {
5369 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5370 // }
5371 //
5372 // Note: the original implementation in ReadBarrier::Barrier is
5373 // slightly more complex as it performs additional checks that we do
5374 // not do here for performance reasons.
5375
5376 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5377 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5378 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5379
5380 // /* int32_t */ monitor = obj->monitor_
5381 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5382 if (needs_null_check) {
5383 MaybeRecordImplicitNullCheck(instruction);
5384 }
5385 // /* LockWord */ lock_word = LockWord(monitor)
5386 static_assert(sizeof(LockWord) == sizeof(int32_t),
5387 "art::LockWord and int32_t have different sizes.");
5388
5389 __ Sync(0); // Barrier to prevent load-load reordering.
5390
5391 // The actual reference load.
5392 if (index.IsValid()) {
5393 // Load types involving an "index": ArrayGet,
5394 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5395 // intrinsics.
5396 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5397 if (index.IsConstant()) {
5398 size_t computed_offset =
5399 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5400 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5401 } else {
5402 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005403 if (scale_factor == TIMES_1) {
5404 __ Daddu(TMP, index_reg, obj);
5405 } else {
5406 __ Dlsa(TMP, index_reg, obj, scale_factor);
5407 }
Alexey Frunze15958152017-02-09 19:08:30 -08005408 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5409 }
5410 } else {
5411 // /* HeapReference<Object> */ ref = *(obj + offset)
5412 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5413 }
5414
5415 // Object* ref = ref_addr->AsMirrorPtr()
5416 __ MaybeUnpoisonHeapReference(ref_reg);
5417
5418 // Slow path marking the object `ref` when it is gray.
5419 SlowPathCodeMIPS64* slow_path;
5420 if (always_update_field) {
5421 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5422 // of the form `obj + field_offset`, where `obj` is a register and
5423 // `field_offset` is a register. Thus `offset` and `scale_factor`
5424 // above are expected to be null in this code path.
5425 DCHECK_EQ(offset, 0u);
5426 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005427 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005428 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5429 ref,
5430 obj,
5431 /* field_offset */ index,
5432 temp_reg);
5433 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005434 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005435 }
5436 AddSlowPath(slow_path);
5437
5438 // if (rb_state == ReadBarrier::GrayState())
5439 // ref = ReadBarrier::Mark(ref);
5440 // Given the numeric representation, it's enough to check the low bit of the
5441 // rb_state. We do that by shifting the bit into the sign bit (31) and
5442 // performing a branch on less than zero.
5443 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5444 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5445 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5446 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5447 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5448 __ Bind(slow_path->GetExitLabel());
5449}
5450
5451void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5452 Location out,
5453 Location ref,
5454 Location obj,
5455 uint32_t offset,
5456 Location index) {
5457 DCHECK(kEmitCompilerReadBarrier);
5458
5459 // Insert a slow path based read barrier *after* the reference load.
5460 //
5461 // If heap poisoning is enabled, the unpoisoning of the loaded
5462 // reference will be carried out by the runtime within the slow
5463 // path.
5464 //
5465 // Note that `ref` currently does not get unpoisoned (when heap
5466 // poisoning is enabled), which is alright as the `ref` argument is
5467 // not used by the artReadBarrierSlow entry point.
5468 //
5469 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005470 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005471 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5472 AddSlowPath(slow_path);
5473
5474 __ Bc(slow_path->GetEntryLabel());
5475 __ Bind(slow_path->GetExitLabel());
5476}
5477
5478void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5479 Location out,
5480 Location ref,
5481 Location obj,
5482 uint32_t offset,
5483 Location index) {
5484 if (kEmitCompilerReadBarrier) {
5485 // Baker's read barriers shall be handled by the fast path
5486 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5487 DCHECK(!kUseBakerReadBarrier);
5488 // If heap poisoning is enabled, unpoisoning will be taken care of
5489 // by the runtime within the slow path.
5490 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5491 } else if (kPoisonHeapReferences) {
5492 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5493 }
5494}
5495
5496void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5497 Location out,
5498 Location root) {
5499 DCHECK(kEmitCompilerReadBarrier);
5500
5501 // Insert a slow path based read barrier *after* the GC root load.
5502 //
5503 // Note that GC roots are not affected by heap poisoning, so we do
5504 // not need to do anything special for this here.
5505 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005506 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005507 AddSlowPath(slow_path);
5508
5509 __ Bc(slow_path->GetEntryLabel());
5510 __ Bind(slow_path->GetExitLabel());
5511}
5512
Alexey Frunze4dda3372015-06-01 18:31:49 -07005513void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005514 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5515 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005516 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005517 switch (type_check_kind) {
5518 case TypeCheckKind::kExactCheck:
5519 case TypeCheckKind::kAbstractClassCheck:
5520 case TypeCheckKind::kClassHierarchyCheck:
5521 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005522 call_kind =
5523 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005524 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005525 break;
5526 case TypeCheckKind::kArrayCheck:
5527 case TypeCheckKind::kUnresolvedCheck:
5528 case TypeCheckKind::kInterfaceCheck:
5529 call_kind = LocationSummary::kCallOnSlowPath;
5530 break;
5531 }
5532
Vladimir Markoca6fff82017-10-03 14:49:14 +01005533 LocationSummary* locations =
5534 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005535 if (baker_read_barrier_slow_path) {
5536 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5537 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005538 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005539 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005540 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005541 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005542 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005543 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005544}
5545
5546void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005547 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005548 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005549 Location obj_loc = locations->InAt(0);
5550 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005551 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005552 Location out_loc = locations->Out();
5553 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5554 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5555 DCHECK_LE(num_temps, 1u);
5556 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005557 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5558 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5559 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5560 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005561 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005562 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005563
5564 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005565 // Avoid this check if we know `obj` is not null.
5566 if (instruction->MustDoNullCheck()) {
5567 __ Move(out, ZERO);
5568 __ Beqzc(obj, &done);
5569 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005570
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005571 switch (type_check_kind) {
5572 case TypeCheckKind::kExactCheck: {
5573 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005574 GenerateReferenceLoadTwoRegisters(instruction,
5575 out_loc,
5576 obj_loc,
5577 class_offset,
5578 maybe_temp_loc,
5579 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005580 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005581 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005582 __ Sltiu(out, out, 1);
5583 break;
5584 }
5585
5586 case TypeCheckKind::kAbstractClassCheck: {
5587 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005588 GenerateReferenceLoadTwoRegisters(instruction,
5589 out_loc,
5590 obj_loc,
5591 class_offset,
5592 maybe_temp_loc,
5593 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005594 // If the class is abstract, we eagerly fetch the super class of the
5595 // object to avoid doing a comparison we know will fail.
5596 Mips64Label loop;
5597 __ Bind(&loop);
5598 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005599 GenerateReferenceLoadOneRegister(instruction,
5600 out_loc,
5601 super_offset,
5602 maybe_temp_loc,
5603 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005604 // If `out` is null, we use it for the result, and jump to `done`.
5605 __ Beqzc(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005606 __ Bnec(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005607 __ LoadConst32(out, 1);
5608 break;
5609 }
5610
5611 case TypeCheckKind::kClassHierarchyCheck: {
5612 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005613 GenerateReferenceLoadTwoRegisters(instruction,
5614 out_loc,
5615 obj_loc,
5616 class_offset,
5617 maybe_temp_loc,
5618 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005619 // Walk over the class hierarchy to find a match.
5620 Mips64Label loop, success;
5621 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005622 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005623 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005624 GenerateReferenceLoadOneRegister(instruction,
5625 out_loc,
5626 super_offset,
5627 maybe_temp_loc,
5628 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005629 __ Bnezc(out, &loop);
5630 // If `out` is null, we use it for the result, and jump to `done`.
5631 __ Bc(&done);
5632 __ Bind(&success);
5633 __ LoadConst32(out, 1);
5634 break;
5635 }
5636
5637 case TypeCheckKind::kArrayObjectCheck: {
5638 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005639 GenerateReferenceLoadTwoRegisters(instruction,
5640 out_loc,
5641 obj_loc,
5642 class_offset,
5643 maybe_temp_loc,
5644 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005645 // Do an exact check.
5646 Mips64Label success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005647 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005648 // Otherwise, we need to check that the object's class is a non-primitive array.
5649 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005650 GenerateReferenceLoadOneRegister(instruction,
5651 out_loc,
5652 component_offset,
5653 maybe_temp_loc,
5654 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005655 // If `out` is null, we use it for the result, and jump to `done`.
5656 __ Beqzc(out, &done);
5657 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5658 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5659 __ Sltiu(out, out, 1);
5660 __ Bc(&done);
5661 __ Bind(&success);
5662 __ LoadConst32(out, 1);
5663 break;
5664 }
5665
5666 case TypeCheckKind::kArrayCheck: {
5667 // No read barrier since the slow path will retry upon failure.
5668 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005669 GenerateReferenceLoadTwoRegisters(instruction,
5670 out_loc,
5671 obj_loc,
5672 class_offset,
5673 maybe_temp_loc,
5674 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005675 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005676 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5677 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005678 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005679 __ Bnec(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005680 __ LoadConst32(out, 1);
5681 break;
5682 }
5683
5684 case TypeCheckKind::kUnresolvedCheck:
5685 case TypeCheckKind::kInterfaceCheck: {
5686 // Note that we indeed only call on slow path, but we always go
5687 // into the slow path for the unresolved and interface check
5688 // cases.
5689 //
5690 // We cannot directly call the InstanceofNonTrivial runtime
5691 // entry point without resorting to a type checking slow path
5692 // here (i.e. by calling InvokeRuntime directly), as it would
5693 // require to assign fixed registers for the inputs of this
5694 // HInstanceOf instruction (following the runtime calling
5695 // convention), which might be cluttered by the potential first
5696 // read barrier emission at the beginning of this method.
5697 //
5698 // TODO: Introduce a new runtime entry point taking the object
5699 // to test (instead of its class) as argument, and let it deal
5700 // with the read barrier issues. This will let us refactor this
5701 // case of the `switch` code as it was previously (with a direct
5702 // call to the runtime not using a type checking slow path).
5703 // This should also be beneficial for the other cases above.
5704 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005705 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5706 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005707 codegen_->AddSlowPath(slow_path);
5708 __ Bc(slow_path->GetEntryLabel());
5709 break;
5710 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005711 }
5712
5713 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005714
5715 if (slow_path != nullptr) {
5716 __ Bind(slow_path->GetExitLabel());
5717 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005718}
5719
5720void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005721 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005722 locations->SetOut(Location::ConstantLocation(constant));
5723}
5724
5725void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5726 // Will be generated at use site.
5727}
5728
5729void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005730 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005731 locations->SetOut(Location::ConstantLocation(constant));
5732}
5733
5734void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5735 // Will be generated at use site.
5736}
5737
Calin Juravle175dc732015-08-25 15:42:32 +01005738void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5739 // The trampoline uses the same calling convention as dex calling conventions,
5740 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5741 // the method_idx.
5742 HandleInvoke(invoke);
5743}
5744
5745void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5746 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5747}
5748
Alexey Frunze4dda3372015-06-01 18:31:49 -07005749void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5750 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5751 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5752}
5753
5754void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5755 HandleInvoke(invoke);
5756 // The register T0 is required to be used for the hidden argument in
5757 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5758 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5759}
5760
5761void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5762 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5763 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005764 Location receiver = invoke->GetLocations()->InAt(0);
5765 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005766 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005767
5768 // Set the hidden argument.
5769 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5770 invoke->GetDexMethodIndex());
5771
5772 // temp = object->GetClass();
5773 if (receiver.IsStackSlot()) {
5774 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5775 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5776 } else {
5777 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5778 }
5779 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005780 // Instead of simply (possibly) unpoisoning `temp` here, we should
5781 // emit a read barrier for the previous class reference load.
5782 // However this is not required in practice, as this is an
5783 // intermediate/temporary reference and because the current
5784 // concurrent copying collector keeps the from-space memory
5785 // intact/accessible until the end of the marking phase (the
5786 // concurrent copying collector may not in the future).
5787 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005788 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5789 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5790 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005791 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005792 // temp = temp->GetImtEntryAt(method_offset);
5793 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5794 // T9 = temp->GetEntryPoint();
5795 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5796 // T9();
5797 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005798 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005799 DCHECK(!codegen_->IsLeafMethod());
5800 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5801}
5802
5803void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005804 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5805 if (intrinsic.TryDispatch(invoke)) {
5806 return;
5807 }
5808
Alexey Frunze4dda3372015-06-01 18:31:49 -07005809 HandleInvoke(invoke);
5810}
5811
5812void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005813 // Explicit clinit checks triggered by static invokes must have been pruned by
5814 // art::PrepareForRegisterAllocation.
5815 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005816
Chris Larsen3039e382015-08-26 07:54:08 -07005817 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5818 if (intrinsic.TryDispatch(invoke)) {
5819 return;
5820 }
5821
Alexey Frunze4dda3372015-06-01 18:31:49 -07005822 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005823}
5824
Orion Hodsonac141392017-01-13 11:53:47 +00005825void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5826 HandleInvoke(invoke);
5827}
5828
5829void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5830 codegen_->GenerateInvokePolymorphicCall(invoke);
5831}
5832
Chris Larsen3039e382015-08-26 07:54:08 -07005833static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005834 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005835 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5836 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005837 return true;
5838 }
5839 return false;
5840}
5841
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005842HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005843 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005844 bool fallback_load = false;
5845 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005846 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005847 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005848 case HLoadString::LoadKind::kBssEntry:
5849 DCHECK(!Runtime::Current()->UseJitCompilation());
5850 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005851 case HLoadString::LoadKind::kJitTableAddress:
5852 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005853 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005854 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005855 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005856 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005857 }
5858 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005859 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005860 }
5861 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862}
5863
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005864HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5865 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005866 bool fallback_load = false;
5867 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005868 case HLoadClass::LoadKind::kInvalid:
5869 LOG(FATAL) << "UNREACHABLE";
5870 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005871 case HLoadClass::LoadKind::kReferrersClass:
5872 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005873 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005874 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005875 case HLoadClass::LoadKind::kBssEntry:
5876 DCHECK(!Runtime::Current()->UseJitCompilation());
5877 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005878 case HLoadClass::LoadKind::kJitTableAddress:
5879 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005880 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005881 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005882 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005883 break;
5884 }
5885 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005886 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005887 }
5888 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005889}
5890
Vladimir Markodc151b22015-10-15 18:02:30 +01005891HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5892 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005893 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005894 // On MIPS64 we support all dispatch types.
5895 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005896}
5897
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005898void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5899 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005900 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005901 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005902 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5903 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5904
Alexey Frunze19f6c692016-11-30 19:19:55 -08005905 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005906 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005907 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005908 uint32_t offset =
5909 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005910 __ LoadFromOffset(kLoadDoubleword,
5911 temp.AsRegister<GpuRegister>(),
5912 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005913 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005914 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005915 }
Vladimir Marko58155012015-08-19 12:49:41 +00005916 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005917 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005918 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005919 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5920 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005921 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005922 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005923 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5924 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5925 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005926 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5927 break;
5928 }
Vladimir Marko58155012015-08-19 12:49:41 +00005929 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005930 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5931 kLoadDoubleword,
5932 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005933 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005934 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005935 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005936 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005937 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5938 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5939 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005940 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5941 break;
5942 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005943 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5944 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5945 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005946 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005947 }
5948
Alexey Frunze19f6c692016-11-30 19:19:55 -08005949 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005950 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005951 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005952 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005953 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5954 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5955 __ LoadFromOffset(kLoadDoubleword,
5956 T9,
5957 callee_method.AsRegister<GpuRegister>(),
5958 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005959 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005960 // T9()
5961 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005962 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005963 break;
5964 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005965 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5966
Alexey Frunze4dda3372015-06-01 18:31:49 -07005967 DCHECK(!IsLeafMethod());
5968}
5969
5970void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005971 // Explicit clinit checks triggered by static invokes must have been pruned by
5972 // art::PrepareForRegisterAllocation.
5973 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005974
5975 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5976 return;
5977 }
5978
5979 LocationSummary* locations = invoke->GetLocations();
5980 codegen_->GenerateStaticOrDirectCall(invoke,
5981 locations->HasTemps()
5982 ? locations->GetTemp(0)
5983 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005984}
5985
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005986void CodeGeneratorMIPS64::GenerateVirtualCall(
5987 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005988 // Use the calling convention instead of the location of the receiver, as
5989 // intrinsics may have put the receiver in a different register. In the intrinsics
5990 // slow path, the arguments have been moved to the right place, so here we are
5991 // guaranteed that the receiver is the first register of the calling convention.
5992 InvokeDexCallingConvention calling_convention;
5993 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5994
Alexey Frunze53afca12015-11-05 16:34:23 -08005995 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005996 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5997 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5998 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005999 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006000
6001 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006002 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08006003 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006004 // Instead of simply (possibly) unpoisoning `temp` here, we should
6005 // emit a read barrier for the previous class reference load.
6006 // However this is not required in practice, as this is an
6007 // intermediate/temporary reference and because the current
6008 // concurrent copying collector keeps the from-space memory
6009 // intact/accessible until the end of the marking phase (the
6010 // concurrent copying collector may not in the future).
6011 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006012 // temp = temp->GetMethodAt(method_offset);
6013 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6014 // T9 = temp->GetEntryPoint();
6015 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6016 // T9();
6017 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006018 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006019 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006020}
6021
6022void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6023 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6024 return;
6025 }
6026
6027 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006028 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006029}
6030
6031void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006032 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006033 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006034 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006035 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6036 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006037 return;
6038 }
Vladimir Marko41559982017-01-06 14:04:23 +00006039 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006040
Alexey Frunze15958152017-02-09 19:08:30 -08006041 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6042 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006043 ? LocationSummary::kCallOnSlowPath
6044 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006045 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006046 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6047 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6048 }
Vladimir Marko41559982017-01-06 14:04:23 +00006049 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006050 locations->SetInAt(0, Location::RequiresRegister());
6051 }
6052 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006053 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6054 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6055 // Rely on the type resolution or initialization and marking to save everything we need.
6056 RegisterSet caller_saves = RegisterSet::Empty();
6057 InvokeRuntimeCallingConvention calling_convention;
6058 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6059 locations->SetCustomSlowPathCallerSaves(caller_saves);
6060 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006061 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006062 }
6063 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006064}
6065
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006066// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6067// move.
6068void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006069 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006070 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006071 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006072 return;
6073 }
Vladimir Marko41559982017-01-06 14:04:23 +00006074 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006075
Vladimir Marko41559982017-01-06 14:04:23 +00006076 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006077 Location out_loc = locations->Out();
6078 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6079 GpuRegister current_method_reg = ZERO;
6080 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006081 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006082 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6083 }
6084
Alexey Frunze15958152017-02-09 19:08:30 -08006085 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6086 ? kWithoutReadBarrier
6087 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006088 bool generate_null_check = false;
6089 switch (load_kind) {
6090 case HLoadClass::LoadKind::kReferrersClass:
6091 DCHECK(!cls->CanCallRuntime());
6092 DCHECK(!cls->MustGenerateClinitCheck());
6093 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6094 GenerateGcRootFieldLoad(cls,
6095 out_loc,
6096 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006097 ArtMethod::DeclaringClassOffset().Int32Value(),
6098 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006099 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006100 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006101 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006102 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006103 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006104 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006105 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6106 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6107 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006108 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6109 break;
6110 }
6111 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006112 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006113 uint32_t address = dchecked_integral_cast<uint32_t>(
6114 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6115 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006116 __ LoadLiteral(out,
6117 kLoadUnsignedWord,
6118 codegen_->DeduplicateBootImageAddressLiteral(address));
6119 break;
6120 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006121 case HLoadClass::LoadKind::kBootImageClassTable: {
6122 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6123 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6124 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6125 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6126 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6127 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6128 __ Lwu(out, AT, /* placeholder */ 0x5678);
6129 // Extract the reference from the slot data, i.e. clear the hash bits.
6130 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6131 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6132 if (masked_hash != 0) {
6133 __ Daddiu(out, out, -masked_hash);
6134 }
6135 break;
6136 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006137 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006138 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6139 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006140 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6141 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006142 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006143 GenerateGcRootFieldLoad(cls,
6144 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006145 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006146 /* placeholder */ 0x5678,
6147 read_barrier_option,
6148 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006149 generate_null_check = true;
6150 break;
6151 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006152 case HLoadClass::LoadKind::kJitTableAddress:
6153 __ LoadLiteral(out,
6154 kLoadUnsignedWord,
6155 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6156 cls->GetTypeIndex(),
6157 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006158 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006159 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006160 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006161 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006162 LOG(FATAL) << "UNREACHABLE";
6163 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006164 }
6165
6166 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6167 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006168 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006169 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006170 codegen_->AddSlowPath(slow_path);
6171 if (generate_null_check) {
6172 __ Beqzc(out, slow_path->GetEntryLabel());
6173 }
6174 if (cls->MustGenerateClinitCheck()) {
6175 GenerateClassInitializationCheck(slow_path, out);
6176 } else {
6177 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006178 }
6179 }
6180}
6181
David Brazdilcb1c0552015-08-04 16:22:25 +01006182static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006183 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006184}
6185
Alexey Frunze4dda3372015-06-01 18:31:49 -07006186void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6187 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006188 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006189 locations->SetOut(Location::RequiresRegister());
6190}
6191
6192void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6193 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006194 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6195}
6196
6197void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006198 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006199}
6200
6201void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6202 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006203}
6204
Alexey Frunze4dda3372015-06-01 18:31:49 -07006205void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006206 HLoadString::LoadKind load_kind = load->GetLoadKind();
6207 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006208 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006209 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006210 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006211 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006212 } else {
6213 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006214 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6215 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6216 // Rely on the pResolveString and marking to save everything we need.
6217 RegisterSet caller_saves = RegisterSet::Empty();
6218 InvokeRuntimeCallingConvention calling_convention;
6219 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6220 locations->SetCustomSlowPathCallerSaves(caller_saves);
6221 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006222 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006223 }
6224 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006225 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006226}
6227
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006228// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6229// move.
6230void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006231 HLoadString::LoadKind load_kind = load->GetLoadKind();
6232 LocationSummary* locations = load->GetLocations();
6233 Location out_loc = locations->Out();
6234 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6235
6236 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006237 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6238 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006239 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006240 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006241 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6242 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6243 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006244 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006245 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006246 }
6247 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006248 uint32_t address = dchecked_integral_cast<uint32_t>(
6249 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6250 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006251 __ LoadLiteral(out,
6252 kLoadUnsignedWord,
6253 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006254 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006255 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006256 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006257 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006258 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006259 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006260 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6261 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006262 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6263 __ Lwu(out, AT, /* placeholder */ 0x5678);
6264 return;
6265 }
6266 case HLoadString::LoadKind::kBssEntry: {
6267 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6268 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6269 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6270 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6271 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006272 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006273 GenerateGcRootFieldLoad(load,
6274 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006275 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006276 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006277 kCompilerReadBarrierOption,
6278 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006279 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006280 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006281 codegen_->AddSlowPath(slow_path);
6282 __ Beqzc(out, slow_path->GetEntryLabel());
6283 __ Bind(slow_path->GetExitLabel());
6284 return;
6285 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006286 case HLoadString::LoadKind::kJitTableAddress:
6287 __ LoadLiteral(out,
6288 kLoadUnsignedWord,
6289 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6290 load->GetStringIndex(),
6291 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006292 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006293 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006294 default:
6295 break;
6296 }
6297
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006298 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006299 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006300 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006301 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006302 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6303 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6304 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006305}
6306
Alexey Frunze4dda3372015-06-01 18:31:49 -07006307void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006308 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006309 locations->SetOut(Location::ConstantLocation(constant));
6310}
6311
6312void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6313 // Will be generated at use site.
6314}
6315
6316void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006317 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6318 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006319 InvokeRuntimeCallingConvention calling_convention;
6320 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6321}
6322
6323void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006324 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006325 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006326 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006327 if (instruction->IsEnter()) {
6328 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6329 } else {
6330 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6331 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006332}
6333
6334void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6335 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006336 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006337 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 case DataType::Type::kInt32:
6339 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006340 locations->SetInAt(0, Location::RequiresRegister());
6341 locations->SetInAt(1, Location::RequiresRegister());
6342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6343 break;
6344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 case DataType::Type::kFloat32:
6346 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006347 locations->SetInAt(0, Location::RequiresFpuRegister());
6348 locations->SetInAt(1, Location::RequiresFpuRegister());
6349 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6350 break;
6351
6352 default:
6353 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6354 }
6355}
6356
6357void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006358 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006359 LocationSummary* locations = instruction->GetLocations();
6360
6361 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 case DataType::Type::kInt32:
6363 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006364 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6365 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6366 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006367 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006368 __ MulR6(dst, lhs, rhs);
6369 else
6370 __ Dmul(dst, lhs, rhs);
6371 break;
6372 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 case DataType::Type::kFloat32:
6374 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006375 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6376 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6377 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006378 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006379 __ MulS(dst, lhs, rhs);
6380 else
6381 __ MulD(dst, lhs, rhs);
6382 break;
6383 }
6384 default:
6385 LOG(FATAL) << "Unexpected mul type " << type;
6386 }
6387}
6388
6389void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6390 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006391 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006392 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006393 case DataType::Type::kInt32:
6394 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006395 locations->SetInAt(0, Location::RequiresRegister());
6396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6397 break;
6398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 case DataType::Type::kFloat32:
6400 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006401 locations->SetInAt(0, Location::RequiresFpuRegister());
6402 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6403 break;
6404
6405 default:
6406 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6407 }
6408}
6409
6410void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006411 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006412 LocationSummary* locations = instruction->GetLocations();
6413
6414 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006415 case DataType::Type::kInt32:
6416 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006417 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6418 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006419 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006420 __ Subu(dst, ZERO, src);
6421 else
6422 __ Dsubu(dst, ZERO, src);
6423 break;
6424 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006425 case DataType::Type::kFloat32:
6426 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006427 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6428 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006429 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006430 __ NegS(dst, src);
6431 else
6432 __ NegD(dst, src);
6433 break;
6434 }
6435 default:
6436 LOG(FATAL) << "Unexpected neg type " << type;
6437 }
6438}
6439
6440void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006441 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6442 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006443 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006444 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006445 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6446 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006447}
6448
6449void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006450 // Note: if heap poisoning is enabled, the entry point takes care
6451 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006452 QuickEntrypointEnum entrypoint =
6453 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6454 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006455 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006456 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006457}
6458
6459void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006460 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6461 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006462 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006463 if (instruction->IsStringAlloc()) {
6464 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6465 } else {
6466 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006467 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006468 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006469}
6470
6471void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006472 // Note: if heap poisoning is enabled, the entry point takes care
6473 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006474 if (instruction->IsStringAlloc()) {
6475 // String is allocated through StringFactory. Call NewEmptyString entry point.
6476 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006477 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006478 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006479 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6480 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6481 __ Jalr(T9);
6482 __ Nop();
6483 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6484 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006485 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006486 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006487 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006488}
6489
6490void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006491 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006492 locations->SetInAt(0, Location::RequiresRegister());
6493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6494}
6495
6496void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006497 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006498 LocationSummary* locations = instruction->GetLocations();
6499
6500 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006501 case DataType::Type::kInt32:
6502 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006503 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6504 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6505 __ Nor(dst, src, ZERO);
6506 break;
6507 }
6508
6509 default:
6510 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6511 }
6512}
6513
6514void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006515 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006516 locations->SetInAt(0, Location::RequiresRegister());
6517 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6518}
6519
6520void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6521 LocationSummary* locations = instruction->GetLocations();
6522 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6523 locations->InAt(0).AsRegister<GpuRegister>(),
6524 1);
6525}
6526
6527void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006528 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6529 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006530}
6531
Calin Juravle2ae48182016-03-16 14:05:09 +00006532void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6533 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006534 return;
6535 }
6536 Location obj = instruction->GetLocations()->InAt(0);
6537
6538 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006539 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006540}
6541
Calin Juravle2ae48182016-03-16 14:05:09 +00006542void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006543 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006544 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006545 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006546
6547 Location obj = instruction->GetLocations()->InAt(0);
6548
6549 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6550}
6551
6552void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006553 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006554}
6555
6556void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6557 HandleBinaryOp(instruction);
6558}
6559
6560void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6561 HandleBinaryOp(instruction);
6562}
6563
6564void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6565 LOG(FATAL) << "Unreachable";
6566}
6567
6568void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006569 if (instruction->GetNext()->IsSuspendCheck() &&
6570 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6571 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6572 // The back edge will generate the suspend check.
6573 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6574 }
6575
Alexey Frunze4dda3372015-06-01 18:31:49 -07006576 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6577}
6578
6579void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006580 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006581 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6582 if (location.IsStackSlot()) {
6583 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6584 } else if (location.IsDoubleStackSlot()) {
6585 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6586 }
6587 locations->SetOut(location);
6588}
6589
6590void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6591 ATTRIBUTE_UNUSED) {
6592 // Nothing to do, the parameter is already at its location.
6593}
6594
6595void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6596 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006597 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006598 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6599}
6600
6601void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6602 ATTRIBUTE_UNUSED) {
6603 // Nothing to do, the method is already at its location.
6604}
6605
6606void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006607 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006608 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006609 locations->SetInAt(i, Location::Any());
6610 }
6611 locations->SetOut(Location::Any());
6612}
6613
6614void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6615 LOG(FATAL) << "Unreachable";
6616}
6617
6618void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006619 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006620 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006621 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6622 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006623 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006624
6625 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 case DataType::Type::kInt32:
6627 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006628 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006629 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6631 break;
6632
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006633 case DataType::Type::kFloat32:
6634 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006635 InvokeRuntimeCallingConvention calling_convention;
6636 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6637 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6638 locations->SetOut(calling_convention.GetReturnLocation(type));
6639 break;
6640 }
6641
6642 default:
6643 LOG(FATAL) << "Unexpected rem type " << type;
6644 }
6645}
6646
6647void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006648 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006649
6650 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006651 case DataType::Type::kInt32:
6652 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006653 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006654 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006656 case DataType::Type::kFloat32:
6657 case DataType::Type::kFloat64: {
6658 QuickEntrypointEnum entrypoint =
6659 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006660 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006661 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006662 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6663 } else {
6664 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6665 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006666 break;
6667 }
6668 default:
6669 LOG(FATAL) << "Unexpected rem type " << type;
6670 }
6671}
6672
Igor Murashkind01745e2017-04-05 16:40:31 -07006673void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6674 constructor_fence->SetLocations(nullptr);
6675}
6676
6677void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6678 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6679 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6680}
6681
Alexey Frunze4dda3372015-06-01 18:31:49 -07006682void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6683 memory_barrier->SetLocations(nullptr);
6684}
6685
6686void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6687 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6688}
6689
6690void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006692 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006693 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6694}
6695
6696void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6697 codegen_->GenerateFrameExit();
6698}
6699
6700void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6701 ret->SetLocations(nullptr);
6702}
6703
6704void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6705 codegen_->GenerateFrameExit();
6706}
6707
Alexey Frunze92d90602015-12-18 18:16:36 -08006708void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6709 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006710}
6711
Alexey Frunze92d90602015-12-18 18:16:36 -08006712void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6713 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006714}
6715
Alexey Frunze4dda3372015-06-01 18:31:49 -07006716void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6717 HandleShift(shl);
6718}
6719
6720void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6721 HandleShift(shl);
6722}
6723
6724void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6725 HandleShift(shr);
6726}
6727
6728void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6729 HandleShift(shr);
6730}
6731
Alexey Frunze4dda3372015-06-01 18:31:49 -07006732void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6733 HandleBinaryOp(instruction);
6734}
6735
6736void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6737 HandleBinaryOp(instruction);
6738}
6739
6740void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6741 HandleFieldGet(instruction, instruction->GetFieldInfo());
6742}
6743
6744void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6745 HandleFieldGet(instruction, instruction->GetFieldInfo());
6746}
6747
6748void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6749 HandleFieldSet(instruction, instruction->GetFieldInfo());
6750}
6751
6752void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006753 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006754}
6755
Calin Juravlee460d1d2015-09-29 04:52:17 +01006756void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6757 HUnresolvedInstanceFieldGet* instruction) {
6758 FieldAccessCallingConventionMIPS64 calling_convention;
6759 codegen_->CreateUnresolvedFieldLocationSummary(
6760 instruction, instruction->GetFieldType(), calling_convention);
6761}
6762
6763void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6764 HUnresolvedInstanceFieldGet* instruction) {
6765 FieldAccessCallingConventionMIPS64 calling_convention;
6766 codegen_->GenerateUnresolvedFieldAccess(instruction,
6767 instruction->GetFieldType(),
6768 instruction->GetFieldIndex(),
6769 instruction->GetDexPc(),
6770 calling_convention);
6771}
6772
6773void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6774 HUnresolvedInstanceFieldSet* instruction) {
6775 FieldAccessCallingConventionMIPS64 calling_convention;
6776 codegen_->CreateUnresolvedFieldLocationSummary(
6777 instruction, instruction->GetFieldType(), calling_convention);
6778}
6779
6780void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6781 HUnresolvedInstanceFieldSet* instruction) {
6782 FieldAccessCallingConventionMIPS64 calling_convention;
6783 codegen_->GenerateUnresolvedFieldAccess(instruction,
6784 instruction->GetFieldType(),
6785 instruction->GetFieldIndex(),
6786 instruction->GetDexPc(),
6787 calling_convention);
6788}
6789
6790void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6791 HUnresolvedStaticFieldGet* instruction) {
6792 FieldAccessCallingConventionMIPS64 calling_convention;
6793 codegen_->CreateUnresolvedFieldLocationSummary(
6794 instruction, instruction->GetFieldType(), calling_convention);
6795}
6796
6797void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6798 HUnresolvedStaticFieldGet* instruction) {
6799 FieldAccessCallingConventionMIPS64 calling_convention;
6800 codegen_->GenerateUnresolvedFieldAccess(instruction,
6801 instruction->GetFieldType(),
6802 instruction->GetFieldIndex(),
6803 instruction->GetDexPc(),
6804 calling_convention);
6805}
6806
6807void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6808 HUnresolvedStaticFieldSet* instruction) {
6809 FieldAccessCallingConventionMIPS64 calling_convention;
6810 codegen_->CreateUnresolvedFieldLocationSummary(
6811 instruction, instruction->GetFieldType(), calling_convention);
6812}
6813
6814void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6815 HUnresolvedStaticFieldSet* instruction) {
6816 FieldAccessCallingConventionMIPS64 calling_convention;
6817 codegen_->GenerateUnresolvedFieldAccess(instruction,
6818 instruction->GetFieldType(),
6819 instruction->GetFieldIndex(),
6820 instruction->GetDexPc(),
6821 calling_convention);
6822}
6823
Alexey Frunze4dda3372015-06-01 18:31:49 -07006824void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006825 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6826 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006827 // In suspend check slow path, usually there are no caller-save registers at all.
6828 // If SIMD instructions are present, however, we force spilling all live SIMD
6829 // registers in full width (since the runtime only saves/restores lower part).
6830 locations->SetCustomSlowPathCallerSaves(
6831 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006832}
6833
6834void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6835 HBasicBlock* block = instruction->GetBlock();
6836 if (block->GetLoopInformation() != nullptr) {
6837 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6838 // The back edge will generate the suspend check.
6839 return;
6840 }
6841 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6842 // The goto will generate the suspend check.
6843 return;
6844 }
6845 GenerateSuspendCheck(instruction, nullptr);
6846}
6847
Alexey Frunze4dda3372015-06-01 18:31:49 -07006848void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006849 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6850 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006851 InvokeRuntimeCallingConvention calling_convention;
6852 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6853}
6854
6855void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006856 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006857 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6858}
6859
6860void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006861 DataType::Type input_type = conversion->GetInputType();
6862 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006863 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6864 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6867 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006868 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6869 }
6870
Vladimir Markoca6fff82017-10-03 14:49:14 +01006871 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006872
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006873 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006874 locations->SetInAt(0, Location::RequiresFpuRegister());
6875 } else {
6876 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006877 }
6878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006879 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006880 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006881 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006882 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006883 }
6884}
6885
6886void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6887 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006888 DataType::Type result_type = conversion->GetResultType();
6889 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006890
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006891 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6892 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006894 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006895 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6896 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6897
6898 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006899 case DataType::Type::kUint8:
6900 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006901 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006902 case DataType::Type::kInt8:
6903 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006904 // Type conversion from long to types narrower than int is a result of code
6905 // transformations. To avoid unpredictable results for SEB and SEH, we first
6906 // need to sign-extend the low 32-bit value into bits 32 through 63.
6907 __ Sll(dst, src, 0);
6908 __ Seb(dst, dst);
6909 } else {
6910 __ Seb(dst, src);
6911 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006912 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006913 case DataType::Type::kUint16:
6914 __ Andi(dst, src, 0xFFFF);
6915 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006916 case DataType::Type::kInt16:
6917 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006918 // Type conversion from long to types narrower than int is a result of code
6919 // transformations. To avoid unpredictable results for SEB and SEH, we first
6920 // need to sign-extend the low 32-bit value into bits 32 through 63.
6921 __ Sll(dst, src, 0);
6922 __ Seh(dst, dst);
6923 } else {
6924 __ Seh(dst, src);
6925 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006926 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006927 case DataType::Type::kInt32:
6928 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006929 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6930 // conversions, except when the input and output registers are the same and we are not
6931 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006932 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006933 __ Sll(dst, src, 0);
6934 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006935 break;
6936
6937 default:
6938 LOG(FATAL) << "Unexpected type conversion from " << input_type
6939 << " to " << result_type;
6940 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006941 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006942 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6943 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006944 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006945 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006946 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006947 __ Cvtsl(dst, FTMP);
6948 } else {
6949 __ Cvtdl(dst, FTMP);
6950 }
6951 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006952 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006953 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006954 __ Cvtsw(dst, FTMP);
6955 } else {
6956 __ Cvtdw(dst, FTMP);
6957 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006958 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006959 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6960 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006961 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6962 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006964 if (result_type == DataType::Type::kInt64) {
6965 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006966 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006967 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006968 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006969 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006970 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006971 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006972 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006973 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006974 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006975 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006976 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006977 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006978 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006979 } else if (DataType::IsFloatingPointType(result_type) &&
6980 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006981 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6982 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006983 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006984 __ Cvtsd(dst, src);
6985 } else {
6986 __ Cvtds(dst, src);
6987 }
6988 } else {
6989 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6990 << " to " << result_type;
6991 }
6992}
6993
6994void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6995 HandleShift(ushr);
6996}
6997
6998void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6999 HandleShift(ushr);
7000}
7001
7002void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7003 HandleBinaryOp(instruction);
7004}
7005
7006void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7007 HandleBinaryOp(instruction);
7008}
7009
7010void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7011 // Nothing to do, this should be removed during prepare for register allocator.
7012 LOG(FATAL) << "Unreachable";
7013}
7014
7015void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7016 // Nothing to do, this should be removed during prepare for register allocator.
7017 LOG(FATAL) << "Unreachable";
7018}
7019
7020void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007021 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007022}
7023
7024void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007025 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007026}
7027
7028void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007029 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007030}
7031
7032void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007033 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007034}
7035
7036void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007037 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007038}
7039
7040void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007041 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007042}
7043
7044void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007045 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007046}
7047
7048void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007049 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007050}
7051
7052void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007053 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007054}
7055
7056void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007057 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007058}
7059
7060void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007061 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007062}
7063
7064void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007065 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007066}
7067
Aart Bike9f37602015-10-09 11:15:55 -07007068void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007069 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007070}
7071
7072void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007073 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007074}
7075
7076void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007077 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007078}
7079
7080void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007081 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007082}
7083
7084void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007085 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007086}
7087
7088void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007089 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007090}
7091
7092void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007093 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007094}
7095
7096void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007097 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007098}
7099
Mark Mendellfe57faa2015-09-18 09:26:15 -04007100// Simple implementation of packed switch - generate cascaded compare/jumps.
7101void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7102 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007103 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007104 locations->SetInAt(0, Location::RequiresRegister());
7105}
7106
Alexey Frunze0960ac52016-12-20 17:24:59 -08007107void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7108 int32_t lower_bound,
7109 uint32_t num_entries,
7110 HBasicBlock* switch_block,
7111 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007112 // Create a set of compare/jumps.
7113 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007114 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007115 // Jump to default if index is negative
7116 // Note: We don't check the case that index is positive while value < lower_bound, because in
7117 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7118 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7119
Alexey Frunze0960ac52016-12-20 17:24:59 -08007120 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007121 // Jump to successors[0] if value == lower_bound.
7122 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7123 int32_t last_index = 0;
7124 for (; num_entries - last_index > 2; last_index += 2) {
7125 __ Addiu(temp_reg, temp_reg, -2);
7126 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7127 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7128 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7129 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7130 }
7131 if (num_entries - last_index == 2) {
7132 // The last missing case_value.
7133 __ Addiu(temp_reg, temp_reg, -1);
7134 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007135 }
7136
7137 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007138 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007139 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007140 }
7141}
7142
Alexey Frunze0960ac52016-12-20 17:24:59 -08007143void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7144 int32_t lower_bound,
7145 uint32_t num_entries,
7146 HBasicBlock* switch_block,
7147 HBasicBlock* default_block) {
7148 // Create a jump table.
7149 std::vector<Mips64Label*> labels(num_entries);
7150 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7151 for (uint32_t i = 0; i < num_entries; i++) {
7152 labels[i] = codegen_->GetLabelOf(successors[i]);
7153 }
7154 JumpTable* table = __ CreateJumpTable(std::move(labels));
7155
7156 // Is the value in range?
7157 __ Addiu32(TMP, value_reg, -lower_bound);
7158 __ LoadConst32(AT, num_entries);
7159 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7160
7161 // We are in the range of the table.
7162 // Load the target address from the jump table, indexing by the value.
7163 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007164 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007165 __ Lw(TMP, TMP, 0);
7166 // Compute the absolute target address by adding the table start address
7167 // (the table contains offsets to targets relative to its start).
7168 __ Daddu(TMP, TMP, AT);
7169 // And jump.
7170 __ Jr(TMP);
7171 __ Nop();
7172}
7173
7174void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7175 int32_t lower_bound = switch_instr->GetStartValue();
7176 uint32_t num_entries = switch_instr->GetNumEntries();
7177 LocationSummary* locations = switch_instr->GetLocations();
7178 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7179 HBasicBlock* switch_block = switch_instr->GetBlock();
7180 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7181
7182 if (num_entries > kPackedSwitchJumpTableThreshold) {
7183 GenTableBasedPackedSwitch(value_reg,
7184 lower_bound,
7185 num_entries,
7186 switch_block,
7187 default_block);
7188 } else {
7189 GenPackedSwitchWithCompares(value_reg,
7190 lower_bound,
7191 num_entries,
7192 switch_block,
7193 default_block);
7194 }
7195}
7196
Chris Larsenc9905a62017-03-13 17:06:18 -07007197void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7198 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007199 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007200 locations->SetInAt(0, Location::RequiresRegister());
7201 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007202}
7203
Chris Larsenc9905a62017-03-13 17:06:18 -07007204void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7205 LocationSummary* locations = instruction->GetLocations();
7206 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7207 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7208 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7209 __ LoadFromOffset(kLoadDoubleword,
7210 locations->Out().AsRegister<GpuRegister>(),
7211 locations->InAt(0).AsRegister<GpuRegister>(),
7212 method_offset);
7213 } else {
7214 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7215 instruction->GetIndex(), kMips64PointerSize));
7216 __ LoadFromOffset(kLoadDoubleword,
7217 locations->Out().AsRegister<GpuRegister>(),
7218 locations->InAt(0).AsRegister<GpuRegister>(),
7219 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7220 __ LoadFromOffset(kLoadDoubleword,
7221 locations->Out().AsRegister<GpuRegister>(),
7222 locations->Out().AsRegister<GpuRegister>(),
7223 method_offset);
7224 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007225}
7226
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007227void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7228 ATTRIBUTE_UNUSED) {
7229 LOG(FATAL) << "Unreachable";
7230}
7231
7232void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7233 ATTRIBUTE_UNUSED) {
7234 LOG(FATAL) << "Unreachable";
7235}
7236
Alexey Frunze4dda3372015-06-01 18:31:49 -07007237} // namespace mips64
7238} // namespace art