blob: 87ed286076beaa762f0f69edff81f27893bdaaf8 [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 Frunzedfc30af2018-01-24 16:25:10 -0800353 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800354 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
1778void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1779 __ Sync(0); // only stype 0 is supported
1780}
1781
1782void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1783 HBasicBlock* successor) {
1784 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001785 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1786
1787 if (slow_path == nullptr) {
1788 slow_path =
1789 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1790 instruction->SetSlowPath(slow_path);
1791 codegen_->AddSlowPath(slow_path);
1792 if (successor != nullptr) {
1793 DCHECK(successor->IsLoopHeader());
1794 }
1795 } else {
1796 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1797 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001798
1799 __ LoadFromOffset(kLoadUnsignedHalfword,
1800 TMP,
1801 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001802 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803 if (successor == nullptr) {
1804 __ Bnezc(TMP, slow_path->GetEntryLabel());
1805 __ Bind(slow_path->GetReturnLabel());
1806 } else {
1807 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001808 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809 // slow_path will return to GetLabelOf(successor).
1810 }
1811}
1812
1813InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1814 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001815 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001816 assembler_(codegen->GetAssembler()),
1817 codegen_(codegen) {}
1818
1819void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1820 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001821 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001823 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 case DataType::Type::kInt32:
1825 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001826 locations->SetInAt(0, Location::RequiresRegister());
1827 HInstruction* right = instruction->InputAt(1);
1828 bool can_use_imm = false;
1829 if (right->IsConstant()) {
1830 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1831 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1832 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001834 DCHECK(instruction->IsAdd() || instruction->IsSub());
1835 bool single_use = right->GetUses().HasExactlyOneElement();
1836 if (instruction->IsSub()) {
1837 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1838 imm = -imm;
1839 }
1840 }
1841 if (type == DataType::Type::kInt32) {
1842 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1843 } else {
1844 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1845 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001846 }
1847 }
1848 if (can_use_imm)
1849 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1850 else
1851 locations->SetInAt(1, Location::RequiresRegister());
1852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1853 }
1854 break;
1855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001856 case DataType::Type::kFloat32:
1857 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001858 locations->SetInAt(0, Location::RequiresFpuRegister());
1859 locations->SetInAt(1, Location::RequiresFpuRegister());
1860 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1861 break;
1862
1863 default:
1864 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1865 }
1866}
1867
1868void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001870 LocationSummary* locations = instruction->GetLocations();
1871
1872 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 case DataType::Type::kInt32:
1874 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001875 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1876 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1877 Location rhs_location = locations->InAt(1);
1878
1879 GpuRegister rhs_reg = ZERO;
1880 int64_t rhs_imm = 0;
1881 bool use_imm = rhs_location.IsConstant();
1882 if (use_imm) {
1883 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1884 } else {
1885 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1886 }
1887
1888 if (instruction->IsAnd()) {
1889 if (use_imm)
1890 __ Andi(dst, lhs, rhs_imm);
1891 else
1892 __ And(dst, lhs, rhs_reg);
1893 } else if (instruction->IsOr()) {
1894 if (use_imm)
1895 __ Ori(dst, lhs, rhs_imm);
1896 else
1897 __ Or(dst, lhs, rhs_reg);
1898 } else if (instruction->IsXor()) {
1899 if (use_imm)
1900 __ Xori(dst, lhs, rhs_imm);
1901 else
1902 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001903 } else if (instruction->IsAdd() || instruction->IsSub()) {
1904 if (instruction->IsSub()) {
1905 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001906 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001907 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001908 if (use_imm) {
1909 if (IsInt<16>(rhs_imm)) {
1910 __ Addiu(dst, lhs, rhs_imm);
1911 } else {
1912 int16_t rhs_imm_high = High16Bits(rhs_imm);
1913 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1914 if (rhs_imm_low < 0) {
1915 rhs_imm_high += 1;
1916 }
1917 __ Aui(dst, lhs, rhs_imm_high);
1918 if (rhs_imm_low != 0) {
1919 __ Addiu(dst, dst, rhs_imm_low);
1920 }
1921 }
1922 } else {
1923 if (instruction->IsAdd()) {
1924 __ Addu(dst, lhs, rhs_reg);
1925 } else {
1926 DCHECK(instruction->IsSub());
1927 __ Subu(dst, lhs, rhs_reg);
1928 }
1929 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001930 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001931 if (use_imm) {
1932 if (IsInt<16>(rhs_imm)) {
1933 __ Daddiu(dst, lhs, rhs_imm);
1934 } else if (IsInt<32>(rhs_imm)) {
1935 int16_t rhs_imm_high = High16Bits(rhs_imm);
1936 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1937 bool overflow_hi16 = false;
1938 if (rhs_imm_low < 0) {
1939 rhs_imm_high += 1;
1940 overflow_hi16 = (rhs_imm_high == -32768);
1941 }
1942 __ Daui(dst, lhs, rhs_imm_high);
1943 if (rhs_imm_low != 0) {
1944 __ Daddiu(dst, dst, rhs_imm_low);
1945 }
1946 if (overflow_hi16) {
1947 __ Dahi(dst, 1);
1948 }
1949 } else {
1950 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1951 if (rhs_imm_low < 0) {
1952 rhs_imm += (INT64_C(1) << 16);
1953 }
1954 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
1955 if (rhs_imm_upper < 0) {
1956 rhs_imm += (INT64_C(1) << 32);
1957 }
1958 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
1959 if (rhs_imm_high < 0) {
1960 rhs_imm += (INT64_C(1) << 48);
1961 }
1962 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
1963 GpuRegister tmp = lhs;
1964 if (rhs_imm_low != 0) {
1965 __ Daddiu(dst, tmp, rhs_imm_low);
1966 tmp = dst;
1967 }
1968 // Dahi and Dati must use the same input and output register, so we have to initialize
1969 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
1970 // Daui(dst, lhs, 0).
1971 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
1972 __ Daui(dst, tmp, rhs_imm_upper);
1973 }
1974 if (rhs_imm_high != 0) {
1975 __ Dahi(dst, rhs_imm_high);
1976 }
1977 if (rhs_imm_top != 0) {
1978 __ Dati(dst, rhs_imm_top);
1979 }
1980 }
1981 } else if (instruction->IsAdd()) {
1982 __ Daddu(dst, lhs, rhs_reg);
1983 } else {
1984 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001985 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001986 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001987 }
1988 }
1989 break;
1990 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 case DataType::Type::kFloat32:
1992 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001993 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1994 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1995 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1996 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001998 __ AddS(dst, lhs, rhs);
1999 else
2000 __ AddD(dst, lhs, rhs);
2001 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07002003 __ SubS(dst, lhs, rhs);
2004 else
2005 __ SubD(dst, lhs, rhs);
2006 } else {
2007 LOG(FATAL) << "Unexpected floating-point binary operation";
2008 }
2009 break;
2010 }
2011 default:
2012 LOG(FATAL) << "Unexpected binary operation type " << type;
2013 }
2014}
2015
2016void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002017 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002018
Vladimir Markoca6fff82017-10-03 14:49:14 +01002019 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002022 case DataType::Type::kInt32:
2023 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002027 break;
2028 }
2029 default:
2030 LOG(FATAL) << "Unexpected shift type " << type;
2031 }
2032}
2033
2034void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002035 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002036 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002037 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002038
2039 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 case DataType::Type::kInt32:
2041 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002042 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2043 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2044 Location rhs_location = locations->InAt(1);
2045
2046 GpuRegister rhs_reg = ZERO;
2047 int64_t rhs_imm = 0;
2048 bool use_imm = rhs_location.IsConstant();
2049 if (use_imm) {
2050 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2051 } else {
2052 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2053 }
2054
2055 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002056 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002058
Alexey Frunze92d90602015-12-18 18:16:36 -08002059 if (shift_value == 0) {
2060 if (dst != lhs) {
2061 __ Move(dst, lhs);
2062 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002063 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002064 if (instr->IsShl()) {
2065 __ Sll(dst, lhs, shift_value);
2066 } else if (instr->IsShr()) {
2067 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002068 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002069 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002070 } else {
2071 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002072 }
2073 } else {
2074 if (shift_value < 32) {
2075 if (instr->IsShl()) {
2076 __ Dsll(dst, lhs, shift_value);
2077 } else if (instr->IsShr()) {
2078 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002079 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002080 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002081 } else {
2082 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002083 }
2084 } else {
2085 shift_value -= 32;
2086 if (instr->IsShl()) {
2087 __ Dsll32(dst, lhs, shift_value);
2088 } else if (instr->IsShr()) {
2089 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002090 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002091 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002092 } else {
2093 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002094 }
2095 }
2096 }
2097 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002099 if (instr->IsShl()) {
2100 __ Sllv(dst, lhs, rhs_reg);
2101 } else if (instr->IsShr()) {
2102 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002103 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002104 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002105 } else {
2106 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002107 }
2108 } else {
2109 if (instr->IsShl()) {
2110 __ Dsllv(dst, lhs, rhs_reg);
2111 } else if (instr->IsShr()) {
2112 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002113 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002114 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002115 } else {
2116 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002117 }
2118 }
2119 }
2120 break;
2121 }
2122 default:
2123 LOG(FATAL) << "Unexpected shift operation type " << type;
2124 }
2125}
2126
2127void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2128 HandleBinaryOp(instruction);
2129}
2130
2131void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2132 HandleBinaryOp(instruction);
2133}
2134
2135void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2136 HandleBinaryOp(instruction);
2137}
2138
2139void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2140 HandleBinaryOp(instruction);
2141}
2142
2143void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002145 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002147 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002148 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2149 object_array_get_with_read_barrier
2150 ? LocationSummary::kCallOnSlowPath
2151 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002152 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2153 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2154 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002155 locations->SetInAt(0, Location::RequiresRegister());
2156 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002157 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002158 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2159 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002160 // The output overlaps in the case of an object array get with
2161 // read barriers enabled: we do not want the move to overwrite the
2162 // array's location, as we need it to emit the read barrier.
2163 locations->SetOut(Location::RequiresRegister(),
2164 object_array_get_with_read_barrier
2165 ? Location::kOutputOverlap
2166 : Location::kNoOutputOverlap);
2167 }
2168 // We need a temporary register for the read barrier marking slow
2169 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2170 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002171 bool temp_needed = instruction->GetIndex()->IsConstant()
2172 ? !kBakerReadBarrierThunksEnableForFields
2173 : !kBakerReadBarrierThunksEnableForArrays;
2174 if (temp_needed) {
2175 locations->AddTemp(Location::RequiresRegister());
2176 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002177 }
2178}
2179
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002180static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2181 auto null_checker = [codegen, instruction]() {
2182 codegen->MaybeRecordImplicitNullCheck(instruction);
2183 };
2184 return null_checker;
2185}
2186
Alexey Frunze4dda3372015-06-01 18:31:49 -07002187void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2188 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002189 Location obj_loc = locations->InAt(0);
2190 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2191 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002193 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002194 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002196 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002197 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2198 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002200 case DataType::Type::kBool:
2201 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002202 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203 if (index.IsConstant()) {
2204 size_t offset =
2205 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002206 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002207 } else {
2208 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002209 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002210 }
2211 break;
2212 }
2213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002214 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002215 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002216 if (index.IsConstant()) {
2217 size_t offset =
2218 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002219 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002220 } else {
2221 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002222 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002223 }
2224 break;
2225 }
2226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002228 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002229 if (maybe_compressed_char_at) {
2230 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002231 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002232 __ Dext(TMP, TMP, 0, 1);
2233 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2234 "Expecting 0=compressed, 1=uncompressed");
2235 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002236 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002237 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2238 if (maybe_compressed_char_at) {
2239 Mips64Label uncompressed_load, done;
2240 __ Bnezc(TMP, &uncompressed_load);
2241 __ LoadFromOffset(kLoadUnsignedByte,
2242 out,
2243 obj,
2244 data_offset + (const_index << TIMES_1));
2245 __ Bc(&done);
2246 __ Bind(&uncompressed_load);
2247 __ LoadFromOffset(kLoadUnsignedHalfword,
2248 out,
2249 obj,
2250 data_offset + (const_index << TIMES_2));
2251 __ Bind(&done);
2252 } else {
2253 __ LoadFromOffset(kLoadUnsignedHalfword,
2254 out,
2255 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002256 data_offset + (const_index << TIMES_2),
2257 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002258 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002259 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002260 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2261 if (maybe_compressed_char_at) {
2262 Mips64Label uncompressed_load, done;
2263 __ Bnezc(TMP, &uncompressed_load);
2264 __ Daddu(TMP, obj, index_reg);
2265 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2266 __ Bc(&done);
2267 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002268 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002269 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2270 __ Bind(&done);
2271 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002272 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002273 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002275 }
2276 break;
2277 }
2278
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002279 case DataType::Type::kInt16: {
2280 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2281 if (index.IsConstant()) {
2282 size_t offset =
2283 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2284 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2285 } else {
2286 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2287 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2288 }
2289 break;
2290 }
2291
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002293 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002294 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 LoadOperandType load_type =
2296 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002297 if (index.IsConstant()) {
2298 size_t offset =
2299 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002300 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002301 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002302 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002303 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002304 }
2305 break;
2306 }
2307
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002308 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002309 static_assert(
2310 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2311 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2312 // /* HeapReference<Object> */ out =
2313 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2314 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002315 bool temp_needed = index.IsConstant()
2316 ? !kBakerReadBarrierThunksEnableForFields
2317 : !kBakerReadBarrierThunksEnableForArrays;
2318 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002319 // Note that a potential implicit null check is handled in this
2320 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002321 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2322 if (index.IsConstant()) {
2323 // Array load with a constant index can be treated as a field load.
2324 size_t offset =
2325 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2326 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2327 out_loc,
2328 obj,
2329 offset,
2330 temp,
2331 /* needs_null_check */ false);
2332 } else {
2333 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2334 out_loc,
2335 obj,
2336 data_offset,
2337 index,
2338 temp,
2339 /* needs_null_check */ false);
2340 }
Alexey Frunze15958152017-02-09 19:08:30 -08002341 } else {
2342 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2343 if (index.IsConstant()) {
2344 size_t offset =
2345 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2346 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2347 // If read barriers are enabled, emit read barriers other than
2348 // Baker's using a slow path (and also unpoison the loaded
2349 // reference, if heap poisoning is enabled).
2350 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2351 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002352 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002353 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2354 // If read barriers are enabled, emit read barriers other than
2355 // Baker's using a slow path (and also unpoison the loaded
2356 // reference, if heap poisoning is enabled).
2357 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2358 out_loc,
2359 out_loc,
2360 obj_loc,
2361 data_offset,
2362 index);
2363 }
2364 }
2365 break;
2366 }
2367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002369 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002370 if (index.IsConstant()) {
2371 size_t offset =
2372 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002373 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002374 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002375 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002376 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377 }
2378 break;
2379 }
2380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002382 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002383 if (index.IsConstant()) {
2384 size_t offset =
2385 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002386 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002387 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002388 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002389 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002390 }
2391 break;
2392 }
2393
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002394 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002395 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002396 if (index.IsConstant()) {
2397 size_t offset =
2398 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002399 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002401 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002402 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002403 }
2404 break;
2405 }
2406
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002408 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2409 UNREACHABLE();
2410 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002411}
2412
2413void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002414 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002415 locations->SetInAt(0, Location::RequiresRegister());
2416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2417}
2418
2419void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2420 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002421 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002422 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2423 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2424 __ LoadFromOffset(kLoadWord, out, obj, offset);
2425 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002426 // Mask out compression flag from String's array length.
2427 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2428 __ Srl(out, out, 1u);
2429 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002430}
2431
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002432Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2433 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2434 ? Location::ConstantLocation(instruction->AsConstant())
2435 : Location::RequiresRegister();
2436}
2437
2438Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2439 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2440 // We can store a non-zero float or double constant without first loading it into the FPU,
2441 // but we should only prefer this if the constant has a single use.
2442 if (instruction->IsConstant() &&
2443 (instruction->AsConstant()->IsZeroBitPattern() ||
2444 instruction->GetUses().HasExactlyOneElement())) {
2445 return Location::ConstantLocation(instruction->AsConstant());
2446 // Otherwise fall through and require an FPU register for the constant.
2447 }
2448 return Location::RequiresFpuRegister();
2449}
2450
Alexey Frunze4dda3372015-06-01 18:31:49 -07002451void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002453
2454 bool needs_write_barrier =
2455 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2456 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2457
Vladimir Markoca6fff82017-10-03 14:49:14 +01002458 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002459 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002460 may_need_runtime_call_for_type_check ?
2461 LocationSummary::kCallOnSlowPath :
2462 LocationSummary::kNoCall);
2463
2464 locations->SetInAt(0, Location::RequiresRegister());
2465 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002466 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002467 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002469 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2470 }
2471 if (needs_write_barrier) {
2472 // Temporary register for the write barrier.
2473 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002474 }
2475}
2476
2477void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2478 LocationSummary* locations = instruction->GetLocations();
2479 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2480 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002481 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002483 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002484 bool needs_write_barrier =
2485 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002486 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002487 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002488
2489 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002491 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002492 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002494 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002495 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002496 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002497 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2498 }
2499 if (value_location.IsConstant()) {
2500 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2501 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2502 } else {
2503 GpuRegister value = value_location.AsRegister<GpuRegister>();
2504 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002505 }
2506 break;
2507 }
2508
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002509 case DataType::Type::kUint16:
2510 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002512 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002513 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002514 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002515 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002516 }
2517 if (value_location.IsConstant()) {
2518 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2519 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2520 } else {
2521 GpuRegister value = value_location.AsRegister<GpuRegister>();
2522 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002523 }
2524 break;
2525 }
2526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002528 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2529 if (index.IsConstant()) {
2530 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2531 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002532 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002533 }
2534 if (value_location.IsConstant()) {
2535 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2536 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2537 } else {
2538 GpuRegister value = value_location.AsRegister<GpuRegister>();
2539 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2540 }
2541 break;
2542 }
2543
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002545 if (value_location.IsConstant()) {
2546 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002547 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002548 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002549 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002550 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002551 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002552 }
Alexey Frunze15958152017-02-09 19:08:30 -08002553 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2554 DCHECK_EQ(value, 0);
2555 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2556 DCHECK(!needs_write_barrier);
2557 DCHECK(!may_need_runtime_call_for_type_check);
2558 break;
2559 }
2560
2561 DCHECK(needs_write_barrier);
2562 GpuRegister value = value_location.AsRegister<GpuRegister>();
2563 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2564 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2565 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2566 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2567 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2568 Mips64Label done;
2569 SlowPathCodeMIPS64* slow_path = nullptr;
2570
2571 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002572 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002573 codegen_->AddSlowPath(slow_path);
2574 if (instruction->GetValueCanBeNull()) {
2575 Mips64Label non_zero;
2576 __ Bnezc(value, &non_zero);
2577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2578 if (index.IsConstant()) {
2579 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002580 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002581 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002582 }
Alexey Frunze15958152017-02-09 19:08:30 -08002583 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2584 __ Bc(&done);
2585 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002586 }
Alexey Frunze15958152017-02-09 19:08:30 -08002587
2588 // Note that when read barriers are enabled, the type checks
2589 // are performed without read barriers. This is fine, even in
2590 // the case where a class object is in the from-space after
2591 // the flip, as a comparison involving such a type would not
2592 // produce a false positive; it may of course produce a false
2593 // negative, in which case we would take the ArraySet slow
2594 // path.
2595
2596 // /* HeapReference<Class> */ temp1 = obj->klass_
2597 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2598 __ MaybeUnpoisonHeapReference(temp1);
2599
2600 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2601 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2602 // /* HeapReference<Class> */ temp2 = value->klass_
2603 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2604 // If heap poisoning is enabled, no need to unpoison `temp1`
2605 // nor `temp2`, as we are comparing two poisoned references.
2606
2607 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2608 Mips64Label do_put;
2609 __ Beqc(temp1, temp2, &do_put);
2610 // If heap poisoning is enabled, the `temp1` reference has
2611 // not been unpoisoned yet; unpoison it now.
2612 __ MaybeUnpoisonHeapReference(temp1);
2613
2614 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2615 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2616 // If heap poisoning is enabled, no need to unpoison
2617 // `temp1`, as we are comparing against null below.
2618 __ Bnezc(temp1, slow_path->GetEntryLabel());
2619 __ Bind(&do_put);
2620 } else {
2621 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2622 }
2623 }
2624
2625 GpuRegister source = value;
2626 if (kPoisonHeapReferences) {
2627 // Note that in the case where `value` is a null reference,
2628 // we do not enter this block, as a null reference does not
2629 // need poisoning.
2630 __ Move(temp1, value);
2631 __ PoisonHeapReference(temp1);
2632 source = temp1;
2633 }
2634
2635 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2636 if (index.IsConstant()) {
2637 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002638 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002639 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002640 }
2641 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2642
2643 if (!may_need_runtime_call_for_type_check) {
2644 codegen_->MaybeRecordImplicitNullCheck(instruction);
2645 }
2646
2647 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2648
2649 if (done.IsLinked()) {
2650 __ Bind(&done);
2651 }
2652
2653 if (slow_path != nullptr) {
2654 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002655 }
2656 break;
2657 }
2658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002659 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002660 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002661 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002662 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002663 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002664 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002665 }
2666 if (value_location.IsConstant()) {
2667 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2668 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2669 } else {
2670 GpuRegister value = value_location.AsRegister<GpuRegister>();
2671 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002672 }
2673 break;
2674 }
2675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002677 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002678 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002679 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002680 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002681 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002682 }
2683 if (value_location.IsConstant()) {
2684 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2685 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2686 } else {
2687 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2688 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689 }
2690 break;
2691 }
2692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002693 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002694 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002695 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002696 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002697 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002698 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002699 }
2700 if (value_location.IsConstant()) {
2701 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2702 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2703 } else {
2704 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2705 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002706 }
2707 break;
2708 }
2709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002710 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002711 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2712 UNREACHABLE();
2713 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002714}
2715
2716void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002717 RegisterSet caller_saves = RegisterSet::Empty();
2718 InvokeRuntimeCallingConvention calling_convention;
2719 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2720 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2721 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002722
2723 HInstruction* index = instruction->InputAt(0);
2724 HInstruction* length = instruction->InputAt(1);
2725
2726 bool const_index = false;
2727 bool const_length = false;
2728
2729 if (index->IsConstant()) {
2730 if (length->IsConstant()) {
2731 const_index = true;
2732 const_length = true;
2733 } else {
2734 int32_t index_value = index->AsIntConstant()->GetValue();
2735 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2736 const_index = true;
2737 }
2738 }
2739 } else if (length->IsConstant()) {
2740 int32_t length_value = length->AsIntConstant()->GetValue();
2741 if (IsUint<15>(length_value)) {
2742 const_length = true;
2743 }
2744 }
2745
2746 locations->SetInAt(0, const_index
2747 ? Location::ConstantLocation(index->AsConstant())
2748 : Location::RequiresRegister());
2749 locations->SetInAt(1, const_length
2750 ? Location::ConstantLocation(length->AsConstant())
2751 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002752}
2753
2754void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2755 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002756 Location index_loc = locations->InAt(0);
2757 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002758
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002759 if (length_loc.IsConstant()) {
2760 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2761 if (index_loc.IsConstant()) {
2762 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2763 if (index < 0 || index >= length) {
2764 BoundsCheckSlowPathMIPS64* slow_path =
2765 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2766 codegen_->AddSlowPath(slow_path);
2767 __ Bc(slow_path->GetEntryLabel());
2768 } else {
2769 // Nothing to be done.
2770 }
2771 return;
2772 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002773
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002774 BoundsCheckSlowPathMIPS64* slow_path =
2775 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2776 codegen_->AddSlowPath(slow_path);
2777 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2778 if (length == 0) {
2779 __ Bc(slow_path->GetEntryLabel());
2780 } else if (length == 1) {
2781 __ Bnezc(index, slow_path->GetEntryLabel());
2782 } else {
2783 DCHECK(IsUint<15>(length)) << length;
2784 __ Sltiu(TMP, index, length);
2785 __ Beqzc(TMP, slow_path->GetEntryLabel());
2786 }
2787 } else {
2788 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2789 BoundsCheckSlowPathMIPS64* slow_path =
2790 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2791 codegen_->AddSlowPath(slow_path);
2792 if (index_loc.IsConstant()) {
2793 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2794 if (index < 0) {
2795 __ Bc(slow_path->GetEntryLabel());
2796 } else if (index == 0) {
2797 __ Blezc(length, slow_path->GetEntryLabel());
2798 } else {
2799 DCHECK(IsInt<16>(index + 1)) << index;
2800 __ Sltiu(TMP, length, index + 1);
2801 __ Bnezc(TMP, slow_path->GetEntryLabel());
2802 }
2803 } else {
2804 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2805 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2806 }
2807 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002808}
2809
Alexey Frunze15958152017-02-09 19:08:30 -08002810// Temp is used for read barrier.
2811static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2812 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002813 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002814 (kUseBakerReadBarrier ||
2815 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2816 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2817 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2818 return 1;
2819 }
2820 return 0;
2821}
2822
2823// Extra temp is used for read barrier.
2824static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2825 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2826}
2827
Alexey Frunze4dda3372015-06-01 18:31:49 -07002828void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002829 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002830 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002831 LocationSummary* locations =
2832 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002833 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002834 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002835 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002836}
2837
2838void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002839 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002840 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002841 Location obj_loc = locations->InAt(0);
2842 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002843 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002844 Location temp_loc = locations->GetTemp(0);
2845 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2846 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2847 DCHECK_LE(num_temps, 2u);
2848 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002849 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2850 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2851 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2852 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2853 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2854 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2855 const uint32_t object_array_data_offset =
2856 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2857 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002858
Alexey Frunzedfc30af2018-01-24 16:25:10 -08002859 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002860 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002861 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2862 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002863 codegen_->AddSlowPath(slow_path);
2864
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002865 // Avoid this check if we know `obj` is not null.
2866 if (instruction->MustDoNullCheck()) {
2867 __ Beqzc(obj, &done);
2868 }
2869
2870 switch (type_check_kind) {
2871 case TypeCheckKind::kExactCheck:
2872 case TypeCheckKind::kArrayCheck: {
2873 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002874 GenerateReferenceLoadTwoRegisters(instruction,
2875 temp_loc,
2876 obj_loc,
2877 class_offset,
2878 maybe_temp2_loc,
2879 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002880 // Jump to slow path for throwing the exception or doing a
2881 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002882 __ Bnec(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002883 break;
2884 }
2885
2886 case TypeCheckKind::kAbstractClassCheck: {
2887 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002888 GenerateReferenceLoadTwoRegisters(instruction,
2889 temp_loc,
2890 obj_loc,
2891 class_offset,
2892 maybe_temp2_loc,
2893 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002894 // If the class is abstract, we eagerly fetch the super class of the
2895 // object to avoid doing a comparison we know will fail.
2896 Mips64Label loop;
2897 __ Bind(&loop);
2898 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002899 GenerateReferenceLoadOneRegister(instruction,
2900 temp_loc,
2901 super_offset,
2902 maybe_temp2_loc,
2903 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002904 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2905 // exception.
2906 __ Beqzc(temp, slow_path->GetEntryLabel());
2907 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002908 __ Bnec(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002909 break;
2910 }
2911
2912 case TypeCheckKind::kClassHierarchyCheck: {
2913 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002914 GenerateReferenceLoadTwoRegisters(instruction,
2915 temp_loc,
2916 obj_loc,
2917 class_offset,
2918 maybe_temp2_loc,
2919 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002920 // Walk over the class hierarchy to find a match.
2921 Mips64Label loop;
2922 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002923 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002924 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002925 GenerateReferenceLoadOneRegister(instruction,
2926 temp_loc,
2927 super_offset,
2928 maybe_temp2_loc,
2929 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002930 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2931 // exception. Otherwise, jump to the beginning of the loop.
2932 __ Bnezc(temp, &loop);
2933 __ Bc(slow_path->GetEntryLabel());
2934 break;
2935 }
2936
2937 case TypeCheckKind::kArrayObjectCheck: {
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 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00002946 __ Beqc(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002947 // Otherwise, we need to check that the object's class is a non-primitive array.
2948 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002949 GenerateReferenceLoadOneRegister(instruction,
2950 temp_loc,
2951 component_offset,
2952 maybe_temp2_loc,
2953 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002954 // If the component type is null, jump to the slow path to throw the exception.
2955 __ Beqzc(temp, slow_path->GetEntryLabel());
2956 // Otherwise, the object is indeed an array, further check that this component
2957 // type is not a primitive type.
2958 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2959 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2960 __ Bnezc(temp, slow_path->GetEntryLabel());
2961 break;
2962 }
2963
2964 case TypeCheckKind::kUnresolvedCheck:
2965 // We always go into the type check slow path for the unresolved check case.
2966 // We cannot directly call the CheckCast runtime entry point
2967 // without resorting to a type checking slow path here (i.e. by
2968 // calling InvokeRuntime directly), as it would require to
2969 // assign fixed registers for the inputs of this HInstanceOf
2970 // instruction (following the runtime calling convention), which
2971 // might be cluttered by the potential first read barrier
2972 // emission at the beginning of this method.
2973 __ Bc(slow_path->GetEntryLabel());
2974 break;
2975
2976 case TypeCheckKind::kInterfaceCheck: {
2977 // Avoid read barriers to improve performance of the fast path. We can not get false
2978 // positives by doing this.
2979 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002980 GenerateReferenceLoadTwoRegisters(instruction,
2981 temp_loc,
2982 obj_loc,
2983 class_offset,
2984 maybe_temp2_loc,
2985 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002986 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002987 GenerateReferenceLoadTwoRegisters(instruction,
2988 temp_loc,
2989 temp_loc,
2990 iftable_offset,
2991 maybe_temp2_loc,
2992 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002993 // Iftable is never null.
2994 __ Lw(TMP, temp, array_length_offset);
2995 // Loop through the iftable and check if any class matches.
2996 Mips64Label loop;
2997 __ Bind(&loop);
2998 __ Beqzc(TMP, slow_path->GetEntryLabel());
2999 __ Lwu(AT, temp, object_array_data_offset);
3000 __ MaybeUnpoisonHeapReference(AT);
3001 // Go to next interface.
3002 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3003 __ Addiu(TMP, TMP, -2);
3004 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003005 __ Bnec(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003006 break;
3007 }
3008 }
3009
3010 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003011 __ Bind(slow_path->GetExitLabel());
3012}
3013
3014void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3015 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003016 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003017 locations->SetInAt(0, Location::RequiresRegister());
3018 if (check->HasUses()) {
3019 locations->SetOut(Location::SameAsFirstInput());
3020 }
3021}
3022
3023void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3024 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003025 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003026 check->GetLoadClass(),
3027 check,
3028 check->GetDexPc(),
3029 true);
3030 codegen_->AddSlowPath(slow_path);
3031 GenerateClassInitializationCheck(slow_path,
3032 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3033}
3034
3035void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003037
Vladimir Markoca6fff82017-10-03 14:49:14 +01003038 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039
3040 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003042 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003043 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003045 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003046 case DataType::Type::kInt32:
3047 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003048 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003049 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003050 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3051 break;
3052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kFloat32:
3054 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003055 locations->SetInAt(0, Location::RequiresFpuRegister());
3056 locations->SetInAt(1, Location::RequiresFpuRegister());
3057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003058 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059
3060 default:
3061 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3062 }
3063}
3064
3065void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3066 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003067 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003069
3070 // 0 if: left == right
3071 // 1 if: left > right
3072 // -1 if: left < right
3073 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003074 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003075 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003076 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003078 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 case DataType::Type::kInt32:
3080 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003081 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003082 Location rhs_location = locations->InAt(1);
3083 bool use_imm = rhs_location.IsConstant();
3084 GpuRegister rhs = ZERO;
3085 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003087 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3088 if (value != 0) {
3089 rhs = AT;
3090 __ LoadConst64(rhs, value);
3091 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003092 } else {
3093 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3094 if (value != 0) {
3095 rhs = AT;
3096 __ LoadConst32(rhs, value);
3097 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003098 }
3099 } else {
3100 rhs = rhs_location.AsRegister<GpuRegister>();
3101 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003102 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003103 __ Slt(res, rhs, lhs);
3104 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003105 break;
3106 }
3107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003108 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003109 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3110 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3111 Mips64Label done;
3112 __ CmpEqS(FTMP, lhs, rhs);
3113 __ LoadConst32(res, 0);
3114 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003115 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003116 __ CmpLtS(FTMP, lhs, rhs);
3117 __ LoadConst32(res, -1);
3118 __ Bc1nez(FTMP, &done);
3119 __ LoadConst32(res, 1);
3120 } else {
3121 __ CmpLtS(FTMP, rhs, lhs);
3122 __ LoadConst32(res, 1);
3123 __ Bc1nez(FTMP, &done);
3124 __ LoadConst32(res, -1);
3125 }
3126 __ Bind(&done);
3127 break;
3128 }
3129
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003130 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003131 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3132 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3133 Mips64Label done;
3134 __ CmpEqD(FTMP, lhs, rhs);
3135 __ LoadConst32(res, 0);
3136 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003137 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003138 __ CmpLtD(FTMP, lhs, rhs);
3139 __ LoadConst32(res, -1);
3140 __ Bc1nez(FTMP, &done);
3141 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003143 __ CmpLtD(FTMP, rhs, lhs);
3144 __ LoadConst32(res, 1);
3145 __ Bc1nez(FTMP, &done);
3146 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003147 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003148 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003149 break;
3150 }
3151
3152 default:
3153 LOG(FATAL) << "Unimplemented compare type " << in_type;
3154 }
3155}
3156
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003157void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003158 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003159 switch (instruction->InputAt(0)->GetType()) {
3160 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003162 locations->SetInAt(0, Location::RequiresRegister());
3163 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3164 break;
3165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003166 case DataType::Type::kFloat32:
3167 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003168 locations->SetInAt(0, Location::RequiresFpuRegister());
3169 locations->SetInAt(1, Location::RequiresFpuRegister());
3170 break;
3171 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003172 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003173 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3174 }
3175}
3176
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003177void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003178 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003179 return;
3180 }
3181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003182 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003183 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003184 switch (type) {
3185 default:
3186 // Integer case.
3187 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3188 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003189 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003190 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3191 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003192 case DataType::Type::kFloat32:
3193 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003194 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3195 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003196 }
3197}
3198
Alexey Frunzec857c742015-09-23 15:12:39 -07003199void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3200 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003201 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003202
3203 LocationSummary* locations = instruction->GetLocations();
3204 Location second = locations->InAt(1);
3205 DCHECK(second.IsConstant());
3206
3207 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3208 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3209 int64_t imm = Int64FromConstant(second.GetConstant());
3210 DCHECK(imm == 1 || imm == -1);
3211
3212 if (instruction->IsRem()) {
3213 __ Move(out, ZERO);
3214 } else {
3215 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003216 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003217 __ Subu(out, ZERO, dividend);
3218 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003219 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003220 __ Dsubu(out, ZERO, dividend);
3221 }
3222 } else if (out != dividend) {
3223 __ Move(out, dividend);
3224 }
3225 }
3226}
3227
3228void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3229 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003231
3232 LocationSummary* locations = instruction->GetLocations();
3233 Location second = locations->InAt(1);
3234 DCHECK(second.IsConstant());
3235
3236 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3237 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3238 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003239 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003240 int ctz_imm = CTZ(abs_imm);
3241
3242 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003243 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003244 if (ctz_imm == 1) {
3245 // Fast path for division by +/-2, which is very common.
3246 __ Srl(TMP, dividend, 31);
3247 } else {
3248 __ Sra(TMP, dividend, 31);
3249 __ Srl(TMP, TMP, 32 - ctz_imm);
3250 }
3251 __ Addu(out, dividend, TMP);
3252 __ Sra(out, out, ctz_imm);
3253 if (imm < 0) {
3254 __ Subu(out, ZERO, out);
3255 }
3256 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003257 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003258 if (ctz_imm == 1) {
3259 // Fast path for division by +/-2, which is very common.
3260 __ Dsrl32(TMP, dividend, 31);
3261 } else {
3262 __ Dsra32(TMP, dividend, 31);
3263 if (ctz_imm > 32) {
3264 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3265 } else {
3266 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3267 }
3268 }
3269 __ Daddu(out, dividend, TMP);
3270 if (ctz_imm < 32) {
3271 __ Dsra(out, out, ctz_imm);
3272 } else {
3273 __ Dsra32(out, out, ctz_imm - 32);
3274 }
3275 if (imm < 0) {
3276 __ Dsubu(out, ZERO, out);
3277 }
3278 }
3279 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003281 if (ctz_imm == 1) {
3282 // Fast path for modulo +/-2, which is very common.
3283 __ Sra(TMP, dividend, 31);
3284 __ Subu(out, dividend, TMP);
3285 __ Andi(out, out, 1);
3286 __ Addu(out, out, TMP);
3287 } else {
3288 __ Sra(TMP, dividend, 31);
3289 __ Srl(TMP, TMP, 32 - ctz_imm);
3290 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003291 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003292 __ Subu(out, out, TMP);
3293 }
3294 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003295 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003296 if (ctz_imm == 1) {
3297 // Fast path for modulo +/-2, which is very common.
3298 __ Dsra32(TMP, dividend, 31);
3299 __ Dsubu(out, dividend, TMP);
3300 __ Andi(out, out, 1);
3301 __ Daddu(out, out, TMP);
3302 } else {
3303 __ Dsra32(TMP, dividend, 31);
3304 if (ctz_imm > 32) {
3305 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3306 } else {
3307 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3308 }
3309 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003310 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003311 __ Dsubu(out, out, TMP);
3312 }
3313 }
3314 }
3315}
3316
3317void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3318 DCHECK(instruction->IsDiv() || instruction->IsRem());
3319
3320 LocationSummary* locations = instruction->GetLocations();
3321 Location second = locations->InAt(1);
3322 DCHECK(second.IsConstant());
3323
3324 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3325 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3326 int64_t imm = Int64FromConstant(second.GetConstant());
3327
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003328 DataType::Type type = instruction->GetResultType();
3329 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003330
3331 int64_t magic;
3332 int shift;
3333 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003334 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003335 &magic,
3336 &shift);
3337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003339 __ LoadConst32(TMP, magic);
3340 __ MuhR6(TMP, dividend, TMP);
3341
3342 if (imm > 0 && magic < 0) {
3343 __ Addu(TMP, TMP, dividend);
3344 } else if (imm < 0 && magic > 0) {
3345 __ Subu(TMP, TMP, dividend);
3346 }
3347
3348 if (shift != 0) {
3349 __ Sra(TMP, TMP, shift);
3350 }
3351
3352 if (instruction->IsDiv()) {
3353 __ Sra(out, TMP, 31);
3354 __ Subu(out, TMP, out);
3355 } else {
3356 __ Sra(AT, TMP, 31);
3357 __ Subu(AT, TMP, AT);
3358 __ LoadConst32(TMP, imm);
3359 __ MulR6(TMP, AT, TMP);
3360 __ Subu(out, dividend, TMP);
3361 }
3362 } else {
3363 __ LoadConst64(TMP, magic);
3364 __ Dmuh(TMP, dividend, TMP);
3365
3366 if (imm > 0 && magic < 0) {
3367 __ Daddu(TMP, TMP, dividend);
3368 } else if (imm < 0 && magic > 0) {
3369 __ Dsubu(TMP, TMP, dividend);
3370 }
3371
3372 if (shift >= 32) {
3373 __ Dsra32(TMP, TMP, shift - 32);
3374 } else if (shift > 0) {
3375 __ Dsra(TMP, TMP, shift);
3376 }
3377
3378 if (instruction->IsDiv()) {
3379 __ Dsra32(out, TMP, 31);
3380 __ Dsubu(out, TMP, out);
3381 } else {
3382 __ Dsra32(AT, TMP, 31);
3383 __ Dsubu(AT, TMP, AT);
3384 __ LoadConst64(TMP, imm);
3385 __ Dmul(TMP, AT, TMP);
3386 __ Dsubu(out, dividend, TMP);
3387 }
3388 }
3389}
3390
3391void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3392 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003393 DataType::Type type = instruction->GetResultType();
3394 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003395
3396 LocationSummary* locations = instruction->GetLocations();
3397 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3398 Location second = locations->InAt(1);
3399
3400 if (second.IsConstant()) {
3401 int64_t imm = Int64FromConstant(second.GetConstant());
3402 if (imm == 0) {
3403 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3404 } else if (imm == 1 || imm == -1) {
3405 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003406 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003407 DivRemByPowerOfTwo(instruction);
3408 } else {
3409 DCHECK(imm <= -2 || imm >= 2);
3410 GenerateDivRemWithAnyConstant(instruction);
3411 }
3412 } else {
3413 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3414 GpuRegister divisor = second.AsRegister<GpuRegister>();
3415 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003416 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003417 __ DivR6(out, dividend, divisor);
3418 else
3419 __ Ddiv(out, dividend, divisor);
3420 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003421 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003422 __ ModR6(out, dividend, divisor);
3423 else
3424 __ Dmod(out, dividend, divisor);
3425 }
3426 }
3427}
3428
Alexey Frunze4dda3372015-06-01 18:31:49 -07003429void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3430 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003431 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003432 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003433 case DataType::Type::kInt32:
3434 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003435 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003436 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3438 break;
3439
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003440 case DataType::Type::kFloat32:
3441 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003442 locations->SetInAt(0, Location::RequiresFpuRegister());
3443 locations->SetInAt(1, Location::RequiresFpuRegister());
3444 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3445 break;
3446
3447 default:
3448 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3449 }
3450}
3451
3452void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003454 LocationSummary* locations = instruction->GetLocations();
3455
3456 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003457 case DataType::Type::kInt32:
3458 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003459 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003460 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 case DataType::Type::kFloat32:
3462 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003463 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3464 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3465 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003467 __ DivS(dst, lhs, rhs);
3468 else
3469 __ DivD(dst, lhs, rhs);
3470 break;
3471 }
3472 default:
3473 LOG(FATAL) << "Unexpected div type " << type;
3474 }
3475}
3476
3477void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003478 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003479 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003480}
3481
3482void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3483 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003484 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003485 codegen_->AddSlowPath(slow_path);
3486 Location value = instruction->GetLocations()->InAt(0);
3487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003488 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003491 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003492 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003493 }
3494
3495 if (value.IsConstant()) {
3496 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3497 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003498 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003499 } else {
3500 // A division by a non-null constant is valid. We don't need to perform
3501 // any check, so simply fall through.
3502 }
3503 } else {
3504 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3505 }
3506}
3507
3508void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3509 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003510 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003511 locations->SetOut(Location::ConstantLocation(constant));
3512}
3513
3514void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3515 // Will be generated at use site.
3516}
3517
3518void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3519 exit->SetLocations(nullptr);
3520}
3521
3522void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3523}
3524
3525void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3526 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003527 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003528 locations->SetOut(Location::ConstantLocation(constant));
3529}
3530
3531void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3532 // Will be generated at use site.
3533}
3534
David Brazdilfc6a86a2015-06-26 10:33:45 +00003535void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003536 if (successor->IsExitBlock()) {
3537 DCHECK(got->GetPrevious()->AlwaysThrows());
3538 return; // no code needed
3539 }
3540
Alexey Frunze4dda3372015-06-01 18:31:49 -07003541 HBasicBlock* block = got->GetBlock();
3542 HInstruction* previous = got->GetPrevious();
3543 HLoopInformation* info = block->GetLoopInformation();
3544
3545 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003546 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3547 return;
3548 }
3549 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3550 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3551 }
3552 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003553 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003554 }
3555}
3556
David Brazdilfc6a86a2015-06-26 10:33:45 +00003557void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3558 got->SetLocations(nullptr);
3559}
3560
3561void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3562 HandleGoto(got, got->GetSuccessor());
3563}
3564
3565void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3566 try_boundary->SetLocations(nullptr);
3567}
3568
3569void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3570 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3571 if (!successor->IsExitBlock()) {
3572 HandleGoto(try_boundary, successor);
3573 }
3574}
3575
Alexey Frunze299a9392015-12-08 16:08:02 -08003576void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3577 bool is64bit,
3578 LocationSummary* locations) {
3579 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3580 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3581 Location rhs_location = locations->InAt(1);
3582 GpuRegister rhs_reg = ZERO;
3583 int64_t rhs_imm = 0;
3584 bool use_imm = rhs_location.IsConstant();
3585 if (use_imm) {
3586 if (is64bit) {
3587 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3588 } else {
3589 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3590 }
3591 } else {
3592 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3593 }
3594 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3595
3596 switch (cond) {
3597 case kCondEQ:
3598 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003599 if (use_imm && IsInt<16>(-rhs_imm)) {
3600 if (rhs_imm == 0) {
3601 if (cond == kCondEQ) {
3602 __ Sltiu(dst, lhs, 1);
3603 } else {
3604 __ Sltu(dst, ZERO, lhs);
3605 }
3606 } else {
3607 if (is64bit) {
3608 __ Daddiu(dst, lhs, -rhs_imm);
3609 } else {
3610 __ Addiu(dst, lhs, -rhs_imm);
3611 }
3612 if (cond == kCondEQ) {
3613 __ Sltiu(dst, dst, 1);
3614 } else {
3615 __ Sltu(dst, ZERO, dst);
3616 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003617 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003618 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003619 if (use_imm && IsUint<16>(rhs_imm)) {
3620 __ Xori(dst, lhs, rhs_imm);
3621 } else {
3622 if (use_imm) {
3623 rhs_reg = TMP;
3624 __ LoadConst64(rhs_reg, rhs_imm);
3625 }
3626 __ Xor(dst, lhs, rhs_reg);
3627 }
3628 if (cond == kCondEQ) {
3629 __ Sltiu(dst, dst, 1);
3630 } else {
3631 __ Sltu(dst, ZERO, dst);
3632 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003633 }
3634 break;
3635
3636 case kCondLT:
3637 case kCondGE:
3638 if (use_imm && IsInt<16>(rhs_imm)) {
3639 __ Slti(dst, lhs, rhs_imm);
3640 } else {
3641 if (use_imm) {
3642 rhs_reg = TMP;
3643 __ LoadConst64(rhs_reg, rhs_imm);
3644 }
3645 __ Slt(dst, lhs, rhs_reg);
3646 }
3647 if (cond == kCondGE) {
3648 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3649 // only the slt instruction but no sge.
3650 __ Xori(dst, dst, 1);
3651 }
3652 break;
3653
3654 case kCondLE:
3655 case kCondGT:
3656 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3657 // Simulate lhs <= rhs via lhs < rhs + 1.
3658 __ Slti(dst, lhs, rhs_imm_plus_one);
3659 if (cond == kCondGT) {
3660 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3661 // only the slti instruction but no sgti.
3662 __ Xori(dst, dst, 1);
3663 }
3664 } else {
3665 if (use_imm) {
3666 rhs_reg = TMP;
3667 __ LoadConst64(rhs_reg, rhs_imm);
3668 }
3669 __ Slt(dst, rhs_reg, lhs);
3670 if (cond == kCondLE) {
3671 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3672 // only the slt instruction but no sle.
3673 __ Xori(dst, dst, 1);
3674 }
3675 }
3676 break;
3677
3678 case kCondB:
3679 case kCondAE:
3680 if (use_imm && IsInt<16>(rhs_imm)) {
3681 // Sltiu sign-extends its 16-bit immediate operand before
3682 // the comparison and thus lets us compare directly with
3683 // unsigned values in the ranges [0, 0x7fff] and
3684 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3685 __ Sltiu(dst, lhs, rhs_imm);
3686 } else {
3687 if (use_imm) {
3688 rhs_reg = TMP;
3689 __ LoadConst64(rhs_reg, rhs_imm);
3690 }
3691 __ Sltu(dst, lhs, rhs_reg);
3692 }
3693 if (cond == kCondAE) {
3694 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3695 // only the sltu instruction but no sgeu.
3696 __ Xori(dst, dst, 1);
3697 }
3698 break;
3699
3700 case kCondBE:
3701 case kCondA:
3702 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3703 // Simulate lhs <= rhs via lhs < rhs + 1.
3704 // Note that this only works if rhs + 1 does not overflow
3705 // to 0, hence the check above.
3706 // Sltiu sign-extends its 16-bit immediate operand before
3707 // the comparison and thus lets us compare directly with
3708 // unsigned values in the ranges [0, 0x7fff] and
3709 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3710 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3711 if (cond == kCondA) {
3712 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3713 // only the sltiu instruction but no sgtiu.
3714 __ Xori(dst, dst, 1);
3715 }
3716 } else {
3717 if (use_imm) {
3718 rhs_reg = TMP;
3719 __ LoadConst64(rhs_reg, rhs_imm);
3720 }
3721 __ Sltu(dst, rhs_reg, lhs);
3722 if (cond == kCondBE) {
3723 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3724 // only the sltu instruction but no sleu.
3725 __ Xori(dst, dst, 1);
3726 }
3727 }
3728 break;
3729 }
3730}
3731
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003732bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3733 bool is64bit,
3734 LocationSummary* input_locations,
3735 GpuRegister dst) {
3736 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3737 Location rhs_location = input_locations->InAt(1);
3738 GpuRegister rhs_reg = ZERO;
3739 int64_t rhs_imm = 0;
3740 bool use_imm = rhs_location.IsConstant();
3741 if (use_imm) {
3742 if (is64bit) {
3743 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3744 } else {
3745 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3746 }
3747 } else {
3748 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3749 }
3750 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3751
3752 switch (cond) {
3753 case kCondEQ:
3754 case kCondNE:
3755 if (use_imm && IsInt<16>(-rhs_imm)) {
3756 if (is64bit) {
3757 __ Daddiu(dst, lhs, -rhs_imm);
3758 } else {
3759 __ Addiu(dst, lhs, -rhs_imm);
3760 }
3761 } else if (use_imm && IsUint<16>(rhs_imm)) {
3762 __ Xori(dst, lhs, rhs_imm);
3763 } else {
3764 if (use_imm) {
3765 rhs_reg = TMP;
3766 __ LoadConst64(rhs_reg, rhs_imm);
3767 }
3768 __ Xor(dst, lhs, rhs_reg);
3769 }
3770 return (cond == kCondEQ);
3771
3772 case kCondLT:
3773 case kCondGE:
3774 if (use_imm && IsInt<16>(rhs_imm)) {
3775 __ Slti(dst, lhs, rhs_imm);
3776 } else {
3777 if (use_imm) {
3778 rhs_reg = TMP;
3779 __ LoadConst64(rhs_reg, rhs_imm);
3780 }
3781 __ Slt(dst, lhs, rhs_reg);
3782 }
3783 return (cond == kCondGE);
3784
3785 case kCondLE:
3786 case kCondGT:
3787 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3788 // Simulate lhs <= rhs via lhs < rhs + 1.
3789 __ Slti(dst, lhs, rhs_imm_plus_one);
3790 return (cond == kCondGT);
3791 } else {
3792 if (use_imm) {
3793 rhs_reg = TMP;
3794 __ LoadConst64(rhs_reg, rhs_imm);
3795 }
3796 __ Slt(dst, rhs_reg, lhs);
3797 return (cond == kCondLE);
3798 }
3799
3800 case kCondB:
3801 case kCondAE:
3802 if (use_imm && IsInt<16>(rhs_imm)) {
3803 // Sltiu sign-extends its 16-bit immediate operand before
3804 // the comparison and thus lets us compare directly with
3805 // unsigned values in the ranges [0, 0x7fff] and
3806 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3807 __ Sltiu(dst, lhs, rhs_imm);
3808 } else {
3809 if (use_imm) {
3810 rhs_reg = TMP;
3811 __ LoadConst64(rhs_reg, rhs_imm);
3812 }
3813 __ Sltu(dst, lhs, rhs_reg);
3814 }
3815 return (cond == kCondAE);
3816
3817 case kCondBE:
3818 case kCondA:
3819 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3820 // Simulate lhs <= rhs via lhs < rhs + 1.
3821 // Note that this only works if rhs + 1 does not overflow
3822 // to 0, hence the check above.
3823 // Sltiu sign-extends its 16-bit immediate operand before
3824 // the comparison and thus lets us compare directly with
3825 // unsigned values in the ranges [0, 0x7fff] and
3826 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3827 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3828 return (cond == kCondA);
3829 } else {
3830 if (use_imm) {
3831 rhs_reg = TMP;
3832 __ LoadConst64(rhs_reg, rhs_imm);
3833 }
3834 __ Sltu(dst, rhs_reg, lhs);
3835 return (cond == kCondBE);
3836 }
3837 }
3838}
3839
Alexey Frunze299a9392015-12-08 16:08:02 -08003840void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3841 bool is64bit,
3842 LocationSummary* locations,
3843 Mips64Label* label) {
3844 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3845 Location rhs_location = locations->InAt(1);
3846 GpuRegister rhs_reg = ZERO;
3847 int64_t rhs_imm = 0;
3848 bool use_imm = rhs_location.IsConstant();
3849 if (use_imm) {
3850 if (is64bit) {
3851 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3852 } else {
3853 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3854 }
3855 } else {
3856 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3857 }
3858
3859 if (use_imm && rhs_imm == 0) {
3860 switch (cond) {
3861 case kCondEQ:
3862 case kCondBE: // <= 0 if zero
3863 __ Beqzc(lhs, label);
3864 break;
3865 case kCondNE:
3866 case kCondA: // > 0 if non-zero
3867 __ Bnezc(lhs, label);
3868 break;
3869 case kCondLT:
3870 __ Bltzc(lhs, label);
3871 break;
3872 case kCondGE:
3873 __ Bgezc(lhs, label);
3874 break;
3875 case kCondLE:
3876 __ Blezc(lhs, label);
3877 break;
3878 case kCondGT:
3879 __ Bgtzc(lhs, label);
3880 break;
3881 case kCondB: // always false
3882 break;
3883 case kCondAE: // always true
3884 __ Bc(label);
3885 break;
3886 }
3887 } else {
3888 if (use_imm) {
3889 rhs_reg = TMP;
3890 __ LoadConst64(rhs_reg, rhs_imm);
3891 }
3892 switch (cond) {
3893 case kCondEQ:
3894 __ Beqc(lhs, rhs_reg, label);
3895 break;
3896 case kCondNE:
3897 __ Bnec(lhs, rhs_reg, label);
3898 break;
3899 case kCondLT:
3900 __ Bltc(lhs, rhs_reg, label);
3901 break;
3902 case kCondGE:
3903 __ Bgec(lhs, rhs_reg, label);
3904 break;
3905 case kCondLE:
3906 __ Bgec(rhs_reg, lhs, label);
3907 break;
3908 case kCondGT:
3909 __ Bltc(rhs_reg, lhs, label);
3910 break;
3911 case kCondB:
3912 __ Bltuc(lhs, rhs_reg, label);
3913 break;
3914 case kCondAE:
3915 __ Bgeuc(lhs, rhs_reg, label);
3916 break;
3917 case kCondBE:
3918 __ Bgeuc(rhs_reg, lhs, label);
3919 break;
3920 case kCondA:
3921 __ Bltuc(rhs_reg, lhs, label);
3922 break;
3923 }
3924 }
3925}
3926
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003927void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3928 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003929 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003930 LocationSummary* locations) {
3931 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3932 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3933 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003935 switch (cond) {
3936 case kCondEQ:
3937 __ CmpEqS(FTMP, lhs, rhs);
3938 __ Mfc1(dst, FTMP);
3939 __ Andi(dst, dst, 1);
3940 break;
3941 case kCondNE:
3942 __ CmpEqS(FTMP, lhs, rhs);
3943 __ Mfc1(dst, FTMP);
3944 __ Addiu(dst, dst, 1);
3945 break;
3946 case kCondLT:
3947 if (gt_bias) {
3948 __ CmpLtS(FTMP, lhs, rhs);
3949 } else {
3950 __ CmpUltS(FTMP, lhs, rhs);
3951 }
3952 __ Mfc1(dst, FTMP);
3953 __ Andi(dst, dst, 1);
3954 break;
3955 case kCondLE:
3956 if (gt_bias) {
3957 __ CmpLeS(FTMP, lhs, rhs);
3958 } else {
3959 __ CmpUleS(FTMP, lhs, rhs);
3960 }
3961 __ Mfc1(dst, FTMP);
3962 __ Andi(dst, dst, 1);
3963 break;
3964 case kCondGT:
3965 if (gt_bias) {
3966 __ CmpUltS(FTMP, rhs, lhs);
3967 } else {
3968 __ CmpLtS(FTMP, rhs, lhs);
3969 }
3970 __ Mfc1(dst, FTMP);
3971 __ Andi(dst, dst, 1);
3972 break;
3973 case kCondGE:
3974 if (gt_bias) {
3975 __ CmpUleS(FTMP, rhs, lhs);
3976 } else {
3977 __ CmpLeS(FTMP, rhs, lhs);
3978 }
3979 __ Mfc1(dst, FTMP);
3980 __ Andi(dst, dst, 1);
3981 break;
3982 default:
3983 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3984 UNREACHABLE();
3985 }
3986 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003987 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003988 switch (cond) {
3989 case kCondEQ:
3990 __ CmpEqD(FTMP, lhs, rhs);
3991 __ Mfc1(dst, FTMP);
3992 __ Andi(dst, dst, 1);
3993 break;
3994 case kCondNE:
3995 __ CmpEqD(FTMP, lhs, rhs);
3996 __ Mfc1(dst, FTMP);
3997 __ Addiu(dst, dst, 1);
3998 break;
3999 case kCondLT:
4000 if (gt_bias) {
4001 __ CmpLtD(FTMP, lhs, rhs);
4002 } else {
4003 __ CmpUltD(FTMP, lhs, rhs);
4004 }
4005 __ Mfc1(dst, FTMP);
4006 __ Andi(dst, dst, 1);
4007 break;
4008 case kCondLE:
4009 if (gt_bias) {
4010 __ CmpLeD(FTMP, lhs, rhs);
4011 } else {
4012 __ CmpUleD(FTMP, lhs, rhs);
4013 }
4014 __ Mfc1(dst, FTMP);
4015 __ Andi(dst, dst, 1);
4016 break;
4017 case kCondGT:
4018 if (gt_bias) {
4019 __ CmpUltD(FTMP, rhs, lhs);
4020 } else {
4021 __ CmpLtD(FTMP, rhs, lhs);
4022 }
4023 __ Mfc1(dst, FTMP);
4024 __ Andi(dst, dst, 1);
4025 break;
4026 case kCondGE:
4027 if (gt_bias) {
4028 __ CmpUleD(FTMP, rhs, lhs);
4029 } else {
4030 __ CmpLeD(FTMP, rhs, lhs);
4031 }
4032 __ Mfc1(dst, FTMP);
4033 __ Andi(dst, dst, 1);
4034 break;
4035 default:
4036 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4037 UNREACHABLE();
4038 }
4039 }
4040}
4041
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004042bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4043 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004044 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004045 LocationSummary* input_locations,
4046 FpuRegister dst) {
4047 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4048 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004049 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004050 switch (cond) {
4051 case kCondEQ:
4052 __ CmpEqS(dst, lhs, rhs);
4053 return false;
4054 case kCondNE:
4055 __ CmpEqS(dst, lhs, rhs);
4056 return true;
4057 case kCondLT:
4058 if (gt_bias) {
4059 __ CmpLtS(dst, lhs, rhs);
4060 } else {
4061 __ CmpUltS(dst, lhs, rhs);
4062 }
4063 return false;
4064 case kCondLE:
4065 if (gt_bias) {
4066 __ CmpLeS(dst, lhs, rhs);
4067 } else {
4068 __ CmpUleS(dst, lhs, rhs);
4069 }
4070 return false;
4071 case kCondGT:
4072 if (gt_bias) {
4073 __ CmpUltS(dst, rhs, lhs);
4074 } else {
4075 __ CmpLtS(dst, rhs, lhs);
4076 }
4077 return false;
4078 case kCondGE:
4079 if (gt_bias) {
4080 __ CmpUleS(dst, rhs, lhs);
4081 } else {
4082 __ CmpLeS(dst, rhs, lhs);
4083 }
4084 return false;
4085 default:
4086 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4087 UNREACHABLE();
4088 }
4089 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004090 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004091 switch (cond) {
4092 case kCondEQ:
4093 __ CmpEqD(dst, lhs, rhs);
4094 return false;
4095 case kCondNE:
4096 __ CmpEqD(dst, lhs, rhs);
4097 return true;
4098 case kCondLT:
4099 if (gt_bias) {
4100 __ CmpLtD(dst, lhs, rhs);
4101 } else {
4102 __ CmpUltD(dst, lhs, rhs);
4103 }
4104 return false;
4105 case kCondLE:
4106 if (gt_bias) {
4107 __ CmpLeD(dst, lhs, rhs);
4108 } else {
4109 __ CmpUleD(dst, lhs, rhs);
4110 }
4111 return false;
4112 case kCondGT:
4113 if (gt_bias) {
4114 __ CmpUltD(dst, rhs, lhs);
4115 } else {
4116 __ CmpLtD(dst, rhs, lhs);
4117 }
4118 return false;
4119 case kCondGE:
4120 if (gt_bias) {
4121 __ CmpUleD(dst, rhs, lhs);
4122 } else {
4123 __ CmpLeD(dst, rhs, lhs);
4124 }
4125 return false;
4126 default:
4127 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4128 UNREACHABLE();
4129 }
4130 }
4131}
4132
Alexey Frunze299a9392015-12-08 16:08:02 -08004133void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4134 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004135 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004136 LocationSummary* locations,
4137 Mips64Label* label) {
4138 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4139 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004140 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004141 switch (cond) {
4142 case kCondEQ:
4143 __ CmpEqS(FTMP, lhs, rhs);
4144 __ Bc1nez(FTMP, label);
4145 break;
4146 case kCondNE:
4147 __ CmpEqS(FTMP, lhs, rhs);
4148 __ Bc1eqz(FTMP, label);
4149 break;
4150 case kCondLT:
4151 if (gt_bias) {
4152 __ CmpLtS(FTMP, lhs, rhs);
4153 } else {
4154 __ CmpUltS(FTMP, lhs, rhs);
4155 }
4156 __ Bc1nez(FTMP, label);
4157 break;
4158 case kCondLE:
4159 if (gt_bias) {
4160 __ CmpLeS(FTMP, lhs, rhs);
4161 } else {
4162 __ CmpUleS(FTMP, lhs, rhs);
4163 }
4164 __ Bc1nez(FTMP, label);
4165 break;
4166 case kCondGT:
4167 if (gt_bias) {
4168 __ CmpUltS(FTMP, rhs, lhs);
4169 } else {
4170 __ CmpLtS(FTMP, rhs, lhs);
4171 }
4172 __ Bc1nez(FTMP, label);
4173 break;
4174 case kCondGE:
4175 if (gt_bias) {
4176 __ CmpUleS(FTMP, rhs, lhs);
4177 } else {
4178 __ CmpLeS(FTMP, rhs, lhs);
4179 }
4180 __ Bc1nez(FTMP, label);
4181 break;
4182 default:
4183 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004184 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004185 }
4186 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004187 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004188 switch (cond) {
4189 case kCondEQ:
4190 __ CmpEqD(FTMP, lhs, rhs);
4191 __ Bc1nez(FTMP, label);
4192 break;
4193 case kCondNE:
4194 __ CmpEqD(FTMP, lhs, rhs);
4195 __ Bc1eqz(FTMP, label);
4196 break;
4197 case kCondLT:
4198 if (gt_bias) {
4199 __ CmpLtD(FTMP, lhs, rhs);
4200 } else {
4201 __ CmpUltD(FTMP, lhs, rhs);
4202 }
4203 __ Bc1nez(FTMP, label);
4204 break;
4205 case kCondLE:
4206 if (gt_bias) {
4207 __ CmpLeD(FTMP, lhs, rhs);
4208 } else {
4209 __ CmpUleD(FTMP, lhs, rhs);
4210 }
4211 __ Bc1nez(FTMP, label);
4212 break;
4213 case kCondGT:
4214 if (gt_bias) {
4215 __ CmpUltD(FTMP, rhs, lhs);
4216 } else {
4217 __ CmpLtD(FTMP, rhs, lhs);
4218 }
4219 __ Bc1nez(FTMP, label);
4220 break;
4221 case kCondGE:
4222 if (gt_bias) {
4223 __ CmpUleD(FTMP, rhs, lhs);
4224 } else {
4225 __ CmpLeD(FTMP, rhs, lhs);
4226 }
4227 __ Bc1nez(FTMP, label);
4228 break;
4229 default:
4230 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004231 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004232 }
4233 }
4234}
4235
Alexey Frunze4dda3372015-06-01 18:31:49 -07004236void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004237 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004238 Mips64Label* true_target,
4239 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004240 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004241
David Brazdil0debae72015-11-12 18:37:00 +00004242 if (true_target == nullptr && false_target == nullptr) {
4243 // Nothing to do. The code always falls through.
4244 return;
4245 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004246 // Constant condition, statically compared against "true" (integer value 1).
4247 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004248 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004249 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004250 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004251 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004252 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004253 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004254 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004255 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004256 }
David Brazdil0debae72015-11-12 18:37:00 +00004257 return;
4258 }
4259
4260 // The following code generates these patterns:
4261 // (1) true_target == nullptr && false_target != nullptr
4262 // - opposite condition true => branch to false_target
4263 // (2) true_target != nullptr && false_target == nullptr
4264 // - condition true => branch to true_target
4265 // (3) true_target != nullptr && false_target != nullptr
4266 // - condition true => branch to true_target
4267 // - branch to false_target
4268 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004269 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004270 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004271 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004272 if (true_target == nullptr) {
4273 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4274 } else {
4275 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4276 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004277 } else {
4278 // The condition instruction has not been materialized, use its inputs as
4279 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004280 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004281 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004282 LocationSummary* locations = cond->GetLocations();
4283 IfCondition if_cond = condition->GetCondition();
4284 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004285
David Brazdil0debae72015-11-12 18:37:00 +00004286 if (true_target == nullptr) {
4287 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004288 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004289 }
4290
Alexey Frunze299a9392015-12-08 16:08:02 -08004291 switch (type) {
4292 default:
4293 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4294 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004296 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004298 case DataType::Type::kFloat32:
4299 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004300 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4301 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004302 }
4303 }
David Brazdil0debae72015-11-12 18:37:00 +00004304
4305 // If neither branch falls through (case 3), the conditional branch to `true_target`
4306 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4307 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004308 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004309 }
4310}
4311
4312void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004313 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004314 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004315 locations->SetInAt(0, Location::RequiresRegister());
4316 }
4317}
4318
4319void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004320 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4321 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004322 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004323 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004324 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004325 nullptr : codegen_->GetLabelOf(false_successor);
4326 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004327}
4328
4329void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004330 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004331 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004332 InvokeRuntimeCallingConvention calling_convention;
4333 RegisterSet caller_saves = RegisterSet::Empty();
4334 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4335 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004336 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004337 locations->SetInAt(0, Location::RequiresRegister());
4338 }
4339}
4340
4341void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004342 SlowPathCodeMIPS64* slow_path =
4343 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004344 GenerateTestAndBranch(deoptimize,
4345 /* condition_input_index */ 0,
4346 slow_path->GetEntryLabel(),
4347 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004348}
4349
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004350// This function returns true if a conditional move can be generated for HSelect.
4351// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4352// branches and regular moves.
4353//
4354// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4355//
4356// While determining feasibility of a conditional move and setting inputs/outputs
4357// are two distinct tasks, this function does both because they share quite a bit
4358// of common logic.
4359static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4360 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4361 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4362 HCondition* condition = cond->AsCondition();
4363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004364 DataType::Type cond_type =
4365 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4366 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004367
4368 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4369 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4370 bool is_true_value_zero_constant =
4371 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4372 bool is_false_value_zero_constant =
4373 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4374
4375 bool can_move_conditionally = false;
4376 bool use_const_for_false_in = false;
4377 bool use_const_for_true_in = false;
4378
4379 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004380 if (!DataType::IsFloatingPointType(cond_type)) {
4381 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004382 // Moving int/long on int/long condition.
4383 if (is_true_value_zero_constant) {
4384 // seleqz out_reg, false_reg, cond_reg
4385 can_move_conditionally = true;
4386 use_const_for_true_in = true;
4387 } else if (is_false_value_zero_constant) {
4388 // selnez out_reg, true_reg, cond_reg
4389 can_move_conditionally = true;
4390 use_const_for_false_in = true;
4391 } else if (materialized) {
4392 // Not materializing unmaterialized int conditions
4393 // to keep the instruction count low.
4394 // selnez AT, true_reg, cond_reg
4395 // seleqz TMP, false_reg, cond_reg
4396 // or out_reg, AT, TMP
4397 can_move_conditionally = true;
4398 }
4399 } else {
4400 // Moving float/double on int/long condition.
4401 if (materialized) {
4402 // Not materializing unmaterialized int conditions
4403 // to keep the instruction count low.
4404 can_move_conditionally = true;
4405 if (is_true_value_zero_constant) {
4406 // sltu TMP, ZERO, cond_reg
4407 // mtc1 TMP, temp_cond_reg
4408 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4409 use_const_for_true_in = true;
4410 } else if (is_false_value_zero_constant) {
4411 // sltu TMP, ZERO, cond_reg
4412 // mtc1 TMP, temp_cond_reg
4413 // selnez.fmt out_reg, true_reg, temp_cond_reg
4414 use_const_for_false_in = true;
4415 } else {
4416 // sltu TMP, ZERO, cond_reg
4417 // mtc1 TMP, temp_cond_reg
4418 // sel.fmt temp_cond_reg, false_reg, true_reg
4419 // mov.fmt out_reg, temp_cond_reg
4420 }
4421 }
4422 }
4423 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004424 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004425 // Moving int/long on float/double condition.
4426 can_move_conditionally = true;
4427 if (is_true_value_zero_constant) {
4428 // mfc1 TMP, temp_cond_reg
4429 // seleqz out_reg, false_reg, TMP
4430 use_const_for_true_in = true;
4431 } else if (is_false_value_zero_constant) {
4432 // mfc1 TMP, temp_cond_reg
4433 // selnez out_reg, true_reg, TMP
4434 use_const_for_false_in = true;
4435 } else {
4436 // mfc1 TMP, temp_cond_reg
4437 // selnez AT, true_reg, TMP
4438 // seleqz TMP, false_reg, TMP
4439 // or out_reg, AT, TMP
4440 }
4441 } else {
4442 // Moving float/double on float/double condition.
4443 can_move_conditionally = true;
4444 if (is_true_value_zero_constant) {
4445 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4446 use_const_for_true_in = true;
4447 } else if (is_false_value_zero_constant) {
4448 // selnez.fmt out_reg, true_reg, temp_cond_reg
4449 use_const_for_false_in = true;
4450 } else {
4451 // sel.fmt temp_cond_reg, false_reg, true_reg
4452 // mov.fmt out_reg, temp_cond_reg
4453 }
4454 }
4455 }
4456 }
4457
4458 if (can_move_conditionally) {
4459 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4460 } else {
4461 DCHECK(!use_const_for_false_in);
4462 DCHECK(!use_const_for_true_in);
4463 }
4464
4465 if (locations_to_set != nullptr) {
4466 if (use_const_for_false_in) {
4467 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4468 } else {
4469 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004470 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004471 ? Location::RequiresFpuRegister()
4472 : Location::RequiresRegister());
4473 }
4474 if (use_const_for_true_in) {
4475 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4476 } else {
4477 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004478 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004479 ? Location::RequiresFpuRegister()
4480 : Location::RequiresRegister());
4481 }
4482 if (materialized) {
4483 locations_to_set->SetInAt(2, Location::RequiresRegister());
4484 }
4485
4486 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004487 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004488 ? Location::RequiresFpuRegister()
4489 : Location::RequiresRegister());
4490 } else {
4491 locations_to_set->SetOut(Location::SameAsFirstInput());
4492 }
4493 }
4494
4495 return can_move_conditionally;
4496}
4497
4498
4499void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4500 LocationSummary* locations = select->GetLocations();
4501 Location dst = locations->Out();
4502 Location false_src = locations->InAt(0);
4503 Location true_src = locations->InAt(1);
4504 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4505 GpuRegister cond_reg = TMP;
4506 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004507 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004508 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004509 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004510
4511 if (IsBooleanValueOrMaterializedCondition(cond)) {
4512 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4513 } else {
4514 HCondition* condition = cond->AsCondition();
4515 LocationSummary* cond_locations = cond->GetLocations();
4516 IfCondition if_cond = condition->GetCondition();
4517 cond_type = condition->InputAt(0)->GetType();
4518 switch (cond_type) {
4519 default:
4520 cond_inverted = MaterializeIntLongCompare(if_cond,
4521 /* is64bit */ false,
4522 cond_locations,
4523 cond_reg);
4524 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004525 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004526 cond_inverted = MaterializeIntLongCompare(if_cond,
4527 /* is64bit */ true,
4528 cond_locations,
4529 cond_reg);
4530 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004531 case DataType::Type::kFloat32:
4532 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004533 cond_inverted = MaterializeFpCompare(if_cond,
4534 condition->IsGtBias(),
4535 cond_type,
4536 cond_locations,
4537 fcond_reg);
4538 break;
4539 }
4540 }
4541
4542 if (true_src.IsConstant()) {
4543 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4544 }
4545 if (false_src.IsConstant()) {
4546 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4547 }
4548
4549 switch (dst_type) {
4550 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004551 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004552 __ Mfc1(cond_reg, fcond_reg);
4553 }
4554 if (true_src.IsConstant()) {
4555 if (cond_inverted) {
4556 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4557 } else {
4558 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4559 }
4560 } else if (false_src.IsConstant()) {
4561 if (cond_inverted) {
4562 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4563 } else {
4564 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4565 }
4566 } else {
4567 DCHECK_NE(cond_reg, AT);
4568 if (cond_inverted) {
4569 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4570 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4571 } else {
4572 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4573 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4574 }
4575 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4576 }
4577 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004578 case DataType::Type::kFloat32: {
4579 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004580 // sel*.fmt tests bit 0 of the condition register, account for that.
4581 __ Sltu(TMP, ZERO, cond_reg);
4582 __ Mtc1(TMP, fcond_reg);
4583 }
4584 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4585 if (true_src.IsConstant()) {
4586 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4587 if (cond_inverted) {
4588 __ SelnezS(dst_reg, src_reg, fcond_reg);
4589 } else {
4590 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4591 }
4592 } else if (false_src.IsConstant()) {
4593 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4594 if (cond_inverted) {
4595 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4596 } else {
4597 __ SelnezS(dst_reg, src_reg, fcond_reg);
4598 }
4599 } else {
4600 if (cond_inverted) {
4601 __ SelS(fcond_reg,
4602 true_src.AsFpuRegister<FpuRegister>(),
4603 false_src.AsFpuRegister<FpuRegister>());
4604 } else {
4605 __ SelS(fcond_reg,
4606 false_src.AsFpuRegister<FpuRegister>(),
4607 true_src.AsFpuRegister<FpuRegister>());
4608 }
4609 __ MovS(dst_reg, fcond_reg);
4610 }
4611 break;
4612 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004613 case DataType::Type::kFloat64: {
4614 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004615 // sel*.fmt tests bit 0 of the condition register, account for that.
4616 __ Sltu(TMP, ZERO, cond_reg);
4617 __ Mtc1(TMP, fcond_reg);
4618 }
4619 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4620 if (true_src.IsConstant()) {
4621 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4622 if (cond_inverted) {
4623 __ SelnezD(dst_reg, src_reg, fcond_reg);
4624 } else {
4625 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4626 }
4627 } else if (false_src.IsConstant()) {
4628 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4629 if (cond_inverted) {
4630 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4631 } else {
4632 __ SelnezD(dst_reg, src_reg, fcond_reg);
4633 }
4634 } else {
4635 if (cond_inverted) {
4636 __ SelD(fcond_reg,
4637 true_src.AsFpuRegister<FpuRegister>(),
4638 false_src.AsFpuRegister<FpuRegister>());
4639 } else {
4640 __ SelD(fcond_reg,
4641 false_src.AsFpuRegister<FpuRegister>(),
4642 true_src.AsFpuRegister<FpuRegister>());
4643 }
4644 __ MovD(dst_reg, fcond_reg);
4645 }
4646 break;
4647 }
4648 }
4649}
4650
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004651void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004652 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004653 LocationSummary(flag, LocationSummary::kNoCall);
4654 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004655}
4656
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004657void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4658 __ LoadFromOffset(kLoadWord,
4659 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4660 SP,
4661 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004662}
4663
David Brazdil74eb1b22015-12-14 11:44:01 +00004664void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004665 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004666 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004667}
4668
4669void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004670 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4671 GenConditionalMove(select);
4672 } else {
4673 LocationSummary* locations = select->GetLocations();
4674 Mips64Label false_target;
4675 GenerateTestAndBranch(select,
4676 /* condition_input_index */ 2,
4677 /* true_target */ nullptr,
4678 &false_target);
4679 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4680 __ Bind(&false_target);
4681 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004682}
4683
David Srbecky0cf44932015-12-09 14:09:59 +00004684void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004685 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004686}
4687
David Srbeckyd28f4a02016-03-14 17:14:24 +00004688void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4689 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004690}
4691
4692void CodeGeneratorMIPS64::GenerateNop() {
4693 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004694}
4695
Alexey Frunze4dda3372015-06-01 18:31:49 -07004696void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004697 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004698 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004699 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004700 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004701 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004702 instruction,
4703 object_field_get_with_read_barrier
4704 ? LocationSummary::kCallOnSlowPath
4705 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004706 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4707 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4708 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004709 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004710 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004711 locations->SetOut(Location::RequiresFpuRegister());
4712 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004713 // The output overlaps in the case of an object field get with
4714 // read barriers enabled: we do not want the move to overwrite the
4715 // object's location, as we need it to emit the read barrier.
4716 locations->SetOut(Location::RequiresRegister(),
4717 object_field_get_with_read_barrier
4718 ? Location::kOutputOverlap
4719 : Location::kNoOutputOverlap);
4720 }
4721 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4722 // We need a temporary register for the read barrier marking slow
4723 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004724 if (!kBakerReadBarrierThunksEnableForFields) {
4725 locations->AddTemp(Location::RequiresRegister());
4726 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004727 }
4728}
4729
4730void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4731 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004732 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4733 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004734 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004735 Location obj_loc = locations->InAt(0);
4736 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4737 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004738 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004739 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004740 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004741 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4742
Alexey Frunze4dda3372015-06-01 18:31:49 -07004743 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004744 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004745 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004746 load_type = kLoadUnsignedByte;
4747 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004748 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004749 load_type = kLoadSignedByte;
4750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004751 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004752 load_type = kLoadUnsignedHalfword;
4753 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004754 case DataType::Type::kInt16:
4755 load_type = kLoadSignedHalfword;
4756 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004757 case DataType::Type::kInt32:
4758 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004759 load_type = kLoadWord;
4760 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004761 case DataType::Type::kInt64:
4762 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004763 load_type = kLoadDoubleword;
4764 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004765 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004766 load_type = kLoadUnsignedWord;
4767 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004768 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004769 LOG(FATAL) << "Unreachable type " << type;
4770 UNREACHABLE();
4771 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004772 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004773 DCHECK(dst_loc.IsRegister());
4774 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004775 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004776 // /* HeapReference<Object> */ dst = *(obj + offset)
4777 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004778 Location temp_loc =
4779 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004780 // Note that a potential implicit null check is handled in this
4781 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4782 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4783 dst_loc,
4784 obj,
4785 offset,
4786 temp_loc,
4787 /* needs_null_check */ true);
4788 if (is_volatile) {
4789 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4790 }
4791 } else {
4792 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4793 if (is_volatile) {
4794 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4795 }
4796 // If read barriers are enabled, emit read barriers other than
4797 // Baker's using a slow path (and also unpoison the loaded
4798 // reference, if heap poisoning is enabled).
4799 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4800 }
4801 } else {
4802 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4803 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004804 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004805 DCHECK(dst_loc.IsFpuRegister());
4806 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004807 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004808 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004809
Alexey Frunze15958152017-02-09 19:08:30 -08004810 // Memory barriers, in the case of references, are handled in the
4811 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004812 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004813 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004814 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004815}
4816
4817void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4818 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4819 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004820 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004821 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004822 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004823 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004824 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004825 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004826 }
4827}
4828
4829void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004830 const FieldInfo& field_info,
4831 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004833 LocationSummary* locations = instruction->GetLocations();
4834 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004835 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004836 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004837 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004838 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4839 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004840 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4841
Alexey Frunze4dda3372015-06-01 18:31:49 -07004842 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004843 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004844 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004845 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004846 store_type = kStoreByte;
4847 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004848 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004849 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004850 store_type = kStoreHalfword;
4851 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 case DataType::Type::kInt32:
4853 case DataType::Type::kFloat32:
4854 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004855 store_type = kStoreWord;
4856 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004857 case DataType::Type::kInt64:
4858 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004859 store_type = kStoreDoubleword;
4860 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004861 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004862 LOG(FATAL) << "Unreachable type " << type;
4863 UNREACHABLE();
4864 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004865
Alexey Frunze15958152017-02-09 19:08:30 -08004866 if (is_volatile) {
4867 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4868 }
4869
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004870 if (value_location.IsConstant()) {
4871 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4872 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4873 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004874 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004875 DCHECK(value_location.IsRegister());
4876 GpuRegister src = value_location.AsRegister<GpuRegister>();
4877 if (kPoisonHeapReferences && needs_write_barrier) {
4878 // Note that in the case where `value` is a null reference,
4879 // we do not enter this block, as a null reference does not
4880 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004881 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004882 __ PoisonHeapReference(TMP, src);
4883 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4884 } else {
4885 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4886 }
4887 } else {
4888 DCHECK(value_location.IsFpuRegister());
4889 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4890 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4891 }
4892 }
Alexey Frunze15958152017-02-09 19:08:30 -08004893
Alexey Frunzec061de12017-02-14 13:27:23 -08004894 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004895 DCHECK(value_location.IsRegister());
4896 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004897 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004898 }
Alexey Frunze15958152017-02-09 19:08:30 -08004899
4900 if (is_volatile) {
4901 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4902 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004903}
4904
4905void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4906 HandleFieldGet(instruction, instruction->GetFieldInfo());
4907}
4908
4909void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4910 HandleFieldGet(instruction, instruction->GetFieldInfo());
4911}
4912
4913void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4914 HandleFieldSet(instruction, instruction->GetFieldInfo());
4915}
4916
4917void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004918 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004919}
4920
Alexey Frunze15958152017-02-09 19:08:30 -08004921void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4922 HInstruction* instruction,
4923 Location out,
4924 uint32_t offset,
4925 Location maybe_temp,
4926 ReadBarrierOption read_barrier_option) {
4927 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4928 if (read_barrier_option == kWithReadBarrier) {
4929 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004930 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4931 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4932 }
Alexey Frunze15958152017-02-09 19:08:30 -08004933 if (kUseBakerReadBarrier) {
4934 // Load with fast path based Baker's read barrier.
4935 // /* HeapReference<Object> */ out = *(out + offset)
4936 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4937 out,
4938 out_reg,
4939 offset,
4940 maybe_temp,
4941 /* needs_null_check */ false);
4942 } else {
4943 // Load with slow path based read barrier.
4944 // Save the value of `out` into `maybe_temp` before overwriting it
4945 // in the following move operation, as we will need it for the
4946 // read barrier below.
4947 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4948 // /* HeapReference<Object> */ out = *(out + offset)
4949 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4950 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4951 }
4952 } else {
4953 // Plain load with no read barrier.
4954 // /* HeapReference<Object> */ out = *(out + offset)
4955 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4956 __ MaybeUnpoisonHeapReference(out_reg);
4957 }
4958}
4959
4960void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4961 HInstruction* instruction,
4962 Location out,
4963 Location obj,
4964 uint32_t offset,
4965 Location maybe_temp,
4966 ReadBarrierOption read_barrier_option) {
4967 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4968 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4969 if (read_barrier_option == kWithReadBarrier) {
4970 CHECK(kEmitCompilerReadBarrier);
4971 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004972 if (!kBakerReadBarrierThunksEnableForFields) {
4973 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4974 }
Alexey Frunze15958152017-02-09 19:08:30 -08004975 // Load with fast path based Baker's read barrier.
4976 // /* HeapReference<Object> */ out = *(obj + offset)
4977 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4978 out,
4979 obj_reg,
4980 offset,
4981 maybe_temp,
4982 /* needs_null_check */ false);
4983 } else {
4984 // Load with slow path based read barrier.
4985 // /* HeapReference<Object> */ out = *(obj + offset)
4986 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4987 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4988 }
4989 } else {
4990 // Plain load with no read barrier.
4991 // /* HeapReference<Object> */ out = *(obj + offset)
4992 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4993 __ MaybeUnpoisonHeapReference(out_reg);
4994 }
4995}
4996
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004997static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4998 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4999 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5000 return reg - V0;
5001 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5002 return 13 + (reg - S2);
5003 } else if (reg == S8) { // One more.
5004 return 19;
5005 }
5006 LOG(FATAL) << "Unexpected register " << reg;
5007 UNREACHABLE();
5008}
5009
5010static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5011 int num = GetBakerMarkThunkNumber(reg) +
5012 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5013 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5014}
5015
5016static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5017 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5018 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5019}
5020
5021void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5022 Location root,
5023 GpuRegister obj,
5024 uint32_t offset,
5025 ReadBarrierOption read_barrier_option,
5026 Mips64Label* label_low) {
5027 if (label_low != nullptr) {
5028 DCHECK_EQ(offset, 0x5678u);
5029 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005030 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005031 if (read_barrier_option == kWithReadBarrier) {
5032 DCHECK(kEmitCompilerReadBarrier);
5033 if (kUseBakerReadBarrier) {
5034 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5035 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005036 if (kBakerReadBarrierThunksEnableForGcRoots) {
5037 // Note that we do not actually check the value of `GetIsGcMarking()`
5038 // to decide whether to mark the loaded GC root or not. Instead, we
5039 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5040 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5041 // vice versa.
5042 //
5043 // We use thunks for the slow path. That thunk checks the reference
5044 // and jumps to the entrypoint if needed.
5045 //
5046 // temp = Thread::Current()->pReadBarrierMarkReg00
5047 // // AKA &art_quick_read_barrier_mark_introspection.
5048 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5049 // if (temp != nullptr) {
5050 // temp = &gc_root_thunk<root_reg>
5051 // root = temp(root)
5052 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005053
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005054 const int32_t entry_point_offset =
5055 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5056 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5057 int16_t offset_low = Low16Bits(offset);
5058 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5059 // extension in lwu.
5060 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5061 GpuRegister base = short_offset ? obj : TMP;
5062 // Loading the entrypoint does not require a load acquire since it is only changed when
5063 // threads are suspended or running a checkpoint.
5064 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5065 if (!short_offset) {
5066 DCHECK(!label_low);
5067 __ Daui(base, obj, offset_high);
5068 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005069 Mips64Label skip_call;
5070 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005071 if (label_low != nullptr) {
5072 DCHECK(short_offset);
5073 __ Bind(label_low);
5074 }
5075 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5076 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5077 // in delay slot.
5078 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005079 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005080 } else {
5081 // Note that we do not actually check the value of `GetIsGcMarking()`
5082 // to decide whether to mark the loaded GC root or not. Instead, we
5083 // load into `temp` (T9) the read barrier mark entry point corresponding
5084 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5085 // is false, and vice versa.
5086 //
5087 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5088 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5089 // if (temp != null) {
5090 // root = temp(root)
5091 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005092
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005093 if (label_low != nullptr) {
5094 __ Bind(label_low);
5095 }
5096 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5097 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5098 static_assert(
5099 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5100 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5101 "have different sizes.");
5102 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5103 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5104 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005105
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005106 // Slow path marking the GC root `root`.
5107 Location temp = Location::RegisterLocation(T9);
5108 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005109 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005110 instruction,
5111 root,
5112 /*entrypoint*/ temp);
5113 codegen_->AddSlowPath(slow_path);
5114
5115 const int32_t entry_point_offset =
5116 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5117 // Loading the entrypoint does not require a load acquire since it is only changed when
5118 // threads are suspended or running a checkpoint.
5119 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5120 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5121 __ Bind(slow_path->GetExitLabel());
5122 }
Alexey Frunze15958152017-02-09 19:08:30 -08005123 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005124 if (label_low != nullptr) {
5125 __ Bind(label_low);
5126 }
Alexey Frunze15958152017-02-09 19:08:30 -08005127 // GC root loaded through a slow path for read barriers other
5128 // than Baker's.
5129 // /* GcRoot<mirror::Object>* */ root = obj + offset
5130 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5131 // /* mirror::Object* */ root = root->Read()
5132 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5133 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005134 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005135 if (label_low != nullptr) {
5136 __ Bind(label_low);
5137 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005138 // Plain GC root load with no read barrier.
5139 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5140 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5141 // Note that GC roots are not affected by heap poisoning, thus we
5142 // do not have to unpoison `root_reg` here.
5143 }
5144}
5145
Alexey Frunze15958152017-02-09 19:08:30 -08005146void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5147 Location ref,
5148 GpuRegister obj,
5149 uint32_t offset,
5150 Location temp,
5151 bool needs_null_check) {
5152 DCHECK(kEmitCompilerReadBarrier);
5153 DCHECK(kUseBakerReadBarrier);
5154
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005155 if (kBakerReadBarrierThunksEnableForFields) {
5156 // Note that we do not actually check the value of `GetIsGcMarking()`
5157 // to decide whether to mark the loaded reference or not. Instead, we
5158 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5159 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5160 // vice versa.
5161 //
5162 // We use thunks for the slow path. That thunk checks the reference
5163 // and jumps to the entrypoint if needed. If the holder is not gray,
5164 // it issues a load-load memory barrier and returns to the original
5165 // reference load.
5166 //
5167 // temp = Thread::Current()->pReadBarrierMarkReg00
5168 // // AKA &art_quick_read_barrier_mark_introspection.
5169 // if (temp != nullptr) {
5170 // temp = &field_array_thunk<holder_reg>
5171 // temp()
5172 // }
5173 // not_gray_return_address:
5174 // // If the offset is too large to fit into the lw instruction, we
5175 // // use an adjusted base register (TMP) here. This register
5176 // // receives bits 16 ... 31 of the offset before the thunk invocation
5177 // // and the thunk benefits from it.
5178 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5179 // gray_return_address:
5180
5181 DCHECK(temp.IsInvalid());
5182 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5183 const int32_t entry_point_offset =
5184 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5185 // There may have or may have not been a null check if the field offset is smaller than
5186 // the page size.
5187 // There must've been a null check in case it's actually a load from an array.
5188 // We will, however, perform an explicit null check in the thunk as it's easier to
5189 // do it than not.
5190 if (instruction->IsArrayGet()) {
5191 DCHECK(!needs_null_check);
5192 }
5193 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5194 // Loading the entrypoint does not require a load acquire since it is only changed when
5195 // threads are suspended or running a checkpoint.
5196 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5197 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005198 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005199 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005200 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005201 __ Nop(); // In forbidden slot.
5202 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005203 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005204 // /* HeapReference<Object> */ ref = *(obj + offset)
5205 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5206 } else {
5207 int16_t offset_low = Low16Bits(offset);
5208 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005209 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005210 __ Daui(TMP, obj, offset_high); // In delay slot.
5211 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005212 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005213 // /* HeapReference<Object> */ ref = *(obj + offset)
5214 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5215 }
5216 if (needs_null_check) {
5217 MaybeRecordImplicitNullCheck(instruction);
5218 }
5219 __ MaybeUnpoisonHeapReference(ref_reg);
5220 return;
5221 }
5222
Alexey Frunze15958152017-02-09 19:08:30 -08005223 // /* HeapReference<Object> */ ref = *(obj + offset)
5224 Location no_index = Location::NoLocation();
5225 ScaleFactor no_scale_factor = TIMES_1;
5226 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5227 ref,
5228 obj,
5229 offset,
5230 no_index,
5231 no_scale_factor,
5232 temp,
5233 needs_null_check);
5234}
5235
5236void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5237 Location ref,
5238 GpuRegister obj,
5239 uint32_t data_offset,
5240 Location index,
5241 Location temp,
5242 bool needs_null_check) {
5243 DCHECK(kEmitCompilerReadBarrier);
5244 DCHECK(kUseBakerReadBarrier);
5245
5246 static_assert(
5247 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5248 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005249 ScaleFactor scale_factor = TIMES_4;
5250
5251 if (kBakerReadBarrierThunksEnableForArrays) {
5252 // Note that we do not actually check the value of `GetIsGcMarking()`
5253 // to decide whether to mark the loaded reference or not. Instead, we
5254 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5255 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5256 // vice versa.
5257 //
5258 // We use thunks for the slow path. That thunk checks the reference
5259 // and jumps to the entrypoint if needed. If the holder is not gray,
5260 // it issues a load-load memory barrier and returns to the original
5261 // reference load.
5262 //
5263 // temp = Thread::Current()->pReadBarrierMarkReg00
5264 // // AKA &art_quick_read_barrier_mark_introspection.
5265 // if (temp != nullptr) {
5266 // temp = &field_array_thunk<holder_reg>
5267 // temp()
5268 // }
5269 // not_gray_return_address:
5270 // // The element address is pre-calculated in the TMP register before the
5271 // // thunk invocation and the thunk benefits from it.
5272 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5273 // gray_return_address:
5274
5275 DCHECK(temp.IsInvalid());
5276 DCHECK(index.IsValid());
5277 const int32_t entry_point_offset =
5278 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5279 // We will not do the explicit null check in the thunk as some form of a null check
5280 // must've been done earlier.
5281 DCHECK(!needs_null_check);
5282 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5283 // Loading the entrypoint does not require a load acquire since it is only changed when
5284 // threads are suspended or running a checkpoint.
5285 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005286 Mips64Label skip_call;
5287 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005288 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5289 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5290 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5291 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005292 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005293 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5294 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5295 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5296 __ MaybeUnpoisonHeapReference(ref_reg);
5297 return;
5298 }
5299
Alexey Frunze15958152017-02-09 19:08:30 -08005300 // /* HeapReference<Object> */ ref =
5301 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005302 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5303 ref,
5304 obj,
5305 data_offset,
5306 index,
5307 scale_factor,
5308 temp,
5309 needs_null_check);
5310}
5311
5312void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5313 Location ref,
5314 GpuRegister obj,
5315 uint32_t offset,
5316 Location index,
5317 ScaleFactor scale_factor,
5318 Location temp,
5319 bool needs_null_check,
5320 bool always_update_field) {
5321 DCHECK(kEmitCompilerReadBarrier);
5322 DCHECK(kUseBakerReadBarrier);
5323
5324 // In slow path based read barriers, the read barrier call is
5325 // inserted after the original load. However, in fast path based
5326 // Baker's read barriers, we need to perform the load of
5327 // mirror::Object::monitor_ *before* the original reference load.
5328 // This load-load ordering is required by the read barrier.
5329 // The fast path/slow path (for Baker's algorithm) should look like:
5330 //
5331 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5332 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5333 // HeapReference<Object> ref = *src; // Original reference load.
5334 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5335 // if (is_gray) {
5336 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5337 // }
5338 //
5339 // Note: the original implementation in ReadBarrier::Barrier is
5340 // slightly more complex as it performs additional checks that we do
5341 // not do here for performance reasons.
5342
5343 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5344 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5345 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5346
5347 // /* int32_t */ monitor = obj->monitor_
5348 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5349 if (needs_null_check) {
5350 MaybeRecordImplicitNullCheck(instruction);
5351 }
5352 // /* LockWord */ lock_word = LockWord(monitor)
5353 static_assert(sizeof(LockWord) == sizeof(int32_t),
5354 "art::LockWord and int32_t have different sizes.");
5355
5356 __ Sync(0); // Barrier to prevent load-load reordering.
5357
5358 // The actual reference load.
5359 if (index.IsValid()) {
5360 // Load types involving an "index": ArrayGet,
5361 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5362 // intrinsics.
5363 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5364 if (index.IsConstant()) {
5365 size_t computed_offset =
5366 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5367 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5368 } else {
5369 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005370 if (scale_factor == TIMES_1) {
5371 __ Daddu(TMP, index_reg, obj);
5372 } else {
5373 __ Dlsa(TMP, index_reg, obj, scale_factor);
5374 }
Alexey Frunze15958152017-02-09 19:08:30 -08005375 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5376 }
5377 } else {
5378 // /* HeapReference<Object> */ ref = *(obj + offset)
5379 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5380 }
5381
5382 // Object* ref = ref_addr->AsMirrorPtr()
5383 __ MaybeUnpoisonHeapReference(ref_reg);
5384
5385 // Slow path marking the object `ref` when it is gray.
5386 SlowPathCodeMIPS64* slow_path;
5387 if (always_update_field) {
5388 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5389 // of the form `obj + field_offset`, where `obj` is a register and
5390 // `field_offset` is a register. Thus `offset` and `scale_factor`
5391 // above are expected to be null in this code path.
5392 DCHECK_EQ(offset, 0u);
5393 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005394 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005395 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5396 ref,
5397 obj,
5398 /* field_offset */ index,
5399 temp_reg);
5400 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005401 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005402 }
5403 AddSlowPath(slow_path);
5404
5405 // if (rb_state == ReadBarrier::GrayState())
5406 // ref = ReadBarrier::Mark(ref);
5407 // Given the numeric representation, it's enough to check the low bit of the
5408 // rb_state. We do that by shifting the bit into the sign bit (31) and
5409 // performing a branch on less than zero.
5410 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5411 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5412 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5413 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5414 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5415 __ Bind(slow_path->GetExitLabel());
5416}
5417
5418void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5419 Location out,
5420 Location ref,
5421 Location obj,
5422 uint32_t offset,
5423 Location index) {
5424 DCHECK(kEmitCompilerReadBarrier);
5425
5426 // Insert a slow path based read barrier *after* the reference load.
5427 //
5428 // If heap poisoning is enabled, the unpoisoning of the loaded
5429 // reference will be carried out by the runtime within the slow
5430 // path.
5431 //
5432 // Note that `ref` currently does not get unpoisoned (when heap
5433 // poisoning is enabled), which is alright as the `ref` argument is
5434 // not used by the artReadBarrierSlow entry point.
5435 //
5436 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005437 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005438 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5439 AddSlowPath(slow_path);
5440
5441 __ Bc(slow_path->GetEntryLabel());
5442 __ Bind(slow_path->GetExitLabel());
5443}
5444
5445void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5446 Location out,
5447 Location ref,
5448 Location obj,
5449 uint32_t offset,
5450 Location index) {
5451 if (kEmitCompilerReadBarrier) {
5452 // Baker's read barriers shall be handled by the fast path
5453 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5454 DCHECK(!kUseBakerReadBarrier);
5455 // If heap poisoning is enabled, unpoisoning will be taken care of
5456 // by the runtime within the slow path.
5457 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5458 } else if (kPoisonHeapReferences) {
5459 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5460 }
5461}
5462
5463void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5464 Location out,
5465 Location root) {
5466 DCHECK(kEmitCompilerReadBarrier);
5467
5468 // Insert a slow path based read barrier *after* the GC root load.
5469 //
5470 // Note that GC roots are not affected by heap poisoning, so we do
5471 // not need to do anything special for this here.
5472 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005473 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005474 AddSlowPath(slow_path);
5475
5476 __ Bc(slow_path->GetEntryLabel());
5477 __ Bind(slow_path->GetExitLabel());
5478}
5479
Alexey Frunze4dda3372015-06-01 18:31:49 -07005480void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005481 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5482 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005483 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005484 switch (type_check_kind) {
5485 case TypeCheckKind::kExactCheck:
5486 case TypeCheckKind::kAbstractClassCheck:
5487 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005488 case TypeCheckKind::kArrayObjectCheck: {
5489 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
5490 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
5491 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005492 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005493 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005494 case TypeCheckKind::kArrayCheck:
5495 case TypeCheckKind::kUnresolvedCheck:
5496 case TypeCheckKind::kInterfaceCheck:
5497 call_kind = LocationSummary::kCallOnSlowPath;
5498 break;
5499 }
5500
Vladimir Markoca6fff82017-10-03 14:49:14 +01005501 LocationSummary* locations =
5502 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005503 if (baker_read_barrier_slow_path) {
5504 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5505 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005506 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005507 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005508 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005509 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005510 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005511 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005512}
5513
5514void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005515 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005516 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005517 Location obj_loc = locations->InAt(0);
5518 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005519 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005520 Location out_loc = locations->Out();
5521 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5522 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5523 DCHECK_LE(num_temps, 1u);
5524 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005525 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5526 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5527 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5528 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005529 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005530 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005531
5532 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005533 // Avoid this check if we know `obj` is not null.
5534 if (instruction->MustDoNullCheck()) {
5535 __ Move(out, ZERO);
5536 __ Beqzc(obj, &done);
5537 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005538
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005539 switch (type_check_kind) {
5540 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005541 ReadBarrierOption read_barrier_option =
5542 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005543 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005544 GenerateReferenceLoadTwoRegisters(instruction,
5545 out_loc,
5546 obj_loc,
5547 class_offset,
5548 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005549 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005550 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005551 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005552 __ Sltiu(out, out, 1);
5553 break;
5554 }
5555
5556 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005557 ReadBarrierOption read_barrier_option =
5558 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005559 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005560 GenerateReferenceLoadTwoRegisters(instruction,
5561 out_loc,
5562 obj_loc,
5563 class_offset,
5564 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005565 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005566 // If the class is abstract, we eagerly fetch the super class of the
5567 // object to avoid doing a comparison we know will fail.
5568 Mips64Label loop;
5569 __ Bind(&loop);
5570 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005571 GenerateReferenceLoadOneRegister(instruction,
5572 out_loc,
5573 super_offset,
5574 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005575 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005576 // If `out` is null, we use it for the result, and jump to `done`.
5577 __ Beqzc(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005578 __ Bnec(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005579 __ LoadConst32(out, 1);
5580 break;
5581 }
5582
5583 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005584 ReadBarrierOption read_barrier_option =
5585 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005586 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005587 GenerateReferenceLoadTwoRegisters(instruction,
5588 out_loc,
5589 obj_loc,
5590 class_offset,
5591 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005592 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005593 // Walk over the class hierarchy to find a match.
5594 Mips64Label loop, success;
5595 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005596 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005597 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005598 GenerateReferenceLoadOneRegister(instruction,
5599 out_loc,
5600 super_offset,
5601 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005602 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005603 __ Bnezc(out, &loop);
5604 // If `out` is null, we use it for the result, and jump to `done`.
5605 __ Bc(&done);
5606 __ Bind(&success);
5607 __ LoadConst32(out, 1);
5608 break;
5609 }
5610
5611 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005612 ReadBarrierOption read_barrier_option =
5613 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005614 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005615 GenerateReferenceLoadTwoRegisters(instruction,
5616 out_loc,
5617 obj_loc,
5618 class_offset,
5619 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005620 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005621 // Do an exact check.
5622 Mips64Label success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005623 __ Beqc(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005624 // Otherwise, we need to check that the object's class is a non-primitive array.
5625 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005626 GenerateReferenceLoadOneRegister(instruction,
5627 out_loc,
5628 component_offset,
5629 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08005630 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005631 // If `out` is null, we use it for the result, and jump to `done`.
5632 __ Beqzc(out, &done);
5633 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5634 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5635 __ Sltiu(out, out, 1);
5636 __ Bc(&done);
5637 __ Bind(&success);
5638 __ LoadConst32(out, 1);
5639 break;
5640 }
5641
5642 case TypeCheckKind::kArrayCheck: {
5643 // No read barrier since the slow path will retry upon failure.
5644 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005645 GenerateReferenceLoadTwoRegisters(instruction,
5646 out_loc,
5647 obj_loc,
5648 class_offset,
5649 maybe_temp_loc,
5650 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005651 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005652 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5653 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005654 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00005655 __ Bnec(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005656 __ LoadConst32(out, 1);
5657 break;
5658 }
5659
5660 case TypeCheckKind::kUnresolvedCheck:
5661 case TypeCheckKind::kInterfaceCheck: {
5662 // Note that we indeed only call on slow path, but we always go
5663 // into the slow path for the unresolved and interface check
5664 // cases.
5665 //
5666 // We cannot directly call the InstanceofNonTrivial runtime
5667 // entry point without resorting to a type checking slow path
5668 // here (i.e. by calling InvokeRuntime directly), as it would
5669 // require to assign fixed registers for the inputs of this
5670 // HInstanceOf instruction (following the runtime calling
5671 // convention), which might be cluttered by the potential first
5672 // read barrier emission at the beginning of this method.
5673 //
5674 // TODO: Introduce a new runtime entry point taking the object
5675 // to test (instead of its class) as argument, and let it deal
5676 // with the read barrier issues. This will let us refactor this
5677 // case of the `switch` code as it was previously (with a direct
5678 // call to the runtime not using a type checking slow path).
5679 // This should also be beneficial for the other cases above.
5680 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005681 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5682 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005683 codegen_->AddSlowPath(slow_path);
5684 __ Bc(slow_path->GetEntryLabel());
5685 break;
5686 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005687 }
5688
5689 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005690
5691 if (slow_path != nullptr) {
5692 __ Bind(slow_path->GetExitLabel());
5693 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005694}
5695
5696void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005697 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005698 locations->SetOut(Location::ConstantLocation(constant));
5699}
5700
5701void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5702 // Will be generated at use site.
5703}
5704
5705void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005706 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005707 locations->SetOut(Location::ConstantLocation(constant));
5708}
5709
5710void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5711 // Will be generated at use site.
5712}
5713
Calin Juravle175dc732015-08-25 15:42:32 +01005714void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5715 // The trampoline uses the same calling convention as dex calling conventions,
5716 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5717 // the method_idx.
5718 HandleInvoke(invoke);
5719}
5720
5721void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5722 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5723}
5724
Alexey Frunze4dda3372015-06-01 18:31:49 -07005725void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5726 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5727 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5728}
5729
5730void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5731 HandleInvoke(invoke);
5732 // The register T0 is required to be used for the hidden argument in
5733 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5734 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5735}
5736
5737void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5738 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5739 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005740 Location receiver = invoke->GetLocations()->InAt(0);
5741 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005742 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005743
5744 // Set the hidden argument.
5745 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5746 invoke->GetDexMethodIndex());
5747
5748 // temp = object->GetClass();
5749 if (receiver.IsStackSlot()) {
5750 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5751 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5752 } else {
5753 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5754 }
5755 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005756 // Instead of simply (possibly) unpoisoning `temp` here, we should
5757 // emit a read barrier for the previous class reference load.
5758 // However this is not required in practice, as this is an
5759 // intermediate/temporary reference and because the current
5760 // concurrent copying collector keeps the from-space memory
5761 // intact/accessible until the end of the marking phase (the
5762 // concurrent copying collector may not in the future).
5763 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005764 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5765 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5766 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005767 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005768 // temp = temp->GetImtEntryAt(method_offset);
5769 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5770 // T9 = temp->GetEntryPoint();
5771 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5772 // T9();
5773 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005774 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005775 DCHECK(!codegen_->IsLeafMethod());
5776 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5777}
5778
5779void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005780 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5781 if (intrinsic.TryDispatch(invoke)) {
5782 return;
5783 }
5784
Alexey Frunze4dda3372015-06-01 18:31:49 -07005785 HandleInvoke(invoke);
5786}
5787
5788void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005789 // Explicit clinit checks triggered by static invokes must have been pruned by
5790 // art::PrepareForRegisterAllocation.
5791 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005792
Chris Larsen3039e382015-08-26 07:54:08 -07005793 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5794 if (intrinsic.TryDispatch(invoke)) {
5795 return;
5796 }
5797
Alexey Frunze4dda3372015-06-01 18:31:49 -07005798 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005799}
5800
Orion Hodsonac141392017-01-13 11:53:47 +00005801void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5802 HandleInvoke(invoke);
5803}
5804
5805void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5806 codegen_->GenerateInvokePolymorphicCall(invoke);
5807}
5808
Chris Larsen3039e382015-08-26 07:54:08 -07005809static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005810 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005811 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5812 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005813 return true;
5814 }
5815 return false;
5816}
5817
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005819 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005820 bool fallback_load = false;
5821 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005822 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005823 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005824 case HLoadString::LoadKind::kBssEntry:
5825 DCHECK(!Runtime::Current()->UseJitCompilation());
5826 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005827 case HLoadString::LoadKind::kJitTableAddress:
5828 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005829 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005830 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005831 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005832 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005833 }
5834 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005835 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005836 }
5837 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005838}
5839
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005840HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5841 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005842 bool fallback_load = false;
5843 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005844 case HLoadClass::LoadKind::kInvalid:
5845 LOG(FATAL) << "UNREACHABLE";
5846 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005847 case HLoadClass::LoadKind::kReferrersClass:
5848 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005849 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005850 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005851 case HLoadClass::LoadKind::kBssEntry:
5852 DCHECK(!Runtime::Current()->UseJitCompilation());
5853 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005854 case HLoadClass::LoadKind::kJitTableAddress:
5855 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005856 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005857 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005858 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005859 break;
5860 }
5861 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005862 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005863 }
5864 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005865}
5866
Vladimir Markodc151b22015-10-15 18:02:30 +01005867HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5868 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005869 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005870 // On MIPS64 we support all dispatch types.
5871 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005872}
5873
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005874void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5875 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005876 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005877 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005878 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5879 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5880
Alexey Frunze19f6c692016-11-30 19:19:55 -08005881 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005882 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005883 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005884 uint32_t offset =
5885 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005886 __ LoadFromOffset(kLoadDoubleword,
5887 temp.AsRegister<GpuRegister>(),
5888 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005889 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005890 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005891 }
Vladimir Marko58155012015-08-19 12:49:41 +00005892 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005893 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005894 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005895 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5896 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005897 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005898 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005899 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5900 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5901 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005902 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5903 break;
5904 }
Vladimir Marko58155012015-08-19 12:49:41 +00005905 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005906 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5907 kLoadDoubleword,
5908 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005909 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005910 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005911 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005912 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005913 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5914 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5915 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005916 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5917 break;
5918 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005919 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5920 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5921 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005922 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005923 }
5924
Alexey Frunze19f6c692016-11-30 19:19:55 -08005925 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005926 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005927 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005928 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005929 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5930 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5931 __ LoadFromOffset(kLoadDoubleword,
5932 T9,
5933 callee_method.AsRegister<GpuRegister>(),
5934 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005935 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005936 // T9()
5937 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005938 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005939 break;
5940 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005941 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5942
Alexey Frunze4dda3372015-06-01 18:31:49 -07005943 DCHECK(!IsLeafMethod());
5944}
5945
5946void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005947 // Explicit clinit checks triggered by static invokes must have been pruned by
5948 // art::PrepareForRegisterAllocation.
5949 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005950
5951 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5952 return;
5953 }
5954
5955 LocationSummary* locations = invoke->GetLocations();
5956 codegen_->GenerateStaticOrDirectCall(invoke,
5957 locations->HasTemps()
5958 ? locations->GetTemp(0)
5959 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005960}
5961
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005962void CodeGeneratorMIPS64::GenerateVirtualCall(
5963 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005964 // Use the calling convention instead of the location of the receiver, as
5965 // intrinsics may have put the receiver in a different register. In the intrinsics
5966 // slow path, the arguments have been moved to the right place, so here we are
5967 // guaranteed that the receiver is the first register of the calling convention.
5968 InvokeDexCallingConvention calling_convention;
5969 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5970
Alexey Frunze53afca12015-11-05 16:34:23 -08005971 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005972 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5973 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5974 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005975 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005976
5977 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005978 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005979 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005980 // Instead of simply (possibly) unpoisoning `temp` here, we should
5981 // emit a read barrier for the previous class reference load.
5982 // However this is not required in practice, as this is an
5983 // intermediate/temporary reference and because the current
5984 // concurrent copying collector keeps the from-space memory
5985 // intact/accessible until the end of the marking phase (the
5986 // concurrent copying collector may not in the future).
5987 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005988 // temp = temp->GetMethodAt(method_offset);
5989 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5990 // T9 = temp->GetEntryPoint();
5991 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5992 // T9();
5993 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005994 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005995 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005996}
5997
5998void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5999 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6000 return;
6001 }
6002
6003 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006004 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006005}
6006
6007void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006008 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006009 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006010 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006011 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6012 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006013 return;
6014 }
Vladimir Marko41559982017-01-06 14:04:23 +00006015 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006016
Alexey Frunze15958152017-02-09 19:08:30 -08006017 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6018 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006019 ? LocationSummary::kCallOnSlowPath
6020 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006021 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006022 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6023 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6024 }
Vladimir Marko41559982017-01-06 14:04:23 +00006025 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006026 locations->SetInAt(0, Location::RequiresRegister());
6027 }
6028 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006029 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6030 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6031 // Rely on the type resolution or initialization and marking to save everything we need.
6032 RegisterSet caller_saves = RegisterSet::Empty();
6033 InvokeRuntimeCallingConvention calling_convention;
6034 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6035 locations->SetCustomSlowPathCallerSaves(caller_saves);
6036 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006037 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006038 }
6039 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006040}
6041
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006042// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6043// move.
6044void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006045 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006046 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006047 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006048 return;
6049 }
Vladimir Marko41559982017-01-06 14:04:23 +00006050 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006051
Vladimir Marko41559982017-01-06 14:04:23 +00006052 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006053 Location out_loc = locations->Out();
6054 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6055 GpuRegister current_method_reg = ZERO;
6056 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006057 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006058 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6059 }
6060
Alexey Frunze15958152017-02-09 19:08:30 -08006061 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6062 ? kWithoutReadBarrier
6063 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006064 bool generate_null_check = false;
6065 switch (load_kind) {
6066 case HLoadClass::LoadKind::kReferrersClass:
6067 DCHECK(!cls->CanCallRuntime());
6068 DCHECK(!cls->MustGenerateClinitCheck());
6069 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6070 GenerateGcRootFieldLoad(cls,
6071 out_loc,
6072 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006073 ArtMethod::DeclaringClassOffset().Int32Value(),
6074 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006075 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006076 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006077 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006078 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006079 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006080 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006081 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6082 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6083 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006084 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6085 break;
6086 }
6087 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006088 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006089 uint32_t address = dchecked_integral_cast<uint32_t>(
6090 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6091 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006092 __ LoadLiteral(out,
6093 kLoadUnsignedWord,
6094 codegen_->DeduplicateBootImageAddressLiteral(address));
6095 break;
6096 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006097 case HLoadClass::LoadKind::kBootImageClassTable: {
6098 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6099 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6100 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6101 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6102 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6103 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6104 __ Lwu(out, AT, /* placeholder */ 0x5678);
6105 // Extract the reference from the slot data, i.e. clear the hash bits.
6106 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6107 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6108 if (masked_hash != 0) {
6109 __ Daddiu(out, out, -masked_hash);
6110 }
6111 break;
6112 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006113 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006114 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6115 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006116 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6117 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006118 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006119 GenerateGcRootFieldLoad(cls,
6120 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006121 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006122 /* placeholder */ 0x5678,
6123 read_barrier_option,
6124 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006125 generate_null_check = true;
6126 break;
6127 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006128 case HLoadClass::LoadKind::kJitTableAddress:
6129 __ LoadLiteral(out,
6130 kLoadUnsignedWord,
6131 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6132 cls->GetTypeIndex(),
6133 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006134 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006135 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006136 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006137 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006138 LOG(FATAL) << "UNREACHABLE";
6139 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006140 }
6141
6142 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6143 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006144 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006145 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006146 codegen_->AddSlowPath(slow_path);
6147 if (generate_null_check) {
6148 __ Beqzc(out, slow_path->GetEntryLabel());
6149 }
6150 if (cls->MustGenerateClinitCheck()) {
6151 GenerateClassInitializationCheck(slow_path, out);
6152 } else {
6153 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006154 }
6155 }
6156}
6157
David Brazdilcb1c0552015-08-04 16:22:25 +01006158static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006159 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006160}
6161
Alexey Frunze4dda3372015-06-01 18:31:49 -07006162void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6163 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006164 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006165 locations->SetOut(Location::RequiresRegister());
6166}
6167
6168void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6169 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006170 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6171}
6172
6173void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006174 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006175}
6176
6177void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6178 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006179}
6180
Alexey Frunze4dda3372015-06-01 18:31:49 -07006181void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006182 HLoadString::LoadKind load_kind = load->GetLoadKind();
6183 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006184 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006185 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006186 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006187 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006188 } else {
6189 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006190 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6191 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6192 // Rely on the pResolveString and marking to save everything we need.
6193 RegisterSet caller_saves = RegisterSet::Empty();
6194 InvokeRuntimeCallingConvention calling_convention;
6195 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6196 locations->SetCustomSlowPathCallerSaves(caller_saves);
6197 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006198 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006199 }
6200 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006201 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006202}
6203
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006204// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6205// move.
6206void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006207 HLoadString::LoadKind load_kind = load->GetLoadKind();
6208 LocationSummary* locations = load->GetLocations();
6209 Location out_loc = locations->Out();
6210 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6211
6212 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006213 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6214 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006215 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006216 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006217 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6218 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6219 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006220 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006221 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006222 }
6223 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006224 uint32_t address = dchecked_integral_cast<uint32_t>(
6225 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6226 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006227 __ LoadLiteral(out,
6228 kLoadUnsignedWord,
6229 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006230 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006231 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006232 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006233 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006234 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006235 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006236 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6237 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006238 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6239 __ Lwu(out, AT, /* placeholder */ 0x5678);
6240 return;
6241 }
6242 case HLoadString::LoadKind::kBssEntry: {
6243 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6244 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6245 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6246 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6247 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006248 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006249 GenerateGcRootFieldLoad(load,
6250 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006251 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006252 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006253 kCompilerReadBarrierOption,
6254 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006255 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006256 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006257 codegen_->AddSlowPath(slow_path);
6258 __ Beqzc(out, slow_path->GetEntryLabel());
6259 __ Bind(slow_path->GetExitLabel());
6260 return;
6261 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006262 case HLoadString::LoadKind::kJitTableAddress:
6263 __ LoadLiteral(out,
6264 kLoadUnsignedWord,
6265 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6266 load->GetStringIndex(),
6267 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006268 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006269 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006270 default:
6271 break;
6272 }
6273
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006274 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006275 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006276 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006277 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006278 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6279 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6280 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006281}
6282
Alexey Frunze4dda3372015-06-01 18:31:49 -07006283void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006284 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006285 locations->SetOut(Location::ConstantLocation(constant));
6286}
6287
6288void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6289 // Will be generated at use site.
6290}
6291
6292void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006293 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6294 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006295 InvokeRuntimeCallingConvention calling_convention;
6296 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6297}
6298
6299void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006300 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006301 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006302 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006303 if (instruction->IsEnter()) {
6304 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6305 } else {
6306 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6307 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006308}
6309
6310void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6311 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006312 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006313 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 case DataType::Type::kInt32:
6315 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006316 locations->SetInAt(0, Location::RequiresRegister());
6317 locations->SetInAt(1, Location::RequiresRegister());
6318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6319 break;
6320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 case DataType::Type::kFloat32:
6322 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006323 locations->SetInAt(0, Location::RequiresFpuRegister());
6324 locations->SetInAt(1, Location::RequiresFpuRegister());
6325 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6326 break;
6327
6328 default:
6329 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6330 }
6331}
6332
6333void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006334 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006335 LocationSummary* locations = instruction->GetLocations();
6336
6337 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006338 case DataType::Type::kInt32:
6339 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006340 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6341 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6342 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006343 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006344 __ MulR6(dst, lhs, rhs);
6345 else
6346 __ Dmul(dst, lhs, rhs);
6347 break;
6348 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006349 case DataType::Type::kFloat32:
6350 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006351 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6352 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6353 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006354 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006355 __ MulS(dst, lhs, rhs);
6356 else
6357 __ MulD(dst, lhs, rhs);
6358 break;
6359 }
6360 default:
6361 LOG(FATAL) << "Unexpected mul type " << type;
6362 }
6363}
6364
6365void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6366 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006367 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006368 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006369 case DataType::Type::kInt32:
6370 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006371 locations->SetInAt(0, Location::RequiresRegister());
6372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6373 break;
6374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 case DataType::Type::kFloat32:
6376 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006377 locations->SetInAt(0, Location::RequiresFpuRegister());
6378 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6379 break;
6380
6381 default:
6382 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6383 }
6384}
6385
6386void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006387 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006388 LocationSummary* locations = instruction->GetLocations();
6389
6390 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kInt32:
6392 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006393 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6394 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006395 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006396 __ Subu(dst, ZERO, src);
6397 else
6398 __ Dsubu(dst, ZERO, src);
6399 break;
6400 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006401 case DataType::Type::kFloat32:
6402 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006403 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6404 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006405 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006406 __ NegS(dst, src);
6407 else
6408 __ NegD(dst, src);
6409 break;
6410 }
6411 default:
6412 LOG(FATAL) << "Unexpected neg type " << type;
6413 }
6414}
6415
6416void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006417 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6418 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006419 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006420 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006421 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6422 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006423}
6424
6425void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006426 // Note: if heap poisoning is enabled, the entry point takes care
6427 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006428 QuickEntrypointEnum entrypoint =
6429 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6430 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006431 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006432 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006433}
6434
6435void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006436 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6437 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006438 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006439 if (instruction->IsStringAlloc()) {
6440 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6441 } else {
6442 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006443 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006444 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006445}
6446
6447void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006448 // Note: if heap poisoning is enabled, the entry point takes care
6449 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006450 if (instruction->IsStringAlloc()) {
6451 // String is allocated through StringFactory. Call NewEmptyString entry point.
6452 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006453 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006454 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006455 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6456 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6457 __ Jalr(T9);
6458 __ Nop();
6459 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6460 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006461 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006462 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006463 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006464}
6465
6466void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006467 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006468 locations->SetInAt(0, Location::RequiresRegister());
6469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6470}
6471
6472void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006473 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006474 LocationSummary* locations = instruction->GetLocations();
6475
6476 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006477 case DataType::Type::kInt32:
6478 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006479 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6480 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6481 __ Nor(dst, src, ZERO);
6482 break;
6483 }
6484
6485 default:
6486 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6487 }
6488}
6489
6490void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006491 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006492 locations->SetInAt(0, Location::RequiresRegister());
6493 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6494}
6495
6496void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6497 LocationSummary* locations = instruction->GetLocations();
6498 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6499 locations->InAt(0).AsRegister<GpuRegister>(),
6500 1);
6501}
6502
6503void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006504 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6505 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006506}
6507
Calin Juravle2ae48182016-03-16 14:05:09 +00006508void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6509 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006510 return;
6511 }
6512 Location obj = instruction->GetLocations()->InAt(0);
6513
6514 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006515 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006516}
6517
Calin Juravle2ae48182016-03-16 14:05:09 +00006518void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006519 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006520 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006521 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006522
6523 Location obj = instruction->GetLocations()->InAt(0);
6524
6525 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6526}
6527
6528void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006529 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006530}
6531
6532void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6533 HandleBinaryOp(instruction);
6534}
6535
6536void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6537 HandleBinaryOp(instruction);
6538}
6539
6540void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6541 LOG(FATAL) << "Unreachable";
6542}
6543
6544void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006545 if (instruction->GetNext()->IsSuspendCheck() &&
6546 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6547 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6548 // The back edge will generate the suspend check.
6549 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6550 }
6551
Alexey Frunze4dda3372015-06-01 18:31:49 -07006552 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6553}
6554
6555void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006556 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006557 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6558 if (location.IsStackSlot()) {
6559 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6560 } else if (location.IsDoubleStackSlot()) {
6561 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6562 }
6563 locations->SetOut(location);
6564}
6565
6566void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6567 ATTRIBUTE_UNUSED) {
6568 // Nothing to do, the parameter is already at its location.
6569}
6570
6571void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6572 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006573 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006574 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6575}
6576
6577void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6578 ATTRIBUTE_UNUSED) {
6579 // Nothing to do, the method is already at its location.
6580}
6581
6582void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006583 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006584 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006585 locations->SetInAt(i, Location::Any());
6586 }
6587 locations->SetOut(Location::Any());
6588}
6589
6590void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6591 LOG(FATAL) << "Unreachable";
6592}
6593
6594void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006595 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006596 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006597 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6598 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006599 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006600
6601 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006602 case DataType::Type::kInt32:
6603 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006604 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006605 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006606 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6607 break;
6608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006609 case DataType::Type::kFloat32:
6610 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006611 InvokeRuntimeCallingConvention calling_convention;
6612 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6613 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6614 locations->SetOut(calling_convention.GetReturnLocation(type));
6615 break;
6616 }
6617
6618 default:
6619 LOG(FATAL) << "Unexpected rem type " << type;
6620 }
6621}
6622
6623void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006624 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006625
6626 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006627 case DataType::Type::kInt32:
6628 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006629 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006630 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006632 case DataType::Type::kFloat32:
6633 case DataType::Type::kFloat64: {
6634 QuickEntrypointEnum entrypoint =
6635 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006636 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006637 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006638 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6639 } else {
6640 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6641 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006642 break;
6643 }
6644 default:
6645 LOG(FATAL) << "Unexpected rem type " << type;
6646 }
6647}
6648
Igor Murashkind01745e2017-04-05 16:40:31 -07006649void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6650 constructor_fence->SetLocations(nullptr);
6651}
6652
6653void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6654 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6655 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6656}
6657
Alexey Frunze4dda3372015-06-01 18:31:49 -07006658void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6659 memory_barrier->SetLocations(nullptr);
6660}
6661
6662void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6663 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6664}
6665
6666void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006667 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006668 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006669 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6670}
6671
6672void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6673 codegen_->GenerateFrameExit();
6674}
6675
6676void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6677 ret->SetLocations(nullptr);
6678}
6679
6680void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6681 codegen_->GenerateFrameExit();
6682}
6683
Alexey Frunze92d90602015-12-18 18:16:36 -08006684void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6685 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006686}
6687
Alexey Frunze92d90602015-12-18 18:16:36 -08006688void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6689 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006690}
6691
Alexey Frunze4dda3372015-06-01 18:31:49 -07006692void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6693 HandleShift(shl);
6694}
6695
6696void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6697 HandleShift(shl);
6698}
6699
6700void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6701 HandleShift(shr);
6702}
6703
6704void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6705 HandleShift(shr);
6706}
6707
Alexey Frunze4dda3372015-06-01 18:31:49 -07006708void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6709 HandleBinaryOp(instruction);
6710}
6711
6712void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6713 HandleBinaryOp(instruction);
6714}
6715
6716void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6717 HandleFieldGet(instruction, instruction->GetFieldInfo());
6718}
6719
6720void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6721 HandleFieldGet(instruction, instruction->GetFieldInfo());
6722}
6723
6724void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6725 HandleFieldSet(instruction, instruction->GetFieldInfo());
6726}
6727
6728void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006729 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006730}
6731
Calin Juravlee460d1d2015-09-29 04:52:17 +01006732void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6733 HUnresolvedInstanceFieldGet* instruction) {
6734 FieldAccessCallingConventionMIPS64 calling_convention;
6735 codegen_->CreateUnresolvedFieldLocationSummary(
6736 instruction, instruction->GetFieldType(), calling_convention);
6737}
6738
6739void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6740 HUnresolvedInstanceFieldGet* instruction) {
6741 FieldAccessCallingConventionMIPS64 calling_convention;
6742 codegen_->GenerateUnresolvedFieldAccess(instruction,
6743 instruction->GetFieldType(),
6744 instruction->GetFieldIndex(),
6745 instruction->GetDexPc(),
6746 calling_convention);
6747}
6748
6749void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6750 HUnresolvedInstanceFieldSet* instruction) {
6751 FieldAccessCallingConventionMIPS64 calling_convention;
6752 codegen_->CreateUnresolvedFieldLocationSummary(
6753 instruction, instruction->GetFieldType(), calling_convention);
6754}
6755
6756void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6757 HUnresolvedInstanceFieldSet* instruction) {
6758 FieldAccessCallingConventionMIPS64 calling_convention;
6759 codegen_->GenerateUnresolvedFieldAccess(instruction,
6760 instruction->GetFieldType(),
6761 instruction->GetFieldIndex(),
6762 instruction->GetDexPc(),
6763 calling_convention);
6764}
6765
6766void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6767 HUnresolvedStaticFieldGet* instruction) {
6768 FieldAccessCallingConventionMIPS64 calling_convention;
6769 codegen_->CreateUnresolvedFieldLocationSummary(
6770 instruction, instruction->GetFieldType(), calling_convention);
6771}
6772
6773void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6774 HUnresolvedStaticFieldGet* instruction) {
6775 FieldAccessCallingConventionMIPS64 calling_convention;
6776 codegen_->GenerateUnresolvedFieldAccess(instruction,
6777 instruction->GetFieldType(),
6778 instruction->GetFieldIndex(),
6779 instruction->GetDexPc(),
6780 calling_convention);
6781}
6782
6783void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6784 HUnresolvedStaticFieldSet* instruction) {
6785 FieldAccessCallingConventionMIPS64 calling_convention;
6786 codegen_->CreateUnresolvedFieldLocationSummary(
6787 instruction, instruction->GetFieldType(), calling_convention);
6788}
6789
6790void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6791 HUnresolvedStaticFieldSet* instruction) {
6792 FieldAccessCallingConventionMIPS64 calling_convention;
6793 codegen_->GenerateUnresolvedFieldAccess(instruction,
6794 instruction->GetFieldType(),
6795 instruction->GetFieldIndex(),
6796 instruction->GetDexPc(),
6797 calling_convention);
6798}
6799
Alexey Frunze4dda3372015-06-01 18:31:49 -07006800void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006801 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6802 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006803 // In suspend check slow path, usually there are no caller-save registers at all.
6804 // If SIMD instructions are present, however, we force spilling all live SIMD
6805 // registers in full width (since the runtime only saves/restores lower part).
6806 locations->SetCustomSlowPathCallerSaves(
6807 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006808}
6809
6810void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6811 HBasicBlock* block = instruction->GetBlock();
6812 if (block->GetLoopInformation() != nullptr) {
6813 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6814 // The back edge will generate the suspend check.
6815 return;
6816 }
6817 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6818 // The goto will generate the suspend check.
6819 return;
6820 }
6821 GenerateSuspendCheck(instruction, nullptr);
6822}
6823
Alexey Frunze4dda3372015-06-01 18:31:49 -07006824void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006825 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6826 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006827 InvokeRuntimeCallingConvention calling_convention;
6828 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6829}
6830
6831void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006832 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006833 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6834}
6835
6836void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006837 DataType::Type input_type = conversion->GetInputType();
6838 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006839 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6840 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006842 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6843 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006844 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6845 }
6846
Vladimir Markoca6fff82017-10-03 14:49:14 +01006847 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006848
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006849 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006850 locations->SetInAt(0, Location::RequiresFpuRegister());
6851 } else {
6852 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006853 }
6854
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006855 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006856 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006857 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006858 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006859 }
6860}
6861
6862void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6863 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006864 DataType::Type result_type = conversion->GetResultType();
6865 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006866
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006867 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6868 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006869
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006870 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006871 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6872 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6873
6874 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006875 case DataType::Type::kUint8:
6876 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006877 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006878 case DataType::Type::kInt8:
6879 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006880 // Type conversion from long to types narrower than int is a result of code
6881 // transformations. To avoid unpredictable results for SEB and SEH, we first
6882 // need to sign-extend the low 32-bit value into bits 32 through 63.
6883 __ Sll(dst, src, 0);
6884 __ Seb(dst, dst);
6885 } else {
6886 __ Seb(dst, src);
6887 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006888 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006889 case DataType::Type::kUint16:
6890 __ Andi(dst, src, 0xFFFF);
6891 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006892 case DataType::Type::kInt16:
6893 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006894 // Type conversion from long to types narrower than int is a result of code
6895 // transformations. To avoid unpredictable results for SEB and SEH, we first
6896 // need to sign-extend the low 32-bit value into bits 32 through 63.
6897 __ Sll(dst, src, 0);
6898 __ Seh(dst, dst);
6899 } else {
6900 __ Seh(dst, src);
6901 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006902 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006903 case DataType::Type::kInt32:
6904 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006905 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6906 // conversions, except when the input and output registers are the same and we are not
6907 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006908 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006909 __ Sll(dst, src, 0);
6910 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006911 break;
6912
6913 default:
6914 LOG(FATAL) << "Unexpected type conversion from " << input_type
6915 << " to " << result_type;
6916 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006917 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006918 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6919 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006920 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006921 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006922 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006923 __ Cvtsl(dst, FTMP);
6924 } else {
6925 __ Cvtdl(dst, FTMP);
6926 }
6927 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006928 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006929 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006930 __ Cvtsw(dst, FTMP);
6931 } else {
6932 __ Cvtdw(dst, FTMP);
6933 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006934 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006935 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6936 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006937 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6938 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006940 if (result_type == DataType::Type::kInt64) {
6941 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006942 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006943 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006944 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006945 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006946 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006947 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006948 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006949 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006950 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006951 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006952 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006953 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006954 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006955 } else if (DataType::IsFloatingPointType(result_type) &&
6956 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006957 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6958 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006959 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006960 __ Cvtsd(dst, src);
6961 } else {
6962 __ Cvtds(dst, src);
6963 }
6964 } else {
6965 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6966 << " to " << result_type;
6967 }
6968}
6969
6970void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6971 HandleShift(ushr);
6972}
6973
6974void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6975 HandleShift(ushr);
6976}
6977
6978void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6979 HandleBinaryOp(instruction);
6980}
6981
6982void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6983 HandleBinaryOp(instruction);
6984}
6985
6986void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6987 // Nothing to do, this should be removed during prepare for register allocator.
6988 LOG(FATAL) << "Unreachable";
6989}
6990
6991void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6992 // Nothing to do, this should be removed during prepare for register allocator.
6993 LOG(FATAL) << "Unreachable";
6994}
6995
6996void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006997 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006998}
6999
7000void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007001 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007002}
7003
7004void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007005 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007006}
7007
7008void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007009 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007010}
7011
7012void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007013 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007014}
7015
7016void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007017 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007018}
7019
7020void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007021 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007022}
7023
7024void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007025 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007026}
7027
7028void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007029 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007030}
7031
7032void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007033 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007034}
7035
7036void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007037 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007038}
7039
7040void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007041 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007042}
7043
Aart Bike9f37602015-10-09 11:15:55 -07007044void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007045 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007046}
7047
7048void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007049 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007050}
7051
7052void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007053 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007054}
7055
7056void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007057 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007058}
7059
7060void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007061 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007062}
7063
7064void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007065 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007066}
7067
7068void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007069 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007070}
7071
7072void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007073 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007074}
7075
Mark Mendellfe57faa2015-09-18 09:26:15 -04007076// Simple implementation of packed switch - generate cascaded compare/jumps.
7077void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7078 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007079 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007080 locations->SetInAt(0, Location::RequiresRegister());
7081}
7082
Alexey Frunze0960ac52016-12-20 17:24:59 -08007083void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7084 int32_t lower_bound,
7085 uint32_t num_entries,
7086 HBasicBlock* switch_block,
7087 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007088 // Create a set of compare/jumps.
7089 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007090 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007091 // Jump to default if index is negative
7092 // Note: We don't check the case that index is positive while value < lower_bound, because in
7093 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7094 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7095
Alexey Frunze0960ac52016-12-20 17:24:59 -08007096 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007097 // Jump to successors[0] if value == lower_bound.
7098 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7099 int32_t last_index = 0;
7100 for (; num_entries - last_index > 2; last_index += 2) {
7101 __ Addiu(temp_reg, temp_reg, -2);
7102 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7103 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7104 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7105 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7106 }
7107 if (num_entries - last_index == 2) {
7108 // The last missing case_value.
7109 __ Addiu(temp_reg, temp_reg, -1);
7110 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007111 }
7112
7113 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007114 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007115 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007116 }
7117}
7118
Alexey Frunze0960ac52016-12-20 17:24:59 -08007119void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7120 int32_t lower_bound,
7121 uint32_t num_entries,
7122 HBasicBlock* switch_block,
7123 HBasicBlock* default_block) {
7124 // Create a jump table.
7125 std::vector<Mips64Label*> labels(num_entries);
7126 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7127 for (uint32_t i = 0; i < num_entries; i++) {
7128 labels[i] = codegen_->GetLabelOf(successors[i]);
7129 }
7130 JumpTable* table = __ CreateJumpTable(std::move(labels));
7131
7132 // Is the value in range?
7133 __ Addiu32(TMP, value_reg, -lower_bound);
7134 __ LoadConst32(AT, num_entries);
7135 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7136
7137 // We are in the range of the table.
7138 // Load the target address from the jump table, indexing by the value.
7139 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007140 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007141 __ Lw(TMP, TMP, 0);
7142 // Compute the absolute target address by adding the table start address
7143 // (the table contains offsets to targets relative to its start).
7144 __ Daddu(TMP, TMP, AT);
7145 // And jump.
7146 __ Jr(TMP);
7147 __ Nop();
7148}
7149
7150void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7151 int32_t lower_bound = switch_instr->GetStartValue();
7152 uint32_t num_entries = switch_instr->GetNumEntries();
7153 LocationSummary* locations = switch_instr->GetLocations();
7154 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7155 HBasicBlock* switch_block = switch_instr->GetBlock();
7156 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7157
7158 if (num_entries > kPackedSwitchJumpTableThreshold) {
7159 GenTableBasedPackedSwitch(value_reg,
7160 lower_bound,
7161 num_entries,
7162 switch_block,
7163 default_block);
7164 } else {
7165 GenPackedSwitchWithCompares(value_reg,
7166 lower_bound,
7167 num_entries,
7168 switch_block,
7169 default_block);
7170 }
7171}
7172
Chris Larsenc9905a62017-03-13 17:06:18 -07007173void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7174 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007175 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007176 locations->SetInAt(0, Location::RequiresRegister());
7177 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007178}
7179
Chris Larsenc9905a62017-03-13 17:06:18 -07007180void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7181 LocationSummary* locations = instruction->GetLocations();
7182 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7183 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7184 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7185 __ LoadFromOffset(kLoadDoubleword,
7186 locations->Out().AsRegister<GpuRegister>(),
7187 locations->InAt(0).AsRegister<GpuRegister>(),
7188 method_offset);
7189 } else {
7190 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7191 instruction->GetIndex(), kMips64PointerSize));
7192 __ LoadFromOffset(kLoadDoubleword,
7193 locations->Out().AsRegister<GpuRegister>(),
7194 locations->InAt(0).AsRegister<GpuRegister>(),
7195 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7196 __ LoadFromOffset(kLoadDoubleword,
7197 locations->Out().AsRegister<GpuRegister>(),
7198 locations->Out().AsRegister<GpuRegister>(),
7199 method_offset);
7200 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007201}
7202
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007203void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7204 ATTRIBUTE_UNUSED) {
7205 LOG(FATAL) << "Unreachable";
7206}
7207
7208void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7209 ATTRIBUTE_UNUSED) {
7210 LOG(FATAL) << "Unreachable";
7211}
7212
Alexey Frunze4dda3372015-06-01 18:31:49 -07007213} // namespace mips64
7214} // namespace art