blob: 3ae8a30754a6b127b40044ca0bd054af3a68700f [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips64/asm_support_mips64.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070020#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070022#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080023#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070028#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070029#include "intrinsics_mips64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "mirror/array-inl.h"
32#include "mirror/class-inl.h"
33#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010034#include "stack_map_stream.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070035#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070037#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070038#include "utils/stack_checks.h"
39
40namespace art {
41namespace mips64 {
42
43static constexpr int kCurrentMethodStackOffset = 0;
44static constexpr GpuRegister kMethodRegisterArgument = A0;
45
Alexey Frunze4147fcc2017-06-17 19:57:27 -070046// Flags controlling the use of thunks for Baker read barriers.
47constexpr bool kBakerReadBarrierThunksEnableForFields = true;
48constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
49constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
50
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051Location Mips64ReturnLocation(DataType::Type return_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070052 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010054 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 case DataType::Type::kInt8:
56 case DataType::Type::kUint16:
57 case DataType::Type::kInt16:
58 case DataType::Type::kInt32:
59 case DataType::Type::kReference:
60 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070061 return Location::RegisterLocation(V0);
62
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kFloat32:
64 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070065 return Location::FpuRegisterLocation(F0);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070068 return Location();
69 }
70 UNREACHABLE();
71}
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070074 return Mips64ReturnLocation(type);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
78 return Location::RegisterLocation(kMethodRegisterArgument);
79}
80
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070082 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070084 LOG(FATAL) << "Unexpected parameter type " << type;
85 }
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070088 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
89 next_location = Location::FpuRegisterLocation(
90 calling_convention.GetFpuRegisterAt(float_index_++));
91 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070093 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
94 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
95 float_index_++;
96 } else {
97 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010098 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
99 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700100 }
101
102 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
Alexey Frunze4dda3372015-06-01 18:31:49 -0700105 return next_location;
106}
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700109 return Mips64ReturnLocation(type);
110}
111
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100112// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
113#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700114#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115
116class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
117 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700119
120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100121 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
123 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000124 if (instruction_->CanThrowIntoCatchBlock()) {
125 // Live registers will be restored in the catch block if caught.
126 SaveLiveRegisters(codegen, instruction_->GetLocations());
127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 // We're moving two locations to locations that could overlap, so we need a parallel
129 // move resolver.
130 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100137 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
138 ? kQuickThrowStringBounds
139 : kQuickThrowArrayBounds;
140 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100141 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
143 }
144
Alexandre Rames8158f282015-08-07 10:26:17 +0100145 bool IsFatal() const OVERRIDE { return true; }
146
Roland Levillain46648892015-06-19 16:07:18 +0100147 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
148
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
151};
152
153class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
154 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700155 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
156 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
160 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100161 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
163 }
164
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 bool IsFatal() const OVERRIDE { return true; }
166
Roland Levillain46648892015-06-19 16:07:18 +0100167 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
168
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700170 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
171};
172
173class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
174 public:
175 LoadClassSlowPathMIPS64(HLoadClass* cls,
176 HInstruction* at,
177 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000178 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700179 : SlowPathCodeMIPS64(at),
180 cls_(cls),
181 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000182 do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000187 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700188 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700189 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700190 InvokeRuntimeCallingConvention calling_convention;
191 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 __ Bind(GetEntryLabel());
193 SaveLiveRegisters(codegen, locations);
194
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000195 dex::TypeIndex type_index = cls_->GetTypeIndex();
196 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100197 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
198 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000199 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700200 if (do_clinit_) {
201 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
202 } else {
203 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
204 }
205
206 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700207 if (out.IsValid()) {
208 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100209 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700210 mips64_codegen->MoveLocation(out,
211 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
212 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700215
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700216 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 }
218
Roland Levillain46648892015-06-19 16:07:18 +0100219 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
220
Alexey Frunze4dda3372015-06-01 18:31:49 -0700221 private:
222 // The class this slow path will load.
223 HLoadClass* const cls_;
224
Alexey Frunze4dda3372015-06-01 18:31:49 -0700225 // The dex PC of `at_`.
226 const uint32_t dex_pc_;
227
228 // Whether to initialize the class.
229 const bool do_clinit_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
232};
233
234class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
235 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000236 explicit LoadStringSlowPathMIPS64(HLoadString* instruction)
237 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700240 DCHECK(instruction_->IsLoadString());
241 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700242 LocationSummary* locations = instruction_->GetLocations();
243 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000244 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700246 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000250 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100251 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700252 instruction_,
253 instruction_->GetDexPc(),
254 this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100257 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700258 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800262
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700263 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700264 }
265
Roland Levillain46648892015-06-19 16:07:18 +0100266 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
267
Alexey Frunze4dda3372015-06-01 18:31:49 -0700268 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
270};
271
272class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
273 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000274 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275
276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
277 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
278 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000279 if (instruction_->CanThrowIntoCatchBlock()) {
280 // Live registers will be restored in the catch block if caught.
281 SaveLiveRegisters(codegen, instruction_->GetLocations());
282 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100283 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700284 instruction_,
285 instruction_->GetDexPc(),
286 this);
287 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
288 }
289
Alexandre Rames8158f282015-08-07 10:26:17 +0100290 bool IsFatal() const OVERRIDE { return true; }
291
Roland Levillain46648892015-06-19 16:07:18 +0100292 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
293
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
296};
297
298class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
299 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100300 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000301 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200304 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200307 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100308 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200310 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700312 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 }
316 }
317
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700318 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 DCHECK(successor_ == nullptr);
320 return &return_label_;
321 }
322
Roland Levillain46648892015-06-19 16:07:18 +0100323 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
324
Chris Larsena2045912017-11-02 12:39:54 -0700325 HBasicBlock* GetSuccessor() const {
326 return successor_;
327 }
328
Alexey Frunze4dda3372015-06-01 18:31:49 -0700329 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
342 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700343
344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
345 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800346
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 DCHECK(instruction_->IsCheckCast()
349 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
350 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
351
352 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800353 if (!is_fatal_) {
354 SaveLiveRegisters(codegen, locations);
355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356
357 // We're moving two locations to locations that could overlap, so we need a parallel
358 // move resolver.
359 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100362 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100365 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100367 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800368 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100369 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
371 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 } else {
373 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800374 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
375 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 }
377
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800378 if (!is_fatal_) {
379 RestoreLiveRegisters(codegen, locations);
380 __ Bc(GetExitLabel());
381 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
385
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800386 bool IsFatal() const OVERRIDE { return is_fatal_; }
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800389 const bool is_fatal_;
390
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
392};
393
394class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
395 public:
Aart Bik42249c32016-01-07 15:33:50 -0800396 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000397 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398
399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800400 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100402 LocationSummary* locations = instruction_->GetLocations();
403 SaveLiveRegisters(codegen, locations);
404 InvokeRuntimeCallingConvention calling_convention;
405 __ LoadConst32(calling_convention.GetRegisterAt(0),
406 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100407 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100408 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 }
410
Roland Levillain46648892015-06-19 16:07:18 +0100411 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
412
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
415};
416
Alexey Frunze15958152017-02-09 19:08:30 -0800417class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
418 public:
419 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
420
421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
422 LocationSummary* locations = instruction_->GetLocations();
423 __ Bind(GetEntryLabel());
424 SaveLiveRegisters(codegen, locations);
425
426 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100427 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800428 parallel_move.AddMove(
429 locations->InAt(0),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100431 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800432 nullptr);
433 parallel_move.AddMove(
434 locations->InAt(1),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100436 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(2),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100441 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800442 nullptr);
443 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
444
445 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
446 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
447 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
448 RestoreLiveRegisters(codegen, locations);
449 __ Bc(GetExitLabel());
450 }
451
452 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
453
454 private:
455 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
456};
457
458// Slow path marking an object reference `ref` during a read
459// barrier. The field `obj.field` in the object `obj` holding this
460// reference does not get updated by this slow path after marking (see
461// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
462//
463// This means that after the execution of this slow path, `ref` will
464// always be up-to-date, but `obj.field` may not; i.e., after the
465// flip, `ref` will be a to-space reference, but `obj.field` will
466// probably still be a from-space reference (unless it gets updated by
467// another thread, or if another thread installed another object
468// reference (different from `ref`) in `obj.field`).
469//
470// If `entrypoint` is a valid location it is assumed to already be
471// holding the entrypoint. The case where the entrypoint is passed in
472// is for the GcRoot read barrier.
473class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
474 public:
475 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
476 Location ref,
477 Location entrypoint = Location::NoLocation())
478 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
479 DCHECK(kEmitCompilerReadBarrier);
480 }
481
482 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
483
484 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
485 LocationSummary* locations = instruction_->GetLocations();
486 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
487 DCHECK(locations->CanCall());
488 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
489 DCHECK(instruction_->IsInstanceFieldGet() ||
490 instruction_->IsStaticFieldGet() ||
491 instruction_->IsArrayGet() ||
492 instruction_->IsArraySet() ||
493 instruction_->IsLoadClass() ||
494 instruction_->IsLoadString() ||
495 instruction_->IsInstanceOf() ||
496 instruction_->IsCheckCast() ||
497 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
498 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
499 << "Unexpected instruction in read barrier marking slow path: "
500 << instruction_->DebugName();
501
502 __ Bind(GetEntryLabel());
503 // No need to save live registers; it's taken care of by the
504 // entrypoint. Also, there is no need to update the stack mask,
505 // as this runtime call will not trigger a garbage collection.
506 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
507 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
508 (S2 <= ref_reg && ref_reg <= S7) ||
509 (ref_reg == S8)) << ref_reg;
510 // "Compact" slow path, saving two moves.
511 //
512 // Instead of using the standard runtime calling convention (input
513 // and output in A0 and V0 respectively):
514 //
515 // A0 <- ref
516 // V0 <- ReadBarrierMark(A0)
517 // ref <- V0
518 //
519 // we just use rX (the register containing `ref`) as input and output
520 // of a dedicated entrypoint:
521 //
522 // rX <- ReadBarrierMarkRegX(rX)
523 //
524 if (entrypoint_.IsValid()) {
525 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
526 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
527 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
528 __ Nop();
529 } else {
530 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100531 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800532 // This runtime call does not require a stack map.
533 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
534 instruction_,
535 this);
536 }
537 __ Bc(GetExitLabel());
538 }
539
540 private:
541 // The location (register) of the marked object reference.
542 const Location ref_;
543
544 // The location of the entrypoint if already loaded.
545 const Location entrypoint_;
546
547 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
548};
549
550// Slow path marking an object reference `ref` during a read barrier,
551// and if needed, atomically updating the field `obj.field` in the
552// object `obj` holding this reference after marking (contrary to
553// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
554// `obj.field`).
555//
556// This means that after the execution of this slow path, both `ref`
557// and `obj.field` will be up-to-date; i.e., after the flip, both will
558// hold the same to-space reference (unless another thread installed
559// another object reference (different from `ref`) in `obj.field`).
560class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
561 public:
562 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
563 Location ref,
564 GpuRegister obj,
565 Location field_offset,
566 GpuRegister temp1)
567 : SlowPathCodeMIPS64(instruction),
568 ref_(ref),
569 obj_(obj),
570 field_offset_(field_offset),
571 temp1_(temp1) {
572 DCHECK(kEmitCompilerReadBarrier);
573 }
574
575 const char* GetDescription() const OVERRIDE {
576 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
577 }
578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 LocationSummary* locations = instruction_->GetLocations();
581 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
582 DCHECK(locations->CanCall());
583 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
584 // This slow path is only used by the UnsafeCASObject intrinsic.
585 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
586 << "Unexpected instruction in read barrier marking and field updating slow path: "
587 << instruction_->DebugName();
588 DCHECK(instruction_->GetLocations()->Intrinsified());
589 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
590 DCHECK(field_offset_.IsRegister()) << field_offset_;
591
592 __ Bind(GetEntryLabel());
593
594 // Save the old reference.
595 // Note that we cannot use AT or TMP to save the old reference, as those
596 // are used by the code that follows, but we need the old reference after
597 // the call to the ReadBarrierMarkRegX entry point.
598 DCHECK_NE(temp1_, AT);
599 DCHECK_NE(temp1_, TMP);
600 __ Move(temp1_, ref_reg);
601
602 // No need to save live registers; it's taken care of by the
603 // entrypoint. Also, there is no need to update the stack mask,
604 // as this runtime call will not trigger a garbage collection.
605 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
606 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
607 (S2 <= ref_reg && ref_reg <= S7) ||
608 (ref_reg == S8)) << ref_reg;
609 // "Compact" slow path, saving two moves.
610 //
611 // Instead of using the standard runtime calling convention (input
612 // and output in A0 and V0 respectively):
613 //
614 // A0 <- ref
615 // V0 <- ReadBarrierMark(A0)
616 // ref <- V0
617 //
618 // we just use rX (the register containing `ref`) as input and output
619 // of a dedicated entrypoint:
620 //
621 // rX <- ReadBarrierMarkRegX(rX)
622 //
623 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100624 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800625 // This runtime call does not require a stack map.
626 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
627 instruction_,
628 this);
629
630 // If the new reference is different from the old reference,
631 // update the field in the holder (`*(obj_ + field_offset_)`).
632 //
633 // Note that this field could also hold a different object, if
634 // another thread had concurrently changed it. In that case, the
635 // the compare-and-set (CAS) loop below would abort, leaving the
636 // field as-is.
637 Mips64Label done;
638 __ Beqc(temp1_, ref_reg, &done);
639
640 // Update the the holder's field atomically. This may fail if
641 // mutator updates before us, but it's OK. This is achieved
642 // using a strong compare-and-set (CAS) operation with relaxed
643 // memory synchronization ordering, where the expected value is
644 // the old reference and the desired value is the new reference.
645
646 // Convenience aliases.
647 GpuRegister base = obj_;
648 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
649 GpuRegister expected = temp1_;
650 GpuRegister value = ref_reg;
651 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
652 GpuRegister tmp = AT; // Value in memory.
653
654 __ Daddu(tmp_ptr, base, offset);
655
656 if (kPoisonHeapReferences) {
657 __ PoisonHeapReference(expected);
658 // Do not poison `value` if it is the same register as
659 // `expected`, which has just been poisoned.
660 if (value != expected) {
661 __ PoisonHeapReference(value);
662 }
663 }
664
665 // do {
666 // tmp = [r_ptr] - expected;
667 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
668
669 Mips64Label loop_head, exit_loop;
670 __ Bind(&loop_head);
671 __ Ll(tmp, tmp_ptr);
672 // The LL instruction sign-extends the 32-bit value, but
673 // 32-bit references must be zero-extended. Zero-extend `tmp`.
674 __ Dext(tmp, tmp, 0, 32);
675 __ Bnec(tmp, expected, &exit_loop);
676 __ Move(tmp, value);
677 __ Sc(tmp, tmp_ptr);
678 __ Beqzc(tmp, &loop_head);
679 __ Bind(&exit_loop);
680
681 if (kPoisonHeapReferences) {
682 __ UnpoisonHeapReference(expected);
683 // Do not unpoison `value` if it is the same register as
684 // `expected`, which has just been unpoisoned.
685 if (value != expected) {
686 __ UnpoisonHeapReference(value);
687 }
688 }
689
690 __ Bind(&done);
691 __ Bc(GetExitLabel());
692 }
693
694 private:
695 // The location (register) of the marked object reference.
696 const Location ref_;
697 // The register containing the object holding the marked object reference field.
698 const GpuRegister obj_;
699 // The location of the offset of the marked reference field within `obj_`.
700 Location field_offset_;
701
702 const GpuRegister temp1_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
705};
706
707// Slow path generating a read barrier for a heap reference.
708class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
709 public:
710 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
711 Location out,
712 Location ref,
713 Location obj,
714 uint32_t offset,
715 Location index)
716 : SlowPathCodeMIPS64(instruction),
717 out_(out),
718 ref_(ref),
719 obj_(obj),
720 offset_(offset),
721 index_(index) {
722 DCHECK(kEmitCompilerReadBarrier);
723 // If `obj` is equal to `out` or `ref`, it means the initial object
724 // has been overwritten by (or after) the heap object reference load
725 // to be instrumented, e.g.:
726 //
727 // __ LoadFromOffset(kLoadWord, out, out, offset);
728 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
729 //
730 // In that case, we have lost the information about the original
731 // object, and the emitted read barrier cannot work properly.
732 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
733 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
734 }
735
736 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
737 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
738 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800740 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
741 DCHECK(locations->CanCall());
742 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
743 DCHECK(instruction_->IsInstanceFieldGet() ||
744 instruction_->IsStaticFieldGet() ||
745 instruction_->IsArrayGet() ||
746 instruction_->IsInstanceOf() ||
747 instruction_->IsCheckCast() ||
748 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
749 << "Unexpected instruction in read barrier for heap reference slow path: "
750 << instruction_->DebugName();
751
752 __ Bind(GetEntryLabel());
753 SaveLiveRegisters(codegen, locations);
754
755 // We may have to change the index's value, but as `index_` is a
756 // constant member (like other "inputs" of this slow path),
757 // introduce a copy of it, `index`.
758 Location index = index_;
759 if (index_.IsValid()) {
760 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
761 if (instruction_->IsArrayGet()) {
762 // Compute the actual memory offset and store it in `index`.
763 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
764 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
765 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
766 // We are about to change the value of `index_reg` (see the
767 // calls to art::mips64::Mips64Assembler::Sll and
768 // art::mips64::MipsAssembler::Addiu32 below), but it has
769 // not been saved by the previous call to
770 // art::SlowPathCode::SaveLiveRegisters, as it is a
771 // callee-save register --
772 // art::SlowPathCode::SaveLiveRegisters does not consider
773 // callee-save registers, as it has been designed with the
774 // assumption that callee-save registers are supposed to be
775 // handled by the called function. So, as a callee-save
776 // register, `index_reg` _would_ eventually be saved onto
777 // the stack, but it would be too late: we would have
778 // changed its value earlier. Therefore, we manually save
779 // it here into another freely available register,
780 // `free_reg`, chosen of course among the caller-save
781 // registers (as a callee-save `free_reg` register would
782 // exhibit the same problem).
783 //
784 // Note we could have requested a temporary register from
785 // the register allocator instead; but we prefer not to, as
786 // this is a slow path, and we know we can find a
787 // caller-save register that is available.
788 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
789 __ Move(free_reg, index_reg);
790 index_reg = free_reg;
791 index = Location::RegisterLocation(index_reg);
792 } else {
793 // The initial register stored in `index_` has already been
794 // saved in the call to art::SlowPathCode::SaveLiveRegisters
795 // (as it is not a callee-save register), so we can freely
796 // use it.
797 }
798 // Shifting the index value contained in `index_reg` by the scale
799 // factor (2) cannot overflow in practice, as the runtime is
800 // unable to allocate object arrays with a size larger than
801 // 2^26 - 1 (that is, 2^28 - 4 bytes).
802 __ Sll(index_reg, index_reg, TIMES_4);
803 static_assert(
804 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
805 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
806 __ Addiu32(index_reg, index_reg, offset_);
807 } else {
808 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
809 // intrinsics, `index_` is not shifted by a scale factor of 2
810 // (as in the case of ArrayGet), as it is actually an offset
811 // to an object field within an object.
812 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
813 DCHECK(instruction_->GetLocations()->Intrinsified());
814 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
815 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
816 << instruction_->AsInvoke()->GetIntrinsic();
817 DCHECK_EQ(offset_, 0U);
818 DCHECK(index_.IsRegister());
819 }
820 }
821
822 // We're moving two or three locations to locations that could
823 // overlap, so we need a parallel move resolver.
824 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100825 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800826 parallel_move.AddMove(ref_,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800829 nullptr);
830 parallel_move.AddMove(obj_,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800833 nullptr);
834 if (index.IsValid()) {
835 parallel_move.AddMove(index,
836 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100837 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800838 nullptr);
839 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
840 } else {
841 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
842 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
843 }
844 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
845 instruction_,
846 instruction_->GetDexPc(),
847 this);
848 CheckEntrypointTypes<
849 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
850 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
851
852 RestoreLiveRegisters(codegen, locations);
853 __ Bc(GetExitLabel());
854 }
855
856 const char* GetDescription() const OVERRIDE {
857 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
858 }
859
860 private:
861 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
862 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
863 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
864 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
865 if (i != ref &&
866 i != obj &&
867 !codegen->IsCoreCalleeSaveRegister(i) &&
868 !codegen->IsBlockedCoreRegister(i)) {
869 return static_cast<GpuRegister>(i);
870 }
871 }
872 // We shall never fail to find a free caller-save register, as
873 // there are more than two core caller-save registers on MIPS64
874 // (meaning it is possible to find one which is different from
875 // `ref` and `obj`).
876 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
877 LOG(FATAL) << "Could not find a free caller-save register";
878 UNREACHABLE();
879 }
880
881 const Location out_;
882 const Location ref_;
883 const Location obj_;
884 const uint32_t offset_;
885 // An additional location containing an index to an array.
886 // Only used for HArrayGet and the UnsafeGetObject &
887 // UnsafeGetObjectVolatile intrinsics.
888 const Location index_;
889
890 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
891};
892
893// Slow path generating a read barrier for a GC root.
894class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
895 public:
896 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
897 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
898 DCHECK(kEmitCompilerReadBarrier);
899 }
900
901 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
902 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100903 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800904 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
905 DCHECK(locations->CanCall());
906 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
907 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
908 << "Unexpected instruction in read barrier for GC root slow path: "
909 << instruction_->DebugName();
910
911 __ Bind(GetEntryLabel());
912 SaveLiveRegisters(codegen, locations);
913
914 InvokeRuntimeCallingConvention calling_convention;
915 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
916 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
917 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100918 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800919 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
920 instruction_,
921 instruction_->GetDexPc(),
922 this);
923 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
924 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
925
926 RestoreLiveRegisters(codegen, locations);
927 __ Bc(GetExitLabel());
928 }
929
930 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
931
932 private:
933 const Location out_;
934 const Location root_;
935
936 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
937};
938
Alexey Frunze4dda3372015-06-01 18:31:49 -0700939CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
940 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100941 const CompilerOptions& compiler_options,
942 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943 : CodeGenerator(graph,
944 kNumberOfGpuRegisters,
945 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000946 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
948 arraysize(kCoreCalleeSaves)),
949 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
950 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100951 compiler_options,
952 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100953 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700954 location_builder_(graph, this),
955 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 move_resolver_(graph->GetAllocator(), this),
957 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800958 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800959 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800961 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
963 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
964 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
965 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
966 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
967 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
968 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800969 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800971 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700973 // Save RA (containing the return address) to mimic Quick.
974 AddAllocatedRegister(Location::RegisterLocation(RA));
975}
976
977#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100978// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
979#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700980#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700981
982void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700983 // Ensure that we fix up branches.
984 __ FinalizeCode();
985
986 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100987 StackMapStream* stack_map_stream = GetStackMapStream();
988 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800989 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +0000990 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700991 uint32_t new_position = __ GetAdjustedPosition(old_position);
992 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100993 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700994 }
995
996 // Adjust pc offsets for the disassembly information.
997 if (disasm_info_ != nullptr) {
998 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
999 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1000 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1001 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1002 it.second.start = __ GetAdjustedPosition(it.second.start);
1003 it.second.end = __ GetAdjustedPosition(it.second.end);
1004 }
1005 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1006 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1007 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1008 }
1009 }
1010
Alexey Frunze4dda3372015-06-01 18:31:49 -07001011 CodeGenerator::Finalize(allocator);
1012}
1013
1014Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1015 return codegen_->GetAssembler();
1016}
1017
1018void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001019 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001020 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1021}
1022
1023void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001024 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1026}
1027
1028void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1029 // Pop reg
1030 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001031 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032}
1033
1034void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1035 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001036 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001037 __ Sd(GpuRegister(reg), SP, 0);
1038}
1039
1040void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1041 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1042 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1043 // Allocate a scratch register other than TMP, if available.
1044 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1045 // automatically unspilled when the scratch scope object is destroyed).
1046 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1047 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001048 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001049 __ LoadFromOffset(load_type,
1050 GpuRegister(ensure_scratch.GetRegister()),
1051 SP,
1052 index1 + stack_offset);
1053 __ LoadFromOffset(load_type,
1054 TMP,
1055 SP,
1056 index2 + stack_offset);
1057 __ StoreToOffset(store_type,
1058 GpuRegister(ensure_scratch.GetRegister()),
1059 SP,
1060 index2 + stack_offset);
1061 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1062}
1063
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001064void ParallelMoveResolverMIPS64::ExchangeQuadSlots(int index1, int index2) {
1065 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, index1);
1066 __ LoadFpuFromOffset(kLoadQuadword, FTMP2, SP, index2);
1067 __ StoreFpuToOffset(kStoreQuadword, FTMP, SP, index2);
1068 __ StoreFpuToOffset(kStoreQuadword, FTMP2, SP, index1);
1069}
1070
Alexey Frunze4dda3372015-06-01 18:31:49 -07001071static dwarf::Reg DWARFReg(GpuRegister reg) {
1072 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1073}
1074
David Srbeckyba702002016-02-01 18:15:29 +00001075static dwarf::Reg DWARFReg(FpuRegister reg) {
1076 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1077}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001078
1079void CodeGeneratorMIPS64::GenerateFrameEntry() {
1080 __ Bind(&frame_entry_label_);
1081
Vladimir Marko33bff252017-11-01 14:35:42 +00001082 bool do_overflow_check =
1083 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001084
1085 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001086 __ LoadFromOffset(
1087 kLoadWord,
1088 ZERO,
1089 SP,
1090 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091 RecordPcInfo(nullptr, 0);
1092 }
1093
Alexey Frunze4dda3372015-06-01 18:31:49 -07001094 if (HasEmptyFrame()) {
1095 return;
1096 }
1097
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001098 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001099 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1100 LOG(FATAL) << "Stack frame larger than "
1101 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001102 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001103
1104 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001105
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001107 __ IncreaseFrameSize(ofs);
1108
1109 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1110 GpuRegister reg = kCoreCalleeSaves[i];
1111 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001112 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001113 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001114 __ cfi().RelOffset(DWARFReg(reg), ofs);
1115 }
1116 }
1117
1118 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1119 FpuRegister reg = kFpuCalleeSaves[i];
1120 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001121 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001122 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001123 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001124 }
1125 }
1126
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001127 // Save the current method if we need it. Note that we do not
1128 // do this in HCurrentMethod, as the instruction might have been removed
1129 // in the SSA graph.
1130 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001131 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001132 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001133
1134 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1135 // Initialize should_deoptimize flag to 0.
1136 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1137 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138}
1139
1140void CodeGeneratorMIPS64::GenerateFrameExit() {
1141 __ cfi().RememberState();
1142
Alexey Frunze4dda3372015-06-01 18:31:49 -07001143 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001144 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001145
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001146 // For better instruction scheduling restore RA before other registers.
1147 uint32_t ofs = GetFrameSize();
1148 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149 GpuRegister reg = kCoreCalleeSaves[i];
1150 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001151 ofs -= kMips64DoublewordSize;
1152 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153 __ cfi().Restore(DWARFReg(reg));
1154 }
1155 }
1156
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001157 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1158 FpuRegister reg = kFpuCalleeSaves[i];
1159 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1160 ofs -= kMips64DoublewordSize;
1161 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1162 __ cfi().Restore(DWARFReg(reg));
1163 }
1164 }
1165
1166 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001167 }
1168
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001169 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001170
1171 __ cfi().RestoreState();
1172 __ cfi().DefCFAOffset(GetFrameSize());
1173}
1174
1175void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1176 __ Bind(GetLabelOf(block));
1177}
1178
1179void CodeGeneratorMIPS64::MoveLocation(Location destination,
1180 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001181 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182 if (source.Equals(destination)) {
1183 return;
1184 }
1185
1186 // A valid move can always be inferred from the destination and source
1187 // locations. When moving from and to a register, the argument type can be
1188 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001189 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001190 DCHECK_EQ(unspecified_type, false);
1191
1192 if (destination.IsRegister() || destination.IsFpuRegister()) {
1193 if (unspecified_type) {
1194 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1195 if (source.IsStackSlot() ||
1196 (src_cst != nullptr && (src_cst->IsIntConstant()
1197 || src_cst->IsFloatConstant()
1198 || src_cst->IsNullConstant()))) {
1199 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001201 } else {
1202 // If the source is a double stack slot or a 64bit constant, a 64bit
1203 // type is appropriate. Else the source is a register, and since the
1204 // type has not been specified, we chose a 64bit type to force a 64bit
1205 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001207 }
1208 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001209 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1210 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001211 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1212 // Move to GPR/FPR from stack
1213 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 __ LoadFpuFromOffset(load_type,
1216 destination.AsFpuRegister<FpuRegister>(),
1217 SP,
1218 source.GetStackIndex());
1219 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001220 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001221 __ LoadFromOffset(load_type,
1222 destination.AsRegister<GpuRegister>(),
1223 SP,
1224 source.GetStackIndex());
1225 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001226 } else if (source.IsSIMDStackSlot()) {
1227 __ LoadFpuFromOffset(kLoadQuadword,
1228 destination.AsFpuRegister<FpuRegister>(),
1229 SP,
1230 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001231 } else if (source.IsConstant()) {
1232 // Move to GPR/FPR from constant
1233 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001234 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001235 gpr = destination.AsRegister<GpuRegister>();
1236 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001237 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001238 int32_t value = GetInt32ValueOf(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 __ LoadConst32(gpr, value);
1243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001244 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001245 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001246 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001247 gpr = ZERO;
1248 } else {
1249 __ LoadConst64(gpr, value);
1250 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001252 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001253 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001254 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001255 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1256 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001258 if (destination.IsRegister()) {
1259 // Move to GPR from GPR
1260 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1261 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001263 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001264 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1265 } else {
1266 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1267 }
1268 }
1269 } else if (source.IsFpuRegister()) {
1270 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001271 if (GetGraph()->HasSIMD()) {
1272 __ MoveV(VectorRegisterFrom(destination),
1273 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001274 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001275 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001276 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001277 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1278 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001279 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001280 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001282 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 } else {
1284 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001285 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1287 } else {
1288 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1289 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 }
1291 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001292 } else if (destination.IsSIMDStackSlot()) {
1293 if (source.IsFpuRegister()) {
1294 __ StoreFpuToOffset(kStoreQuadword,
1295 source.AsFpuRegister<FpuRegister>(),
1296 SP,
1297 destination.GetStackIndex());
1298 } else {
1299 DCHECK(source.IsSIMDStackSlot());
1300 __ LoadFpuFromOffset(kLoadQuadword,
1301 FTMP,
1302 SP,
1303 source.GetStackIndex());
1304 __ StoreFpuToOffset(kStoreQuadword,
1305 FTMP,
1306 SP,
1307 destination.GetStackIndex());
1308 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001309 } else { // The destination is not a register. It must be a stack slot.
1310 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1311 if (source.IsRegister() || source.IsFpuRegister()) {
1312 if (unspecified_type) {
1313 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001315 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001316 dst_type =
1317 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001318 }
1319 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001320 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1321 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001322 // Move to stack from GPR/FPR
1323 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1324 if (source.IsRegister()) {
1325 __ StoreToOffset(store_type,
1326 source.AsRegister<GpuRegister>(),
1327 SP,
1328 destination.GetStackIndex());
1329 } else {
1330 __ StoreFpuToOffset(store_type,
1331 source.AsFpuRegister<FpuRegister>(),
1332 SP,
1333 destination.GetStackIndex());
1334 }
1335 } else if (source.IsConstant()) {
1336 // Move to stack from constant
1337 HConstant* src_cst = source.GetConstant();
1338 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001339 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001340 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001341 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1342 if (value != 0) {
1343 gpr = TMP;
1344 __ LoadConst32(gpr, value);
1345 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001346 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001347 DCHECK(destination.IsDoubleStackSlot());
1348 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1349 if (value != 0) {
1350 gpr = TMP;
1351 __ LoadConst64(gpr, value);
1352 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001353 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001354 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001355 } else {
1356 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1357 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1358 // Move to stack from stack
1359 if (destination.IsStackSlot()) {
1360 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1361 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1362 } else {
1363 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1364 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1365 }
1366 }
1367 }
1368}
1369
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001370void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001371 DCHECK(!loc1.IsConstant());
1372 DCHECK(!loc2.IsConstant());
1373
1374 if (loc1.Equals(loc2)) {
1375 return;
1376 }
1377
1378 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1379 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001380 bool is_simd1 = loc1.IsSIMDStackSlot();
1381 bool is_simd2 = loc2.IsSIMDStackSlot();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001382 bool is_fp_reg1 = loc1.IsFpuRegister();
1383 bool is_fp_reg2 = loc2.IsFpuRegister();
1384
1385 if (loc2.IsRegister() && loc1.IsRegister()) {
1386 // Swap 2 GPRs
1387 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1388 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1389 __ Move(TMP, r2);
1390 __ Move(r2, r1);
1391 __ Move(r1, TMP);
1392 } else if (is_fp_reg2 && is_fp_reg1) {
1393 // Swap 2 FPRs
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001394 if (GetGraph()->HasSIMD()) {
1395 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1396 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1397 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001398 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001399 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1400 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
1401 if (type == DataType::Type::kFloat32) {
1402 __ MovS(FTMP, r1);
1403 __ MovS(r1, r2);
1404 __ MovS(r2, FTMP);
1405 } else {
1406 DCHECK_EQ(type, DataType::Type::kFloat64);
1407 __ MovD(FTMP, r1);
1408 __ MovD(r1, r2);
1409 __ MovD(r2, FTMP);
1410 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001411 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001412 } else if (is_slot1 != is_slot2) {
1413 // Swap GPR/FPR and stack slot
1414 Location reg_loc = is_slot1 ? loc2 : loc1;
1415 Location mem_loc = is_slot1 ? loc1 : loc2;
1416 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1417 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001418 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1420 if (reg_loc.IsFpuRegister()) {
1421 __ StoreFpuToOffset(store_type,
1422 reg_loc.AsFpuRegister<FpuRegister>(),
1423 SP,
1424 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425 if (mem_loc.IsStackSlot()) {
1426 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1427 } else {
1428 DCHECK(mem_loc.IsDoubleStackSlot());
1429 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1430 }
1431 } else {
1432 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1433 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1434 }
1435 } else if (is_slot1 && is_slot2) {
1436 move_resolver_.Exchange(loc1.GetStackIndex(),
1437 loc2.GetStackIndex(),
1438 loc1.IsDoubleStackSlot());
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001439 } else if (is_simd1 && is_simd2) {
1440 move_resolver_.ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
1441 } else if ((is_fp_reg1 && is_simd2) || (is_fp_reg2 && is_simd1)) {
1442 Location fp_reg_loc = is_fp_reg1 ? loc1 : loc2;
1443 Location mem_loc = is_fp_reg1 ? loc2 : loc1;
1444 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, mem_loc.GetStackIndex());
1445 __ StoreFpuToOffset(kStoreQuadword,
1446 fp_reg_loc.AsFpuRegister<FpuRegister>(),
1447 SP,
1448 mem_loc.GetStackIndex());
1449 __ MoveV(VectorRegisterFrom(fp_reg_loc), static_cast<VectorRegister>(FTMP));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001450 } else {
1451 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1452 }
1453}
1454
Calin Juravle175dc732015-08-25 15:42:32 +01001455void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1456 DCHECK(location.IsRegister());
1457 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1458}
1459
Calin Juravlee460d1d2015-09-29 04:52:17 +01001460void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1461 if (location.IsRegister()) {
1462 locations->AddTemp(location);
1463 } else {
1464 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1465 }
1466}
1467
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001468void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1469 GpuRegister value,
1470 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001471 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001472 GpuRegister card = AT;
1473 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001474 if (value_can_be_null) {
1475 __ Beqzc(value, &done);
1476 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001477 __ LoadFromOffset(kLoadDoubleword,
1478 card,
1479 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001480 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001481 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1482 __ Daddu(temp, card, temp);
1483 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001484 if (value_can_be_null) {
1485 __ Bind(&done);
1486 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001487}
1488
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001489template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001490inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1491 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001492 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001493 for (const PcRelativePatchInfo& info : infos) {
1494 const DexFile& dex_file = info.target_dex_file;
1495 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001496 DCHECK(info.label.IsBound());
1497 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1498 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1499 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1500 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001501 }
1502}
1503
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001504void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001505 DCHECK(linker_patches->empty());
1506 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001507 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001508 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001509 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001510 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001511 pc_relative_string_patches_.size() +
1512 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001513 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001514 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001515 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1516 pc_relative_method_patches_, linker_patches);
1517 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1518 pc_relative_type_patches_, linker_patches);
1519 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1520 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001521 } else {
1522 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001523 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1524 pc_relative_type_patches_, linker_patches);
1525 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1526 pc_relative_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001527 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001528 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1529 method_bss_entry_patches_, linker_patches);
1530 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1531 type_bss_entry_patches_, linker_patches);
1532 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1533 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001534 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001535}
1536
Vladimir Marko65979462017-05-19 17:25:12 +01001537CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001538 MethodReference target_method,
1539 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001540 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001541 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001542 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001543 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001544}
1545
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001546CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001547 MethodReference target_method,
1548 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001549 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001550 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001551 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001552 &method_bss_entry_patches_);
1553}
1554
Alexey Frunzef63f5692016-12-13 17:43:11 -08001555CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001556 const DexFile& dex_file,
1557 dex::TypeIndex type_index,
1558 const PcRelativePatchInfo* info_high) {
1559 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001560}
1561
Vladimir Marko1998cd02017-01-13 13:02:58 +00001562CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001563 const DexFile& dex_file,
1564 dex::TypeIndex type_index,
1565 const PcRelativePatchInfo* info_high) {
1566 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001567}
1568
Vladimir Marko65979462017-05-19 17:25:12 +01001569CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001570 const DexFile& dex_file,
1571 dex::StringIndex string_index,
1572 const PcRelativePatchInfo* info_high) {
1573 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001574}
1575
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001576CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1577 const DexFile& dex_file,
1578 dex::StringIndex string_index,
1579 const PcRelativePatchInfo* info_high) {
1580 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1581}
1582
Alexey Frunze19f6c692016-11-30 19:19:55 -08001583CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001584 const DexFile& dex_file,
1585 uint32_t offset_or_index,
1586 const PcRelativePatchInfo* info_high,
1587 ArenaDeque<PcRelativePatchInfo>* patches) {
1588 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001589 return &patches->back();
1590}
1591
Alexey Frunzef63f5692016-12-13 17:43:11 -08001592Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1593 return map->GetOrCreate(
1594 value,
1595 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1596}
1597
Alexey Frunze19f6c692016-11-30 19:19:55 -08001598Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1599 return uint64_literals_.GetOrCreate(
1600 value,
1601 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1602}
1603
Alexey Frunzef63f5692016-12-13 17:43:11 -08001604Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001605 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001606}
1607
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001608void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1609 GpuRegister out,
1610 PcRelativePatchInfo* info_low) {
1611 DCHECK(!info_high->patch_info_high);
1612 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001613 // Add the high half of a 32-bit offset to PC.
1614 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001615 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001616 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001617 if (info_low != nullptr) {
1618 DCHECK_EQ(info_low->patch_info_high, info_high);
1619 __ Bind(&info_low->label);
1620 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001621}
1622
Alexey Frunze627c1a02017-01-30 19:28:14 -08001623Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1624 dex::StringIndex string_index,
1625 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001626 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001627 return jit_string_patches_.GetOrCreate(
1628 StringReference(&dex_file, string_index),
1629 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1630}
1631
1632Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1633 dex::TypeIndex type_index,
1634 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001635 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001636 return jit_class_patches_.GetOrCreate(
1637 TypeReference(&dex_file, type_index),
1638 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1639}
1640
1641void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1642 const uint8_t* roots_data,
1643 const Literal* literal,
1644 uint64_t index_in_table) const {
1645 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1646 uintptr_t address =
1647 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1648 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1649}
1650
1651void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1652 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001653 const StringReference& string_reference = entry.first;
1654 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001655 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001656 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001657 }
1658 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001659 const TypeReference& type_reference = entry.first;
1660 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001661 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001662 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001663 }
1664}
1665
David Brazdil58282f42016-01-14 12:45:10 +00001666void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001667 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1668 blocked_core_registers_[ZERO] = true;
1669 blocked_core_registers_[K0] = true;
1670 blocked_core_registers_[K1] = true;
1671 blocked_core_registers_[GP] = true;
1672 blocked_core_registers_[SP] = true;
1673 blocked_core_registers_[RA] = true;
1674
Lazar Trsicd9672662015-09-03 17:33:01 +02001675 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1676 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001677 blocked_core_registers_[AT] = true;
1678 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001679 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001680 blocked_fpu_registers_[FTMP] = true;
1681
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001682 if (GetInstructionSetFeatures().HasMsa()) {
1683 // To be used just for MSA instructions.
1684 blocked_fpu_registers_[FTMP2] = true;
1685 }
1686
Alexey Frunze4dda3372015-06-01 18:31:49 -07001687 // Reserve suspend and thread registers.
1688 blocked_core_registers_[S0] = true;
1689 blocked_core_registers_[TR] = true;
1690
1691 // Reserve T9 for function calls
1692 blocked_core_registers_[T9] = true;
1693
Goran Jakovljevic782be112016-06-21 12:39:04 +02001694 if (GetGraph()->IsDebuggable()) {
1695 // Stubs do not save callee-save floating point registers. If the graph
1696 // is debuggable, we need to deal with these registers differently. For
1697 // now, just block them.
1698 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1699 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1700 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701 }
1702}
1703
Alexey Frunze4dda3372015-06-01 18:31:49 -07001704size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1705 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001706 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001707}
1708
1709size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1710 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001711 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001712}
1713
1714size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001715 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1716 FpuRegister(reg_id),
1717 SP,
1718 stack_index);
1719 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001720}
1721
1722size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001723 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1724 FpuRegister(reg_id),
1725 SP,
1726 stack_index);
1727 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728}
1729
1730void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001731 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001732}
1733
1734void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001735 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001736}
1737
Calin Juravle175dc732015-08-25 15:42:32 +01001738void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001739 HInstruction* instruction,
1740 uint32_t dex_pc,
1741 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001742 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001743 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001744 if (EntrypointRequiresStackMap(entrypoint)) {
1745 RecordPcInfo(instruction, dex_pc, slow_path);
1746 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001747}
1748
Alexey Frunze15958152017-02-09 19:08:30 -08001749void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1750 HInstruction* instruction,
1751 SlowPathCode* slow_path) {
1752 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1753 GenerateInvokeRuntime(entry_point_offset);
1754}
1755
1756void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1757 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1758 __ Jalr(T9);
1759 __ Nop();
1760}
1761
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1763 GpuRegister class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001764 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1765 const size_t status_byte_offset =
1766 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1767 constexpr uint32_t shifted_initialized_value =
1768 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1769
1770 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1771 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001772 __ Bltuc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001773 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1774 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001775 __ Bind(slow_path->GetExitLabel());
1776}
1777
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00001778void InstructionCodeGeneratorMIPS64::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1779 GpuRegister temp) {
1780 uint32_t path_to_root = check->GetBitstringPathToRoot();
1781 uint32_t mask = check->GetBitstringMask();
1782 DCHECK(IsPowerOfTwo(mask + 1));
1783 size_t mask_bits = WhichPowerOf2(mask + 1);
1784
1785 if (mask_bits == 16u) {
1786 // Load only the bitstring part of the status word.
1787 __ LoadFromOffset(
1788 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1789 // Compare the bitstring bits using XOR.
1790 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1791 } else {
1792 // /* uint32_t */ temp = temp->status_
1793 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1794 // Compare the bitstring bits using XOR.
1795 if (IsUint<16>(path_to_root)) {
1796 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1797 } else {
1798 __ LoadConst32(TMP, path_to_root);
1799 __ Xor(temp, temp, TMP);
1800 }
1801 // Shift out bits that do not contribute to the comparison.
1802 __ Sll(temp, temp, 32 - mask_bits);
1803 }
1804}
1805
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1807 __ Sync(0); // only stype 0 is supported
1808}
1809
1810void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1811 HBasicBlock* successor) {
1812 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001813 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1814
1815 if (slow_path == nullptr) {
1816 slow_path =
1817 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1818 instruction->SetSlowPath(slow_path);
1819 codegen_->AddSlowPath(slow_path);
1820 if (successor != nullptr) {
1821 DCHECK(successor->IsLoopHeader());
1822 }
1823 } else {
1824 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1825 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001826
1827 __ LoadFromOffset(kLoadUnsignedHalfword,
1828 TMP,
1829 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001830 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001831 if (successor == nullptr) {
1832 __ Bnezc(TMP, slow_path->GetEntryLabel());
1833 __ Bind(slow_path->GetReturnLabel());
1834 } else {
1835 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001836 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001837 // slow_path will return to GetLabelOf(successor).
1838 }
1839}
1840
1841InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1842 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001843 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001844 assembler_(codegen->GetAssembler()),
1845 codegen_(codegen) {}
1846
1847void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1848 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001849 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001850 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001851 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001852 case DataType::Type::kInt32:
1853 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001854 locations->SetInAt(0, Location::RequiresRegister());
1855 HInstruction* right = instruction->InputAt(1);
1856 bool can_use_imm = false;
1857 if (right->IsConstant()) {
1858 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1859 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1860 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001861 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001862 DCHECK(instruction->IsAdd() || instruction->IsSub());
1863 bool single_use = right->GetUses().HasExactlyOneElement();
1864 if (instruction->IsSub()) {
1865 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1866 imm = -imm;
1867 }
1868 }
1869 if (type == DataType::Type::kInt32) {
1870 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1871 } else {
1872 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1873 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001874 }
1875 }
1876 if (can_use_imm)
1877 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1878 else
1879 locations->SetInAt(1, Location::RequiresRegister());
1880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1881 }
1882 break;
1883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001884 case DataType::Type::kFloat32:
1885 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001886 locations->SetInAt(0, Location::RequiresFpuRegister());
1887 locations->SetInAt(1, Location::RequiresFpuRegister());
1888 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1889 break;
1890
1891 default:
1892 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1893 }
1894}
1895
1896void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001897 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001898 LocationSummary* locations = instruction->GetLocations();
1899
1900 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001901 case DataType::Type::kInt32:
1902 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001903 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1904 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1905 Location rhs_location = locations->InAt(1);
1906
1907 GpuRegister rhs_reg = ZERO;
1908 int64_t rhs_imm = 0;
1909 bool use_imm = rhs_location.IsConstant();
1910 if (use_imm) {
1911 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1912 } else {
1913 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1914 }
1915
1916 if (instruction->IsAnd()) {
1917 if (use_imm)
1918 __ Andi(dst, lhs, rhs_imm);
1919 else
1920 __ And(dst, lhs, rhs_reg);
1921 } else if (instruction->IsOr()) {
1922 if (use_imm)
1923 __ Ori(dst, lhs, rhs_imm);
1924 else
1925 __ Or(dst, lhs, rhs_reg);
1926 } else if (instruction->IsXor()) {
1927 if (use_imm)
1928 __ Xori(dst, lhs, rhs_imm);
1929 else
1930 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001931 } else if (instruction->IsAdd() || instruction->IsSub()) {
1932 if (instruction->IsSub()) {
1933 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001934 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001936 if (use_imm) {
1937 if (IsInt<16>(rhs_imm)) {
1938 __ Addiu(dst, lhs, rhs_imm);
1939 } else {
1940 int16_t rhs_imm_high = High16Bits(rhs_imm);
1941 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1942 if (rhs_imm_low < 0) {
1943 rhs_imm_high += 1;
1944 }
1945 __ Aui(dst, lhs, rhs_imm_high);
1946 if (rhs_imm_low != 0) {
1947 __ Addiu(dst, dst, rhs_imm_low);
1948 }
1949 }
1950 } else {
1951 if (instruction->IsAdd()) {
1952 __ Addu(dst, lhs, rhs_reg);
1953 } else {
1954 DCHECK(instruction->IsSub());
1955 __ Subu(dst, lhs, rhs_reg);
1956 }
1957 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001958 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001959 if (use_imm) {
1960 if (IsInt<16>(rhs_imm)) {
1961 __ Daddiu(dst, lhs, rhs_imm);
1962 } else if (IsInt<32>(rhs_imm)) {
1963 int16_t rhs_imm_high = High16Bits(rhs_imm);
1964 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1965 bool overflow_hi16 = false;
1966 if (rhs_imm_low < 0) {
1967 rhs_imm_high += 1;
1968 overflow_hi16 = (rhs_imm_high == -32768);
1969 }
1970 __ Daui(dst, lhs, rhs_imm_high);
1971 if (rhs_imm_low != 0) {
1972 __ Daddiu(dst, dst, rhs_imm_low);
1973 }
1974 if (overflow_hi16) {
1975 __ Dahi(dst, 1);
1976 }
1977 } else {
1978 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1979 if (rhs_imm_low < 0) {
1980 rhs_imm += (INT64_C(1) << 16);
1981 }
1982 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
1983 if (rhs_imm_upper < 0) {
1984 rhs_imm += (INT64_C(1) << 32);
1985 }
1986 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
1987 if (rhs_imm_high < 0) {
1988 rhs_imm += (INT64_C(1) << 48);
1989 }
1990 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
1991 GpuRegister tmp = lhs;
1992 if (rhs_imm_low != 0) {
1993 __ Daddiu(dst, tmp, rhs_imm_low);
1994 tmp = dst;
1995 }
1996 // Dahi and Dati must use the same input and output register, so we have to initialize
1997 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
1998 // Daui(dst, lhs, 0).
1999 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
2000 __ Daui(dst, tmp, rhs_imm_upper);
2001 }
2002 if (rhs_imm_high != 0) {
2003 __ Dahi(dst, rhs_imm_high);
2004 }
2005 if (rhs_imm_top != 0) {
2006 __ Dati(dst, rhs_imm_top);
2007 }
2008 }
2009 } else if (instruction->IsAdd()) {
2010 __ Daddu(dst, lhs, rhs_reg);
2011 } else {
2012 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002013 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002014 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002015 }
2016 }
2017 break;
2018 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002019 case DataType::Type::kFloat32:
2020 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2022 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2023 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2024 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026 __ AddS(dst, lhs, rhs);
2027 else
2028 __ AddD(dst, lhs, rhs);
2029 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002031 __ SubS(dst, lhs, rhs);
2032 else
2033 __ SubD(dst, lhs, rhs);
2034 } else {
2035 LOG(FATAL) << "Unexpected floating-point binary operation";
2036 }
2037 break;
2038 }
2039 default:
2040 LOG(FATAL) << "Unexpected binary operation type " << type;
2041 }
2042}
2043
2044void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002045 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002046
Vladimir Markoca6fff82017-10-03 14:49:14 +01002047 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002049 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002050 case DataType::Type::kInt32:
2051 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002052 locations->SetInAt(0, Location::RequiresRegister());
2053 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002054 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002055 break;
2056 }
2057 default:
2058 LOG(FATAL) << "Unexpected shift type " << type;
2059 }
2060}
2061
2062void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002063 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002064 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002066
2067 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 case DataType::Type::kInt32:
2069 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002070 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2071 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2072 Location rhs_location = locations->InAt(1);
2073
2074 GpuRegister rhs_reg = ZERO;
2075 int64_t rhs_imm = 0;
2076 bool use_imm = rhs_location.IsConstant();
2077 if (use_imm) {
2078 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2079 } else {
2080 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2081 }
2082
2083 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002084 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002085 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002086
Alexey Frunze92d90602015-12-18 18:16:36 -08002087 if (shift_value == 0) {
2088 if (dst != lhs) {
2089 __ Move(dst, lhs);
2090 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002092 if (instr->IsShl()) {
2093 __ Sll(dst, lhs, shift_value);
2094 } else if (instr->IsShr()) {
2095 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002096 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002097 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002098 } else {
2099 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002100 }
2101 } else {
2102 if (shift_value < 32) {
2103 if (instr->IsShl()) {
2104 __ Dsll(dst, lhs, shift_value);
2105 } else if (instr->IsShr()) {
2106 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002107 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002108 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002109 } else {
2110 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 }
2112 } else {
2113 shift_value -= 32;
2114 if (instr->IsShl()) {
2115 __ Dsll32(dst, lhs, shift_value);
2116 } else if (instr->IsShr()) {
2117 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002118 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002120 } else {
2121 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122 }
2123 }
2124 }
2125 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002127 if (instr->IsShl()) {
2128 __ Sllv(dst, lhs, rhs_reg);
2129 } else if (instr->IsShr()) {
2130 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002131 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002132 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002133 } else {
2134 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002135 }
2136 } else {
2137 if (instr->IsShl()) {
2138 __ Dsllv(dst, lhs, rhs_reg);
2139 } else if (instr->IsShr()) {
2140 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002141 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002142 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002143 } else {
2144 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002145 }
2146 }
2147 }
2148 break;
2149 }
2150 default:
2151 LOG(FATAL) << "Unexpected shift operation type " << type;
2152 }
2153}
2154
2155void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2156 HandleBinaryOp(instruction);
2157}
2158
2159void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2160 HandleBinaryOp(instruction);
2161}
2162
2163void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2164 HandleBinaryOp(instruction);
2165}
2166
2167void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2168 HandleBinaryOp(instruction);
2169}
2170
2171void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002172 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002173 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002175 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002176 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2177 object_array_get_with_read_barrier
2178 ? LocationSummary::kCallOnSlowPath
2179 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002180 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2181 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2182 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002183 locations->SetInAt(0, Location::RequiresRegister());
2184 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002185 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2187 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002188 // The output overlaps in the case of an object array get with
2189 // read barriers enabled: we do not want the move to overwrite the
2190 // array's location, as we need it to emit the read barrier.
2191 locations->SetOut(Location::RequiresRegister(),
2192 object_array_get_with_read_barrier
2193 ? Location::kOutputOverlap
2194 : Location::kNoOutputOverlap);
2195 }
2196 // We need a temporary register for the read barrier marking slow
2197 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2198 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002199 bool temp_needed = instruction->GetIndex()->IsConstant()
2200 ? !kBakerReadBarrierThunksEnableForFields
2201 : !kBakerReadBarrierThunksEnableForArrays;
2202 if (temp_needed) {
2203 locations->AddTemp(Location::RequiresRegister());
2204 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002205 }
2206}
2207
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002208static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2209 auto null_checker = [codegen, instruction]() {
2210 codegen->MaybeRecordImplicitNullCheck(instruction);
2211 };
2212 return null_checker;
2213}
2214
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2216 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002217 Location obj_loc = locations->InAt(0);
2218 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2219 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002220 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002221 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002222 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002224 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002225 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2226 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002227 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002228 case DataType::Type::kBool:
2229 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002230 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002231 if (index.IsConstant()) {
2232 size_t offset =
2233 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002234 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002235 } else {
2236 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002237 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002238 }
2239 break;
2240 }
2241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002243 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002244 if (index.IsConstant()) {
2245 size_t offset =
2246 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002247 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002248 } else {
2249 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002250 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002251 }
2252 break;
2253 }
2254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002255 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002256 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002257 if (maybe_compressed_char_at) {
2258 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002259 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002260 __ Dext(TMP, TMP, 0, 1);
2261 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2262 "Expecting 0=compressed, 1=uncompressed");
2263 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002264 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002265 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2266 if (maybe_compressed_char_at) {
2267 Mips64Label uncompressed_load, done;
2268 __ Bnezc(TMP, &uncompressed_load);
2269 __ LoadFromOffset(kLoadUnsignedByte,
2270 out,
2271 obj,
2272 data_offset + (const_index << TIMES_1));
2273 __ Bc(&done);
2274 __ Bind(&uncompressed_load);
2275 __ LoadFromOffset(kLoadUnsignedHalfword,
2276 out,
2277 obj,
2278 data_offset + (const_index << TIMES_2));
2279 __ Bind(&done);
2280 } else {
2281 __ LoadFromOffset(kLoadUnsignedHalfword,
2282 out,
2283 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002284 data_offset + (const_index << TIMES_2),
2285 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002286 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002287 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002288 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2289 if (maybe_compressed_char_at) {
2290 Mips64Label uncompressed_load, done;
2291 __ Bnezc(TMP, &uncompressed_load);
2292 __ Daddu(TMP, obj, index_reg);
2293 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2294 __ Bc(&done);
2295 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002296 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002297 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2298 __ Bind(&done);
2299 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002300 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002301 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002302 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002303 }
2304 break;
2305 }
2306
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002307 case DataType::Type::kInt16: {
2308 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2309 if (index.IsConstant()) {
2310 size_t offset =
2311 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2312 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2313 } else {
2314 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2315 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2316 }
2317 break;
2318 }
2319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002321 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002322 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002323 LoadOperandType load_type =
2324 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002325 if (index.IsConstant()) {
2326 size_t offset =
2327 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002328 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002329 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002330 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002331 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002332 }
2333 break;
2334 }
2335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002337 static_assert(
2338 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2339 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2340 // /* HeapReference<Object> */ out =
2341 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2342 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002343 bool temp_needed = index.IsConstant()
2344 ? !kBakerReadBarrierThunksEnableForFields
2345 : !kBakerReadBarrierThunksEnableForArrays;
2346 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002347 // Note that a potential implicit null check is handled in this
2348 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002349 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2350 if (index.IsConstant()) {
2351 // Array load with a constant index can be treated as a field load.
2352 size_t offset =
2353 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2354 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2355 out_loc,
2356 obj,
2357 offset,
2358 temp,
2359 /* needs_null_check */ false);
2360 } else {
2361 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2362 out_loc,
2363 obj,
2364 data_offset,
2365 index,
2366 temp,
2367 /* needs_null_check */ false);
2368 }
Alexey Frunze15958152017-02-09 19:08:30 -08002369 } else {
2370 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2371 if (index.IsConstant()) {
2372 size_t offset =
2373 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2374 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2375 // If read barriers are enabled, emit read barriers other than
2376 // Baker's using a slow path (and also unpoison the loaded
2377 // reference, if heap poisoning is enabled).
2378 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2379 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002380 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002381 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2382 // If read barriers are enabled, emit read barriers other than
2383 // Baker's using a slow path (and also unpoison the loaded
2384 // reference, if heap poisoning is enabled).
2385 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2386 out_loc,
2387 out_loc,
2388 obj_loc,
2389 data_offset,
2390 index);
2391 }
2392 }
2393 break;
2394 }
2395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002396 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002397 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002398 if (index.IsConstant()) {
2399 size_t offset =
2400 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002401 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002402 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002403 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002404 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002405 }
2406 break;
2407 }
2408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002410 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002411 if (index.IsConstant()) {
2412 size_t offset =
2413 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002414 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002415 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002416 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002417 __ LoadFpuFromOffset(kLoadWord, out, TMP, 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::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002423 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002424 if (index.IsConstant()) {
2425 size_t offset =
2426 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002427 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002428 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002429 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002430 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002431 }
2432 break;
2433 }
2434
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002435 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002436 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2437 UNREACHABLE();
2438 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002439}
2440
2441void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002442 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002443 locations->SetInAt(0, Location::RequiresRegister());
2444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2445}
2446
2447void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2448 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002449 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002450 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2451 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2452 __ LoadFromOffset(kLoadWord, out, obj, offset);
2453 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002454 // Mask out compression flag from String's array length.
2455 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2456 __ Srl(out, out, 1u);
2457 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002458}
2459
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002460Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2461 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2462 ? Location::ConstantLocation(instruction->AsConstant())
2463 : Location::RequiresRegister();
2464}
2465
2466Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2467 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2468 // We can store a non-zero float or double constant without first loading it into the FPU,
2469 // but we should only prefer this if the constant has a single use.
2470 if (instruction->IsConstant() &&
2471 (instruction->AsConstant()->IsZeroBitPattern() ||
2472 instruction->GetUses().HasExactlyOneElement())) {
2473 return Location::ConstantLocation(instruction->AsConstant());
2474 // Otherwise fall through and require an FPU register for the constant.
2475 }
2476 return Location::RequiresFpuRegister();
2477}
2478
Alexey Frunze4dda3372015-06-01 18:31:49 -07002479void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002481
2482 bool needs_write_barrier =
2483 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2484 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2485
Vladimir Markoca6fff82017-10-03 14:49:14 +01002486 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002487 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002488 may_need_runtime_call_for_type_check ?
2489 LocationSummary::kCallOnSlowPath :
2490 LocationSummary::kNoCall);
2491
2492 locations->SetInAt(0, Location::RequiresRegister());
2493 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002495 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002496 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002497 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2498 }
2499 if (needs_write_barrier) {
2500 // Temporary register for the write barrier.
2501 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002502 }
2503}
2504
2505void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2506 LocationSummary* locations = instruction->GetLocations();
2507 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2508 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002509 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002511 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002512 bool needs_write_barrier =
2513 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002514 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002515 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002516
2517 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002519 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002522 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002523 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002524 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002525 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2526 }
2527 if (value_location.IsConstant()) {
2528 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2529 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2530 } else {
2531 GpuRegister value = value_location.AsRegister<GpuRegister>();
2532 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002533 }
2534 break;
2535 }
2536
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002537 case DataType::Type::kUint16:
2538 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002539 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002540 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002541 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002543 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002544 }
2545 if (value_location.IsConstant()) {
2546 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2547 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2548 } else {
2549 GpuRegister value = value_location.AsRegister<GpuRegister>();
2550 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002551 }
2552 break;
2553 }
2554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002556 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2557 if (index.IsConstant()) {
2558 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2559 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002560 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002561 }
2562 if (value_location.IsConstant()) {
2563 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2564 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2565 } else {
2566 GpuRegister value = value_location.AsRegister<GpuRegister>();
2567 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2568 }
2569 break;
2570 }
2571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002573 if (value_location.IsConstant()) {
2574 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002575 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002576 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002577 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002579 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002580 }
Alexey Frunze15958152017-02-09 19:08:30 -08002581 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2582 DCHECK_EQ(value, 0);
2583 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2584 DCHECK(!needs_write_barrier);
2585 DCHECK(!may_need_runtime_call_for_type_check);
2586 break;
2587 }
2588
2589 DCHECK(needs_write_barrier);
2590 GpuRegister value = value_location.AsRegister<GpuRegister>();
2591 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2592 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2593 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2594 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2595 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2596 Mips64Label done;
2597 SlowPathCodeMIPS64* slow_path = nullptr;
2598
2599 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002600 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002601 codegen_->AddSlowPath(slow_path);
2602 if (instruction->GetValueCanBeNull()) {
2603 Mips64Label non_zero;
2604 __ Bnezc(value, &non_zero);
2605 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2606 if (index.IsConstant()) {
2607 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002608 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002609 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002610 }
Alexey Frunze15958152017-02-09 19:08:30 -08002611 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2612 __ Bc(&done);
2613 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002614 }
Alexey Frunze15958152017-02-09 19:08:30 -08002615
2616 // Note that when read barriers are enabled, the type checks
2617 // are performed without read barriers. This is fine, even in
2618 // the case where a class object is in the from-space after
2619 // the flip, as a comparison involving such a type would not
2620 // produce a false positive; it may of course produce a false
2621 // negative, in which case we would take the ArraySet slow
2622 // path.
2623
2624 // /* HeapReference<Class> */ temp1 = obj->klass_
2625 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2626 __ MaybeUnpoisonHeapReference(temp1);
2627
2628 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2629 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2630 // /* HeapReference<Class> */ temp2 = value->klass_
2631 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2632 // If heap poisoning is enabled, no need to unpoison `temp1`
2633 // nor `temp2`, as we are comparing two poisoned references.
2634
2635 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2636 Mips64Label do_put;
2637 __ Beqc(temp1, temp2, &do_put);
2638 // If heap poisoning is enabled, the `temp1` reference has
2639 // not been unpoisoned yet; unpoison it now.
2640 __ MaybeUnpoisonHeapReference(temp1);
2641
2642 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2643 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2644 // If heap poisoning is enabled, no need to unpoison
2645 // `temp1`, as we are comparing against null below.
2646 __ Bnezc(temp1, slow_path->GetEntryLabel());
2647 __ Bind(&do_put);
2648 } else {
2649 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2650 }
2651 }
2652
2653 GpuRegister source = value;
2654 if (kPoisonHeapReferences) {
2655 // Note that in the case where `value` is a null reference,
2656 // we do not enter this block, as a null reference does not
2657 // need poisoning.
2658 __ Move(temp1, value);
2659 __ PoisonHeapReference(temp1);
2660 source = temp1;
2661 }
2662
2663 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2664 if (index.IsConstant()) {
2665 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002666 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002667 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002668 }
2669 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2670
2671 if (!may_need_runtime_call_for_type_check) {
2672 codegen_->MaybeRecordImplicitNullCheck(instruction);
2673 }
2674
2675 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2676
2677 if (done.IsLinked()) {
2678 __ Bind(&done);
2679 }
2680
2681 if (slow_path != nullptr) {
2682 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002683 }
2684 break;
2685 }
2686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002688 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002690 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002691 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002692 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002693 }
2694 if (value_location.IsConstant()) {
2695 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2696 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2697 } else {
2698 GpuRegister value = value_location.AsRegister<GpuRegister>();
2699 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002700 }
2701 break;
2702 }
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002705 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002706 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002707 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002708 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002709 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002710 }
2711 if (value_location.IsConstant()) {
2712 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2713 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2714 } else {
2715 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2716 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002717 }
2718 break;
2719 }
2720
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002722 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002723 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002724 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002725 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002726 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002727 }
2728 if (value_location.IsConstant()) {
2729 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2730 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2731 } else {
2732 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2733 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002734 }
2735 break;
2736 }
2737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002739 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2740 UNREACHABLE();
2741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002742}
2743
2744void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002745 RegisterSet caller_saves = RegisterSet::Empty();
2746 InvokeRuntimeCallingConvention calling_convention;
2747 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2748 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2749 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002750
2751 HInstruction* index = instruction->InputAt(0);
2752 HInstruction* length = instruction->InputAt(1);
2753
2754 bool const_index = false;
2755 bool const_length = false;
2756
2757 if (index->IsConstant()) {
2758 if (length->IsConstant()) {
2759 const_index = true;
2760 const_length = true;
2761 } else {
2762 int32_t index_value = index->AsIntConstant()->GetValue();
2763 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2764 const_index = true;
2765 }
2766 }
2767 } else if (length->IsConstant()) {
2768 int32_t length_value = length->AsIntConstant()->GetValue();
2769 if (IsUint<15>(length_value)) {
2770 const_length = true;
2771 }
2772 }
2773
2774 locations->SetInAt(0, const_index
2775 ? Location::ConstantLocation(index->AsConstant())
2776 : Location::RequiresRegister());
2777 locations->SetInAt(1, const_length
2778 ? Location::ConstantLocation(length->AsConstant())
2779 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002780}
2781
2782void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2783 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002784 Location index_loc = locations->InAt(0);
2785 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002786
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002787 if (length_loc.IsConstant()) {
2788 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2789 if (index_loc.IsConstant()) {
2790 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2791 if (index < 0 || index >= length) {
2792 BoundsCheckSlowPathMIPS64* slow_path =
2793 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2794 codegen_->AddSlowPath(slow_path);
2795 __ Bc(slow_path->GetEntryLabel());
2796 } else {
2797 // Nothing to be done.
2798 }
2799 return;
2800 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002801
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002802 BoundsCheckSlowPathMIPS64* slow_path =
2803 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2804 codegen_->AddSlowPath(slow_path);
2805 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2806 if (length == 0) {
2807 __ Bc(slow_path->GetEntryLabel());
2808 } else if (length == 1) {
2809 __ Bnezc(index, slow_path->GetEntryLabel());
2810 } else {
2811 DCHECK(IsUint<15>(length)) << length;
2812 __ Sltiu(TMP, index, length);
2813 __ Beqzc(TMP, slow_path->GetEntryLabel());
2814 }
2815 } else {
2816 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2817 BoundsCheckSlowPathMIPS64* slow_path =
2818 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2819 codegen_->AddSlowPath(slow_path);
2820 if (index_loc.IsConstant()) {
2821 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2822 if (index < 0) {
2823 __ Bc(slow_path->GetEntryLabel());
2824 } else if (index == 0) {
2825 __ Blezc(length, slow_path->GetEntryLabel());
2826 } else {
2827 DCHECK(IsInt<16>(index + 1)) << index;
2828 __ Sltiu(TMP, length, index + 1);
2829 __ Bnezc(TMP, slow_path->GetEntryLabel());
2830 }
2831 } else {
2832 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2833 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2834 }
2835 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002836}
2837
Alexey Frunze15958152017-02-09 19:08:30 -08002838// Temp is used for read barrier.
2839static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2840 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002841 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002842 (kUseBakerReadBarrier ||
2843 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2844 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2845 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2846 return 1;
2847 }
2848 return 0;
2849}
2850
2851// Extra temp is used for read barrier.
2852static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2853 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2854}
2855
Alexey Frunze4dda3372015-06-01 18:31:49 -07002856void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002857 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2858 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2859
2860 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2861 switch (type_check_kind) {
2862 case TypeCheckKind::kExactCheck:
2863 case TypeCheckKind::kAbstractClassCheck:
2864 case TypeCheckKind::kClassHierarchyCheck:
2865 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002866 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002867 ? LocationSummary::kCallOnSlowPath
2868 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2869 break;
2870 case TypeCheckKind::kArrayCheck:
2871 case TypeCheckKind::kUnresolvedCheck:
2872 case TypeCheckKind::kInterfaceCheck:
2873 call_kind = LocationSummary::kCallOnSlowPath;
2874 break;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00002875 case TypeCheckKind::kBitstringCheck:
2876 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002877 }
2878
Vladimir Markoca6fff82017-10-03 14:49:14 +01002879 LocationSummary* locations =
2880 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002881 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00002882 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
2883 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2884 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2885 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
2886 } else {
2887 locations->SetInAt(1, Location::RequiresRegister());
2888 }
Alexey Frunze15958152017-02-09 19:08:30 -08002889 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002890}
2891
2892void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002893 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002894 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002895 Location obj_loc = locations->InAt(0);
2896 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00002897 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08002898 Location temp_loc = locations->GetTemp(0);
2899 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2900 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2901 DCHECK_LE(num_temps, 2u);
2902 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002903 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2904 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2905 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2906 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2907 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2908 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2909 const uint32_t object_array_data_offset =
2910 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2911 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002912
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002913 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2914 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2915 // read barriers is done for performance and code size reasons.
2916 bool is_type_check_slow_path_fatal = false;
2917 if (!kEmitCompilerReadBarrier) {
2918 is_type_check_slow_path_fatal =
2919 (type_check_kind == TypeCheckKind::kExactCheck ||
2920 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2921 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2922 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2923 !instruction->CanThrowIntoCatchBlock();
2924 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002925 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002926 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2927 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002928 codegen_->AddSlowPath(slow_path);
2929
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002930 // Avoid this check if we know `obj` is not null.
2931 if (instruction->MustDoNullCheck()) {
2932 __ Beqzc(obj, &done);
2933 }
2934
2935 switch (type_check_kind) {
2936 case TypeCheckKind::kExactCheck:
2937 case TypeCheckKind::kArrayCheck: {
2938 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002939 GenerateReferenceLoadTwoRegisters(instruction,
2940 temp_loc,
2941 obj_loc,
2942 class_offset,
2943 maybe_temp2_loc,
2944 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002945 // Jump to slow path for throwing the exception or doing a
2946 // more involved array check.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00002947 __ Bnec(temp, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002948 break;
2949 }
2950
2951 case TypeCheckKind::kAbstractClassCheck: {
2952 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002953 GenerateReferenceLoadTwoRegisters(instruction,
2954 temp_loc,
2955 obj_loc,
2956 class_offset,
2957 maybe_temp2_loc,
2958 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002959 // If the class is abstract, we eagerly fetch the super class of the
2960 // object to avoid doing a comparison we know will fail.
2961 Mips64Label loop;
2962 __ Bind(&loop);
2963 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002964 GenerateReferenceLoadOneRegister(instruction,
2965 temp_loc,
2966 super_offset,
2967 maybe_temp2_loc,
2968 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002969 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2970 // exception.
2971 __ Beqzc(temp, slow_path->GetEntryLabel());
2972 // Otherwise, compare the classes.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00002973 __ Bnec(temp, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002974 break;
2975 }
2976
2977 case TypeCheckKind::kClassHierarchyCheck: {
2978 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002979 GenerateReferenceLoadTwoRegisters(instruction,
2980 temp_loc,
2981 obj_loc,
2982 class_offset,
2983 maybe_temp2_loc,
2984 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002985 // Walk over the class hierarchy to find a match.
2986 Mips64Label loop;
2987 __ Bind(&loop);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00002988 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002989 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002990 GenerateReferenceLoadOneRegister(instruction,
2991 temp_loc,
2992 super_offset,
2993 maybe_temp2_loc,
2994 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002995 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2996 // exception. Otherwise, jump to the beginning of the loop.
2997 __ Bnezc(temp, &loop);
2998 __ Bc(slow_path->GetEntryLabel());
2999 break;
3000 }
3001
3002 case TypeCheckKind::kArrayObjectCheck: {
3003 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003004 GenerateReferenceLoadTwoRegisters(instruction,
3005 temp_loc,
3006 obj_loc,
3007 class_offset,
3008 maybe_temp2_loc,
3009 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003010 // Do an exact check.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003011 __ Beqc(temp, cls.AsRegister<GpuRegister>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003012 // Otherwise, we need to check that the object's class is a non-primitive array.
3013 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003014 GenerateReferenceLoadOneRegister(instruction,
3015 temp_loc,
3016 component_offset,
3017 maybe_temp2_loc,
3018 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003019 // If the component type is null, jump to the slow path to throw the exception.
3020 __ Beqzc(temp, slow_path->GetEntryLabel());
3021 // Otherwise, the object is indeed an array, further check that this component
3022 // type is not a primitive type.
3023 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3024 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3025 __ Bnezc(temp, slow_path->GetEntryLabel());
3026 break;
3027 }
3028
3029 case TypeCheckKind::kUnresolvedCheck:
3030 // We always go into the type check slow path for the unresolved check case.
3031 // We cannot directly call the CheckCast runtime entry point
3032 // without resorting to a type checking slow path here (i.e. by
3033 // calling InvokeRuntime directly), as it would require to
3034 // assign fixed registers for the inputs of this HInstanceOf
3035 // instruction (following the runtime calling convention), which
3036 // might be cluttered by the potential first read barrier
3037 // emission at the beginning of this method.
3038 __ Bc(slow_path->GetEntryLabel());
3039 break;
3040
3041 case TypeCheckKind::kInterfaceCheck: {
3042 // Avoid read barriers to improve performance of the fast path. We can not get false
3043 // positives by doing this.
3044 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003045 GenerateReferenceLoadTwoRegisters(instruction,
3046 temp_loc,
3047 obj_loc,
3048 class_offset,
3049 maybe_temp2_loc,
3050 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003051 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003052 GenerateReferenceLoadTwoRegisters(instruction,
3053 temp_loc,
3054 temp_loc,
3055 iftable_offset,
3056 maybe_temp2_loc,
3057 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003058 // Iftable is never null.
3059 __ Lw(TMP, temp, array_length_offset);
3060 // Loop through the iftable and check if any class matches.
3061 Mips64Label loop;
3062 __ Bind(&loop);
3063 __ Beqzc(TMP, slow_path->GetEntryLabel());
3064 __ Lwu(AT, temp, object_array_data_offset);
3065 __ MaybeUnpoisonHeapReference(AT);
3066 // Go to next interface.
3067 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3068 __ Addiu(TMP, TMP, -2);
3069 // Compare the classes and continue the loop if they do not match.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00003070 __ Bnec(AT, cls.AsRegister<GpuRegister>(), &loop);
3071 break;
3072 }
3073
3074 case TypeCheckKind::kBitstringCheck: {
3075 // /* HeapReference<Class> */ temp = obj->klass_
3076 GenerateReferenceLoadTwoRegisters(instruction,
3077 temp_loc,
3078 obj_loc,
3079 class_offset,
3080 maybe_temp2_loc,
3081 kWithoutReadBarrier);
3082
3083 GenerateBitstringTypeCheckCompare(instruction, temp);
3084 __ Bnezc(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003085 break;
3086 }
3087 }
3088
3089 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003090 __ Bind(slow_path->GetExitLabel());
3091}
3092
3093void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3094 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003095 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003096 locations->SetInAt(0, Location::RequiresRegister());
3097 if (check->HasUses()) {
3098 locations->SetOut(Location::SameAsFirstInput());
3099 }
3100}
3101
3102void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3103 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003104 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003105 check->GetLoadClass(),
3106 check,
3107 check->GetDexPc(),
3108 true);
3109 codegen_->AddSlowPath(slow_path);
3110 GenerateClassInitializationCheck(slow_path,
3111 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3112}
3113
3114void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003115 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003116
Vladimir Markoca6fff82017-10-03 14:49:14 +01003117 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003118
3119 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003121 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003124 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kInt32:
3126 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003127 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003128 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003129 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3130 break;
3131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 case DataType::Type::kFloat32:
3133 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003134 locations->SetInAt(0, Location::RequiresFpuRegister());
3135 locations->SetInAt(1, Location::RequiresFpuRegister());
3136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003137 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003138
3139 default:
3140 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3141 }
3142}
3143
3144void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3145 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003146 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003148
3149 // 0 if: left == right
3150 // 1 if: left > right
3151 // -1 if: left < right
3152 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003154 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003155 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003156 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003157 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003158 case DataType::Type::kInt32:
3159 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003160 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003161 Location rhs_location = locations->InAt(1);
3162 bool use_imm = rhs_location.IsConstant();
3163 GpuRegister rhs = ZERO;
3164 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003166 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3167 if (value != 0) {
3168 rhs = AT;
3169 __ LoadConst64(rhs, value);
3170 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003171 } else {
3172 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3173 if (value != 0) {
3174 rhs = AT;
3175 __ LoadConst32(rhs, value);
3176 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003177 }
3178 } else {
3179 rhs = rhs_location.AsRegister<GpuRegister>();
3180 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003181 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003182 __ Slt(res, rhs, lhs);
3183 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003184 break;
3185 }
3186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003187 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003188 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3189 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3190 Mips64Label done;
3191 __ CmpEqS(FTMP, lhs, rhs);
3192 __ LoadConst32(res, 0);
3193 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003194 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003195 __ CmpLtS(FTMP, lhs, rhs);
3196 __ LoadConst32(res, -1);
3197 __ Bc1nez(FTMP, &done);
3198 __ LoadConst32(res, 1);
3199 } else {
3200 __ CmpLtS(FTMP, rhs, lhs);
3201 __ LoadConst32(res, 1);
3202 __ Bc1nez(FTMP, &done);
3203 __ LoadConst32(res, -1);
3204 }
3205 __ Bind(&done);
3206 break;
3207 }
3208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003210 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3211 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3212 Mips64Label done;
3213 __ CmpEqD(FTMP, lhs, rhs);
3214 __ LoadConst32(res, 0);
3215 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003216 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003217 __ CmpLtD(FTMP, lhs, rhs);
3218 __ LoadConst32(res, -1);
3219 __ Bc1nez(FTMP, &done);
3220 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003221 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003222 __ CmpLtD(FTMP, rhs, lhs);
3223 __ LoadConst32(res, 1);
3224 __ Bc1nez(FTMP, &done);
3225 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003226 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003227 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003228 break;
3229 }
3230
3231 default:
3232 LOG(FATAL) << "Unimplemented compare type " << in_type;
3233 }
3234}
3235
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003236void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003237 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003238 switch (instruction->InputAt(0)->GetType()) {
3239 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003241 locations->SetInAt(0, Location::RequiresRegister());
3242 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3243 break;
3244
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003245 case DataType::Type::kFloat32:
3246 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003247 locations->SetInAt(0, Location::RequiresFpuRegister());
3248 locations->SetInAt(1, Location::RequiresFpuRegister());
3249 break;
3250 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003251 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3253 }
3254}
3255
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003256void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003257 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003258 return;
3259 }
3260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003261 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003262 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003263 switch (type) {
3264 default:
3265 // Integer case.
3266 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3267 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003268 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003269 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3270 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003271 case DataType::Type::kFloat32:
3272 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003273 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3274 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003275 }
3276}
3277
Alexey Frunzec857c742015-09-23 15:12:39 -07003278void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3279 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003281
3282 LocationSummary* locations = instruction->GetLocations();
3283 Location second = locations->InAt(1);
3284 DCHECK(second.IsConstant());
3285
3286 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3287 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3288 int64_t imm = Int64FromConstant(second.GetConstant());
3289 DCHECK(imm == 1 || imm == -1);
3290
3291 if (instruction->IsRem()) {
3292 __ Move(out, ZERO);
3293 } else {
3294 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003295 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003296 __ Subu(out, ZERO, dividend);
3297 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003298 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003299 __ Dsubu(out, ZERO, dividend);
3300 }
3301 } else if (out != dividend) {
3302 __ Move(out, dividend);
3303 }
3304 }
3305}
3306
3307void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3308 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003309 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003310
3311 LocationSummary* locations = instruction->GetLocations();
3312 Location second = locations->InAt(1);
3313 DCHECK(second.IsConstant());
3314
3315 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3316 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3317 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003318 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003319 int ctz_imm = CTZ(abs_imm);
3320
3321 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003322 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003323 if (ctz_imm == 1) {
3324 // Fast path for division by +/-2, which is very common.
3325 __ Srl(TMP, dividend, 31);
3326 } else {
3327 __ Sra(TMP, dividend, 31);
3328 __ Srl(TMP, TMP, 32 - ctz_imm);
3329 }
3330 __ Addu(out, dividend, TMP);
3331 __ Sra(out, out, ctz_imm);
3332 if (imm < 0) {
3333 __ Subu(out, ZERO, out);
3334 }
3335 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003336 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003337 if (ctz_imm == 1) {
3338 // Fast path for division by +/-2, which is very common.
3339 __ Dsrl32(TMP, dividend, 31);
3340 } else {
3341 __ Dsra32(TMP, dividend, 31);
3342 if (ctz_imm > 32) {
3343 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3344 } else {
3345 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3346 }
3347 }
3348 __ Daddu(out, dividend, TMP);
3349 if (ctz_imm < 32) {
3350 __ Dsra(out, out, ctz_imm);
3351 } else {
3352 __ Dsra32(out, out, ctz_imm - 32);
3353 }
3354 if (imm < 0) {
3355 __ Dsubu(out, ZERO, out);
3356 }
3357 }
3358 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003359 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003360 if (ctz_imm == 1) {
3361 // Fast path for modulo +/-2, which is very common.
3362 __ Sra(TMP, dividend, 31);
3363 __ Subu(out, dividend, TMP);
3364 __ Andi(out, out, 1);
3365 __ Addu(out, out, TMP);
3366 } else {
3367 __ Sra(TMP, dividend, 31);
3368 __ Srl(TMP, TMP, 32 - ctz_imm);
3369 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003370 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003371 __ Subu(out, out, TMP);
3372 }
3373 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003374 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003375 if (ctz_imm == 1) {
3376 // Fast path for modulo +/-2, which is very common.
3377 __ Dsra32(TMP, dividend, 31);
3378 __ Dsubu(out, dividend, TMP);
3379 __ Andi(out, out, 1);
3380 __ Daddu(out, out, TMP);
3381 } else {
3382 __ Dsra32(TMP, dividend, 31);
3383 if (ctz_imm > 32) {
3384 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3385 } else {
3386 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3387 }
3388 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003389 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003390 __ Dsubu(out, out, TMP);
3391 }
3392 }
3393 }
3394}
3395
3396void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3397 DCHECK(instruction->IsDiv() || instruction->IsRem());
3398
3399 LocationSummary* locations = instruction->GetLocations();
3400 Location second = locations->InAt(1);
3401 DCHECK(second.IsConstant());
3402
3403 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3404 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3405 int64_t imm = Int64FromConstant(second.GetConstant());
3406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003407 DataType::Type type = instruction->GetResultType();
3408 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003409
3410 int64_t magic;
3411 int shift;
3412 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003414 &magic,
3415 &shift);
3416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003418 __ LoadConst32(TMP, magic);
3419 __ MuhR6(TMP, dividend, TMP);
3420
3421 if (imm > 0 && magic < 0) {
3422 __ Addu(TMP, TMP, dividend);
3423 } else if (imm < 0 && magic > 0) {
3424 __ Subu(TMP, TMP, dividend);
3425 }
3426
3427 if (shift != 0) {
3428 __ Sra(TMP, TMP, shift);
3429 }
3430
3431 if (instruction->IsDiv()) {
3432 __ Sra(out, TMP, 31);
3433 __ Subu(out, TMP, out);
3434 } else {
3435 __ Sra(AT, TMP, 31);
3436 __ Subu(AT, TMP, AT);
3437 __ LoadConst32(TMP, imm);
3438 __ MulR6(TMP, AT, TMP);
3439 __ Subu(out, dividend, TMP);
3440 }
3441 } else {
3442 __ LoadConst64(TMP, magic);
3443 __ Dmuh(TMP, dividend, TMP);
3444
3445 if (imm > 0 && magic < 0) {
3446 __ Daddu(TMP, TMP, dividend);
3447 } else if (imm < 0 && magic > 0) {
3448 __ Dsubu(TMP, TMP, dividend);
3449 }
3450
3451 if (shift >= 32) {
3452 __ Dsra32(TMP, TMP, shift - 32);
3453 } else if (shift > 0) {
3454 __ Dsra(TMP, TMP, shift);
3455 }
3456
3457 if (instruction->IsDiv()) {
3458 __ Dsra32(out, TMP, 31);
3459 __ Dsubu(out, TMP, out);
3460 } else {
3461 __ Dsra32(AT, TMP, 31);
3462 __ Dsubu(AT, TMP, AT);
3463 __ LoadConst64(TMP, imm);
3464 __ Dmul(TMP, AT, TMP);
3465 __ Dsubu(out, dividend, TMP);
3466 }
3467 }
3468}
3469
3470void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3471 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003472 DataType::Type type = instruction->GetResultType();
3473 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003474
3475 LocationSummary* locations = instruction->GetLocations();
3476 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3477 Location second = locations->InAt(1);
3478
3479 if (second.IsConstant()) {
3480 int64_t imm = Int64FromConstant(second.GetConstant());
3481 if (imm == 0) {
3482 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3483 } else if (imm == 1 || imm == -1) {
3484 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003485 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003486 DivRemByPowerOfTwo(instruction);
3487 } else {
3488 DCHECK(imm <= -2 || imm >= 2);
3489 GenerateDivRemWithAnyConstant(instruction);
3490 }
3491 } else {
3492 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3493 GpuRegister divisor = second.AsRegister<GpuRegister>();
3494 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003495 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003496 __ DivR6(out, dividend, divisor);
3497 else
3498 __ Ddiv(out, dividend, divisor);
3499 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003501 __ ModR6(out, dividend, divisor);
3502 else
3503 __ Dmod(out, dividend, divisor);
3504 }
3505 }
3506}
3507
Alexey Frunze4dda3372015-06-01 18:31:49 -07003508void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3509 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003510 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003511 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 case DataType::Type::kInt32:
3513 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003515 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003516 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3517 break;
3518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003519 case DataType::Type::kFloat32:
3520 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003521 locations->SetInAt(0, Location::RequiresFpuRegister());
3522 locations->SetInAt(1, Location::RequiresFpuRegister());
3523 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3524 break;
3525
3526 default:
3527 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3528 }
3529}
3530
3531void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003532 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003533 LocationSummary* locations = instruction->GetLocations();
3534
3535 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003536 case DataType::Type::kInt32:
3537 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003538 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003539 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003540 case DataType::Type::kFloat32:
3541 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003542 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3543 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3544 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003545 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003546 __ DivS(dst, lhs, rhs);
3547 else
3548 __ DivD(dst, lhs, rhs);
3549 break;
3550 }
3551 default:
3552 LOG(FATAL) << "Unexpected div type " << type;
3553 }
3554}
3555
3556void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003557 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003558 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003559}
3560
3561void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3562 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003563 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003564 codegen_->AddSlowPath(slow_path);
3565 Location value = instruction->GetLocations()->InAt(0);
3566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003570 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003571 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003572 }
3573
3574 if (value.IsConstant()) {
3575 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3576 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003577 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003578 } else {
3579 // A division by a non-null constant is valid. We don't need to perform
3580 // any check, so simply fall through.
3581 }
3582 } else {
3583 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3584 }
3585}
3586
3587void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3588 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003589 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003590 locations->SetOut(Location::ConstantLocation(constant));
3591}
3592
3593void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3594 // Will be generated at use site.
3595}
3596
3597void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3598 exit->SetLocations(nullptr);
3599}
3600
3601void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3602}
3603
3604void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3605 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003606 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003607 locations->SetOut(Location::ConstantLocation(constant));
3608}
3609
3610void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3611 // Will be generated at use site.
3612}
3613
David Brazdilfc6a86a2015-06-26 10:33:45 +00003614void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003615 if (successor->IsExitBlock()) {
3616 DCHECK(got->GetPrevious()->AlwaysThrows());
3617 return; // no code needed
3618 }
3619
Alexey Frunze4dda3372015-06-01 18:31:49 -07003620 HBasicBlock* block = got->GetBlock();
3621 HInstruction* previous = got->GetPrevious();
3622 HLoopInformation* info = block->GetLoopInformation();
3623
3624 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003625 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3626 return;
3627 }
3628 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3629 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3630 }
3631 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003632 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003633 }
3634}
3635
David Brazdilfc6a86a2015-06-26 10:33:45 +00003636void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3637 got->SetLocations(nullptr);
3638}
3639
3640void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3641 HandleGoto(got, got->GetSuccessor());
3642}
3643
3644void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3645 try_boundary->SetLocations(nullptr);
3646}
3647
3648void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3649 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3650 if (!successor->IsExitBlock()) {
3651 HandleGoto(try_boundary, successor);
3652 }
3653}
3654
Alexey Frunze299a9392015-12-08 16:08:02 -08003655void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3656 bool is64bit,
3657 LocationSummary* locations) {
3658 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3659 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3660 Location rhs_location = locations->InAt(1);
3661 GpuRegister rhs_reg = ZERO;
3662 int64_t rhs_imm = 0;
3663 bool use_imm = rhs_location.IsConstant();
3664 if (use_imm) {
3665 if (is64bit) {
3666 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3667 } else {
3668 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3669 }
3670 } else {
3671 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3672 }
3673 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3674
3675 switch (cond) {
3676 case kCondEQ:
3677 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003678 if (use_imm && IsInt<16>(-rhs_imm)) {
3679 if (rhs_imm == 0) {
3680 if (cond == kCondEQ) {
3681 __ Sltiu(dst, lhs, 1);
3682 } else {
3683 __ Sltu(dst, ZERO, lhs);
3684 }
3685 } else {
3686 if (is64bit) {
3687 __ Daddiu(dst, lhs, -rhs_imm);
3688 } else {
3689 __ Addiu(dst, lhs, -rhs_imm);
3690 }
3691 if (cond == kCondEQ) {
3692 __ Sltiu(dst, dst, 1);
3693 } else {
3694 __ Sltu(dst, ZERO, dst);
3695 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003696 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003697 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003698 if (use_imm && IsUint<16>(rhs_imm)) {
3699 __ Xori(dst, lhs, rhs_imm);
3700 } else {
3701 if (use_imm) {
3702 rhs_reg = TMP;
3703 __ LoadConst64(rhs_reg, rhs_imm);
3704 }
3705 __ Xor(dst, lhs, rhs_reg);
3706 }
3707 if (cond == kCondEQ) {
3708 __ Sltiu(dst, dst, 1);
3709 } else {
3710 __ Sltu(dst, ZERO, dst);
3711 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003712 }
3713 break;
3714
3715 case kCondLT:
3716 case kCondGE:
3717 if (use_imm && IsInt<16>(rhs_imm)) {
3718 __ Slti(dst, lhs, rhs_imm);
3719 } else {
3720 if (use_imm) {
3721 rhs_reg = TMP;
3722 __ LoadConst64(rhs_reg, rhs_imm);
3723 }
3724 __ Slt(dst, lhs, rhs_reg);
3725 }
3726 if (cond == kCondGE) {
3727 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3728 // only the slt instruction but no sge.
3729 __ Xori(dst, dst, 1);
3730 }
3731 break;
3732
3733 case kCondLE:
3734 case kCondGT:
3735 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3736 // Simulate lhs <= rhs via lhs < rhs + 1.
3737 __ Slti(dst, lhs, rhs_imm_plus_one);
3738 if (cond == kCondGT) {
3739 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3740 // only the slti instruction but no sgti.
3741 __ Xori(dst, dst, 1);
3742 }
3743 } else {
3744 if (use_imm) {
3745 rhs_reg = TMP;
3746 __ LoadConst64(rhs_reg, rhs_imm);
3747 }
3748 __ Slt(dst, rhs_reg, lhs);
3749 if (cond == kCondLE) {
3750 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3751 // only the slt instruction but no sle.
3752 __ Xori(dst, dst, 1);
3753 }
3754 }
3755 break;
3756
3757 case kCondB:
3758 case kCondAE:
3759 if (use_imm && IsInt<16>(rhs_imm)) {
3760 // Sltiu sign-extends its 16-bit immediate operand before
3761 // the comparison and thus lets us compare directly with
3762 // unsigned values in the ranges [0, 0x7fff] and
3763 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3764 __ Sltiu(dst, lhs, rhs_imm);
3765 } else {
3766 if (use_imm) {
3767 rhs_reg = TMP;
3768 __ LoadConst64(rhs_reg, rhs_imm);
3769 }
3770 __ Sltu(dst, lhs, rhs_reg);
3771 }
3772 if (cond == kCondAE) {
3773 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3774 // only the sltu instruction but no sgeu.
3775 __ Xori(dst, dst, 1);
3776 }
3777 break;
3778
3779 case kCondBE:
3780 case kCondA:
3781 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3782 // Simulate lhs <= rhs via lhs < rhs + 1.
3783 // Note that this only works if rhs + 1 does not overflow
3784 // to 0, hence the check above.
3785 // Sltiu sign-extends its 16-bit immediate operand before
3786 // the comparison and thus lets us compare directly with
3787 // unsigned values in the ranges [0, 0x7fff] and
3788 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3789 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3790 if (cond == kCondA) {
3791 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3792 // only the sltiu instruction but no sgtiu.
3793 __ Xori(dst, dst, 1);
3794 }
3795 } else {
3796 if (use_imm) {
3797 rhs_reg = TMP;
3798 __ LoadConst64(rhs_reg, rhs_imm);
3799 }
3800 __ Sltu(dst, rhs_reg, lhs);
3801 if (cond == kCondBE) {
3802 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3803 // only the sltu instruction but no sleu.
3804 __ Xori(dst, dst, 1);
3805 }
3806 }
3807 break;
3808 }
3809}
3810
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003811bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3812 bool is64bit,
3813 LocationSummary* input_locations,
3814 GpuRegister dst) {
3815 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3816 Location rhs_location = input_locations->InAt(1);
3817 GpuRegister rhs_reg = ZERO;
3818 int64_t rhs_imm = 0;
3819 bool use_imm = rhs_location.IsConstant();
3820 if (use_imm) {
3821 if (is64bit) {
3822 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3823 } else {
3824 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3825 }
3826 } else {
3827 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3828 }
3829 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3830
3831 switch (cond) {
3832 case kCondEQ:
3833 case kCondNE:
3834 if (use_imm && IsInt<16>(-rhs_imm)) {
3835 if (is64bit) {
3836 __ Daddiu(dst, lhs, -rhs_imm);
3837 } else {
3838 __ Addiu(dst, lhs, -rhs_imm);
3839 }
3840 } else if (use_imm && IsUint<16>(rhs_imm)) {
3841 __ Xori(dst, lhs, rhs_imm);
3842 } else {
3843 if (use_imm) {
3844 rhs_reg = TMP;
3845 __ LoadConst64(rhs_reg, rhs_imm);
3846 }
3847 __ Xor(dst, lhs, rhs_reg);
3848 }
3849 return (cond == kCondEQ);
3850
3851 case kCondLT:
3852 case kCondGE:
3853 if (use_imm && IsInt<16>(rhs_imm)) {
3854 __ Slti(dst, lhs, rhs_imm);
3855 } else {
3856 if (use_imm) {
3857 rhs_reg = TMP;
3858 __ LoadConst64(rhs_reg, rhs_imm);
3859 }
3860 __ Slt(dst, lhs, rhs_reg);
3861 }
3862 return (cond == kCondGE);
3863
3864 case kCondLE:
3865 case kCondGT:
3866 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3867 // Simulate lhs <= rhs via lhs < rhs + 1.
3868 __ Slti(dst, lhs, rhs_imm_plus_one);
3869 return (cond == kCondGT);
3870 } else {
3871 if (use_imm) {
3872 rhs_reg = TMP;
3873 __ LoadConst64(rhs_reg, rhs_imm);
3874 }
3875 __ Slt(dst, rhs_reg, lhs);
3876 return (cond == kCondLE);
3877 }
3878
3879 case kCondB:
3880 case kCondAE:
3881 if (use_imm && IsInt<16>(rhs_imm)) {
3882 // Sltiu sign-extends its 16-bit immediate operand before
3883 // the comparison and thus lets us compare directly with
3884 // unsigned values in the ranges [0, 0x7fff] and
3885 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3886 __ Sltiu(dst, lhs, rhs_imm);
3887 } else {
3888 if (use_imm) {
3889 rhs_reg = TMP;
3890 __ LoadConst64(rhs_reg, rhs_imm);
3891 }
3892 __ Sltu(dst, lhs, rhs_reg);
3893 }
3894 return (cond == kCondAE);
3895
3896 case kCondBE:
3897 case kCondA:
3898 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3899 // Simulate lhs <= rhs via lhs < rhs + 1.
3900 // Note that this only works if rhs + 1 does not overflow
3901 // to 0, hence the check above.
3902 // Sltiu sign-extends its 16-bit immediate operand before
3903 // the comparison and thus lets us compare directly with
3904 // unsigned values in the ranges [0, 0x7fff] and
3905 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3906 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3907 return (cond == kCondA);
3908 } else {
3909 if (use_imm) {
3910 rhs_reg = TMP;
3911 __ LoadConst64(rhs_reg, rhs_imm);
3912 }
3913 __ Sltu(dst, rhs_reg, lhs);
3914 return (cond == kCondBE);
3915 }
3916 }
3917}
3918
Alexey Frunze299a9392015-12-08 16:08:02 -08003919void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3920 bool is64bit,
3921 LocationSummary* locations,
3922 Mips64Label* label) {
3923 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3924 Location rhs_location = locations->InAt(1);
3925 GpuRegister rhs_reg = ZERO;
3926 int64_t rhs_imm = 0;
3927 bool use_imm = rhs_location.IsConstant();
3928 if (use_imm) {
3929 if (is64bit) {
3930 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3931 } else {
3932 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3933 }
3934 } else {
3935 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3936 }
3937
3938 if (use_imm && rhs_imm == 0) {
3939 switch (cond) {
3940 case kCondEQ:
3941 case kCondBE: // <= 0 if zero
3942 __ Beqzc(lhs, label);
3943 break;
3944 case kCondNE:
3945 case kCondA: // > 0 if non-zero
3946 __ Bnezc(lhs, label);
3947 break;
3948 case kCondLT:
3949 __ Bltzc(lhs, label);
3950 break;
3951 case kCondGE:
3952 __ Bgezc(lhs, label);
3953 break;
3954 case kCondLE:
3955 __ Blezc(lhs, label);
3956 break;
3957 case kCondGT:
3958 __ Bgtzc(lhs, label);
3959 break;
3960 case kCondB: // always false
3961 break;
3962 case kCondAE: // always true
3963 __ Bc(label);
3964 break;
3965 }
3966 } else {
3967 if (use_imm) {
3968 rhs_reg = TMP;
3969 __ LoadConst64(rhs_reg, rhs_imm);
3970 }
3971 switch (cond) {
3972 case kCondEQ:
3973 __ Beqc(lhs, rhs_reg, label);
3974 break;
3975 case kCondNE:
3976 __ Bnec(lhs, rhs_reg, label);
3977 break;
3978 case kCondLT:
3979 __ Bltc(lhs, rhs_reg, label);
3980 break;
3981 case kCondGE:
3982 __ Bgec(lhs, rhs_reg, label);
3983 break;
3984 case kCondLE:
3985 __ Bgec(rhs_reg, lhs, label);
3986 break;
3987 case kCondGT:
3988 __ Bltc(rhs_reg, lhs, label);
3989 break;
3990 case kCondB:
3991 __ Bltuc(lhs, rhs_reg, label);
3992 break;
3993 case kCondAE:
3994 __ Bgeuc(lhs, rhs_reg, label);
3995 break;
3996 case kCondBE:
3997 __ Bgeuc(rhs_reg, lhs, label);
3998 break;
3999 case kCondA:
4000 __ Bltuc(rhs_reg, lhs, label);
4001 break;
4002 }
4003 }
4004}
4005
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004006void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
4007 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004008 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004009 LocationSummary* locations) {
4010 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4011 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4012 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004013 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004014 switch (cond) {
4015 case kCondEQ:
4016 __ CmpEqS(FTMP, lhs, rhs);
4017 __ Mfc1(dst, FTMP);
4018 __ Andi(dst, dst, 1);
4019 break;
4020 case kCondNE:
4021 __ CmpEqS(FTMP, lhs, rhs);
4022 __ Mfc1(dst, FTMP);
4023 __ Addiu(dst, dst, 1);
4024 break;
4025 case kCondLT:
4026 if (gt_bias) {
4027 __ CmpLtS(FTMP, lhs, rhs);
4028 } else {
4029 __ CmpUltS(FTMP, lhs, rhs);
4030 }
4031 __ Mfc1(dst, FTMP);
4032 __ Andi(dst, dst, 1);
4033 break;
4034 case kCondLE:
4035 if (gt_bias) {
4036 __ CmpLeS(FTMP, lhs, rhs);
4037 } else {
4038 __ CmpUleS(FTMP, lhs, rhs);
4039 }
4040 __ Mfc1(dst, FTMP);
4041 __ Andi(dst, dst, 1);
4042 break;
4043 case kCondGT:
4044 if (gt_bias) {
4045 __ CmpUltS(FTMP, rhs, lhs);
4046 } else {
4047 __ CmpLtS(FTMP, rhs, lhs);
4048 }
4049 __ Mfc1(dst, FTMP);
4050 __ Andi(dst, dst, 1);
4051 break;
4052 case kCondGE:
4053 if (gt_bias) {
4054 __ CmpUleS(FTMP, rhs, lhs);
4055 } else {
4056 __ CmpLeS(FTMP, rhs, lhs);
4057 }
4058 __ Mfc1(dst, FTMP);
4059 __ Andi(dst, dst, 1);
4060 break;
4061 default:
4062 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4063 UNREACHABLE();
4064 }
4065 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004067 switch (cond) {
4068 case kCondEQ:
4069 __ CmpEqD(FTMP, lhs, rhs);
4070 __ Mfc1(dst, FTMP);
4071 __ Andi(dst, dst, 1);
4072 break;
4073 case kCondNE:
4074 __ CmpEqD(FTMP, lhs, rhs);
4075 __ Mfc1(dst, FTMP);
4076 __ Addiu(dst, dst, 1);
4077 break;
4078 case kCondLT:
4079 if (gt_bias) {
4080 __ CmpLtD(FTMP, lhs, rhs);
4081 } else {
4082 __ CmpUltD(FTMP, lhs, rhs);
4083 }
4084 __ Mfc1(dst, FTMP);
4085 __ Andi(dst, dst, 1);
4086 break;
4087 case kCondLE:
4088 if (gt_bias) {
4089 __ CmpLeD(FTMP, lhs, rhs);
4090 } else {
4091 __ CmpUleD(FTMP, lhs, rhs);
4092 }
4093 __ Mfc1(dst, FTMP);
4094 __ Andi(dst, dst, 1);
4095 break;
4096 case kCondGT:
4097 if (gt_bias) {
4098 __ CmpUltD(FTMP, rhs, lhs);
4099 } else {
4100 __ CmpLtD(FTMP, rhs, lhs);
4101 }
4102 __ Mfc1(dst, FTMP);
4103 __ Andi(dst, dst, 1);
4104 break;
4105 case kCondGE:
4106 if (gt_bias) {
4107 __ CmpUleD(FTMP, rhs, lhs);
4108 } else {
4109 __ CmpLeD(FTMP, rhs, lhs);
4110 }
4111 __ Mfc1(dst, FTMP);
4112 __ Andi(dst, dst, 1);
4113 break;
4114 default:
4115 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4116 UNREACHABLE();
4117 }
4118 }
4119}
4120
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004121bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4122 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004123 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004124 LocationSummary* input_locations,
4125 FpuRegister dst) {
4126 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4127 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004129 switch (cond) {
4130 case kCondEQ:
4131 __ CmpEqS(dst, lhs, rhs);
4132 return false;
4133 case kCondNE:
4134 __ CmpEqS(dst, lhs, rhs);
4135 return true;
4136 case kCondLT:
4137 if (gt_bias) {
4138 __ CmpLtS(dst, lhs, rhs);
4139 } else {
4140 __ CmpUltS(dst, lhs, rhs);
4141 }
4142 return false;
4143 case kCondLE:
4144 if (gt_bias) {
4145 __ CmpLeS(dst, lhs, rhs);
4146 } else {
4147 __ CmpUleS(dst, lhs, rhs);
4148 }
4149 return false;
4150 case kCondGT:
4151 if (gt_bias) {
4152 __ CmpUltS(dst, rhs, lhs);
4153 } else {
4154 __ CmpLtS(dst, rhs, lhs);
4155 }
4156 return false;
4157 case kCondGE:
4158 if (gt_bias) {
4159 __ CmpUleS(dst, rhs, lhs);
4160 } else {
4161 __ CmpLeS(dst, rhs, lhs);
4162 }
4163 return false;
4164 default:
4165 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4166 UNREACHABLE();
4167 }
4168 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004169 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004170 switch (cond) {
4171 case kCondEQ:
4172 __ CmpEqD(dst, lhs, rhs);
4173 return false;
4174 case kCondNE:
4175 __ CmpEqD(dst, lhs, rhs);
4176 return true;
4177 case kCondLT:
4178 if (gt_bias) {
4179 __ CmpLtD(dst, lhs, rhs);
4180 } else {
4181 __ CmpUltD(dst, lhs, rhs);
4182 }
4183 return false;
4184 case kCondLE:
4185 if (gt_bias) {
4186 __ CmpLeD(dst, lhs, rhs);
4187 } else {
4188 __ CmpUleD(dst, lhs, rhs);
4189 }
4190 return false;
4191 case kCondGT:
4192 if (gt_bias) {
4193 __ CmpUltD(dst, rhs, lhs);
4194 } else {
4195 __ CmpLtD(dst, rhs, lhs);
4196 }
4197 return false;
4198 case kCondGE:
4199 if (gt_bias) {
4200 __ CmpUleD(dst, rhs, lhs);
4201 } else {
4202 __ CmpLeD(dst, rhs, lhs);
4203 }
4204 return false;
4205 default:
4206 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4207 UNREACHABLE();
4208 }
4209 }
4210}
4211
Alexey Frunze299a9392015-12-08 16:08:02 -08004212void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4213 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004214 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004215 LocationSummary* locations,
4216 Mips64Label* label) {
4217 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4218 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004220 switch (cond) {
4221 case kCondEQ:
4222 __ CmpEqS(FTMP, lhs, rhs);
4223 __ Bc1nez(FTMP, label);
4224 break;
4225 case kCondNE:
4226 __ CmpEqS(FTMP, lhs, rhs);
4227 __ Bc1eqz(FTMP, label);
4228 break;
4229 case kCondLT:
4230 if (gt_bias) {
4231 __ CmpLtS(FTMP, lhs, rhs);
4232 } else {
4233 __ CmpUltS(FTMP, lhs, rhs);
4234 }
4235 __ Bc1nez(FTMP, label);
4236 break;
4237 case kCondLE:
4238 if (gt_bias) {
4239 __ CmpLeS(FTMP, lhs, rhs);
4240 } else {
4241 __ CmpUleS(FTMP, lhs, rhs);
4242 }
4243 __ Bc1nez(FTMP, label);
4244 break;
4245 case kCondGT:
4246 if (gt_bias) {
4247 __ CmpUltS(FTMP, rhs, lhs);
4248 } else {
4249 __ CmpLtS(FTMP, rhs, lhs);
4250 }
4251 __ Bc1nez(FTMP, label);
4252 break;
4253 case kCondGE:
4254 if (gt_bias) {
4255 __ CmpUleS(FTMP, rhs, lhs);
4256 } else {
4257 __ CmpLeS(FTMP, rhs, lhs);
4258 }
4259 __ Bc1nez(FTMP, label);
4260 break;
4261 default:
4262 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004263 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004264 }
4265 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004266 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004267 switch (cond) {
4268 case kCondEQ:
4269 __ CmpEqD(FTMP, lhs, rhs);
4270 __ Bc1nez(FTMP, label);
4271 break;
4272 case kCondNE:
4273 __ CmpEqD(FTMP, lhs, rhs);
4274 __ Bc1eqz(FTMP, label);
4275 break;
4276 case kCondLT:
4277 if (gt_bias) {
4278 __ CmpLtD(FTMP, lhs, rhs);
4279 } else {
4280 __ CmpUltD(FTMP, lhs, rhs);
4281 }
4282 __ Bc1nez(FTMP, label);
4283 break;
4284 case kCondLE:
4285 if (gt_bias) {
4286 __ CmpLeD(FTMP, lhs, rhs);
4287 } else {
4288 __ CmpUleD(FTMP, lhs, rhs);
4289 }
4290 __ Bc1nez(FTMP, label);
4291 break;
4292 case kCondGT:
4293 if (gt_bias) {
4294 __ CmpUltD(FTMP, rhs, lhs);
4295 } else {
4296 __ CmpLtD(FTMP, rhs, lhs);
4297 }
4298 __ Bc1nez(FTMP, label);
4299 break;
4300 case kCondGE:
4301 if (gt_bias) {
4302 __ CmpUleD(FTMP, rhs, lhs);
4303 } else {
4304 __ CmpLeD(FTMP, rhs, lhs);
4305 }
4306 __ Bc1nez(FTMP, label);
4307 break;
4308 default:
4309 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004310 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004311 }
4312 }
4313}
4314
Alexey Frunze4dda3372015-06-01 18:31:49 -07004315void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004316 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004317 Mips64Label* true_target,
4318 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004319 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004320
David Brazdil0debae72015-11-12 18:37:00 +00004321 if (true_target == nullptr && false_target == nullptr) {
4322 // Nothing to do. The code always falls through.
4323 return;
4324 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004325 // Constant condition, statically compared against "true" (integer value 1).
4326 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004327 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004328 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004329 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004330 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004331 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004332 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004333 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004334 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004335 }
David Brazdil0debae72015-11-12 18:37:00 +00004336 return;
4337 }
4338
4339 // The following code generates these patterns:
4340 // (1) true_target == nullptr && false_target != nullptr
4341 // - opposite condition true => branch to false_target
4342 // (2) true_target != nullptr && false_target == nullptr
4343 // - condition true => branch to true_target
4344 // (3) true_target != nullptr && false_target != nullptr
4345 // - condition true => branch to true_target
4346 // - branch to false_target
4347 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004348 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004349 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004350 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004351 if (true_target == nullptr) {
4352 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4353 } else {
4354 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004356 } else {
4357 // The condition instruction has not been materialized, use its inputs as
4358 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004359 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004360 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004361 LocationSummary* locations = cond->GetLocations();
4362 IfCondition if_cond = condition->GetCondition();
4363 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004364
David Brazdil0debae72015-11-12 18:37:00 +00004365 if (true_target == nullptr) {
4366 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004367 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004368 }
4369
Alexey Frunze299a9392015-12-08 16:08:02 -08004370 switch (type) {
4371 default:
4372 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4373 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004374 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004375 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4376 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004377 case DataType::Type::kFloat32:
4378 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004379 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4380 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004381 }
4382 }
David Brazdil0debae72015-11-12 18:37:00 +00004383
4384 // If neither branch falls through (case 3), the conditional branch to `true_target`
4385 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4386 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004387 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004388 }
4389}
4390
4391void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004392 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004393 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004394 locations->SetInAt(0, Location::RequiresRegister());
4395 }
4396}
4397
4398void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004399 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4400 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004401 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004402 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004403 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004404 nullptr : codegen_->GetLabelOf(false_successor);
4405 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004406}
4407
4408void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004409 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004410 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004411 InvokeRuntimeCallingConvention calling_convention;
4412 RegisterSet caller_saves = RegisterSet::Empty();
4413 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4414 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004415 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004416 locations->SetInAt(0, Location::RequiresRegister());
4417 }
4418}
4419
4420void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004421 SlowPathCodeMIPS64* slow_path =
4422 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004423 GenerateTestAndBranch(deoptimize,
4424 /* condition_input_index */ 0,
4425 slow_path->GetEntryLabel(),
4426 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004427}
4428
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004429// This function returns true if a conditional move can be generated for HSelect.
4430// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4431// branches and regular moves.
4432//
4433// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4434//
4435// While determining feasibility of a conditional move and setting inputs/outputs
4436// are two distinct tasks, this function does both because they share quite a bit
4437// of common logic.
4438static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4439 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4440 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4441 HCondition* condition = cond->AsCondition();
4442
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004443 DataType::Type cond_type =
4444 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4445 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004446
4447 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4448 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4449 bool is_true_value_zero_constant =
4450 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4451 bool is_false_value_zero_constant =
4452 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4453
4454 bool can_move_conditionally = false;
4455 bool use_const_for_false_in = false;
4456 bool use_const_for_true_in = false;
4457
4458 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004459 if (!DataType::IsFloatingPointType(cond_type)) {
4460 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004461 // Moving int/long on int/long condition.
4462 if (is_true_value_zero_constant) {
4463 // seleqz out_reg, false_reg, cond_reg
4464 can_move_conditionally = true;
4465 use_const_for_true_in = true;
4466 } else if (is_false_value_zero_constant) {
4467 // selnez out_reg, true_reg, cond_reg
4468 can_move_conditionally = true;
4469 use_const_for_false_in = true;
4470 } else if (materialized) {
4471 // Not materializing unmaterialized int conditions
4472 // to keep the instruction count low.
4473 // selnez AT, true_reg, cond_reg
4474 // seleqz TMP, false_reg, cond_reg
4475 // or out_reg, AT, TMP
4476 can_move_conditionally = true;
4477 }
4478 } else {
4479 // Moving float/double on int/long condition.
4480 if (materialized) {
4481 // Not materializing unmaterialized int conditions
4482 // to keep the instruction count low.
4483 can_move_conditionally = true;
4484 if (is_true_value_zero_constant) {
4485 // sltu TMP, ZERO, cond_reg
4486 // mtc1 TMP, temp_cond_reg
4487 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4488 use_const_for_true_in = true;
4489 } else if (is_false_value_zero_constant) {
4490 // sltu TMP, ZERO, cond_reg
4491 // mtc1 TMP, temp_cond_reg
4492 // selnez.fmt out_reg, true_reg, temp_cond_reg
4493 use_const_for_false_in = true;
4494 } else {
4495 // sltu TMP, ZERO, cond_reg
4496 // mtc1 TMP, temp_cond_reg
4497 // sel.fmt temp_cond_reg, false_reg, true_reg
4498 // mov.fmt out_reg, temp_cond_reg
4499 }
4500 }
4501 }
4502 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004503 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004504 // Moving int/long on float/double condition.
4505 can_move_conditionally = true;
4506 if (is_true_value_zero_constant) {
4507 // mfc1 TMP, temp_cond_reg
4508 // seleqz out_reg, false_reg, TMP
4509 use_const_for_true_in = true;
4510 } else if (is_false_value_zero_constant) {
4511 // mfc1 TMP, temp_cond_reg
4512 // selnez out_reg, true_reg, TMP
4513 use_const_for_false_in = true;
4514 } else {
4515 // mfc1 TMP, temp_cond_reg
4516 // selnez AT, true_reg, TMP
4517 // seleqz TMP, false_reg, TMP
4518 // or out_reg, AT, TMP
4519 }
4520 } else {
4521 // Moving float/double on float/double condition.
4522 can_move_conditionally = true;
4523 if (is_true_value_zero_constant) {
4524 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4525 use_const_for_true_in = true;
4526 } else if (is_false_value_zero_constant) {
4527 // selnez.fmt out_reg, true_reg, temp_cond_reg
4528 use_const_for_false_in = true;
4529 } else {
4530 // sel.fmt temp_cond_reg, false_reg, true_reg
4531 // mov.fmt out_reg, temp_cond_reg
4532 }
4533 }
4534 }
4535 }
4536
4537 if (can_move_conditionally) {
4538 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4539 } else {
4540 DCHECK(!use_const_for_false_in);
4541 DCHECK(!use_const_for_true_in);
4542 }
4543
4544 if (locations_to_set != nullptr) {
4545 if (use_const_for_false_in) {
4546 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4547 } else {
4548 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004549 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004550 ? Location::RequiresFpuRegister()
4551 : Location::RequiresRegister());
4552 }
4553 if (use_const_for_true_in) {
4554 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4555 } else {
4556 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004557 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004558 ? Location::RequiresFpuRegister()
4559 : Location::RequiresRegister());
4560 }
4561 if (materialized) {
4562 locations_to_set->SetInAt(2, Location::RequiresRegister());
4563 }
4564
4565 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004566 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004567 ? Location::RequiresFpuRegister()
4568 : Location::RequiresRegister());
4569 } else {
4570 locations_to_set->SetOut(Location::SameAsFirstInput());
4571 }
4572 }
4573
4574 return can_move_conditionally;
4575}
4576
4577
4578void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4579 LocationSummary* locations = select->GetLocations();
4580 Location dst = locations->Out();
4581 Location false_src = locations->InAt(0);
4582 Location true_src = locations->InAt(1);
4583 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4584 GpuRegister cond_reg = TMP;
4585 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004586 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004587 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004588 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004589
4590 if (IsBooleanValueOrMaterializedCondition(cond)) {
4591 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4592 } else {
4593 HCondition* condition = cond->AsCondition();
4594 LocationSummary* cond_locations = cond->GetLocations();
4595 IfCondition if_cond = condition->GetCondition();
4596 cond_type = condition->InputAt(0)->GetType();
4597 switch (cond_type) {
4598 default:
4599 cond_inverted = MaterializeIntLongCompare(if_cond,
4600 /* is64bit */ false,
4601 cond_locations,
4602 cond_reg);
4603 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004604 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004605 cond_inverted = MaterializeIntLongCompare(if_cond,
4606 /* is64bit */ true,
4607 cond_locations,
4608 cond_reg);
4609 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 case DataType::Type::kFloat32:
4611 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004612 cond_inverted = MaterializeFpCompare(if_cond,
4613 condition->IsGtBias(),
4614 cond_type,
4615 cond_locations,
4616 fcond_reg);
4617 break;
4618 }
4619 }
4620
4621 if (true_src.IsConstant()) {
4622 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4623 }
4624 if (false_src.IsConstant()) {
4625 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4626 }
4627
4628 switch (dst_type) {
4629 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004630 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004631 __ Mfc1(cond_reg, fcond_reg);
4632 }
4633 if (true_src.IsConstant()) {
4634 if (cond_inverted) {
4635 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4636 } else {
4637 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4638 }
4639 } else if (false_src.IsConstant()) {
4640 if (cond_inverted) {
4641 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4642 } else {
4643 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4644 }
4645 } else {
4646 DCHECK_NE(cond_reg, AT);
4647 if (cond_inverted) {
4648 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4649 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4650 } else {
4651 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4652 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4653 }
4654 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4655 }
4656 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004657 case DataType::Type::kFloat32: {
4658 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004659 // sel*.fmt tests bit 0 of the condition register, account for that.
4660 __ Sltu(TMP, ZERO, cond_reg);
4661 __ Mtc1(TMP, fcond_reg);
4662 }
4663 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4664 if (true_src.IsConstant()) {
4665 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4666 if (cond_inverted) {
4667 __ SelnezS(dst_reg, src_reg, fcond_reg);
4668 } else {
4669 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4670 }
4671 } else if (false_src.IsConstant()) {
4672 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4673 if (cond_inverted) {
4674 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4675 } else {
4676 __ SelnezS(dst_reg, src_reg, fcond_reg);
4677 }
4678 } else {
4679 if (cond_inverted) {
4680 __ SelS(fcond_reg,
4681 true_src.AsFpuRegister<FpuRegister>(),
4682 false_src.AsFpuRegister<FpuRegister>());
4683 } else {
4684 __ SelS(fcond_reg,
4685 false_src.AsFpuRegister<FpuRegister>(),
4686 true_src.AsFpuRegister<FpuRegister>());
4687 }
4688 __ MovS(dst_reg, fcond_reg);
4689 }
4690 break;
4691 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kFloat64: {
4693 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004694 // sel*.fmt tests bit 0 of the condition register, account for that.
4695 __ Sltu(TMP, ZERO, cond_reg);
4696 __ Mtc1(TMP, fcond_reg);
4697 }
4698 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4699 if (true_src.IsConstant()) {
4700 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4701 if (cond_inverted) {
4702 __ SelnezD(dst_reg, src_reg, fcond_reg);
4703 } else {
4704 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4705 }
4706 } else if (false_src.IsConstant()) {
4707 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4708 if (cond_inverted) {
4709 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4710 } else {
4711 __ SelnezD(dst_reg, src_reg, fcond_reg);
4712 }
4713 } else {
4714 if (cond_inverted) {
4715 __ SelD(fcond_reg,
4716 true_src.AsFpuRegister<FpuRegister>(),
4717 false_src.AsFpuRegister<FpuRegister>());
4718 } else {
4719 __ SelD(fcond_reg,
4720 false_src.AsFpuRegister<FpuRegister>(),
4721 true_src.AsFpuRegister<FpuRegister>());
4722 }
4723 __ MovD(dst_reg, fcond_reg);
4724 }
4725 break;
4726 }
4727 }
4728}
4729
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004730void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004731 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004732 LocationSummary(flag, LocationSummary::kNoCall);
4733 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004734}
4735
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004736void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4737 __ LoadFromOffset(kLoadWord,
4738 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4739 SP,
4740 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004741}
4742
David Brazdil74eb1b22015-12-14 11:44:01 +00004743void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004744 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004745 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004746}
4747
4748void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004749 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4750 GenConditionalMove(select);
4751 } else {
4752 LocationSummary* locations = select->GetLocations();
4753 Mips64Label false_target;
4754 GenerateTestAndBranch(select,
4755 /* condition_input_index */ 2,
4756 /* true_target */ nullptr,
4757 &false_target);
4758 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4759 __ Bind(&false_target);
4760 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004761}
4762
David Srbecky0cf44932015-12-09 14:09:59 +00004763void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004764 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004765}
4766
David Srbeckyd28f4a02016-03-14 17:14:24 +00004767void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4768 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004769}
4770
4771void CodeGeneratorMIPS64::GenerateNop() {
4772 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004773}
4774
Alexey Frunze4dda3372015-06-01 18:31:49 -07004775void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004776 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004777 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004778 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004779 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004780 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004781 instruction,
4782 object_field_get_with_read_barrier
4783 ? LocationSummary::kCallOnSlowPath
4784 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004785 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4786 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4787 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004788 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004790 locations->SetOut(Location::RequiresFpuRegister());
4791 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004792 // The output overlaps in the case of an object field get with
4793 // read barriers enabled: we do not want the move to overwrite the
4794 // object's location, as we need it to emit the read barrier.
4795 locations->SetOut(Location::RequiresRegister(),
4796 object_field_get_with_read_barrier
4797 ? Location::kOutputOverlap
4798 : Location::kNoOutputOverlap);
4799 }
4800 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4801 // We need a temporary register for the read barrier marking slow
4802 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004803 if (!kBakerReadBarrierThunksEnableForFields) {
4804 locations->AddTemp(Location::RequiresRegister());
4805 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004806 }
4807}
4808
4809void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4810 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004811 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4812 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004813 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004814 Location obj_loc = locations->InAt(0);
4815 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4816 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004817 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004818 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004819 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004820 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4821
Alexey Frunze4dda3372015-06-01 18:31:49 -07004822 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004824 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004825 load_type = kLoadUnsignedByte;
4826 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004827 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004828 load_type = kLoadSignedByte;
4829 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004831 load_type = kLoadUnsignedHalfword;
4832 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004833 case DataType::Type::kInt16:
4834 load_type = kLoadSignedHalfword;
4835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 case DataType::Type::kInt32:
4837 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838 load_type = kLoadWord;
4839 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004840 case DataType::Type::kInt64:
4841 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004842 load_type = kLoadDoubleword;
4843 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004844 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004845 load_type = kLoadUnsignedWord;
4846 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004847 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004848 LOG(FATAL) << "Unreachable type " << type;
4849 UNREACHABLE();
4850 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004851 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004852 DCHECK(dst_loc.IsRegister());
4853 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004854 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004855 // /* HeapReference<Object> */ dst = *(obj + offset)
4856 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004857 Location temp_loc =
4858 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004859 // Note that a potential implicit null check is handled in this
4860 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4861 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4862 dst_loc,
4863 obj,
4864 offset,
4865 temp_loc,
4866 /* needs_null_check */ true);
4867 if (is_volatile) {
4868 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4869 }
4870 } else {
4871 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4872 if (is_volatile) {
4873 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4874 }
4875 // If read barriers are enabled, emit read barriers other than
4876 // Baker's using a slow path (and also unpoison the loaded
4877 // reference, if heap poisoning is enabled).
4878 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4879 }
4880 } else {
4881 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4882 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004883 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004884 DCHECK(dst_loc.IsFpuRegister());
4885 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004886 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004887 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004888
Alexey Frunze15958152017-02-09 19:08:30 -08004889 // Memory barriers, in the case of references, are handled in the
4890 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004891 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004892 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004893 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004894}
4895
4896void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4897 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4898 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004899 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004900 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004901 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004902 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004903 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004904 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004905 }
4906}
4907
4908void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004909 const FieldInfo& field_info,
4910 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004911 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004912 LocationSummary* locations = instruction->GetLocations();
4913 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004914 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004915 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004916 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004917 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4918 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004919 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4920
Alexey Frunze4dda3372015-06-01 18:31:49 -07004921 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004922 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004923 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004925 store_type = kStoreByte;
4926 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004927 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004928 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004929 store_type = kStoreHalfword;
4930 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004931 case DataType::Type::kInt32:
4932 case DataType::Type::kFloat32:
4933 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004934 store_type = kStoreWord;
4935 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004936 case DataType::Type::kInt64:
4937 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004938 store_type = kStoreDoubleword;
4939 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004940 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004941 LOG(FATAL) << "Unreachable type " << type;
4942 UNREACHABLE();
4943 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004944
Alexey Frunze15958152017-02-09 19:08:30 -08004945 if (is_volatile) {
4946 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4947 }
4948
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004949 if (value_location.IsConstant()) {
4950 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4951 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4952 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004953 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004954 DCHECK(value_location.IsRegister());
4955 GpuRegister src = value_location.AsRegister<GpuRegister>();
4956 if (kPoisonHeapReferences && needs_write_barrier) {
4957 // Note that in the case where `value` is a null reference,
4958 // we do not enter this block, as a null reference does not
4959 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004960 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004961 __ PoisonHeapReference(TMP, src);
4962 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4963 } else {
4964 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4965 }
4966 } else {
4967 DCHECK(value_location.IsFpuRegister());
4968 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4969 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4970 }
4971 }
Alexey Frunze15958152017-02-09 19:08:30 -08004972
Alexey Frunzec061de12017-02-14 13:27:23 -08004973 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004974 DCHECK(value_location.IsRegister());
4975 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004976 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004977 }
Alexey Frunze15958152017-02-09 19:08:30 -08004978
4979 if (is_volatile) {
4980 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4981 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004982}
4983
4984void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4985 HandleFieldGet(instruction, instruction->GetFieldInfo());
4986}
4987
4988void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4989 HandleFieldGet(instruction, instruction->GetFieldInfo());
4990}
4991
4992void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4993 HandleFieldSet(instruction, instruction->GetFieldInfo());
4994}
4995
4996void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004997 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004998}
4999
Alexey Frunze15958152017-02-09 19:08:30 -08005000void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
5001 HInstruction* instruction,
5002 Location out,
5003 uint32_t offset,
5004 Location maybe_temp,
5005 ReadBarrierOption read_barrier_option) {
5006 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5007 if (read_barrier_option == kWithReadBarrier) {
5008 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005009 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
5010 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5011 }
Alexey Frunze15958152017-02-09 19:08:30 -08005012 if (kUseBakerReadBarrier) {
5013 // Load with fast path based Baker's read barrier.
5014 // /* HeapReference<Object> */ out = *(out + offset)
5015 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5016 out,
5017 out_reg,
5018 offset,
5019 maybe_temp,
5020 /* needs_null_check */ false);
5021 } else {
5022 // Load with slow path based read barrier.
5023 // Save the value of `out` into `maybe_temp` before overwriting it
5024 // in the following move operation, as we will need it for the
5025 // read barrier below.
5026 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
5027 // /* HeapReference<Object> */ out = *(out + offset)
5028 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5029 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5030 }
5031 } else {
5032 // Plain load with no read barrier.
5033 // /* HeapReference<Object> */ out = *(out + offset)
5034 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
5035 __ MaybeUnpoisonHeapReference(out_reg);
5036 }
5037}
5038
5039void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
5040 HInstruction* instruction,
5041 Location out,
5042 Location obj,
5043 uint32_t offset,
5044 Location maybe_temp,
5045 ReadBarrierOption read_barrier_option) {
5046 GpuRegister out_reg = out.AsRegister<GpuRegister>();
5047 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
5048 if (read_barrier_option == kWithReadBarrier) {
5049 CHECK(kEmitCompilerReadBarrier);
5050 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005051 if (!kBakerReadBarrierThunksEnableForFields) {
5052 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
5053 }
Alexey Frunze15958152017-02-09 19:08:30 -08005054 // Load with fast path based Baker's read barrier.
5055 // /* HeapReference<Object> */ out = *(obj + offset)
5056 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5057 out,
5058 obj_reg,
5059 offset,
5060 maybe_temp,
5061 /* needs_null_check */ false);
5062 } else {
5063 // Load with slow path based read barrier.
5064 // /* HeapReference<Object> */ out = *(obj + offset)
5065 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5066 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5067 }
5068 } else {
5069 // Plain load with no read barrier.
5070 // /* HeapReference<Object> */ out = *(obj + offset)
5071 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5072 __ MaybeUnpoisonHeapReference(out_reg);
5073 }
5074}
5075
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005076static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5077 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5078 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5079 return reg - V0;
5080 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5081 return 13 + (reg - S2);
5082 } else if (reg == S8) { // One more.
5083 return 19;
5084 }
5085 LOG(FATAL) << "Unexpected register " << reg;
5086 UNREACHABLE();
5087}
5088
5089static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5090 int num = GetBakerMarkThunkNumber(reg) +
5091 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5092 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5093}
5094
5095static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5096 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5097 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5098}
5099
5100void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5101 Location root,
5102 GpuRegister obj,
5103 uint32_t offset,
5104 ReadBarrierOption read_barrier_option,
5105 Mips64Label* label_low) {
5106 if (label_low != nullptr) {
5107 DCHECK_EQ(offset, 0x5678u);
5108 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005109 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005110 if (read_barrier_option == kWithReadBarrier) {
5111 DCHECK(kEmitCompilerReadBarrier);
5112 if (kUseBakerReadBarrier) {
5113 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5114 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005115 if (kBakerReadBarrierThunksEnableForGcRoots) {
5116 // Note that we do not actually check the value of `GetIsGcMarking()`
5117 // to decide whether to mark the loaded GC root or not. Instead, we
5118 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5119 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5120 // vice versa.
5121 //
5122 // We use thunks for the slow path. That thunk checks the reference
5123 // and jumps to the entrypoint if needed.
5124 //
5125 // temp = Thread::Current()->pReadBarrierMarkReg00
5126 // // AKA &art_quick_read_barrier_mark_introspection.
5127 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5128 // if (temp != nullptr) {
5129 // temp = &gc_root_thunk<root_reg>
5130 // root = temp(root)
5131 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005132
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005133 const int32_t entry_point_offset =
5134 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5135 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5136 int16_t offset_low = Low16Bits(offset);
5137 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5138 // extension in lwu.
5139 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5140 GpuRegister base = short_offset ? obj : TMP;
5141 // Loading the entrypoint does not require a load acquire since it is only changed when
5142 // threads are suspended or running a checkpoint.
5143 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5144 if (!short_offset) {
5145 DCHECK(!label_low);
5146 __ Daui(base, obj, offset_high);
5147 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005148 Mips64Label skip_call;
5149 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005150 if (label_low != nullptr) {
5151 DCHECK(short_offset);
5152 __ Bind(label_low);
5153 }
5154 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5155 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5156 // 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 } else {
5160 // Note that we do not actually check the value of `GetIsGcMarking()`
5161 // to decide whether to mark the loaded GC root or not. Instead, we
5162 // load into `temp` (T9) the read barrier mark entry point corresponding
5163 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5164 // is false, and vice versa.
5165 //
5166 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5167 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5168 // if (temp != null) {
5169 // root = temp(root)
5170 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005171
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005172 if (label_low != nullptr) {
5173 __ Bind(label_low);
5174 }
5175 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5176 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5177 static_assert(
5178 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5179 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5180 "have different sizes.");
5181 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5182 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5183 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005184
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005185 // Slow path marking the GC root `root`.
5186 Location temp = Location::RegisterLocation(T9);
5187 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005188 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005189 instruction,
5190 root,
5191 /*entrypoint*/ temp);
5192 codegen_->AddSlowPath(slow_path);
5193
5194 const int32_t entry_point_offset =
5195 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5196 // Loading the entrypoint does not require a load acquire since it is only changed when
5197 // threads are suspended or running a checkpoint.
5198 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5199 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5200 __ Bind(slow_path->GetExitLabel());
5201 }
Alexey Frunze15958152017-02-09 19:08:30 -08005202 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005203 if (label_low != nullptr) {
5204 __ Bind(label_low);
5205 }
Alexey Frunze15958152017-02-09 19:08:30 -08005206 // GC root loaded through a slow path for read barriers other
5207 // than Baker's.
5208 // /* GcRoot<mirror::Object>* */ root = obj + offset
5209 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5210 // /* mirror::Object* */ root = root->Read()
5211 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5212 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005213 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005214 if (label_low != nullptr) {
5215 __ Bind(label_low);
5216 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005217 // Plain GC root load with no read barrier.
5218 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5219 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5220 // Note that GC roots are not affected by heap poisoning, thus we
5221 // do not have to unpoison `root_reg` here.
5222 }
5223}
5224
Alexey Frunze15958152017-02-09 19:08:30 -08005225void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5226 Location ref,
5227 GpuRegister obj,
5228 uint32_t offset,
5229 Location temp,
5230 bool needs_null_check) {
5231 DCHECK(kEmitCompilerReadBarrier);
5232 DCHECK(kUseBakerReadBarrier);
5233
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005234 if (kBakerReadBarrierThunksEnableForFields) {
5235 // Note that we do not actually check the value of `GetIsGcMarking()`
5236 // to decide whether to mark the loaded reference or not. Instead, we
5237 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5238 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5239 // vice versa.
5240 //
5241 // We use thunks for the slow path. That thunk checks the reference
5242 // and jumps to the entrypoint if needed. If the holder is not gray,
5243 // it issues a load-load memory barrier and returns to the original
5244 // reference load.
5245 //
5246 // temp = Thread::Current()->pReadBarrierMarkReg00
5247 // // AKA &art_quick_read_barrier_mark_introspection.
5248 // if (temp != nullptr) {
5249 // temp = &field_array_thunk<holder_reg>
5250 // temp()
5251 // }
5252 // not_gray_return_address:
5253 // // If the offset is too large to fit into the lw instruction, we
5254 // // use an adjusted base register (TMP) here. This register
5255 // // receives bits 16 ... 31 of the offset before the thunk invocation
5256 // // and the thunk benefits from it.
5257 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5258 // gray_return_address:
5259
5260 DCHECK(temp.IsInvalid());
5261 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5262 const int32_t entry_point_offset =
5263 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5264 // There may have or may have not been a null check if the field offset is smaller than
5265 // the page size.
5266 // There must've been a null check in case it's actually a load from an array.
5267 // We will, however, perform an explicit null check in the thunk as it's easier to
5268 // do it than not.
5269 if (instruction->IsArrayGet()) {
5270 DCHECK(!needs_null_check);
5271 }
5272 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5273 // Loading the entrypoint does not require a load acquire since it is only changed when
5274 // threads are suspended or running a checkpoint.
5275 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5276 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005277 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005278 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005279 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005280 __ Nop(); // In forbidden slot.
5281 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005282 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005283 // /* HeapReference<Object> */ ref = *(obj + offset)
5284 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5285 } else {
5286 int16_t offset_low = Low16Bits(offset);
5287 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005288 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005289 __ Daui(TMP, obj, offset_high); // In delay slot.
5290 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005291 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005292 // /* HeapReference<Object> */ ref = *(obj + offset)
5293 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5294 }
5295 if (needs_null_check) {
5296 MaybeRecordImplicitNullCheck(instruction);
5297 }
5298 __ MaybeUnpoisonHeapReference(ref_reg);
5299 return;
5300 }
5301
Alexey Frunze15958152017-02-09 19:08:30 -08005302 // /* HeapReference<Object> */ ref = *(obj + offset)
5303 Location no_index = Location::NoLocation();
5304 ScaleFactor no_scale_factor = TIMES_1;
5305 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5306 ref,
5307 obj,
5308 offset,
5309 no_index,
5310 no_scale_factor,
5311 temp,
5312 needs_null_check);
5313}
5314
5315void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5316 Location ref,
5317 GpuRegister obj,
5318 uint32_t data_offset,
5319 Location index,
5320 Location temp,
5321 bool needs_null_check) {
5322 DCHECK(kEmitCompilerReadBarrier);
5323 DCHECK(kUseBakerReadBarrier);
5324
5325 static_assert(
5326 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5327 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005328 ScaleFactor scale_factor = TIMES_4;
5329
5330 if (kBakerReadBarrierThunksEnableForArrays) {
5331 // Note that we do not actually check the value of `GetIsGcMarking()`
5332 // to decide whether to mark the loaded reference or not. Instead, we
5333 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5334 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5335 // vice versa.
5336 //
5337 // We use thunks for the slow path. That thunk checks the reference
5338 // and jumps to the entrypoint if needed. If the holder is not gray,
5339 // it issues a load-load memory barrier and returns to the original
5340 // reference load.
5341 //
5342 // temp = Thread::Current()->pReadBarrierMarkReg00
5343 // // AKA &art_quick_read_barrier_mark_introspection.
5344 // if (temp != nullptr) {
5345 // temp = &field_array_thunk<holder_reg>
5346 // temp()
5347 // }
5348 // not_gray_return_address:
5349 // // The element address is pre-calculated in the TMP register before the
5350 // // thunk invocation and the thunk benefits from it.
5351 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5352 // gray_return_address:
5353
5354 DCHECK(temp.IsInvalid());
5355 DCHECK(index.IsValid());
5356 const int32_t entry_point_offset =
5357 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5358 // We will not do the explicit null check in the thunk as some form of a null check
5359 // must've been done earlier.
5360 DCHECK(!needs_null_check);
5361 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5362 // Loading the entrypoint does not require a load acquire since it is only changed when
5363 // threads are suspended or running a checkpoint.
5364 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005365 Mips64Label skip_call;
5366 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005367 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5368 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5369 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5370 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005371 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005372 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5373 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5374 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5375 __ MaybeUnpoisonHeapReference(ref_reg);
5376 return;
5377 }
5378
Alexey Frunze15958152017-02-09 19:08:30 -08005379 // /* HeapReference<Object> */ ref =
5380 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005381 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5382 ref,
5383 obj,
5384 data_offset,
5385 index,
5386 scale_factor,
5387 temp,
5388 needs_null_check);
5389}
5390
5391void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5392 Location ref,
5393 GpuRegister obj,
5394 uint32_t offset,
5395 Location index,
5396 ScaleFactor scale_factor,
5397 Location temp,
5398 bool needs_null_check,
5399 bool always_update_field) {
5400 DCHECK(kEmitCompilerReadBarrier);
5401 DCHECK(kUseBakerReadBarrier);
5402
5403 // In slow path based read barriers, the read barrier call is
5404 // inserted after the original load. However, in fast path based
5405 // Baker's read barriers, we need to perform the load of
5406 // mirror::Object::monitor_ *before* the original reference load.
5407 // This load-load ordering is required by the read barrier.
5408 // The fast path/slow path (for Baker's algorithm) should look like:
5409 //
5410 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5411 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5412 // HeapReference<Object> ref = *src; // Original reference load.
5413 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5414 // if (is_gray) {
5415 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5416 // }
5417 //
5418 // Note: the original implementation in ReadBarrier::Barrier is
5419 // slightly more complex as it performs additional checks that we do
5420 // not do here for performance reasons.
5421
5422 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5423 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5424 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5425
5426 // /* int32_t */ monitor = obj->monitor_
5427 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5428 if (needs_null_check) {
5429 MaybeRecordImplicitNullCheck(instruction);
5430 }
5431 // /* LockWord */ lock_word = LockWord(monitor)
5432 static_assert(sizeof(LockWord) == sizeof(int32_t),
5433 "art::LockWord and int32_t have different sizes.");
5434
5435 __ Sync(0); // Barrier to prevent load-load reordering.
5436
5437 // The actual reference load.
5438 if (index.IsValid()) {
5439 // Load types involving an "index": ArrayGet,
5440 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5441 // intrinsics.
5442 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5443 if (index.IsConstant()) {
5444 size_t computed_offset =
5445 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5446 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5447 } else {
5448 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005449 if (scale_factor == TIMES_1) {
5450 __ Daddu(TMP, index_reg, obj);
5451 } else {
5452 __ Dlsa(TMP, index_reg, obj, scale_factor);
5453 }
Alexey Frunze15958152017-02-09 19:08:30 -08005454 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5455 }
5456 } else {
5457 // /* HeapReference<Object> */ ref = *(obj + offset)
5458 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5459 }
5460
5461 // Object* ref = ref_addr->AsMirrorPtr()
5462 __ MaybeUnpoisonHeapReference(ref_reg);
5463
5464 // Slow path marking the object `ref` when it is gray.
5465 SlowPathCodeMIPS64* slow_path;
5466 if (always_update_field) {
5467 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5468 // of the form `obj + field_offset`, where `obj` is a register and
5469 // `field_offset` is a register. Thus `offset` and `scale_factor`
5470 // above are expected to be null in this code path.
5471 DCHECK_EQ(offset, 0u);
5472 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005473 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005474 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5475 ref,
5476 obj,
5477 /* field_offset */ index,
5478 temp_reg);
5479 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005480 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005481 }
5482 AddSlowPath(slow_path);
5483
5484 // if (rb_state == ReadBarrier::GrayState())
5485 // ref = ReadBarrier::Mark(ref);
5486 // Given the numeric representation, it's enough to check the low bit of the
5487 // rb_state. We do that by shifting the bit into the sign bit (31) and
5488 // performing a branch on less than zero.
5489 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5490 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5491 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5492 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5493 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5494 __ Bind(slow_path->GetExitLabel());
5495}
5496
5497void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5498 Location out,
5499 Location ref,
5500 Location obj,
5501 uint32_t offset,
5502 Location index) {
5503 DCHECK(kEmitCompilerReadBarrier);
5504
5505 // Insert a slow path based read barrier *after* the reference load.
5506 //
5507 // If heap poisoning is enabled, the unpoisoning of the loaded
5508 // reference will be carried out by the runtime within the slow
5509 // path.
5510 //
5511 // Note that `ref` currently does not get unpoisoned (when heap
5512 // poisoning is enabled), which is alright as the `ref` argument is
5513 // not used by the artReadBarrierSlow entry point.
5514 //
5515 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005516 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005517 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5518 AddSlowPath(slow_path);
5519
5520 __ Bc(slow_path->GetEntryLabel());
5521 __ Bind(slow_path->GetExitLabel());
5522}
5523
5524void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5525 Location out,
5526 Location ref,
5527 Location obj,
5528 uint32_t offset,
5529 Location index) {
5530 if (kEmitCompilerReadBarrier) {
5531 // Baker's read barriers shall be handled by the fast path
5532 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5533 DCHECK(!kUseBakerReadBarrier);
5534 // If heap poisoning is enabled, unpoisoning will be taken care of
5535 // by the runtime within the slow path.
5536 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5537 } else if (kPoisonHeapReferences) {
5538 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5539 }
5540}
5541
5542void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5543 Location out,
5544 Location root) {
5545 DCHECK(kEmitCompilerReadBarrier);
5546
5547 // Insert a slow path based read barrier *after* the GC root load.
5548 //
5549 // Note that GC roots are not affected by heap poisoning, so we do
5550 // not need to do anything special for this here.
5551 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005552 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005553 AddSlowPath(slow_path);
5554
5555 __ Bc(slow_path->GetEntryLabel());
5556 __ Bind(slow_path->GetExitLabel());
5557}
5558
Alexey Frunze4dda3372015-06-01 18:31:49 -07005559void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005560 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5561 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005562 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005563 switch (type_check_kind) {
5564 case TypeCheckKind::kExactCheck:
5565 case TypeCheckKind::kAbstractClassCheck:
5566 case TypeCheckKind::kClassHierarchyCheck:
5567 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005568 call_kind =
5569 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005570 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005571 break;
5572 case TypeCheckKind::kArrayCheck:
5573 case TypeCheckKind::kUnresolvedCheck:
5574 case TypeCheckKind::kInterfaceCheck:
5575 call_kind = LocationSummary::kCallOnSlowPath;
5576 break;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005577 case TypeCheckKind::kBitstringCheck:
5578 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005579 }
5580
Vladimir Markoca6fff82017-10-03 14:49:14 +01005581 LocationSummary* locations =
5582 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005583 if (baker_read_barrier_slow_path) {
5584 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5585 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005586 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005587 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
5588 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
5589 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
5590 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
5591 } else {
5592 locations->SetInAt(1, Location::RequiresRegister());
5593 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005594 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005595 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005596 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005597 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005598}
5599
5600void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005601 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005602 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005603 Location obj_loc = locations->InAt(0);
5604 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005605 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08005606 Location out_loc = locations->Out();
5607 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5608 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5609 DCHECK_LE(num_temps, 1u);
5610 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005611 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5612 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5613 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5614 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005615 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005616 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005617
5618 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005619 // Avoid this check if we know `obj` is not null.
5620 if (instruction->MustDoNullCheck()) {
5621 __ Move(out, ZERO);
5622 __ Beqzc(obj, &done);
5623 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005624
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005625 switch (type_check_kind) {
5626 case TypeCheckKind::kExactCheck: {
5627 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005628 GenerateReferenceLoadTwoRegisters(instruction,
5629 out_loc,
5630 obj_loc,
5631 class_offset,
5632 maybe_temp_loc,
5633 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005634 // Classes must be equal for the instanceof to succeed.
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005635 __ Xor(out, out, cls.AsRegister<GpuRegister>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005636 __ Sltiu(out, out, 1);
5637 break;
5638 }
5639
5640 case TypeCheckKind::kAbstractClassCheck: {
5641 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005642 GenerateReferenceLoadTwoRegisters(instruction,
5643 out_loc,
5644 obj_loc,
5645 class_offset,
5646 maybe_temp_loc,
5647 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005648 // If the class is abstract, we eagerly fetch the super class of the
5649 // object to avoid doing a comparison we know will fail.
5650 Mips64Label loop;
5651 __ Bind(&loop);
5652 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005653 GenerateReferenceLoadOneRegister(instruction,
5654 out_loc,
5655 super_offset,
5656 maybe_temp_loc,
5657 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005658 // If `out` is null, we use it for the result, and jump to `done`.
5659 __ Beqzc(out, &done);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005660 __ Bnec(out, cls.AsRegister<GpuRegister>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005661 __ LoadConst32(out, 1);
5662 break;
5663 }
5664
5665 case TypeCheckKind::kClassHierarchyCheck: {
5666 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005667 GenerateReferenceLoadTwoRegisters(instruction,
5668 out_loc,
5669 obj_loc,
5670 class_offset,
5671 maybe_temp_loc,
5672 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005673 // Walk over the class hierarchy to find a match.
5674 Mips64Label loop, success;
5675 __ Bind(&loop);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005676 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005677 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005678 GenerateReferenceLoadOneRegister(instruction,
5679 out_loc,
5680 super_offset,
5681 maybe_temp_loc,
5682 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005683 __ Bnezc(out, &loop);
5684 // If `out` is null, we use it for the result, and jump to `done`.
5685 __ Bc(&done);
5686 __ Bind(&success);
5687 __ LoadConst32(out, 1);
5688 break;
5689 }
5690
5691 case TypeCheckKind::kArrayObjectCheck: {
5692 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005693 GenerateReferenceLoadTwoRegisters(instruction,
5694 out_loc,
5695 obj_loc,
5696 class_offset,
5697 maybe_temp_loc,
5698 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005699 // Do an exact check.
5700 Mips64Label success;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005701 __ Beqc(out, cls.AsRegister<GpuRegister>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005702 // Otherwise, we need to check that the object's class is a non-primitive array.
5703 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005704 GenerateReferenceLoadOneRegister(instruction,
5705 out_loc,
5706 component_offset,
5707 maybe_temp_loc,
5708 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005709 // If `out` is null, we use it for the result, and jump to `done`.
5710 __ Beqzc(out, &done);
5711 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5712 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5713 __ Sltiu(out, out, 1);
5714 __ Bc(&done);
5715 __ Bind(&success);
5716 __ LoadConst32(out, 1);
5717 break;
5718 }
5719
5720 case TypeCheckKind::kArrayCheck: {
5721 // No read barrier since the slow path will retry upon failure.
5722 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005723 GenerateReferenceLoadTwoRegisters(instruction,
5724 out_loc,
5725 obj_loc,
5726 class_offset,
5727 maybe_temp_loc,
5728 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005729 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005730 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5731 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005732 codegen_->AddSlowPath(slow_path);
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005733 __ Bnec(out, cls.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005734 __ LoadConst32(out, 1);
5735 break;
5736 }
5737
5738 case TypeCheckKind::kUnresolvedCheck:
5739 case TypeCheckKind::kInterfaceCheck: {
5740 // Note that we indeed only call on slow path, but we always go
5741 // into the slow path for the unresolved and interface check
5742 // cases.
5743 //
5744 // We cannot directly call the InstanceofNonTrivial runtime
5745 // entry point without resorting to a type checking slow path
5746 // here (i.e. by calling InvokeRuntime directly), as it would
5747 // require to assign fixed registers for the inputs of this
5748 // HInstanceOf instruction (following the runtime calling
5749 // convention), which might be cluttered by the potential first
5750 // read barrier emission at the beginning of this method.
5751 //
5752 // TODO: Introduce a new runtime entry point taking the object
5753 // to test (instead of its class) as argument, and let it deal
5754 // with the read barrier issues. This will let us refactor this
5755 // case of the `switch` code as it was previously (with a direct
5756 // call to the runtime not using a type checking slow path).
5757 // This should also be beneficial for the other cases above.
5758 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005759 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5760 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005761 codegen_->AddSlowPath(slow_path);
5762 __ Bc(slow_path->GetEntryLabel());
5763 break;
5764 }
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00005765
5766 case TypeCheckKind::kBitstringCheck: {
5767 // /* HeapReference<Class> */ temp = obj->klass_
5768 GenerateReferenceLoadTwoRegisters(instruction,
5769 out_loc,
5770 obj_loc,
5771 class_offset,
5772 maybe_temp_loc,
5773 kWithoutReadBarrier);
5774
5775 GenerateBitstringTypeCheckCompare(instruction, out);
5776 __ Sltiu(out, out, 1);
5777 break;
5778 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005779 }
5780
5781 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005782
5783 if (slow_path != nullptr) {
5784 __ Bind(slow_path->GetExitLabel());
5785 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005786}
5787
5788void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005789 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005790 locations->SetOut(Location::ConstantLocation(constant));
5791}
5792
5793void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5794 // Will be generated at use site.
5795}
5796
5797void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005798 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005799 locations->SetOut(Location::ConstantLocation(constant));
5800}
5801
5802void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5803 // Will be generated at use site.
5804}
5805
Calin Juravle175dc732015-08-25 15:42:32 +01005806void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5807 // The trampoline uses the same calling convention as dex calling conventions,
5808 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5809 // the method_idx.
5810 HandleInvoke(invoke);
5811}
5812
5813void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5814 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5815}
5816
Alexey Frunze4dda3372015-06-01 18:31:49 -07005817void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5818 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5819 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5820}
5821
5822void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5823 HandleInvoke(invoke);
5824 // The register T0 is required to be used for the hidden argument in
5825 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5826 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5827}
5828
5829void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5830 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5831 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005832 Location receiver = invoke->GetLocations()->InAt(0);
5833 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005834 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005835
5836 // Set the hidden argument.
5837 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5838 invoke->GetDexMethodIndex());
5839
5840 // temp = object->GetClass();
5841 if (receiver.IsStackSlot()) {
5842 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5843 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5844 } else {
5845 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5846 }
5847 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005848 // Instead of simply (possibly) unpoisoning `temp` here, we should
5849 // emit a read barrier for the previous class reference load.
5850 // However this is not required in practice, as this is an
5851 // intermediate/temporary reference and because the current
5852 // concurrent copying collector keeps the from-space memory
5853 // intact/accessible until the end of the marking phase (the
5854 // concurrent copying collector may not in the future).
5855 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005856 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5857 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5858 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005859 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005860 // temp = temp->GetImtEntryAt(method_offset);
5861 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5862 // T9 = temp->GetEntryPoint();
5863 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5864 // T9();
5865 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005866 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005867 DCHECK(!codegen_->IsLeafMethod());
5868 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5869}
5870
5871void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005872 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5873 if (intrinsic.TryDispatch(invoke)) {
5874 return;
5875 }
5876
Alexey Frunze4dda3372015-06-01 18:31:49 -07005877 HandleInvoke(invoke);
5878}
5879
5880void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005881 // Explicit clinit checks triggered by static invokes must have been pruned by
5882 // art::PrepareForRegisterAllocation.
5883 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005884
Chris Larsen3039e382015-08-26 07:54:08 -07005885 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5886 if (intrinsic.TryDispatch(invoke)) {
5887 return;
5888 }
5889
Alexey Frunze4dda3372015-06-01 18:31:49 -07005890 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005891}
5892
Orion Hodsonac141392017-01-13 11:53:47 +00005893void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5894 HandleInvoke(invoke);
5895}
5896
5897void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5898 codegen_->GenerateInvokePolymorphicCall(invoke);
5899}
5900
Chris Larsen3039e382015-08-26 07:54:08 -07005901static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005902 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005903 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5904 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005905 return true;
5906 }
5907 return false;
5908}
5909
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005910HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005911 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005912 bool fallback_load = false;
5913 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005914 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005915 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005916 case HLoadString::LoadKind::kBssEntry:
5917 DCHECK(!Runtime::Current()->UseJitCompilation());
5918 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005919 case HLoadString::LoadKind::kJitTableAddress:
5920 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005921 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005922 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005923 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005924 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005925 }
5926 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005927 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005928 }
5929 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005930}
5931
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005932HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5933 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005934 bool fallback_load = false;
5935 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005936 case HLoadClass::LoadKind::kInvalid:
5937 LOG(FATAL) << "UNREACHABLE";
5938 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005939 case HLoadClass::LoadKind::kReferrersClass:
5940 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005941 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005942 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005943 case HLoadClass::LoadKind::kBssEntry:
5944 DCHECK(!Runtime::Current()->UseJitCompilation());
5945 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005946 case HLoadClass::LoadKind::kJitTableAddress:
5947 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005948 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005949 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005950 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005951 break;
5952 }
5953 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005954 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005955 }
5956 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005957}
5958
Vladimir Markodc151b22015-10-15 18:02:30 +01005959HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5960 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005961 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005962 // On MIPS64 we support all dispatch types.
5963 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005964}
5965
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005966void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5967 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005968 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005969 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005970 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5971 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5972
Alexey Frunze19f6c692016-11-30 19:19:55 -08005973 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005974 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005975 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005976 uint32_t offset =
5977 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005978 __ LoadFromOffset(kLoadDoubleword,
5979 temp.AsRegister<GpuRegister>(),
5980 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005981 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005982 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005983 }
Vladimir Marko58155012015-08-19 12:49:41 +00005984 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005985 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005986 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005987 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5988 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005989 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005990 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005991 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5992 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5993 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005994 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5995 break;
5996 }
Vladimir Marko58155012015-08-19 12:49:41 +00005997 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005998 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5999 kLoadDoubleword,
6000 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00006001 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006002 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006003 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01006004 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006005 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
6006 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
6007 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08006008 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
6009 break;
6010 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006011 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
6012 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
6013 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07006014 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006015 }
6016
Alexey Frunze19f6c692016-11-30 19:19:55 -08006017 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00006018 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08006019 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00006020 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006021 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6022 // T9 = callee_method->entry_point_from_quick_compiled_code_;
6023 __ LoadFromOffset(kLoadDoubleword,
6024 T9,
6025 callee_method.AsRegister<GpuRegister>(),
6026 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006027 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006028 // T9()
6029 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006030 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00006031 break;
6032 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006033 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
6034
Alexey Frunze4dda3372015-06-01 18:31:49 -07006035 DCHECK(!IsLeafMethod());
6036}
6037
6038void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006039 // Explicit clinit checks triggered by static invokes must have been pruned by
6040 // art::PrepareForRegisterAllocation.
6041 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006042
6043 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6044 return;
6045 }
6046
6047 LocationSummary* locations = invoke->GetLocations();
6048 codegen_->GenerateStaticOrDirectCall(invoke,
6049 locations->HasTemps()
6050 ? locations->GetTemp(0)
6051 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006052}
6053
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006054void CodeGeneratorMIPS64::GenerateVirtualCall(
6055 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006056 // Use the calling convention instead of the location of the receiver, as
6057 // intrinsics may have put the receiver in a different register. In the intrinsics
6058 // slow path, the arguments have been moved to the right place, so here we are
6059 // guaranteed that the receiver is the first register of the calling convention.
6060 InvokeDexCallingConvention calling_convention;
6061 GpuRegister receiver = calling_convention.GetRegisterAt(0);
6062
Alexey Frunze53afca12015-11-05 16:34:23 -08006063 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006064 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6065 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
6066 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006067 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006068
6069 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006070 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08006071 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006072 // Instead of simply (possibly) unpoisoning `temp` here, we should
6073 // emit a read barrier for the previous class reference load.
6074 // However this is not required in practice, as this is an
6075 // intermediate/temporary reference and because the current
6076 // concurrent copying collector keeps the from-space memory
6077 // intact/accessible until the end of the marking phase (the
6078 // concurrent copying collector may not in the future).
6079 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006080 // temp = temp->GetMethodAt(method_offset);
6081 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6082 // T9 = temp->GetEntryPoint();
6083 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6084 // T9();
6085 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006086 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006087 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006088}
6089
6090void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6091 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6092 return;
6093 }
6094
6095 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006096 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006097}
6098
6099void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006100 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006101 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006102 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006103 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6104 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006105 return;
6106 }
Vladimir Marko41559982017-01-06 14:04:23 +00006107 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006108
Alexey Frunze15958152017-02-09 19:08:30 -08006109 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6110 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006111 ? LocationSummary::kCallOnSlowPath
6112 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006113 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006114 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6115 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6116 }
Vladimir Marko41559982017-01-06 14:04:23 +00006117 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006118 locations->SetInAt(0, Location::RequiresRegister());
6119 }
6120 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006121 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6122 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6123 // Rely on the type resolution or initialization and marking to save everything we need.
6124 RegisterSet caller_saves = RegisterSet::Empty();
6125 InvokeRuntimeCallingConvention calling_convention;
6126 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6127 locations->SetCustomSlowPathCallerSaves(caller_saves);
6128 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006129 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006130 }
6131 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006132}
6133
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006134// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6135// move.
6136void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006137 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006138 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006139 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006140 return;
6141 }
Vladimir Marko41559982017-01-06 14:04:23 +00006142 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006143
Vladimir Marko41559982017-01-06 14:04:23 +00006144 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006145 Location out_loc = locations->Out();
6146 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6147 GpuRegister current_method_reg = ZERO;
6148 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006149 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006150 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6151 }
6152
Alexey Frunze15958152017-02-09 19:08:30 -08006153 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6154 ? kWithoutReadBarrier
6155 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006156 bool generate_null_check = false;
6157 switch (load_kind) {
6158 case HLoadClass::LoadKind::kReferrersClass:
6159 DCHECK(!cls->CanCallRuntime());
6160 DCHECK(!cls->MustGenerateClinitCheck());
6161 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6162 GenerateGcRootFieldLoad(cls,
6163 out_loc,
6164 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006165 ArtMethod::DeclaringClassOffset().Int32Value(),
6166 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006167 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006168 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006169 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006170 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006171 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006172 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006173 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6174 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6175 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006176 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6177 break;
6178 }
6179 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006180 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006181 uint32_t address = dchecked_integral_cast<uint32_t>(
6182 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6183 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006184 __ LoadLiteral(out,
6185 kLoadUnsignedWord,
6186 codegen_->DeduplicateBootImageAddressLiteral(address));
6187 break;
6188 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006189 case HLoadClass::LoadKind::kBootImageClassTable: {
6190 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6191 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6192 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6193 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6194 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6195 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6196 __ Lwu(out, AT, /* placeholder */ 0x5678);
6197 // Extract the reference from the slot data, i.e. clear the hash bits.
6198 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6199 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6200 if (masked_hash != 0) {
6201 __ Daddiu(out, out, -masked_hash);
6202 }
6203 break;
6204 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006205 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006206 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6207 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006208 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6209 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006210 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006211 GenerateGcRootFieldLoad(cls,
6212 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006213 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006214 /* placeholder */ 0x5678,
6215 read_barrier_option,
6216 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006217 generate_null_check = true;
6218 break;
6219 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006220 case HLoadClass::LoadKind::kJitTableAddress:
6221 __ LoadLiteral(out,
6222 kLoadUnsignedWord,
6223 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6224 cls->GetTypeIndex(),
6225 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006226 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006227 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006228 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006229 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006230 LOG(FATAL) << "UNREACHABLE";
6231 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006232 }
6233
6234 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6235 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006236 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006237 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006238 codegen_->AddSlowPath(slow_path);
6239 if (generate_null_check) {
6240 __ Beqzc(out, slow_path->GetEntryLabel());
6241 }
6242 if (cls->MustGenerateClinitCheck()) {
6243 GenerateClassInitializationCheck(slow_path, out);
6244 } else {
6245 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006246 }
6247 }
6248}
6249
David Brazdilcb1c0552015-08-04 16:22:25 +01006250static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006251 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006252}
6253
Alexey Frunze4dda3372015-06-01 18:31:49 -07006254void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6255 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006256 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006257 locations->SetOut(Location::RequiresRegister());
6258}
6259
6260void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6261 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006262 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6263}
6264
6265void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006266 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006267}
6268
6269void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6270 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006271}
6272
Alexey Frunze4dda3372015-06-01 18:31:49 -07006273void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006274 HLoadString::LoadKind load_kind = load->GetLoadKind();
6275 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006276 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006277 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006278 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006279 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006280 } else {
6281 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006282 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6283 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6284 // Rely on the pResolveString and marking to save everything we need.
6285 RegisterSet caller_saves = RegisterSet::Empty();
6286 InvokeRuntimeCallingConvention calling_convention;
6287 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6288 locations->SetCustomSlowPathCallerSaves(caller_saves);
6289 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006290 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006291 }
6292 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006293 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006294}
6295
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006296// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6297// move.
6298void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006299 HLoadString::LoadKind load_kind = load->GetLoadKind();
6300 LocationSummary* locations = load->GetLocations();
6301 Location out_loc = locations->Out();
6302 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6303
6304 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006305 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6306 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006307 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006308 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006309 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6310 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6311 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006312 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006313 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006314 }
6315 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006316 uint32_t address = dchecked_integral_cast<uint32_t>(
6317 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6318 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006319 __ LoadLiteral(out,
6320 kLoadUnsignedWord,
6321 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006322 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006323 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006324 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006325 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006326 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006327 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006328 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6329 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006330 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6331 __ Lwu(out, AT, /* placeholder */ 0x5678);
6332 return;
6333 }
6334 case HLoadString::LoadKind::kBssEntry: {
6335 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6336 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6337 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6338 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6339 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006340 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006341 GenerateGcRootFieldLoad(load,
6342 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006343 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006344 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006345 kCompilerReadBarrierOption,
6346 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006347 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006348 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006349 codegen_->AddSlowPath(slow_path);
6350 __ Beqzc(out, slow_path->GetEntryLabel());
6351 __ Bind(slow_path->GetExitLabel());
6352 return;
6353 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006354 case HLoadString::LoadKind::kJitTableAddress:
6355 __ LoadLiteral(out,
6356 kLoadUnsignedWord,
6357 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6358 load->GetStringIndex(),
6359 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006360 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006361 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006362 default:
6363 break;
6364 }
6365
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006366 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006367 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006368 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006369 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006370 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6371 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6372 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006373}
6374
Alexey Frunze4dda3372015-06-01 18:31:49 -07006375void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006376 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006377 locations->SetOut(Location::ConstantLocation(constant));
6378}
6379
6380void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6381 // Will be generated at use site.
6382}
6383
6384void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006385 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6386 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006387 InvokeRuntimeCallingConvention calling_convention;
6388 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6389}
6390
6391void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006392 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006393 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006394 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006395 if (instruction->IsEnter()) {
6396 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6397 } else {
6398 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6399 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006400}
6401
6402void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6403 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006404 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006405 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006406 case DataType::Type::kInt32:
6407 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006408 locations->SetInAt(0, Location::RequiresRegister());
6409 locations->SetInAt(1, Location::RequiresRegister());
6410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6411 break;
6412
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006413 case DataType::Type::kFloat32:
6414 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006415 locations->SetInAt(0, Location::RequiresFpuRegister());
6416 locations->SetInAt(1, Location::RequiresFpuRegister());
6417 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6418 break;
6419
6420 default:
6421 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6422 }
6423}
6424
6425void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006426 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006427 LocationSummary* locations = instruction->GetLocations();
6428
6429 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006430 case DataType::Type::kInt32:
6431 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006432 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6433 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6434 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006435 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006436 __ MulR6(dst, lhs, rhs);
6437 else
6438 __ Dmul(dst, lhs, rhs);
6439 break;
6440 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006441 case DataType::Type::kFloat32:
6442 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006443 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6444 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6445 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006446 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006447 __ MulS(dst, lhs, rhs);
6448 else
6449 __ MulD(dst, lhs, rhs);
6450 break;
6451 }
6452 default:
6453 LOG(FATAL) << "Unexpected mul type " << type;
6454 }
6455}
6456
6457void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6458 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006459 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006460 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006461 case DataType::Type::kInt32:
6462 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006463 locations->SetInAt(0, Location::RequiresRegister());
6464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6465 break;
6466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006467 case DataType::Type::kFloat32:
6468 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006469 locations->SetInAt(0, Location::RequiresFpuRegister());
6470 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6471 break;
6472
6473 default:
6474 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6475 }
6476}
6477
6478void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006479 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006480 LocationSummary* locations = instruction->GetLocations();
6481
6482 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006483 case DataType::Type::kInt32:
6484 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006485 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6486 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006487 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006488 __ Subu(dst, ZERO, src);
6489 else
6490 __ Dsubu(dst, ZERO, src);
6491 break;
6492 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006493 case DataType::Type::kFloat32:
6494 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006495 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6496 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006497 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006498 __ NegS(dst, src);
6499 else
6500 __ NegD(dst, src);
6501 break;
6502 }
6503 default:
6504 LOG(FATAL) << "Unexpected neg type " << type;
6505 }
6506}
6507
6508void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006509 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6510 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006511 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006512 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006513 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6514 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006515}
6516
6517void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006518 // Note: if heap poisoning is enabled, the entry point takes care
6519 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006520 QuickEntrypointEnum entrypoint =
6521 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6522 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006523 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006524 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006525}
6526
6527void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006528 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6529 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006530 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006531 if (instruction->IsStringAlloc()) {
6532 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6533 } else {
6534 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006535 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006536 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006537}
6538
6539void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006540 // Note: if heap poisoning is enabled, the entry point takes care
6541 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006542 if (instruction->IsStringAlloc()) {
6543 // String is allocated through StringFactory. Call NewEmptyString entry point.
6544 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006545 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006546 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006547 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6548 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6549 __ Jalr(T9);
6550 __ Nop();
6551 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6552 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006553 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006554 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006555 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006556}
6557
6558void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006559 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006560 locations->SetInAt(0, Location::RequiresRegister());
6561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6562}
6563
6564void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006565 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006566 LocationSummary* locations = instruction->GetLocations();
6567
6568 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006569 case DataType::Type::kInt32:
6570 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006571 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6572 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6573 __ Nor(dst, src, ZERO);
6574 break;
6575 }
6576
6577 default:
6578 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6579 }
6580}
6581
6582void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006583 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006584 locations->SetInAt(0, Location::RequiresRegister());
6585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6586}
6587
6588void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6589 LocationSummary* locations = instruction->GetLocations();
6590 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6591 locations->InAt(0).AsRegister<GpuRegister>(),
6592 1);
6593}
6594
6595void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006596 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6597 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006598}
6599
Calin Juravle2ae48182016-03-16 14:05:09 +00006600void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6601 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006602 return;
6603 }
6604 Location obj = instruction->GetLocations()->InAt(0);
6605
6606 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006607 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006608}
6609
Calin Juravle2ae48182016-03-16 14:05:09 +00006610void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006611 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006612 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006613 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006614
6615 Location obj = instruction->GetLocations()->InAt(0);
6616
6617 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6618}
6619
6620void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006621 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006622}
6623
6624void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6625 HandleBinaryOp(instruction);
6626}
6627
6628void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6629 HandleBinaryOp(instruction);
6630}
6631
6632void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6633 LOG(FATAL) << "Unreachable";
6634}
6635
6636void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006637 if (instruction->GetNext()->IsSuspendCheck() &&
6638 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6639 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6640 // The back edge will generate the suspend check.
6641 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6642 }
6643
Alexey Frunze4dda3372015-06-01 18:31:49 -07006644 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6645}
6646
6647void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006648 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006649 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6650 if (location.IsStackSlot()) {
6651 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6652 } else if (location.IsDoubleStackSlot()) {
6653 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6654 }
6655 locations->SetOut(location);
6656}
6657
6658void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6659 ATTRIBUTE_UNUSED) {
6660 // Nothing to do, the parameter is already at its location.
6661}
6662
6663void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6664 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006665 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006666 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6667}
6668
6669void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6670 ATTRIBUTE_UNUSED) {
6671 // Nothing to do, the method is already at its location.
6672}
6673
6674void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006675 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006676 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006677 locations->SetInAt(i, Location::Any());
6678 }
6679 locations->SetOut(Location::Any());
6680}
6681
6682void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6683 LOG(FATAL) << "Unreachable";
6684}
6685
6686void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006687 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006688 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006689 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6690 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006692
6693 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006694 case DataType::Type::kInt32:
6695 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006696 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006697 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6699 break;
6700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006701 case DataType::Type::kFloat32:
6702 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006703 InvokeRuntimeCallingConvention calling_convention;
6704 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6705 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6706 locations->SetOut(calling_convention.GetReturnLocation(type));
6707 break;
6708 }
6709
6710 default:
6711 LOG(FATAL) << "Unexpected rem type " << type;
6712 }
6713}
6714
6715void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006716 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006717
6718 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006719 case DataType::Type::kInt32:
6720 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006721 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006722 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006723
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006724 case DataType::Type::kFloat32:
6725 case DataType::Type::kFloat64: {
6726 QuickEntrypointEnum entrypoint =
6727 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006728 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006729 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006730 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6731 } else {
6732 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6733 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006734 break;
6735 }
6736 default:
6737 LOG(FATAL) << "Unexpected rem type " << type;
6738 }
6739}
6740
Igor Murashkind01745e2017-04-05 16:40:31 -07006741void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6742 constructor_fence->SetLocations(nullptr);
6743}
6744
6745void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6746 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6747 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6748}
6749
Alexey Frunze4dda3372015-06-01 18:31:49 -07006750void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6751 memory_barrier->SetLocations(nullptr);
6752}
6753
6754void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6755 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6756}
6757
6758void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006759 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006760 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006761 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6762}
6763
6764void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6765 codegen_->GenerateFrameExit();
6766}
6767
6768void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6769 ret->SetLocations(nullptr);
6770}
6771
6772void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6773 codegen_->GenerateFrameExit();
6774}
6775
Alexey Frunze92d90602015-12-18 18:16:36 -08006776void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6777 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006778}
6779
Alexey Frunze92d90602015-12-18 18:16:36 -08006780void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6781 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006782}
6783
Alexey Frunze4dda3372015-06-01 18:31:49 -07006784void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6785 HandleShift(shl);
6786}
6787
6788void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6789 HandleShift(shl);
6790}
6791
6792void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6793 HandleShift(shr);
6794}
6795
6796void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6797 HandleShift(shr);
6798}
6799
Alexey Frunze4dda3372015-06-01 18:31:49 -07006800void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6801 HandleBinaryOp(instruction);
6802}
6803
6804void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6805 HandleBinaryOp(instruction);
6806}
6807
6808void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6809 HandleFieldGet(instruction, instruction->GetFieldInfo());
6810}
6811
6812void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6813 HandleFieldGet(instruction, instruction->GetFieldInfo());
6814}
6815
6816void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6817 HandleFieldSet(instruction, instruction->GetFieldInfo());
6818}
6819
6820void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006821 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006822}
6823
Calin Juravlee460d1d2015-09-29 04:52:17 +01006824void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6825 HUnresolvedInstanceFieldGet* instruction) {
6826 FieldAccessCallingConventionMIPS64 calling_convention;
6827 codegen_->CreateUnresolvedFieldLocationSummary(
6828 instruction, instruction->GetFieldType(), calling_convention);
6829}
6830
6831void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6832 HUnresolvedInstanceFieldGet* instruction) {
6833 FieldAccessCallingConventionMIPS64 calling_convention;
6834 codegen_->GenerateUnresolvedFieldAccess(instruction,
6835 instruction->GetFieldType(),
6836 instruction->GetFieldIndex(),
6837 instruction->GetDexPc(),
6838 calling_convention);
6839}
6840
6841void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6842 HUnresolvedInstanceFieldSet* instruction) {
6843 FieldAccessCallingConventionMIPS64 calling_convention;
6844 codegen_->CreateUnresolvedFieldLocationSummary(
6845 instruction, instruction->GetFieldType(), calling_convention);
6846}
6847
6848void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6849 HUnresolvedInstanceFieldSet* instruction) {
6850 FieldAccessCallingConventionMIPS64 calling_convention;
6851 codegen_->GenerateUnresolvedFieldAccess(instruction,
6852 instruction->GetFieldType(),
6853 instruction->GetFieldIndex(),
6854 instruction->GetDexPc(),
6855 calling_convention);
6856}
6857
6858void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6859 HUnresolvedStaticFieldGet* instruction) {
6860 FieldAccessCallingConventionMIPS64 calling_convention;
6861 codegen_->CreateUnresolvedFieldLocationSummary(
6862 instruction, instruction->GetFieldType(), calling_convention);
6863}
6864
6865void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6866 HUnresolvedStaticFieldGet* instruction) {
6867 FieldAccessCallingConventionMIPS64 calling_convention;
6868 codegen_->GenerateUnresolvedFieldAccess(instruction,
6869 instruction->GetFieldType(),
6870 instruction->GetFieldIndex(),
6871 instruction->GetDexPc(),
6872 calling_convention);
6873}
6874
6875void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6876 HUnresolvedStaticFieldSet* instruction) {
6877 FieldAccessCallingConventionMIPS64 calling_convention;
6878 codegen_->CreateUnresolvedFieldLocationSummary(
6879 instruction, instruction->GetFieldType(), calling_convention);
6880}
6881
6882void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6883 HUnresolvedStaticFieldSet* instruction) {
6884 FieldAccessCallingConventionMIPS64 calling_convention;
6885 codegen_->GenerateUnresolvedFieldAccess(instruction,
6886 instruction->GetFieldType(),
6887 instruction->GetFieldIndex(),
6888 instruction->GetDexPc(),
6889 calling_convention);
6890}
6891
Alexey Frunze4dda3372015-06-01 18:31:49 -07006892void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006893 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6894 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006895 // In suspend check slow path, usually there are no caller-save registers at all.
6896 // If SIMD instructions are present, however, we force spilling all live SIMD
6897 // registers in full width (since the runtime only saves/restores lower part).
6898 locations->SetCustomSlowPathCallerSaves(
6899 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006900}
6901
6902void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6903 HBasicBlock* block = instruction->GetBlock();
6904 if (block->GetLoopInformation() != nullptr) {
6905 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6906 // The back edge will generate the suspend check.
6907 return;
6908 }
6909 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6910 // The goto will generate the suspend check.
6911 return;
6912 }
6913 GenerateSuspendCheck(instruction, nullptr);
6914}
6915
Alexey Frunze4dda3372015-06-01 18:31:49 -07006916void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006917 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6918 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006919 InvokeRuntimeCallingConvention calling_convention;
6920 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6921}
6922
6923void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006924 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006925 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6926}
6927
6928void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006929 DataType::Type input_type = conversion->GetInputType();
6930 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006931 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6932 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006934 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6935 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006936 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6937 }
6938
Vladimir Markoca6fff82017-10-03 14:49:14 +01006939 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006940
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006941 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006942 locations->SetInAt(0, Location::RequiresFpuRegister());
6943 } else {
6944 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006945 }
6946
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006947 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006948 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006949 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006951 }
6952}
6953
6954void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6955 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006956 DataType::Type result_type = conversion->GetResultType();
6957 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006958
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006959 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6960 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006962 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006963 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6964 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6965
6966 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006967 case DataType::Type::kUint8:
6968 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006969 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006970 case DataType::Type::kInt8:
6971 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006972 // Type conversion from long to types narrower than int is a result of code
6973 // transformations. To avoid unpredictable results for SEB and SEH, we first
6974 // need to sign-extend the low 32-bit value into bits 32 through 63.
6975 __ Sll(dst, src, 0);
6976 __ Seb(dst, dst);
6977 } else {
6978 __ Seb(dst, src);
6979 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006980 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006981 case DataType::Type::kUint16:
6982 __ Andi(dst, src, 0xFFFF);
6983 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006984 case DataType::Type::kInt16:
6985 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006986 // Type conversion from long to types narrower than int is a result of code
6987 // transformations. To avoid unpredictable results for SEB and SEH, we first
6988 // need to sign-extend the low 32-bit value into bits 32 through 63.
6989 __ Sll(dst, src, 0);
6990 __ Seh(dst, dst);
6991 } else {
6992 __ Seh(dst, src);
6993 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006994 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006995 case DataType::Type::kInt32:
6996 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006997 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6998 // conversions, except when the input and output registers are the same and we are not
6999 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007000 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01007001 __ Sll(dst, src, 0);
7002 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007003 break;
7004
7005 default:
7006 LOG(FATAL) << "Unexpected type conversion from " << input_type
7007 << " to " << result_type;
7008 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007009 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007010 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7011 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007012 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007013 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007014 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007015 __ Cvtsl(dst, FTMP);
7016 } else {
7017 __ Cvtdl(dst, FTMP);
7018 }
7019 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007020 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007021 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007022 __ Cvtsw(dst, FTMP);
7023 } else {
7024 __ Cvtdw(dst, FTMP);
7025 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007026 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007027 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
7028 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007029 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
7030 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007032 if (result_type == DataType::Type::kInt64) {
7033 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007034 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007035 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007036 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007037 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007038 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007039 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007040 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007041 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007042 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007043 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00007044 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08007045 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00007046 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007047 } else if (DataType::IsFloatingPointType(result_type) &&
7048 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007049 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
7050 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007051 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07007052 __ Cvtsd(dst, src);
7053 } else {
7054 __ Cvtds(dst, src);
7055 }
7056 } else {
7057 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
7058 << " to " << result_type;
7059 }
7060}
7061
7062void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
7063 HandleShift(ushr);
7064}
7065
7066void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
7067 HandleShift(ushr);
7068}
7069
7070void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
7071 HandleBinaryOp(instruction);
7072}
7073
7074void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
7075 HandleBinaryOp(instruction);
7076}
7077
7078void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7079 // Nothing to do, this should be removed during prepare for register allocator.
7080 LOG(FATAL) << "Unreachable";
7081}
7082
7083void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7084 // Nothing to do, this should be removed during prepare for register allocator.
7085 LOG(FATAL) << "Unreachable";
7086}
7087
7088void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007089 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007090}
7091
7092void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007093 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007094}
7095
7096void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007097 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007098}
7099
7100void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007101 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007102}
7103
7104void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007105 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007106}
7107
7108void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007109 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007110}
7111
7112void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007113 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007114}
7115
7116void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007117 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007118}
7119
7120void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007121 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007122}
7123
7124void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007125 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007126}
7127
7128void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007129 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007130}
7131
7132void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007133 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007134}
7135
Aart Bike9f37602015-10-09 11:15:55 -07007136void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007137 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007138}
7139
7140void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007141 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007142}
7143
7144void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007145 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007146}
7147
7148void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007149 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007150}
7151
7152void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007153 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007154}
7155
7156void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007157 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007158}
7159
7160void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007161 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007162}
7163
7164void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007165 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007166}
7167
Mark Mendellfe57faa2015-09-18 09:26:15 -04007168// Simple implementation of packed switch - generate cascaded compare/jumps.
7169void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7170 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007171 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007172 locations->SetInAt(0, Location::RequiresRegister());
7173}
7174
Alexey Frunze0960ac52016-12-20 17:24:59 -08007175void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7176 int32_t lower_bound,
7177 uint32_t num_entries,
7178 HBasicBlock* switch_block,
7179 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007180 // Create a set of compare/jumps.
7181 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007182 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007183 // Jump to default if index is negative
7184 // Note: We don't check the case that index is positive while value < lower_bound, because in
7185 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7186 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7187
Alexey Frunze0960ac52016-12-20 17:24:59 -08007188 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007189 // Jump to successors[0] if value == lower_bound.
7190 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7191 int32_t last_index = 0;
7192 for (; num_entries - last_index > 2; last_index += 2) {
7193 __ Addiu(temp_reg, temp_reg, -2);
7194 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7195 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7196 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7197 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7198 }
7199 if (num_entries - last_index == 2) {
7200 // The last missing case_value.
7201 __ Addiu(temp_reg, temp_reg, -1);
7202 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007203 }
7204
7205 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007206 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007207 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007208 }
7209}
7210
Alexey Frunze0960ac52016-12-20 17:24:59 -08007211void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7212 int32_t lower_bound,
7213 uint32_t num_entries,
7214 HBasicBlock* switch_block,
7215 HBasicBlock* default_block) {
7216 // Create a jump table.
7217 std::vector<Mips64Label*> labels(num_entries);
7218 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7219 for (uint32_t i = 0; i < num_entries; i++) {
7220 labels[i] = codegen_->GetLabelOf(successors[i]);
7221 }
7222 JumpTable* table = __ CreateJumpTable(std::move(labels));
7223
7224 // Is the value in range?
7225 __ Addiu32(TMP, value_reg, -lower_bound);
7226 __ LoadConst32(AT, num_entries);
7227 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7228
7229 // We are in the range of the table.
7230 // Load the target address from the jump table, indexing by the value.
7231 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007232 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007233 __ Lw(TMP, TMP, 0);
7234 // Compute the absolute target address by adding the table start address
7235 // (the table contains offsets to targets relative to its start).
7236 __ Daddu(TMP, TMP, AT);
7237 // And jump.
7238 __ Jr(TMP);
7239 __ Nop();
7240}
7241
7242void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7243 int32_t lower_bound = switch_instr->GetStartValue();
7244 uint32_t num_entries = switch_instr->GetNumEntries();
7245 LocationSummary* locations = switch_instr->GetLocations();
7246 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7247 HBasicBlock* switch_block = switch_instr->GetBlock();
7248 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7249
7250 if (num_entries > kPackedSwitchJumpTableThreshold) {
7251 GenTableBasedPackedSwitch(value_reg,
7252 lower_bound,
7253 num_entries,
7254 switch_block,
7255 default_block);
7256 } else {
7257 GenPackedSwitchWithCompares(value_reg,
7258 lower_bound,
7259 num_entries,
7260 switch_block,
7261 default_block);
7262 }
7263}
7264
Chris Larsenc9905a62017-03-13 17:06:18 -07007265void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7266 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007267 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007268 locations->SetInAt(0, Location::RequiresRegister());
7269 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007270}
7271
Chris Larsenc9905a62017-03-13 17:06:18 -07007272void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7273 LocationSummary* locations = instruction->GetLocations();
7274 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7275 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7276 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7277 __ LoadFromOffset(kLoadDoubleword,
7278 locations->Out().AsRegister<GpuRegister>(),
7279 locations->InAt(0).AsRegister<GpuRegister>(),
7280 method_offset);
7281 } else {
7282 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7283 instruction->GetIndex(), kMips64PointerSize));
7284 __ LoadFromOffset(kLoadDoubleword,
7285 locations->Out().AsRegister<GpuRegister>(),
7286 locations->InAt(0).AsRegister<GpuRegister>(),
7287 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7288 __ LoadFromOffset(kLoadDoubleword,
7289 locations->Out().AsRegister<GpuRegister>(),
7290 locations->Out().AsRegister<GpuRegister>(),
7291 method_offset);
7292 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007293}
7294
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007295void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7296 ATTRIBUTE_UNUSED) {
7297 LOG(FATAL) << "Unreachable";
7298}
7299
7300void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7301 ATTRIBUTE_UNUSED) {
7302 LOG(FATAL) << "Unreachable";
7303}
7304
Alexey Frunze4dda3372015-06-01 18:31:49 -07007305} // namespace mips64
7306} // namespace art