blob: ee33b3f3351397ec311f4429c38aa627b78a24c8 [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
1064static dwarf::Reg DWARFReg(GpuRegister reg) {
1065 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1066}
1067
David Srbeckyba702002016-02-01 18:15:29 +00001068static dwarf::Reg DWARFReg(FpuRegister reg) {
1069 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1070}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001071
1072void CodeGeneratorMIPS64::GenerateFrameEntry() {
1073 __ Bind(&frame_entry_label_);
1074
Vladimir Marko33bff252017-11-01 14:35:42 +00001075 bool do_overflow_check =
1076 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001077
1078 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001079 __ LoadFromOffset(
1080 kLoadWord,
1081 ZERO,
1082 SP,
1083 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001084 RecordPcInfo(nullptr, 0);
1085 }
1086
Alexey Frunze4dda3372015-06-01 18:31:49 -07001087 if (HasEmptyFrame()) {
1088 return;
1089 }
1090
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001091 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001092 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1093 LOG(FATAL) << "Stack frame larger than "
1094 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001095 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001096
1097 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001099 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001100 __ IncreaseFrameSize(ofs);
1101
1102 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1103 GpuRegister reg = kCoreCalleeSaves[i];
1104 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001105 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001107 __ cfi().RelOffset(DWARFReg(reg), ofs);
1108 }
1109 }
1110
1111 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1112 FpuRegister reg = kFpuCalleeSaves[i];
1113 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001114 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001115 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001116 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001117 }
1118 }
1119
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001120 // Save the current method if we need it. Note that we do not
1121 // do this in HCurrentMethod, as the instruction might have been removed
1122 // in the SSA graph.
1123 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001124 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001125 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001126
1127 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1128 // Initialize should_deoptimize flag to 0.
1129 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1130 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001131}
1132
1133void CodeGeneratorMIPS64::GenerateFrameExit() {
1134 __ cfi().RememberState();
1135
Alexey Frunze4dda3372015-06-01 18:31:49 -07001136 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001137 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001139 // For better instruction scheduling restore RA before other registers.
1140 uint32_t ofs = GetFrameSize();
1141 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001142 GpuRegister reg = kCoreCalleeSaves[i];
1143 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001144 ofs -= kMips64DoublewordSize;
1145 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001146 __ cfi().Restore(DWARFReg(reg));
1147 }
1148 }
1149
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001150 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1151 FpuRegister reg = kFpuCalleeSaves[i];
1152 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1153 ofs -= kMips64DoublewordSize;
1154 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1155 __ cfi().Restore(DWARFReg(reg));
1156 }
1157 }
1158
1159 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001160 }
1161
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001162 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001163
1164 __ cfi().RestoreState();
1165 __ cfi().DefCFAOffset(GetFrameSize());
1166}
1167
1168void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1169 __ Bind(GetLabelOf(block));
1170}
1171
1172void CodeGeneratorMIPS64::MoveLocation(Location destination,
1173 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001174 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001175 if (source.Equals(destination)) {
1176 return;
1177 }
1178
1179 // A valid move can always be inferred from the destination and source
1180 // locations. When moving from and to a register, the argument type can be
1181 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001182 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001183 DCHECK_EQ(unspecified_type, false);
1184
1185 if (destination.IsRegister() || destination.IsFpuRegister()) {
1186 if (unspecified_type) {
1187 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1188 if (source.IsStackSlot() ||
1189 (src_cst != nullptr && (src_cst->IsIntConstant()
1190 || src_cst->IsFloatConstant()
1191 || src_cst->IsNullConstant()))) {
1192 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001194 } else {
1195 // If the source is a double stack slot or a 64bit constant, a 64bit
1196 // type is appropriate. Else the source is a register, and since the
1197 // type has not been specified, we chose a 64bit type to force a 64bit
1198 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001199 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001200 }
1201 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001202 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1203 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001204 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1205 // Move to GPR/FPR from stack
1206 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001207 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001208 __ LoadFpuFromOffset(load_type,
1209 destination.AsFpuRegister<FpuRegister>(),
1210 SP,
1211 source.GetStackIndex());
1212 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001214 __ LoadFromOffset(load_type,
1215 destination.AsRegister<GpuRegister>(),
1216 SP,
1217 source.GetStackIndex());
1218 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001219 } else if (source.IsSIMDStackSlot()) {
1220 __ LoadFpuFromOffset(kLoadQuadword,
1221 destination.AsFpuRegister<FpuRegister>(),
1222 SP,
1223 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 } else if (source.IsConstant()) {
1225 // Move to GPR/FPR from constant
1226 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 gpr = destination.AsRegister<GpuRegister>();
1229 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001231 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001232 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001233 gpr = ZERO;
1234 } else {
1235 __ LoadConst32(gpr, value);
1236 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001237 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001238 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001240 gpr = ZERO;
1241 } else {
1242 __ LoadConst64(gpr, value);
1243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001244 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001246 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001248 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1249 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 if (destination.IsRegister()) {
1252 // Move to GPR from GPR
1253 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1254 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001256 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1258 } else {
1259 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1260 }
1261 }
1262 } else if (source.IsFpuRegister()) {
1263 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001264 if (GetGraph()->HasSIMD()) {
1265 __ MoveV(VectorRegisterFrom(destination),
1266 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001267 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001268 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001270 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1271 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001273 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001275 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001276 } else {
1277 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001278 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001279 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1280 } else {
1281 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1282 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001283 }
1284 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001285 } else if (destination.IsSIMDStackSlot()) {
1286 if (source.IsFpuRegister()) {
1287 __ StoreFpuToOffset(kStoreQuadword,
1288 source.AsFpuRegister<FpuRegister>(),
1289 SP,
1290 destination.GetStackIndex());
1291 } else {
1292 DCHECK(source.IsSIMDStackSlot());
1293 __ LoadFpuFromOffset(kLoadQuadword,
1294 FTMP,
1295 SP,
1296 source.GetStackIndex());
1297 __ StoreFpuToOffset(kStoreQuadword,
1298 FTMP,
1299 SP,
1300 destination.GetStackIndex());
1301 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001302 } else { // The destination is not a register. It must be a stack slot.
1303 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1304 if (source.IsRegister() || source.IsFpuRegister()) {
1305 if (unspecified_type) {
1306 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001307 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001309 dst_type =
1310 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001311 }
1312 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001313 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1314 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001315 // Move to stack from GPR/FPR
1316 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1317 if (source.IsRegister()) {
1318 __ StoreToOffset(store_type,
1319 source.AsRegister<GpuRegister>(),
1320 SP,
1321 destination.GetStackIndex());
1322 } else {
1323 __ StoreFpuToOffset(store_type,
1324 source.AsFpuRegister<FpuRegister>(),
1325 SP,
1326 destination.GetStackIndex());
1327 }
1328 } else if (source.IsConstant()) {
1329 // Move to stack from constant
1330 HConstant* src_cst = source.GetConstant();
1331 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001332 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001333 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001334 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1335 if (value != 0) {
1336 gpr = TMP;
1337 __ LoadConst32(gpr, value);
1338 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001339 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001340 DCHECK(destination.IsDoubleStackSlot());
1341 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1342 if (value != 0) {
1343 gpr = TMP;
1344 __ LoadConst64(gpr, value);
1345 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001346 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001347 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001348 } else {
1349 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1350 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1351 // Move to stack from stack
1352 if (destination.IsStackSlot()) {
1353 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1354 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1355 } else {
1356 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1357 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1358 }
1359 }
1360 }
1361}
1362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001363void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001364 DCHECK(!loc1.IsConstant());
1365 DCHECK(!loc2.IsConstant());
1366
1367 if (loc1.Equals(loc2)) {
1368 return;
1369 }
1370
1371 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1372 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1373 bool is_fp_reg1 = loc1.IsFpuRegister();
1374 bool is_fp_reg2 = loc2.IsFpuRegister();
1375
1376 if (loc2.IsRegister() && loc1.IsRegister()) {
1377 // Swap 2 GPRs
1378 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1379 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1380 __ Move(TMP, r2);
1381 __ Move(r2, r1);
1382 __ Move(r1, TMP);
1383 } else if (is_fp_reg2 && is_fp_reg1) {
1384 // Swap 2 FPRs
1385 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1386 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001387 if (type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001388 __ MovS(FTMP, r1);
1389 __ MovS(r1, r2);
1390 __ MovS(r2, FTMP);
1391 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001392 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001393 __ MovD(FTMP, r1);
1394 __ MovD(r1, r2);
1395 __ MovD(r2, FTMP);
1396 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001397 } else if (is_slot1 != is_slot2) {
1398 // Swap GPR/FPR and stack slot
1399 Location reg_loc = is_slot1 ? loc2 : loc1;
1400 Location mem_loc = is_slot1 ? loc1 : loc2;
1401 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1402 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001403 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001404 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1405 if (reg_loc.IsFpuRegister()) {
1406 __ StoreFpuToOffset(store_type,
1407 reg_loc.AsFpuRegister<FpuRegister>(),
1408 SP,
1409 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001410 if (mem_loc.IsStackSlot()) {
1411 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1412 } else {
1413 DCHECK(mem_loc.IsDoubleStackSlot());
1414 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1415 }
1416 } else {
1417 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1418 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1419 }
1420 } else if (is_slot1 && is_slot2) {
1421 move_resolver_.Exchange(loc1.GetStackIndex(),
1422 loc2.GetStackIndex(),
1423 loc1.IsDoubleStackSlot());
1424 } else {
1425 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1426 }
1427}
1428
Calin Juravle175dc732015-08-25 15:42:32 +01001429void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1430 DCHECK(location.IsRegister());
1431 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1432}
1433
Calin Juravlee460d1d2015-09-29 04:52:17 +01001434void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1435 if (location.IsRegister()) {
1436 locations->AddTemp(location);
1437 } else {
1438 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1439 }
1440}
1441
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001442void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1443 GpuRegister value,
1444 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001445 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001446 GpuRegister card = AT;
1447 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001448 if (value_can_be_null) {
1449 __ Beqzc(value, &done);
1450 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001451 __ LoadFromOffset(kLoadDoubleword,
1452 card,
1453 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001454 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001455 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1456 __ Daddu(temp, card, temp);
1457 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001458 if (value_can_be_null) {
1459 __ Bind(&done);
1460 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001461}
1462
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001463template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001464inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1465 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001466 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001467 for (const PcRelativePatchInfo& info : infos) {
1468 const DexFile& dex_file = info.target_dex_file;
1469 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001470 DCHECK(info.label.IsBound());
1471 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1472 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1473 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1474 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001475 }
1476}
1477
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001478void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001479 DCHECK(linker_patches->empty());
1480 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001481 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001482 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001483 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001484 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001485 pc_relative_string_patches_.size() +
1486 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001487 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001488 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001489 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1490 pc_relative_method_patches_, linker_patches);
1491 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1492 pc_relative_type_patches_, linker_patches);
1493 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1494 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001495 } else {
1496 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001497 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1498 pc_relative_type_patches_, linker_patches);
1499 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1500 pc_relative_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001501 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001502 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1503 method_bss_entry_patches_, linker_patches);
1504 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1505 type_bss_entry_patches_, linker_patches);
1506 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1507 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001508 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001509}
1510
Vladimir Marko65979462017-05-19 17:25:12 +01001511CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001512 MethodReference target_method,
1513 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001514 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001515 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001516 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001517 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001518}
1519
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001520CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001521 MethodReference target_method,
1522 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001523 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001524 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001525 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001526 &method_bss_entry_patches_);
1527}
1528
Alexey Frunzef63f5692016-12-13 17:43:11 -08001529CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001530 const DexFile& dex_file,
1531 dex::TypeIndex type_index,
1532 const PcRelativePatchInfo* info_high) {
1533 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001534}
1535
Vladimir Marko1998cd02017-01-13 13:02:58 +00001536CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001537 const DexFile& dex_file,
1538 dex::TypeIndex type_index,
1539 const PcRelativePatchInfo* info_high) {
1540 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001541}
1542
Vladimir Marko65979462017-05-19 17:25:12 +01001543CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001544 const DexFile& dex_file,
1545 dex::StringIndex string_index,
1546 const PcRelativePatchInfo* info_high) {
1547 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001548}
1549
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001550CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1551 const DexFile& dex_file,
1552 dex::StringIndex string_index,
1553 const PcRelativePatchInfo* info_high) {
1554 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1555}
1556
Alexey Frunze19f6c692016-11-30 19:19:55 -08001557CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001558 const DexFile& dex_file,
1559 uint32_t offset_or_index,
1560 const PcRelativePatchInfo* info_high,
1561 ArenaDeque<PcRelativePatchInfo>* patches) {
1562 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001563 return &patches->back();
1564}
1565
Alexey Frunzef63f5692016-12-13 17:43:11 -08001566Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1567 return map->GetOrCreate(
1568 value,
1569 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1570}
1571
Alexey Frunze19f6c692016-11-30 19:19:55 -08001572Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1573 return uint64_literals_.GetOrCreate(
1574 value,
1575 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1576}
1577
Alexey Frunzef63f5692016-12-13 17:43:11 -08001578Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001579 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001580}
1581
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001582void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1583 GpuRegister out,
1584 PcRelativePatchInfo* info_low) {
1585 DCHECK(!info_high->patch_info_high);
1586 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001587 // Add the high half of a 32-bit offset to PC.
1588 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001589 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001590 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001591 if (info_low != nullptr) {
1592 DCHECK_EQ(info_low->patch_info_high, info_high);
1593 __ Bind(&info_low->label);
1594 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001595}
1596
Alexey Frunze627c1a02017-01-30 19:28:14 -08001597Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1598 dex::StringIndex string_index,
1599 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001600 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001601 return jit_string_patches_.GetOrCreate(
1602 StringReference(&dex_file, string_index),
1603 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1604}
1605
1606Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1607 dex::TypeIndex type_index,
1608 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001609 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001610 return jit_class_patches_.GetOrCreate(
1611 TypeReference(&dex_file, type_index),
1612 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1613}
1614
1615void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1616 const uint8_t* roots_data,
1617 const Literal* literal,
1618 uint64_t index_in_table) const {
1619 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1620 uintptr_t address =
1621 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1622 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1623}
1624
1625void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1626 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001627 const StringReference& string_reference = entry.first;
1628 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001629 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001630 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001631 }
1632 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001633 const TypeReference& type_reference = entry.first;
1634 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001635 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001636 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001637 }
1638}
1639
David Brazdil58282f42016-01-14 12:45:10 +00001640void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1642 blocked_core_registers_[ZERO] = true;
1643 blocked_core_registers_[K0] = true;
1644 blocked_core_registers_[K1] = true;
1645 blocked_core_registers_[GP] = true;
1646 blocked_core_registers_[SP] = true;
1647 blocked_core_registers_[RA] = true;
1648
Lazar Trsicd9672662015-09-03 17:33:01 +02001649 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1650 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001651 blocked_core_registers_[AT] = true;
1652 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001653 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001654 blocked_fpu_registers_[FTMP] = true;
1655
1656 // Reserve suspend and thread registers.
1657 blocked_core_registers_[S0] = true;
1658 blocked_core_registers_[TR] = true;
1659
1660 // Reserve T9 for function calls
1661 blocked_core_registers_[T9] = true;
1662
Goran Jakovljevic782be112016-06-21 12:39:04 +02001663 if (GetGraph()->IsDebuggable()) {
1664 // Stubs do not save callee-save floating point registers. If the graph
1665 // is debuggable, we need to deal with these registers differently. For
1666 // now, just block them.
1667 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1668 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1669 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001670 }
1671}
1672
Alexey Frunze4dda3372015-06-01 18:31:49 -07001673size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1674 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001675 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676}
1677
1678size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1679 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001680 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681}
1682
1683size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001684 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1685 FpuRegister(reg_id),
1686 SP,
1687 stack_index);
1688 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001689}
1690
1691size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001692 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1693 FpuRegister(reg_id),
1694 SP,
1695 stack_index);
1696 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001697}
1698
1699void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001700 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701}
1702
1703void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001704 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705}
1706
Calin Juravle175dc732015-08-25 15:42:32 +01001707void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001708 HInstruction* instruction,
1709 uint32_t dex_pc,
1710 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001711 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001712 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001713 if (EntrypointRequiresStackMap(entrypoint)) {
1714 RecordPcInfo(instruction, dex_pc, slow_path);
1715 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001716}
1717
Alexey Frunze15958152017-02-09 19:08:30 -08001718void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1719 HInstruction* instruction,
1720 SlowPathCode* slow_path) {
1721 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1722 GenerateInvokeRuntime(entry_point_offset);
1723}
1724
1725void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1726 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1727 __ Jalr(T9);
1728 __ Nop();
1729}
1730
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1732 GpuRegister class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07001733 __ LoadFromOffset(kLoadSignedByte, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001734 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1735 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001736 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1737 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738 __ Bind(slow_path->GetExitLabel());
1739}
1740
1741void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1742 __ Sync(0); // only stype 0 is supported
1743}
1744
1745void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1746 HBasicBlock* successor) {
1747 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001748 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1749
1750 if (slow_path == nullptr) {
1751 slow_path =
1752 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1753 instruction->SetSlowPath(slow_path);
1754 codegen_->AddSlowPath(slow_path);
1755 if (successor != nullptr) {
1756 DCHECK(successor->IsLoopHeader());
1757 }
1758 } else {
1759 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1760 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001761
1762 __ LoadFromOffset(kLoadUnsignedHalfword,
1763 TMP,
1764 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001765 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766 if (successor == nullptr) {
1767 __ Bnezc(TMP, slow_path->GetEntryLabel());
1768 __ Bind(slow_path->GetReturnLabel());
1769 } else {
1770 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001771 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772 // slow_path will return to GetLabelOf(successor).
1773 }
1774}
1775
1776InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1777 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001778 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 assembler_(codegen->GetAssembler()),
1780 codegen_(codegen) {}
1781
1782void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1783 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001784 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001785 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001786 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 case DataType::Type::kInt32:
1788 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001789 locations->SetInAt(0, Location::RequiresRegister());
1790 HInstruction* right = instruction->InputAt(1);
1791 bool can_use_imm = false;
1792 if (right->IsConstant()) {
1793 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1794 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1795 can_use_imm = IsUint<16>(imm);
1796 } else if (instruction->IsAdd()) {
1797 can_use_imm = IsInt<16>(imm);
1798 } else {
1799 DCHECK(instruction->IsSub());
1800 can_use_imm = IsInt<16>(-imm);
1801 }
1802 }
1803 if (can_use_imm)
1804 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1805 else
1806 locations->SetInAt(1, Location::RequiresRegister());
1807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1808 }
1809 break;
1810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001811 case DataType::Type::kFloat32:
1812 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001813 locations->SetInAt(0, Location::RequiresFpuRegister());
1814 locations->SetInAt(1, Location::RequiresFpuRegister());
1815 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1816 break;
1817
1818 default:
1819 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1820 }
1821}
1822
1823void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001825 LocationSummary* locations = instruction->GetLocations();
1826
1827 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 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1831 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1832 Location rhs_location = locations->InAt(1);
1833
1834 GpuRegister rhs_reg = ZERO;
1835 int64_t rhs_imm = 0;
1836 bool use_imm = rhs_location.IsConstant();
1837 if (use_imm) {
1838 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1839 } else {
1840 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1841 }
1842
1843 if (instruction->IsAnd()) {
1844 if (use_imm)
1845 __ Andi(dst, lhs, rhs_imm);
1846 else
1847 __ And(dst, lhs, rhs_reg);
1848 } else if (instruction->IsOr()) {
1849 if (use_imm)
1850 __ Ori(dst, lhs, rhs_imm);
1851 else
1852 __ Or(dst, lhs, rhs_reg);
1853 } else if (instruction->IsXor()) {
1854 if (use_imm)
1855 __ Xori(dst, lhs, rhs_imm);
1856 else
1857 __ Xor(dst, lhs, rhs_reg);
1858 } else if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001859 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001860 if (use_imm)
1861 __ Addiu(dst, lhs, rhs_imm);
1862 else
1863 __ Addu(dst, lhs, rhs_reg);
1864 } else {
1865 if (use_imm)
1866 __ Daddiu(dst, lhs, rhs_imm);
1867 else
1868 __ Daddu(dst, lhs, rhs_reg);
1869 }
1870 } else {
1871 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001872 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001873 if (use_imm)
1874 __ Addiu(dst, lhs, -rhs_imm);
1875 else
1876 __ Subu(dst, lhs, rhs_reg);
1877 } else {
1878 if (use_imm)
1879 __ Daddiu(dst, lhs, -rhs_imm);
1880 else
1881 __ Dsubu(dst, lhs, rhs_reg);
1882 }
1883 }
1884 break;
1885 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 case DataType::Type::kFloat32:
1887 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001888 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1889 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1890 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1891 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001892 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001893 __ AddS(dst, lhs, rhs);
1894 else
1895 __ AddD(dst, lhs, rhs);
1896 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001897 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001898 __ SubS(dst, lhs, rhs);
1899 else
1900 __ SubD(dst, lhs, rhs);
1901 } else {
1902 LOG(FATAL) << "Unexpected floating-point binary operation";
1903 }
1904 break;
1905 }
1906 default:
1907 LOG(FATAL) << "Unexpected binary operation type " << type;
1908 }
1909}
1910
1911void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001912 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001913
Vladimir Markoca6fff82017-10-03 14:49:14 +01001914 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001915 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001916 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001917 case DataType::Type::kInt32:
1918 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001919 locations->SetInAt(0, Location::RequiresRegister());
1920 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001922 break;
1923 }
1924 default:
1925 LOG(FATAL) << "Unexpected shift type " << type;
1926 }
1927}
1928
1929void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001930 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001931 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001933
1934 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kInt32:
1936 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001937 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1938 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1939 Location rhs_location = locations->InAt(1);
1940
1941 GpuRegister rhs_reg = ZERO;
1942 int64_t rhs_imm = 0;
1943 bool use_imm = rhs_location.IsConstant();
1944 if (use_imm) {
1945 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1946 } else {
1947 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1948 }
1949
1950 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001951 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001953
Alexey Frunze92d90602015-12-18 18:16:36 -08001954 if (shift_value == 0) {
1955 if (dst != lhs) {
1956 __ Move(dst, lhs);
1957 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001958 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001959 if (instr->IsShl()) {
1960 __ Sll(dst, lhs, shift_value);
1961 } else if (instr->IsShr()) {
1962 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001963 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001964 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001965 } else {
1966 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967 }
1968 } else {
1969 if (shift_value < 32) {
1970 if (instr->IsShl()) {
1971 __ Dsll(dst, lhs, shift_value);
1972 } else if (instr->IsShr()) {
1973 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001974 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001975 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001976 } else {
1977 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001978 }
1979 } else {
1980 shift_value -= 32;
1981 if (instr->IsShl()) {
1982 __ Dsll32(dst, lhs, shift_value);
1983 } else if (instr->IsShr()) {
1984 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001985 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001986 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001987 } else {
1988 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001989 }
1990 }
1991 }
1992 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001993 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001994 if (instr->IsShl()) {
1995 __ Sllv(dst, lhs, rhs_reg);
1996 } else if (instr->IsShr()) {
1997 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001998 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001999 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002000 } else {
2001 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002002 }
2003 } else {
2004 if (instr->IsShl()) {
2005 __ Dsllv(dst, lhs, rhs_reg);
2006 } else if (instr->IsShr()) {
2007 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002008 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002009 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002010 } else {
2011 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002012 }
2013 }
2014 }
2015 break;
2016 }
2017 default:
2018 LOG(FATAL) << "Unexpected shift operation type " << type;
2019 }
2020}
2021
2022void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2023 HandleBinaryOp(instruction);
2024}
2025
2026void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2027 HandleBinaryOp(instruction);
2028}
2029
2030void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2031 HandleBinaryOp(instruction);
2032}
2033
2034void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2035 HandleBinaryOp(instruction);
2036}
2037
2038void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002040 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002042 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002043 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2044 object_array_get_with_read_barrier
2045 ? LocationSummary::kCallOnSlowPath
2046 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002047 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2048 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2049 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 locations->SetInAt(0, Location::RequiresRegister());
2051 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2054 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002055 // The output overlaps in the case of an object array get with
2056 // read barriers enabled: we do not want the move to overwrite the
2057 // array's location, as we need it to emit the read barrier.
2058 locations->SetOut(Location::RequiresRegister(),
2059 object_array_get_with_read_barrier
2060 ? Location::kOutputOverlap
2061 : Location::kNoOutputOverlap);
2062 }
2063 // We need a temporary register for the read barrier marking slow
2064 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2065 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002066 bool temp_needed = instruction->GetIndex()->IsConstant()
2067 ? !kBakerReadBarrierThunksEnableForFields
2068 : !kBakerReadBarrierThunksEnableForArrays;
2069 if (temp_needed) {
2070 locations->AddTemp(Location::RequiresRegister());
2071 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002072 }
2073}
2074
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002075static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2076 auto null_checker = [codegen, instruction]() {
2077 codegen->MaybeRecordImplicitNullCheck(instruction);
2078 };
2079 return null_checker;
2080}
2081
Alexey Frunze4dda3372015-06-01 18:31:49 -07002082void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2083 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002084 Location obj_loc = locations->InAt(0);
2085 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2086 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002087 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002088 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002089 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002092 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2093 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002094 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002095 case DataType::Type::kBool:
2096 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002097 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002098 if (index.IsConstant()) {
2099 size_t offset =
2100 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002101 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002102 } else {
2103 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002104 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002105 }
2106 break;
2107 }
2108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002110 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 if (index.IsConstant()) {
2112 size_t offset =
2113 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002114 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002115 } else {
2116 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002117 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002118 }
2119 break;
2120 }
2121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002123 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002124 if (maybe_compressed_char_at) {
2125 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002126 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002127 __ Dext(TMP, TMP, 0, 1);
2128 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2129 "Expecting 0=compressed, 1=uncompressed");
2130 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002132 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2133 if (maybe_compressed_char_at) {
2134 Mips64Label uncompressed_load, done;
2135 __ Bnezc(TMP, &uncompressed_load);
2136 __ LoadFromOffset(kLoadUnsignedByte,
2137 out,
2138 obj,
2139 data_offset + (const_index << TIMES_1));
2140 __ Bc(&done);
2141 __ Bind(&uncompressed_load);
2142 __ LoadFromOffset(kLoadUnsignedHalfword,
2143 out,
2144 obj,
2145 data_offset + (const_index << TIMES_2));
2146 __ Bind(&done);
2147 } else {
2148 __ LoadFromOffset(kLoadUnsignedHalfword,
2149 out,
2150 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002151 data_offset + (const_index << TIMES_2),
2152 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002153 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002154 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002155 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2156 if (maybe_compressed_char_at) {
2157 Mips64Label uncompressed_load, done;
2158 __ Bnezc(TMP, &uncompressed_load);
2159 __ Daddu(TMP, obj, index_reg);
2160 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2161 __ Bc(&done);
2162 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002163 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002164 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2165 __ Bind(&done);
2166 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002167 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002168 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002169 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002170 }
2171 break;
2172 }
2173
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002174 case DataType::Type::kInt16: {
2175 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2176 if (index.IsConstant()) {
2177 size_t offset =
2178 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2179 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2180 } else {
2181 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2182 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2183 }
2184 break;
2185 }
2186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002187 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002188 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002189 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002190 LoadOperandType load_type =
2191 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 if (index.IsConstant()) {
2193 size_t offset =
2194 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002195 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002196 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002197 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002198 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199 }
2200 break;
2201 }
2202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002204 static_assert(
2205 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2206 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2207 // /* HeapReference<Object> */ out =
2208 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2209 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002210 bool temp_needed = index.IsConstant()
2211 ? !kBakerReadBarrierThunksEnableForFields
2212 : !kBakerReadBarrierThunksEnableForArrays;
2213 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002214 // Note that a potential implicit null check is handled in this
2215 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002216 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2217 if (index.IsConstant()) {
2218 // Array load with a constant index can be treated as a field load.
2219 size_t offset =
2220 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2221 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2222 out_loc,
2223 obj,
2224 offset,
2225 temp,
2226 /* needs_null_check */ false);
2227 } else {
2228 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2229 out_loc,
2230 obj,
2231 data_offset,
2232 index,
2233 temp,
2234 /* needs_null_check */ false);
2235 }
Alexey Frunze15958152017-02-09 19:08:30 -08002236 } else {
2237 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2238 if (index.IsConstant()) {
2239 size_t offset =
2240 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2241 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2242 // If read barriers are enabled, emit read barriers other than
2243 // Baker's using a slow path (and also unpoison the loaded
2244 // reference, if heap poisoning is enabled).
2245 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2246 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002247 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002248 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2249 // If read barriers are enabled, emit read barriers other than
2250 // Baker's using a slow path (and also unpoison the loaded
2251 // reference, if heap poisoning is enabled).
2252 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2253 out_loc,
2254 out_loc,
2255 obj_loc,
2256 data_offset,
2257 index);
2258 }
2259 }
2260 break;
2261 }
2262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002264 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002265 if (index.IsConstant()) {
2266 size_t offset =
2267 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002268 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002269 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002270 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002271 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002272 }
2273 break;
2274 }
2275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002277 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002278 if (index.IsConstant()) {
2279 size_t offset =
2280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002281 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002282 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002283 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002284 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002285 }
2286 break;
2287 }
2288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002290 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002291 if (index.IsConstant()) {
2292 size_t offset =
2293 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002294 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002295 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002296 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002297 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002298 }
2299 break;
2300 }
2301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002303 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2304 UNREACHABLE();
2305 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002306}
2307
2308void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002309 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002310 locations->SetInAt(0, Location::RequiresRegister());
2311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2312}
2313
2314void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2315 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002316 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002317 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2318 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2319 __ LoadFromOffset(kLoadWord, out, obj, offset);
2320 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002321 // Mask out compression flag from String's array length.
2322 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2323 __ Srl(out, out, 1u);
2324 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002325}
2326
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002327Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2328 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2329 ? Location::ConstantLocation(instruction->AsConstant())
2330 : Location::RequiresRegister();
2331}
2332
2333Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2334 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2335 // We can store a non-zero float or double constant without first loading it into the FPU,
2336 // but we should only prefer this if the constant has a single use.
2337 if (instruction->IsConstant() &&
2338 (instruction->AsConstant()->IsZeroBitPattern() ||
2339 instruction->GetUses().HasExactlyOneElement())) {
2340 return Location::ConstantLocation(instruction->AsConstant());
2341 // Otherwise fall through and require an FPU register for the constant.
2342 }
2343 return Location::RequiresFpuRegister();
2344}
2345
Alexey Frunze4dda3372015-06-01 18:31:49 -07002346void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002348
2349 bool needs_write_barrier =
2350 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2351 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2352
Vladimir Markoca6fff82017-10-03 14:49:14 +01002353 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002354 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002355 may_need_runtime_call_for_type_check ?
2356 LocationSummary::kCallOnSlowPath :
2357 LocationSummary::kNoCall);
2358
2359 locations->SetInAt(0, Location::RequiresRegister());
2360 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002362 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002363 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002364 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2365 }
2366 if (needs_write_barrier) {
2367 // Temporary register for the write barrier.
2368 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002369 }
2370}
2371
2372void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2373 LocationSummary* locations = instruction->GetLocations();
2374 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2375 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002376 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002378 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002379 bool needs_write_barrier =
2380 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002381 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002382 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002383
2384 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002386 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002388 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002389 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002390 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002391 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002392 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2393 }
2394 if (value_location.IsConstant()) {
2395 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2396 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2397 } else {
2398 GpuRegister value = value_location.AsRegister<GpuRegister>();
2399 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400 }
2401 break;
2402 }
2403
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002404 case DataType::Type::kUint16:
2405 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002407 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002408 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002409 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002410 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002411 }
2412 if (value_location.IsConstant()) {
2413 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2414 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2415 } else {
2416 GpuRegister value = value_location.AsRegister<GpuRegister>();
2417 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002418 }
2419 break;
2420 }
2421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002423 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2424 if (index.IsConstant()) {
2425 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2426 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002427 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002428 }
2429 if (value_location.IsConstant()) {
2430 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2431 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2432 } else {
2433 GpuRegister value = value_location.AsRegister<GpuRegister>();
2434 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2435 }
2436 break;
2437 }
2438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002440 if (value_location.IsConstant()) {
2441 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002442 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002443 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002444 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002445 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002446 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002447 }
Alexey Frunze15958152017-02-09 19:08:30 -08002448 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2449 DCHECK_EQ(value, 0);
2450 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2451 DCHECK(!needs_write_barrier);
2452 DCHECK(!may_need_runtime_call_for_type_check);
2453 break;
2454 }
2455
2456 DCHECK(needs_write_barrier);
2457 GpuRegister value = value_location.AsRegister<GpuRegister>();
2458 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2459 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2460 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2461 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2462 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2463 Mips64Label done;
2464 SlowPathCodeMIPS64* slow_path = nullptr;
2465
2466 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002467 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002468 codegen_->AddSlowPath(slow_path);
2469 if (instruction->GetValueCanBeNull()) {
2470 Mips64Label non_zero;
2471 __ Bnezc(value, &non_zero);
2472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2473 if (index.IsConstant()) {
2474 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002475 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002476 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002477 }
Alexey Frunze15958152017-02-09 19:08:30 -08002478 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2479 __ Bc(&done);
2480 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481 }
Alexey Frunze15958152017-02-09 19:08:30 -08002482
2483 // Note that when read barriers are enabled, the type checks
2484 // are performed without read barriers. This is fine, even in
2485 // the case where a class object is in the from-space after
2486 // the flip, as a comparison involving such a type would not
2487 // produce a false positive; it may of course produce a false
2488 // negative, in which case we would take the ArraySet slow
2489 // path.
2490
2491 // /* HeapReference<Class> */ temp1 = obj->klass_
2492 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2493 __ MaybeUnpoisonHeapReference(temp1);
2494
2495 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2496 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2497 // /* HeapReference<Class> */ temp2 = value->klass_
2498 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2499 // If heap poisoning is enabled, no need to unpoison `temp1`
2500 // nor `temp2`, as we are comparing two poisoned references.
2501
2502 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2503 Mips64Label do_put;
2504 __ Beqc(temp1, temp2, &do_put);
2505 // If heap poisoning is enabled, the `temp1` reference has
2506 // not been unpoisoned yet; unpoison it now.
2507 __ MaybeUnpoisonHeapReference(temp1);
2508
2509 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2510 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2511 // If heap poisoning is enabled, no need to unpoison
2512 // `temp1`, as we are comparing against null below.
2513 __ Bnezc(temp1, slow_path->GetEntryLabel());
2514 __ Bind(&do_put);
2515 } else {
2516 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2517 }
2518 }
2519
2520 GpuRegister source = value;
2521 if (kPoisonHeapReferences) {
2522 // Note that in the case where `value` is a null reference,
2523 // we do not enter this block, as a null reference does not
2524 // need poisoning.
2525 __ Move(temp1, value);
2526 __ PoisonHeapReference(temp1);
2527 source = temp1;
2528 }
2529
2530 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2531 if (index.IsConstant()) {
2532 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002533 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002534 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002535 }
2536 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2537
2538 if (!may_need_runtime_call_for_type_check) {
2539 codegen_->MaybeRecordImplicitNullCheck(instruction);
2540 }
2541
2542 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2543
2544 if (done.IsLinked()) {
2545 __ Bind(&done);
2546 }
2547
2548 if (slow_path != nullptr) {
2549 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002550 }
2551 break;
2552 }
2553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002555 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002556 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002557 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002558 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002559 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002560 }
2561 if (value_location.IsConstant()) {
2562 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2563 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2564 } else {
2565 GpuRegister value = value_location.AsRegister<GpuRegister>();
2566 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002567 }
2568 break;
2569 }
2570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002572 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002573 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002574 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002575 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002576 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002577 }
2578 if (value_location.IsConstant()) {
2579 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2580 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2581 } else {
2582 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2583 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002584 }
2585 break;
2586 }
2587
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002590 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002591 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002592 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002593 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002594 }
2595 if (value_location.IsConstant()) {
2596 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2597 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2598 } else {
2599 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2600 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002601 }
2602 break;
2603 }
2604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002606 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2607 UNREACHABLE();
2608 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002609}
2610
2611void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002612 RegisterSet caller_saves = RegisterSet::Empty();
2613 InvokeRuntimeCallingConvention calling_convention;
2614 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2615 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2616 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002617 locations->SetInAt(0, Location::RequiresRegister());
2618 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002619}
2620
2621void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2622 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002623 BoundsCheckSlowPathMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002624 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002625 codegen_->AddSlowPath(slow_path);
2626
2627 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2628 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2629
2630 // length is limited by the maximum positive signed 32-bit integer.
2631 // Unsigned comparison of length and index checks for index < 0
2632 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002633 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002634}
2635
Alexey Frunze15958152017-02-09 19:08:30 -08002636// Temp is used for read barrier.
2637static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2638 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002639 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002640 (kUseBakerReadBarrier ||
2641 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2642 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2643 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2644 return 1;
2645 }
2646 return 0;
2647}
2648
2649// Extra temp is used for read barrier.
2650static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2651 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2652}
2653
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002655 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2656 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2657
2658 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2659 switch (type_check_kind) {
2660 case TypeCheckKind::kExactCheck:
2661 case TypeCheckKind::kAbstractClassCheck:
2662 case TypeCheckKind::kClassHierarchyCheck:
2663 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002664 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002665 ? LocationSummary::kCallOnSlowPath
2666 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2667 break;
2668 case TypeCheckKind::kArrayCheck:
2669 case TypeCheckKind::kUnresolvedCheck:
2670 case TypeCheckKind::kInterfaceCheck:
2671 call_kind = LocationSummary::kCallOnSlowPath;
2672 break;
2673 }
2674
Vladimir Markoca6fff82017-10-03 14:49:14 +01002675 LocationSummary* locations =
2676 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002677 locations->SetInAt(0, Location::RequiresRegister());
2678 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002679 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002680}
2681
2682void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002683 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002684 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002685 Location obj_loc = locations->InAt(0);
2686 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002687 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002688 Location temp_loc = locations->GetTemp(0);
2689 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2690 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2691 DCHECK_LE(num_temps, 2u);
2692 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002693 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2694 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2695 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2696 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2697 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2698 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2699 const uint32_t object_array_data_offset =
2700 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2701 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002702
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002703 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2704 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2705 // read barriers is done for performance and code size reasons.
2706 bool is_type_check_slow_path_fatal = false;
2707 if (!kEmitCompilerReadBarrier) {
2708 is_type_check_slow_path_fatal =
2709 (type_check_kind == TypeCheckKind::kExactCheck ||
2710 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2711 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2712 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2713 !instruction->CanThrowIntoCatchBlock();
2714 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002715 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002716 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2717 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002718 codegen_->AddSlowPath(slow_path);
2719
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002720 // Avoid this check if we know `obj` is not null.
2721 if (instruction->MustDoNullCheck()) {
2722 __ Beqzc(obj, &done);
2723 }
2724
2725 switch (type_check_kind) {
2726 case TypeCheckKind::kExactCheck:
2727 case TypeCheckKind::kArrayCheck: {
2728 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002729 GenerateReferenceLoadTwoRegisters(instruction,
2730 temp_loc,
2731 obj_loc,
2732 class_offset,
2733 maybe_temp2_loc,
2734 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002735 // Jump to slow path for throwing the exception or doing a
2736 // more involved array check.
2737 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2738 break;
2739 }
2740
2741 case TypeCheckKind::kAbstractClassCheck: {
2742 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002743 GenerateReferenceLoadTwoRegisters(instruction,
2744 temp_loc,
2745 obj_loc,
2746 class_offset,
2747 maybe_temp2_loc,
2748 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002749 // If the class is abstract, we eagerly fetch the super class of the
2750 // object to avoid doing a comparison we know will fail.
2751 Mips64Label loop;
2752 __ Bind(&loop);
2753 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002754 GenerateReferenceLoadOneRegister(instruction,
2755 temp_loc,
2756 super_offset,
2757 maybe_temp2_loc,
2758 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002759 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2760 // exception.
2761 __ Beqzc(temp, slow_path->GetEntryLabel());
2762 // Otherwise, compare the classes.
2763 __ Bnec(temp, cls, &loop);
2764 break;
2765 }
2766
2767 case TypeCheckKind::kClassHierarchyCheck: {
2768 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002769 GenerateReferenceLoadTwoRegisters(instruction,
2770 temp_loc,
2771 obj_loc,
2772 class_offset,
2773 maybe_temp2_loc,
2774 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002775 // Walk over the class hierarchy to find a match.
2776 Mips64Label loop;
2777 __ Bind(&loop);
2778 __ Beqc(temp, cls, &done);
2779 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002780 GenerateReferenceLoadOneRegister(instruction,
2781 temp_loc,
2782 super_offset,
2783 maybe_temp2_loc,
2784 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002785 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2786 // exception. Otherwise, jump to the beginning of the loop.
2787 __ Bnezc(temp, &loop);
2788 __ Bc(slow_path->GetEntryLabel());
2789 break;
2790 }
2791
2792 case TypeCheckKind::kArrayObjectCheck: {
2793 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002794 GenerateReferenceLoadTwoRegisters(instruction,
2795 temp_loc,
2796 obj_loc,
2797 class_offset,
2798 maybe_temp2_loc,
2799 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002800 // Do an exact check.
2801 __ Beqc(temp, cls, &done);
2802 // Otherwise, we need to check that the object's class is a non-primitive array.
2803 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002804 GenerateReferenceLoadOneRegister(instruction,
2805 temp_loc,
2806 component_offset,
2807 maybe_temp2_loc,
2808 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002809 // If the component type is null, jump to the slow path to throw the exception.
2810 __ Beqzc(temp, slow_path->GetEntryLabel());
2811 // Otherwise, the object is indeed an array, further check that this component
2812 // type is not a primitive type.
2813 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2814 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2815 __ Bnezc(temp, slow_path->GetEntryLabel());
2816 break;
2817 }
2818
2819 case TypeCheckKind::kUnresolvedCheck:
2820 // We always go into the type check slow path for the unresolved check case.
2821 // We cannot directly call the CheckCast runtime entry point
2822 // without resorting to a type checking slow path here (i.e. by
2823 // calling InvokeRuntime directly), as it would require to
2824 // assign fixed registers for the inputs of this HInstanceOf
2825 // instruction (following the runtime calling convention), which
2826 // might be cluttered by the potential first read barrier
2827 // emission at the beginning of this method.
2828 __ Bc(slow_path->GetEntryLabel());
2829 break;
2830
2831 case TypeCheckKind::kInterfaceCheck: {
2832 // Avoid read barriers to improve performance of the fast path. We can not get false
2833 // positives by doing this.
2834 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002835 GenerateReferenceLoadTwoRegisters(instruction,
2836 temp_loc,
2837 obj_loc,
2838 class_offset,
2839 maybe_temp2_loc,
2840 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002841 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002842 GenerateReferenceLoadTwoRegisters(instruction,
2843 temp_loc,
2844 temp_loc,
2845 iftable_offset,
2846 maybe_temp2_loc,
2847 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002848 // Iftable is never null.
2849 __ Lw(TMP, temp, array_length_offset);
2850 // Loop through the iftable and check if any class matches.
2851 Mips64Label loop;
2852 __ Bind(&loop);
2853 __ Beqzc(TMP, slow_path->GetEntryLabel());
2854 __ Lwu(AT, temp, object_array_data_offset);
2855 __ MaybeUnpoisonHeapReference(AT);
2856 // Go to next interface.
2857 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2858 __ Addiu(TMP, TMP, -2);
2859 // Compare the classes and continue the loop if they do not match.
2860 __ Bnec(AT, cls, &loop);
2861 break;
2862 }
2863 }
2864
2865 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002866 __ Bind(slow_path->GetExitLabel());
2867}
2868
2869void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2870 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002871 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002872 locations->SetInAt(0, Location::RequiresRegister());
2873 if (check->HasUses()) {
2874 locations->SetOut(Location::SameAsFirstInput());
2875 }
2876}
2877
2878void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2879 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01002880 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002881 check->GetLoadClass(),
2882 check,
2883 check->GetDexPc(),
2884 true);
2885 codegen_->AddSlowPath(slow_path);
2886 GenerateClassInitializationCheck(slow_path,
2887 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2888}
2889
2890void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002892
Vladimir Markoca6fff82017-10-03 14:49:14 +01002893 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002894
2895 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002897 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002900 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kInt32:
2902 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002903 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002904 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2906 break;
2907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 case DataType::Type::kFloat32:
2909 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08002910 locations->SetInAt(0, Location::RequiresFpuRegister());
2911 locations->SetInAt(1, Location::RequiresFpuRegister());
2912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002913 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002914
2915 default:
2916 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2917 }
2918}
2919
2920void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2921 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002922 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002923 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002924
2925 // 0 if: left == right
2926 // 1 if: left > right
2927 // -1 if: left < right
2928 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002930 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002931 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002933 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kInt32:
2935 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002936 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002937 Location rhs_location = locations->InAt(1);
2938 bool use_imm = rhs_location.IsConstant();
2939 GpuRegister rhs = ZERO;
2940 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002941 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08002942 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2943 if (value != 0) {
2944 rhs = AT;
2945 __ LoadConst64(rhs, value);
2946 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002947 } else {
2948 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2949 if (value != 0) {
2950 rhs = AT;
2951 __ LoadConst32(rhs, value);
2952 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002953 }
2954 } else {
2955 rhs = rhs_location.AsRegister<GpuRegister>();
2956 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002957 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002958 __ Slt(res, rhs, lhs);
2959 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 break;
2961 }
2962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002964 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2965 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2966 Mips64Label done;
2967 __ CmpEqS(FTMP, lhs, rhs);
2968 __ LoadConst32(res, 0);
2969 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002970 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002971 __ CmpLtS(FTMP, lhs, rhs);
2972 __ LoadConst32(res, -1);
2973 __ Bc1nez(FTMP, &done);
2974 __ LoadConst32(res, 1);
2975 } else {
2976 __ CmpLtS(FTMP, rhs, lhs);
2977 __ LoadConst32(res, 1);
2978 __ Bc1nez(FTMP, &done);
2979 __ LoadConst32(res, -1);
2980 }
2981 __ Bind(&done);
2982 break;
2983 }
2984
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002986 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2987 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2988 Mips64Label done;
2989 __ CmpEqD(FTMP, lhs, rhs);
2990 __ LoadConst32(res, 0);
2991 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002992 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002993 __ CmpLtD(FTMP, lhs, rhs);
2994 __ LoadConst32(res, -1);
2995 __ Bc1nez(FTMP, &done);
2996 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002997 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002998 __ CmpLtD(FTMP, rhs, lhs);
2999 __ LoadConst32(res, 1);
3000 __ Bc1nez(FTMP, &done);
3001 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003003 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003004 break;
3005 }
3006
3007 default:
3008 LOG(FATAL) << "Unimplemented compare type " << in_type;
3009 }
3010}
3011
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003012void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003013 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003014 switch (instruction->InputAt(0)->GetType()) {
3015 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003017 locations->SetInAt(0, Location::RequiresRegister());
3018 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3019 break;
3020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003021 case DataType::Type::kFloat32:
3022 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003023 locations->SetInAt(0, Location::RequiresFpuRegister());
3024 locations->SetInAt(1, Location::RequiresFpuRegister());
3025 break;
3026 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003027 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3029 }
3030}
3031
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003032void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003033 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003034 return;
3035 }
3036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003037 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003038 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003039 switch (type) {
3040 default:
3041 // Integer case.
3042 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3043 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003045 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3046 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003047 case DataType::Type::kFloat32:
3048 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003049 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3050 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003051 }
3052}
3053
Alexey Frunzec857c742015-09-23 15:12:39 -07003054void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3055 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003056 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003057
3058 LocationSummary* locations = instruction->GetLocations();
3059 Location second = locations->InAt(1);
3060 DCHECK(second.IsConstant());
3061
3062 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3063 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3064 int64_t imm = Int64FromConstant(second.GetConstant());
3065 DCHECK(imm == 1 || imm == -1);
3066
3067 if (instruction->IsRem()) {
3068 __ Move(out, ZERO);
3069 } else {
3070 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003071 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003072 __ Subu(out, ZERO, dividend);
3073 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003075 __ Dsubu(out, ZERO, dividend);
3076 }
3077 } else if (out != dividend) {
3078 __ Move(out, dividend);
3079 }
3080 }
3081}
3082
3083void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3084 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003086
3087 LocationSummary* locations = instruction->GetLocations();
3088 Location second = locations->InAt(1);
3089 DCHECK(second.IsConstant());
3090
3091 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3092 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3093 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003094 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003095 int ctz_imm = CTZ(abs_imm);
3096
3097 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003098 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003099 if (ctz_imm == 1) {
3100 // Fast path for division by +/-2, which is very common.
3101 __ Srl(TMP, dividend, 31);
3102 } else {
3103 __ Sra(TMP, dividend, 31);
3104 __ Srl(TMP, TMP, 32 - ctz_imm);
3105 }
3106 __ Addu(out, dividend, TMP);
3107 __ Sra(out, out, ctz_imm);
3108 if (imm < 0) {
3109 __ Subu(out, ZERO, out);
3110 }
3111 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003113 if (ctz_imm == 1) {
3114 // Fast path for division by +/-2, which is very common.
3115 __ Dsrl32(TMP, dividend, 31);
3116 } else {
3117 __ Dsra32(TMP, dividend, 31);
3118 if (ctz_imm > 32) {
3119 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3120 } else {
3121 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3122 }
3123 }
3124 __ Daddu(out, dividend, TMP);
3125 if (ctz_imm < 32) {
3126 __ Dsra(out, out, ctz_imm);
3127 } else {
3128 __ Dsra32(out, out, ctz_imm - 32);
3129 }
3130 if (imm < 0) {
3131 __ Dsubu(out, ZERO, out);
3132 }
3133 }
3134 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003136 if (ctz_imm == 1) {
3137 // Fast path for modulo +/-2, which is very common.
3138 __ Sra(TMP, dividend, 31);
3139 __ Subu(out, dividend, TMP);
3140 __ Andi(out, out, 1);
3141 __ Addu(out, out, TMP);
3142 } else {
3143 __ Sra(TMP, dividend, 31);
3144 __ Srl(TMP, TMP, 32 - ctz_imm);
3145 __ Addu(out, dividend, TMP);
3146 if (IsUint<16>(abs_imm - 1)) {
3147 __ Andi(out, out, abs_imm - 1);
3148 } else {
3149 __ Sll(out, out, 32 - ctz_imm);
3150 __ Srl(out, out, 32 - ctz_imm);
3151 }
3152 __ Subu(out, out, TMP);
3153 }
3154 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003155 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003156 if (ctz_imm == 1) {
3157 // Fast path for modulo +/-2, which is very common.
3158 __ Dsra32(TMP, dividend, 31);
3159 __ Dsubu(out, dividend, TMP);
3160 __ Andi(out, out, 1);
3161 __ Daddu(out, out, TMP);
3162 } else {
3163 __ Dsra32(TMP, dividend, 31);
3164 if (ctz_imm > 32) {
3165 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3166 } else {
3167 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3168 }
3169 __ Daddu(out, dividend, TMP);
3170 if (IsUint<16>(abs_imm - 1)) {
3171 __ Andi(out, out, abs_imm - 1);
3172 } else {
3173 if (ctz_imm > 32) {
3174 __ Dsll(out, out, 64 - ctz_imm);
3175 __ Dsrl(out, out, 64 - ctz_imm);
3176 } else {
3177 __ Dsll32(out, out, 32 - ctz_imm);
3178 __ Dsrl32(out, out, 32 - ctz_imm);
3179 }
3180 }
3181 __ Dsubu(out, out, TMP);
3182 }
3183 }
3184 }
3185}
3186
3187void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3188 DCHECK(instruction->IsDiv() || instruction->IsRem());
3189
3190 LocationSummary* locations = instruction->GetLocations();
3191 Location second = locations->InAt(1);
3192 DCHECK(second.IsConstant());
3193
3194 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3195 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3196 int64_t imm = Int64FromConstant(second.GetConstant());
3197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003198 DataType::Type type = instruction->GetResultType();
3199 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003200
3201 int64_t magic;
3202 int shift;
3203 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003204 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003205 &magic,
3206 &shift);
3207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003208 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003209 __ LoadConst32(TMP, magic);
3210 __ MuhR6(TMP, dividend, TMP);
3211
3212 if (imm > 0 && magic < 0) {
3213 __ Addu(TMP, TMP, dividend);
3214 } else if (imm < 0 && magic > 0) {
3215 __ Subu(TMP, TMP, dividend);
3216 }
3217
3218 if (shift != 0) {
3219 __ Sra(TMP, TMP, shift);
3220 }
3221
3222 if (instruction->IsDiv()) {
3223 __ Sra(out, TMP, 31);
3224 __ Subu(out, TMP, out);
3225 } else {
3226 __ Sra(AT, TMP, 31);
3227 __ Subu(AT, TMP, AT);
3228 __ LoadConst32(TMP, imm);
3229 __ MulR6(TMP, AT, TMP);
3230 __ Subu(out, dividend, TMP);
3231 }
3232 } else {
3233 __ LoadConst64(TMP, magic);
3234 __ Dmuh(TMP, dividend, TMP);
3235
3236 if (imm > 0 && magic < 0) {
3237 __ Daddu(TMP, TMP, dividend);
3238 } else if (imm < 0 && magic > 0) {
3239 __ Dsubu(TMP, TMP, dividend);
3240 }
3241
3242 if (shift >= 32) {
3243 __ Dsra32(TMP, TMP, shift - 32);
3244 } else if (shift > 0) {
3245 __ Dsra(TMP, TMP, shift);
3246 }
3247
3248 if (instruction->IsDiv()) {
3249 __ Dsra32(out, TMP, 31);
3250 __ Dsubu(out, TMP, out);
3251 } else {
3252 __ Dsra32(AT, TMP, 31);
3253 __ Dsubu(AT, TMP, AT);
3254 __ LoadConst64(TMP, imm);
3255 __ Dmul(TMP, AT, TMP);
3256 __ Dsubu(out, dividend, TMP);
3257 }
3258 }
3259}
3260
3261void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3262 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 DataType::Type type = instruction->GetResultType();
3264 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003265
3266 LocationSummary* locations = instruction->GetLocations();
3267 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3268 Location second = locations->InAt(1);
3269
3270 if (second.IsConstant()) {
3271 int64_t imm = Int64FromConstant(second.GetConstant());
3272 if (imm == 0) {
3273 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3274 } else if (imm == 1 || imm == -1) {
3275 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003276 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003277 DivRemByPowerOfTwo(instruction);
3278 } else {
3279 DCHECK(imm <= -2 || imm >= 2);
3280 GenerateDivRemWithAnyConstant(instruction);
3281 }
3282 } else {
3283 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3284 GpuRegister divisor = second.AsRegister<GpuRegister>();
3285 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003286 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003287 __ DivR6(out, dividend, divisor);
3288 else
3289 __ Ddiv(out, dividend, divisor);
3290 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003291 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003292 __ ModR6(out, dividend, divisor);
3293 else
3294 __ Dmod(out, dividend, divisor);
3295 }
3296 }
3297}
3298
Alexey Frunze4dda3372015-06-01 18:31:49 -07003299void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3300 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003301 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003302 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003303 case DataType::Type::kInt32:
3304 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003305 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003306 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3308 break;
3309
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003310 case DataType::Type::kFloat32:
3311 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003312 locations->SetInAt(0, Location::RequiresFpuRegister());
3313 locations->SetInAt(1, Location::RequiresFpuRegister());
3314 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3315 break;
3316
3317 default:
3318 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3319 }
3320}
3321
3322void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003323 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003324 LocationSummary* locations = instruction->GetLocations();
3325
3326 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003327 case DataType::Type::kInt32:
3328 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003329 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003330 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003331 case DataType::Type::kFloat32:
3332 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003333 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3334 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3335 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003336 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003337 __ DivS(dst, lhs, rhs);
3338 else
3339 __ DivD(dst, lhs, rhs);
3340 break;
3341 }
3342 default:
3343 LOG(FATAL) << "Unexpected div type " << type;
3344 }
3345}
3346
3347void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003348 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003349 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003350}
3351
3352void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3353 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003354 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003355 codegen_->AddSlowPath(slow_path);
3356 Location value = instruction->GetLocations()->InAt(0);
3357
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003358 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003359
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003360 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003361 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003362 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003363 }
3364
3365 if (value.IsConstant()) {
3366 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3367 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003368 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003369 } else {
3370 // A division by a non-null constant is valid. We don't need to perform
3371 // any check, so simply fall through.
3372 }
3373 } else {
3374 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3375 }
3376}
3377
3378void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3379 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003380 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003381 locations->SetOut(Location::ConstantLocation(constant));
3382}
3383
3384void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3385 // Will be generated at use site.
3386}
3387
3388void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3389 exit->SetLocations(nullptr);
3390}
3391
3392void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3393}
3394
3395void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3396 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003397 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003398 locations->SetOut(Location::ConstantLocation(constant));
3399}
3400
3401void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3402 // Will be generated at use site.
3403}
3404
David Brazdilfc6a86a2015-06-26 10:33:45 +00003405void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406 DCHECK(!successor->IsExitBlock());
3407 HBasicBlock* block = got->GetBlock();
3408 HInstruction* previous = got->GetPrevious();
3409 HLoopInformation* info = block->GetLoopInformation();
3410
3411 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003412 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3413 return;
3414 }
3415 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3416 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3417 }
3418 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003419 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003420 }
3421}
3422
David Brazdilfc6a86a2015-06-26 10:33:45 +00003423void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3424 got->SetLocations(nullptr);
3425}
3426
3427void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3428 HandleGoto(got, got->GetSuccessor());
3429}
3430
3431void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3432 try_boundary->SetLocations(nullptr);
3433}
3434
3435void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3436 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3437 if (!successor->IsExitBlock()) {
3438 HandleGoto(try_boundary, successor);
3439 }
3440}
3441
Alexey Frunze299a9392015-12-08 16:08:02 -08003442void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3443 bool is64bit,
3444 LocationSummary* locations) {
3445 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3446 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3447 Location rhs_location = locations->InAt(1);
3448 GpuRegister rhs_reg = ZERO;
3449 int64_t rhs_imm = 0;
3450 bool use_imm = rhs_location.IsConstant();
3451 if (use_imm) {
3452 if (is64bit) {
3453 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3454 } else {
3455 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3456 }
3457 } else {
3458 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3459 }
3460 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3461
3462 switch (cond) {
3463 case kCondEQ:
3464 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003465 if (use_imm && IsInt<16>(-rhs_imm)) {
3466 if (rhs_imm == 0) {
3467 if (cond == kCondEQ) {
3468 __ Sltiu(dst, lhs, 1);
3469 } else {
3470 __ Sltu(dst, ZERO, lhs);
3471 }
3472 } else {
3473 if (is64bit) {
3474 __ Daddiu(dst, lhs, -rhs_imm);
3475 } else {
3476 __ Addiu(dst, lhs, -rhs_imm);
3477 }
3478 if (cond == kCondEQ) {
3479 __ Sltiu(dst, dst, 1);
3480 } else {
3481 __ Sltu(dst, ZERO, dst);
3482 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003483 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003484 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003485 if (use_imm && IsUint<16>(rhs_imm)) {
3486 __ Xori(dst, lhs, rhs_imm);
3487 } else {
3488 if (use_imm) {
3489 rhs_reg = TMP;
3490 __ LoadConst64(rhs_reg, rhs_imm);
3491 }
3492 __ Xor(dst, lhs, rhs_reg);
3493 }
3494 if (cond == kCondEQ) {
3495 __ Sltiu(dst, dst, 1);
3496 } else {
3497 __ Sltu(dst, ZERO, dst);
3498 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003499 }
3500 break;
3501
3502 case kCondLT:
3503 case kCondGE:
3504 if (use_imm && IsInt<16>(rhs_imm)) {
3505 __ Slti(dst, lhs, rhs_imm);
3506 } else {
3507 if (use_imm) {
3508 rhs_reg = TMP;
3509 __ LoadConst64(rhs_reg, rhs_imm);
3510 }
3511 __ Slt(dst, lhs, rhs_reg);
3512 }
3513 if (cond == kCondGE) {
3514 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3515 // only the slt instruction but no sge.
3516 __ Xori(dst, dst, 1);
3517 }
3518 break;
3519
3520 case kCondLE:
3521 case kCondGT:
3522 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3523 // Simulate lhs <= rhs via lhs < rhs + 1.
3524 __ Slti(dst, lhs, rhs_imm_plus_one);
3525 if (cond == kCondGT) {
3526 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3527 // only the slti instruction but no sgti.
3528 __ Xori(dst, dst, 1);
3529 }
3530 } else {
3531 if (use_imm) {
3532 rhs_reg = TMP;
3533 __ LoadConst64(rhs_reg, rhs_imm);
3534 }
3535 __ Slt(dst, rhs_reg, lhs);
3536 if (cond == kCondLE) {
3537 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3538 // only the slt instruction but no sle.
3539 __ Xori(dst, dst, 1);
3540 }
3541 }
3542 break;
3543
3544 case kCondB:
3545 case kCondAE:
3546 if (use_imm && IsInt<16>(rhs_imm)) {
3547 // Sltiu sign-extends its 16-bit immediate operand before
3548 // the comparison and thus lets us compare directly with
3549 // unsigned values in the ranges [0, 0x7fff] and
3550 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3551 __ Sltiu(dst, lhs, rhs_imm);
3552 } else {
3553 if (use_imm) {
3554 rhs_reg = TMP;
3555 __ LoadConst64(rhs_reg, rhs_imm);
3556 }
3557 __ Sltu(dst, lhs, rhs_reg);
3558 }
3559 if (cond == kCondAE) {
3560 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3561 // only the sltu instruction but no sgeu.
3562 __ Xori(dst, dst, 1);
3563 }
3564 break;
3565
3566 case kCondBE:
3567 case kCondA:
3568 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3569 // Simulate lhs <= rhs via lhs < rhs + 1.
3570 // Note that this only works if rhs + 1 does not overflow
3571 // to 0, hence the check above.
3572 // Sltiu sign-extends its 16-bit immediate operand before
3573 // the comparison and thus lets us compare directly with
3574 // unsigned values in the ranges [0, 0x7fff] and
3575 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3576 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3577 if (cond == kCondA) {
3578 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3579 // only the sltiu instruction but no sgtiu.
3580 __ Xori(dst, dst, 1);
3581 }
3582 } else {
3583 if (use_imm) {
3584 rhs_reg = TMP;
3585 __ LoadConst64(rhs_reg, rhs_imm);
3586 }
3587 __ Sltu(dst, rhs_reg, lhs);
3588 if (cond == kCondBE) {
3589 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3590 // only the sltu instruction but no sleu.
3591 __ Xori(dst, dst, 1);
3592 }
3593 }
3594 break;
3595 }
3596}
3597
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003598bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3599 bool is64bit,
3600 LocationSummary* input_locations,
3601 GpuRegister dst) {
3602 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3603 Location rhs_location = input_locations->InAt(1);
3604 GpuRegister rhs_reg = ZERO;
3605 int64_t rhs_imm = 0;
3606 bool use_imm = rhs_location.IsConstant();
3607 if (use_imm) {
3608 if (is64bit) {
3609 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3610 } else {
3611 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3612 }
3613 } else {
3614 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3615 }
3616 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3617
3618 switch (cond) {
3619 case kCondEQ:
3620 case kCondNE:
3621 if (use_imm && IsInt<16>(-rhs_imm)) {
3622 if (is64bit) {
3623 __ Daddiu(dst, lhs, -rhs_imm);
3624 } else {
3625 __ Addiu(dst, lhs, -rhs_imm);
3626 }
3627 } else if (use_imm && IsUint<16>(rhs_imm)) {
3628 __ Xori(dst, lhs, rhs_imm);
3629 } else {
3630 if (use_imm) {
3631 rhs_reg = TMP;
3632 __ LoadConst64(rhs_reg, rhs_imm);
3633 }
3634 __ Xor(dst, lhs, rhs_reg);
3635 }
3636 return (cond == kCondEQ);
3637
3638 case kCondLT:
3639 case kCondGE:
3640 if (use_imm && IsInt<16>(rhs_imm)) {
3641 __ Slti(dst, lhs, rhs_imm);
3642 } else {
3643 if (use_imm) {
3644 rhs_reg = TMP;
3645 __ LoadConst64(rhs_reg, rhs_imm);
3646 }
3647 __ Slt(dst, lhs, rhs_reg);
3648 }
3649 return (cond == kCondGE);
3650
3651 case kCondLE:
3652 case kCondGT:
3653 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3654 // Simulate lhs <= rhs via lhs < rhs + 1.
3655 __ Slti(dst, lhs, rhs_imm_plus_one);
3656 return (cond == kCondGT);
3657 } else {
3658 if (use_imm) {
3659 rhs_reg = TMP;
3660 __ LoadConst64(rhs_reg, rhs_imm);
3661 }
3662 __ Slt(dst, rhs_reg, lhs);
3663 return (cond == kCondLE);
3664 }
3665
3666 case kCondB:
3667 case kCondAE:
3668 if (use_imm && IsInt<16>(rhs_imm)) {
3669 // Sltiu sign-extends its 16-bit immediate operand before
3670 // the comparison and thus lets us compare directly with
3671 // unsigned values in the ranges [0, 0x7fff] and
3672 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3673 __ Sltiu(dst, lhs, rhs_imm);
3674 } else {
3675 if (use_imm) {
3676 rhs_reg = TMP;
3677 __ LoadConst64(rhs_reg, rhs_imm);
3678 }
3679 __ Sltu(dst, lhs, rhs_reg);
3680 }
3681 return (cond == kCondAE);
3682
3683 case kCondBE:
3684 case kCondA:
3685 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3686 // Simulate lhs <= rhs via lhs < rhs + 1.
3687 // Note that this only works if rhs + 1 does not overflow
3688 // to 0, hence the check above.
3689 // Sltiu sign-extends its 16-bit immediate operand before
3690 // the comparison and thus lets us compare directly with
3691 // unsigned values in the ranges [0, 0x7fff] and
3692 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3693 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3694 return (cond == kCondA);
3695 } else {
3696 if (use_imm) {
3697 rhs_reg = TMP;
3698 __ LoadConst64(rhs_reg, rhs_imm);
3699 }
3700 __ Sltu(dst, rhs_reg, lhs);
3701 return (cond == kCondBE);
3702 }
3703 }
3704}
3705
Alexey Frunze299a9392015-12-08 16:08:02 -08003706void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3707 bool is64bit,
3708 LocationSummary* locations,
3709 Mips64Label* label) {
3710 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3711 Location rhs_location = locations->InAt(1);
3712 GpuRegister rhs_reg = ZERO;
3713 int64_t rhs_imm = 0;
3714 bool use_imm = rhs_location.IsConstant();
3715 if (use_imm) {
3716 if (is64bit) {
3717 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3718 } else {
3719 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3720 }
3721 } else {
3722 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3723 }
3724
3725 if (use_imm && rhs_imm == 0) {
3726 switch (cond) {
3727 case kCondEQ:
3728 case kCondBE: // <= 0 if zero
3729 __ Beqzc(lhs, label);
3730 break;
3731 case kCondNE:
3732 case kCondA: // > 0 if non-zero
3733 __ Bnezc(lhs, label);
3734 break;
3735 case kCondLT:
3736 __ Bltzc(lhs, label);
3737 break;
3738 case kCondGE:
3739 __ Bgezc(lhs, label);
3740 break;
3741 case kCondLE:
3742 __ Blezc(lhs, label);
3743 break;
3744 case kCondGT:
3745 __ Bgtzc(lhs, label);
3746 break;
3747 case kCondB: // always false
3748 break;
3749 case kCondAE: // always true
3750 __ Bc(label);
3751 break;
3752 }
3753 } else {
3754 if (use_imm) {
3755 rhs_reg = TMP;
3756 __ LoadConst64(rhs_reg, rhs_imm);
3757 }
3758 switch (cond) {
3759 case kCondEQ:
3760 __ Beqc(lhs, rhs_reg, label);
3761 break;
3762 case kCondNE:
3763 __ Bnec(lhs, rhs_reg, label);
3764 break;
3765 case kCondLT:
3766 __ Bltc(lhs, rhs_reg, label);
3767 break;
3768 case kCondGE:
3769 __ Bgec(lhs, rhs_reg, label);
3770 break;
3771 case kCondLE:
3772 __ Bgec(rhs_reg, lhs, label);
3773 break;
3774 case kCondGT:
3775 __ Bltc(rhs_reg, lhs, label);
3776 break;
3777 case kCondB:
3778 __ Bltuc(lhs, rhs_reg, label);
3779 break;
3780 case kCondAE:
3781 __ Bgeuc(lhs, rhs_reg, label);
3782 break;
3783 case kCondBE:
3784 __ Bgeuc(rhs_reg, lhs, label);
3785 break;
3786 case kCondA:
3787 __ Bltuc(rhs_reg, lhs, label);
3788 break;
3789 }
3790 }
3791}
3792
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003793void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3794 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003795 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003796 LocationSummary* locations) {
3797 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3798 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3799 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003801 switch (cond) {
3802 case kCondEQ:
3803 __ CmpEqS(FTMP, lhs, rhs);
3804 __ Mfc1(dst, FTMP);
3805 __ Andi(dst, dst, 1);
3806 break;
3807 case kCondNE:
3808 __ CmpEqS(FTMP, lhs, rhs);
3809 __ Mfc1(dst, FTMP);
3810 __ Addiu(dst, dst, 1);
3811 break;
3812 case kCondLT:
3813 if (gt_bias) {
3814 __ CmpLtS(FTMP, lhs, rhs);
3815 } else {
3816 __ CmpUltS(FTMP, lhs, rhs);
3817 }
3818 __ Mfc1(dst, FTMP);
3819 __ Andi(dst, dst, 1);
3820 break;
3821 case kCondLE:
3822 if (gt_bias) {
3823 __ CmpLeS(FTMP, lhs, rhs);
3824 } else {
3825 __ CmpUleS(FTMP, lhs, rhs);
3826 }
3827 __ Mfc1(dst, FTMP);
3828 __ Andi(dst, dst, 1);
3829 break;
3830 case kCondGT:
3831 if (gt_bias) {
3832 __ CmpUltS(FTMP, rhs, lhs);
3833 } else {
3834 __ CmpLtS(FTMP, rhs, lhs);
3835 }
3836 __ Mfc1(dst, FTMP);
3837 __ Andi(dst, dst, 1);
3838 break;
3839 case kCondGE:
3840 if (gt_bias) {
3841 __ CmpUleS(FTMP, rhs, lhs);
3842 } else {
3843 __ CmpLeS(FTMP, rhs, lhs);
3844 }
3845 __ Mfc1(dst, FTMP);
3846 __ Andi(dst, dst, 1);
3847 break;
3848 default:
3849 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3850 UNREACHABLE();
3851 }
3852 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003853 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003854 switch (cond) {
3855 case kCondEQ:
3856 __ CmpEqD(FTMP, lhs, rhs);
3857 __ Mfc1(dst, FTMP);
3858 __ Andi(dst, dst, 1);
3859 break;
3860 case kCondNE:
3861 __ CmpEqD(FTMP, lhs, rhs);
3862 __ Mfc1(dst, FTMP);
3863 __ Addiu(dst, dst, 1);
3864 break;
3865 case kCondLT:
3866 if (gt_bias) {
3867 __ CmpLtD(FTMP, lhs, rhs);
3868 } else {
3869 __ CmpUltD(FTMP, lhs, rhs);
3870 }
3871 __ Mfc1(dst, FTMP);
3872 __ Andi(dst, dst, 1);
3873 break;
3874 case kCondLE:
3875 if (gt_bias) {
3876 __ CmpLeD(FTMP, lhs, rhs);
3877 } else {
3878 __ CmpUleD(FTMP, lhs, rhs);
3879 }
3880 __ Mfc1(dst, FTMP);
3881 __ Andi(dst, dst, 1);
3882 break;
3883 case kCondGT:
3884 if (gt_bias) {
3885 __ CmpUltD(FTMP, rhs, lhs);
3886 } else {
3887 __ CmpLtD(FTMP, rhs, lhs);
3888 }
3889 __ Mfc1(dst, FTMP);
3890 __ Andi(dst, dst, 1);
3891 break;
3892 case kCondGE:
3893 if (gt_bias) {
3894 __ CmpUleD(FTMP, rhs, lhs);
3895 } else {
3896 __ CmpLeD(FTMP, rhs, lhs);
3897 }
3898 __ Mfc1(dst, FTMP);
3899 __ Andi(dst, dst, 1);
3900 break;
3901 default:
3902 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3903 UNREACHABLE();
3904 }
3905 }
3906}
3907
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003908bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
3909 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003910 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003911 LocationSummary* input_locations,
3912 FpuRegister dst) {
3913 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
3914 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003915 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003916 switch (cond) {
3917 case kCondEQ:
3918 __ CmpEqS(dst, lhs, rhs);
3919 return false;
3920 case kCondNE:
3921 __ CmpEqS(dst, lhs, rhs);
3922 return true;
3923 case kCondLT:
3924 if (gt_bias) {
3925 __ CmpLtS(dst, lhs, rhs);
3926 } else {
3927 __ CmpUltS(dst, lhs, rhs);
3928 }
3929 return false;
3930 case kCondLE:
3931 if (gt_bias) {
3932 __ CmpLeS(dst, lhs, rhs);
3933 } else {
3934 __ CmpUleS(dst, lhs, rhs);
3935 }
3936 return false;
3937 case kCondGT:
3938 if (gt_bias) {
3939 __ CmpUltS(dst, rhs, lhs);
3940 } else {
3941 __ CmpLtS(dst, rhs, lhs);
3942 }
3943 return false;
3944 case kCondGE:
3945 if (gt_bias) {
3946 __ CmpUleS(dst, rhs, lhs);
3947 } else {
3948 __ CmpLeS(dst, rhs, lhs);
3949 }
3950 return false;
3951 default:
3952 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3953 UNREACHABLE();
3954 }
3955 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003956 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003957 switch (cond) {
3958 case kCondEQ:
3959 __ CmpEqD(dst, lhs, rhs);
3960 return false;
3961 case kCondNE:
3962 __ CmpEqD(dst, lhs, rhs);
3963 return true;
3964 case kCondLT:
3965 if (gt_bias) {
3966 __ CmpLtD(dst, lhs, rhs);
3967 } else {
3968 __ CmpUltD(dst, lhs, rhs);
3969 }
3970 return false;
3971 case kCondLE:
3972 if (gt_bias) {
3973 __ CmpLeD(dst, lhs, rhs);
3974 } else {
3975 __ CmpUleD(dst, lhs, rhs);
3976 }
3977 return false;
3978 case kCondGT:
3979 if (gt_bias) {
3980 __ CmpUltD(dst, rhs, lhs);
3981 } else {
3982 __ CmpLtD(dst, rhs, lhs);
3983 }
3984 return false;
3985 case kCondGE:
3986 if (gt_bias) {
3987 __ CmpUleD(dst, rhs, lhs);
3988 } else {
3989 __ CmpLeD(dst, rhs, lhs);
3990 }
3991 return false;
3992 default:
3993 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3994 UNREACHABLE();
3995 }
3996 }
3997}
3998
Alexey Frunze299a9392015-12-08 16:08:02 -08003999void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4000 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004001 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004002 LocationSummary* locations,
4003 Mips64Label* label) {
4004 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4005 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004006 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004007 switch (cond) {
4008 case kCondEQ:
4009 __ CmpEqS(FTMP, lhs, rhs);
4010 __ Bc1nez(FTMP, label);
4011 break;
4012 case kCondNE:
4013 __ CmpEqS(FTMP, lhs, rhs);
4014 __ Bc1eqz(FTMP, label);
4015 break;
4016 case kCondLT:
4017 if (gt_bias) {
4018 __ CmpLtS(FTMP, lhs, rhs);
4019 } else {
4020 __ CmpUltS(FTMP, lhs, rhs);
4021 }
4022 __ Bc1nez(FTMP, label);
4023 break;
4024 case kCondLE:
4025 if (gt_bias) {
4026 __ CmpLeS(FTMP, lhs, rhs);
4027 } else {
4028 __ CmpUleS(FTMP, lhs, rhs);
4029 }
4030 __ Bc1nez(FTMP, label);
4031 break;
4032 case kCondGT:
4033 if (gt_bias) {
4034 __ CmpUltS(FTMP, rhs, lhs);
4035 } else {
4036 __ CmpLtS(FTMP, rhs, lhs);
4037 }
4038 __ Bc1nez(FTMP, label);
4039 break;
4040 case kCondGE:
4041 if (gt_bias) {
4042 __ CmpUleS(FTMP, rhs, lhs);
4043 } else {
4044 __ CmpLeS(FTMP, rhs, lhs);
4045 }
4046 __ Bc1nez(FTMP, label);
4047 break;
4048 default:
4049 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004050 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004051 }
4052 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004053 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004054 switch (cond) {
4055 case kCondEQ:
4056 __ CmpEqD(FTMP, lhs, rhs);
4057 __ Bc1nez(FTMP, label);
4058 break;
4059 case kCondNE:
4060 __ CmpEqD(FTMP, lhs, rhs);
4061 __ Bc1eqz(FTMP, label);
4062 break;
4063 case kCondLT:
4064 if (gt_bias) {
4065 __ CmpLtD(FTMP, lhs, rhs);
4066 } else {
4067 __ CmpUltD(FTMP, lhs, rhs);
4068 }
4069 __ Bc1nez(FTMP, label);
4070 break;
4071 case kCondLE:
4072 if (gt_bias) {
4073 __ CmpLeD(FTMP, lhs, rhs);
4074 } else {
4075 __ CmpUleD(FTMP, lhs, rhs);
4076 }
4077 __ Bc1nez(FTMP, label);
4078 break;
4079 case kCondGT:
4080 if (gt_bias) {
4081 __ CmpUltD(FTMP, rhs, lhs);
4082 } else {
4083 __ CmpLtD(FTMP, rhs, lhs);
4084 }
4085 __ Bc1nez(FTMP, label);
4086 break;
4087 case kCondGE:
4088 if (gt_bias) {
4089 __ CmpUleD(FTMP, rhs, lhs);
4090 } else {
4091 __ CmpLeD(FTMP, rhs, lhs);
4092 }
4093 __ Bc1nez(FTMP, label);
4094 break;
4095 default:
4096 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004097 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004098 }
4099 }
4100}
4101
Alexey Frunze4dda3372015-06-01 18:31:49 -07004102void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004103 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004104 Mips64Label* true_target,
4105 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004106 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004107
David Brazdil0debae72015-11-12 18:37:00 +00004108 if (true_target == nullptr && false_target == nullptr) {
4109 // Nothing to do. The code always falls through.
4110 return;
4111 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004112 // Constant condition, statically compared against "true" (integer value 1).
4113 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004114 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004115 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004116 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004117 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004118 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004119 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004120 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004122 }
David Brazdil0debae72015-11-12 18:37:00 +00004123 return;
4124 }
4125
4126 // The following code generates these patterns:
4127 // (1) true_target == nullptr && false_target != nullptr
4128 // - opposite condition true => branch to false_target
4129 // (2) true_target != nullptr && false_target == nullptr
4130 // - condition true => branch to true_target
4131 // (3) true_target != nullptr && false_target != nullptr
4132 // - condition true => branch to true_target
4133 // - branch to false_target
4134 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004135 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004136 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004137 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004138 if (true_target == nullptr) {
4139 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4140 } else {
4141 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4142 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004143 } else {
4144 // The condition instruction has not been materialized, use its inputs as
4145 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004146 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004147 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004148 LocationSummary* locations = cond->GetLocations();
4149 IfCondition if_cond = condition->GetCondition();
4150 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004151
David Brazdil0debae72015-11-12 18:37:00 +00004152 if (true_target == nullptr) {
4153 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004154 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004155 }
4156
Alexey Frunze299a9392015-12-08 16:08:02 -08004157 switch (type) {
4158 default:
4159 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4160 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004161 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004162 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4163 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004164 case DataType::Type::kFloat32:
4165 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004166 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4167 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004168 }
4169 }
David Brazdil0debae72015-11-12 18:37:00 +00004170
4171 // If neither branch falls through (case 3), the conditional branch to `true_target`
4172 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4173 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004174 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004175 }
4176}
4177
4178void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004179 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004180 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004181 locations->SetInAt(0, Location::RequiresRegister());
4182 }
4183}
4184
4185void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004186 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4187 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004188 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004189 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004190 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004191 nullptr : codegen_->GetLabelOf(false_successor);
4192 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004193}
4194
4195void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004196 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004197 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004198 InvokeRuntimeCallingConvention calling_convention;
4199 RegisterSet caller_saves = RegisterSet::Empty();
4200 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4201 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004202 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004203 locations->SetInAt(0, Location::RequiresRegister());
4204 }
4205}
4206
4207void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004208 SlowPathCodeMIPS64* slow_path =
4209 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004210 GenerateTestAndBranch(deoptimize,
4211 /* condition_input_index */ 0,
4212 slow_path->GetEntryLabel(),
4213 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004214}
4215
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004216// This function returns true if a conditional move can be generated for HSelect.
4217// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4218// branches and regular moves.
4219//
4220// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4221//
4222// While determining feasibility of a conditional move and setting inputs/outputs
4223// are two distinct tasks, this function does both because they share quite a bit
4224// of common logic.
4225static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4226 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4227 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4228 HCondition* condition = cond->AsCondition();
4229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004230 DataType::Type cond_type =
4231 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4232 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004233
4234 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4235 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4236 bool is_true_value_zero_constant =
4237 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4238 bool is_false_value_zero_constant =
4239 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4240
4241 bool can_move_conditionally = false;
4242 bool use_const_for_false_in = false;
4243 bool use_const_for_true_in = false;
4244
4245 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004246 if (!DataType::IsFloatingPointType(cond_type)) {
4247 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004248 // Moving int/long on int/long condition.
4249 if (is_true_value_zero_constant) {
4250 // seleqz out_reg, false_reg, cond_reg
4251 can_move_conditionally = true;
4252 use_const_for_true_in = true;
4253 } else if (is_false_value_zero_constant) {
4254 // selnez out_reg, true_reg, cond_reg
4255 can_move_conditionally = true;
4256 use_const_for_false_in = true;
4257 } else if (materialized) {
4258 // Not materializing unmaterialized int conditions
4259 // to keep the instruction count low.
4260 // selnez AT, true_reg, cond_reg
4261 // seleqz TMP, false_reg, cond_reg
4262 // or out_reg, AT, TMP
4263 can_move_conditionally = true;
4264 }
4265 } else {
4266 // Moving float/double on int/long condition.
4267 if (materialized) {
4268 // Not materializing unmaterialized int conditions
4269 // to keep the instruction count low.
4270 can_move_conditionally = true;
4271 if (is_true_value_zero_constant) {
4272 // sltu TMP, ZERO, cond_reg
4273 // mtc1 TMP, temp_cond_reg
4274 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4275 use_const_for_true_in = true;
4276 } else if (is_false_value_zero_constant) {
4277 // sltu TMP, ZERO, cond_reg
4278 // mtc1 TMP, temp_cond_reg
4279 // selnez.fmt out_reg, true_reg, temp_cond_reg
4280 use_const_for_false_in = true;
4281 } else {
4282 // sltu TMP, ZERO, cond_reg
4283 // mtc1 TMP, temp_cond_reg
4284 // sel.fmt temp_cond_reg, false_reg, true_reg
4285 // mov.fmt out_reg, temp_cond_reg
4286 }
4287 }
4288 }
4289 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004290 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004291 // Moving int/long on float/double condition.
4292 can_move_conditionally = true;
4293 if (is_true_value_zero_constant) {
4294 // mfc1 TMP, temp_cond_reg
4295 // seleqz out_reg, false_reg, TMP
4296 use_const_for_true_in = true;
4297 } else if (is_false_value_zero_constant) {
4298 // mfc1 TMP, temp_cond_reg
4299 // selnez out_reg, true_reg, TMP
4300 use_const_for_false_in = true;
4301 } else {
4302 // mfc1 TMP, temp_cond_reg
4303 // selnez AT, true_reg, TMP
4304 // seleqz TMP, false_reg, TMP
4305 // or out_reg, AT, TMP
4306 }
4307 } else {
4308 // Moving float/double on float/double condition.
4309 can_move_conditionally = true;
4310 if (is_true_value_zero_constant) {
4311 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4312 use_const_for_true_in = true;
4313 } else if (is_false_value_zero_constant) {
4314 // selnez.fmt out_reg, true_reg, temp_cond_reg
4315 use_const_for_false_in = true;
4316 } else {
4317 // sel.fmt temp_cond_reg, false_reg, true_reg
4318 // mov.fmt out_reg, temp_cond_reg
4319 }
4320 }
4321 }
4322 }
4323
4324 if (can_move_conditionally) {
4325 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4326 } else {
4327 DCHECK(!use_const_for_false_in);
4328 DCHECK(!use_const_for_true_in);
4329 }
4330
4331 if (locations_to_set != nullptr) {
4332 if (use_const_for_false_in) {
4333 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4334 } else {
4335 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004336 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004337 ? Location::RequiresFpuRegister()
4338 : Location::RequiresRegister());
4339 }
4340 if (use_const_for_true_in) {
4341 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4342 } else {
4343 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004344 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004345 ? Location::RequiresFpuRegister()
4346 : Location::RequiresRegister());
4347 }
4348 if (materialized) {
4349 locations_to_set->SetInAt(2, Location::RequiresRegister());
4350 }
4351
4352 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004354 ? Location::RequiresFpuRegister()
4355 : Location::RequiresRegister());
4356 } else {
4357 locations_to_set->SetOut(Location::SameAsFirstInput());
4358 }
4359 }
4360
4361 return can_move_conditionally;
4362}
4363
4364
4365void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4366 LocationSummary* locations = select->GetLocations();
4367 Location dst = locations->Out();
4368 Location false_src = locations->InAt(0);
4369 Location true_src = locations->InAt(1);
4370 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4371 GpuRegister cond_reg = TMP;
4372 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004373 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004374 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004375 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004376
4377 if (IsBooleanValueOrMaterializedCondition(cond)) {
4378 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4379 } else {
4380 HCondition* condition = cond->AsCondition();
4381 LocationSummary* cond_locations = cond->GetLocations();
4382 IfCondition if_cond = condition->GetCondition();
4383 cond_type = condition->InputAt(0)->GetType();
4384 switch (cond_type) {
4385 default:
4386 cond_inverted = MaterializeIntLongCompare(if_cond,
4387 /* is64bit */ false,
4388 cond_locations,
4389 cond_reg);
4390 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004391 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004392 cond_inverted = MaterializeIntLongCompare(if_cond,
4393 /* is64bit */ true,
4394 cond_locations,
4395 cond_reg);
4396 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004397 case DataType::Type::kFloat32:
4398 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004399 cond_inverted = MaterializeFpCompare(if_cond,
4400 condition->IsGtBias(),
4401 cond_type,
4402 cond_locations,
4403 fcond_reg);
4404 break;
4405 }
4406 }
4407
4408 if (true_src.IsConstant()) {
4409 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4410 }
4411 if (false_src.IsConstant()) {
4412 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4413 }
4414
4415 switch (dst_type) {
4416 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004417 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004418 __ Mfc1(cond_reg, fcond_reg);
4419 }
4420 if (true_src.IsConstant()) {
4421 if (cond_inverted) {
4422 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4423 } else {
4424 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4425 }
4426 } else if (false_src.IsConstant()) {
4427 if (cond_inverted) {
4428 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4429 } else {
4430 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4431 }
4432 } else {
4433 DCHECK_NE(cond_reg, AT);
4434 if (cond_inverted) {
4435 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4436 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4437 } else {
4438 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4439 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4440 }
4441 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4442 }
4443 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004444 case DataType::Type::kFloat32: {
4445 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004446 // sel*.fmt tests bit 0 of the condition register, account for that.
4447 __ Sltu(TMP, ZERO, cond_reg);
4448 __ Mtc1(TMP, fcond_reg);
4449 }
4450 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4451 if (true_src.IsConstant()) {
4452 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4453 if (cond_inverted) {
4454 __ SelnezS(dst_reg, src_reg, fcond_reg);
4455 } else {
4456 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4457 }
4458 } else if (false_src.IsConstant()) {
4459 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4460 if (cond_inverted) {
4461 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4462 } else {
4463 __ SelnezS(dst_reg, src_reg, fcond_reg);
4464 }
4465 } else {
4466 if (cond_inverted) {
4467 __ SelS(fcond_reg,
4468 true_src.AsFpuRegister<FpuRegister>(),
4469 false_src.AsFpuRegister<FpuRegister>());
4470 } else {
4471 __ SelS(fcond_reg,
4472 false_src.AsFpuRegister<FpuRegister>(),
4473 true_src.AsFpuRegister<FpuRegister>());
4474 }
4475 __ MovS(dst_reg, fcond_reg);
4476 }
4477 break;
4478 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004479 case DataType::Type::kFloat64: {
4480 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004481 // sel*.fmt tests bit 0 of the condition register, account for that.
4482 __ Sltu(TMP, ZERO, cond_reg);
4483 __ Mtc1(TMP, fcond_reg);
4484 }
4485 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4486 if (true_src.IsConstant()) {
4487 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4488 if (cond_inverted) {
4489 __ SelnezD(dst_reg, src_reg, fcond_reg);
4490 } else {
4491 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4492 }
4493 } else if (false_src.IsConstant()) {
4494 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4495 if (cond_inverted) {
4496 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4497 } else {
4498 __ SelnezD(dst_reg, src_reg, fcond_reg);
4499 }
4500 } else {
4501 if (cond_inverted) {
4502 __ SelD(fcond_reg,
4503 true_src.AsFpuRegister<FpuRegister>(),
4504 false_src.AsFpuRegister<FpuRegister>());
4505 } else {
4506 __ SelD(fcond_reg,
4507 false_src.AsFpuRegister<FpuRegister>(),
4508 true_src.AsFpuRegister<FpuRegister>());
4509 }
4510 __ MovD(dst_reg, fcond_reg);
4511 }
4512 break;
4513 }
4514 }
4515}
4516
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004517void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004518 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004519 LocationSummary(flag, LocationSummary::kNoCall);
4520 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004521}
4522
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004523void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4524 __ LoadFromOffset(kLoadWord,
4525 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4526 SP,
4527 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004528}
4529
David Brazdil74eb1b22015-12-14 11:44:01 +00004530void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004531 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004532 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004533}
4534
4535void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004536 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4537 GenConditionalMove(select);
4538 } else {
4539 LocationSummary* locations = select->GetLocations();
4540 Mips64Label false_target;
4541 GenerateTestAndBranch(select,
4542 /* condition_input_index */ 2,
4543 /* true_target */ nullptr,
4544 &false_target);
4545 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4546 __ Bind(&false_target);
4547 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004548}
4549
David Srbecky0cf44932015-12-09 14:09:59 +00004550void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004551 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004552}
4553
David Srbeckyd28f4a02016-03-14 17:14:24 +00004554void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4555 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004556}
4557
4558void CodeGeneratorMIPS64::GenerateNop() {
4559 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004560}
4561
Alexey Frunze4dda3372015-06-01 18:31:49 -07004562void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004563 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004564 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004565 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004566 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004567 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004568 instruction,
4569 object_field_get_with_read_barrier
4570 ? LocationSummary::kCallOnSlowPath
4571 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004572 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4573 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4574 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004575 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004576 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004577 locations->SetOut(Location::RequiresFpuRegister());
4578 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004579 // The output overlaps in the case of an object field get with
4580 // read barriers enabled: we do not want the move to overwrite the
4581 // object's location, as we need it to emit the read barrier.
4582 locations->SetOut(Location::RequiresRegister(),
4583 object_field_get_with_read_barrier
4584 ? Location::kOutputOverlap
4585 : Location::kNoOutputOverlap);
4586 }
4587 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4588 // We need a temporary register for the read barrier marking slow
4589 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004590 if (!kBakerReadBarrierThunksEnableForFields) {
4591 locations->AddTemp(Location::RequiresRegister());
4592 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004593 }
4594}
4595
4596void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4597 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004598 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4599 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004600 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004601 Location obj_loc = locations->InAt(0);
4602 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4603 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004604 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004605 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004606 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004607 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4608
Alexey Frunze4dda3372015-06-01 18:31:49 -07004609 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004611 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004612 load_type = kLoadUnsignedByte;
4613 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004614 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004615 load_type = kLoadSignedByte;
4616 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004617 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004618 load_type = kLoadUnsignedHalfword;
4619 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004620 case DataType::Type::kInt16:
4621 load_type = kLoadSignedHalfword;
4622 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004623 case DataType::Type::kInt32:
4624 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004625 load_type = kLoadWord;
4626 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004627 case DataType::Type::kInt64:
4628 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004629 load_type = kLoadDoubleword;
4630 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004631 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004632 load_type = kLoadUnsignedWord;
4633 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004634 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004635 LOG(FATAL) << "Unreachable type " << type;
4636 UNREACHABLE();
4637 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004638 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004639 DCHECK(dst_loc.IsRegister());
4640 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004641 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004642 // /* HeapReference<Object> */ dst = *(obj + offset)
4643 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004644 Location temp_loc =
4645 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004646 // Note that a potential implicit null check is handled in this
4647 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4648 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4649 dst_loc,
4650 obj,
4651 offset,
4652 temp_loc,
4653 /* needs_null_check */ true);
4654 if (is_volatile) {
4655 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4656 }
4657 } else {
4658 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4659 if (is_volatile) {
4660 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4661 }
4662 // If read barriers are enabled, emit read barriers other than
4663 // Baker's using a slow path (and also unpoison the loaded
4664 // reference, if heap poisoning is enabled).
4665 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4666 }
4667 } else {
4668 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4669 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004670 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004671 DCHECK(dst_loc.IsFpuRegister());
4672 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004673 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004674 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004675
Alexey Frunze15958152017-02-09 19:08:30 -08004676 // Memory barriers, in the case of references, are handled in the
4677 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004678 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004679 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004680 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004681}
4682
4683void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4684 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4685 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004686 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004687 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004688 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004689 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004690 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004691 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004692 }
4693}
4694
4695void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004696 const FieldInfo& field_info,
4697 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004698 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004699 LocationSummary* locations = instruction->GetLocations();
4700 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004701 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004702 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004703 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004704 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4705 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004706 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4707
Alexey Frunze4dda3372015-06-01 18:31:49 -07004708 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004709 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004710 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004711 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004712 store_type = kStoreByte;
4713 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004714 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004715 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004716 store_type = kStoreHalfword;
4717 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004718 case DataType::Type::kInt32:
4719 case DataType::Type::kFloat32:
4720 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004721 store_type = kStoreWord;
4722 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004723 case DataType::Type::kInt64:
4724 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004725 store_type = kStoreDoubleword;
4726 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004727 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004728 LOG(FATAL) << "Unreachable type " << type;
4729 UNREACHABLE();
4730 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004731
Alexey Frunze15958152017-02-09 19:08:30 -08004732 if (is_volatile) {
4733 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4734 }
4735
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004736 if (value_location.IsConstant()) {
4737 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4738 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4739 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004741 DCHECK(value_location.IsRegister());
4742 GpuRegister src = value_location.AsRegister<GpuRegister>();
4743 if (kPoisonHeapReferences && needs_write_barrier) {
4744 // Note that in the case where `value` is a null reference,
4745 // we do not enter this block, as a null reference does not
4746 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004748 __ PoisonHeapReference(TMP, src);
4749 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4750 } else {
4751 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4752 }
4753 } else {
4754 DCHECK(value_location.IsFpuRegister());
4755 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4756 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4757 }
4758 }
Alexey Frunze15958152017-02-09 19:08:30 -08004759
Alexey Frunzec061de12017-02-14 13:27:23 -08004760 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004761 DCHECK(value_location.IsRegister());
4762 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004763 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004764 }
Alexey Frunze15958152017-02-09 19:08:30 -08004765
4766 if (is_volatile) {
4767 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4768 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004769}
4770
4771void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4772 HandleFieldGet(instruction, instruction->GetFieldInfo());
4773}
4774
4775void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4776 HandleFieldGet(instruction, instruction->GetFieldInfo());
4777}
4778
4779void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4780 HandleFieldSet(instruction, instruction->GetFieldInfo());
4781}
4782
4783void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004784 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004785}
4786
Alexey Frunze15958152017-02-09 19:08:30 -08004787void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4788 HInstruction* instruction,
4789 Location out,
4790 uint32_t offset,
4791 Location maybe_temp,
4792 ReadBarrierOption read_barrier_option) {
4793 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4794 if (read_barrier_option == kWithReadBarrier) {
4795 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004796 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4797 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4798 }
Alexey Frunze15958152017-02-09 19:08:30 -08004799 if (kUseBakerReadBarrier) {
4800 // Load with fast path based Baker's read barrier.
4801 // /* HeapReference<Object> */ out = *(out + offset)
4802 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4803 out,
4804 out_reg,
4805 offset,
4806 maybe_temp,
4807 /* needs_null_check */ false);
4808 } else {
4809 // Load with slow path based read barrier.
4810 // Save the value of `out` into `maybe_temp` before overwriting it
4811 // in the following move operation, as we will need it for the
4812 // read barrier below.
4813 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4814 // /* HeapReference<Object> */ out = *(out + offset)
4815 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4816 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4817 }
4818 } else {
4819 // Plain load with no read barrier.
4820 // /* HeapReference<Object> */ out = *(out + offset)
4821 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4822 __ MaybeUnpoisonHeapReference(out_reg);
4823 }
4824}
4825
4826void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4827 HInstruction* instruction,
4828 Location out,
4829 Location obj,
4830 uint32_t offset,
4831 Location maybe_temp,
4832 ReadBarrierOption read_barrier_option) {
4833 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4834 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4835 if (read_barrier_option == kWithReadBarrier) {
4836 CHECK(kEmitCompilerReadBarrier);
4837 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004838 if (!kBakerReadBarrierThunksEnableForFields) {
4839 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4840 }
Alexey Frunze15958152017-02-09 19:08:30 -08004841 // Load with fast path based Baker's read barrier.
4842 // /* HeapReference<Object> */ out = *(obj + offset)
4843 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4844 out,
4845 obj_reg,
4846 offset,
4847 maybe_temp,
4848 /* needs_null_check */ false);
4849 } else {
4850 // Load with slow path based read barrier.
4851 // /* HeapReference<Object> */ out = *(obj + offset)
4852 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4853 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4854 }
4855 } else {
4856 // Plain load with no read barrier.
4857 // /* HeapReference<Object> */ out = *(obj + offset)
4858 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4859 __ MaybeUnpoisonHeapReference(out_reg);
4860 }
4861}
4862
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004863static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4864 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4865 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4866 return reg - V0;
4867 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4868 return 13 + (reg - S2);
4869 } else if (reg == S8) { // One more.
4870 return 19;
4871 }
4872 LOG(FATAL) << "Unexpected register " << reg;
4873 UNREACHABLE();
4874}
4875
4876static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4877 int num = GetBakerMarkThunkNumber(reg) +
4878 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4879 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4880}
4881
4882static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4883 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4884 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4885}
4886
4887void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4888 Location root,
4889 GpuRegister obj,
4890 uint32_t offset,
4891 ReadBarrierOption read_barrier_option,
4892 Mips64Label* label_low) {
4893 if (label_low != nullptr) {
4894 DCHECK_EQ(offset, 0x5678u);
4895 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004896 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004897 if (read_barrier_option == kWithReadBarrier) {
4898 DCHECK(kEmitCompilerReadBarrier);
4899 if (kUseBakerReadBarrier) {
4900 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4901 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004902 if (kBakerReadBarrierThunksEnableForGcRoots) {
4903 // Note that we do not actually check the value of `GetIsGcMarking()`
4904 // to decide whether to mark the loaded GC root or not. Instead, we
4905 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4906 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4907 // vice versa.
4908 //
4909 // We use thunks for the slow path. That thunk checks the reference
4910 // and jumps to the entrypoint if needed.
4911 //
4912 // temp = Thread::Current()->pReadBarrierMarkReg00
4913 // // AKA &art_quick_read_barrier_mark_introspection.
4914 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4915 // if (temp != nullptr) {
4916 // temp = &gc_root_thunk<root_reg>
4917 // root = temp(root)
4918 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004919
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004920 const int32_t entry_point_offset =
4921 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4922 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4923 int16_t offset_low = Low16Bits(offset);
4924 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
4925 // extension in lwu.
4926 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4927 GpuRegister base = short_offset ? obj : TMP;
4928 // Loading the entrypoint does not require a load acquire since it is only changed when
4929 // threads are suspended or running a checkpoint.
4930 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
4931 if (!short_offset) {
4932 DCHECK(!label_low);
4933 __ Daui(base, obj, offset_high);
4934 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07004935 Mips64Label skip_call;
4936 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004937 if (label_low != nullptr) {
4938 DCHECK(short_offset);
4939 __ Bind(label_low);
4940 }
4941 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4942 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
4943 // in delay slot.
4944 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07004945 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004946 } else {
4947 // Note that we do not actually check the value of `GetIsGcMarking()`
4948 // to decide whether to mark the loaded GC root or not. Instead, we
4949 // load into `temp` (T9) the read barrier mark entry point corresponding
4950 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
4951 // is false, and vice versa.
4952 //
4953 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4954 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4955 // if (temp != null) {
4956 // root = temp(root)
4957 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004958
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004959 if (label_low != nullptr) {
4960 __ Bind(label_low);
4961 }
4962 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4963 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4964 static_assert(
4965 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4966 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4967 "have different sizes.");
4968 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4969 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4970 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08004971
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004972 // Slow path marking the GC root `root`.
4973 Location temp = Location::RegisterLocation(T9);
4974 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004975 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004976 instruction,
4977 root,
4978 /*entrypoint*/ temp);
4979 codegen_->AddSlowPath(slow_path);
4980
4981 const int32_t entry_point_offset =
4982 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4983 // Loading the entrypoint does not require a load acquire since it is only changed when
4984 // threads are suspended or running a checkpoint.
4985 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4986 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4987 __ Bind(slow_path->GetExitLabel());
4988 }
Alexey Frunze15958152017-02-09 19:08:30 -08004989 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004990 if (label_low != nullptr) {
4991 __ Bind(label_low);
4992 }
Alexey Frunze15958152017-02-09 19:08:30 -08004993 // GC root loaded through a slow path for read barriers other
4994 // than Baker's.
4995 // /* GcRoot<mirror::Object>* */ root = obj + offset
4996 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4997 // /* mirror::Object* */ root = root->Read()
4998 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4999 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005000 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005001 if (label_low != nullptr) {
5002 __ Bind(label_low);
5003 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005004 // Plain GC root load with no read barrier.
5005 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5006 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5007 // Note that GC roots are not affected by heap poisoning, thus we
5008 // do not have to unpoison `root_reg` here.
5009 }
5010}
5011
Alexey Frunze15958152017-02-09 19:08:30 -08005012void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5013 Location ref,
5014 GpuRegister obj,
5015 uint32_t offset,
5016 Location temp,
5017 bool needs_null_check) {
5018 DCHECK(kEmitCompilerReadBarrier);
5019 DCHECK(kUseBakerReadBarrier);
5020
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005021 if (kBakerReadBarrierThunksEnableForFields) {
5022 // Note that we do not actually check the value of `GetIsGcMarking()`
5023 // to decide whether to mark the loaded reference or not. Instead, we
5024 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5025 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5026 // vice versa.
5027 //
5028 // We use thunks for the slow path. That thunk checks the reference
5029 // and jumps to the entrypoint if needed. If the holder is not gray,
5030 // it issues a load-load memory barrier and returns to the original
5031 // reference load.
5032 //
5033 // temp = Thread::Current()->pReadBarrierMarkReg00
5034 // // AKA &art_quick_read_barrier_mark_introspection.
5035 // if (temp != nullptr) {
5036 // temp = &field_array_thunk<holder_reg>
5037 // temp()
5038 // }
5039 // not_gray_return_address:
5040 // // If the offset is too large to fit into the lw instruction, we
5041 // // use an adjusted base register (TMP) here. This register
5042 // // receives bits 16 ... 31 of the offset before the thunk invocation
5043 // // and the thunk benefits from it.
5044 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5045 // gray_return_address:
5046
5047 DCHECK(temp.IsInvalid());
5048 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5049 const int32_t entry_point_offset =
5050 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5051 // There may have or may have not been a null check if the field offset is smaller than
5052 // the page size.
5053 // There must've been a null check in case it's actually a load from an array.
5054 // We will, however, perform an explicit null check in the thunk as it's easier to
5055 // do it than not.
5056 if (instruction->IsArrayGet()) {
5057 DCHECK(!needs_null_check);
5058 }
5059 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5060 // Loading the entrypoint does not require a load acquire since it is only changed when
5061 // threads are suspended or running a checkpoint.
5062 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5063 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005064 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005065 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005066 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005067 __ Nop(); // In forbidden slot.
5068 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005069 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005070 // /* HeapReference<Object> */ ref = *(obj + offset)
5071 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5072 } else {
5073 int16_t offset_low = Low16Bits(offset);
5074 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005075 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005076 __ Daui(TMP, obj, offset_high); // In delay slot.
5077 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005078 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005079 // /* HeapReference<Object> */ ref = *(obj + offset)
5080 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5081 }
5082 if (needs_null_check) {
5083 MaybeRecordImplicitNullCheck(instruction);
5084 }
5085 __ MaybeUnpoisonHeapReference(ref_reg);
5086 return;
5087 }
5088
Alexey Frunze15958152017-02-09 19:08:30 -08005089 // /* HeapReference<Object> */ ref = *(obj + offset)
5090 Location no_index = Location::NoLocation();
5091 ScaleFactor no_scale_factor = TIMES_1;
5092 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5093 ref,
5094 obj,
5095 offset,
5096 no_index,
5097 no_scale_factor,
5098 temp,
5099 needs_null_check);
5100}
5101
5102void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5103 Location ref,
5104 GpuRegister obj,
5105 uint32_t data_offset,
5106 Location index,
5107 Location temp,
5108 bool needs_null_check) {
5109 DCHECK(kEmitCompilerReadBarrier);
5110 DCHECK(kUseBakerReadBarrier);
5111
5112 static_assert(
5113 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5114 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005115 ScaleFactor scale_factor = TIMES_4;
5116
5117 if (kBakerReadBarrierThunksEnableForArrays) {
5118 // Note that we do not actually check the value of `GetIsGcMarking()`
5119 // to decide whether to mark the loaded reference or not. Instead, we
5120 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5121 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5122 // vice versa.
5123 //
5124 // We use thunks for the slow path. That thunk checks the reference
5125 // and jumps to the entrypoint if needed. If the holder is not gray,
5126 // it issues a load-load memory barrier and returns to the original
5127 // reference load.
5128 //
5129 // temp = Thread::Current()->pReadBarrierMarkReg00
5130 // // AKA &art_quick_read_barrier_mark_introspection.
5131 // if (temp != nullptr) {
5132 // temp = &field_array_thunk<holder_reg>
5133 // temp()
5134 // }
5135 // not_gray_return_address:
5136 // // The element address is pre-calculated in the TMP register before the
5137 // // thunk invocation and the thunk benefits from it.
5138 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5139 // gray_return_address:
5140
5141 DCHECK(temp.IsInvalid());
5142 DCHECK(index.IsValid());
5143 const int32_t entry_point_offset =
5144 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5145 // We will not do the explicit null check in the thunk as some form of a null check
5146 // must've been done earlier.
5147 DCHECK(!needs_null_check);
5148 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5149 // Loading the entrypoint does not require a load acquire since it is only changed when
5150 // threads are suspended or running a checkpoint.
5151 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005152 Mips64Label skip_call;
5153 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005154 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5155 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5156 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5157 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005158 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005159 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5160 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5161 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5162 __ MaybeUnpoisonHeapReference(ref_reg);
5163 return;
5164 }
5165
Alexey Frunze15958152017-02-09 19:08:30 -08005166 // /* HeapReference<Object> */ ref =
5167 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005168 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5169 ref,
5170 obj,
5171 data_offset,
5172 index,
5173 scale_factor,
5174 temp,
5175 needs_null_check);
5176}
5177
5178void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5179 Location ref,
5180 GpuRegister obj,
5181 uint32_t offset,
5182 Location index,
5183 ScaleFactor scale_factor,
5184 Location temp,
5185 bool needs_null_check,
5186 bool always_update_field) {
5187 DCHECK(kEmitCompilerReadBarrier);
5188 DCHECK(kUseBakerReadBarrier);
5189
5190 // In slow path based read barriers, the read barrier call is
5191 // inserted after the original load. However, in fast path based
5192 // Baker's read barriers, we need to perform the load of
5193 // mirror::Object::monitor_ *before* the original reference load.
5194 // This load-load ordering is required by the read barrier.
5195 // The fast path/slow path (for Baker's algorithm) should look like:
5196 //
5197 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5198 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5199 // HeapReference<Object> ref = *src; // Original reference load.
5200 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5201 // if (is_gray) {
5202 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5203 // }
5204 //
5205 // Note: the original implementation in ReadBarrier::Barrier is
5206 // slightly more complex as it performs additional checks that we do
5207 // not do here for performance reasons.
5208
5209 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5210 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5211 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5212
5213 // /* int32_t */ monitor = obj->monitor_
5214 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5215 if (needs_null_check) {
5216 MaybeRecordImplicitNullCheck(instruction);
5217 }
5218 // /* LockWord */ lock_word = LockWord(monitor)
5219 static_assert(sizeof(LockWord) == sizeof(int32_t),
5220 "art::LockWord and int32_t have different sizes.");
5221
5222 __ Sync(0); // Barrier to prevent load-load reordering.
5223
5224 // The actual reference load.
5225 if (index.IsValid()) {
5226 // Load types involving an "index": ArrayGet,
5227 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5228 // intrinsics.
5229 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5230 if (index.IsConstant()) {
5231 size_t computed_offset =
5232 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5233 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5234 } else {
5235 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005236 if (scale_factor == TIMES_1) {
5237 __ Daddu(TMP, index_reg, obj);
5238 } else {
5239 __ Dlsa(TMP, index_reg, obj, scale_factor);
5240 }
Alexey Frunze15958152017-02-09 19:08:30 -08005241 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5242 }
5243 } else {
5244 // /* HeapReference<Object> */ ref = *(obj + offset)
5245 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5246 }
5247
5248 // Object* ref = ref_addr->AsMirrorPtr()
5249 __ MaybeUnpoisonHeapReference(ref_reg);
5250
5251 // Slow path marking the object `ref` when it is gray.
5252 SlowPathCodeMIPS64* slow_path;
5253 if (always_update_field) {
5254 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5255 // of the form `obj + field_offset`, where `obj` is a register and
5256 // `field_offset` is a register. Thus `offset` and `scale_factor`
5257 // above are expected to be null in this code path.
5258 DCHECK_EQ(offset, 0u);
5259 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005260 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005261 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5262 ref,
5263 obj,
5264 /* field_offset */ index,
5265 temp_reg);
5266 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005267 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005268 }
5269 AddSlowPath(slow_path);
5270
5271 // if (rb_state == ReadBarrier::GrayState())
5272 // ref = ReadBarrier::Mark(ref);
5273 // Given the numeric representation, it's enough to check the low bit of the
5274 // rb_state. We do that by shifting the bit into the sign bit (31) and
5275 // performing a branch on less than zero.
5276 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5277 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5278 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5279 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5280 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5281 __ Bind(slow_path->GetExitLabel());
5282}
5283
5284void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5285 Location out,
5286 Location ref,
5287 Location obj,
5288 uint32_t offset,
5289 Location index) {
5290 DCHECK(kEmitCompilerReadBarrier);
5291
5292 // Insert a slow path based read barrier *after* the reference load.
5293 //
5294 // If heap poisoning is enabled, the unpoisoning of the loaded
5295 // reference will be carried out by the runtime within the slow
5296 // path.
5297 //
5298 // Note that `ref` currently does not get unpoisoned (when heap
5299 // poisoning is enabled), which is alright as the `ref` argument is
5300 // not used by the artReadBarrierSlow entry point.
5301 //
5302 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005303 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005304 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5305 AddSlowPath(slow_path);
5306
5307 __ Bc(slow_path->GetEntryLabel());
5308 __ Bind(slow_path->GetExitLabel());
5309}
5310
5311void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5312 Location out,
5313 Location ref,
5314 Location obj,
5315 uint32_t offset,
5316 Location index) {
5317 if (kEmitCompilerReadBarrier) {
5318 // Baker's read barriers shall be handled by the fast path
5319 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5320 DCHECK(!kUseBakerReadBarrier);
5321 // If heap poisoning is enabled, unpoisoning will be taken care of
5322 // by the runtime within the slow path.
5323 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5324 } else if (kPoisonHeapReferences) {
5325 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5326 }
5327}
5328
5329void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5330 Location out,
5331 Location root) {
5332 DCHECK(kEmitCompilerReadBarrier);
5333
5334 // Insert a slow path based read barrier *after* the GC root load.
5335 //
5336 // Note that GC roots are not affected by heap poisoning, so we do
5337 // not need to do anything special for this here.
5338 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005339 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005340 AddSlowPath(slow_path);
5341
5342 __ Bc(slow_path->GetEntryLabel());
5343 __ Bind(slow_path->GetExitLabel());
5344}
5345
Alexey Frunze4dda3372015-06-01 18:31:49 -07005346void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005347 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5348 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005349 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005350 switch (type_check_kind) {
5351 case TypeCheckKind::kExactCheck:
5352 case TypeCheckKind::kAbstractClassCheck:
5353 case TypeCheckKind::kClassHierarchyCheck:
5354 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005355 call_kind =
5356 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005357 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005358 break;
5359 case TypeCheckKind::kArrayCheck:
5360 case TypeCheckKind::kUnresolvedCheck:
5361 case TypeCheckKind::kInterfaceCheck:
5362 call_kind = LocationSummary::kCallOnSlowPath;
5363 break;
5364 }
5365
Vladimir Markoca6fff82017-10-03 14:49:14 +01005366 LocationSummary* locations =
5367 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005368 if (baker_read_barrier_slow_path) {
5369 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5370 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005371 locations->SetInAt(0, Location::RequiresRegister());
5372 locations->SetInAt(1, Location::RequiresRegister());
5373 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005374 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005375 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005376 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005377}
5378
5379void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005380 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005381 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005382 Location obj_loc = locations->InAt(0);
5383 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005384 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005385 Location out_loc = locations->Out();
5386 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5387 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5388 DCHECK_LE(num_temps, 1u);
5389 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005390 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5391 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5392 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5393 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005394 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005395 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005396
5397 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005398 // Avoid this check if we know `obj` is not null.
5399 if (instruction->MustDoNullCheck()) {
5400 __ Move(out, ZERO);
5401 __ Beqzc(obj, &done);
5402 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005403
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005404 switch (type_check_kind) {
5405 case TypeCheckKind::kExactCheck: {
5406 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005407 GenerateReferenceLoadTwoRegisters(instruction,
5408 out_loc,
5409 obj_loc,
5410 class_offset,
5411 maybe_temp_loc,
5412 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005413 // Classes must be equal for the instanceof to succeed.
5414 __ Xor(out, out, cls);
5415 __ Sltiu(out, out, 1);
5416 break;
5417 }
5418
5419 case TypeCheckKind::kAbstractClassCheck: {
5420 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005421 GenerateReferenceLoadTwoRegisters(instruction,
5422 out_loc,
5423 obj_loc,
5424 class_offset,
5425 maybe_temp_loc,
5426 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005427 // If the class is abstract, we eagerly fetch the super class of the
5428 // object to avoid doing a comparison we know will fail.
5429 Mips64Label loop;
5430 __ Bind(&loop);
5431 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005432 GenerateReferenceLoadOneRegister(instruction,
5433 out_loc,
5434 super_offset,
5435 maybe_temp_loc,
5436 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005437 // If `out` is null, we use it for the result, and jump to `done`.
5438 __ Beqzc(out, &done);
5439 __ Bnec(out, cls, &loop);
5440 __ LoadConst32(out, 1);
5441 break;
5442 }
5443
5444 case TypeCheckKind::kClassHierarchyCheck: {
5445 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005446 GenerateReferenceLoadTwoRegisters(instruction,
5447 out_loc,
5448 obj_loc,
5449 class_offset,
5450 maybe_temp_loc,
5451 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005452 // Walk over the class hierarchy to find a match.
5453 Mips64Label loop, success;
5454 __ Bind(&loop);
5455 __ Beqc(out, cls, &success);
5456 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005457 GenerateReferenceLoadOneRegister(instruction,
5458 out_loc,
5459 super_offset,
5460 maybe_temp_loc,
5461 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005462 __ Bnezc(out, &loop);
5463 // If `out` is null, we use it for the result, and jump to `done`.
5464 __ Bc(&done);
5465 __ Bind(&success);
5466 __ LoadConst32(out, 1);
5467 break;
5468 }
5469
5470 case TypeCheckKind::kArrayObjectCheck: {
5471 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005472 GenerateReferenceLoadTwoRegisters(instruction,
5473 out_loc,
5474 obj_loc,
5475 class_offset,
5476 maybe_temp_loc,
5477 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005478 // Do an exact check.
5479 Mips64Label success;
5480 __ Beqc(out, cls, &success);
5481 // Otherwise, we need to check that the object's class is a non-primitive array.
5482 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005483 GenerateReferenceLoadOneRegister(instruction,
5484 out_loc,
5485 component_offset,
5486 maybe_temp_loc,
5487 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005488 // If `out` is null, we use it for the result, and jump to `done`.
5489 __ Beqzc(out, &done);
5490 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5491 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5492 __ Sltiu(out, out, 1);
5493 __ Bc(&done);
5494 __ Bind(&success);
5495 __ LoadConst32(out, 1);
5496 break;
5497 }
5498
5499 case TypeCheckKind::kArrayCheck: {
5500 // No read barrier since the slow path will retry upon failure.
5501 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005502 GenerateReferenceLoadTwoRegisters(instruction,
5503 out_loc,
5504 obj_loc,
5505 class_offset,
5506 maybe_temp_loc,
5507 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005508 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005509 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5510 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005511 codegen_->AddSlowPath(slow_path);
5512 __ Bnec(out, cls, slow_path->GetEntryLabel());
5513 __ LoadConst32(out, 1);
5514 break;
5515 }
5516
5517 case TypeCheckKind::kUnresolvedCheck:
5518 case TypeCheckKind::kInterfaceCheck: {
5519 // Note that we indeed only call on slow path, but we always go
5520 // into the slow path for the unresolved and interface check
5521 // cases.
5522 //
5523 // We cannot directly call the InstanceofNonTrivial runtime
5524 // entry point without resorting to a type checking slow path
5525 // here (i.e. by calling InvokeRuntime directly), as it would
5526 // require to assign fixed registers for the inputs of this
5527 // HInstanceOf instruction (following the runtime calling
5528 // convention), which might be cluttered by the potential first
5529 // read barrier emission at the beginning of this method.
5530 //
5531 // TODO: Introduce a new runtime entry point taking the object
5532 // to test (instead of its class) as argument, and let it deal
5533 // with the read barrier issues. This will let us refactor this
5534 // case of the `switch` code as it was previously (with a direct
5535 // call to the runtime not using a type checking slow path).
5536 // This should also be beneficial for the other cases above.
5537 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005538 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5539 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005540 codegen_->AddSlowPath(slow_path);
5541 __ Bc(slow_path->GetEntryLabel());
5542 break;
5543 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005544 }
5545
5546 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005547
5548 if (slow_path != nullptr) {
5549 __ Bind(slow_path->GetExitLabel());
5550 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005551}
5552
5553void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005554 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005555 locations->SetOut(Location::ConstantLocation(constant));
5556}
5557
5558void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5559 // Will be generated at use site.
5560}
5561
5562void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005563 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005564 locations->SetOut(Location::ConstantLocation(constant));
5565}
5566
5567void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5568 // Will be generated at use site.
5569}
5570
Calin Juravle175dc732015-08-25 15:42:32 +01005571void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5572 // The trampoline uses the same calling convention as dex calling conventions,
5573 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5574 // the method_idx.
5575 HandleInvoke(invoke);
5576}
5577
5578void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5579 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5580}
5581
Alexey Frunze4dda3372015-06-01 18:31:49 -07005582void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5583 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5584 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5585}
5586
5587void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5588 HandleInvoke(invoke);
5589 // The register T0 is required to be used for the hidden argument in
5590 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5591 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5592}
5593
5594void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5595 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5596 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005597 Location receiver = invoke->GetLocations()->InAt(0);
5598 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005599 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005600
5601 // Set the hidden argument.
5602 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5603 invoke->GetDexMethodIndex());
5604
5605 // temp = object->GetClass();
5606 if (receiver.IsStackSlot()) {
5607 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5608 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5609 } else {
5610 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5611 }
5612 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005613 // Instead of simply (possibly) unpoisoning `temp` here, we should
5614 // emit a read barrier for the previous class reference load.
5615 // However this is not required in practice, as this is an
5616 // intermediate/temporary reference and because the current
5617 // concurrent copying collector keeps the from-space memory
5618 // intact/accessible until the end of the marking phase (the
5619 // concurrent copying collector may not in the future).
5620 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005621 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5622 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5623 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005624 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005625 // temp = temp->GetImtEntryAt(method_offset);
5626 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5627 // T9 = temp->GetEntryPoint();
5628 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5629 // T9();
5630 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005631 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005632 DCHECK(!codegen_->IsLeafMethod());
5633 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5634}
5635
5636void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005637 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5638 if (intrinsic.TryDispatch(invoke)) {
5639 return;
5640 }
5641
Alexey Frunze4dda3372015-06-01 18:31:49 -07005642 HandleInvoke(invoke);
5643}
5644
5645void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005646 // Explicit clinit checks triggered by static invokes must have been pruned by
5647 // art::PrepareForRegisterAllocation.
5648 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005649
Chris Larsen3039e382015-08-26 07:54:08 -07005650 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5651 if (intrinsic.TryDispatch(invoke)) {
5652 return;
5653 }
5654
Alexey Frunze4dda3372015-06-01 18:31:49 -07005655 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005656}
5657
Orion Hodsonac141392017-01-13 11:53:47 +00005658void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5659 HandleInvoke(invoke);
5660}
5661
5662void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5663 codegen_->GenerateInvokePolymorphicCall(invoke);
5664}
5665
Chris Larsen3039e382015-08-26 07:54:08 -07005666static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005667 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005668 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5669 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005670 return true;
5671 }
5672 return false;
5673}
5674
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005675HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005676 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005677 bool fallback_load = false;
5678 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005679 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005680 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005681 case HLoadString::LoadKind::kBssEntry:
5682 DCHECK(!Runtime::Current()->UseJitCompilation());
5683 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005684 case HLoadString::LoadKind::kJitTableAddress:
5685 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005686 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005687 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005688 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005689 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005690 }
5691 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005692 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005693 }
5694 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005695}
5696
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005697HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5698 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005699 bool fallback_load = false;
5700 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005701 case HLoadClass::LoadKind::kInvalid:
5702 LOG(FATAL) << "UNREACHABLE";
5703 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005704 case HLoadClass::LoadKind::kReferrersClass:
5705 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005706 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005707 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005708 case HLoadClass::LoadKind::kBssEntry:
5709 DCHECK(!Runtime::Current()->UseJitCompilation());
5710 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005711 case HLoadClass::LoadKind::kJitTableAddress:
5712 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005713 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005714 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005715 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005716 break;
5717 }
5718 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005719 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005720 }
5721 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005722}
5723
Vladimir Markodc151b22015-10-15 18:02:30 +01005724HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5725 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005726 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005727 // On MIPS64 we support all dispatch types.
5728 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005729}
5730
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005731void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5732 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005733 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005734 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005735 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5736 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5737
Alexey Frunze19f6c692016-11-30 19:19:55 -08005738 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005739 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005740 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005741 uint32_t offset =
5742 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005743 __ LoadFromOffset(kLoadDoubleword,
5744 temp.AsRegister<GpuRegister>(),
5745 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005746 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005747 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005748 }
Vladimir Marko58155012015-08-19 12:49:41 +00005749 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005750 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005751 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005752 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5753 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005754 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005755 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005756 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5757 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5758 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005759 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5760 break;
5761 }
Vladimir Marko58155012015-08-19 12:49:41 +00005762 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005763 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5764 kLoadDoubleword,
5765 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005766 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005767 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005768 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005769 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005770 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5771 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5772 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005773 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5774 break;
5775 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005776 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5777 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5778 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005779 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005780 }
5781
Alexey Frunze19f6c692016-11-30 19:19:55 -08005782 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005783 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005784 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005785 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005786 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5787 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5788 __ LoadFromOffset(kLoadDoubleword,
5789 T9,
5790 callee_method.AsRegister<GpuRegister>(),
5791 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005792 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005793 // T9()
5794 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005795 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005796 break;
5797 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005798 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5799
Alexey Frunze4dda3372015-06-01 18:31:49 -07005800 DCHECK(!IsLeafMethod());
5801}
5802
5803void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005804 // Explicit clinit checks triggered by static invokes must have been pruned by
5805 // art::PrepareForRegisterAllocation.
5806 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005807
5808 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5809 return;
5810 }
5811
5812 LocationSummary* locations = invoke->GetLocations();
5813 codegen_->GenerateStaticOrDirectCall(invoke,
5814 locations->HasTemps()
5815 ? locations->GetTemp(0)
5816 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005817}
5818
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005819void CodeGeneratorMIPS64::GenerateVirtualCall(
5820 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005821 // Use the calling convention instead of the location of the receiver, as
5822 // intrinsics may have put the receiver in a different register. In the intrinsics
5823 // slow path, the arguments have been moved to the right place, so here we are
5824 // guaranteed that the receiver is the first register of the calling convention.
5825 InvokeDexCallingConvention calling_convention;
5826 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5827
Alexey Frunze53afca12015-11-05 16:34:23 -08005828 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005829 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5830 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5831 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005832 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005833
5834 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005835 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005836 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005837 // Instead of simply (possibly) unpoisoning `temp` here, we should
5838 // emit a read barrier for the previous class reference load.
5839 // However this is not required in practice, as this is an
5840 // intermediate/temporary reference and because the current
5841 // concurrent copying collector keeps the from-space memory
5842 // intact/accessible until the end of the marking phase (the
5843 // concurrent copying collector may not in the future).
5844 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005845 // temp = temp->GetMethodAt(method_offset);
5846 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5847 // T9 = temp->GetEntryPoint();
5848 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5849 // T9();
5850 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005851 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005852 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005853}
5854
5855void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5856 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5857 return;
5858 }
5859
5860 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005861 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005862}
5863
5864void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005865 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005866 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005867 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005868 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5869 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005870 return;
5871 }
Vladimir Marko41559982017-01-06 14:04:23 +00005872 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005873
Alexey Frunze15958152017-02-09 19:08:30 -08005874 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5875 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005876 ? LocationSummary::kCallOnSlowPath
5877 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005878 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005879 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5880 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5881 }
Vladimir Marko41559982017-01-06 14:04:23 +00005882 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005883 locations->SetInAt(0, Location::RequiresRegister());
5884 }
5885 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005886 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5887 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5888 // Rely on the type resolution or initialization and marking to save everything we need.
5889 RegisterSet caller_saves = RegisterSet::Empty();
5890 InvokeRuntimeCallingConvention calling_convention;
5891 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5892 locations->SetCustomSlowPathCallerSaves(caller_saves);
5893 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005894 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005895 }
5896 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005897}
5898
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005899// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5900// move.
5901void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005902 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005903 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005904 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005905 return;
5906 }
Vladimir Marko41559982017-01-06 14:04:23 +00005907 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005908
Vladimir Marko41559982017-01-06 14:04:23 +00005909 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005910 Location out_loc = locations->Out();
5911 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5912 GpuRegister current_method_reg = ZERO;
5913 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005914 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005915 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5916 }
5917
Alexey Frunze15958152017-02-09 19:08:30 -08005918 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5919 ? kWithoutReadBarrier
5920 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005921 bool generate_null_check = false;
5922 switch (load_kind) {
5923 case HLoadClass::LoadKind::kReferrersClass:
5924 DCHECK(!cls->CanCallRuntime());
5925 DCHECK(!cls->MustGenerateClinitCheck());
5926 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5927 GenerateGcRootFieldLoad(cls,
5928 out_loc,
5929 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005930 ArtMethod::DeclaringClassOffset().Int32Value(),
5931 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005932 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005933 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005934 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005935 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005936 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08005937 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005938 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5939 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
5940 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005941 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5942 break;
5943 }
5944 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005945 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005946 uint32_t address = dchecked_integral_cast<uint32_t>(
5947 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5948 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005949 __ LoadLiteral(out,
5950 kLoadUnsignedWord,
5951 codegen_->DeduplicateBootImageAddressLiteral(address));
5952 break;
5953 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005954 case HLoadClass::LoadKind::kBootImageClassTable: {
5955 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5956 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
5957 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5958 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5959 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
5960 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
5961 __ Lwu(out, AT, /* placeholder */ 0x5678);
5962 // Extract the reference from the slot data, i.e. clear the hash bits.
5963 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
5964 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
5965 if (masked_hash != 0) {
5966 __ Daddiu(out, out, -masked_hash);
5967 }
5968 break;
5969 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005970 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00005971 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
5972 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005973 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5974 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00005975 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005976 GenerateGcRootFieldLoad(cls,
5977 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00005978 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005979 /* placeholder */ 0x5678,
5980 read_barrier_option,
5981 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005982 generate_null_check = true;
5983 break;
5984 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005985 case HLoadClass::LoadKind::kJitTableAddress:
5986 __ LoadLiteral(out,
5987 kLoadUnsignedWord,
5988 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5989 cls->GetTypeIndex(),
5990 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005991 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005992 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005993 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005994 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005995 LOG(FATAL) << "UNREACHABLE";
5996 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005997 }
5998
5999 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6000 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006001 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006002 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006003 codegen_->AddSlowPath(slow_path);
6004 if (generate_null_check) {
6005 __ Beqzc(out, slow_path->GetEntryLabel());
6006 }
6007 if (cls->MustGenerateClinitCheck()) {
6008 GenerateClassInitializationCheck(slow_path, out);
6009 } else {
6010 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006011 }
6012 }
6013}
6014
David Brazdilcb1c0552015-08-04 16:22:25 +01006015static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006016 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006017}
6018
Alexey Frunze4dda3372015-06-01 18:31:49 -07006019void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6020 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006021 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006022 locations->SetOut(Location::RequiresRegister());
6023}
6024
6025void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6026 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006027 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6028}
6029
6030void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006031 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006032}
6033
6034void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6035 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006036}
6037
Alexey Frunze4dda3372015-06-01 18:31:49 -07006038void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006039 HLoadString::LoadKind load_kind = load->GetLoadKind();
6040 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006041 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006042 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006043 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006044 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006045 } else {
6046 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006047 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6048 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6049 // Rely on the pResolveString and marking to save everything we need.
6050 RegisterSet caller_saves = RegisterSet::Empty();
6051 InvokeRuntimeCallingConvention calling_convention;
6052 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6053 locations->SetCustomSlowPathCallerSaves(caller_saves);
6054 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006055 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006056 }
6057 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006058 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006059}
6060
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006061// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6062// move.
6063void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006064 HLoadString::LoadKind load_kind = load->GetLoadKind();
6065 LocationSummary* locations = load->GetLocations();
6066 Location out_loc = locations->Out();
6067 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6068
6069 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006070 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6071 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006072 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006073 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006074 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6075 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6076 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006077 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006078 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006079 }
6080 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006081 uint32_t address = dchecked_integral_cast<uint32_t>(
6082 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6083 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006084 __ LoadLiteral(out,
6085 kLoadUnsignedWord,
6086 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006087 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006088 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006089 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006090 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006091 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006092 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006093 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6094 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006095 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6096 __ Lwu(out, AT, /* placeholder */ 0x5678);
6097 return;
6098 }
6099 case HLoadString::LoadKind::kBssEntry: {
6100 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6101 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6102 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6103 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6104 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006105 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006106 GenerateGcRootFieldLoad(load,
6107 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006108 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006109 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006110 kCompilerReadBarrierOption,
6111 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006112 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006113 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006114 codegen_->AddSlowPath(slow_path);
6115 __ Beqzc(out, slow_path->GetEntryLabel());
6116 __ Bind(slow_path->GetExitLabel());
6117 return;
6118 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006119 case HLoadString::LoadKind::kJitTableAddress:
6120 __ LoadLiteral(out,
6121 kLoadUnsignedWord,
6122 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6123 load->GetStringIndex(),
6124 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006125 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006126 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006127 default:
6128 break;
6129 }
6130
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006131 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006132 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006133 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006134 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006135 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6136 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6137 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006138}
6139
Alexey Frunze4dda3372015-06-01 18:31:49 -07006140void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006141 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006142 locations->SetOut(Location::ConstantLocation(constant));
6143}
6144
6145void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6146 // Will be generated at use site.
6147}
6148
6149void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006150 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6151 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006152 InvokeRuntimeCallingConvention calling_convention;
6153 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6154}
6155
6156void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006157 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006158 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006159 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006160 if (instruction->IsEnter()) {
6161 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6162 } else {
6163 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6164 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006165}
6166
6167void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6168 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006169 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006170 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006171 case DataType::Type::kInt32:
6172 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006173 locations->SetInAt(0, Location::RequiresRegister());
6174 locations->SetInAt(1, Location::RequiresRegister());
6175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6176 break;
6177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006178 case DataType::Type::kFloat32:
6179 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006180 locations->SetInAt(0, Location::RequiresFpuRegister());
6181 locations->SetInAt(1, Location::RequiresFpuRegister());
6182 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6183 break;
6184
6185 default:
6186 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6187 }
6188}
6189
6190void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006191 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006192 LocationSummary* locations = instruction->GetLocations();
6193
6194 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006195 case DataType::Type::kInt32:
6196 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006197 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6198 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6199 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006200 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006201 __ MulR6(dst, lhs, rhs);
6202 else
6203 __ Dmul(dst, lhs, rhs);
6204 break;
6205 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 case DataType::Type::kFloat32:
6207 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006208 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6209 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6210 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006211 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006212 __ MulS(dst, lhs, rhs);
6213 else
6214 __ MulD(dst, lhs, rhs);
6215 break;
6216 }
6217 default:
6218 LOG(FATAL) << "Unexpected mul type " << type;
6219 }
6220}
6221
6222void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6223 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006224 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006225 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 case DataType::Type::kInt32:
6227 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006228 locations->SetInAt(0, Location::RequiresRegister());
6229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6230 break;
6231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006232 case DataType::Type::kFloat32:
6233 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006234 locations->SetInAt(0, Location::RequiresFpuRegister());
6235 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6236 break;
6237
6238 default:
6239 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6240 }
6241}
6242
6243void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006244 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006245 LocationSummary* locations = instruction->GetLocations();
6246
6247 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006248 case DataType::Type::kInt32:
6249 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006250 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6251 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006252 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006253 __ Subu(dst, ZERO, src);
6254 else
6255 __ Dsubu(dst, ZERO, src);
6256 break;
6257 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006258 case DataType::Type::kFloat32:
6259 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006260 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6261 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006262 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006263 __ NegS(dst, src);
6264 else
6265 __ NegD(dst, src);
6266 break;
6267 }
6268 default:
6269 LOG(FATAL) << "Unexpected neg type " << type;
6270 }
6271}
6272
6273void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006274 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6275 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006276 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006277 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006278 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6279 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006280}
6281
6282void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006283 // Note: if heap poisoning is enabled, the entry point takes care
6284 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006285 QuickEntrypointEnum entrypoint =
6286 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6287 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006288 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006289 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006290}
6291
6292void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006293 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6294 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006295 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006296 if (instruction->IsStringAlloc()) {
6297 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6298 } else {
6299 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006300 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006302}
6303
6304void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006305 // Note: if heap poisoning is enabled, the entry point takes care
6306 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006307 if (instruction->IsStringAlloc()) {
6308 // String is allocated through StringFactory. Call NewEmptyString entry point.
6309 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006310 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006311 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006312 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6313 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6314 __ Jalr(T9);
6315 __ Nop();
6316 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6317 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006318 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006319 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006320 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006321}
6322
6323void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006324 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006325 locations->SetInAt(0, Location::RequiresRegister());
6326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6327}
6328
6329void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006330 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006331 LocationSummary* locations = instruction->GetLocations();
6332
6333 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006334 case DataType::Type::kInt32:
6335 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006336 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6337 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6338 __ Nor(dst, src, ZERO);
6339 break;
6340 }
6341
6342 default:
6343 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6344 }
6345}
6346
6347void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006348 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006349 locations->SetInAt(0, Location::RequiresRegister());
6350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6351}
6352
6353void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6354 LocationSummary* locations = instruction->GetLocations();
6355 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6356 locations->InAt(0).AsRegister<GpuRegister>(),
6357 1);
6358}
6359
6360void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006361 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6362 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006363}
6364
Calin Juravle2ae48182016-03-16 14:05:09 +00006365void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6366 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006367 return;
6368 }
6369 Location obj = instruction->GetLocations()->InAt(0);
6370
6371 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006372 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006373}
6374
Calin Juravle2ae48182016-03-16 14:05:09 +00006375void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006376 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006377 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006378 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006379
6380 Location obj = instruction->GetLocations()->InAt(0);
6381
6382 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6383}
6384
6385void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006386 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006387}
6388
6389void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6390 HandleBinaryOp(instruction);
6391}
6392
6393void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6394 HandleBinaryOp(instruction);
6395}
6396
6397void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6398 LOG(FATAL) << "Unreachable";
6399}
6400
6401void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006402 if (instruction->GetNext()->IsSuspendCheck() &&
6403 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6404 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6405 // The back edge will generate the suspend check.
6406 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6407 }
6408
Alexey Frunze4dda3372015-06-01 18:31:49 -07006409 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6410}
6411
6412void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006413 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006414 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6415 if (location.IsStackSlot()) {
6416 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6417 } else if (location.IsDoubleStackSlot()) {
6418 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6419 }
6420 locations->SetOut(location);
6421}
6422
6423void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6424 ATTRIBUTE_UNUSED) {
6425 // Nothing to do, the parameter is already at its location.
6426}
6427
6428void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6429 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006430 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006431 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6432}
6433
6434void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6435 ATTRIBUTE_UNUSED) {
6436 // Nothing to do, the method is already at its location.
6437}
6438
6439void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006440 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006441 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006442 locations->SetInAt(i, Location::Any());
6443 }
6444 locations->SetOut(Location::Any());
6445}
6446
6447void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6448 LOG(FATAL) << "Unreachable";
6449}
6450
6451void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006452 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006453 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006454 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6455 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006456 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006457
6458 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006459 case DataType::Type::kInt32:
6460 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006461 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006462 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6464 break;
6465
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006466 case DataType::Type::kFloat32:
6467 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006468 InvokeRuntimeCallingConvention calling_convention;
6469 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6470 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6471 locations->SetOut(calling_convention.GetReturnLocation(type));
6472 break;
6473 }
6474
6475 default:
6476 LOG(FATAL) << "Unexpected rem type " << type;
6477 }
6478}
6479
6480void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006481 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006482
6483 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006484 case DataType::Type::kInt32:
6485 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006486 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006487 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006488
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006489 case DataType::Type::kFloat32:
6490 case DataType::Type::kFloat64: {
6491 QuickEntrypointEnum entrypoint =
6492 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006493 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006494 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006495 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6496 } else {
6497 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6498 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006499 break;
6500 }
6501 default:
6502 LOG(FATAL) << "Unexpected rem type " << type;
6503 }
6504}
6505
Igor Murashkind01745e2017-04-05 16:40:31 -07006506void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6507 constructor_fence->SetLocations(nullptr);
6508}
6509
6510void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6511 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6512 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6513}
6514
Alexey Frunze4dda3372015-06-01 18:31:49 -07006515void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6516 memory_barrier->SetLocations(nullptr);
6517}
6518
6519void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6520 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6521}
6522
6523void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006524 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006525 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006526 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6527}
6528
6529void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6530 codegen_->GenerateFrameExit();
6531}
6532
6533void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6534 ret->SetLocations(nullptr);
6535}
6536
6537void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6538 codegen_->GenerateFrameExit();
6539}
6540
Alexey Frunze92d90602015-12-18 18:16:36 -08006541void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6542 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006543}
6544
Alexey Frunze92d90602015-12-18 18:16:36 -08006545void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6546 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006547}
6548
Alexey Frunze4dda3372015-06-01 18:31:49 -07006549void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6550 HandleShift(shl);
6551}
6552
6553void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6554 HandleShift(shl);
6555}
6556
6557void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6558 HandleShift(shr);
6559}
6560
6561void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6562 HandleShift(shr);
6563}
6564
Alexey Frunze4dda3372015-06-01 18:31:49 -07006565void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6566 HandleBinaryOp(instruction);
6567}
6568
6569void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6570 HandleBinaryOp(instruction);
6571}
6572
6573void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6574 HandleFieldGet(instruction, instruction->GetFieldInfo());
6575}
6576
6577void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6578 HandleFieldGet(instruction, instruction->GetFieldInfo());
6579}
6580
6581void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6582 HandleFieldSet(instruction, instruction->GetFieldInfo());
6583}
6584
6585void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006586 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006587}
6588
Calin Juravlee460d1d2015-09-29 04:52:17 +01006589void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6590 HUnresolvedInstanceFieldGet* instruction) {
6591 FieldAccessCallingConventionMIPS64 calling_convention;
6592 codegen_->CreateUnresolvedFieldLocationSummary(
6593 instruction, instruction->GetFieldType(), calling_convention);
6594}
6595
6596void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6597 HUnresolvedInstanceFieldGet* instruction) {
6598 FieldAccessCallingConventionMIPS64 calling_convention;
6599 codegen_->GenerateUnresolvedFieldAccess(instruction,
6600 instruction->GetFieldType(),
6601 instruction->GetFieldIndex(),
6602 instruction->GetDexPc(),
6603 calling_convention);
6604}
6605
6606void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6607 HUnresolvedInstanceFieldSet* instruction) {
6608 FieldAccessCallingConventionMIPS64 calling_convention;
6609 codegen_->CreateUnresolvedFieldLocationSummary(
6610 instruction, instruction->GetFieldType(), calling_convention);
6611}
6612
6613void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6614 HUnresolvedInstanceFieldSet* instruction) {
6615 FieldAccessCallingConventionMIPS64 calling_convention;
6616 codegen_->GenerateUnresolvedFieldAccess(instruction,
6617 instruction->GetFieldType(),
6618 instruction->GetFieldIndex(),
6619 instruction->GetDexPc(),
6620 calling_convention);
6621}
6622
6623void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6624 HUnresolvedStaticFieldGet* instruction) {
6625 FieldAccessCallingConventionMIPS64 calling_convention;
6626 codegen_->CreateUnresolvedFieldLocationSummary(
6627 instruction, instruction->GetFieldType(), calling_convention);
6628}
6629
6630void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6631 HUnresolvedStaticFieldGet* instruction) {
6632 FieldAccessCallingConventionMIPS64 calling_convention;
6633 codegen_->GenerateUnresolvedFieldAccess(instruction,
6634 instruction->GetFieldType(),
6635 instruction->GetFieldIndex(),
6636 instruction->GetDexPc(),
6637 calling_convention);
6638}
6639
6640void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6641 HUnresolvedStaticFieldSet* instruction) {
6642 FieldAccessCallingConventionMIPS64 calling_convention;
6643 codegen_->CreateUnresolvedFieldLocationSummary(
6644 instruction, instruction->GetFieldType(), calling_convention);
6645}
6646
6647void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6648 HUnresolvedStaticFieldSet* instruction) {
6649 FieldAccessCallingConventionMIPS64 calling_convention;
6650 codegen_->GenerateUnresolvedFieldAccess(instruction,
6651 instruction->GetFieldType(),
6652 instruction->GetFieldIndex(),
6653 instruction->GetDexPc(),
6654 calling_convention);
6655}
6656
Alexey Frunze4dda3372015-06-01 18:31:49 -07006657void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006658 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6659 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006660 // In suspend check slow path, usually there are no caller-save registers at all.
6661 // If SIMD instructions are present, however, we force spilling all live SIMD
6662 // registers in full width (since the runtime only saves/restores lower part).
6663 locations->SetCustomSlowPathCallerSaves(
6664 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006665}
6666
6667void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6668 HBasicBlock* block = instruction->GetBlock();
6669 if (block->GetLoopInformation() != nullptr) {
6670 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6671 // The back edge will generate the suspend check.
6672 return;
6673 }
6674 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6675 // The goto will generate the suspend check.
6676 return;
6677 }
6678 GenerateSuspendCheck(instruction, nullptr);
6679}
6680
Alexey Frunze4dda3372015-06-01 18:31:49 -07006681void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006682 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6683 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006684 InvokeRuntimeCallingConvention calling_convention;
6685 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6686}
6687
6688void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006689 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006690 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6691}
6692
6693void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006694 DataType::Type input_type = conversion->GetInputType();
6695 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006696 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6697 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006699 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6700 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006701 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6702 }
6703
Vladimir Markoca6fff82017-10-03 14:49:14 +01006704 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006706 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006707 locations->SetInAt(0, Location::RequiresFpuRegister());
6708 } else {
6709 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006710 }
6711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006712 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006713 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006714 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006716 }
6717}
6718
6719void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6720 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006721 DataType::Type result_type = conversion->GetResultType();
6722 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006723
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006724 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6725 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006728 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6729 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6730
6731 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006732 case DataType::Type::kUint8:
6733 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006734 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006735 case DataType::Type::kInt8:
6736 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006737 // Type conversion from long to types narrower than int is a result of code
6738 // transformations. To avoid unpredictable results for SEB and SEH, we first
6739 // need to sign-extend the low 32-bit value into bits 32 through 63.
6740 __ Sll(dst, src, 0);
6741 __ Seb(dst, dst);
6742 } else {
6743 __ Seb(dst, src);
6744 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006745 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006746 case DataType::Type::kUint16:
6747 __ Andi(dst, src, 0xFFFF);
6748 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006749 case DataType::Type::kInt16:
6750 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006751 // Type conversion from long to types narrower than int is a result of code
6752 // transformations. To avoid unpredictable results for SEB and SEH, we first
6753 // need to sign-extend the low 32-bit value into bits 32 through 63.
6754 __ Sll(dst, src, 0);
6755 __ Seh(dst, dst);
6756 } else {
6757 __ Seh(dst, src);
6758 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006759 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006760 case DataType::Type::kInt32:
6761 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006762 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6763 // conversions, except when the input and output registers are the same and we are not
6764 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006765 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006766 __ Sll(dst, src, 0);
6767 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006768 break;
6769
6770 default:
6771 LOG(FATAL) << "Unexpected type conversion from " << input_type
6772 << " to " << result_type;
6773 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006774 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006775 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6776 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006777 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006778 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006779 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006780 __ Cvtsl(dst, FTMP);
6781 } else {
6782 __ Cvtdl(dst, FTMP);
6783 }
6784 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006785 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006786 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006787 __ Cvtsw(dst, FTMP);
6788 } else {
6789 __ Cvtdw(dst, FTMP);
6790 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006791 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006792 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6793 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006794 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6795 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006797 if (result_type == DataType::Type::kInt64) {
6798 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006799 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006800 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006801 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006802 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006803 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006804 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006805 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006806 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006807 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006808 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006809 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006810 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006811 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006812 } else if (DataType::IsFloatingPointType(result_type) &&
6813 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006814 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6815 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006816 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006817 __ Cvtsd(dst, src);
6818 } else {
6819 __ Cvtds(dst, src);
6820 }
6821 } else {
6822 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6823 << " to " << result_type;
6824 }
6825}
6826
6827void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6828 HandleShift(ushr);
6829}
6830
6831void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6832 HandleShift(ushr);
6833}
6834
6835void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6836 HandleBinaryOp(instruction);
6837}
6838
6839void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6840 HandleBinaryOp(instruction);
6841}
6842
6843void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6844 // Nothing to do, this should be removed during prepare for register allocator.
6845 LOG(FATAL) << "Unreachable";
6846}
6847
6848void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6849 // Nothing to do, this should be removed during prepare for register allocator.
6850 LOG(FATAL) << "Unreachable";
6851}
6852
6853void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006854 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006855}
6856
6857void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006858 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006859}
6860
6861void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006862 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006863}
6864
6865void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006866 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006867}
6868
6869void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006870 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006871}
6872
6873void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006874 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006875}
6876
6877void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006878 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006879}
6880
6881void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006882 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006883}
6884
6885void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006886 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006887}
6888
6889void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006890 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006891}
6892
6893void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006894 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006895}
6896
6897void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006898 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006899}
6900
Aart Bike9f37602015-10-09 11:15:55 -07006901void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006902 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006903}
6904
6905void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006906 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006907}
6908
6909void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006910 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006911}
6912
6913void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006914 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006915}
6916
6917void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006918 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006919}
6920
6921void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006922 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006923}
6924
6925void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006926 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006927}
6928
6929void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006930 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006931}
6932
Mark Mendellfe57faa2015-09-18 09:26:15 -04006933// Simple implementation of packed switch - generate cascaded compare/jumps.
6934void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6935 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006936 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006937 locations->SetInAt(0, Location::RequiresRegister());
6938}
6939
Alexey Frunze0960ac52016-12-20 17:24:59 -08006940void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6941 int32_t lower_bound,
6942 uint32_t num_entries,
6943 HBasicBlock* switch_block,
6944 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006945 // Create a set of compare/jumps.
6946 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006947 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006948 // Jump to default if index is negative
6949 // Note: We don't check the case that index is positive while value < lower_bound, because in
6950 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6951 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6952
Alexey Frunze0960ac52016-12-20 17:24:59 -08006953 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006954 // Jump to successors[0] if value == lower_bound.
6955 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6956 int32_t last_index = 0;
6957 for (; num_entries - last_index > 2; last_index += 2) {
6958 __ Addiu(temp_reg, temp_reg, -2);
6959 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6960 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6961 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6962 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6963 }
6964 if (num_entries - last_index == 2) {
6965 // The last missing case_value.
6966 __ Addiu(temp_reg, temp_reg, -1);
6967 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006968 }
6969
6970 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006971 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006972 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006973 }
6974}
6975
Alexey Frunze0960ac52016-12-20 17:24:59 -08006976void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6977 int32_t lower_bound,
6978 uint32_t num_entries,
6979 HBasicBlock* switch_block,
6980 HBasicBlock* default_block) {
6981 // Create a jump table.
6982 std::vector<Mips64Label*> labels(num_entries);
6983 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6984 for (uint32_t i = 0; i < num_entries; i++) {
6985 labels[i] = codegen_->GetLabelOf(successors[i]);
6986 }
6987 JumpTable* table = __ CreateJumpTable(std::move(labels));
6988
6989 // Is the value in range?
6990 __ Addiu32(TMP, value_reg, -lower_bound);
6991 __ LoadConst32(AT, num_entries);
6992 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6993
6994 // We are in the range of the table.
6995 // Load the target address from the jump table, indexing by the value.
6996 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006997 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006998 __ Lw(TMP, TMP, 0);
6999 // Compute the absolute target address by adding the table start address
7000 // (the table contains offsets to targets relative to its start).
7001 __ Daddu(TMP, TMP, AT);
7002 // And jump.
7003 __ Jr(TMP);
7004 __ Nop();
7005}
7006
7007void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7008 int32_t lower_bound = switch_instr->GetStartValue();
7009 uint32_t num_entries = switch_instr->GetNumEntries();
7010 LocationSummary* locations = switch_instr->GetLocations();
7011 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7012 HBasicBlock* switch_block = switch_instr->GetBlock();
7013 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7014
7015 if (num_entries > kPackedSwitchJumpTableThreshold) {
7016 GenTableBasedPackedSwitch(value_reg,
7017 lower_bound,
7018 num_entries,
7019 switch_block,
7020 default_block);
7021 } else {
7022 GenPackedSwitchWithCompares(value_reg,
7023 lower_bound,
7024 num_entries,
7025 switch_block,
7026 default_block);
7027 }
7028}
7029
Chris Larsenc9905a62017-03-13 17:06:18 -07007030void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7031 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007032 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007033 locations->SetInAt(0, Location::RequiresRegister());
7034 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007035}
7036
Chris Larsenc9905a62017-03-13 17:06:18 -07007037void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7038 LocationSummary* locations = instruction->GetLocations();
7039 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7040 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7041 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7042 __ LoadFromOffset(kLoadDoubleword,
7043 locations->Out().AsRegister<GpuRegister>(),
7044 locations->InAt(0).AsRegister<GpuRegister>(),
7045 method_offset);
7046 } else {
7047 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7048 instruction->GetIndex(), kMips64PointerSize));
7049 __ LoadFromOffset(kLoadDoubleword,
7050 locations->Out().AsRegister<GpuRegister>(),
7051 locations->InAt(0).AsRegister<GpuRegister>(),
7052 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7053 __ LoadFromOffset(kLoadDoubleword,
7054 locations->Out().AsRegister<GpuRegister>(),
7055 locations->Out().AsRegister<GpuRegister>(),
7056 method_offset);
7057 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007058}
7059
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007060void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7061 ATTRIBUTE_UNUSED) {
7062 LOG(FATAL) << "Unreachable";
7063}
7064
7065void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7066 ATTRIBUTE_UNUSED) {
7067 LOG(FATAL) << "Unreachable";
7068}
7069
Alexey Frunze4dda3372015-06-01 18:31:49 -07007070} // namespace mips64
7071} // namespace art