blob: c8891eddfcbf4855e536e037ce3b5c3f3eae7c54 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips64/asm_support_mips64.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070020#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070022#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080023#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070028#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070029#include "intrinsics_mips64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "mirror/array-inl.h"
32#include "mirror/class-inl.h"
33#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010034#include "stack_map_stream.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070035#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070037#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070038#include "utils/stack_checks.h"
39
40namespace art {
41namespace mips64 {
42
43static constexpr int kCurrentMethodStackOffset = 0;
44static constexpr GpuRegister kMethodRegisterArgument = A0;
45
Alexey Frunze4147fcc2017-06-17 19:57:27 -070046// Flags controlling the use of thunks for Baker read barriers.
47constexpr bool kBakerReadBarrierThunksEnableForFields = true;
48constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
49constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
50
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051Location Mips64ReturnLocation(DataType::Type return_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070052 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010054 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 case DataType::Type::kInt8:
56 case DataType::Type::kUint16:
57 case DataType::Type::kInt16:
58 case DataType::Type::kInt32:
59 case DataType::Type::kReference:
60 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070061 return Location::RegisterLocation(V0);
62
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kFloat32:
64 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070065 return Location::FpuRegisterLocation(F0);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070068 return Location();
69 }
70 UNREACHABLE();
71}
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070074 return Mips64ReturnLocation(type);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
78 return Location::RegisterLocation(kMethodRegisterArgument);
79}
80
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070082 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070084 LOG(FATAL) << "Unexpected parameter type " << type;
85 }
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070088 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
89 next_location = Location::FpuRegisterLocation(
90 calling_convention.GetFpuRegisterAt(float_index_++));
91 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070093 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
94 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
95 float_index_++;
96 } else {
97 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010098 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
99 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700100 }
101
102 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
Alexey Frunze4dda3372015-06-01 18:31:49 -0700105 return next_location;
106}
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700109 return Mips64ReturnLocation(type);
110}
111
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100112// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
113#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700114#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115
116class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
117 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700119
120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100121 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
123 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000124 if (instruction_->CanThrowIntoCatchBlock()) {
125 // Live registers will be restored in the catch block if caught.
126 SaveLiveRegisters(codegen, instruction_->GetLocations());
127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 // We're moving two locations to locations that could overlap, so we need a parallel
129 // move resolver.
130 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100137 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
138 ? kQuickThrowStringBounds
139 : kQuickThrowArrayBounds;
140 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100141 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
143 }
144
Alexandre Rames8158f282015-08-07 10:26:17 +0100145 bool IsFatal() const OVERRIDE { return true; }
146
Roland Levillain46648892015-06-19 16:07:18 +0100147 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
148
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
151};
152
153class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
154 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700155 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
156 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
160 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100161 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
163 }
164
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 bool IsFatal() const OVERRIDE { return true; }
166
Roland Levillain46648892015-06-19 16:07:18 +0100167 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
168
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700170 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
171};
172
173class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
174 public:
175 LoadClassSlowPathMIPS64(HLoadClass* cls,
176 HInstruction* at,
177 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000178 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700179 : SlowPathCodeMIPS64(at),
180 cls_(cls),
181 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000182 do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000187 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700188 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700189 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700190 InvokeRuntimeCallingConvention calling_convention;
191 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 __ Bind(GetEntryLabel());
193 SaveLiveRegisters(codegen, locations);
194
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000195 dex::TypeIndex type_index = cls_->GetTypeIndex();
196 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100197 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
198 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000199 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700200 if (do_clinit_) {
201 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
202 } else {
203 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
204 }
205
206 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700207 if (out.IsValid()) {
208 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100209 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700210 mips64_codegen->MoveLocation(out,
211 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
212 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700215
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700216 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 }
218
Roland Levillain46648892015-06-19 16:07:18 +0100219 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
220
Alexey Frunze4dda3372015-06-01 18:31:49 -0700221 private:
222 // The class this slow path will load.
223 HLoadClass* const cls_;
224
Alexey Frunze4dda3372015-06-01 18:31:49 -0700225 // The dex PC of `at_`.
226 const uint32_t dex_pc_;
227
228 // Whether to initialize the class.
229 const bool do_clinit_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
232};
233
234class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
235 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000236 explicit LoadStringSlowPathMIPS64(HLoadString* instruction)
237 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700240 DCHECK(instruction_->IsLoadString());
241 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700242 LocationSummary* locations = instruction_->GetLocations();
243 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000244 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700246 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000250 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100251 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700252 instruction_,
253 instruction_->GetDexPc(),
254 this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100257 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700258 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800262
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700263 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700264 }
265
Roland Levillain46648892015-06-19 16:07:18 +0100266 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
267
Alexey Frunze4dda3372015-06-01 18:31:49 -0700268 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
270};
271
272class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
273 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000274 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275
276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
277 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
278 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000279 if (instruction_->CanThrowIntoCatchBlock()) {
280 // Live registers will be restored in the catch block if caught.
281 SaveLiveRegisters(codegen, instruction_->GetLocations());
282 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100283 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700284 instruction_,
285 instruction_->GetDexPc(),
286 this);
287 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
288 }
289
Alexandre Rames8158f282015-08-07 10:26:17 +0100290 bool IsFatal() const OVERRIDE { return true; }
291
Roland Levillain46648892015-06-19 16:07:18 +0100292 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
293
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
296};
297
298class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
299 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100300 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000301 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200304 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200307 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100308 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200310 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700312 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 }
316 }
317
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700318 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 DCHECK(successor_ == nullptr);
320 return &return_label_;
321 }
322
Roland Levillain46648892015-06-19 16:07:18 +0100323 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
324
Chris Larsena2045912017-11-02 12:39:54 -0700325 HBasicBlock* GetSuccessor() const {
326 return successor_;
327 }
328
Alexey Frunze4dda3372015-06-01 18:31:49 -0700329 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
342 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700343
344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
345 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800346
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 DCHECK(instruction_->IsCheckCast()
349 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
350 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
351
352 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800353 if (!is_fatal_) {
354 SaveLiveRegisters(codegen, locations);
355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356
357 // We're moving two locations to locations that could overlap, so we need a parallel
358 // move resolver.
359 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100362 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100365 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100367 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800368 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100369 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
371 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 } else {
373 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800374 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
375 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 }
377
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800378 if (!is_fatal_) {
379 RestoreLiveRegisters(codegen, locations);
380 __ Bc(GetExitLabel());
381 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
385
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800386 bool IsFatal() const OVERRIDE { return is_fatal_; }
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800389 const bool is_fatal_;
390
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
392};
393
394class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
395 public:
Aart Bik42249c32016-01-07 15:33:50 -0800396 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000397 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398
399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800400 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100402 LocationSummary* locations = instruction_->GetLocations();
403 SaveLiveRegisters(codegen, locations);
404 InvokeRuntimeCallingConvention calling_convention;
405 __ LoadConst32(calling_convention.GetRegisterAt(0),
406 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100407 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100408 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 }
410
Roland Levillain46648892015-06-19 16:07:18 +0100411 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
412
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
415};
416
Alexey Frunze15958152017-02-09 19:08:30 -0800417class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
418 public:
419 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
420
421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
422 LocationSummary* locations = instruction_->GetLocations();
423 __ Bind(GetEntryLabel());
424 SaveLiveRegisters(codegen, locations);
425
426 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100427 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800428 parallel_move.AddMove(
429 locations->InAt(0),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100431 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800432 nullptr);
433 parallel_move.AddMove(
434 locations->InAt(1),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100436 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(2),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100441 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800442 nullptr);
443 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
444
445 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
446 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
447 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
448 RestoreLiveRegisters(codegen, locations);
449 __ Bc(GetExitLabel());
450 }
451
452 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
453
454 private:
455 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
456};
457
458// Slow path marking an object reference `ref` during a read
459// barrier. The field `obj.field` in the object `obj` holding this
460// reference does not get updated by this slow path after marking (see
461// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
462//
463// This means that after the execution of this slow path, `ref` will
464// always be up-to-date, but `obj.field` may not; i.e., after the
465// flip, `ref` will be a to-space reference, but `obj.field` will
466// probably still be a from-space reference (unless it gets updated by
467// another thread, or if another thread installed another object
468// reference (different from `ref`) in `obj.field`).
469//
470// If `entrypoint` is a valid location it is assumed to already be
471// holding the entrypoint. The case where the entrypoint is passed in
472// is for the GcRoot read barrier.
473class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
474 public:
475 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
476 Location ref,
477 Location entrypoint = Location::NoLocation())
478 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
479 DCHECK(kEmitCompilerReadBarrier);
480 }
481
482 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
483
484 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
485 LocationSummary* locations = instruction_->GetLocations();
486 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
487 DCHECK(locations->CanCall());
488 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
489 DCHECK(instruction_->IsInstanceFieldGet() ||
490 instruction_->IsStaticFieldGet() ||
491 instruction_->IsArrayGet() ||
492 instruction_->IsArraySet() ||
493 instruction_->IsLoadClass() ||
494 instruction_->IsLoadString() ||
495 instruction_->IsInstanceOf() ||
496 instruction_->IsCheckCast() ||
497 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
498 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
499 << "Unexpected instruction in read barrier marking slow path: "
500 << instruction_->DebugName();
501
502 __ Bind(GetEntryLabel());
503 // No need to save live registers; it's taken care of by the
504 // entrypoint. Also, there is no need to update the stack mask,
505 // as this runtime call will not trigger a garbage collection.
506 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
507 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
508 (S2 <= ref_reg && ref_reg <= S7) ||
509 (ref_reg == S8)) << ref_reg;
510 // "Compact" slow path, saving two moves.
511 //
512 // Instead of using the standard runtime calling convention (input
513 // and output in A0 and V0 respectively):
514 //
515 // A0 <- ref
516 // V0 <- ReadBarrierMark(A0)
517 // ref <- V0
518 //
519 // we just use rX (the register containing `ref`) as input and output
520 // of a dedicated entrypoint:
521 //
522 // rX <- ReadBarrierMarkRegX(rX)
523 //
524 if (entrypoint_.IsValid()) {
525 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
526 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
527 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
528 __ Nop();
529 } else {
530 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100531 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800532 // This runtime call does not require a stack map.
533 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
534 instruction_,
535 this);
536 }
537 __ Bc(GetExitLabel());
538 }
539
540 private:
541 // The location (register) of the marked object reference.
542 const Location ref_;
543
544 // The location of the entrypoint if already loaded.
545 const Location entrypoint_;
546
547 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
548};
549
550// Slow path marking an object reference `ref` during a read barrier,
551// and if needed, atomically updating the field `obj.field` in the
552// object `obj` holding this reference after marking (contrary to
553// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
554// `obj.field`).
555//
556// This means that after the execution of this slow path, both `ref`
557// and `obj.field` will be up-to-date; i.e., after the flip, both will
558// hold the same to-space reference (unless another thread installed
559// another object reference (different from `ref`) in `obj.field`).
560class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
561 public:
562 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
563 Location ref,
564 GpuRegister obj,
565 Location field_offset,
566 GpuRegister temp1)
567 : SlowPathCodeMIPS64(instruction),
568 ref_(ref),
569 obj_(obj),
570 field_offset_(field_offset),
571 temp1_(temp1) {
572 DCHECK(kEmitCompilerReadBarrier);
573 }
574
575 const char* GetDescription() const OVERRIDE {
576 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
577 }
578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 LocationSummary* locations = instruction_->GetLocations();
581 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
582 DCHECK(locations->CanCall());
583 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
584 // This slow path is only used by the UnsafeCASObject intrinsic.
585 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
586 << "Unexpected instruction in read barrier marking and field updating slow path: "
587 << instruction_->DebugName();
588 DCHECK(instruction_->GetLocations()->Intrinsified());
589 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
590 DCHECK(field_offset_.IsRegister()) << field_offset_;
591
592 __ Bind(GetEntryLabel());
593
594 // Save the old reference.
595 // Note that we cannot use AT or TMP to save the old reference, as those
596 // are used by the code that follows, but we need the old reference after
597 // the call to the ReadBarrierMarkRegX entry point.
598 DCHECK_NE(temp1_, AT);
599 DCHECK_NE(temp1_, TMP);
600 __ Move(temp1_, ref_reg);
601
602 // No need to save live registers; it's taken care of by the
603 // entrypoint. Also, there is no need to update the stack mask,
604 // as this runtime call will not trigger a garbage collection.
605 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
606 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
607 (S2 <= ref_reg && ref_reg <= S7) ||
608 (ref_reg == S8)) << ref_reg;
609 // "Compact" slow path, saving two moves.
610 //
611 // Instead of using the standard runtime calling convention (input
612 // and output in A0 and V0 respectively):
613 //
614 // A0 <- ref
615 // V0 <- ReadBarrierMark(A0)
616 // ref <- V0
617 //
618 // we just use rX (the register containing `ref`) as input and output
619 // of a dedicated entrypoint:
620 //
621 // rX <- ReadBarrierMarkRegX(rX)
622 //
623 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100624 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800625 // This runtime call does not require a stack map.
626 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
627 instruction_,
628 this);
629
630 // If the new reference is different from the old reference,
631 // update the field in the holder (`*(obj_ + field_offset_)`).
632 //
633 // Note that this field could also hold a different object, if
634 // another thread had concurrently changed it. In that case, the
635 // the compare-and-set (CAS) loop below would abort, leaving the
636 // field as-is.
637 Mips64Label done;
638 __ Beqc(temp1_, ref_reg, &done);
639
640 // Update the the holder's field atomically. This may fail if
641 // mutator updates before us, but it's OK. This is achieved
642 // using a strong compare-and-set (CAS) operation with relaxed
643 // memory synchronization ordering, where the expected value is
644 // the old reference and the desired value is the new reference.
645
646 // Convenience aliases.
647 GpuRegister base = obj_;
648 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
649 GpuRegister expected = temp1_;
650 GpuRegister value = ref_reg;
651 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
652 GpuRegister tmp = AT; // Value in memory.
653
654 __ Daddu(tmp_ptr, base, offset);
655
656 if (kPoisonHeapReferences) {
657 __ PoisonHeapReference(expected);
658 // Do not poison `value` if it is the same register as
659 // `expected`, which has just been poisoned.
660 if (value != expected) {
661 __ PoisonHeapReference(value);
662 }
663 }
664
665 // do {
666 // tmp = [r_ptr] - expected;
667 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
668
669 Mips64Label loop_head, exit_loop;
670 __ Bind(&loop_head);
671 __ Ll(tmp, tmp_ptr);
672 // The LL instruction sign-extends the 32-bit value, but
673 // 32-bit references must be zero-extended. Zero-extend `tmp`.
674 __ Dext(tmp, tmp, 0, 32);
675 __ Bnec(tmp, expected, &exit_loop);
676 __ Move(tmp, value);
677 __ Sc(tmp, tmp_ptr);
678 __ Beqzc(tmp, &loop_head);
679 __ Bind(&exit_loop);
680
681 if (kPoisonHeapReferences) {
682 __ UnpoisonHeapReference(expected);
683 // Do not unpoison `value` if it is the same register as
684 // `expected`, which has just been unpoisoned.
685 if (value != expected) {
686 __ UnpoisonHeapReference(value);
687 }
688 }
689
690 __ Bind(&done);
691 __ Bc(GetExitLabel());
692 }
693
694 private:
695 // The location (register) of the marked object reference.
696 const Location ref_;
697 // The register containing the object holding the marked object reference field.
698 const GpuRegister obj_;
699 // The location of the offset of the marked reference field within `obj_`.
700 Location field_offset_;
701
702 const GpuRegister temp1_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
705};
706
707// Slow path generating a read barrier for a heap reference.
708class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
709 public:
710 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
711 Location out,
712 Location ref,
713 Location obj,
714 uint32_t offset,
715 Location index)
716 : SlowPathCodeMIPS64(instruction),
717 out_(out),
718 ref_(ref),
719 obj_(obj),
720 offset_(offset),
721 index_(index) {
722 DCHECK(kEmitCompilerReadBarrier);
723 // If `obj` is equal to `out` or `ref`, it means the initial object
724 // has been overwritten by (or after) the heap object reference load
725 // to be instrumented, e.g.:
726 //
727 // __ LoadFromOffset(kLoadWord, out, out, offset);
728 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
729 //
730 // In that case, we have lost the information about the original
731 // object, and the emitted read barrier cannot work properly.
732 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
733 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
734 }
735
736 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
737 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
738 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800740 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
741 DCHECK(locations->CanCall());
742 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
743 DCHECK(instruction_->IsInstanceFieldGet() ||
744 instruction_->IsStaticFieldGet() ||
745 instruction_->IsArrayGet() ||
746 instruction_->IsInstanceOf() ||
747 instruction_->IsCheckCast() ||
748 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
749 << "Unexpected instruction in read barrier for heap reference slow path: "
750 << instruction_->DebugName();
751
752 __ Bind(GetEntryLabel());
753 SaveLiveRegisters(codegen, locations);
754
755 // We may have to change the index's value, but as `index_` is a
756 // constant member (like other "inputs" of this slow path),
757 // introduce a copy of it, `index`.
758 Location index = index_;
759 if (index_.IsValid()) {
760 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
761 if (instruction_->IsArrayGet()) {
762 // Compute the actual memory offset and store it in `index`.
763 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
764 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
765 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
766 // We are about to change the value of `index_reg` (see the
767 // calls to art::mips64::Mips64Assembler::Sll and
768 // art::mips64::MipsAssembler::Addiu32 below), but it has
769 // not been saved by the previous call to
770 // art::SlowPathCode::SaveLiveRegisters, as it is a
771 // callee-save register --
772 // art::SlowPathCode::SaveLiveRegisters does not consider
773 // callee-save registers, as it has been designed with the
774 // assumption that callee-save registers are supposed to be
775 // handled by the called function. So, as a callee-save
776 // register, `index_reg` _would_ eventually be saved onto
777 // the stack, but it would be too late: we would have
778 // changed its value earlier. Therefore, we manually save
779 // it here into another freely available register,
780 // `free_reg`, chosen of course among the caller-save
781 // registers (as a callee-save `free_reg` register would
782 // exhibit the same problem).
783 //
784 // Note we could have requested a temporary register from
785 // the register allocator instead; but we prefer not to, as
786 // this is a slow path, and we know we can find a
787 // caller-save register that is available.
788 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
789 __ Move(free_reg, index_reg);
790 index_reg = free_reg;
791 index = Location::RegisterLocation(index_reg);
792 } else {
793 // The initial register stored in `index_` has already been
794 // saved in the call to art::SlowPathCode::SaveLiveRegisters
795 // (as it is not a callee-save register), so we can freely
796 // use it.
797 }
798 // Shifting the index value contained in `index_reg` by the scale
799 // factor (2) cannot overflow in practice, as the runtime is
800 // unable to allocate object arrays with a size larger than
801 // 2^26 - 1 (that is, 2^28 - 4 bytes).
802 __ Sll(index_reg, index_reg, TIMES_4);
803 static_assert(
804 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
805 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
806 __ Addiu32(index_reg, index_reg, offset_);
807 } else {
808 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
809 // intrinsics, `index_` is not shifted by a scale factor of 2
810 // (as in the case of ArrayGet), as it is actually an offset
811 // to an object field within an object.
812 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
813 DCHECK(instruction_->GetLocations()->Intrinsified());
814 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
815 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
816 << instruction_->AsInvoke()->GetIntrinsic();
817 DCHECK_EQ(offset_, 0U);
818 DCHECK(index_.IsRegister());
819 }
820 }
821
822 // We're moving two or three locations to locations that could
823 // overlap, so we need a parallel move resolver.
824 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100825 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800826 parallel_move.AddMove(ref_,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800829 nullptr);
830 parallel_move.AddMove(obj_,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800833 nullptr);
834 if (index.IsValid()) {
835 parallel_move.AddMove(index,
836 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100837 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800838 nullptr);
839 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
840 } else {
841 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
842 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
843 }
844 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
845 instruction_,
846 instruction_->GetDexPc(),
847 this);
848 CheckEntrypointTypes<
849 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
850 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
851
852 RestoreLiveRegisters(codegen, locations);
853 __ Bc(GetExitLabel());
854 }
855
856 const char* GetDescription() const OVERRIDE {
857 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
858 }
859
860 private:
861 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
862 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
863 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
864 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
865 if (i != ref &&
866 i != obj &&
867 !codegen->IsCoreCalleeSaveRegister(i) &&
868 !codegen->IsBlockedCoreRegister(i)) {
869 return static_cast<GpuRegister>(i);
870 }
871 }
872 // We shall never fail to find a free caller-save register, as
873 // there are more than two core caller-save registers on MIPS64
874 // (meaning it is possible to find one which is different from
875 // `ref` and `obj`).
876 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
877 LOG(FATAL) << "Could not find a free caller-save register";
878 UNREACHABLE();
879 }
880
881 const Location out_;
882 const Location ref_;
883 const Location obj_;
884 const uint32_t offset_;
885 // An additional location containing an index to an array.
886 // Only used for HArrayGet and the UnsafeGetObject &
887 // UnsafeGetObjectVolatile intrinsics.
888 const Location index_;
889
890 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
891};
892
893// Slow path generating a read barrier for a GC root.
894class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
895 public:
896 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
897 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
898 DCHECK(kEmitCompilerReadBarrier);
899 }
900
901 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
902 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100903 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800904 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
905 DCHECK(locations->CanCall());
906 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
907 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
908 << "Unexpected instruction in read barrier for GC root slow path: "
909 << instruction_->DebugName();
910
911 __ Bind(GetEntryLabel());
912 SaveLiveRegisters(codegen, locations);
913
914 InvokeRuntimeCallingConvention calling_convention;
915 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
916 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
917 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100918 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800919 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
920 instruction_,
921 instruction_->GetDexPc(),
922 this);
923 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
924 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
925
926 RestoreLiveRegisters(codegen, locations);
927 __ Bc(GetExitLabel());
928 }
929
930 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
931
932 private:
933 const Location out_;
934 const Location root_;
935
936 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
937};
938
Alexey Frunze4dda3372015-06-01 18:31:49 -0700939CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
940 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100941 const CompilerOptions& compiler_options,
942 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943 : CodeGenerator(graph,
944 kNumberOfGpuRegisters,
945 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000946 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
948 arraysize(kCoreCalleeSaves)),
949 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
950 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100951 compiler_options,
952 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100953 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700954 location_builder_(graph, this),
955 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 move_resolver_(graph->GetAllocator(), this),
957 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800958 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800959 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800961 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
963 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
964 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
965 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
966 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
967 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
968 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800969 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800971 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700973 // Save RA (containing the return address) to mimic Quick.
974 AddAllocatedRegister(Location::RegisterLocation(RA));
975}
976
977#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100978// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
979#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700980#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700981
982void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700983 // Ensure that we fix up branches.
984 __ FinalizeCode();
985
986 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100987 StackMapStream* stack_map_stream = GetStackMapStream();
988 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800989 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +0000990 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700991 uint32_t new_position = __ GetAdjustedPosition(old_position);
992 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100993 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700994 }
995
996 // Adjust pc offsets for the disassembly information.
997 if (disasm_info_ != nullptr) {
998 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
999 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1000 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1001 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1002 it.second.start = __ GetAdjustedPosition(it.second.start);
1003 it.second.end = __ GetAdjustedPosition(it.second.end);
1004 }
1005 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1006 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1007 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1008 }
1009 }
1010
Alexey Frunze4dda3372015-06-01 18:31:49 -07001011 CodeGenerator::Finalize(allocator);
1012}
1013
1014Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1015 return codegen_->GetAssembler();
1016}
1017
1018void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001019 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001020 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1021}
1022
1023void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001024 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1026}
1027
1028void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1029 // Pop reg
1030 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001031 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032}
1033
1034void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1035 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001036 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001037 __ Sd(GpuRegister(reg), SP, 0);
1038}
1039
1040void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1041 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1042 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1043 // Allocate a scratch register other than TMP, if available.
1044 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1045 // automatically unspilled when the scratch scope object is destroyed).
1046 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1047 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001048 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001049 __ LoadFromOffset(load_type,
1050 GpuRegister(ensure_scratch.GetRegister()),
1051 SP,
1052 index1 + stack_offset);
1053 __ LoadFromOffset(load_type,
1054 TMP,
1055 SP,
1056 index2 + stack_offset);
1057 __ StoreToOffset(store_type,
1058 GpuRegister(ensure_scratch.GetRegister()),
1059 SP,
1060 index2 + stack_offset);
1061 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1062}
1063
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001064void ParallelMoveResolverMIPS64::ExchangeQuadSlots(int index1, int index2) {
1065 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, index1);
1066 __ LoadFpuFromOffset(kLoadQuadword, FTMP2, SP, index2);
1067 __ StoreFpuToOffset(kStoreQuadword, FTMP, SP, index2);
1068 __ StoreFpuToOffset(kStoreQuadword, FTMP2, SP, index1);
1069}
1070
Alexey Frunze4dda3372015-06-01 18:31:49 -07001071static dwarf::Reg DWARFReg(GpuRegister reg) {
1072 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1073}
1074
David Srbeckyba702002016-02-01 18:15:29 +00001075static dwarf::Reg DWARFReg(FpuRegister reg) {
1076 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1077}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001078
1079void CodeGeneratorMIPS64::GenerateFrameEntry() {
1080 __ Bind(&frame_entry_label_);
1081
Vladimir Marko33bff252017-11-01 14:35:42 +00001082 bool do_overflow_check =
1083 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001084
1085 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001086 __ LoadFromOffset(
1087 kLoadWord,
1088 ZERO,
1089 SP,
1090 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091 RecordPcInfo(nullptr, 0);
1092 }
1093
Alexey Frunze4dda3372015-06-01 18:31:49 -07001094 if (HasEmptyFrame()) {
1095 return;
1096 }
1097
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001098 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001099 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1100 LOG(FATAL) << "Stack frame larger than "
1101 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001102 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001103
1104 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001105
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001107 __ IncreaseFrameSize(ofs);
1108
1109 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1110 GpuRegister reg = kCoreCalleeSaves[i];
1111 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001112 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001113 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001114 __ cfi().RelOffset(DWARFReg(reg), ofs);
1115 }
1116 }
1117
1118 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1119 FpuRegister reg = kFpuCalleeSaves[i];
1120 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001121 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001122 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001123 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001124 }
1125 }
1126
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001127 // Save the current method if we need it. Note that we do not
1128 // do this in HCurrentMethod, as the instruction might have been removed
1129 // in the SSA graph.
1130 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001131 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001132 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001133
1134 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1135 // Initialize should_deoptimize flag to 0.
1136 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1137 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138}
1139
1140void CodeGeneratorMIPS64::GenerateFrameExit() {
1141 __ cfi().RememberState();
1142
Alexey Frunze4dda3372015-06-01 18:31:49 -07001143 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001144 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001145
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001146 // For better instruction scheduling restore RA before other registers.
1147 uint32_t ofs = GetFrameSize();
1148 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149 GpuRegister reg = kCoreCalleeSaves[i];
1150 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001151 ofs -= kMips64DoublewordSize;
1152 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153 __ cfi().Restore(DWARFReg(reg));
1154 }
1155 }
1156
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001157 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1158 FpuRegister reg = kFpuCalleeSaves[i];
1159 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1160 ofs -= kMips64DoublewordSize;
1161 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1162 __ cfi().Restore(DWARFReg(reg));
1163 }
1164 }
1165
1166 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001167 }
1168
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001169 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001170
1171 __ cfi().RestoreState();
1172 __ cfi().DefCFAOffset(GetFrameSize());
1173}
1174
1175void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1176 __ Bind(GetLabelOf(block));
1177}
1178
1179void CodeGeneratorMIPS64::MoveLocation(Location destination,
1180 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001181 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182 if (source.Equals(destination)) {
1183 return;
1184 }
1185
1186 // A valid move can always be inferred from the destination and source
1187 // locations. When moving from and to a register, the argument type can be
1188 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001189 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001190 DCHECK_EQ(unspecified_type, false);
1191
1192 if (destination.IsRegister() || destination.IsFpuRegister()) {
1193 if (unspecified_type) {
1194 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1195 if (source.IsStackSlot() ||
1196 (src_cst != nullptr && (src_cst->IsIntConstant()
1197 || src_cst->IsFloatConstant()
1198 || src_cst->IsNullConstant()))) {
1199 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001201 } else {
1202 // If the source is a double stack slot or a 64bit constant, a 64bit
1203 // type is appropriate. Else the source is a register, and since the
1204 // type has not been specified, we chose a 64bit type to force a 64bit
1205 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001206 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001207 }
1208 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001209 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1210 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001211 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1212 // Move to GPR/FPR from stack
1213 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 __ LoadFpuFromOffset(load_type,
1216 destination.AsFpuRegister<FpuRegister>(),
1217 SP,
1218 source.GetStackIndex());
1219 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001220 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001221 __ LoadFromOffset(load_type,
1222 destination.AsRegister<GpuRegister>(),
1223 SP,
1224 source.GetStackIndex());
1225 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001226 } else if (source.IsSIMDStackSlot()) {
1227 __ LoadFpuFromOffset(kLoadQuadword,
1228 destination.AsFpuRegister<FpuRegister>(),
1229 SP,
1230 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001231 } else if (source.IsConstant()) {
1232 // Move to GPR/FPR from constant
1233 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001234 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001235 gpr = destination.AsRegister<GpuRegister>();
1236 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001237 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001238 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001240 gpr = ZERO;
1241 } else {
1242 __ LoadConst32(gpr, value);
1243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001244 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001245 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001246 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001247 gpr = ZERO;
1248 } else {
1249 __ LoadConst64(gpr, value);
1250 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001252 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001253 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001254 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001255 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1256 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001258 if (destination.IsRegister()) {
1259 // Move to GPR from GPR
1260 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1261 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001263 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001264 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1265 } else {
1266 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1267 }
1268 }
1269 } else if (source.IsFpuRegister()) {
1270 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001271 if (GetGraph()->HasSIMD()) {
1272 __ MoveV(VectorRegisterFrom(destination),
1273 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001274 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001275 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001276 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001277 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1278 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001279 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001280 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001282 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 } else {
1284 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001285 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1287 } else {
1288 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1289 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 }
1291 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001292 } else if (destination.IsSIMDStackSlot()) {
1293 if (source.IsFpuRegister()) {
1294 __ StoreFpuToOffset(kStoreQuadword,
1295 source.AsFpuRegister<FpuRegister>(),
1296 SP,
1297 destination.GetStackIndex());
1298 } else {
1299 DCHECK(source.IsSIMDStackSlot());
1300 __ LoadFpuFromOffset(kLoadQuadword,
1301 FTMP,
1302 SP,
1303 source.GetStackIndex());
1304 __ StoreFpuToOffset(kStoreQuadword,
1305 FTMP,
1306 SP,
1307 destination.GetStackIndex());
1308 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001309 } else { // The destination is not a register. It must be a stack slot.
1310 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1311 if (source.IsRegister() || source.IsFpuRegister()) {
1312 if (unspecified_type) {
1313 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001315 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001316 dst_type =
1317 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001318 }
1319 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001320 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1321 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001322 // Move to stack from GPR/FPR
1323 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1324 if (source.IsRegister()) {
1325 __ StoreToOffset(store_type,
1326 source.AsRegister<GpuRegister>(),
1327 SP,
1328 destination.GetStackIndex());
1329 } else {
1330 __ StoreFpuToOffset(store_type,
1331 source.AsFpuRegister<FpuRegister>(),
1332 SP,
1333 destination.GetStackIndex());
1334 }
1335 } else if (source.IsConstant()) {
1336 // Move to stack from constant
1337 HConstant* src_cst = source.GetConstant();
1338 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001339 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001340 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001341 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1342 if (value != 0) {
1343 gpr = TMP;
1344 __ LoadConst32(gpr, value);
1345 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001346 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001347 DCHECK(destination.IsDoubleStackSlot());
1348 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1349 if (value != 0) {
1350 gpr = TMP;
1351 __ LoadConst64(gpr, value);
1352 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001353 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001354 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001355 } else {
1356 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1357 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1358 // Move to stack from stack
1359 if (destination.IsStackSlot()) {
1360 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1361 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1362 } else {
1363 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1364 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1365 }
1366 }
1367 }
1368}
1369
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001370void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001371 DCHECK(!loc1.IsConstant());
1372 DCHECK(!loc2.IsConstant());
1373
1374 if (loc1.Equals(loc2)) {
1375 return;
1376 }
1377
1378 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1379 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001380 bool is_simd1 = loc1.IsSIMDStackSlot();
1381 bool is_simd2 = loc2.IsSIMDStackSlot();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001382 bool is_fp_reg1 = loc1.IsFpuRegister();
1383 bool is_fp_reg2 = loc2.IsFpuRegister();
1384
1385 if (loc2.IsRegister() && loc1.IsRegister()) {
1386 // Swap 2 GPRs
1387 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1388 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1389 __ Move(TMP, r2);
1390 __ Move(r2, r1);
1391 __ Move(r1, TMP);
1392 } else if (is_fp_reg2 && is_fp_reg1) {
1393 // Swap 2 FPRs
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001394 if (GetGraph()->HasSIMD()) {
1395 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1396 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1397 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001398 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001399 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1400 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
1401 if (type == DataType::Type::kFloat32) {
1402 __ MovS(FTMP, r1);
1403 __ MovS(r1, r2);
1404 __ MovS(r2, FTMP);
1405 } else {
1406 DCHECK_EQ(type, DataType::Type::kFloat64);
1407 __ MovD(FTMP, r1);
1408 __ MovD(r1, r2);
1409 __ MovD(r2, FTMP);
1410 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001411 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001412 } else if (is_slot1 != is_slot2) {
1413 // Swap GPR/FPR and stack slot
1414 Location reg_loc = is_slot1 ? loc2 : loc1;
1415 Location mem_loc = is_slot1 ? loc1 : loc2;
1416 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1417 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001418 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1420 if (reg_loc.IsFpuRegister()) {
1421 __ StoreFpuToOffset(store_type,
1422 reg_loc.AsFpuRegister<FpuRegister>(),
1423 SP,
1424 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425 if (mem_loc.IsStackSlot()) {
1426 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1427 } else {
1428 DCHECK(mem_loc.IsDoubleStackSlot());
1429 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1430 }
1431 } else {
1432 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1433 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1434 }
1435 } else if (is_slot1 && is_slot2) {
1436 move_resolver_.Exchange(loc1.GetStackIndex(),
1437 loc2.GetStackIndex(),
1438 loc1.IsDoubleStackSlot());
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001439 } else if (is_simd1 && is_simd2) {
1440 move_resolver_.ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
1441 } else if ((is_fp_reg1 && is_simd2) || (is_fp_reg2 && is_simd1)) {
1442 Location fp_reg_loc = is_fp_reg1 ? loc1 : loc2;
1443 Location mem_loc = is_fp_reg1 ? loc2 : loc1;
1444 __ LoadFpuFromOffset(kLoadQuadword, FTMP, SP, mem_loc.GetStackIndex());
1445 __ StoreFpuToOffset(kStoreQuadword,
1446 fp_reg_loc.AsFpuRegister<FpuRegister>(),
1447 SP,
1448 mem_loc.GetStackIndex());
1449 __ MoveV(VectorRegisterFrom(fp_reg_loc), static_cast<VectorRegister>(FTMP));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001450 } else {
1451 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1452 }
1453}
1454
Calin Juravle175dc732015-08-25 15:42:32 +01001455void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1456 DCHECK(location.IsRegister());
1457 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1458}
1459
Calin Juravlee460d1d2015-09-29 04:52:17 +01001460void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1461 if (location.IsRegister()) {
1462 locations->AddTemp(location);
1463 } else {
1464 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1465 }
1466}
1467
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001468void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1469 GpuRegister value,
1470 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001471 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001472 GpuRegister card = AT;
1473 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001474 if (value_can_be_null) {
1475 __ Beqzc(value, &done);
1476 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001477 __ LoadFromOffset(kLoadDoubleword,
1478 card,
1479 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001480 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001481 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1482 __ Daddu(temp, card, temp);
1483 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001484 if (value_can_be_null) {
1485 __ Bind(&done);
1486 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001487}
1488
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001489template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001490inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1491 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001492 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001493 for (const PcRelativePatchInfo& info : infos) {
1494 const DexFile& dex_file = info.target_dex_file;
1495 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001496 DCHECK(info.label.IsBound());
1497 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1498 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1499 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1500 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001501 }
1502}
1503
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001504void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001505 DCHECK(linker_patches->empty());
1506 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001507 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001508 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001509 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001510 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001511 pc_relative_string_patches_.size() +
1512 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001513 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001514 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001515 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1516 pc_relative_method_patches_, linker_patches);
1517 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1518 pc_relative_type_patches_, linker_patches);
1519 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1520 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001521 } else {
1522 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001523 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1524 pc_relative_type_patches_, linker_patches);
1525 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1526 pc_relative_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001527 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001528 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1529 method_bss_entry_patches_, linker_patches);
1530 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1531 type_bss_entry_patches_, linker_patches);
1532 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1533 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001534 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001535}
1536
Vladimir Marko65979462017-05-19 17:25:12 +01001537CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001538 MethodReference target_method,
1539 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001540 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001541 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001542 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001543 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001544}
1545
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001546CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001547 MethodReference target_method,
1548 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001549 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001550 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001551 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001552 &method_bss_entry_patches_);
1553}
1554
Alexey Frunzef63f5692016-12-13 17:43:11 -08001555CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001556 const DexFile& dex_file,
1557 dex::TypeIndex type_index,
1558 const PcRelativePatchInfo* info_high) {
1559 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001560}
1561
Vladimir Marko1998cd02017-01-13 13:02:58 +00001562CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001563 const DexFile& dex_file,
1564 dex::TypeIndex type_index,
1565 const PcRelativePatchInfo* info_high) {
1566 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001567}
1568
Vladimir Marko65979462017-05-19 17:25:12 +01001569CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001570 const DexFile& dex_file,
1571 dex::StringIndex string_index,
1572 const PcRelativePatchInfo* info_high) {
1573 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001574}
1575
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001576CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1577 const DexFile& dex_file,
1578 dex::StringIndex string_index,
1579 const PcRelativePatchInfo* info_high) {
1580 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1581}
1582
Alexey Frunze19f6c692016-11-30 19:19:55 -08001583CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001584 const DexFile& dex_file,
1585 uint32_t offset_or_index,
1586 const PcRelativePatchInfo* info_high,
1587 ArenaDeque<PcRelativePatchInfo>* patches) {
1588 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001589 return &patches->back();
1590}
1591
Alexey Frunzef63f5692016-12-13 17:43:11 -08001592Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1593 return map->GetOrCreate(
1594 value,
1595 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1596}
1597
Alexey Frunze19f6c692016-11-30 19:19:55 -08001598Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1599 return uint64_literals_.GetOrCreate(
1600 value,
1601 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1602}
1603
Alexey Frunzef63f5692016-12-13 17:43:11 -08001604Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001605 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001606}
1607
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001608void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1609 GpuRegister out,
1610 PcRelativePatchInfo* info_low) {
1611 DCHECK(!info_high->patch_info_high);
1612 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001613 // Add the high half of a 32-bit offset to PC.
1614 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001615 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001616 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001617 if (info_low != nullptr) {
1618 DCHECK_EQ(info_low->patch_info_high, info_high);
1619 __ Bind(&info_low->label);
1620 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001621}
1622
Alexey Frunze627c1a02017-01-30 19:28:14 -08001623Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1624 dex::StringIndex string_index,
1625 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001626 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001627 return jit_string_patches_.GetOrCreate(
1628 StringReference(&dex_file, string_index),
1629 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1630}
1631
1632Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1633 dex::TypeIndex type_index,
1634 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001635 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001636 return jit_class_patches_.GetOrCreate(
1637 TypeReference(&dex_file, type_index),
1638 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1639}
1640
1641void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1642 const uint8_t* roots_data,
1643 const Literal* literal,
1644 uint64_t index_in_table) const {
1645 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1646 uintptr_t address =
1647 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1648 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1649}
1650
1651void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1652 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001653 const StringReference& string_reference = entry.first;
1654 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001655 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001656 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001657 }
1658 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001659 const TypeReference& type_reference = entry.first;
1660 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001661 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001662 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001663 }
1664}
1665
David Brazdil58282f42016-01-14 12:45:10 +00001666void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001667 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1668 blocked_core_registers_[ZERO] = true;
1669 blocked_core_registers_[K0] = true;
1670 blocked_core_registers_[K1] = true;
1671 blocked_core_registers_[GP] = true;
1672 blocked_core_registers_[SP] = true;
1673 blocked_core_registers_[RA] = true;
1674
Lazar Trsicd9672662015-09-03 17:33:01 +02001675 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1676 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001677 blocked_core_registers_[AT] = true;
1678 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001679 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001680 blocked_fpu_registers_[FTMP] = true;
1681
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001682 if (GetInstructionSetFeatures().HasMsa()) {
1683 // To be used just for MSA instructions.
1684 blocked_fpu_registers_[FTMP2] = true;
1685 }
1686
Alexey Frunze4dda3372015-06-01 18:31:49 -07001687 // Reserve suspend and thread registers.
1688 blocked_core_registers_[S0] = true;
1689 blocked_core_registers_[TR] = true;
1690
1691 // Reserve T9 for function calls
1692 blocked_core_registers_[T9] = true;
1693
Goran Jakovljevic782be112016-06-21 12:39:04 +02001694 if (GetGraph()->IsDebuggable()) {
1695 // Stubs do not save callee-save floating point registers. If the graph
1696 // is debuggable, we need to deal with these registers differently. For
1697 // now, just block them.
1698 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1699 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1700 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701 }
1702}
1703
Alexey Frunze4dda3372015-06-01 18:31:49 -07001704size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1705 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001706 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001707}
1708
1709size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1710 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001711 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001712}
1713
1714size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001715 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1716 FpuRegister(reg_id),
1717 SP,
1718 stack_index);
1719 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001720}
1721
1722size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001723 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1724 FpuRegister(reg_id),
1725 SP,
1726 stack_index);
1727 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728}
1729
1730void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001731 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001732}
1733
1734void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001735 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001736}
1737
Calin Juravle175dc732015-08-25 15:42:32 +01001738void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001739 HInstruction* instruction,
1740 uint32_t dex_pc,
1741 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001742 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001743 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001744 if (EntrypointRequiresStackMap(entrypoint)) {
1745 RecordPcInfo(instruction, dex_pc, slow_path);
1746 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001747}
1748
Alexey Frunze15958152017-02-09 19:08:30 -08001749void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1750 HInstruction* instruction,
1751 SlowPathCode* slow_path) {
1752 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1753 GenerateInvokeRuntime(entry_point_offset);
1754}
1755
1756void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1757 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1758 __ Jalr(T9);
1759 __ Nop();
1760}
1761
Alexey Frunze4dda3372015-06-01 18:31:49 -07001762void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1763 GpuRegister class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07001764 __ LoadFromOffset(kLoadSignedByte, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001765 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1766 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001767 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1768 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001769 __ Bind(slow_path->GetExitLabel());
1770}
1771
1772void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1773 __ Sync(0); // only stype 0 is supported
1774}
1775
1776void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1777 HBasicBlock* successor) {
1778 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001779 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1780
1781 if (slow_path == nullptr) {
1782 slow_path =
1783 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1784 instruction->SetSlowPath(slow_path);
1785 codegen_->AddSlowPath(slow_path);
1786 if (successor != nullptr) {
1787 DCHECK(successor->IsLoopHeader());
1788 }
1789 } else {
1790 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1791 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001792
1793 __ LoadFromOffset(kLoadUnsignedHalfword,
1794 TMP,
1795 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001796 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001797 if (successor == nullptr) {
1798 __ Bnezc(TMP, slow_path->GetEntryLabel());
1799 __ Bind(slow_path->GetReturnLabel());
1800 } else {
1801 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001802 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803 // slow_path will return to GetLabelOf(successor).
1804 }
1805}
1806
1807InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1808 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001809 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001810 assembler_(codegen->GetAssembler()),
1811 codegen_(codegen) {}
1812
1813void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1814 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001815 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001817 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 case DataType::Type::kInt32:
1819 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001820 locations->SetInAt(0, Location::RequiresRegister());
1821 HInstruction* right = instruction->InputAt(1);
1822 bool can_use_imm = false;
1823 if (right->IsConstant()) {
1824 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1825 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1826 can_use_imm = IsUint<16>(imm);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001827 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001828 DCHECK(instruction->IsAdd() || instruction->IsSub());
1829 bool single_use = right->GetUses().HasExactlyOneElement();
1830 if (instruction->IsSub()) {
1831 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1832 imm = -imm;
1833 }
1834 }
1835 if (type == DataType::Type::kInt32) {
1836 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1837 } else {
1838 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1839 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001840 }
1841 }
1842 if (can_use_imm)
1843 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1844 else
1845 locations->SetInAt(1, Location::RequiresRegister());
1846 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1847 }
1848 break;
1849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001850 case DataType::Type::kFloat32:
1851 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001852 locations->SetInAt(0, Location::RequiresFpuRegister());
1853 locations->SetInAt(1, Location::RequiresFpuRegister());
1854 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1855 break;
1856
1857 default:
1858 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1859 }
1860}
1861
1862void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001864 LocationSummary* locations = instruction->GetLocations();
1865
1866 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001867 case DataType::Type::kInt32:
1868 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001869 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1870 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1871 Location rhs_location = locations->InAt(1);
1872
1873 GpuRegister rhs_reg = ZERO;
1874 int64_t rhs_imm = 0;
1875 bool use_imm = rhs_location.IsConstant();
1876 if (use_imm) {
1877 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1878 } else {
1879 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1880 }
1881
1882 if (instruction->IsAnd()) {
1883 if (use_imm)
1884 __ Andi(dst, lhs, rhs_imm);
1885 else
1886 __ And(dst, lhs, rhs_reg);
1887 } else if (instruction->IsOr()) {
1888 if (use_imm)
1889 __ Ori(dst, lhs, rhs_imm);
1890 else
1891 __ Or(dst, lhs, rhs_reg);
1892 } else if (instruction->IsXor()) {
1893 if (use_imm)
1894 __ Xori(dst, lhs, rhs_imm);
1895 else
1896 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001897 } else if (instruction->IsAdd() || instruction->IsSub()) {
1898 if (instruction->IsSub()) {
1899 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001900 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001901 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001902 if (use_imm) {
1903 if (IsInt<16>(rhs_imm)) {
1904 __ Addiu(dst, lhs, rhs_imm);
1905 } else {
1906 int16_t rhs_imm_high = High16Bits(rhs_imm);
1907 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1908 if (rhs_imm_low < 0) {
1909 rhs_imm_high += 1;
1910 }
1911 __ Aui(dst, lhs, rhs_imm_high);
1912 if (rhs_imm_low != 0) {
1913 __ Addiu(dst, dst, rhs_imm_low);
1914 }
1915 }
1916 } else {
1917 if (instruction->IsAdd()) {
1918 __ Addu(dst, lhs, rhs_reg);
1919 } else {
1920 DCHECK(instruction->IsSub());
1921 __ Subu(dst, lhs, rhs_reg);
1922 }
1923 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001924 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001925 if (use_imm) {
1926 if (IsInt<16>(rhs_imm)) {
1927 __ Daddiu(dst, lhs, rhs_imm);
1928 } else if (IsInt<32>(rhs_imm)) {
1929 int16_t rhs_imm_high = High16Bits(rhs_imm);
1930 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1931 bool overflow_hi16 = false;
1932 if (rhs_imm_low < 0) {
1933 rhs_imm_high += 1;
1934 overflow_hi16 = (rhs_imm_high == -32768);
1935 }
1936 __ Daui(dst, lhs, rhs_imm_high);
1937 if (rhs_imm_low != 0) {
1938 __ Daddiu(dst, dst, rhs_imm_low);
1939 }
1940 if (overflow_hi16) {
1941 __ Dahi(dst, 1);
1942 }
1943 } else {
1944 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1945 if (rhs_imm_low < 0) {
1946 rhs_imm += (INT64_C(1) << 16);
1947 }
1948 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
1949 if (rhs_imm_upper < 0) {
1950 rhs_imm += (INT64_C(1) << 32);
1951 }
1952 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
1953 if (rhs_imm_high < 0) {
1954 rhs_imm += (INT64_C(1) << 48);
1955 }
1956 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
1957 GpuRegister tmp = lhs;
1958 if (rhs_imm_low != 0) {
1959 __ Daddiu(dst, tmp, rhs_imm_low);
1960 tmp = dst;
1961 }
1962 // Dahi and Dati must use the same input and output register, so we have to initialize
1963 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
1964 // Daui(dst, lhs, 0).
1965 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
1966 __ Daui(dst, tmp, rhs_imm_upper);
1967 }
1968 if (rhs_imm_high != 0) {
1969 __ Dahi(dst, rhs_imm_high);
1970 }
1971 if (rhs_imm_top != 0) {
1972 __ Dati(dst, rhs_imm_top);
1973 }
1974 }
1975 } else if (instruction->IsAdd()) {
1976 __ Daddu(dst, lhs, rhs_reg);
1977 } else {
1978 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001979 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001980 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001981 }
1982 }
1983 break;
1984 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001985 case DataType::Type::kFloat32:
1986 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001987 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1988 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1989 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1990 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001992 __ AddS(dst, lhs, rhs);
1993 else
1994 __ AddD(dst, lhs, rhs);
1995 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001997 __ SubS(dst, lhs, rhs);
1998 else
1999 __ SubD(dst, lhs, rhs);
2000 } else {
2001 LOG(FATAL) << "Unexpected floating-point binary operation";
2002 }
2003 break;
2004 }
2005 default:
2006 LOG(FATAL) << "Unexpected binary operation type " << type;
2007 }
2008}
2009
2010void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002011 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002012
Vladimir Markoca6fff82017-10-03 14:49:14 +01002013 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002015 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 case DataType::Type::kInt32:
2017 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002018 locations->SetInAt(0, Location::RequiresRegister());
2019 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 break;
2022 }
2023 default:
2024 LOG(FATAL) << "Unexpected shift type " << type;
2025 }
2026}
2027
2028void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002029 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002030 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002031 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032
2033 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 case DataType::Type::kInt32:
2035 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002036 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2037 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2038 Location rhs_location = locations->InAt(1);
2039
2040 GpuRegister rhs_reg = ZERO;
2041 int64_t rhs_imm = 0;
2042 bool use_imm = rhs_location.IsConstant();
2043 if (use_imm) {
2044 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2045 } else {
2046 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2047 }
2048
2049 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002050 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002052
Alexey Frunze92d90602015-12-18 18:16:36 -08002053 if (shift_value == 0) {
2054 if (dst != lhs) {
2055 __ Move(dst, lhs);
2056 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002058 if (instr->IsShl()) {
2059 __ Sll(dst, lhs, shift_value);
2060 } else if (instr->IsShr()) {
2061 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002062 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002064 } else {
2065 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002066 }
2067 } else {
2068 if (shift_value < 32) {
2069 if (instr->IsShl()) {
2070 __ Dsll(dst, lhs, shift_value);
2071 } else if (instr->IsShr()) {
2072 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002073 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002075 } else {
2076 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002077 }
2078 } else {
2079 shift_value -= 32;
2080 if (instr->IsShl()) {
2081 __ Dsll32(dst, lhs, shift_value);
2082 } else if (instr->IsShr()) {
2083 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002084 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002085 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002086 } else {
2087 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002088 }
2089 }
2090 }
2091 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002092 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002093 if (instr->IsShl()) {
2094 __ Sllv(dst, lhs, rhs_reg);
2095 } else if (instr->IsShr()) {
2096 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002097 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002098 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002099 } else {
2100 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002101 }
2102 } else {
2103 if (instr->IsShl()) {
2104 __ Dsllv(dst, lhs, rhs_reg);
2105 } else if (instr->IsShr()) {
2106 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002107 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002108 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002109 } else {
2110 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 }
2112 }
2113 }
2114 break;
2115 }
2116 default:
2117 LOG(FATAL) << "Unexpected shift operation type " << type;
2118 }
2119}
2120
2121void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2122 HandleBinaryOp(instruction);
2123}
2124
2125void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2126 HandleBinaryOp(instruction);
2127}
2128
2129void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2130 HandleBinaryOp(instruction);
2131}
2132
2133void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2134 HandleBinaryOp(instruction);
2135}
2136
2137void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002139 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002140 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002141 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002142 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2143 object_array_get_with_read_barrier
2144 ? LocationSummary::kCallOnSlowPath
2145 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002146 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2147 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2148 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002149 locations->SetInAt(0, Location::RequiresRegister());
2150 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002151 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002152 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2153 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002154 // The output overlaps in the case of an object array get with
2155 // read barriers enabled: we do not want the move to overwrite the
2156 // array's location, as we need it to emit the read barrier.
2157 locations->SetOut(Location::RequiresRegister(),
2158 object_array_get_with_read_barrier
2159 ? Location::kOutputOverlap
2160 : Location::kNoOutputOverlap);
2161 }
2162 // We need a temporary register for the read barrier marking slow
2163 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2164 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002165 bool temp_needed = instruction->GetIndex()->IsConstant()
2166 ? !kBakerReadBarrierThunksEnableForFields
2167 : !kBakerReadBarrierThunksEnableForArrays;
2168 if (temp_needed) {
2169 locations->AddTemp(Location::RequiresRegister());
2170 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002171 }
2172}
2173
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002174static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2175 auto null_checker = [codegen, instruction]() {
2176 codegen->MaybeRecordImplicitNullCheck(instruction);
2177 };
2178 return null_checker;
2179}
2180
Alexey Frunze4dda3372015-06-01 18:31:49 -07002181void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2182 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002183 Location obj_loc = locations->InAt(0);
2184 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2185 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002186 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002187 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002188 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002190 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002191 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2192 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002193 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002194 case DataType::Type::kBool:
2195 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002196 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002197 if (index.IsConstant()) {
2198 size_t offset =
2199 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002200 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002201 } else {
2202 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002203 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002204 }
2205 break;
2206 }
2207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002209 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002210 if (index.IsConstant()) {
2211 size_t offset =
2212 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002213 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002214 } else {
2215 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002216 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002217 }
2218 break;
2219 }
2220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002221 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002222 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002223 if (maybe_compressed_char_at) {
2224 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002225 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002226 __ Dext(TMP, TMP, 0, 1);
2227 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2228 "Expecting 0=compressed, 1=uncompressed");
2229 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002230 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002231 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2232 if (maybe_compressed_char_at) {
2233 Mips64Label uncompressed_load, done;
2234 __ Bnezc(TMP, &uncompressed_load);
2235 __ LoadFromOffset(kLoadUnsignedByte,
2236 out,
2237 obj,
2238 data_offset + (const_index << TIMES_1));
2239 __ Bc(&done);
2240 __ Bind(&uncompressed_load);
2241 __ LoadFromOffset(kLoadUnsignedHalfword,
2242 out,
2243 obj,
2244 data_offset + (const_index << TIMES_2));
2245 __ Bind(&done);
2246 } else {
2247 __ LoadFromOffset(kLoadUnsignedHalfword,
2248 out,
2249 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002250 data_offset + (const_index << TIMES_2),
2251 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002252 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002253 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002254 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2255 if (maybe_compressed_char_at) {
2256 Mips64Label uncompressed_load, done;
2257 __ Bnezc(TMP, &uncompressed_load);
2258 __ Daddu(TMP, obj, index_reg);
2259 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2260 __ Bc(&done);
2261 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002262 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002263 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2264 __ Bind(&done);
2265 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002266 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002267 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002268 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002269 }
2270 break;
2271 }
2272
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002273 case DataType::Type::kInt16: {
2274 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2275 if (index.IsConstant()) {
2276 size_t offset =
2277 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2278 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2279 } else {
2280 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2281 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2282 }
2283 break;
2284 }
2285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002287 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002288 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 LoadOperandType load_type =
2290 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002291 if (index.IsConstant()) {
2292 size_t offset =
2293 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002294 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002295 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002296 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002297 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002298 }
2299 break;
2300 }
2301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002303 static_assert(
2304 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2305 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2306 // /* HeapReference<Object> */ out =
2307 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2308 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002309 bool temp_needed = index.IsConstant()
2310 ? !kBakerReadBarrierThunksEnableForFields
2311 : !kBakerReadBarrierThunksEnableForArrays;
2312 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002313 // Note that a potential implicit null check is handled in this
2314 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002315 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2316 if (index.IsConstant()) {
2317 // Array load with a constant index can be treated as a field load.
2318 size_t offset =
2319 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2320 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2321 out_loc,
2322 obj,
2323 offset,
2324 temp,
2325 /* needs_null_check */ false);
2326 } else {
2327 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2328 out_loc,
2329 obj,
2330 data_offset,
2331 index,
2332 temp,
2333 /* needs_null_check */ false);
2334 }
Alexey Frunze15958152017-02-09 19:08:30 -08002335 } else {
2336 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2337 if (index.IsConstant()) {
2338 size_t offset =
2339 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2340 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2341 // If read barriers are enabled, emit read barriers other than
2342 // Baker's using a slow path (and also unpoison the loaded
2343 // reference, if heap poisoning is enabled).
2344 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2345 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002346 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002347 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2348 // If read barriers are enabled, emit read barriers other than
2349 // Baker's using a slow path (and also unpoison the loaded
2350 // reference, if heap poisoning is enabled).
2351 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2352 out_loc,
2353 out_loc,
2354 obj_loc,
2355 data_offset,
2356 index);
2357 }
2358 }
2359 break;
2360 }
2361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002363 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002364 if (index.IsConstant()) {
2365 size_t offset =
2366 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002367 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002368 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002369 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002370 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002371 }
2372 break;
2373 }
2374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002376 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002377 if (index.IsConstant()) {
2378 size_t offset =
2379 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002380 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002381 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002382 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002383 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002384 }
2385 break;
2386 }
2387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002389 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002390 if (index.IsConstant()) {
2391 size_t offset =
2392 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002393 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002394 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002395 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002396 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002397 }
2398 break;
2399 }
2400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002401 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002402 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2403 UNREACHABLE();
2404 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002405}
2406
2407void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002408 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002409 locations->SetInAt(0, Location::RequiresRegister());
2410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2411}
2412
2413void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2414 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002415 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002416 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2417 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2418 __ LoadFromOffset(kLoadWord, out, obj, offset);
2419 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002420 // Mask out compression flag from String's array length.
2421 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2422 __ Srl(out, out, 1u);
2423 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002424}
2425
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002426Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2427 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2428 ? Location::ConstantLocation(instruction->AsConstant())
2429 : Location::RequiresRegister();
2430}
2431
2432Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2433 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2434 // We can store a non-zero float or double constant without first loading it into the FPU,
2435 // but we should only prefer this if the constant has a single use.
2436 if (instruction->IsConstant() &&
2437 (instruction->AsConstant()->IsZeroBitPattern() ||
2438 instruction->GetUses().HasExactlyOneElement())) {
2439 return Location::ConstantLocation(instruction->AsConstant());
2440 // Otherwise fall through and require an FPU register for the constant.
2441 }
2442 return Location::RequiresFpuRegister();
2443}
2444
Alexey Frunze4dda3372015-06-01 18:31:49 -07002445void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002447
2448 bool needs_write_barrier =
2449 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2450 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2451
Vladimir Markoca6fff82017-10-03 14:49:14 +01002452 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002453 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002454 may_need_runtime_call_for_type_check ?
2455 LocationSummary::kCallOnSlowPath :
2456 LocationSummary::kNoCall);
2457
2458 locations->SetInAt(0, Location::RequiresRegister());
2459 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002460 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002461 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002462 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002463 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2464 }
2465 if (needs_write_barrier) {
2466 // Temporary register for the write barrier.
2467 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 }
2469}
2470
2471void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2472 LocationSummary* locations = instruction->GetLocations();
2473 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2474 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002475 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002477 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002478 bool needs_write_barrier =
2479 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002480 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002481 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002482
2483 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002484 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002485 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002487 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002488 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002489 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002490 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002491 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2492 }
2493 if (value_location.IsConstant()) {
2494 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2495 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2496 } else {
2497 GpuRegister value = value_location.AsRegister<GpuRegister>();
2498 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002499 }
2500 break;
2501 }
2502
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002503 case DataType::Type::kUint16:
2504 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002505 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002506 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002507 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002508 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002509 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002510 }
2511 if (value_location.IsConstant()) {
2512 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2513 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2514 } else {
2515 GpuRegister value = value_location.AsRegister<GpuRegister>();
2516 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517 }
2518 break;
2519 }
2520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002522 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2523 if (index.IsConstant()) {
2524 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2525 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002526 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002527 }
2528 if (value_location.IsConstant()) {
2529 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2530 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2531 } else {
2532 GpuRegister value = value_location.AsRegister<GpuRegister>();
2533 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2534 }
2535 break;
2536 }
2537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002539 if (value_location.IsConstant()) {
2540 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002541 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002543 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002544 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002545 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002546 }
Alexey Frunze15958152017-02-09 19:08:30 -08002547 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2548 DCHECK_EQ(value, 0);
2549 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2550 DCHECK(!needs_write_barrier);
2551 DCHECK(!may_need_runtime_call_for_type_check);
2552 break;
2553 }
2554
2555 DCHECK(needs_write_barrier);
2556 GpuRegister value = value_location.AsRegister<GpuRegister>();
2557 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2558 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2559 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2560 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2561 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2562 Mips64Label done;
2563 SlowPathCodeMIPS64* slow_path = nullptr;
2564
2565 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002566 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002567 codegen_->AddSlowPath(slow_path);
2568 if (instruction->GetValueCanBeNull()) {
2569 Mips64Label non_zero;
2570 __ Bnezc(value, &non_zero);
2571 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2572 if (index.IsConstant()) {
2573 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002574 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002575 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002576 }
Alexey Frunze15958152017-02-09 19:08:30 -08002577 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2578 __ Bc(&done);
2579 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002580 }
Alexey Frunze15958152017-02-09 19:08:30 -08002581
2582 // Note that when read barriers are enabled, the type checks
2583 // are performed without read barriers. This is fine, even in
2584 // the case where a class object is in the from-space after
2585 // the flip, as a comparison involving such a type would not
2586 // produce a false positive; it may of course produce a false
2587 // negative, in which case we would take the ArraySet slow
2588 // path.
2589
2590 // /* HeapReference<Class> */ temp1 = obj->klass_
2591 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2592 __ MaybeUnpoisonHeapReference(temp1);
2593
2594 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2595 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2596 // /* HeapReference<Class> */ temp2 = value->klass_
2597 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2598 // If heap poisoning is enabled, no need to unpoison `temp1`
2599 // nor `temp2`, as we are comparing two poisoned references.
2600
2601 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2602 Mips64Label do_put;
2603 __ Beqc(temp1, temp2, &do_put);
2604 // If heap poisoning is enabled, the `temp1` reference has
2605 // not been unpoisoned yet; unpoison it now.
2606 __ MaybeUnpoisonHeapReference(temp1);
2607
2608 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2609 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2610 // If heap poisoning is enabled, no need to unpoison
2611 // `temp1`, as we are comparing against null below.
2612 __ Bnezc(temp1, slow_path->GetEntryLabel());
2613 __ Bind(&do_put);
2614 } else {
2615 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2616 }
2617 }
2618
2619 GpuRegister source = value;
2620 if (kPoisonHeapReferences) {
2621 // Note that in the case where `value` is a null reference,
2622 // we do not enter this block, as a null reference does not
2623 // need poisoning.
2624 __ Move(temp1, value);
2625 __ PoisonHeapReference(temp1);
2626 source = temp1;
2627 }
2628
2629 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2630 if (index.IsConstant()) {
2631 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002632 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002633 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002634 }
2635 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2636
2637 if (!may_need_runtime_call_for_type_check) {
2638 codegen_->MaybeRecordImplicitNullCheck(instruction);
2639 }
2640
2641 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2642
2643 if (done.IsLinked()) {
2644 __ Bind(&done);
2645 }
2646
2647 if (slow_path != nullptr) {
2648 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002649 }
2650 break;
2651 }
2652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002653 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002655 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002656 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002658 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002659 }
2660 if (value_location.IsConstant()) {
2661 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2662 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2663 } else {
2664 GpuRegister value = value_location.AsRegister<GpuRegister>();
2665 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002666 }
2667 break;
2668 }
2669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002671 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002672 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002673 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002675 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002676 }
2677 if (value_location.IsConstant()) {
2678 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2679 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2680 } else {
2681 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2682 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002683 }
2684 break;
2685 }
2686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002688 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002690 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002691 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002692 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002693 }
2694 if (value_location.IsConstant()) {
2695 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2696 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2697 } else {
2698 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2699 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002700 }
2701 break;
2702 }
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002705 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2706 UNREACHABLE();
2707 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002708}
2709
2710void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002711 RegisterSet caller_saves = RegisterSet::Empty();
2712 InvokeRuntimeCallingConvention calling_convention;
2713 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2714 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2715 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002716
2717 HInstruction* index = instruction->InputAt(0);
2718 HInstruction* length = instruction->InputAt(1);
2719
2720 bool const_index = false;
2721 bool const_length = false;
2722
2723 if (index->IsConstant()) {
2724 if (length->IsConstant()) {
2725 const_index = true;
2726 const_length = true;
2727 } else {
2728 int32_t index_value = index->AsIntConstant()->GetValue();
2729 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2730 const_index = true;
2731 }
2732 }
2733 } else if (length->IsConstant()) {
2734 int32_t length_value = length->AsIntConstant()->GetValue();
2735 if (IsUint<15>(length_value)) {
2736 const_length = true;
2737 }
2738 }
2739
2740 locations->SetInAt(0, const_index
2741 ? Location::ConstantLocation(index->AsConstant())
2742 : Location::RequiresRegister());
2743 locations->SetInAt(1, const_length
2744 ? Location::ConstantLocation(length->AsConstant())
2745 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746}
2747
2748void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2749 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002750 Location index_loc = locations->InAt(0);
2751 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002752
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002753 if (length_loc.IsConstant()) {
2754 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2755 if (index_loc.IsConstant()) {
2756 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2757 if (index < 0 || index >= length) {
2758 BoundsCheckSlowPathMIPS64* slow_path =
2759 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2760 codegen_->AddSlowPath(slow_path);
2761 __ Bc(slow_path->GetEntryLabel());
2762 } else {
2763 // Nothing to be done.
2764 }
2765 return;
2766 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002767
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002768 BoundsCheckSlowPathMIPS64* slow_path =
2769 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2770 codegen_->AddSlowPath(slow_path);
2771 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2772 if (length == 0) {
2773 __ Bc(slow_path->GetEntryLabel());
2774 } else if (length == 1) {
2775 __ Bnezc(index, slow_path->GetEntryLabel());
2776 } else {
2777 DCHECK(IsUint<15>(length)) << length;
2778 __ Sltiu(TMP, index, length);
2779 __ Beqzc(TMP, slow_path->GetEntryLabel());
2780 }
2781 } else {
2782 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2783 BoundsCheckSlowPathMIPS64* slow_path =
2784 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2785 codegen_->AddSlowPath(slow_path);
2786 if (index_loc.IsConstant()) {
2787 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2788 if (index < 0) {
2789 __ Bc(slow_path->GetEntryLabel());
2790 } else if (index == 0) {
2791 __ Blezc(length, slow_path->GetEntryLabel());
2792 } else {
2793 DCHECK(IsInt<16>(index + 1)) << index;
2794 __ Sltiu(TMP, length, index + 1);
2795 __ Bnezc(TMP, slow_path->GetEntryLabel());
2796 }
2797 } else {
2798 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2799 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2800 }
2801 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002802}
2803
Alexey Frunze15958152017-02-09 19:08:30 -08002804// Temp is used for read barrier.
2805static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2806 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002807 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002808 (kUseBakerReadBarrier ||
2809 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2810 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2811 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2812 return 1;
2813 }
2814 return 0;
2815}
2816
2817// Extra temp is used for read barrier.
2818static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2819 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2820}
2821
Alexey Frunze4dda3372015-06-01 18:31:49 -07002822void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002823 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2824 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2825
2826 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2827 switch (type_check_kind) {
2828 case TypeCheckKind::kExactCheck:
2829 case TypeCheckKind::kAbstractClassCheck:
2830 case TypeCheckKind::kClassHierarchyCheck:
2831 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002832 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002833 ? LocationSummary::kCallOnSlowPath
2834 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2835 break;
2836 case TypeCheckKind::kArrayCheck:
2837 case TypeCheckKind::kUnresolvedCheck:
2838 case TypeCheckKind::kInterfaceCheck:
2839 call_kind = LocationSummary::kCallOnSlowPath;
2840 break;
2841 }
2842
Vladimir Markoca6fff82017-10-03 14:49:14 +01002843 LocationSummary* locations =
2844 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845 locations->SetInAt(0, Location::RequiresRegister());
2846 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002847 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002848}
2849
2850void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002851 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002852 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002853 Location obj_loc = locations->InAt(0);
2854 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002855 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002856 Location temp_loc = locations->GetTemp(0);
2857 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2858 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2859 DCHECK_LE(num_temps, 2u);
2860 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002861 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2862 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2863 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2864 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2865 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2866 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2867 const uint32_t object_array_data_offset =
2868 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2869 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002870
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002871 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2872 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2873 // read barriers is done for performance and code size reasons.
2874 bool is_type_check_slow_path_fatal = false;
2875 if (!kEmitCompilerReadBarrier) {
2876 is_type_check_slow_path_fatal =
2877 (type_check_kind == TypeCheckKind::kExactCheck ||
2878 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2879 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2880 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2881 !instruction->CanThrowIntoCatchBlock();
2882 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002883 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002884 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2885 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002886 codegen_->AddSlowPath(slow_path);
2887
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002888 // Avoid this check if we know `obj` is not null.
2889 if (instruction->MustDoNullCheck()) {
2890 __ Beqzc(obj, &done);
2891 }
2892
2893 switch (type_check_kind) {
2894 case TypeCheckKind::kExactCheck:
2895 case TypeCheckKind::kArrayCheck: {
2896 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002897 GenerateReferenceLoadTwoRegisters(instruction,
2898 temp_loc,
2899 obj_loc,
2900 class_offset,
2901 maybe_temp2_loc,
2902 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002903 // Jump to slow path for throwing the exception or doing a
2904 // more involved array check.
2905 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2906 break;
2907 }
2908
2909 case TypeCheckKind::kAbstractClassCheck: {
2910 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002911 GenerateReferenceLoadTwoRegisters(instruction,
2912 temp_loc,
2913 obj_loc,
2914 class_offset,
2915 maybe_temp2_loc,
2916 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002917 // If the class is abstract, we eagerly fetch the super class of the
2918 // object to avoid doing a comparison we know will fail.
2919 Mips64Label loop;
2920 __ Bind(&loop);
2921 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002922 GenerateReferenceLoadOneRegister(instruction,
2923 temp_loc,
2924 super_offset,
2925 maybe_temp2_loc,
2926 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002927 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2928 // exception.
2929 __ Beqzc(temp, slow_path->GetEntryLabel());
2930 // Otherwise, compare the classes.
2931 __ Bnec(temp, cls, &loop);
2932 break;
2933 }
2934
2935 case TypeCheckKind::kClassHierarchyCheck: {
2936 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002937 GenerateReferenceLoadTwoRegisters(instruction,
2938 temp_loc,
2939 obj_loc,
2940 class_offset,
2941 maybe_temp2_loc,
2942 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002943 // Walk over the class hierarchy to find a match.
2944 Mips64Label loop;
2945 __ Bind(&loop);
2946 __ Beqc(temp, cls, &done);
2947 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002948 GenerateReferenceLoadOneRegister(instruction,
2949 temp_loc,
2950 super_offset,
2951 maybe_temp2_loc,
2952 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002953 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2954 // exception. Otherwise, jump to the beginning of the loop.
2955 __ Bnezc(temp, &loop);
2956 __ Bc(slow_path->GetEntryLabel());
2957 break;
2958 }
2959
2960 case TypeCheckKind::kArrayObjectCheck: {
2961 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002962 GenerateReferenceLoadTwoRegisters(instruction,
2963 temp_loc,
2964 obj_loc,
2965 class_offset,
2966 maybe_temp2_loc,
2967 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002968 // Do an exact check.
2969 __ Beqc(temp, cls, &done);
2970 // Otherwise, we need to check that the object's class is a non-primitive array.
2971 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002972 GenerateReferenceLoadOneRegister(instruction,
2973 temp_loc,
2974 component_offset,
2975 maybe_temp2_loc,
2976 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002977 // If the component type is null, jump to the slow path to throw the exception.
2978 __ Beqzc(temp, slow_path->GetEntryLabel());
2979 // Otherwise, the object is indeed an array, further check that this component
2980 // type is not a primitive type.
2981 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2982 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2983 __ Bnezc(temp, slow_path->GetEntryLabel());
2984 break;
2985 }
2986
2987 case TypeCheckKind::kUnresolvedCheck:
2988 // We always go into the type check slow path for the unresolved check case.
2989 // We cannot directly call the CheckCast runtime entry point
2990 // without resorting to a type checking slow path here (i.e. by
2991 // calling InvokeRuntime directly), as it would require to
2992 // assign fixed registers for the inputs of this HInstanceOf
2993 // instruction (following the runtime calling convention), which
2994 // might be cluttered by the potential first read barrier
2995 // emission at the beginning of this method.
2996 __ Bc(slow_path->GetEntryLabel());
2997 break;
2998
2999 case TypeCheckKind::kInterfaceCheck: {
3000 // Avoid read barriers to improve performance of the fast path. We can not get false
3001 // positives by doing this.
3002 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003003 GenerateReferenceLoadTwoRegisters(instruction,
3004 temp_loc,
3005 obj_loc,
3006 class_offset,
3007 maybe_temp2_loc,
3008 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003009 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003010 GenerateReferenceLoadTwoRegisters(instruction,
3011 temp_loc,
3012 temp_loc,
3013 iftable_offset,
3014 maybe_temp2_loc,
3015 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003016 // Iftable is never null.
3017 __ Lw(TMP, temp, array_length_offset);
3018 // Loop through the iftable and check if any class matches.
3019 Mips64Label loop;
3020 __ Bind(&loop);
3021 __ Beqzc(TMP, slow_path->GetEntryLabel());
3022 __ Lwu(AT, temp, object_array_data_offset);
3023 __ MaybeUnpoisonHeapReference(AT);
3024 // Go to next interface.
3025 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
3026 __ Addiu(TMP, TMP, -2);
3027 // Compare the classes and continue the loop if they do not match.
3028 __ Bnec(AT, cls, &loop);
3029 break;
3030 }
3031 }
3032
3033 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003034 __ Bind(slow_path->GetExitLabel());
3035}
3036
3037void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3038 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003039 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003040 locations->SetInAt(0, Location::RequiresRegister());
3041 if (check->HasUses()) {
3042 locations->SetOut(Location::SameAsFirstInput());
3043 }
3044}
3045
3046void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3047 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003048 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003049 check->GetLoadClass(),
3050 check,
3051 check->GetDexPc(),
3052 true);
3053 codegen_->AddSlowPath(slow_path);
3054 GenerateClassInitializationCheck(slow_path,
3055 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3056}
3057
3058void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003059 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003060
Vladimir Markoca6fff82017-10-03 14:49:14 +01003061 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003062
3063 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003064 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003065 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003067 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003068 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003069 case DataType::Type::kInt32:
3070 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003072 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3074 break;
3075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003076 case DataType::Type::kFloat32:
3077 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003078 locations->SetInAt(0, Location::RequiresFpuRegister());
3079 locations->SetInAt(1, Location::RequiresFpuRegister());
3080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003081 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003082
3083 default:
3084 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3085 }
3086}
3087
3088void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3089 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003090 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003092
3093 // 0 if: left == right
3094 // 1 if: left > right
3095 // -1 if: left < right
3096 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003097 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003098 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003099 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003100 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003101 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003102 case DataType::Type::kInt32:
3103 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003104 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003105 Location rhs_location = locations->InAt(1);
3106 bool use_imm = rhs_location.IsConstant();
3107 GpuRegister rhs = ZERO;
3108 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003109 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003110 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3111 if (value != 0) {
3112 rhs = AT;
3113 __ LoadConst64(rhs, value);
3114 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003115 } else {
3116 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3117 if (value != 0) {
3118 rhs = AT;
3119 __ LoadConst32(rhs, value);
3120 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003121 }
3122 } else {
3123 rhs = rhs_location.AsRegister<GpuRegister>();
3124 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003125 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003126 __ Slt(res, rhs, lhs);
3127 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128 break;
3129 }
3130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003131 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003132 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3133 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3134 Mips64Label done;
3135 __ CmpEqS(FTMP, lhs, rhs);
3136 __ LoadConst32(res, 0);
3137 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003138 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003139 __ CmpLtS(FTMP, lhs, rhs);
3140 __ LoadConst32(res, -1);
3141 __ Bc1nez(FTMP, &done);
3142 __ LoadConst32(res, 1);
3143 } else {
3144 __ CmpLtS(FTMP, rhs, lhs);
3145 __ LoadConst32(res, 1);
3146 __ Bc1nez(FTMP, &done);
3147 __ LoadConst32(res, -1);
3148 }
3149 __ Bind(&done);
3150 break;
3151 }
3152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003154 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3155 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3156 Mips64Label done;
3157 __ CmpEqD(FTMP, lhs, rhs);
3158 __ LoadConst32(res, 0);
3159 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003160 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003161 __ CmpLtD(FTMP, lhs, rhs);
3162 __ LoadConst32(res, -1);
3163 __ Bc1nez(FTMP, &done);
3164 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003165 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003166 __ CmpLtD(FTMP, rhs, lhs);
3167 __ LoadConst32(res, 1);
3168 __ Bc1nez(FTMP, &done);
3169 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003170 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003171 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003172 break;
3173 }
3174
3175 default:
3176 LOG(FATAL) << "Unimplemented compare type " << in_type;
3177 }
3178}
3179
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003180void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003181 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003182 switch (instruction->InputAt(0)->GetType()) {
3183 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003184 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003185 locations->SetInAt(0, Location::RequiresRegister());
3186 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3187 break;
3188
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003189 case DataType::Type::kFloat32:
3190 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003191 locations->SetInAt(0, Location::RequiresFpuRegister());
3192 locations->SetInAt(1, Location::RequiresFpuRegister());
3193 break;
3194 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003195 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3197 }
3198}
3199
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003200void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003201 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003202 return;
3203 }
3204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003206 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003207 switch (type) {
3208 default:
3209 // Integer case.
3210 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3211 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003212 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003213 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3214 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 case DataType::Type::kFloat32:
3216 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003217 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3218 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003219 }
3220}
3221
Alexey Frunzec857c742015-09-23 15:12:39 -07003222void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3223 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003224 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003225
3226 LocationSummary* locations = instruction->GetLocations();
3227 Location second = locations->InAt(1);
3228 DCHECK(second.IsConstant());
3229
3230 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3231 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3232 int64_t imm = Int64FromConstant(second.GetConstant());
3233 DCHECK(imm == 1 || imm == -1);
3234
3235 if (instruction->IsRem()) {
3236 __ Move(out, ZERO);
3237 } else {
3238 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003240 __ Subu(out, ZERO, dividend);
3241 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003242 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003243 __ Dsubu(out, ZERO, dividend);
3244 }
3245 } else if (out != dividend) {
3246 __ Move(out, dividend);
3247 }
3248 }
3249}
3250
3251void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3252 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003254
3255 LocationSummary* locations = instruction->GetLocations();
3256 Location second = locations->InAt(1);
3257 DCHECK(second.IsConstant());
3258
3259 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3260 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3261 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003262 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003263 int ctz_imm = CTZ(abs_imm);
3264
3265 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003266 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003267 if (ctz_imm == 1) {
3268 // Fast path for division by +/-2, which is very common.
3269 __ Srl(TMP, dividend, 31);
3270 } else {
3271 __ Sra(TMP, dividend, 31);
3272 __ Srl(TMP, TMP, 32 - ctz_imm);
3273 }
3274 __ Addu(out, dividend, TMP);
3275 __ Sra(out, out, ctz_imm);
3276 if (imm < 0) {
3277 __ Subu(out, ZERO, out);
3278 }
3279 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003281 if (ctz_imm == 1) {
3282 // Fast path for division by +/-2, which is very common.
3283 __ Dsrl32(TMP, dividend, 31);
3284 } else {
3285 __ Dsra32(TMP, dividend, 31);
3286 if (ctz_imm > 32) {
3287 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3288 } else {
3289 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3290 }
3291 }
3292 __ Daddu(out, dividend, TMP);
3293 if (ctz_imm < 32) {
3294 __ Dsra(out, out, ctz_imm);
3295 } else {
3296 __ Dsra32(out, out, ctz_imm - 32);
3297 }
3298 if (imm < 0) {
3299 __ Dsubu(out, ZERO, out);
3300 }
3301 }
3302 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003303 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003304 if (ctz_imm == 1) {
3305 // Fast path for modulo +/-2, which is very common.
3306 __ Sra(TMP, dividend, 31);
3307 __ Subu(out, dividend, TMP);
3308 __ Andi(out, out, 1);
3309 __ Addu(out, out, TMP);
3310 } else {
3311 __ Sra(TMP, dividend, 31);
3312 __ Srl(TMP, TMP, 32 - ctz_imm);
3313 __ Addu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003314 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003315 __ Subu(out, out, TMP);
3316 }
3317 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003318 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003319 if (ctz_imm == 1) {
3320 // Fast path for modulo +/-2, which is very common.
3321 __ Dsra32(TMP, dividend, 31);
3322 __ Dsubu(out, dividend, TMP);
3323 __ Andi(out, out, 1);
3324 __ Daddu(out, out, TMP);
3325 } else {
3326 __ Dsra32(TMP, dividend, 31);
3327 if (ctz_imm > 32) {
3328 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3329 } else {
3330 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3331 }
3332 __ Daddu(out, dividend, TMP);
Lena Djokica556e6b2017-12-13 12:09:42 +01003333 __ DblIns(out, ZERO, ctz_imm, 64 - ctz_imm);
Alexey Frunzec857c742015-09-23 15:12:39 -07003334 __ Dsubu(out, out, TMP);
3335 }
3336 }
3337 }
3338}
3339
3340void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3341 DCHECK(instruction->IsDiv() || instruction->IsRem());
3342
3343 LocationSummary* locations = instruction->GetLocations();
3344 Location second = locations->InAt(1);
3345 DCHECK(second.IsConstant());
3346
3347 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3348 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3349 int64_t imm = Int64FromConstant(second.GetConstant());
3350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003351 DataType::Type type = instruction->GetResultType();
3352 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003353
3354 int64_t magic;
3355 int shift;
3356 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003357 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003358 &magic,
3359 &shift);
3360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003361 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003362 __ LoadConst32(TMP, magic);
3363 __ MuhR6(TMP, dividend, TMP);
3364
3365 if (imm > 0 && magic < 0) {
3366 __ Addu(TMP, TMP, dividend);
3367 } else if (imm < 0 && magic > 0) {
3368 __ Subu(TMP, TMP, dividend);
3369 }
3370
3371 if (shift != 0) {
3372 __ Sra(TMP, TMP, shift);
3373 }
3374
3375 if (instruction->IsDiv()) {
3376 __ Sra(out, TMP, 31);
3377 __ Subu(out, TMP, out);
3378 } else {
3379 __ Sra(AT, TMP, 31);
3380 __ Subu(AT, TMP, AT);
3381 __ LoadConst32(TMP, imm);
3382 __ MulR6(TMP, AT, TMP);
3383 __ Subu(out, dividend, TMP);
3384 }
3385 } else {
3386 __ LoadConst64(TMP, magic);
3387 __ Dmuh(TMP, dividend, TMP);
3388
3389 if (imm > 0 && magic < 0) {
3390 __ Daddu(TMP, TMP, dividend);
3391 } else if (imm < 0 && magic > 0) {
3392 __ Dsubu(TMP, TMP, dividend);
3393 }
3394
3395 if (shift >= 32) {
3396 __ Dsra32(TMP, TMP, shift - 32);
3397 } else if (shift > 0) {
3398 __ Dsra(TMP, TMP, shift);
3399 }
3400
3401 if (instruction->IsDiv()) {
3402 __ Dsra32(out, TMP, 31);
3403 __ Dsubu(out, TMP, out);
3404 } else {
3405 __ Dsra32(AT, TMP, 31);
3406 __ Dsubu(AT, TMP, AT);
3407 __ LoadConst64(TMP, imm);
3408 __ Dmul(TMP, AT, TMP);
3409 __ Dsubu(out, dividend, TMP);
3410 }
3411 }
3412}
3413
3414void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3415 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003416 DataType::Type type = instruction->GetResultType();
3417 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003418
3419 LocationSummary* locations = instruction->GetLocations();
3420 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3421 Location second = locations->InAt(1);
3422
3423 if (second.IsConstant()) {
3424 int64_t imm = Int64FromConstant(second.GetConstant());
3425 if (imm == 0) {
3426 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3427 } else if (imm == 1 || imm == -1) {
3428 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003429 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003430 DivRemByPowerOfTwo(instruction);
3431 } else {
3432 DCHECK(imm <= -2 || imm >= 2);
3433 GenerateDivRemWithAnyConstant(instruction);
3434 }
3435 } else {
3436 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3437 GpuRegister divisor = second.AsRegister<GpuRegister>();
3438 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003439 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003440 __ DivR6(out, dividend, divisor);
3441 else
3442 __ Ddiv(out, dividend, divisor);
3443 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003444 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003445 __ ModR6(out, dividend, divisor);
3446 else
3447 __ Dmod(out, dividend, divisor);
3448 }
3449 }
3450}
3451
Alexey Frunze4dda3372015-06-01 18:31:49 -07003452void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3453 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003454 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003455 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003456 case DataType::Type::kInt32:
3457 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003458 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003459 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3461 break;
3462
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003463 case DataType::Type::kFloat32:
3464 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003465 locations->SetInAt(0, Location::RequiresFpuRegister());
3466 locations->SetInAt(1, Location::RequiresFpuRegister());
3467 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3468 break;
3469
3470 default:
3471 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3472 }
3473}
3474
3475void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003477 LocationSummary* locations = instruction->GetLocations();
3478
3479 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003480 case DataType::Type::kInt32:
3481 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003482 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003483 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003484 case DataType::Type::kFloat32:
3485 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003486 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3487 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3488 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003489 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003490 __ DivS(dst, lhs, rhs);
3491 else
3492 __ DivD(dst, lhs, rhs);
3493 break;
3494 }
3495 default:
3496 LOG(FATAL) << "Unexpected div type " << type;
3497 }
3498}
3499
3500void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003501 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003502 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003503}
3504
3505void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3506 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003507 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003508 codegen_->AddSlowPath(slow_path);
3509 Location value = instruction->GetLocations()->InAt(0);
3510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003511 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003512
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003513 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003514 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003515 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003516 }
3517
3518 if (value.IsConstant()) {
3519 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3520 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003521 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003522 } else {
3523 // A division by a non-null constant is valid. We don't need to perform
3524 // any check, so simply fall through.
3525 }
3526 } else {
3527 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3528 }
3529}
3530
3531void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3532 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003533 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003534 locations->SetOut(Location::ConstantLocation(constant));
3535}
3536
3537void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3538 // Will be generated at use site.
3539}
3540
3541void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3542 exit->SetLocations(nullptr);
3543}
3544
3545void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3546}
3547
3548void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3549 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003550 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003551 locations->SetOut(Location::ConstantLocation(constant));
3552}
3553
3554void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3555 // Will be generated at use site.
3556}
3557
David Brazdilfc6a86a2015-06-26 10:33:45 +00003558void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003559 DCHECK(!successor->IsExitBlock());
3560 HBasicBlock* block = got->GetBlock();
3561 HInstruction* previous = got->GetPrevious();
3562 HLoopInformation* info = block->GetLoopInformation();
3563
3564 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003565 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3566 return;
3567 }
3568 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3569 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3570 }
3571 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003572 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003573 }
3574}
3575
David Brazdilfc6a86a2015-06-26 10:33:45 +00003576void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3577 got->SetLocations(nullptr);
3578}
3579
3580void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3581 HandleGoto(got, got->GetSuccessor());
3582}
3583
3584void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3585 try_boundary->SetLocations(nullptr);
3586}
3587
3588void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3589 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3590 if (!successor->IsExitBlock()) {
3591 HandleGoto(try_boundary, successor);
3592 }
3593}
3594
Alexey Frunze299a9392015-12-08 16:08:02 -08003595void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3596 bool is64bit,
3597 LocationSummary* locations) {
3598 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3599 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3600 Location rhs_location = locations->InAt(1);
3601 GpuRegister rhs_reg = ZERO;
3602 int64_t rhs_imm = 0;
3603 bool use_imm = rhs_location.IsConstant();
3604 if (use_imm) {
3605 if (is64bit) {
3606 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3607 } else {
3608 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3609 }
3610 } else {
3611 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3612 }
3613 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3614
3615 switch (cond) {
3616 case kCondEQ:
3617 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003618 if (use_imm && IsInt<16>(-rhs_imm)) {
3619 if (rhs_imm == 0) {
3620 if (cond == kCondEQ) {
3621 __ Sltiu(dst, lhs, 1);
3622 } else {
3623 __ Sltu(dst, ZERO, lhs);
3624 }
3625 } else {
3626 if (is64bit) {
3627 __ Daddiu(dst, lhs, -rhs_imm);
3628 } else {
3629 __ Addiu(dst, lhs, -rhs_imm);
3630 }
3631 if (cond == kCondEQ) {
3632 __ Sltiu(dst, dst, 1);
3633 } else {
3634 __ Sltu(dst, ZERO, dst);
3635 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003636 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003637 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003638 if (use_imm && IsUint<16>(rhs_imm)) {
3639 __ Xori(dst, lhs, rhs_imm);
3640 } else {
3641 if (use_imm) {
3642 rhs_reg = TMP;
3643 __ LoadConst64(rhs_reg, rhs_imm);
3644 }
3645 __ Xor(dst, lhs, rhs_reg);
3646 }
3647 if (cond == kCondEQ) {
3648 __ Sltiu(dst, dst, 1);
3649 } else {
3650 __ Sltu(dst, ZERO, dst);
3651 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003652 }
3653 break;
3654
3655 case kCondLT:
3656 case kCondGE:
3657 if (use_imm && IsInt<16>(rhs_imm)) {
3658 __ Slti(dst, lhs, rhs_imm);
3659 } else {
3660 if (use_imm) {
3661 rhs_reg = TMP;
3662 __ LoadConst64(rhs_reg, rhs_imm);
3663 }
3664 __ Slt(dst, lhs, rhs_reg);
3665 }
3666 if (cond == kCondGE) {
3667 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3668 // only the slt instruction but no sge.
3669 __ Xori(dst, dst, 1);
3670 }
3671 break;
3672
3673 case kCondLE:
3674 case kCondGT:
3675 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3676 // Simulate lhs <= rhs via lhs < rhs + 1.
3677 __ Slti(dst, lhs, rhs_imm_plus_one);
3678 if (cond == kCondGT) {
3679 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3680 // only the slti instruction but no sgti.
3681 __ Xori(dst, dst, 1);
3682 }
3683 } else {
3684 if (use_imm) {
3685 rhs_reg = TMP;
3686 __ LoadConst64(rhs_reg, rhs_imm);
3687 }
3688 __ Slt(dst, rhs_reg, lhs);
3689 if (cond == kCondLE) {
3690 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3691 // only the slt instruction but no sle.
3692 __ Xori(dst, dst, 1);
3693 }
3694 }
3695 break;
3696
3697 case kCondB:
3698 case kCondAE:
3699 if (use_imm && IsInt<16>(rhs_imm)) {
3700 // Sltiu sign-extends its 16-bit immediate operand before
3701 // the comparison and thus lets us compare directly with
3702 // unsigned values in the ranges [0, 0x7fff] and
3703 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3704 __ Sltiu(dst, lhs, rhs_imm);
3705 } else {
3706 if (use_imm) {
3707 rhs_reg = TMP;
3708 __ LoadConst64(rhs_reg, rhs_imm);
3709 }
3710 __ Sltu(dst, lhs, rhs_reg);
3711 }
3712 if (cond == kCondAE) {
3713 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3714 // only the sltu instruction but no sgeu.
3715 __ Xori(dst, dst, 1);
3716 }
3717 break;
3718
3719 case kCondBE:
3720 case kCondA:
3721 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3722 // Simulate lhs <= rhs via lhs < rhs + 1.
3723 // Note that this only works if rhs + 1 does not overflow
3724 // to 0, hence the check above.
3725 // Sltiu sign-extends its 16-bit immediate operand before
3726 // the comparison and thus lets us compare directly with
3727 // unsigned values in the ranges [0, 0x7fff] and
3728 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3729 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3730 if (cond == kCondA) {
3731 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3732 // only the sltiu instruction but no sgtiu.
3733 __ Xori(dst, dst, 1);
3734 }
3735 } else {
3736 if (use_imm) {
3737 rhs_reg = TMP;
3738 __ LoadConst64(rhs_reg, rhs_imm);
3739 }
3740 __ Sltu(dst, rhs_reg, lhs);
3741 if (cond == kCondBE) {
3742 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3743 // only the sltu instruction but no sleu.
3744 __ Xori(dst, dst, 1);
3745 }
3746 }
3747 break;
3748 }
3749}
3750
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003751bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3752 bool is64bit,
3753 LocationSummary* input_locations,
3754 GpuRegister dst) {
3755 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3756 Location rhs_location = input_locations->InAt(1);
3757 GpuRegister rhs_reg = ZERO;
3758 int64_t rhs_imm = 0;
3759 bool use_imm = rhs_location.IsConstant();
3760 if (use_imm) {
3761 if (is64bit) {
3762 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3763 } else {
3764 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3765 }
3766 } else {
3767 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3768 }
3769 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3770
3771 switch (cond) {
3772 case kCondEQ:
3773 case kCondNE:
3774 if (use_imm && IsInt<16>(-rhs_imm)) {
3775 if (is64bit) {
3776 __ Daddiu(dst, lhs, -rhs_imm);
3777 } else {
3778 __ Addiu(dst, lhs, -rhs_imm);
3779 }
3780 } else if (use_imm && IsUint<16>(rhs_imm)) {
3781 __ Xori(dst, lhs, rhs_imm);
3782 } else {
3783 if (use_imm) {
3784 rhs_reg = TMP;
3785 __ LoadConst64(rhs_reg, rhs_imm);
3786 }
3787 __ Xor(dst, lhs, rhs_reg);
3788 }
3789 return (cond == kCondEQ);
3790
3791 case kCondLT:
3792 case kCondGE:
3793 if (use_imm && IsInt<16>(rhs_imm)) {
3794 __ Slti(dst, lhs, rhs_imm);
3795 } else {
3796 if (use_imm) {
3797 rhs_reg = TMP;
3798 __ LoadConst64(rhs_reg, rhs_imm);
3799 }
3800 __ Slt(dst, lhs, rhs_reg);
3801 }
3802 return (cond == kCondGE);
3803
3804 case kCondLE:
3805 case kCondGT:
3806 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3807 // Simulate lhs <= rhs via lhs < rhs + 1.
3808 __ Slti(dst, lhs, rhs_imm_plus_one);
3809 return (cond == kCondGT);
3810 } else {
3811 if (use_imm) {
3812 rhs_reg = TMP;
3813 __ LoadConst64(rhs_reg, rhs_imm);
3814 }
3815 __ Slt(dst, rhs_reg, lhs);
3816 return (cond == kCondLE);
3817 }
3818
3819 case kCondB:
3820 case kCondAE:
3821 if (use_imm && IsInt<16>(rhs_imm)) {
3822 // Sltiu sign-extends its 16-bit immediate operand before
3823 // the comparison and thus lets us compare directly with
3824 // unsigned values in the ranges [0, 0x7fff] and
3825 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3826 __ Sltiu(dst, lhs, rhs_imm);
3827 } else {
3828 if (use_imm) {
3829 rhs_reg = TMP;
3830 __ LoadConst64(rhs_reg, rhs_imm);
3831 }
3832 __ Sltu(dst, lhs, rhs_reg);
3833 }
3834 return (cond == kCondAE);
3835
3836 case kCondBE:
3837 case kCondA:
3838 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3839 // Simulate lhs <= rhs via lhs < rhs + 1.
3840 // Note that this only works if rhs + 1 does not overflow
3841 // to 0, hence the check above.
3842 // Sltiu sign-extends its 16-bit immediate operand before
3843 // the comparison and thus lets us compare directly with
3844 // unsigned values in the ranges [0, 0x7fff] and
3845 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3846 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3847 return (cond == kCondA);
3848 } else {
3849 if (use_imm) {
3850 rhs_reg = TMP;
3851 __ LoadConst64(rhs_reg, rhs_imm);
3852 }
3853 __ Sltu(dst, rhs_reg, lhs);
3854 return (cond == kCondBE);
3855 }
3856 }
3857}
3858
Alexey Frunze299a9392015-12-08 16:08:02 -08003859void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3860 bool is64bit,
3861 LocationSummary* locations,
3862 Mips64Label* label) {
3863 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3864 Location rhs_location = locations->InAt(1);
3865 GpuRegister rhs_reg = ZERO;
3866 int64_t rhs_imm = 0;
3867 bool use_imm = rhs_location.IsConstant();
3868 if (use_imm) {
3869 if (is64bit) {
3870 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3871 } else {
3872 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3873 }
3874 } else {
3875 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3876 }
3877
3878 if (use_imm && rhs_imm == 0) {
3879 switch (cond) {
3880 case kCondEQ:
3881 case kCondBE: // <= 0 if zero
3882 __ Beqzc(lhs, label);
3883 break;
3884 case kCondNE:
3885 case kCondA: // > 0 if non-zero
3886 __ Bnezc(lhs, label);
3887 break;
3888 case kCondLT:
3889 __ Bltzc(lhs, label);
3890 break;
3891 case kCondGE:
3892 __ Bgezc(lhs, label);
3893 break;
3894 case kCondLE:
3895 __ Blezc(lhs, label);
3896 break;
3897 case kCondGT:
3898 __ Bgtzc(lhs, label);
3899 break;
3900 case kCondB: // always false
3901 break;
3902 case kCondAE: // always true
3903 __ Bc(label);
3904 break;
3905 }
3906 } else {
3907 if (use_imm) {
3908 rhs_reg = TMP;
3909 __ LoadConst64(rhs_reg, rhs_imm);
3910 }
3911 switch (cond) {
3912 case kCondEQ:
3913 __ Beqc(lhs, rhs_reg, label);
3914 break;
3915 case kCondNE:
3916 __ Bnec(lhs, rhs_reg, label);
3917 break;
3918 case kCondLT:
3919 __ Bltc(lhs, rhs_reg, label);
3920 break;
3921 case kCondGE:
3922 __ Bgec(lhs, rhs_reg, label);
3923 break;
3924 case kCondLE:
3925 __ Bgec(rhs_reg, lhs, label);
3926 break;
3927 case kCondGT:
3928 __ Bltc(rhs_reg, lhs, label);
3929 break;
3930 case kCondB:
3931 __ Bltuc(lhs, rhs_reg, label);
3932 break;
3933 case kCondAE:
3934 __ Bgeuc(lhs, rhs_reg, label);
3935 break;
3936 case kCondBE:
3937 __ Bgeuc(rhs_reg, lhs, label);
3938 break;
3939 case kCondA:
3940 __ Bltuc(rhs_reg, lhs, label);
3941 break;
3942 }
3943 }
3944}
3945
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003946void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3947 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003948 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003949 LocationSummary* locations) {
3950 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3951 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3952 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003954 switch (cond) {
3955 case kCondEQ:
3956 __ CmpEqS(FTMP, lhs, rhs);
3957 __ Mfc1(dst, FTMP);
3958 __ Andi(dst, dst, 1);
3959 break;
3960 case kCondNE:
3961 __ CmpEqS(FTMP, lhs, rhs);
3962 __ Mfc1(dst, FTMP);
3963 __ Addiu(dst, dst, 1);
3964 break;
3965 case kCondLT:
3966 if (gt_bias) {
3967 __ CmpLtS(FTMP, lhs, rhs);
3968 } else {
3969 __ CmpUltS(FTMP, lhs, rhs);
3970 }
3971 __ Mfc1(dst, FTMP);
3972 __ Andi(dst, dst, 1);
3973 break;
3974 case kCondLE:
3975 if (gt_bias) {
3976 __ CmpLeS(FTMP, lhs, rhs);
3977 } else {
3978 __ CmpUleS(FTMP, lhs, rhs);
3979 }
3980 __ Mfc1(dst, FTMP);
3981 __ Andi(dst, dst, 1);
3982 break;
3983 case kCondGT:
3984 if (gt_bias) {
3985 __ CmpUltS(FTMP, rhs, lhs);
3986 } else {
3987 __ CmpLtS(FTMP, rhs, lhs);
3988 }
3989 __ Mfc1(dst, FTMP);
3990 __ Andi(dst, dst, 1);
3991 break;
3992 case kCondGE:
3993 if (gt_bias) {
3994 __ CmpUleS(FTMP, rhs, lhs);
3995 } else {
3996 __ CmpLeS(FTMP, rhs, lhs);
3997 }
3998 __ Mfc1(dst, FTMP);
3999 __ Andi(dst, dst, 1);
4000 break;
4001 default:
4002 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4003 UNREACHABLE();
4004 }
4005 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004006 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01004007 switch (cond) {
4008 case kCondEQ:
4009 __ CmpEqD(FTMP, lhs, rhs);
4010 __ Mfc1(dst, FTMP);
4011 __ Andi(dst, dst, 1);
4012 break;
4013 case kCondNE:
4014 __ CmpEqD(FTMP, lhs, rhs);
4015 __ Mfc1(dst, FTMP);
4016 __ Addiu(dst, dst, 1);
4017 break;
4018 case kCondLT:
4019 if (gt_bias) {
4020 __ CmpLtD(FTMP, lhs, rhs);
4021 } else {
4022 __ CmpUltD(FTMP, lhs, rhs);
4023 }
4024 __ Mfc1(dst, FTMP);
4025 __ Andi(dst, dst, 1);
4026 break;
4027 case kCondLE:
4028 if (gt_bias) {
4029 __ CmpLeD(FTMP, lhs, rhs);
4030 } else {
4031 __ CmpUleD(FTMP, lhs, rhs);
4032 }
4033 __ Mfc1(dst, FTMP);
4034 __ Andi(dst, dst, 1);
4035 break;
4036 case kCondGT:
4037 if (gt_bias) {
4038 __ CmpUltD(FTMP, rhs, lhs);
4039 } else {
4040 __ CmpLtD(FTMP, rhs, lhs);
4041 }
4042 __ Mfc1(dst, FTMP);
4043 __ Andi(dst, dst, 1);
4044 break;
4045 case kCondGE:
4046 if (gt_bias) {
4047 __ CmpUleD(FTMP, rhs, lhs);
4048 } else {
4049 __ CmpLeD(FTMP, rhs, lhs);
4050 }
4051 __ Mfc1(dst, FTMP);
4052 __ Andi(dst, dst, 1);
4053 break;
4054 default:
4055 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4056 UNREACHABLE();
4057 }
4058 }
4059}
4060
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004061bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4062 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004064 LocationSummary* input_locations,
4065 FpuRegister dst) {
4066 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4067 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004069 switch (cond) {
4070 case kCondEQ:
4071 __ CmpEqS(dst, lhs, rhs);
4072 return false;
4073 case kCondNE:
4074 __ CmpEqS(dst, lhs, rhs);
4075 return true;
4076 case kCondLT:
4077 if (gt_bias) {
4078 __ CmpLtS(dst, lhs, rhs);
4079 } else {
4080 __ CmpUltS(dst, lhs, rhs);
4081 }
4082 return false;
4083 case kCondLE:
4084 if (gt_bias) {
4085 __ CmpLeS(dst, lhs, rhs);
4086 } else {
4087 __ CmpUleS(dst, lhs, rhs);
4088 }
4089 return false;
4090 case kCondGT:
4091 if (gt_bias) {
4092 __ CmpUltS(dst, rhs, lhs);
4093 } else {
4094 __ CmpLtS(dst, rhs, lhs);
4095 }
4096 return false;
4097 case kCondGE:
4098 if (gt_bias) {
4099 __ CmpUleS(dst, rhs, lhs);
4100 } else {
4101 __ CmpLeS(dst, rhs, lhs);
4102 }
4103 return false;
4104 default:
4105 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4106 UNREACHABLE();
4107 }
4108 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004110 switch (cond) {
4111 case kCondEQ:
4112 __ CmpEqD(dst, lhs, rhs);
4113 return false;
4114 case kCondNE:
4115 __ CmpEqD(dst, lhs, rhs);
4116 return true;
4117 case kCondLT:
4118 if (gt_bias) {
4119 __ CmpLtD(dst, lhs, rhs);
4120 } else {
4121 __ CmpUltD(dst, lhs, rhs);
4122 }
4123 return false;
4124 case kCondLE:
4125 if (gt_bias) {
4126 __ CmpLeD(dst, lhs, rhs);
4127 } else {
4128 __ CmpUleD(dst, lhs, rhs);
4129 }
4130 return false;
4131 case kCondGT:
4132 if (gt_bias) {
4133 __ CmpUltD(dst, rhs, lhs);
4134 } else {
4135 __ CmpLtD(dst, rhs, lhs);
4136 }
4137 return false;
4138 case kCondGE:
4139 if (gt_bias) {
4140 __ CmpUleD(dst, rhs, lhs);
4141 } else {
4142 __ CmpLeD(dst, rhs, lhs);
4143 }
4144 return false;
4145 default:
4146 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4147 UNREACHABLE();
4148 }
4149 }
4150}
4151
Alexey Frunze299a9392015-12-08 16:08:02 -08004152void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4153 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004154 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004155 LocationSummary* locations,
4156 Mips64Label* label) {
4157 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4158 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004159 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004160 switch (cond) {
4161 case kCondEQ:
4162 __ CmpEqS(FTMP, lhs, rhs);
4163 __ Bc1nez(FTMP, label);
4164 break;
4165 case kCondNE:
4166 __ CmpEqS(FTMP, lhs, rhs);
4167 __ Bc1eqz(FTMP, label);
4168 break;
4169 case kCondLT:
4170 if (gt_bias) {
4171 __ CmpLtS(FTMP, lhs, rhs);
4172 } else {
4173 __ CmpUltS(FTMP, lhs, rhs);
4174 }
4175 __ Bc1nez(FTMP, label);
4176 break;
4177 case kCondLE:
4178 if (gt_bias) {
4179 __ CmpLeS(FTMP, lhs, rhs);
4180 } else {
4181 __ CmpUleS(FTMP, lhs, rhs);
4182 }
4183 __ Bc1nez(FTMP, label);
4184 break;
4185 case kCondGT:
4186 if (gt_bias) {
4187 __ CmpUltS(FTMP, rhs, lhs);
4188 } else {
4189 __ CmpLtS(FTMP, rhs, lhs);
4190 }
4191 __ Bc1nez(FTMP, label);
4192 break;
4193 case kCondGE:
4194 if (gt_bias) {
4195 __ CmpUleS(FTMP, rhs, lhs);
4196 } else {
4197 __ CmpLeS(FTMP, rhs, lhs);
4198 }
4199 __ Bc1nez(FTMP, label);
4200 break;
4201 default:
4202 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004203 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004204 }
4205 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004206 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004207 switch (cond) {
4208 case kCondEQ:
4209 __ CmpEqD(FTMP, lhs, rhs);
4210 __ Bc1nez(FTMP, label);
4211 break;
4212 case kCondNE:
4213 __ CmpEqD(FTMP, lhs, rhs);
4214 __ Bc1eqz(FTMP, label);
4215 break;
4216 case kCondLT:
4217 if (gt_bias) {
4218 __ CmpLtD(FTMP, lhs, rhs);
4219 } else {
4220 __ CmpUltD(FTMP, lhs, rhs);
4221 }
4222 __ Bc1nez(FTMP, label);
4223 break;
4224 case kCondLE:
4225 if (gt_bias) {
4226 __ CmpLeD(FTMP, lhs, rhs);
4227 } else {
4228 __ CmpUleD(FTMP, lhs, rhs);
4229 }
4230 __ Bc1nez(FTMP, label);
4231 break;
4232 case kCondGT:
4233 if (gt_bias) {
4234 __ CmpUltD(FTMP, rhs, lhs);
4235 } else {
4236 __ CmpLtD(FTMP, rhs, lhs);
4237 }
4238 __ Bc1nez(FTMP, label);
4239 break;
4240 case kCondGE:
4241 if (gt_bias) {
4242 __ CmpUleD(FTMP, rhs, lhs);
4243 } else {
4244 __ CmpLeD(FTMP, rhs, lhs);
4245 }
4246 __ Bc1nez(FTMP, label);
4247 break;
4248 default:
4249 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004250 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004251 }
4252 }
4253}
4254
Alexey Frunze4dda3372015-06-01 18:31:49 -07004255void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004256 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004257 Mips64Label* true_target,
4258 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004259 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004260
David Brazdil0debae72015-11-12 18:37:00 +00004261 if (true_target == nullptr && false_target == nullptr) {
4262 // Nothing to do. The code always falls through.
4263 return;
4264 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004265 // Constant condition, statically compared against "true" (integer value 1).
4266 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004267 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004268 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004269 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004270 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004271 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004272 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004273 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004275 }
David Brazdil0debae72015-11-12 18:37:00 +00004276 return;
4277 }
4278
4279 // The following code generates these patterns:
4280 // (1) true_target == nullptr && false_target != nullptr
4281 // - opposite condition true => branch to false_target
4282 // (2) true_target != nullptr && false_target == nullptr
4283 // - condition true => branch to true_target
4284 // (3) true_target != nullptr && false_target != nullptr
4285 // - condition true => branch to true_target
4286 // - branch to false_target
4287 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004288 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004289 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004290 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004291 if (true_target == nullptr) {
4292 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4293 } else {
4294 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4295 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004296 } else {
4297 // The condition instruction has not been materialized, use its inputs as
4298 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004299 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004300 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004301 LocationSummary* locations = cond->GetLocations();
4302 IfCondition if_cond = condition->GetCondition();
4303 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004304
David Brazdil0debae72015-11-12 18:37:00 +00004305 if (true_target == nullptr) {
4306 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004307 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004308 }
4309
Alexey Frunze299a9392015-12-08 16:08:02 -08004310 switch (type) {
4311 default:
4312 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4313 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004314 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004315 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4316 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004317 case DataType::Type::kFloat32:
4318 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004319 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4320 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004321 }
4322 }
David Brazdil0debae72015-11-12 18:37:00 +00004323
4324 // If neither branch falls through (case 3), the conditional branch to `true_target`
4325 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4326 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004327 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004328 }
4329}
4330
4331void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004332 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004333 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004334 locations->SetInAt(0, Location::RequiresRegister());
4335 }
4336}
4337
4338void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004339 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4340 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004341 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004342 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004343 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004344 nullptr : codegen_->GetLabelOf(false_successor);
4345 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004346}
4347
4348void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004349 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004350 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004351 InvokeRuntimeCallingConvention calling_convention;
4352 RegisterSet caller_saves = RegisterSet::Empty();
4353 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4354 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004355 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004356 locations->SetInAt(0, Location::RequiresRegister());
4357 }
4358}
4359
4360void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004361 SlowPathCodeMIPS64* slow_path =
4362 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004363 GenerateTestAndBranch(deoptimize,
4364 /* condition_input_index */ 0,
4365 slow_path->GetEntryLabel(),
4366 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004367}
4368
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004369// This function returns true if a conditional move can be generated for HSelect.
4370// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4371// branches and regular moves.
4372//
4373// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4374//
4375// While determining feasibility of a conditional move and setting inputs/outputs
4376// are two distinct tasks, this function does both because they share quite a bit
4377// of common logic.
4378static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4379 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4380 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4381 HCondition* condition = cond->AsCondition();
4382
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004383 DataType::Type cond_type =
4384 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4385 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004386
4387 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4388 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4389 bool is_true_value_zero_constant =
4390 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4391 bool is_false_value_zero_constant =
4392 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4393
4394 bool can_move_conditionally = false;
4395 bool use_const_for_false_in = false;
4396 bool use_const_for_true_in = false;
4397
4398 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004399 if (!DataType::IsFloatingPointType(cond_type)) {
4400 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004401 // Moving int/long on int/long condition.
4402 if (is_true_value_zero_constant) {
4403 // seleqz out_reg, false_reg, cond_reg
4404 can_move_conditionally = true;
4405 use_const_for_true_in = true;
4406 } else if (is_false_value_zero_constant) {
4407 // selnez out_reg, true_reg, cond_reg
4408 can_move_conditionally = true;
4409 use_const_for_false_in = true;
4410 } else if (materialized) {
4411 // Not materializing unmaterialized int conditions
4412 // to keep the instruction count low.
4413 // selnez AT, true_reg, cond_reg
4414 // seleqz TMP, false_reg, cond_reg
4415 // or out_reg, AT, TMP
4416 can_move_conditionally = true;
4417 }
4418 } else {
4419 // Moving float/double on int/long condition.
4420 if (materialized) {
4421 // Not materializing unmaterialized int conditions
4422 // to keep the instruction count low.
4423 can_move_conditionally = true;
4424 if (is_true_value_zero_constant) {
4425 // sltu TMP, ZERO, cond_reg
4426 // mtc1 TMP, temp_cond_reg
4427 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4428 use_const_for_true_in = true;
4429 } else if (is_false_value_zero_constant) {
4430 // sltu TMP, ZERO, cond_reg
4431 // mtc1 TMP, temp_cond_reg
4432 // selnez.fmt out_reg, true_reg, temp_cond_reg
4433 use_const_for_false_in = true;
4434 } else {
4435 // sltu TMP, ZERO, cond_reg
4436 // mtc1 TMP, temp_cond_reg
4437 // sel.fmt temp_cond_reg, false_reg, true_reg
4438 // mov.fmt out_reg, temp_cond_reg
4439 }
4440 }
4441 }
4442 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004443 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004444 // Moving int/long on float/double condition.
4445 can_move_conditionally = true;
4446 if (is_true_value_zero_constant) {
4447 // mfc1 TMP, temp_cond_reg
4448 // seleqz out_reg, false_reg, TMP
4449 use_const_for_true_in = true;
4450 } else if (is_false_value_zero_constant) {
4451 // mfc1 TMP, temp_cond_reg
4452 // selnez out_reg, true_reg, TMP
4453 use_const_for_false_in = true;
4454 } else {
4455 // mfc1 TMP, temp_cond_reg
4456 // selnez AT, true_reg, TMP
4457 // seleqz TMP, false_reg, TMP
4458 // or out_reg, AT, TMP
4459 }
4460 } else {
4461 // Moving float/double on float/double condition.
4462 can_move_conditionally = true;
4463 if (is_true_value_zero_constant) {
4464 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4465 use_const_for_true_in = true;
4466 } else if (is_false_value_zero_constant) {
4467 // selnez.fmt out_reg, true_reg, temp_cond_reg
4468 use_const_for_false_in = true;
4469 } else {
4470 // sel.fmt temp_cond_reg, false_reg, true_reg
4471 // mov.fmt out_reg, temp_cond_reg
4472 }
4473 }
4474 }
4475 }
4476
4477 if (can_move_conditionally) {
4478 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4479 } else {
4480 DCHECK(!use_const_for_false_in);
4481 DCHECK(!use_const_for_true_in);
4482 }
4483
4484 if (locations_to_set != nullptr) {
4485 if (use_const_for_false_in) {
4486 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4487 } else {
4488 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004489 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004490 ? Location::RequiresFpuRegister()
4491 : Location::RequiresRegister());
4492 }
4493 if (use_const_for_true_in) {
4494 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4495 } else {
4496 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004497 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004498 ? Location::RequiresFpuRegister()
4499 : Location::RequiresRegister());
4500 }
4501 if (materialized) {
4502 locations_to_set->SetInAt(2, Location::RequiresRegister());
4503 }
4504
4505 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004506 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004507 ? Location::RequiresFpuRegister()
4508 : Location::RequiresRegister());
4509 } else {
4510 locations_to_set->SetOut(Location::SameAsFirstInput());
4511 }
4512 }
4513
4514 return can_move_conditionally;
4515}
4516
4517
4518void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4519 LocationSummary* locations = select->GetLocations();
4520 Location dst = locations->Out();
4521 Location false_src = locations->InAt(0);
4522 Location true_src = locations->InAt(1);
4523 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4524 GpuRegister cond_reg = TMP;
4525 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004526 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004527 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004529
4530 if (IsBooleanValueOrMaterializedCondition(cond)) {
4531 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4532 } else {
4533 HCondition* condition = cond->AsCondition();
4534 LocationSummary* cond_locations = cond->GetLocations();
4535 IfCondition if_cond = condition->GetCondition();
4536 cond_type = condition->InputAt(0)->GetType();
4537 switch (cond_type) {
4538 default:
4539 cond_inverted = MaterializeIntLongCompare(if_cond,
4540 /* is64bit */ false,
4541 cond_locations,
4542 cond_reg);
4543 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004544 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004545 cond_inverted = MaterializeIntLongCompare(if_cond,
4546 /* is64bit */ true,
4547 cond_locations,
4548 cond_reg);
4549 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004550 case DataType::Type::kFloat32:
4551 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004552 cond_inverted = MaterializeFpCompare(if_cond,
4553 condition->IsGtBias(),
4554 cond_type,
4555 cond_locations,
4556 fcond_reg);
4557 break;
4558 }
4559 }
4560
4561 if (true_src.IsConstant()) {
4562 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4563 }
4564 if (false_src.IsConstant()) {
4565 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4566 }
4567
4568 switch (dst_type) {
4569 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004570 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004571 __ Mfc1(cond_reg, fcond_reg);
4572 }
4573 if (true_src.IsConstant()) {
4574 if (cond_inverted) {
4575 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4576 } else {
4577 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4578 }
4579 } else if (false_src.IsConstant()) {
4580 if (cond_inverted) {
4581 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4582 } else {
4583 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4584 }
4585 } else {
4586 DCHECK_NE(cond_reg, AT);
4587 if (cond_inverted) {
4588 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4589 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4590 } else {
4591 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4592 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4593 }
4594 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4595 }
4596 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004597 case DataType::Type::kFloat32: {
4598 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004599 // sel*.fmt tests bit 0 of the condition register, account for that.
4600 __ Sltu(TMP, ZERO, cond_reg);
4601 __ Mtc1(TMP, fcond_reg);
4602 }
4603 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4604 if (true_src.IsConstant()) {
4605 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4606 if (cond_inverted) {
4607 __ SelnezS(dst_reg, src_reg, fcond_reg);
4608 } else {
4609 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4610 }
4611 } else if (false_src.IsConstant()) {
4612 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4613 if (cond_inverted) {
4614 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4615 } else {
4616 __ SelnezS(dst_reg, src_reg, fcond_reg);
4617 }
4618 } else {
4619 if (cond_inverted) {
4620 __ SelS(fcond_reg,
4621 true_src.AsFpuRegister<FpuRegister>(),
4622 false_src.AsFpuRegister<FpuRegister>());
4623 } else {
4624 __ SelS(fcond_reg,
4625 false_src.AsFpuRegister<FpuRegister>(),
4626 true_src.AsFpuRegister<FpuRegister>());
4627 }
4628 __ MovS(dst_reg, fcond_reg);
4629 }
4630 break;
4631 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004632 case DataType::Type::kFloat64: {
4633 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004634 // sel*.fmt tests bit 0 of the condition register, account for that.
4635 __ Sltu(TMP, ZERO, cond_reg);
4636 __ Mtc1(TMP, fcond_reg);
4637 }
4638 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4639 if (true_src.IsConstant()) {
4640 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4641 if (cond_inverted) {
4642 __ SelnezD(dst_reg, src_reg, fcond_reg);
4643 } else {
4644 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4645 }
4646 } else if (false_src.IsConstant()) {
4647 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4648 if (cond_inverted) {
4649 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4650 } else {
4651 __ SelnezD(dst_reg, src_reg, fcond_reg);
4652 }
4653 } else {
4654 if (cond_inverted) {
4655 __ SelD(fcond_reg,
4656 true_src.AsFpuRegister<FpuRegister>(),
4657 false_src.AsFpuRegister<FpuRegister>());
4658 } else {
4659 __ SelD(fcond_reg,
4660 false_src.AsFpuRegister<FpuRegister>(),
4661 true_src.AsFpuRegister<FpuRegister>());
4662 }
4663 __ MovD(dst_reg, fcond_reg);
4664 }
4665 break;
4666 }
4667 }
4668}
4669
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004670void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004671 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004672 LocationSummary(flag, LocationSummary::kNoCall);
4673 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004674}
4675
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004676void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4677 __ LoadFromOffset(kLoadWord,
4678 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4679 SP,
4680 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004681}
4682
David Brazdil74eb1b22015-12-14 11:44:01 +00004683void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004684 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004685 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004686}
4687
4688void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004689 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4690 GenConditionalMove(select);
4691 } else {
4692 LocationSummary* locations = select->GetLocations();
4693 Mips64Label false_target;
4694 GenerateTestAndBranch(select,
4695 /* condition_input_index */ 2,
4696 /* true_target */ nullptr,
4697 &false_target);
4698 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4699 __ Bind(&false_target);
4700 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004701}
4702
David Srbecky0cf44932015-12-09 14:09:59 +00004703void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004704 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004705}
4706
David Srbeckyd28f4a02016-03-14 17:14:24 +00004707void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4708 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004709}
4710
4711void CodeGeneratorMIPS64::GenerateNop() {
4712 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004713}
4714
Alexey Frunze4dda3372015-06-01 18:31:49 -07004715void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004716 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004717 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004718 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004719 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004720 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004721 instruction,
4722 object_field_get_with_read_barrier
4723 ? LocationSummary::kCallOnSlowPath
4724 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004725 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4726 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4727 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004728 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004729 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004730 locations->SetOut(Location::RequiresFpuRegister());
4731 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004732 // The output overlaps in the case of an object field get with
4733 // read barriers enabled: we do not want the move to overwrite the
4734 // object's location, as we need it to emit the read barrier.
4735 locations->SetOut(Location::RequiresRegister(),
4736 object_field_get_with_read_barrier
4737 ? Location::kOutputOverlap
4738 : Location::kNoOutputOverlap);
4739 }
4740 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4741 // We need a temporary register for the read barrier marking slow
4742 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004743 if (!kBakerReadBarrierThunksEnableForFields) {
4744 locations->AddTemp(Location::RequiresRegister());
4745 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004746 }
4747}
4748
4749void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4750 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004751 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4752 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004753 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004754 Location obj_loc = locations->InAt(0);
4755 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4756 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004757 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004758 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004759 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004760 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4761
Alexey Frunze4dda3372015-06-01 18:31:49 -07004762 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004763 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004764 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004765 load_type = kLoadUnsignedByte;
4766 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004767 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004768 load_type = kLoadSignedByte;
4769 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004770 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004771 load_type = kLoadUnsignedHalfword;
4772 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004773 case DataType::Type::kInt16:
4774 load_type = kLoadSignedHalfword;
4775 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004776 case DataType::Type::kInt32:
4777 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004778 load_type = kLoadWord;
4779 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 case DataType::Type::kInt64:
4781 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004782 load_type = kLoadDoubleword;
4783 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004784 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004785 load_type = kLoadUnsignedWord;
4786 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004787 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004788 LOG(FATAL) << "Unreachable type " << type;
4789 UNREACHABLE();
4790 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004791 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004792 DCHECK(dst_loc.IsRegister());
4793 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004794 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004795 // /* HeapReference<Object> */ dst = *(obj + offset)
4796 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004797 Location temp_loc =
4798 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004799 // Note that a potential implicit null check is handled in this
4800 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4801 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4802 dst_loc,
4803 obj,
4804 offset,
4805 temp_loc,
4806 /* needs_null_check */ true);
4807 if (is_volatile) {
4808 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4809 }
4810 } else {
4811 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4812 if (is_volatile) {
4813 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4814 }
4815 // If read barriers are enabled, emit read barriers other than
4816 // Baker's using a slow path (and also unpoison the loaded
4817 // reference, if heap poisoning is enabled).
4818 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4819 }
4820 } else {
4821 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4822 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004823 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004824 DCHECK(dst_loc.IsFpuRegister());
4825 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004826 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004827 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004828
Alexey Frunze15958152017-02-09 19:08:30 -08004829 // Memory barriers, in the case of references, are handled in the
4830 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004831 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004832 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004833 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004834}
4835
4836void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4837 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4838 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004839 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004840 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004841 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004842 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004843 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004844 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004845 }
4846}
4847
4848void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004849 const FieldInfo& field_info,
4850 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004851 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004852 LocationSummary* locations = instruction->GetLocations();
4853 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004854 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004855 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004856 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004857 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4858 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004859 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4860
Alexey Frunze4dda3372015-06-01 18:31:49 -07004861 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004862 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004863 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004865 store_type = kStoreByte;
4866 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004867 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004868 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004869 store_type = kStoreHalfword;
4870 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004871 case DataType::Type::kInt32:
4872 case DataType::Type::kFloat32:
4873 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004874 store_type = kStoreWord;
4875 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004876 case DataType::Type::kInt64:
4877 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004878 store_type = kStoreDoubleword;
4879 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004880 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004881 LOG(FATAL) << "Unreachable type " << type;
4882 UNREACHABLE();
4883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004884
Alexey Frunze15958152017-02-09 19:08:30 -08004885 if (is_volatile) {
4886 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4887 }
4888
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004889 if (value_location.IsConstant()) {
4890 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4891 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4892 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004893 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004894 DCHECK(value_location.IsRegister());
4895 GpuRegister src = value_location.AsRegister<GpuRegister>();
4896 if (kPoisonHeapReferences && needs_write_barrier) {
4897 // Note that in the case where `value` is a null reference,
4898 // we do not enter this block, as a null reference does not
4899 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004900 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004901 __ PoisonHeapReference(TMP, src);
4902 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4903 } else {
4904 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4905 }
4906 } else {
4907 DCHECK(value_location.IsFpuRegister());
4908 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4909 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4910 }
4911 }
Alexey Frunze15958152017-02-09 19:08:30 -08004912
Alexey Frunzec061de12017-02-14 13:27:23 -08004913 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004914 DCHECK(value_location.IsRegister());
4915 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004916 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004917 }
Alexey Frunze15958152017-02-09 19:08:30 -08004918
4919 if (is_volatile) {
4920 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4921 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004922}
4923
4924void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4925 HandleFieldGet(instruction, instruction->GetFieldInfo());
4926}
4927
4928void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4929 HandleFieldGet(instruction, instruction->GetFieldInfo());
4930}
4931
4932void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4933 HandleFieldSet(instruction, instruction->GetFieldInfo());
4934}
4935
4936void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004937 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004938}
4939
Alexey Frunze15958152017-02-09 19:08:30 -08004940void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4941 HInstruction* instruction,
4942 Location out,
4943 uint32_t offset,
4944 Location maybe_temp,
4945 ReadBarrierOption read_barrier_option) {
4946 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4947 if (read_barrier_option == kWithReadBarrier) {
4948 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004949 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4950 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4951 }
Alexey Frunze15958152017-02-09 19:08:30 -08004952 if (kUseBakerReadBarrier) {
4953 // Load with fast path based Baker's read barrier.
4954 // /* HeapReference<Object> */ out = *(out + offset)
4955 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4956 out,
4957 out_reg,
4958 offset,
4959 maybe_temp,
4960 /* needs_null_check */ false);
4961 } else {
4962 // Load with slow path based read barrier.
4963 // Save the value of `out` into `maybe_temp` before overwriting it
4964 // in the following move operation, as we will need it for the
4965 // read barrier below.
4966 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4967 // /* HeapReference<Object> */ out = *(out + offset)
4968 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4969 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4970 }
4971 } else {
4972 // Plain load with no read barrier.
4973 // /* HeapReference<Object> */ out = *(out + offset)
4974 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4975 __ MaybeUnpoisonHeapReference(out_reg);
4976 }
4977}
4978
4979void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4980 HInstruction* instruction,
4981 Location out,
4982 Location obj,
4983 uint32_t offset,
4984 Location maybe_temp,
4985 ReadBarrierOption read_barrier_option) {
4986 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4987 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4988 if (read_barrier_option == kWithReadBarrier) {
4989 CHECK(kEmitCompilerReadBarrier);
4990 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004991 if (!kBakerReadBarrierThunksEnableForFields) {
4992 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4993 }
Alexey Frunze15958152017-02-09 19:08:30 -08004994 // Load with fast path based Baker's read barrier.
4995 // /* HeapReference<Object> */ out = *(obj + offset)
4996 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4997 out,
4998 obj_reg,
4999 offset,
5000 maybe_temp,
5001 /* needs_null_check */ false);
5002 } else {
5003 // Load with slow path based read barrier.
5004 // /* HeapReference<Object> */ out = *(obj + offset)
5005 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5006 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5007 }
5008 } else {
5009 // Plain load with no read barrier.
5010 // /* HeapReference<Object> */ out = *(obj + offset)
5011 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
5012 __ MaybeUnpoisonHeapReference(out_reg);
5013 }
5014}
5015
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005016static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5017 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5018 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5019 return reg - V0;
5020 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5021 return 13 + (reg - S2);
5022 } else if (reg == S8) { // One more.
5023 return 19;
5024 }
5025 LOG(FATAL) << "Unexpected register " << reg;
5026 UNREACHABLE();
5027}
5028
5029static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5030 int num = GetBakerMarkThunkNumber(reg) +
5031 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5032 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5033}
5034
5035static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5036 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5037 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5038}
5039
5040void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5041 Location root,
5042 GpuRegister obj,
5043 uint32_t offset,
5044 ReadBarrierOption read_barrier_option,
5045 Mips64Label* label_low) {
5046 if (label_low != nullptr) {
5047 DCHECK_EQ(offset, 0x5678u);
5048 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005049 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005050 if (read_barrier_option == kWithReadBarrier) {
5051 DCHECK(kEmitCompilerReadBarrier);
5052 if (kUseBakerReadBarrier) {
5053 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5054 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005055 if (kBakerReadBarrierThunksEnableForGcRoots) {
5056 // Note that we do not actually check the value of `GetIsGcMarking()`
5057 // to decide whether to mark the loaded GC root or not. Instead, we
5058 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5059 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5060 // vice versa.
5061 //
5062 // We use thunks for the slow path. That thunk checks the reference
5063 // and jumps to the entrypoint if needed.
5064 //
5065 // temp = Thread::Current()->pReadBarrierMarkReg00
5066 // // AKA &art_quick_read_barrier_mark_introspection.
5067 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5068 // if (temp != nullptr) {
5069 // temp = &gc_root_thunk<root_reg>
5070 // root = temp(root)
5071 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005072
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005073 const int32_t entry_point_offset =
5074 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5075 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5076 int16_t offset_low = Low16Bits(offset);
5077 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5078 // extension in lwu.
5079 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5080 GpuRegister base = short_offset ? obj : TMP;
5081 // Loading the entrypoint does not require a load acquire since it is only changed when
5082 // threads are suspended or running a checkpoint.
5083 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5084 if (!short_offset) {
5085 DCHECK(!label_low);
5086 __ Daui(base, obj, offset_high);
5087 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005088 Mips64Label skip_call;
5089 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005090 if (label_low != nullptr) {
5091 DCHECK(short_offset);
5092 __ Bind(label_low);
5093 }
5094 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5095 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5096 // in delay slot.
5097 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005098 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005099 } else {
5100 // Note that we do not actually check the value of `GetIsGcMarking()`
5101 // to decide whether to mark the loaded GC root or not. Instead, we
5102 // load into `temp` (T9) the read barrier mark entry point corresponding
5103 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5104 // is false, and vice versa.
5105 //
5106 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5107 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5108 // if (temp != null) {
5109 // root = temp(root)
5110 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005111
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005112 if (label_low != nullptr) {
5113 __ Bind(label_low);
5114 }
5115 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5116 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5117 static_assert(
5118 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5119 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5120 "have different sizes.");
5121 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5122 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5123 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005124
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005125 // Slow path marking the GC root `root`.
5126 Location temp = Location::RegisterLocation(T9);
5127 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005128 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005129 instruction,
5130 root,
5131 /*entrypoint*/ temp);
5132 codegen_->AddSlowPath(slow_path);
5133
5134 const int32_t entry_point_offset =
5135 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5136 // Loading the entrypoint does not require a load acquire since it is only changed when
5137 // threads are suspended or running a checkpoint.
5138 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5139 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5140 __ Bind(slow_path->GetExitLabel());
5141 }
Alexey Frunze15958152017-02-09 19:08:30 -08005142 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005143 if (label_low != nullptr) {
5144 __ Bind(label_low);
5145 }
Alexey Frunze15958152017-02-09 19:08:30 -08005146 // GC root loaded through a slow path for read barriers other
5147 // than Baker's.
5148 // /* GcRoot<mirror::Object>* */ root = obj + offset
5149 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5150 // /* mirror::Object* */ root = root->Read()
5151 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5152 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005153 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005154 if (label_low != nullptr) {
5155 __ Bind(label_low);
5156 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005157 // Plain GC root load with no read barrier.
5158 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5159 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5160 // Note that GC roots are not affected by heap poisoning, thus we
5161 // do not have to unpoison `root_reg` here.
5162 }
5163}
5164
Alexey Frunze15958152017-02-09 19:08:30 -08005165void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5166 Location ref,
5167 GpuRegister obj,
5168 uint32_t offset,
5169 Location temp,
5170 bool needs_null_check) {
5171 DCHECK(kEmitCompilerReadBarrier);
5172 DCHECK(kUseBakerReadBarrier);
5173
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005174 if (kBakerReadBarrierThunksEnableForFields) {
5175 // Note that we do not actually check the value of `GetIsGcMarking()`
5176 // to decide whether to mark the loaded reference or not. Instead, we
5177 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5178 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5179 // vice versa.
5180 //
5181 // We use thunks for the slow path. That thunk checks the reference
5182 // and jumps to the entrypoint if needed. If the holder is not gray,
5183 // it issues a load-load memory barrier and returns to the original
5184 // reference load.
5185 //
5186 // temp = Thread::Current()->pReadBarrierMarkReg00
5187 // // AKA &art_quick_read_barrier_mark_introspection.
5188 // if (temp != nullptr) {
5189 // temp = &field_array_thunk<holder_reg>
5190 // temp()
5191 // }
5192 // not_gray_return_address:
5193 // // If the offset is too large to fit into the lw instruction, we
5194 // // use an adjusted base register (TMP) here. This register
5195 // // receives bits 16 ... 31 of the offset before the thunk invocation
5196 // // and the thunk benefits from it.
5197 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5198 // gray_return_address:
5199
5200 DCHECK(temp.IsInvalid());
5201 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5202 const int32_t entry_point_offset =
5203 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5204 // There may have or may have not been a null check if the field offset is smaller than
5205 // the page size.
5206 // There must've been a null check in case it's actually a load from an array.
5207 // We will, however, perform an explicit null check in the thunk as it's easier to
5208 // do it than not.
5209 if (instruction->IsArrayGet()) {
5210 DCHECK(!needs_null_check);
5211 }
5212 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5213 // Loading the entrypoint does not require a load acquire since it is only changed when
5214 // threads are suspended or running a checkpoint.
5215 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5216 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005217 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005218 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005219 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005220 __ Nop(); // In forbidden slot.
5221 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005222 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005223 // /* HeapReference<Object> */ ref = *(obj + offset)
5224 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5225 } else {
5226 int16_t offset_low = Low16Bits(offset);
5227 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005228 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005229 __ Daui(TMP, obj, offset_high); // In delay slot.
5230 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005231 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005232 // /* HeapReference<Object> */ ref = *(obj + offset)
5233 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5234 }
5235 if (needs_null_check) {
5236 MaybeRecordImplicitNullCheck(instruction);
5237 }
5238 __ MaybeUnpoisonHeapReference(ref_reg);
5239 return;
5240 }
5241
Alexey Frunze15958152017-02-09 19:08:30 -08005242 // /* HeapReference<Object> */ ref = *(obj + offset)
5243 Location no_index = Location::NoLocation();
5244 ScaleFactor no_scale_factor = TIMES_1;
5245 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5246 ref,
5247 obj,
5248 offset,
5249 no_index,
5250 no_scale_factor,
5251 temp,
5252 needs_null_check);
5253}
5254
5255void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5256 Location ref,
5257 GpuRegister obj,
5258 uint32_t data_offset,
5259 Location index,
5260 Location temp,
5261 bool needs_null_check) {
5262 DCHECK(kEmitCompilerReadBarrier);
5263 DCHECK(kUseBakerReadBarrier);
5264
5265 static_assert(
5266 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5267 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005268 ScaleFactor scale_factor = TIMES_4;
5269
5270 if (kBakerReadBarrierThunksEnableForArrays) {
5271 // Note that we do not actually check the value of `GetIsGcMarking()`
5272 // to decide whether to mark the loaded reference or not. Instead, we
5273 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5274 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5275 // vice versa.
5276 //
5277 // We use thunks for the slow path. That thunk checks the reference
5278 // and jumps to the entrypoint if needed. If the holder is not gray,
5279 // it issues a load-load memory barrier and returns to the original
5280 // reference load.
5281 //
5282 // temp = Thread::Current()->pReadBarrierMarkReg00
5283 // // AKA &art_quick_read_barrier_mark_introspection.
5284 // if (temp != nullptr) {
5285 // temp = &field_array_thunk<holder_reg>
5286 // temp()
5287 // }
5288 // not_gray_return_address:
5289 // // The element address is pre-calculated in the TMP register before the
5290 // // thunk invocation and the thunk benefits from it.
5291 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5292 // gray_return_address:
5293
5294 DCHECK(temp.IsInvalid());
5295 DCHECK(index.IsValid());
5296 const int32_t entry_point_offset =
5297 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5298 // We will not do the explicit null check in the thunk as some form of a null check
5299 // must've been done earlier.
5300 DCHECK(!needs_null_check);
5301 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5302 // Loading the entrypoint does not require a load acquire since it is only changed when
5303 // threads are suspended or running a checkpoint.
5304 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005305 Mips64Label skip_call;
5306 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005307 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5308 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5309 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5310 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005311 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005312 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5313 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5314 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5315 __ MaybeUnpoisonHeapReference(ref_reg);
5316 return;
5317 }
5318
Alexey Frunze15958152017-02-09 19:08:30 -08005319 // /* HeapReference<Object> */ ref =
5320 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005321 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5322 ref,
5323 obj,
5324 data_offset,
5325 index,
5326 scale_factor,
5327 temp,
5328 needs_null_check);
5329}
5330
5331void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5332 Location ref,
5333 GpuRegister obj,
5334 uint32_t offset,
5335 Location index,
5336 ScaleFactor scale_factor,
5337 Location temp,
5338 bool needs_null_check,
5339 bool always_update_field) {
5340 DCHECK(kEmitCompilerReadBarrier);
5341 DCHECK(kUseBakerReadBarrier);
5342
5343 // In slow path based read barriers, the read barrier call is
5344 // inserted after the original load. However, in fast path based
5345 // Baker's read barriers, we need to perform the load of
5346 // mirror::Object::monitor_ *before* the original reference load.
5347 // This load-load ordering is required by the read barrier.
5348 // The fast path/slow path (for Baker's algorithm) should look like:
5349 //
5350 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5351 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5352 // HeapReference<Object> ref = *src; // Original reference load.
5353 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5354 // if (is_gray) {
5355 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5356 // }
5357 //
5358 // Note: the original implementation in ReadBarrier::Barrier is
5359 // slightly more complex as it performs additional checks that we do
5360 // not do here for performance reasons.
5361
5362 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5363 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5364 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5365
5366 // /* int32_t */ monitor = obj->monitor_
5367 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5368 if (needs_null_check) {
5369 MaybeRecordImplicitNullCheck(instruction);
5370 }
5371 // /* LockWord */ lock_word = LockWord(monitor)
5372 static_assert(sizeof(LockWord) == sizeof(int32_t),
5373 "art::LockWord and int32_t have different sizes.");
5374
5375 __ Sync(0); // Barrier to prevent load-load reordering.
5376
5377 // The actual reference load.
5378 if (index.IsValid()) {
5379 // Load types involving an "index": ArrayGet,
5380 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5381 // intrinsics.
5382 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5383 if (index.IsConstant()) {
5384 size_t computed_offset =
5385 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5386 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5387 } else {
5388 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005389 if (scale_factor == TIMES_1) {
5390 __ Daddu(TMP, index_reg, obj);
5391 } else {
5392 __ Dlsa(TMP, index_reg, obj, scale_factor);
5393 }
Alexey Frunze15958152017-02-09 19:08:30 -08005394 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5395 }
5396 } else {
5397 // /* HeapReference<Object> */ ref = *(obj + offset)
5398 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5399 }
5400
5401 // Object* ref = ref_addr->AsMirrorPtr()
5402 __ MaybeUnpoisonHeapReference(ref_reg);
5403
5404 // Slow path marking the object `ref` when it is gray.
5405 SlowPathCodeMIPS64* slow_path;
5406 if (always_update_field) {
5407 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5408 // of the form `obj + field_offset`, where `obj` is a register and
5409 // `field_offset` is a register. Thus `offset` and `scale_factor`
5410 // above are expected to be null in this code path.
5411 DCHECK_EQ(offset, 0u);
5412 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005413 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005414 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5415 ref,
5416 obj,
5417 /* field_offset */ index,
5418 temp_reg);
5419 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005420 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005421 }
5422 AddSlowPath(slow_path);
5423
5424 // if (rb_state == ReadBarrier::GrayState())
5425 // ref = ReadBarrier::Mark(ref);
5426 // Given the numeric representation, it's enough to check the low bit of the
5427 // rb_state. We do that by shifting the bit into the sign bit (31) and
5428 // performing a branch on less than zero.
5429 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5430 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5431 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5432 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5433 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5434 __ Bind(slow_path->GetExitLabel());
5435}
5436
5437void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5438 Location out,
5439 Location ref,
5440 Location obj,
5441 uint32_t offset,
5442 Location index) {
5443 DCHECK(kEmitCompilerReadBarrier);
5444
5445 // Insert a slow path based read barrier *after* the reference load.
5446 //
5447 // If heap poisoning is enabled, the unpoisoning of the loaded
5448 // reference will be carried out by the runtime within the slow
5449 // path.
5450 //
5451 // Note that `ref` currently does not get unpoisoned (when heap
5452 // poisoning is enabled), which is alright as the `ref` argument is
5453 // not used by the artReadBarrierSlow entry point.
5454 //
5455 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005456 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005457 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5458 AddSlowPath(slow_path);
5459
5460 __ Bc(slow_path->GetEntryLabel());
5461 __ Bind(slow_path->GetExitLabel());
5462}
5463
5464void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5465 Location out,
5466 Location ref,
5467 Location obj,
5468 uint32_t offset,
5469 Location index) {
5470 if (kEmitCompilerReadBarrier) {
5471 // Baker's read barriers shall be handled by the fast path
5472 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5473 DCHECK(!kUseBakerReadBarrier);
5474 // If heap poisoning is enabled, unpoisoning will be taken care of
5475 // by the runtime within the slow path.
5476 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5477 } else if (kPoisonHeapReferences) {
5478 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5479 }
5480}
5481
5482void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5483 Location out,
5484 Location root) {
5485 DCHECK(kEmitCompilerReadBarrier);
5486
5487 // Insert a slow path based read barrier *after* the GC root load.
5488 //
5489 // Note that GC roots are not affected by heap poisoning, so we do
5490 // not need to do anything special for this here.
5491 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005492 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005493 AddSlowPath(slow_path);
5494
5495 __ Bc(slow_path->GetEntryLabel());
5496 __ Bind(slow_path->GetExitLabel());
5497}
5498
Alexey Frunze4dda3372015-06-01 18:31:49 -07005499void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005500 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5501 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005502 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005503 switch (type_check_kind) {
5504 case TypeCheckKind::kExactCheck:
5505 case TypeCheckKind::kAbstractClassCheck:
5506 case TypeCheckKind::kClassHierarchyCheck:
5507 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005508 call_kind =
5509 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005510 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005511 break;
5512 case TypeCheckKind::kArrayCheck:
5513 case TypeCheckKind::kUnresolvedCheck:
5514 case TypeCheckKind::kInterfaceCheck:
5515 call_kind = LocationSummary::kCallOnSlowPath;
5516 break;
5517 }
5518
Vladimir Markoca6fff82017-10-03 14:49:14 +01005519 LocationSummary* locations =
5520 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005521 if (baker_read_barrier_slow_path) {
5522 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5523 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005524 locations->SetInAt(0, Location::RequiresRegister());
5525 locations->SetInAt(1, Location::RequiresRegister());
5526 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005527 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005528 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005529 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005530}
5531
5532void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005533 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005534 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005535 Location obj_loc = locations->InAt(0);
5536 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005537 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005538 Location out_loc = locations->Out();
5539 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5540 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5541 DCHECK_LE(num_temps, 1u);
5542 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005543 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5544 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5545 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5546 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005547 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005548 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005549
5550 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005551 // Avoid this check if we know `obj` is not null.
5552 if (instruction->MustDoNullCheck()) {
5553 __ Move(out, ZERO);
5554 __ Beqzc(obj, &done);
5555 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005556
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005557 switch (type_check_kind) {
5558 case TypeCheckKind::kExactCheck: {
5559 // /* 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,
5565 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005566 // Classes must be equal for the instanceof to succeed.
5567 __ Xor(out, out, cls);
5568 __ Sltiu(out, out, 1);
5569 break;
5570 }
5571
5572 case TypeCheckKind::kAbstractClassCheck: {
5573 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005574 GenerateReferenceLoadTwoRegisters(instruction,
5575 out_loc,
5576 obj_loc,
5577 class_offset,
5578 maybe_temp_loc,
5579 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005580 // If the class is abstract, we eagerly fetch the super class of the
5581 // object to avoid doing a comparison we know will fail.
5582 Mips64Label loop;
5583 __ Bind(&loop);
5584 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005585 GenerateReferenceLoadOneRegister(instruction,
5586 out_loc,
5587 super_offset,
5588 maybe_temp_loc,
5589 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005590 // If `out` is null, we use it for the result, and jump to `done`.
5591 __ Beqzc(out, &done);
5592 __ Bnec(out, cls, &loop);
5593 __ LoadConst32(out, 1);
5594 break;
5595 }
5596
5597 case TypeCheckKind::kClassHierarchyCheck: {
5598 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005599 GenerateReferenceLoadTwoRegisters(instruction,
5600 out_loc,
5601 obj_loc,
5602 class_offset,
5603 maybe_temp_loc,
5604 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005605 // Walk over the class hierarchy to find a match.
5606 Mips64Label loop, success;
5607 __ Bind(&loop);
5608 __ Beqc(out, cls, &success);
5609 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005610 GenerateReferenceLoadOneRegister(instruction,
5611 out_loc,
5612 super_offset,
5613 maybe_temp_loc,
5614 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005615 __ Bnezc(out, &loop);
5616 // If `out` is null, we use it for the result, and jump to `done`.
5617 __ Bc(&done);
5618 __ Bind(&success);
5619 __ LoadConst32(out, 1);
5620 break;
5621 }
5622
5623 case TypeCheckKind::kArrayObjectCheck: {
5624 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005625 GenerateReferenceLoadTwoRegisters(instruction,
5626 out_loc,
5627 obj_loc,
5628 class_offset,
5629 maybe_temp_loc,
5630 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005631 // Do an exact check.
5632 Mips64Label success;
5633 __ Beqc(out, cls, &success);
5634 // Otherwise, we need to check that the object's class is a non-primitive array.
5635 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005636 GenerateReferenceLoadOneRegister(instruction,
5637 out_loc,
5638 component_offset,
5639 maybe_temp_loc,
5640 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005641 // If `out` is null, we use it for the result, and jump to `done`.
5642 __ Beqzc(out, &done);
5643 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5644 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5645 __ Sltiu(out, out, 1);
5646 __ Bc(&done);
5647 __ Bind(&success);
5648 __ LoadConst32(out, 1);
5649 break;
5650 }
5651
5652 case TypeCheckKind::kArrayCheck: {
5653 // No read barrier since the slow path will retry upon failure.
5654 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005655 GenerateReferenceLoadTwoRegisters(instruction,
5656 out_loc,
5657 obj_loc,
5658 class_offset,
5659 maybe_temp_loc,
5660 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005661 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005662 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5663 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005664 codegen_->AddSlowPath(slow_path);
5665 __ Bnec(out, cls, slow_path->GetEntryLabel());
5666 __ LoadConst32(out, 1);
5667 break;
5668 }
5669
5670 case TypeCheckKind::kUnresolvedCheck:
5671 case TypeCheckKind::kInterfaceCheck: {
5672 // Note that we indeed only call on slow path, but we always go
5673 // into the slow path for the unresolved and interface check
5674 // cases.
5675 //
5676 // We cannot directly call the InstanceofNonTrivial runtime
5677 // entry point without resorting to a type checking slow path
5678 // here (i.e. by calling InvokeRuntime directly), as it would
5679 // require to assign fixed registers for the inputs of this
5680 // HInstanceOf instruction (following the runtime calling
5681 // convention), which might be cluttered by the potential first
5682 // read barrier emission at the beginning of this method.
5683 //
5684 // TODO: Introduce a new runtime entry point taking the object
5685 // to test (instead of its class) as argument, and let it deal
5686 // with the read barrier issues. This will let us refactor this
5687 // case of the `switch` code as it was previously (with a direct
5688 // call to the runtime not using a type checking slow path).
5689 // This should also be beneficial for the other cases above.
5690 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005691 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5692 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005693 codegen_->AddSlowPath(slow_path);
5694 __ Bc(slow_path->GetEntryLabel());
5695 break;
5696 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005697 }
5698
5699 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005700
5701 if (slow_path != nullptr) {
5702 __ Bind(slow_path->GetExitLabel());
5703 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005704}
5705
5706void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005708 locations->SetOut(Location::ConstantLocation(constant));
5709}
5710
5711void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5712 // Will be generated at use site.
5713}
5714
5715void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005716 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005717 locations->SetOut(Location::ConstantLocation(constant));
5718}
5719
5720void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5721 // Will be generated at use site.
5722}
5723
Calin Juravle175dc732015-08-25 15:42:32 +01005724void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5725 // The trampoline uses the same calling convention as dex calling conventions,
5726 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5727 // the method_idx.
5728 HandleInvoke(invoke);
5729}
5730
5731void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5732 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5733}
5734
Alexey Frunze4dda3372015-06-01 18:31:49 -07005735void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5736 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5737 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5738}
5739
5740void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5741 HandleInvoke(invoke);
5742 // The register T0 is required to be used for the hidden argument in
5743 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5744 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5745}
5746
5747void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5748 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5749 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005750 Location receiver = invoke->GetLocations()->InAt(0);
5751 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005752 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005753
5754 // Set the hidden argument.
5755 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5756 invoke->GetDexMethodIndex());
5757
5758 // temp = object->GetClass();
5759 if (receiver.IsStackSlot()) {
5760 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5761 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5762 } else {
5763 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5764 }
5765 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005766 // Instead of simply (possibly) unpoisoning `temp` here, we should
5767 // emit a read barrier for the previous class reference load.
5768 // However this is not required in practice, as this is an
5769 // intermediate/temporary reference and because the current
5770 // concurrent copying collector keeps the from-space memory
5771 // intact/accessible until the end of the marking phase (the
5772 // concurrent copying collector may not in the future).
5773 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005774 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5775 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5776 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005777 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005778 // temp = temp->GetImtEntryAt(method_offset);
5779 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5780 // T9 = temp->GetEntryPoint();
5781 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5782 // T9();
5783 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005784 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005785 DCHECK(!codegen_->IsLeafMethod());
5786 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5787}
5788
5789void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005790 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5791 if (intrinsic.TryDispatch(invoke)) {
5792 return;
5793 }
5794
Alexey Frunze4dda3372015-06-01 18:31:49 -07005795 HandleInvoke(invoke);
5796}
5797
5798void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005799 // Explicit clinit checks triggered by static invokes must have been pruned by
5800 // art::PrepareForRegisterAllocation.
5801 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005802
Chris Larsen3039e382015-08-26 07:54:08 -07005803 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5804 if (intrinsic.TryDispatch(invoke)) {
5805 return;
5806 }
5807
Alexey Frunze4dda3372015-06-01 18:31:49 -07005808 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005809}
5810
Orion Hodsonac141392017-01-13 11:53:47 +00005811void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5812 HandleInvoke(invoke);
5813}
5814
5815void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5816 codegen_->GenerateInvokePolymorphicCall(invoke);
5817}
5818
Chris Larsen3039e382015-08-26 07:54:08 -07005819static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005820 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005821 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5822 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005823 return true;
5824 }
5825 return false;
5826}
5827
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005828HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005829 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005830 bool fallback_load = false;
5831 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005832 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005833 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005834 case HLoadString::LoadKind::kBssEntry:
5835 DCHECK(!Runtime::Current()->UseJitCompilation());
5836 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005837 case HLoadString::LoadKind::kJitTableAddress:
5838 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005839 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005840 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005841 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005842 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005843 }
5844 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005845 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005846 }
5847 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005848}
5849
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005850HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5851 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005852 bool fallback_load = false;
5853 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005854 case HLoadClass::LoadKind::kInvalid:
5855 LOG(FATAL) << "UNREACHABLE";
5856 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005857 case HLoadClass::LoadKind::kReferrersClass:
5858 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005859 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005860 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005861 case HLoadClass::LoadKind::kBssEntry:
5862 DCHECK(!Runtime::Current()->UseJitCompilation());
5863 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005864 case HLoadClass::LoadKind::kJitTableAddress:
5865 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005866 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005867 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005868 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005869 break;
5870 }
5871 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005872 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005873 }
5874 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005875}
5876
Vladimir Markodc151b22015-10-15 18:02:30 +01005877HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5878 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005879 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005880 // On MIPS64 we support all dispatch types.
5881 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005882}
5883
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005884void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5885 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005886 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005887 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005888 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5889 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5890
Alexey Frunze19f6c692016-11-30 19:19:55 -08005891 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005892 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005893 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005894 uint32_t offset =
5895 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005896 __ LoadFromOffset(kLoadDoubleword,
5897 temp.AsRegister<GpuRegister>(),
5898 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005899 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005900 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005901 }
Vladimir Marko58155012015-08-19 12:49:41 +00005902 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005903 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005904 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005905 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5906 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005907 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005908 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005909 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5910 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5911 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005912 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5913 break;
5914 }
Vladimir Marko58155012015-08-19 12:49:41 +00005915 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005916 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5917 kLoadDoubleword,
5918 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005919 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005920 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005921 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005922 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005923 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5924 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5925 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005926 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5927 break;
5928 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005929 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5930 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5931 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005932 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005933 }
5934
Alexey Frunze19f6c692016-11-30 19:19:55 -08005935 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005936 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005937 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005938 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005939 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5940 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5941 __ LoadFromOffset(kLoadDoubleword,
5942 T9,
5943 callee_method.AsRegister<GpuRegister>(),
5944 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005945 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005946 // T9()
5947 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005948 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005949 break;
5950 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005951 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5952
Alexey Frunze4dda3372015-06-01 18:31:49 -07005953 DCHECK(!IsLeafMethod());
5954}
5955
5956void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005957 // Explicit clinit checks triggered by static invokes must have been pruned by
5958 // art::PrepareForRegisterAllocation.
5959 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005960
5961 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5962 return;
5963 }
5964
5965 LocationSummary* locations = invoke->GetLocations();
5966 codegen_->GenerateStaticOrDirectCall(invoke,
5967 locations->HasTemps()
5968 ? locations->GetTemp(0)
5969 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005970}
5971
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005972void CodeGeneratorMIPS64::GenerateVirtualCall(
5973 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005974 // Use the calling convention instead of the location of the receiver, as
5975 // intrinsics may have put the receiver in a different register. In the intrinsics
5976 // slow path, the arguments have been moved to the right place, so here we are
5977 // guaranteed that the receiver is the first register of the calling convention.
5978 InvokeDexCallingConvention calling_convention;
5979 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5980
Alexey Frunze53afca12015-11-05 16:34:23 -08005981 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005982 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5983 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5984 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005985 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005986
5987 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005988 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005989 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005990 // Instead of simply (possibly) unpoisoning `temp` here, we should
5991 // emit a read barrier for the previous class reference load.
5992 // However this is not required in practice, as this is an
5993 // intermediate/temporary reference and because the current
5994 // concurrent copying collector keeps the from-space memory
5995 // intact/accessible until the end of the marking phase (the
5996 // concurrent copying collector may not in the future).
5997 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005998 // temp = temp->GetMethodAt(method_offset);
5999 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
6000 // T9 = temp->GetEntryPoint();
6001 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
6002 // T9();
6003 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006004 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01006005 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08006006}
6007
6008void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
6009 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
6010 return;
6011 }
6012
6013 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006014 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006015}
6016
6017void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006018 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006019 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006020 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006021 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6022 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006023 return;
6024 }
Vladimir Marko41559982017-01-06 14:04:23 +00006025 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006026
Alexey Frunze15958152017-02-09 19:08:30 -08006027 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6028 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006029 ? LocationSummary::kCallOnSlowPath
6030 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006031 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006032 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6033 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6034 }
Vladimir Marko41559982017-01-06 14:04:23 +00006035 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006036 locations->SetInAt(0, Location::RequiresRegister());
6037 }
6038 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006039 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6040 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6041 // Rely on the type resolution or initialization and marking to save everything we need.
6042 RegisterSet caller_saves = RegisterSet::Empty();
6043 InvokeRuntimeCallingConvention calling_convention;
6044 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6045 locations->SetCustomSlowPathCallerSaves(caller_saves);
6046 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006047 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006048 }
6049 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006050}
6051
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006052// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6053// move.
6054void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006055 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006056 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006057 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006058 return;
6059 }
Vladimir Marko41559982017-01-06 14:04:23 +00006060 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006061
Vladimir Marko41559982017-01-06 14:04:23 +00006062 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006063 Location out_loc = locations->Out();
6064 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6065 GpuRegister current_method_reg = ZERO;
6066 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006067 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006068 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6069 }
6070
Alexey Frunze15958152017-02-09 19:08:30 -08006071 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6072 ? kWithoutReadBarrier
6073 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006074 bool generate_null_check = false;
6075 switch (load_kind) {
6076 case HLoadClass::LoadKind::kReferrersClass:
6077 DCHECK(!cls->CanCallRuntime());
6078 DCHECK(!cls->MustGenerateClinitCheck());
6079 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6080 GenerateGcRootFieldLoad(cls,
6081 out_loc,
6082 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006083 ArtMethod::DeclaringClassOffset().Int32Value(),
6084 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006085 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006086 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006087 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006088 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006089 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006090 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006091 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6092 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6093 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006094 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6095 break;
6096 }
6097 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006098 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006099 uint32_t address = dchecked_integral_cast<uint32_t>(
6100 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6101 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006102 __ LoadLiteral(out,
6103 kLoadUnsignedWord,
6104 codegen_->DeduplicateBootImageAddressLiteral(address));
6105 break;
6106 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006107 case HLoadClass::LoadKind::kBootImageClassTable: {
6108 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6109 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6110 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6111 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6112 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6113 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6114 __ Lwu(out, AT, /* placeholder */ 0x5678);
6115 // Extract the reference from the slot data, i.e. clear the hash bits.
6116 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6117 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6118 if (masked_hash != 0) {
6119 __ Daddiu(out, out, -masked_hash);
6120 }
6121 break;
6122 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006123 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006124 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6125 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006126 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6127 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006128 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006129 GenerateGcRootFieldLoad(cls,
6130 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006131 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006132 /* placeholder */ 0x5678,
6133 read_barrier_option,
6134 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006135 generate_null_check = true;
6136 break;
6137 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006138 case HLoadClass::LoadKind::kJitTableAddress:
6139 __ LoadLiteral(out,
6140 kLoadUnsignedWord,
6141 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6142 cls->GetTypeIndex(),
6143 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006144 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006145 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006146 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006147 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006148 LOG(FATAL) << "UNREACHABLE";
6149 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006150 }
6151
6152 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6153 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006154 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006155 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006156 codegen_->AddSlowPath(slow_path);
6157 if (generate_null_check) {
6158 __ Beqzc(out, slow_path->GetEntryLabel());
6159 }
6160 if (cls->MustGenerateClinitCheck()) {
6161 GenerateClassInitializationCheck(slow_path, out);
6162 } else {
6163 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006164 }
6165 }
6166}
6167
David Brazdilcb1c0552015-08-04 16:22:25 +01006168static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006169 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006170}
6171
Alexey Frunze4dda3372015-06-01 18:31:49 -07006172void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6173 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006174 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006175 locations->SetOut(Location::RequiresRegister());
6176}
6177
6178void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6179 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006180 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6181}
6182
6183void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006184 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006185}
6186
6187void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6188 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006189}
6190
Alexey Frunze4dda3372015-06-01 18:31:49 -07006191void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006192 HLoadString::LoadKind load_kind = load->GetLoadKind();
6193 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006194 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006195 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006196 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006197 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006198 } else {
6199 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006200 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6201 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6202 // Rely on the pResolveString and marking to save everything we need.
6203 RegisterSet caller_saves = RegisterSet::Empty();
6204 InvokeRuntimeCallingConvention calling_convention;
6205 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6206 locations->SetCustomSlowPathCallerSaves(caller_saves);
6207 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006208 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006209 }
6210 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006211 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006212}
6213
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006214// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6215// move.
6216void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006217 HLoadString::LoadKind load_kind = load->GetLoadKind();
6218 LocationSummary* locations = load->GetLocations();
6219 Location out_loc = locations->Out();
6220 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6221
6222 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006223 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6224 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006225 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006226 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006227 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6228 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6229 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006230 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006231 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006232 }
6233 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006234 uint32_t address = dchecked_integral_cast<uint32_t>(
6235 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6236 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006237 __ LoadLiteral(out,
6238 kLoadUnsignedWord,
6239 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006240 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006241 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006242 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006243 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006244 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006245 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006246 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6247 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006248 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6249 __ Lwu(out, AT, /* placeholder */ 0x5678);
6250 return;
6251 }
6252 case HLoadString::LoadKind::kBssEntry: {
6253 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6254 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6255 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6256 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6257 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006258 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006259 GenerateGcRootFieldLoad(load,
6260 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006261 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006262 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006263 kCompilerReadBarrierOption,
6264 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006265 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006266 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006267 codegen_->AddSlowPath(slow_path);
6268 __ Beqzc(out, slow_path->GetEntryLabel());
6269 __ Bind(slow_path->GetExitLabel());
6270 return;
6271 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006272 case HLoadString::LoadKind::kJitTableAddress:
6273 __ LoadLiteral(out,
6274 kLoadUnsignedWord,
6275 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6276 load->GetStringIndex(),
6277 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006278 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006279 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006280 default:
6281 break;
6282 }
6283
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006284 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006285 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006286 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006287 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006288 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6289 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6290 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006291}
6292
Alexey Frunze4dda3372015-06-01 18:31:49 -07006293void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006294 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006295 locations->SetOut(Location::ConstantLocation(constant));
6296}
6297
6298void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6299 // Will be generated at use site.
6300}
6301
6302void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006303 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6304 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006305 InvokeRuntimeCallingConvention calling_convention;
6306 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6307}
6308
6309void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006310 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006311 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006312 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006313 if (instruction->IsEnter()) {
6314 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6315 } else {
6316 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6317 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006318}
6319
6320void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6321 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006322 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006323 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006324 case DataType::Type::kInt32:
6325 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006326 locations->SetInAt(0, Location::RequiresRegister());
6327 locations->SetInAt(1, Location::RequiresRegister());
6328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6329 break;
6330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 case DataType::Type::kFloat32:
6332 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006333 locations->SetInAt(0, Location::RequiresFpuRegister());
6334 locations->SetInAt(1, Location::RequiresFpuRegister());
6335 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6336 break;
6337
6338 default:
6339 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6340 }
6341}
6342
6343void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006344 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006345 LocationSummary* locations = instruction->GetLocations();
6346
6347 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 case DataType::Type::kInt32:
6349 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006350 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6351 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6352 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006353 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006354 __ MulR6(dst, lhs, rhs);
6355 else
6356 __ Dmul(dst, lhs, rhs);
6357 break;
6358 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006359 case DataType::Type::kFloat32:
6360 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006361 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6362 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6363 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006364 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006365 __ MulS(dst, lhs, rhs);
6366 else
6367 __ MulD(dst, lhs, rhs);
6368 break;
6369 }
6370 default:
6371 LOG(FATAL) << "Unexpected mul type " << type;
6372 }
6373}
6374
6375void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6376 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006377 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006378 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 case DataType::Type::kInt32:
6380 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006381 locations->SetInAt(0, Location::RequiresRegister());
6382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6383 break;
6384
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006385 case DataType::Type::kFloat32:
6386 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006387 locations->SetInAt(0, Location::RequiresFpuRegister());
6388 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6389 break;
6390
6391 default:
6392 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6393 }
6394}
6395
6396void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006398 LocationSummary* locations = instruction->GetLocations();
6399
6400 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006401 case DataType::Type::kInt32:
6402 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006403 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6404 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006405 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006406 __ Subu(dst, ZERO, src);
6407 else
6408 __ Dsubu(dst, ZERO, src);
6409 break;
6410 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006411 case DataType::Type::kFloat32:
6412 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006413 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6414 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006415 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006416 __ NegS(dst, src);
6417 else
6418 __ NegD(dst, src);
6419 break;
6420 }
6421 default:
6422 LOG(FATAL) << "Unexpected neg type " << type;
6423 }
6424}
6425
6426void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006427 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6428 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006429 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006430 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006431 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6432 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006433}
6434
6435void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006436 // Note: if heap poisoning is enabled, the entry point takes care
6437 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006438 QuickEntrypointEnum entrypoint =
6439 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6440 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006441 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006442 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006443}
6444
6445void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006446 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6447 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006448 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006449 if (instruction->IsStringAlloc()) {
6450 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6451 } else {
6452 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006453 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006454 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006455}
6456
6457void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006458 // Note: if heap poisoning is enabled, the entry point takes care
6459 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006460 if (instruction->IsStringAlloc()) {
6461 // String is allocated through StringFactory. Call NewEmptyString entry point.
6462 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006463 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006464 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006465 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6466 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6467 __ Jalr(T9);
6468 __ Nop();
6469 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6470 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006471 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006472 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006473 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006474}
6475
6476void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006477 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006478 locations->SetInAt(0, Location::RequiresRegister());
6479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6480}
6481
6482void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006483 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006484 LocationSummary* locations = instruction->GetLocations();
6485
6486 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006487 case DataType::Type::kInt32:
6488 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006489 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6490 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6491 __ Nor(dst, src, ZERO);
6492 break;
6493 }
6494
6495 default:
6496 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6497 }
6498}
6499
6500void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006501 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006502 locations->SetInAt(0, Location::RequiresRegister());
6503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6504}
6505
6506void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6507 LocationSummary* locations = instruction->GetLocations();
6508 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6509 locations->InAt(0).AsRegister<GpuRegister>(),
6510 1);
6511}
6512
6513void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006514 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6515 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006516}
6517
Calin Juravle2ae48182016-03-16 14:05:09 +00006518void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6519 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006520 return;
6521 }
6522 Location obj = instruction->GetLocations()->InAt(0);
6523
6524 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006525 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006526}
6527
Calin Juravle2ae48182016-03-16 14:05:09 +00006528void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006529 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006530 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006531 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006532
6533 Location obj = instruction->GetLocations()->InAt(0);
6534
6535 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6536}
6537
6538void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006539 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006540}
6541
6542void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6543 HandleBinaryOp(instruction);
6544}
6545
6546void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6547 HandleBinaryOp(instruction);
6548}
6549
6550void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6551 LOG(FATAL) << "Unreachable";
6552}
6553
6554void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006555 if (instruction->GetNext()->IsSuspendCheck() &&
6556 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6557 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6558 // The back edge will generate the suspend check.
6559 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6560 }
6561
Alexey Frunze4dda3372015-06-01 18:31:49 -07006562 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6563}
6564
6565void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006566 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006567 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6568 if (location.IsStackSlot()) {
6569 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6570 } else if (location.IsDoubleStackSlot()) {
6571 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6572 }
6573 locations->SetOut(location);
6574}
6575
6576void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6577 ATTRIBUTE_UNUSED) {
6578 // Nothing to do, the parameter is already at its location.
6579}
6580
6581void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6582 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006583 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006584 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6585}
6586
6587void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6588 ATTRIBUTE_UNUSED) {
6589 // Nothing to do, the method is already at its location.
6590}
6591
6592void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006594 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006595 locations->SetInAt(i, Location::Any());
6596 }
6597 locations->SetOut(Location::Any());
6598}
6599
6600void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6601 LOG(FATAL) << "Unreachable";
6602}
6603
6604void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006605 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006606 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006607 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6608 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006609 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006610
6611 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006612 case DataType::Type::kInt32:
6613 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006614 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006615 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6617 break;
6618
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006619 case DataType::Type::kFloat32:
6620 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006621 InvokeRuntimeCallingConvention calling_convention;
6622 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6623 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6624 locations->SetOut(calling_convention.GetReturnLocation(type));
6625 break;
6626 }
6627
6628 default:
6629 LOG(FATAL) << "Unexpected rem type " << type;
6630 }
6631}
6632
6633void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006634 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006635
6636 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006637 case DataType::Type::kInt32:
6638 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006639 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006640 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006642 case DataType::Type::kFloat32:
6643 case DataType::Type::kFloat64: {
6644 QuickEntrypointEnum entrypoint =
6645 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006646 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006647 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006648 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6649 } else {
6650 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6651 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006652 break;
6653 }
6654 default:
6655 LOG(FATAL) << "Unexpected rem type " << type;
6656 }
6657}
6658
Igor Murashkind01745e2017-04-05 16:40:31 -07006659void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6660 constructor_fence->SetLocations(nullptr);
6661}
6662
6663void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6664 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6665 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6666}
6667
Alexey Frunze4dda3372015-06-01 18:31:49 -07006668void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6669 memory_barrier->SetLocations(nullptr);
6670}
6671
6672void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6673 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6674}
6675
6676void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006677 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006678 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006679 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6680}
6681
6682void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6683 codegen_->GenerateFrameExit();
6684}
6685
6686void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6687 ret->SetLocations(nullptr);
6688}
6689
6690void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6691 codegen_->GenerateFrameExit();
6692}
6693
Alexey Frunze92d90602015-12-18 18:16:36 -08006694void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6695 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006696}
6697
Alexey Frunze92d90602015-12-18 18:16:36 -08006698void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6699 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006700}
6701
Alexey Frunze4dda3372015-06-01 18:31:49 -07006702void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6703 HandleShift(shl);
6704}
6705
6706void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6707 HandleShift(shl);
6708}
6709
6710void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6711 HandleShift(shr);
6712}
6713
6714void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6715 HandleShift(shr);
6716}
6717
Alexey Frunze4dda3372015-06-01 18:31:49 -07006718void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6719 HandleBinaryOp(instruction);
6720}
6721
6722void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6723 HandleBinaryOp(instruction);
6724}
6725
6726void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6727 HandleFieldGet(instruction, instruction->GetFieldInfo());
6728}
6729
6730void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6731 HandleFieldGet(instruction, instruction->GetFieldInfo());
6732}
6733
6734void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6735 HandleFieldSet(instruction, instruction->GetFieldInfo());
6736}
6737
6738void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006739 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006740}
6741
Calin Juravlee460d1d2015-09-29 04:52:17 +01006742void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6743 HUnresolvedInstanceFieldGet* instruction) {
6744 FieldAccessCallingConventionMIPS64 calling_convention;
6745 codegen_->CreateUnresolvedFieldLocationSummary(
6746 instruction, instruction->GetFieldType(), calling_convention);
6747}
6748
6749void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6750 HUnresolvedInstanceFieldGet* instruction) {
6751 FieldAccessCallingConventionMIPS64 calling_convention;
6752 codegen_->GenerateUnresolvedFieldAccess(instruction,
6753 instruction->GetFieldType(),
6754 instruction->GetFieldIndex(),
6755 instruction->GetDexPc(),
6756 calling_convention);
6757}
6758
6759void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6760 HUnresolvedInstanceFieldSet* instruction) {
6761 FieldAccessCallingConventionMIPS64 calling_convention;
6762 codegen_->CreateUnresolvedFieldLocationSummary(
6763 instruction, instruction->GetFieldType(), calling_convention);
6764}
6765
6766void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6767 HUnresolvedInstanceFieldSet* instruction) {
6768 FieldAccessCallingConventionMIPS64 calling_convention;
6769 codegen_->GenerateUnresolvedFieldAccess(instruction,
6770 instruction->GetFieldType(),
6771 instruction->GetFieldIndex(),
6772 instruction->GetDexPc(),
6773 calling_convention);
6774}
6775
6776void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6777 HUnresolvedStaticFieldGet* instruction) {
6778 FieldAccessCallingConventionMIPS64 calling_convention;
6779 codegen_->CreateUnresolvedFieldLocationSummary(
6780 instruction, instruction->GetFieldType(), calling_convention);
6781}
6782
6783void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6784 HUnresolvedStaticFieldGet* instruction) {
6785 FieldAccessCallingConventionMIPS64 calling_convention;
6786 codegen_->GenerateUnresolvedFieldAccess(instruction,
6787 instruction->GetFieldType(),
6788 instruction->GetFieldIndex(),
6789 instruction->GetDexPc(),
6790 calling_convention);
6791}
6792
6793void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6794 HUnresolvedStaticFieldSet* instruction) {
6795 FieldAccessCallingConventionMIPS64 calling_convention;
6796 codegen_->CreateUnresolvedFieldLocationSummary(
6797 instruction, instruction->GetFieldType(), calling_convention);
6798}
6799
6800void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6801 HUnresolvedStaticFieldSet* instruction) {
6802 FieldAccessCallingConventionMIPS64 calling_convention;
6803 codegen_->GenerateUnresolvedFieldAccess(instruction,
6804 instruction->GetFieldType(),
6805 instruction->GetFieldIndex(),
6806 instruction->GetDexPc(),
6807 calling_convention);
6808}
6809
Alexey Frunze4dda3372015-06-01 18:31:49 -07006810void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006811 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6812 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006813 // In suspend check slow path, usually there are no caller-save registers at all.
6814 // If SIMD instructions are present, however, we force spilling all live SIMD
6815 // registers in full width (since the runtime only saves/restores lower part).
6816 locations->SetCustomSlowPathCallerSaves(
6817 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006818}
6819
6820void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6821 HBasicBlock* block = instruction->GetBlock();
6822 if (block->GetLoopInformation() != nullptr) {
6823 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6824 // The back edge will generate the suspend check.
6825 return;
6826 }
6827 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6828 // The goto will generate the suspend check.
6829 return;
6830 }
6831 GenerateSuspendCheck(instruction, nullptr);
6832}
6833
Alexey Frunze4dda3372015-06-01 18:31:49 -07006834void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006835 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6836 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006837 InvokeRuntimeCallingConvention calling_convention;
6838 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6839}
6840
6841void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006842 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006843 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6844}
6845
6846void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006847 DataType::Type input_type = conversion->GetInputType();
6848 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006849 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6850 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006852 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6853 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006854 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6855 }
6856
Vladimir Markoca6fff82017-10-03 14:49:14 +01006857 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006858
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006859 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006860 locations->SetInAt(0, Location::RequiresFpuRegister());
6861 } else {
6862 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006863 }
6864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006865 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006866 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006867 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006869 }
6870}
6871
6872void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6873 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006874 DataType::Type result_type = conversion->GetResultType();
6875 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006876
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006877 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6878 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006880 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006881 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6882 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6883
6884 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006885 case DataType::Type::kUint8:
6886 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006887 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006888 case DataType::Type::kInt8:
6889 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006890 // Type conversion from long to types narrower than int is a result of code
6891 // transformations. To avoid unpredictable results for SEB and SEH, we first
6892 // need to sign-extend the low 32-bit value into bits 32 through 63.
6893 __ Sll(dst, src, 0);
6894 __ Seb(dst, dst);
6895 } else {
6896 __ Seb(dst, src);
6897 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006898 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006899 case DataType::Type::kUint16:
6900 __ Andi(dst, src, 0xFFFF);
6901 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006902 case DataType::Type::kInt16:
6903 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006904 // Type conversion from long to types narrower than int is a result of code
6905 // transformations. To avoid unpredictable results for SEB and SEH, we first
6906 // need to sign-extend the low 32-bit value into bits 32 through 63.
6907 __ Sll(dst, src, 0);
6908 __ Seh(dst, dst);
6909 } else {
6910 __ Seh(dst, src);
6911 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006912 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006913 case DataType::Type::kInt32:
6914 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006915 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6916 // conversions, except when the input and output registers are the same and we are not
6917 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006918 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006919 __ Sll(dst, src, 0);
6920 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006921 break;
6922
6923 default:
6924 LOG(FATAL) << "Unexpected type conversion from " << input_type
6925 << " to " << result_type;
6926 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006927 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006928 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6929 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006930 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006931 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006932 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006933 __ Cvtsl(dst, FTMP);
6934 } else {
6935 __ Cvtdl(dst, FTMP);
6936 }
6937 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006938 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006939 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006940 __ Cvtsw(dst, FTMP);
6941 } else {
6942 __ Cvtdw(dst, FTMP);
6943 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006944 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006945 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6946 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006947 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6948 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006950 if (result_type == DataType::Type::kInt64) {
6951 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006952 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006953 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006954 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006955 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006956 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006957 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006958 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006959 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006960 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006961 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006962 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006963 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006964 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006965 } else if (DataType::IsFloatingPointType(result_type) &&
6966 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006967 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6968 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006969 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006970 __ Cvtsd(dst, src);
6971 } else {
6972 __ Cvtds(dst, src);
6973 }
6974 } else {
6975 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6976 << " to " << result_type;
6977 }
6978}
6979
6980void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6981 HandleShift(ushr);
6982}
6983
6984void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6985 HandleShift(ushr);
6986}
6987
6988void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6989 HandleBinaryOp(instruction);
6990}
6991
6992void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6993 HandleBinaryOp(instruction);
6994}
6995
6996void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6997 // Nothing to do, this should be removed during prepare for register allocator.
6998 LOG(FATAL) << "Unreachable";
6999}
7000
7001void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
7002 // Nothing to do, this should be removed during prepare for register allocator.
7003 LOG(FATAL) << "Unreachable";
7004}
7005
7006void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007007 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007008}
7009
7010void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007011 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007012}
7013
7014void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007015 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007016}
7017
7018void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007019 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007020}
7021
7022void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007023 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007024}
7025
7026void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007027 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007028}
7029
7030void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007031 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007032}
7033
7034void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007035 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007036}
7037
7038void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007039 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007040}
7041
7042void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007043 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007044}
7045
7046void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007047 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007048}
7049
7050void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007051 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007052}
7053
Aart Bike9f37602015-10-09 11:15:55 -07007054void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007056}
7057
7058void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007060}
7061
7062void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007064}
7065
7066void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007067 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007068}
7069
7070void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007071 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007072}
7073
7074void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007075 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007076}
7077
7078void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007079 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007080}
7081
7082void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007083 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007084}
7085
Mark Mendellfe57faa2015-09-18 09:26:15 -04007086// Simple implementation of packed switch - generate cascaded compare/jumps.
7087void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7088 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007089 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007090 locations->SetInAt(0, Location::RequiresRegister());
7091}
7092
Alexey Frunze0960ac52016-12-20 17:24:59 -08007093void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7094 int32_t lower_bound,
7095 uint32_t num_entries,
7096 HBasicBlock* switch_block,
7097 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007098 // Create a set of compare/jumps.
7099 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007100 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007101 // Jump to default if index is negative
7102 // Note: We don't check the case that index is positive while value < lower_bound, because in
7103 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7104 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7105
Alexey Frunze0960ac52016-12-20 17:24:59 -08007106 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007107 // Jump to successors[0] if value == lower_bound.
7108 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7109 int32_t last_index = 0;
7110 for (; num_entries - last_index > 2; last_index += 2) {
7111 __ Addiu(temp_reg, temp_reg, -2);
7112 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7113 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7114 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7115 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7116 }
7117 if (num_entries - last_index == 2) {
7118 // The last missing case_value.
7119 __ Addiu(temp_reg, temp_reg, -1);
7120 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007121 }
7122
7123 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007124 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007125 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007126 }
7127}
7128
Alexey Frunze0960ac52016-12-20 17:24:59 -08007129void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7130 int32_t lower_bound,
7131 uint32_t num_entries,
7132 HBasicBlock* switch_block,
7133 HBasicBlock* default_block) {
7134 // Create a jump table.
7135 std::vector<Mips64Label*> labels(num_entries);
7136 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7137 for (uint32_t i = 0; i < num_entries; i++) {
7138 labels[i] = codegen_->GetLabelOf(successors[i]);
7139 }
7140 JumpTable* table = __ CreateJumpTable(std::move(labels));
7141
7142 // Is the value in range?
7143 __ Addiu32(TMP, value_reg, -lower_bound);
7144 __ LoadConst32(AT, num_entries);
7145 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7146
7147 // We are in the range of the table.
7148 // Load the target address from the jump table, indexing by the value.
7149 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007150 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007151 __ Lw(TMP, TMP, 0);
7152 // Compute the absolute target address by adding the table start address
7153 // (the table contains offsets to targets relative to its start).
7154 __ Daddu(TMP, TMP, AT);
7155 // And jump.
7156 __ Jr(TMP);
7157 __ Nop();
7158}
7159
7160void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7161 int32_t lower_bound = switch_instr->GetStartValue();
7162 uint32_t num_entries = switch_instr->GetNumEntries();
7163 LocationSummary* locations = switch_instr->GetLocations();
7164 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7165 HBasicBlock* switch_block = switch_instr->GetBlock();
7166 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7167
7168 if (num_entries > kPackedSwitchJumpTableThreshold) {
7169 GenTableBasedPackedSwitch(value_reg,
7170 lower_bound,
7171 num_entries,
7172 switch_block,
7173 default_block);
7174 } else {
7175 GenPackedSwitchWithCompares(value_reg,
7176 lower_bound,
7177 num_entries,
7178 switch_block,
7179 default_block);
7180 }
7181}
7182
Chris Larsenc9905a62017-03-13 17:06:18 -07007183void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7184 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007185 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007186 locations->SetInAt(0, Location::RequiresRegister());
7187 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007188}
7189
Chris Larsenc9905a62017-03-13 17:06:18 -07007190void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7191 LocationSummary* locations = instruction->GetLocations();
7192 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7193 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7194 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7195 __ LoadFromOffset(kLoadDoubleword,
7196 locations->Out().AsRegister<GpuRegister>(),
7197 locations->InAt(0).AsRegister<GpuRegister>(),
7198 method_offset);
7199 } else {
7200 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7201 instruction->GetIndex(), kMips64PointerSize));
7202 __ LoadFromOffset(kLoadDoubleword,
7203 locations->Out().AsRegister<GpuRegister>(),
7204 locations->InAt(0).AsRegister<GpuRegister>(),
7205 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7206 __ LoadFromOffset(kLoadDoubleword,
7207 locations->Out().AsRegister<GpuRegister>(),
7208 locations->Out().AsRegister<GpuRegister>(),
7209 method_offset);
7210 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007211}
7212
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007213void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7214 ATTRIBUTE_UNUSED) {
7215 LOG(FATAL) << "Unreachable";
7216}
7217
7218void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7219 ATTRIBUTE_UNUSED) {
7220 LOG(FATAL) << "Unreachable";
7221}
7222
Alexey Frunze4dda3372015-06-01 18:31:49 -07007223} // namespace mips64
7224} // namespace art